org.el 731 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569
  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 "PlantUML" plantuml)
  154. (const :tag "Python" python)
  155. (const :tag "Ruby" ruby)
  156. (const :tag "Sass" sass)
  157. (const :tag "Screen" screen)
  158. (const :tag "Shell Script" sh)
  159. (const :tag "Sql" sql)
  160. (const :tag "Sqlite" sqlite))
  161. :value-type (boolean :tag "Activate" :value t)))
  162. ;;;; Customization variables
  163. (defcustom org-clone-delete-id nil
  164. "Remove ID property of clones of a subtree.
  165. When non-nil, clones of a subtree don't inherit the ID property.
  166. Otherwise they inherit the ID property with a new unique
  167. identifier."
  168. :type 'boolean
  169. :group 'org-id)
  170. ;;; Version
  171. (defconst org-version "7.01trans"
  172. "The version number of the file org.el.")
  173. (defun org-version (&optional here)
  174. "Show the org-mode version in the echo area.
  175. With prefix arg HERE, insert it at point."
  176. (interactive "P")
  177. (let* ((origin default-directory)
  178. (version org-version)
  179. (git-version)
  180. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  181. (when (and (file-exists-p (expand-file-name ".git" dir))
  182. (executable-find "git"))
  183. (unwind-protect
  184. (progn
  185. (cd dir)
  186. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  187. (with-current-buffer "*Shell Command Output*"
  188. (goto-char (point-min))
  189. (setq git-version (buffer-substring (point) (point-at-eol))))
  190. (subst-char-in-string ?- ?. git-version t)
  191. (when (string-match "\\S-"
  192. (shell-command-to-string
  193. "git diff-index --name-only HEAD --"))
  194. (setq git-version (concat git-version ".dirty")))
  195. (setq version (concat version " (" git-version ")"))))
  196. (cd origin)))
  197. (setq version (format "Org-mode version %s" version))
  198. (if here (insert version))
  199. (message version)))
  200. ;;; Compatibility constants
  201. ;;; The custom variables
  202. (defgroup org nil
  203. "Outline-based notes management and organizer."
  204. :tag "Org"
  205. :group 'outlines
  206. :group 'calendar)
  207. (defcustom org-mode-hook nil
  208. "Mode hook for Org-mode, run after the mode was turned on."
  209. :group 'org
  210. :type 'hook)
  211. (defcustom org-load-hook nil
  212. "Hook that is run after org.el has been loaded."
  213. :group 'org
  214. :type 'hook)
  215. (defvar org-modules) ; defined below
  216. (defvar org-modules-loaded nil
  217. "Have the modules been loaded already?")
  218. (defun org-load-modules-maybe (&optional force)
  219. "Load all extensions listed in `org-modules'."
  220. (when (or force (not org-modules-loaded))
  221. (mapc (lambda (ext)
  222. (condition-case nil (require ext)
  223. (error (message "Problems while trying to load feature `%s'" ext))))
  224. org-modules)
  225. (setq org-modules-loaded t)))
  226. (defun org-set-modules (var value)
  227. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  228. (set var value)
  229. (when (featurep 'org)
  230. (org-load-modules-maybe 'force)))
  231. (when (org-bound-and-true-p org-modules)
  232. (let ((a (member 'org-infojs org-modules)))
  233. (and a (setcar a 'org-jsinfo))))
  234. (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)
  235. "Modules that should always be loaded together with org.el.
  236. If a description starts with <C>, the file is not part of Emacs
  237. and loading it will require that you have downloaded and properly installed
  238. the org-mode distribution.
  239. You can also use this system to load external packages (i.e. neither Org
  240. core modules, nor modules from the CONTRIB directory). Just add symbols
  241. to the end of the list. If the package is called org-xyz.el, then you need
  242. to add the symbol `xyz', and the package must have a call to
  243. (provide 'org-xyz)"
  244. :group 'org
  245. :set 'org-set-modules
  246. :type
  247. '(set :greedy t
  248. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  249. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  250. (const :tag " crypt: Encryption of subtrees" org-crypt)
  251. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  252. (const :tag " docview: Links to doc-view buffers" org-docview)
  253. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  254. (const :tag " id: Global IDs for identifying entries" org-id)
  255. (const :tag " info: Links to Info nodes" org-info)
  256. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  257. (const :tag " habit: Track your consistency with habits" org-habit)
  258. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  259. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  260. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  261. (const :tag " mew Links to Mew folders/messages" org-mew)
  262. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  263. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  264. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  265. (const :tag " vm: Links to VM folders/messages" org-vm)
  266. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  267. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  268. (const :tag " mouse: Additional mouse support" org-mouse)
  269. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  270. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  271. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  272. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  273. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  274. (const :tag "C collector: Collect properties into tables" org-collector)
  275. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  276. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  277. (const :tag "C eval: Include command output as text" org-eval)
  278. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  279. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  280. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  281. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  282. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  283. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  284. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  285. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  286. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  287. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  288. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  289. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  290. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  291. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  292. (const :tag "C registry: A registry for Org-mode links" org-registry)
  293. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  294. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  295. (const :tag "C secretary: Team management with org-mode" org-secretary)
  296. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  297. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  298. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  299. (const :tag "C track: Keep up with Org-mode development" org-track)
  300. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  301. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  302. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  303. (defcustom org-support-shift-select nil
  304. "Non-nil means make shift-cursor commands select text when possible.
  305. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  306. selecting a region, or enlarge regions started in this way.
  307. In Org-mode, in special contexts, these same keys are used for other
  308. purposes, important enough to compete with shift selection. Org tries
  309. to balance these needs by supporting `shift-select-mode' outside these
  310. special contexts, under control of this variable.
  311. The default of this variable is nil, to avoid confusing behavior. Shifted
  312. cursor keys will then execute Org commands in the following contexts:
  313. - on a headline, changing TODO state (left/right) and priority (up/down)
  314. - on a time stamp, changing the time
  315. - in a plain list item, changing the bullet type
  316. - in a property definition line, switching between allowed values
  317. - in the BEGIN line of a clock table (changing the time block).
  318. Outside these contexts, the commands will throw an error.
  319. When this variable is t and the cursor is not in a special context,
  320. Org-mode will support shift-selection for making and enlarging regions.
  321. To make this more effective, the bullet cycling will no longer happen
  322. anywhere in an item line, but only if the cursor is exactly on the bullet.
  323. If you set this variable to the symbol `always', then the keys
  324. will not be special in headlines, property lines, and item lines, to make
  325. shift selection work there as well. If this is what you want, you can
  326. use the following alternative commands: `C-c C-t' and `C-c ,' to
  327. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  328. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  329. edited by hand or in column view.
  330. However, when the cursor is on a timestamp, shift-cursor commands
  331. will still edit the time stamp - this is just too good to give up.
  332. XEmacs user should have this variable set to nil, because shift-select-mode
  333. is Emacs 23 only."
  334. :group 'org
  335. :type '(choice
  336. (const :tag "Never" nil)
  337. (const :tag "When outside special context" t)
  338. (const :tag "Everywhere except timestamps" always)))
  339. (defgroup org-startup nil
  340. "Options concerning startup of Org-mode."
  341. :tag "Org Startup"
  342. :group 'org)
  343. (defcustom org-startup-folded t
  344. "Non-nil means entering Org-mode will switch to OVERVIEW.
  345. This can also be configured on a per-file basis by adding one of
  346. the following lines anywhere in the buffer:
  347. #+STARTUP: fold (or `overview', this is equivalent)
  348. #+STARTUP: nofold (or `showall', this is equivalent)
  349. #+STARTUP: content
  350. #+STARTUP: showeverything"
  351. :group 'org-startup
  352. :type '(choice
  353. (const :tag "nofold: show all" nil)
  354. (const :tag "fold: overview" t)
  355. (const :tag "content: all headlines" content)
  356. (const :tag "show everything, even drawers" showeverything)))
  357. (defcustom org-startup-truncated t
  358. "Non-nil means entering Org-mode will set `truncate-lines'.
  359. This is useful since some lines containing links can be very long and
  360. uninteresting. Also tables look terrible when wrapped."
  361. :group 'org-startup
  362. :type 'boolean)
  363. (defcustom org-startup-indented nil
  364. "Non-nil means turn on `org-indent-mode' on startup.
  365. This can also be configured on a per-file basis by adding one of
  366. the following lines anywhere in the buffer:
  367. #+STARTUP: indent
  368. #+STARTUP: noindent"
  369. :group 'org-structure
  370. :type '(choice
  371. (const :tag "Not" nil)
  372. (const :tag "Globally (slow on startup in large files)" t)))
  373. (defcustom org-use-sub-superscripts t
  374. "Non-nil means interpret \"_\" and \"^\" for export.
  375. When this option is turned on, you can use TeX-like syntax for sub- and
  376. superscripts. Several characters after \"_\" or \"^\" will be
  377. considered as a single item - so grouping with {} is normally not
  378. needed. For example, the following things will be parsed as single
  379. sub- or superscripts.
  380. 10^24 or 10^tau several digits will be considered 1 item.
  381. 10^-12 or 10^-tau a leading sign with digits or a word
  382. x^2-y^3 will be read as x^2 - y^3, because items are
  383. terminated by almost any nonword/nondigit char.
  384. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  385. Still, ambiguity is possible - so when in doubt use {} to enclose the
  386. sub/superscript. If you set this variable to the symbol `{}',
  387. the braces are *required* in order to trigger interpretations as
  388. sub/superscript. This can be helpful in documents that need \"_\"
  389. frequently in plain text.
  390. Not all export backends support this, but HTML does.
  391. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  392. :group 'org-startup
  393. :group 'org-export-translation
  394. :type '(choice
  395. (const :tag "Always interpret" t)
  396. (const :tag "Only with braces" {})
  397. (const :tag "Never interpret" nil)))
  398. (if (fboundp 'defvaralias)
  399. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  400. (defcustom org-startup-with-beamer-mode nil
  401. "Non-nil means turn on `org-beamer-mode' on startup.
  402. This can also be configured on a per-file basis by adding one of
  403. the following lines anywhere in the buffer:
  404. #+STARTUP: beamer"
  405. :group 'org-startup
  406. :type 'boolean)
  407. (defcustom org-startup-align-all-tables nil
  408. "Non-nil means align all tables when visiting a file.
  409. This is useful when the column width in tables is forced with <N> cookies
  410. in table fields. Such tables will look correct only after the first re-align.
  411. This can also be configured on a per-file basis by adding one of
  412. the following lines anywhere in the buffer:
  413. #+STARTUP: align
  414. #+STARTUP: noalign"
  415. :group 'org-startup
  416. :type 'boolean)
  417. (defcustom org-insert-mode-line-in-empty-file nil
  418. "Non-nil means insert the first line setting Org-mode in empty files.
  419. When the function `org-mode' is called interactively in an empty file, this
  420. normally means that the file name does not automatically trigger Org-mode.
  421. To ensure that the file will always be in Org-mode in the future, a
  422. line enforcing Org-mode will be inserted into the buffer, if this option
  423. has been set."
  424. :group 'org-startup
  425. :type 'boolean)
  426. (defcustom org-replace-disputed-keys nil
  427. "Non-nil means use alternative key bindings for some keys.
  428. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  429. These keys are also used by other packages like shift-selection-mode'
  430. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  431. If you want to use Org-mode together with one of these other modes,
  432. or more generally if you would like to move some Org-mode commands to
  433. other keys, set this variable and configure the keys with the variable
  434. `org-disputed-keys'.
  435. This option is only relevant at load-time of Org-mode, and must be set
  436. *before* org.el is loaded. Changing it requires a restart of Emacs to
  437. become effective."
  438. :group 'org-startup
  439. :type 'boolean)
  440. (defcustom org-use-extra-keys nil
  441. "Non-nil means use extra key sequence definitions for certain commands.
  442. This happens automatically if you run XEmacs or if `window-system'
  443. is nil. This variable lets you do the same manually. You must
  444. set it before loading org.
  445. Example: on Carbon Emacs 22 running graphically, with an external
  446. keyboard on a Powerbook, the default way of setting M-left might
  447. not work for either Alt or ESC. Setting this variable will make
  448. it work for ESC."
  449. :group 'org-startup
  450. :type 'boolean)
  451. (if (fboundp 'defvaralias)
  452. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  453. (defcustom org-disputed-keys
  454. '(([(shift up)] . [(meta p)])
  455. ([(shift down)] . [(meta n)])
  456. ([(shift left)] . [(meta -)])
  457. ([(shift right)] . [(meta +)])
  458. ([(control shift right)] . [(meta shift +)])
  459. ([(control shift left)] . [(meta shift -)]))
  460. "Keys for which Org-mode and other modes compete.
  461. This is an alist, cars are the default keys, second element specifies
  462. the alternative to use when `org-replace-disputed-keys' is t.
  463. Keys can be specified in any syntax supported by `define-key'.
  464. The value of this option takes effect only at Org-mode's startup,
  465. therefore you'll have to restart Emacs to apply it after changing."
  466. :group 'org-startup
  467. :type 'alist)
  468. (defun org-key (key)
  469. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  470. Or return the original if not disputed.
  471. Also apply the translations defined in `org-xemacs-key-equivalents'."
  472. (when org-replace-disputed-keys
  473. (let* ((nkey (key-description key))
  474. (x (org-find-if (lambda (x)
  475. (equal (key-description (car x)) nkey))
  476. org-disputed-keys)))
  477. (setq key (if x (cdr x) key))))
  478. (when (featurep 'xemacs)
  479. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  480. key)
  481. (defun org-find-if (predicate seq)
  482. (catch 'exit
  483. (while seq
  484. (if (funcall predicate (car seq))
  485. (throw 'exit (car seq))
  486. (pop seq)))))
  487. (defun org-defkey (keymap key def)
  488. "Define a key, possibly translated, as returned by `org-key'."
  489. (define-key keymap (org-key key) def))
  490. (defcustom org-ellipsis nil
  491. "The ellipsis to use in the Org-mode outline.
  492. When nil, just use the standard three dots. When a string, use that instead,
  493. When a face, use the standard 3 dots, but with the specified face.
  494. The change affects only Org-mode (which will then use its own display table).
  495. Changing this requires executing `M-x org-mode' in a buffer to become
  496. effective."
  497. :group 'org-startup
  498. :type '(choice (const :tag "Default" nil)
  499. (face :tag "Face" :value org-warning)
  500. (string :tag "String" :value "...#")))
  501. (defvar org-display-table nil
  502. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  503. (defgroup org-keywords nil
  504. "Keywords in Org-mode."
  505. :tag "Org Keywords"
  506. :group 'org)
  507. (defcustom org-deadline-string "DEADLINE:"
  508. "String to mark deadline entries.
  509. A deadline is this string, followed by a time stamp. Should be a word,
  510. terminated by a colon. You can insert a schedule keyword and
  511. a timestamp with \\[org-deadline].
  512. Changes become only effective after restarting Emacs."
  513. :group 'org-keywords
  514. :type 'string)
  515. (defcustom org-scheduled-string "SCHEDULED:"
  516. "String to mark scheduled TODO entries.
  517. A schedule is this string, followed by a time stamp. Should be a word,
  518. terminated by a colon. You can insert a schedule keyword and
  519. a timestamp with \\[org-schedule].
  520. Changes become only effective after restarting Emacs."
  521. :group 'org-keywords
  522. :type 'string)
  523. (defcustom org-closed-string "CLOSED:"
  524. "String used as the prefix for timestamps logging closing a TODO entry."
  525. :group 'org-keywords
  526. :type 'string)
  527. (defcustom org-clock-string "CLOCK:"
  528. "String used as prefix for timestamps clocking work hours on an item."
  529. :group 'org-keywords
  530. :type 'string)
  531. (defcustom org-comment-string "COMMENT"
  532. "Entries starting with this keyword will never be exported.
  533. An entry can be toggled between COMMENT and normal with
  534. \\[org-toggle-comment].
  535. Changes become only effective after restarting Emacs."
  536. :group 'org-keywords
  537. :type 'string)
  538. (defcustom org-quote-string "QUOTE"
  539. "Entries starting with this keyword will be exported in fixed-width font.
  540. Quoting applies only to the text in the entry following the headline, and does
  541. not extend beyond the next headline, even if that is lower level.
  542. An entry can be toggled between QUOTE and normal with
  543. \\[org-toggle-fixed-width-section]."
  544. :group 'org-keywords
  545. :type 'string)
  546. (defconst org-repeat-re
  547. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  548. "Regular expression for specifying repeated events.
  549. After a match, group 1 contains the repeat expression.")
  550. (defgroup org-structure nil
  551. "Options concerning the general structure of Org-mode files."
  552. :tag "Org Structure"
  553. :group 'org)
  554. (defgroup org-reveal-location nil
  555. "Options about how to make context of a location visible."
  556. :tag "Org Reveal Location"
  557. :group 'org-structure)
  558. (defconst org-context-choice
  559. '(choice
  560. (const :tag "Always" t)
  561. (const :tag "Never" nil)
  562. (repeat :greedy t :tag "Individual contexts"
  563. (cons
  564. (choice :tag "Context"
  565. (const agenda)
  566. (const org-goto)
  567. (const occur-tree)
  568. (const tags-tree)
  569. (const link-search)
  570. (const mark-goto)
  571. (const bookmark-jump)
  572. (const isearch)
  573. (const default))
  574. (boolean))))
  575. "Contexts for the reveal options.")
  576. (defcustom org-show-hierarchy-above '((default . t))
  577. "Non-nil means show full hierarchy when revealing a location.
  578. Org-mode often shows locations in an org-mode file which might have
  579. been invisible before. When this is set, the hierarchy of headings
  580. above the exposed location is shown.
  581. Turning this off for example for sparse trees makes them very compact.
  582. Instead of t, this can also be an alist specifying this option for different
  583. contexts. Valid contexts are
  584. agenda when exposing an entry from the agenda
  585. org-goto when using the command `org-goto' on key C-c C-j
  586. occur-tree when using the command `org-occur' on key C-c /
  587. tags-tree when constructing a sparse tree based on tags matches
  588. link-search when exposing search matches associated with a link
  589. mark-goto when exposing the jump goal of a mark
  590. bookmark-jump when exposing a bookmark location
  591. isearch when exiting from an incremental search
  592. default default for all contexts not set explicitly"
  593. :group 'org-reveal-location
  594. :type org-context-choice)
  595. (defcustom org-show-following-heading '((default . nil))
  596. "Non-nil means show following heading when revealing a location.
  597. Org-mode often shows locations in an org-mode file which might have
  598. been invisible before. When this is set, the heading following the
  599. match is shown.
  600. Turning this off for example for sparse trees makes them very compact,
  601. but makes it harder to edit the location of the match. In such a case,
  602. use the command \\[org-reveal] to show more context.
  603. Instead of t, this can also be an alist specifying this option for different
  604. contexts. See `org-show-hierarchy-above' for valid contexts."
  605. :group 'org-reveal-location
  606. :type org-context-choice)
  607. (defcustom org-show-siblings '((default . nil) (isearch t))
  608. "Non-nil means show all sibling heading when revealing a location.
  609. Org-mode often shows locations in an org-mode file which might have
  610. been invisible before. When this is set, the sibling of the current entry
  611. heading are all made visible. If `org-show-hierarchy-above' is t,
  612. the same happens on each level of the hierarchy above the current entry.
  613. By default this is on for the isearch context, off for all other contexts.
  614. Turning this off for example for sparse trees makes them very compact,
  615. but makes it harder to edit the location of the match. In such a case,
  616. use the command \\[org-reveal] to show more context.
  617. Instead of t, this can also be an alist specifying this option for different
  618. contexts. See `org-show-hierarchy-above' for valid contexts."
  619. :group 'org-reveal-location
  620. :type org-context-choice)
  621. (defcustom org-show-entry-below '((default . nil))
  622. "Non-nil means show the entry below a headline when revealing a location.
  623. Org-mode often shows locations in an org-mode file which might have
  624. been invisible before. When this is set, the text below the headline that is
  625. exposed is also shown.
  626. By default this is off for all contexts.
  627. Instead of t, this can also be an alist specifying this option for different
  628. contexts. See `org-show-hierarchy-above' for valid contexts."
  629. :group 'org-reveal-location
  630. :type org-context-choice)
  631. (defcustom org-indirect-buffer-display 'other-window
  632. "How should indirect tree buffers be displayed?
  633. This applies to indirect buffers created with the commands
  634. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  635. Valid values are:
  636. current-window Display in the current window
  637. other-window Just display in another window.
  638. dedicated-frame Create one new frame, and re-use it each time.
  639. new-frame Make a new frame each time. Note that in this case
  640. previously-made indirect buffers are kept, and you need to
  641. kill these buffers yourself."
  642. :group 'org-structure
  643. :group 'org-agenda-windows
  644. :type '(choice
  645. (const :tag "In current window" current-window)
  646. (const :tag "In current frame, other window" other-window)
  647. (const :tag "Each time a new frame" new-frame)
  648. (const :tag "One dedicated frame" dedicated-frame)))
  649. (defcustom org-use-speed-commands nil
  650. "Non-nil means activate single letter commands at beginning of a headline.
  651. This may also be a function to test for appropriate locations where speed
  652. commands should be active."
  653. :group 'org-structure
  654. :type '(choice
  655. (const :tag "Never" nil)
  656. (const :tag "At beginning of headline stars" t)
  657. (function)))
  658. (defcustom org-speed-commands-user nil
  659. "Alist of additional speed commands.
  660. This list will be checked before `org-speed-commands-default'
  661. when the variable `org-use-speed-commands' is non-nil
  662. and when the cursor is at the beginning of a headline.
  663. The car if each entry is a string with a single letter, which must
  664. be assigned to `self-insert-command' in the global map.
  665. The cdr is either a command to be called interactively, a function
  666. to be called, or a form to be evaluated.
  667. An entry that is just a list with a single string will be interpreted
  668. as a descriptive headline that will be added when listing the speed
  669. commands in the Help buffer using the `?' speed command."
  670. :group 'org-structure
  671. :type '(repeat :value ("k" . ignore)
  672. (choice :value ("k" . ignore)
  673. (list :tag "Descriptive Headline" (string :tag "Headline"))
  674. (cons :tag "Letter and Command"
  675. (string :tag "Command letter")
  676. (choice
  677. (function)
  678. (sexp))))))
  679. (defgroup org-cycle nil
  680. "Options concerning visibility cycling in Org-mode."
  681. :tag "Org Cycle"
  682. :group 'org-structure)
  683. (defcustom org-cycle-skip-children-state-if-no-children t
  684. "Non-nil means skip CHILDREN state in entries that don't have any."
  685. :group 'org-cycle
  686. :type 'boolean)
  687. (defcustom org-cycle-max-level nil
  688. "Maximum level which should still be subject to visibility cycling.
  689. Levels higher than this will, for cycling, be treated as text, not a headline.
  690. When `org-odd-levels-only' is set, a value of N in this variable actually
  691. means 2N-1 stars as the limiting headline.
  692. When nil, cycle all levels.
  693. Note that the limiting level of cycling is also influenced by
  694. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  695. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  696. than its value."
  697. :group 'org-cycle
  698. :type '(choice
  699. (const :tag "No limit" nil)
  700. (integer :tag "Maximum level")))
  701. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  702. "Names of drawers. Drawers are not opened by cycling on the headline above.
  703. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  704. this:
  705. :DRAWERNAME:
  706. .....
  707. :END:
  708. The drawer \"PROPERTIES\" is special for capturing properties through
  709. the property API.
  710. Drawers can be defined on the per-file basis with a line like:
  711. #+DRAWERS: HIDDEN STATE PROPERTIES"
  712. :group 'org-structure
  713. :group 'org-cycle
  714. :type '(repeat (string :tag "Drawer Name")))
  715. (defcustom org-hide-block-startup nil
  716. "Non-nil means entering Org-mode will fold all blocks.
  717. This can also be set in on a per-file basis with
  718. #+STARTUP: hideblocks
  719. #+STARTUP: showblocks"
  720. :group 'org-startup
  721. :group 'org-cycle
  722. :type 'boolean)
  723. (defcustom org-cycle-global-at-bob nil
  724. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  725. This makes it possible to do global cycling without having to use S-TAB or
  726. \\[universal-argument] TAB. For this special case to work, the first line \
  727. of the buffer
  728. must not be a headline - it may be empty or some other text. When used in
  729. this way, `org-cycle-hook' is disables temporarily, to make sure the
  730. cursor stays at the beginning of the buffer.
  731. When this option is nil, don't do anything special at the beginning
  732. of the buffer."
  733. :group 'org-cycle
  734. :type 'boolean)
  735. (defcustom org-cycle-level-after-item/entry-creation t
  736. "Non-nil means cycle entry level or item indentation in new empty entries.
  737. When the cursor is at the end of an empty headline, i.e with only stars
  738. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  739. and then all possible ancestor states, before returning to the original state.
  740. This makes data entry extremely fast: M-RET to create a new headline,
  741. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  742. When the cursor is at the end of an empty plain list item, one TAB will
  743. make it a subitem, two or more tabs will back up to make this an item
  744. higher up in the item hierarchy."
  745. :group 'org-cycle
  746. :type 'boolean)
  747. (defcustom org-cycle-emulate-tab t
  748. "Where should `org-cycle' emulate TAB.
  749. nil Never
  750. white Only in completely white lines
  751. whitestart Only at the beginning of lines, before the first non-white char
  752. t Everywhere except in headlines
  753. exc-hl-bol Everywhere except at the start of a headline
  754. If TAB is used in a place where it does not emulate TAB, the current subtree
  755. visibility is cycled."
  756. :group 'org-cycle
  757. :type '(choice (const :tag "Never" nil)
  758. (const :tag "Only in completely white lines" white)
  759. (const :tag "Before first char in a line" whitestart)
  760. (const :tag "Everywhere except in headlines" t)
  761. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  762. ))
  763. (defcustom org-cycle-separator-lines 2
  764. "Number of empty lines needed to keep an empty line between collapsed trees.
  765. If you leave an empty line between the end of a subtree and the following
  766. headline, this empty line is hidden when the subtree is folded.
  767. Org-mode will leave (exactly) one empty line visible if the number of
  768. empty lines is equal or larger to the number given in this variable.
  769. So the default 2 means at least 2 empty lines after the end of a subtree
  770. are needed to produce free space between a collapsed subtree and the
  771. following headline.
  772. If the number is negative, and the number of empty lines is at least -N,
  773. all empty lines are shown.
  774. Special case: when 0, never leave empty lines in collapsed view."
  775. :group 'org-cycle
  776. :type 'integer)
  777. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  778. (defcustom org-pre-cycle-hook nil
  779. "Hook that is run before visibility cycling is happening.
  780. The function(s) in this hook must accept a single argument which indicates
  781. the new state that will be set right after running this hook. The
  782. argument is a symbol. Before a global state change, it can have the values
  783. `overview', `content', or `all'. Before a local state change, it can have
  784. the values `folded', `children', or `subtree'."
  785. :group 'org-cycle
  786. :type 'hook)
  787. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  788. org-cycle-hide-drawers
  789. org-cycle-show-empty-lines
  790. org-optimize-window-after-visibility-change)
  791. "Hook that is run after `org-cycle' has changed the buffer visibility.
  792. The function(s) in this hook must accept a single argument which indicates
  793. the new state that was set by the most recent `org-cycle' command. The
  794. argument is a symbol. After a global state change, it can have the values
  795. `overview', `content', or `all'. After a local state change, it can have
  796. the values `folded', `children', or `subtree'."
  797. :group 'org-cycle
  798. :type 'hook)
  799. (defgroup org-edit-structure nil
  800. "Options concerning structure editing in Org-mode."
  801. :tag "Org Edit Structure"
  802. :group 'org-structure)
  803. (defcustom org-odd-levels-only nil
  804. "Non-nil means skip even levels and only use odd levels for the outline.
  805. This has the effect that two stars are being added/taken away in
  806. promotion/demotion commands. It also influences how levels are
  807. handled by the exporters.
  808. Changing it requires restart of `font-lock-mode' to become effective
  809. for fontification also in regions already fontified.
  810. You may also set this on a per-file basis by adding one of the following
  811. lines to the buffer:
  812. #+STARTUP: odd
  813. #+STARTUP: oddeven"
  814. :group 'org-edit-structure
  815. :group 'org-appearance
  816. :type 'boolean)
  817. (defcustom org-adapt-indentation t
  818. "Non-nil means adapt indentation to outline node level.
  819. When this variable is set, Org assumes that you write outlines by
  820. indenting text in each node to align with the headline (after the stars).
  821. The following issues are influenced by this variable:
  822. - When this is set and the *entire* text in an entry is indented, the
  823. indentation is increased by one space in a demotion command, and
  824. decreased by one in a promotion command. If any line in the entry
  825. body starts with text at column 0, indentation is not changed at all.
  826. - Property drawers and planning information is inserted indented when
  827. this variable s set. When nil, they will not be indented.
  828. - TAB indents a line relative to context. The lines below a headline
  829. will be indented when this variable is set.
  830. Note that this is all about true indentation, by adding and removing
  831. space characters. See also `org-indent.el' which does level-dependent
  832. indentation in a virtual way, i.e. at display time in Emacs."
  833. :group 'org-edit-structure
  834. :type 'boolean)
  835. (defcustom org-special-ctrl-a/e nil
  836. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  837. When t, `C-a' will bring back the cursor to the beginning of the
  838. headline text, i.e. after the stars and after a possible TODO keyword.
  839. In an item, this will be the position after the bullet.
  840. When the cursor is already at that position, another `C-a' will bring
  841. it to the beginning of the line.
  842. `C-e' will jump to the end of the headline, ignoring the presence of tags
  843. in the headline. A second `C-e' will then jump to the true end of the
  844. line, after any tags. This also means that, when this variable is
  845. non-nil, `C-e' also will never jump beyond the end of the heading of a
  846. folded section, i.e. not after the ellipses.
  847. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  848. going to the true line boundary first. Only a directly following, identical
  849. keypress will bring the cursor to the special positions.
  850. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  851. set separately."
  852. :group 'org-edit-structure
  853. :type '(choice
  854. (const :tag "off" nil)
  855. (const :tag "on: after stars/bullet and before tags first" t)
  856. (const :tag "reversed: true line boundary first" reversed)
  857. (cons :tag "Set C-a and C-e separately"
  858. (choice :tag "Special C-a"
  859. (const :tag "off" nil)
  860. (const :tag "on: after stars/bullet first" t)
  861. (const :tag "reversed: before stars/bullet first" reversed))
  862. (choice :tag "Special C-e"
  863. (const :tag "off" nil)
  864. (const :tag "on: before tags first" t)
  865. (const :tag "reversed: after tags first" reversed)))))
  866. (if (fboundp 'defvaralias)
  867. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  868. (defcustom org-special-ctrl-k nil
  869. "Non-nil means `C-k' will behave specially in headlines.
  870. When nil, `C-k' will call the default `kill-line' command.
  871. When t, the following will happen while the cursor is in the headline:
  872. - When the cursor is at the beginning of a headline, kill the entire
  873. line and possible the folded subtree below the line.
  874. - When in the middle of the headline text, kill the headline up to the tags.
  875. - When after the headline text, kill the tags."
  876. :group 'org-edit-structure
  877. :type 'boolean)
  878. (defcustom org-ctrl-k-protect-subtree nil
  879. "Non-nil means, do not delete a hidden subtree with C-k.
  880. When set to the symbol `error', simply throw an error when C-k is
  881. used to kill (part-of) a headline that has hidden text behind it.
  882. Any other non-nil value will result in a query to the user, if it is
  883. OK to kill that hidden subtree. When nil, kill without remorse."
  884. :group 'org-edit-structure
  885. :type '(choice
  886. (const :tag "Do not protect hidden subtrees" nil)
  887. (const :tag "Protect hidden subtrees with a security query" t)
  888. (const :tag "Never kill a hidden subtree with C-k" error)))
  889. (defcustom org-yank-folded-subtrees t
  890. "Non-nil means when yanking subtrees, fold them.
  891. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  892. it starts with a heading and all other headings in it are either children
  893. or siblings, then fold all the subtrees. However, do this only if no
  894. text after the yank would be swallowed into a folded tree by this action."
  895. :group 'org-edit-structure
  896. :type 'boolean)
  897. (defcustom org-yank-adjusted-subtrees nil
  898. "Non-nil means when yanking subtrees, adjust the level.
  899. With this setting, `org-paste-subtree' is used to insert the subtree, see
  900. this function for details."
  901. :group 'org-edit-structure
  902. :type 'boolean)
  903. (defcustom org-M-RET-may-split-line '((default . t))
  904. "Non-nil means M-RET will split the line at the cursor position.
  905. When nil, it will go to the end of the line before making a
  906. new line.
  907. You may also set this option in a different way for different
  908. contexts. Valid contexts are:
  909. headline when creating a new headline
  910. item when creating a new item
  911. table in a table field
  912. default the value to be used for all contexts not explicitly
  913. customized"
  914. :group 'org-structure
  915. :group 'org-table
  916. :type '(choice
  917. (const :tag "Always" t)
  918. (const :tag "Never" nil)
  919. (repeat :greedy t :tag "Individual contexts"
  920. (cons
  921. (choice :tag "Context"
  922. (const headline)
  923. (const item)
  924. (const table)
  925. (const default))
  926. (boolean)))))
  927. (defcustom org-insert-heading-respect-content nil
  928. "Non-nil means insert new headings after the current subtree.
  929. When nil, the new heading is created directly after the current line.
  930. The commands \\[org-insert-heading-respect-content] and
  931. \\[org-insert-todo-heading-respect-content] turn this variable on
  932. for the duration of the command."
  933. :group 'org-structure
  934. :type 'boolean)
  935. (defcustom org-blank-before-new-entry '((heading . auto)
  936. (plain-list-item . auto))
  937. "Should `org-insert-heading' leave a blank line before new heading/item?
  938. The value is an alist, with `heading' and `plain-list-item' as car,
  939. and a boolean flag as cdr. For plain lists, if the variable
  940. `org-empty-line-terminates-plain-lists' is set, the setting here
  941. is ignored and no empty line is inserted, to keep the list in tact."
  942. :group 'org-edit-structure
  943. :type '(list
  944. (cons (const heading)
  945. (choice (const :tag "Never" nil)
  946. (const :tag "Always" t)
  947. (const :tag "Auto" auto)))
  948. (cons (const plain-list-item)
  949. (choice (const :tag "Never" nil)
  950. (const :tag "Always" t)
  951. (const :tag "Auto" auto)))))
  952. (defcustom org-insert-heading-hook nil
  953. "Hook being run after inserting a new heading."
  954. :group 'org-edit-structure
  955. :type 'hook)
  956. (defcustom org-enable-fixed-width-editor t
  957. "Non-nil means lines starting with \":\" are treated as fixed-width.
  958. This currently only means they are never auto-wrapped.
  959. When nil, such lines will be treated like ordinary lines.
  960. See also the QUOTE keyword."
  961. :group 'org-edit-structure
  962. :type 'boolean)
  963. (defcustom org-goto-auto-isearch t
  964. "Non-nil means typing characters in `org-goto' starts incremental search."
  965. :group 'org-edit-structure
  966. :type 'boolean)
  967. (defgroup org-sparse-trees nil
  968. "Options concerning sparse trees in Org-mode."
  969. :tag "Org Sparse Trees"
  970. :group 'org-structure)
  971. (defcustom org-highlight-sparse-tree-matches t
  972. "Non-nil means highlight all matches that define a sparse tree.
  973. The highlights will automatically disappear the next time the buffer is
  974. changed by an edit command."
  975. :group 'org-sparse-trees
  976. :type 'boolean)
  977. (defcustom org-remove-highlights-with-change t
  978. "Non-nil means any change to the buffer will remove temporary highlights.
  979. Such highlights are created by `org-occur' and `org-clock-display'.
  980. When nil, `C-c C-c needs to be used to get rid of the highlights.
  981. The highlights created by `org-preview-latex-fragment' always need
  982. `C-c C-c' to be removed."
  983. :group 'org-sparse-trees
  984. :group 'org-time
  985. :type 'boolean)
  986. (defcustom org-occur-hook '(org-first-headline-recenter)
  987. "Hook that is run after `org-occur' has constructed a sparse tree.
  988. This can be used to recenter the window to show as much of the structure
  989. as possible."
  990. :group 'org-sparse-trees
  991. :type 'hook)
  992. (defgroup org-imenu-and-speedbar nil
  993. "Options concerning imenu and speedbar in Org-mode."
  994. :tag "Org Imenu and Speedbar"
  995. :group 'org-structure)
  996. (defcustom org-imenu-depth 2
  997. "The maximum level for Imenu access to Org-mode headlines.
  998. This also applied for speedbar access."
  999. :group 'org-imenu-and-speedbar
  1000. :type 'integer)
  1001. (defgroup org-table nil
  1002. "Options concerning tables in Org-mode."
  1003. :tag "Org Table"
  1004. :group 'org)
  1005. (defcustom org-enable-table-editor 'optimized
  1006. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1007. When nil, such lines will be treated like ordinary lines.
  1008. When equal to the symbol `optimized', the table editor will be optimized to
  1009. do the following:
  1010. - Automatic overwrite mode in front of whitespace in table fields.
  1011. This makes the structure of the table stay in tact as long as the edited
  1012. field does not exceed the column width.
  1013. - Minimize the number of realigns. Normally, the table is aligned each time
  1014. TAB or RET are pressed to move to another field. With optimization this
  1015. happens only if changes to a field might have changed the column width.
  1016. Optimization requires replacing the functions `self-insert-command',
  1017. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1018. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1019. very good at guessing when a re-align will be necessary, but you can always
  1020. force one with \\[org-ctrl-c-ctrl-c].
  1021. If you would like to use the optimized version in Org-mode, but the
  1022. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1023. This variable can be used to turn on and off the table editor during a session,
  1024. but in order to toggle optimization, a restart is required.
  1025. See also the variable `org-table-auto-blank-field'."
  1026. :group 'org-table
  1027. :type '(choice
  1028. (const :tag "off" nil)
  1029. (const :tag "on" t)
  1030. (const :tag "on, optimized" optimized)))
  1031. (defcustom org-self-insert-cluster-for-undo t
  1032. "Non-nil means cluster self-insert commands for undo when possible.
  1033. If this is set, then, like in the Emacs command loop, 20 consecutive
  1034. characters will be undone together.
  1035. This is configurable, because there is some impact on typing performance."
  1036. :group 'org-table
  1037. :type 'boolean)
  1038. (defcustom org-table-tab-recognizes-table.el t
  1039. "Non-nil means TAB will automatically notice a table.el table.
  1040. When it sees such a table, it moves point into it and - if necessary -
  1041. calls `table-recognize-table'."
  1042. :group 'org-table-editing
  1043. :type 'boolean)
  1044. (defgroup org-link nil
  1045. "Options concerning links in Org-mode."
  1046. :tag "Org Link"
  1047. :group 'org)
  1048. (defvar org-link-abbrev-alist-local nil
  1049. "Buffer-local version of `org-link-abbrev-alist', which see.
  1050. The value of this is taken from the #+LINK lines.")
  1051. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1052. (defcustom org-link-abbrev-alist nil
  1053. "Alist of link abbreviations.
  1054. The car of each element is a string, to be replaced at the start of a link.
  1055. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1056. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1057. [[linkkey:tag][description]]
  1058. The 'linkkey' must be a word word, starting with a letter, followed
  1059. by letters, numbers, '-' or '_'.
  1060. If REPLACE is a string, the tag will simply be appended to create the link.
  1061. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1062. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1063. be inserted at that point (see the function `url-hexify-string').
  1064. REPLACE may also be a function that will be called with the tag as the
  1065. only argument to create the link, which should be returned as a string.
  1066. See the manual for examples."
  1067. :group 'org-link
  1068. :type '(repeat
  1069. (cons
  1070. (string :tag "Protocol")
  1071. (choice
  1072. (string :tag "Format")
  1073. (function)))))
  1074. (defcustom org-descriptive-links t
  1075. "Non-nil means hide link part and only show description of bracket links.
  1076. Bracket links are like [[link][description]]. This variable sets the initial
  1077. state in new org-mode buffers. The setting can then be toggled on a
  1078. per-buffer basis from the Org->Hyperlinks menu."
  1079. :group 'org-link
  1080. :type 'boolean)
  1081. (defcustom org-link-file-path-type 'adaptive
  1082. "How the path name in file links should be stored.
  1083. Valid values are:
  1084. relative Relative to the current directory, i.e. the directory of the file
  1085. into which the link is being inserted.
  1086. absolute Absolute path, if possible with ~ for home directory.
  1087. noabbrev Absolute path, no abbreviation of home directory.
  1088. adaptive Use relative path for files in the current directory and sub-
  1089. directories of it. For other files, use an absolute path."
  1090. :group 'org-link
  1091. :type '(choice
  1092. (const relative)
  1093. (const absolute)
  1094. (const noabbrev)
  1095. (const adaptive)))
  1096. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1097. "Types of links that should be activated in Org-mode files.
  1098. This is a list of symbols, each leading to the activation of a certain link
  1099. type. In principle, it does not hurt to turn on most link types - there may
  1100. be a small gain when turning off unused link types. The types are:
  1101. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1102. angular Links in angular brackets that may contain whitespace like
  1103. <bbdb:Carsten Dominik>.
  1104. plain Plain links in normal text, no whitespace, like http://google.com.
  1105. radio Text that is matched by a radio target, see manual for details.
  1106. tag Tag settings in a headline (link to tag search).
  1107. date Time stamps (link to calendar).
  1108. footnote Footnote labels.
  1109. Changing this variable requires a restart of Emacs to become effective."
  1110. :group 'org-link
  1111. :type '(set :greedy t
  1112. (const :tag "Double bracket links (new style)" bracket)
  1113. (const :tag "Angular bracket links (old style)" angular)
  1114. (const :tag "Plain text links" plain)
  1115. (const :tag "Radio target matches" radio)
  1116. (const :tag "Tags" tag)
  1117. (const :tag "Timestamps" date)
  1118. (const :tag "Footnotes" footnote)))
  1119. (defcustom org-make-link-description-function nil
  1120. "Function to use to generate link descriptions from links.
  1121. If nil the link location will be used. This function must take
  1122. two parameters; the first is the link and the second the
  1123. description `org-insert-link' has generated, and should return the
  1124. description to use."
  1125. :group 'org-link
  1126. :type 'function)
  1127. (defgroup org-link-store nil
  1128. "Options concerning storing links in Org-mode."
  1129. :tag "Org Store Link"
  1130. :group 'org-link)
  1131. (defcustom org-email-link-description-format "Email %c: %.30s"
  1132. "Format of the description part of a link to an email or usenet message.
  1133. The following %-escapes will be replaced by corresponding information:
  1134. %F full \"From\" field
  1135. %f name, taken from \"From\" field, address if no name
  1136. %T full \"To\" field
  1137. %t first name in \"To\" field, address if no name
  1138. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1139. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1140. %s subject
  1141. %m message-id.
  1142. You may use normal field width specification between the % and the letter.
  1143. This is for example useful to limit the length of the subject.
  1144. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1145. :group 'org-link-store
  1146. :type 'string)
  1147. (defcustom org-from-is-user-regexp
  1148. (let (r1 r2)
  1149. (when (and user-mail-address (not (string= user-mail-address "")))
  1150. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1151. (when (and user-full-name (not (string= user-full-name "")))
  1152. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1153. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1154. "Regexp matched against the \"From:\" header of an email or usenet message.
  1155. It should match if the message is from the user him/herself."
  1156. :group 'org-link-store
  1157. :type 'regexp)
  1158. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1159. "Non-nil means storing a link to an Org file will use entry IDs.
  1160. Note that before this variable is even considered, org-id must be loaded,
  1161. so please customize `org-modules' and turn it on.
  1162. The variable can have the following values:
  1163. t Create an ID if needed to make a link to the current entry.
  1164. create-if-interactive
  1165. If `org-store-link' is called directly (interactively, as a user
  1166. command), do create an ID to support the link. But when doing the
  1167. job for remember, only use the ID if it already exists. The
  1168. purpose of this setting is to avoid proliferation of unwanted
  1169. IDs, just because you happen to be in an Org file when you
  1170. call `org-remember' that automatically and preemptively
  1171. creates a link. If you do want to get an ID link in a remember
  1172. template to an entry not having an ID, create it first by
  1173. explicitly creating a link to it, using `C-c C-l' first.
  1174. create-if-interactive-and-no-custom-id
  1175. Like create-if-interactive, but do not create an ID if there is
  1176. a CUSTOM_ID property defined in the entry. This is the default.
  1177. use-existing
  1178. Use existing ID, do not create one.
  1179. nil Never use an ID to make a link, instead link using a text search for
  1180. the headline text."
  1181. :group 'org-link-store
  1182. :type '(choice
  1183. (const :tag "Create ID to make link" t)
  1184. (const :tag "Create if storing link interactively"
  1185. create-if-interactive)
  1186. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1187. create-if-interactive-and-no-custom-id)
  1188. (const :tag "Only use existing" use-existing)
  1189. (const :tag "Do not use ID to create link" nil)))
  1190. (defcustom org-context-in-file-links t
  1191. "Non-nil means file links from `org-store-link' contain context.
  1192. A search string will be added to the file name with :: as separator and
  1193. used to find the context when the link is activated by the command
  1194. `org-open-at-point'.
  1195. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1196. negates this setting for the duration of the command."
  1197. :group 'org-link-store
  1198. :type 'boolean)
  1199. (defcustom org-keep-stored-link-after-insertion nil
  1200. "Non-nil means keep link in list for entire session.
  1201. The command `org-store-link' adds a link pointing to the current
  1202. location to an internal list. These links accumulate during a session.
  1203. The command `org-insert-link' can be used to insert links into any
  1204. Org-mode file (offering completion for all stored links). When this
  1205. option is nil, every link which has been inserted once using \\[org-insert-link]
  1206. will be removed from the list, to make completing the unused links
  1207. more efficient."
  1208. :group 'org-link-store
  1209. :type 'boolean)
  1210. (defgroup org-link-follow nil
  1211. "Options concerning following links in Org-mode."
  1212. :tag "Org Follow Link"
  1213. :group 'org-link)
  1214. (defcustom org-link-translation-function nil
  1215. "Function to translate links with different syntax to Org syntax.
  1216. This can be used to translate links created for example by the Planner
  1217. or emacs-wiki packages to Org syntax.
  1218. The function must accept two parameters, a TYPE containing the link
  1219. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1220. which is everything after the link protocol. It should return a cons
  1221. with possibly modified values of type and path.
  1222. Org contains a function for this, so if you set this variable to
  1223. `org-translate-link-from-planner', you should be able follow many
  1224. links created by planner."
  1225. :group 'org-link-follow
  1226. :type 'function)
  1227. (defcustom org-follow-link-hook nil
  1228. "Hook that is run after a link has been followed."
  1229. :group 'org-link-follow
  1230. :type 'hook)
  1231. (defcustom org-tab-follows-link nil
  1232. "Non-nil means on links TAB will follow the link.
  1233. Needs to be set before org.el is loaded.
  1234. This really should not be used, it does not make sense, and the
  1235. implementation is bad."
  1236. :group 'org-link-follow
  1237. :type 'boolean)
  1238. (defcustom org-return-follows-link nil
  1239. "Non-nil means on links RET will follow the link."
  1240. :group 'org-link-follow
  1241. :type 'boolean)
  1242. (defcustom org-mouse-1-follows-link
  1243. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1244. "Non-nil means mouse-1 on a link will follow the link.
  1245. A longer mouse click will still set point. Does not work on XEmacs.
  1246. Needs to be set before org.el is loaded."
  1247. :group 'org-link-follow
  1248. :type 'boolean)
  1249. (defcustom org-mark-ring-length 4
  1250. "Number of different positions to be recorded in the ring.
  1251. Changing this requires a restart of Emacs to work correctly."
  1252. :group 'org-link-follow
  1253. :type 'integer)
  1254. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1255. "Non-nil means internal links in Org files must exactly match a headline.
  1256. When nil, the link search tries to match a phrase will all words
  1257. in the search text."
  1258. :group 'org-link-follow
  1259. :type '(choice
  1260. (const :tag "Use fuzy text search" nil)
  1261. (const :tag "Match only exact headline" t)
  1262. (const :tag "Match extact headline or query to create it"
  1263. query-to-create)))
  1264. (defcustom org-link-frame-setup
  1265. '((vm . vm-visit-folder-other-frame)
  1266. (gnus . org-gnus-no-new-news)
  1267. (file . find-file-other-window)
  1268. (wl . wl-other-frame))
  1269. "Setup the frame configuration for following links.
  1270. When following a link with Emacs, it may often be useful to display
  1271. this link in another window or frame. This variable can be used to
  1272. set this up for the different types of links.
  1273. For VM, use any of
  1274. `vm-visit-folder'
  1275. `vm-visit-folder-other-frame'
  1276. For Gnus, use any of
  1277. `gnus'
  1278. `gnus-other-frame'
  1279. `org-gnus-no-new-news'
  1280. For FILE, use any of
  1281. `find-file'
  1282. `find-file-other-window'
  1283. `find-file-other-frame'
  1284. For Wanderlust use any of
  1285. `wl'
  1286. `wl-other-frame'
  1287. For the calendar, use the variable `calendar-setup'.
  1288. For BBDB, it is currently only possible to display the matches in
  1289. another window."
  1290. :group 'org-link-follow
  1291. :type '(list
  1292. (cons (const vm)
  1293. (choice
  1294. (const vm-visit-folder)
  1295. (const vm-visit-folder-other-window)
  1296. (const vm-visit-folder-other-frame)))
  1297. (cons (const gnus)
  1298. (choice
  1299. (const gnus)
  1300. (const gnus-other-frame)
  1301. (const org-gnus-no-new-news)))
  1302. (cons (const file)
  1303. (choice
  1304. (const find-file)
  1305. (const find-file-other-window)
  1306. (const find-file-other-frame)))
  1307. (cons (const wl)
  1308. (choice
  1309. (const wl)
  1310. (const wl-other-frame)))))
  1311. (defcustom org-display-internal-link-with-indirect-buffer nil
  1312. "Non-nil means use indirect buffer to display infile links.
  1313. Activating internal links (from one location in a file to another location
  1314. in the same file) normally just jumps to the location. When the link is
  1315. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1316. is displayed in
  1317. another window. When this option is set, the other window actually displays
  1318. an indirect buffer clone of the current buffer, to avoid any visibility
  1319. changes to the current buffer."
  1320. :group 'org-link-follow
  1321. :type 'boolean)
  1322. (defcustom org-open-non-existing-files nil
  1323. "Non-nil means `org-open-file' will open non-existing files.
  1324. When nil, an error will be generated.
  1325. This variable applies only to external applications because they
  1326. might choke on non-existing files. If the link is to a file that
  1327. will be opened in Emacs, the variable is ignored."
  1328. :group 'org-link-follow
  1329. :type 'boolean)
  1330. (defcustom org-open-directory-means-index-dot-org nil
  1331. "Non-nil means a link to a directory really means to index.org.
  1332. When nil, following a directory link will run dired or open a finder/explorer
  1333. window on that directory."
  1334. :group 'org-link-follow
  1335. :type 'boolean)
  1336. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1337. "Function and arguments to call for following mailto links.
  1338. This is a list with the first element being a Lisp function, and the
  1339. remaining elements being arguments to the function. In string arguments,
  1340. %a will be replaced by the address, and %s will be replaced by the subject
  1341. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1342. :group 'org-link-follow
  1343. :type '(choice
  1344. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1345. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1346. (const :tag "message-mail" (message-mail "%a" "%s"))
  1347. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1348. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1349. "Non-nil means ask for confirmation before executing shell links.
  1350. Shell links can be dangerous: just think about a link
  1351. [[shell:rm -rf ~/*][Google Search]]
  1352. This link would show up in your Org-mode document as \"Google Search\",
  1353. but really it would remove your entire home directory.
  1354. Therefore we advise against setting this variable to nil.
  1355. Just change it to `y-or-n-p' if you want to confirm with a
  1356. single keystroke rather than having to type \"yes\"."
  1357. :group 'org-link-follow
  1358. :type '(choice
  1359. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1360. (const :tag "with y-or-n (faster)" y-or-n-p)
  1361. (const :tag "no confirmation (dangerous)" nil)))
  1362. (put 'org-confirm-shell-link-function
  1363. 'safe-local-variable
  1364. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1365. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1366. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1367. Elisp links can be dangerous: just think about a link
  1368. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1369. This link would show up in your Org-mode document as \"Google Search\",
  1370. but really it would remove your entire home directory.
  1371. Therefore we advise against setting this variable to nil.
  1372. Just change it to `y-or-n-p' if you want to confirm with a
  1373. single keystroke rather than having to type \"yes\"."
  1374. :group 'org-link-follow
  1375. :type '(choice
  1376. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1377. (const :tag "with y-or-n (faster)" y-or-n-p)
  1378. (const :tag "no confirmation (dangerous)" nil)))
  1379. (put 'org-confirm-shell-link-function
  1380. 'safe-local-variable
  1381. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1382. (defconst org-file-apps-defaults-gnu
  1383. '((remote . emacs)
  1384. (system . mailcap)
  1385. (t . mailcap))
  1386. "Default file applications on a UNIX or GNU/Linux system.
  1387. See `org-file-apps'.")
  1388. (defconst org-file-apps-defaults-macosx
  1389. '((remote . emacs)
  1390. (t . "open %s")
  1391. (system . "open %s")
  1392. ("ps.gz" . "gv %s")
  1393. ("eps.gz" . "gv %s")
  1394. ("dvi" . "xdvi %s")
  1395. ("fig" . "xfig %s"))
  1396. "Default file applications on a MacOS X system.
  1397. The system \"open\" is known as a default, but we use X11 applications
  1398. for some files for which the OS does not have a good default.
  1399. See `org-file-apps'.")
  1400. (defconst org-file-apps-defaults-windowsnt
  1401. (list
  1402. '(remote . emacs)
  1403. (cons t
  1404. (list (if (featurep 'xemacs)
  1405. 'mswindows-shell-execute
  1406. 'w32-shell-execute)
  1407. "open" 'file))
  1408. (cons 'system
  1409. (list (if (featurep 'xemacs)
  1410. 'mswindows-shell-execute
  1411. 'w32-shell-execute)
  1412. "open" 'file)))
  1413. "Default file applications on a Windows NT system.
  1414. The system \"open\" is used for most files.
  1415. See `org-file-apps'.")
  1416. (defcustom org-file-apps
  1417. '(
  1418. (auto-mode . emacs)
  1419. ("\\.mm\\'" . default)
  1420. ("\\.x?html?\\'" . default)
  1421. ("\\.pdf\\'" . default)
  1422. )
  1423. "External applications for opening `file:path' items in a document.
  1424. Org-mode uses system defaults for different file types, but
  1425. you can use this variable to set the application for a given file
  1426. extension. The entries in this list are cons cells where the car identifies
  1427. files and the cdr the corresponding command. Possible values for the
  1428. file identifier are
  1429. \"string\" A string as a file identifier can be interpreted in different
  1430. ways, depending on its contents:
  1431. - Alphanumeric characters only:
  1432. Match links with this file extension.
  1433. Example: (\"pdf\" . \"evince %s\")
  1434. to open PDFs with evince.
  1435. - Regular expression: Match links where the
  1436. filename matches the regexp. If you want to
  1437. use groups here, use shy groups.
  1438. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1439. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1440. to open *.html and *.xhtml with firefox.
  1441. - Regular expression which contains (non-shy) groups:
  1442. Match links where the whole link, including \"::\", and
  1443. anything after that, matches the regexp.
  1444. In a custom command string, %1, %2, etc. are replaced with
  1445. the parts of the link that were matched by the groups.
  1446. For backwards compatibility, if a command string is given
  1447. that does not use any of the group matches, this case is
  1448. handled identically to the second one (i.e. match against
  1449. file name only).
  1450. In a custom lisp form, you can access the group matches with
  1451. (match-string n link).
  1452. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1453. to open [[file:document.pdf::5]] with evince at page 5.
  1454. `directory' Matches a directory
  1455. `remote' Matches a remote file, accessible through tramp or efs.
  1456. Remote files most likely should be visited through Emacs
  1457. because external applications cannot handle such paths.
  1458. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1459. so all files Emacs knows how to handle. Using this with
  1460. command `emacs' will open most files in Emacs. Beware that this
  1461. will also open html files inside Emacs, unless you add
  1462. (\"html\" . default) to the list as well.
  1463. t Default for files not matched by any of the other options.
  1464. `system' The system command to open files, like `open' on Windows
  1465. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1466. that will be selected if you call `C-c C-o' with a double
  1467. \\[universal-argument] \\[universal-argument] prefix.
  1468. Possible values for the command are:
  1469. `emacs' The file will be visited by the current Emacs process.
  1470. `default' Use the default application for this file type, which is the
  1471. association for t in the list, most likely in the system-specific
  1472. part.
  1473. This can be used to overrule an unwanted setting in the
  1474. system-specific variable.
  1475. `system' Use the system command for opening files, like \"open\".
  1476. This command is specified by the entry whose car is `system'.
  1477. Most likely, the system-specific version of this variable
  1478. does define this command, but you can overrule/replace it
  1479. here.
  1480. string A command to be executed by a shell; %s will be replaced
  1481. by the path to the file.
  1482. sexp A Lisp form which will be evaluated. The file path will
  1483. be available in the Lisp variable `file'.
  1484. For more examples, see the system specific constants
  1485. `org-file-apps-defaults-macosx'
  1486. `org-file-apps-defaults-windowsnt'
  1487. `org-file-apps-defaults-gnu'."
  1488. :group 'org-link-follow
  1489. :type '(repeat
  1490. (cons (choice :value ""
  1491. (string :tag "Extension")
  1492. (const :tag "System command to open files" system)
  1493. (const :tag "Default for unrecognized files" t)
  1494. (const :tag "Remote file" remote)
  1495. (const :tag "Links to a directory" directory)
  1496. (const :tag "Any files that have Emacs modes"
  1497. auto-mode))
  1498. (choice :value ""
  1499. (const :tag "Visit with Emacs" emacs)
  1500. (const :tag "Use default" default)
  1501. (const :tag "Use the system command" system)
  1502. (string :tag "Command")
  1503. (sexp :tag "Lisp form")))))
  1504. (defgroup org-refile nil
  1505. "Options concerning refiling entries in Org-mode."
  1506. :tag "Org Refile"
  1507. :group 'org)
  1508. (defcustom org-directory "~/org"
  1509. "Directory with org files.
  1510. This is just a default location to look for Org files. There is no need
  1511. at all to put your files into this directory. It is only used in the
  1512. following situations:
  1513. 1. When a remember template specifies a target file that is not an
  1514. absolute path. The path will then be interpreted relative to
  1515. `org-directory'
  1516. 2. When a remember note is filed away in an interactive way (when exiting the
  1517. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1518. with `org-directory' as the default path."
  1519. :group 'org-refile
  1520. :group 'org-remember
  1521. :type 'directory)
  1522. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1523. "Default target for storing notes.
  1524. Used as a fall back file for org-remember.el and org-capture.el, for
  1525. templates that do not specify a target file."
  1526. :group 'org-refile
  1527. :group 'org-remember
  1528. :type '(choice
  1529. (const :tag "Default from remember-data-file" nil)
  1530. file))
  1531. (defcustom org-goto-interface 'outline
  1532. "The default interface to be used for `org-goto'.
  1533. Allowed values are:
  1534. outline The interface shows an outline of the relevant file
  1535. and the correct heading is found by moving through
  1536. the outline or by searching with incremental search.
  1537. outline-path-completion Headlines in the current buffer are offered via
  1538. completion. This is the interface also used by
  1539. the refile command."
  1540. :group 'org-refile
  1541. :type '(choice
  1542. (const :tag "Outline" outline)
  1543. (const :tag "Outline-path-completion" outline-path-completion)))
  1544. (defcustom org-goto-max-level 5
  1545. "Maximum target level when running `org-goto' with refile interface."
  1546. :group 'org-refile
  1547. :type 'integer)
  1548. (defcustom org-reverse-note-order nil
  1549. "Non-nil means store new notes at the beginning of a file or entry.
  1550. When nil, new notes will be filed to the end of a file or entry.
  1551. This can also be a list with cons cells of regular expressions that
  1552. are matched against file names, and values."
  1553. :group 'org-remember
  1554. :group 'org-refile
  1555. :type '(choice
  1556. (const :tag "Reverse always" t)
  1557. (const :tag "Reverse never" nil)
  1558. (repeat :tag "By file name regexp"
  1559. (cons regexp boolean))))
  1560. (defcustom org-log-refile nil
  1561. "Information to record when a task is refiled.
  1562. Possible values are:
  1563. nil Don't add anything
  1564. time Add a time stamp to the task
  1565. note Prompt for a note and add it with template `org-log-note-headings'
  1566. This option can also be set with on a per-file-basis with
  1567. #+STARTUP: nologrefile
  1568. #+STARTUP: logrefile
  1569. #+STARTUP: lognoterefile
  1570. You can have local logging settings for a subtree by setting the LOGGING
  1571. property to one or more of these keywords.
  1572. When bulk-refiling from the agenda, the value `note' is forbidden and
  1573. will temporarily be changed to `time'."
  1574. :group 'org-refile
  1575. :group 'org-progress
  1576. :type '(choice
  1577. (const :tag "No logging" nil)
  1578. (const :tag "Record timestamp" time)
  1579. (const :tag "Record timestamp with note." note)))
  1580. (defcustom org-refile-targets nil
  1581. "Targets for refiling entries with \\[org-refile].
  1582. This is list of cons cells. Each cell contains:
  1583. - a specification of the files to be considered, either a list of files,
  1584. or a symbol whose function or variable value will be used to retrieve
  1585. a file name or a list of file names. If you use `org-agenda-files' for
  1586. that, all agenda files will be scanned for targets. Nil means consider
  1587. headings in the current buffer.
  1588. - A specification of how to find candidate refile targets. This may be
  1589. any of:
  1590. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1591. This tag has to be present in all target headlines, inheritance will
  1592. not be considered.
  1593. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1594. todo keyword.
  1595. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1596. headlines that are refiling targets.
  1597. - a cons cell (:level . N). Any headline of level N is considered a target.
  1598. Note that, when `org-odd-levels-only' is set, level corresponds to
  1599. order in hierarchy, not to the number of stars.
  1600. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1601. Note that, when `org-odd-levels-only' is set, level corresponds to
  1602. order in hierarchy, not to the number of stars.
  1603. You can set the variable `org-refile-target-verify-function' to a function
  1604. to verify each headline found by the simple criteria above.
  1605. When this variable is nil, all top-level headlines in the current buffer
  1606. are used, equivalent to the value `((nil . (:level . 1))'."
  1607. :group 'org-refile
  1608. :type '(repeat
  1609. (cons
  1610. (choice :value org-agenda-files
  1611. (const :tag "All agenda files" org-agenda-files)
  1612. (const :tag "Current buffer" nil)
  1613. (function) (variable) (file))
  1614. (choice :tag "Identify target headline by"
  1615. (cons :tag "Specific tag" (const :value :tag) (string))
  1616. (cons :tag "TODO keyword" (const :value :todo) (string))
  1617. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1618. (cons :tag "Level number" (const :value :level) (integer))
  1619. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1620. (defcustom org-refile-target-verify-function nil
  1621. "Function to verify if the headline at point should be a refile target.
  1622. The function will be called without arguments, with point at the
  1623. beginning of the headline. It should return t and leave point
  1624. where it is if the headline is a valid target for refiling.
  1625. If the target should not be selected, the function must return nil.
  1626. In addition to this, it may move point to a place from where the search
  1627. should be continued. For example, the function may decide that the entire
  1628. subtree of the current entry should be excluded and move point to the end
  1629. of the subtree."
  1630. :group 'org-refile
  1631. :type 'function)
  1632. (defcustom org-refile-use-cache nil
  1633. "Non-nil means cache refile targets to speed up the process.
  1634. The cache for a particular file will be updated automatically when
  1635. the buffer has been killed, or when any of the marker used for flagging
  1636. refile targets no longer points at a live buffer.
  1637. If you have added new entries to a buffer that might themselves be targets,
  1638. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1639. find that easier, `C-u C-u C-u C-c C-w'."
  1640. :group 'org-refile
  1641. :type 'boolean)
  1642. (defcustom org-refile-use-outline-path nil
  1643. "Non-nil means provide refile targets as paths.
  1644. So a level 3 headline will be available as level1/level2/level3.
  1645. When the value is `file', also include the file name (without directory)
  1646. into the path. In this case, you can also stop the completion after
  1647. the file name, to get entries inserted as top level in the file.
  1648. When `full-file-path', include the full file path."
  1649. :group 'org-refile
  1650. :type '(choice
  1651. (const :tag "Not" nil)
  1652. (const :tag "Yes" t)
  1653. (const :tag "Start with file name" file)
  1654. (const :tag "Start with full file path" full-file-path)))
  1655. (defcustom org-outline-path-complete-in-steps t
  1656. "Non-nil means complete the outline path in hierarchical steps.
  1657. When Org-mode uses the refile interface to select an outline path
  1658. \(see variable `org-refile-use-outline-path'), the completion of
  1659. the path can be done is a single go, or if can be done in steps down
  1660. the headline hierarchy. Going in steps is probably the best if you
  1661. do not use a special completion package like `ido' or `icicles'.
  1662. However, when using these packages, going in one step can be very
  1663. fast, while still showing the whole path to the entry."
  1664. :group 'org-refile
  1665. :type 'boolean)
  1666. (defcustom org-refile-allow-creating-parent-nodes nil
  1667. "Non-nil means allow to create new nodes as refile targets.
  1668. New nodes are then created by adding \"/new node name\" to the completion
  1669. of an existing node. When the value of this variable is `confirm',
  1670. new node creation must be confirmed by the user (recommended)
  1671. When nil, the completion must match an existing entry.
  1672. Note that, if the new heading is not seen by the criteria
  1673. listed in `org-refile-targets', multiple instances of the same
  1674. heading would be created by trying again to file under the new
  1675. heading."
  1676. :group 'org-refile
  1677. :type '(choice
  1678. (const :tag "Never" nil)
  1679. (const :tag "Always" t)
  1680. (const :tag "Prompt for confirmation" confirm)))
  1681. (defgroup org-todo nil
  1682. "Options concerning TODO items in Org-mode."
  1683. :tag "Org TODO"
  1684. :group 'org)
  1685. (defgroup org-progress nil
  1686. "Options concerning Progress logging in Org-mode."
  1687. :tag "Org Progress"
  1688. :group 'org-time)
  1689. (defvar org-todo-interpretation-widgets
  1690. '(
  1691. (:tag "Sequence (cycling hits every state)" sequence)
  1692. (:tag "Type (cycling directly to DONE)" type))
  1693. "The available interpretation symbols for customizing `org-todo-keywords'.
  1694. Interested libraries should add to this list.")
  1695. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1696. "List of TODO entry keyword sequences and their interpretation.
  1697. \\<org-mode-map>This is a list of sequences.
  1698. Each sequence starts with a symbol, either `sequence' or `type',
  1699. indicating if the keywords should be interpreted as a sequence of
  1700. action steps, or as different types of TODO items. The first
  1701. keywords are states requiring action - these states will select a headline
  1702. for inclusion into the global TODO list Org-mode produces. If one of
  1703. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1704. signify that no further action is necessary. If \"|\" is not found,
  1705. the last keyword is treated as the only DONE state of the sequence.
  1706. The command \\[org-todo] cycles an entry through these states, and one
  1707. additional state where no keyword is present. For details about this
  1708. cycling, see the manual.
  1709. TODO keywords and interpretation can also be set on a per-file basis with
  1710. the special #+SEQ_TODO and #+TYP_TODO lines.
  1711. Each keyword can optionally specify a character for fast state selection
  1712. \(in combination with the variable `org-use-fast-todo-selection')
  1713. and specifiers for state change logging, using the same syntax
  1714. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1715. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1716. indicates to record a time stamp each time this state is selected.
  1717. Each keyword may also specify if a timestamp or a note should be
  1718. recorded when entering or leaving the state, by adding additional
  1719. characters in the parenthesis after the keyword. This looks like this:
  1720. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1721. record only the time of the state change. With X and Y being either
  1722. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1723. Y when leaving the state if and only if the *target* state does not
  1724. define X. You may omit any of the fast-selection key or X or /Y,
  1725. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1726. For backward compatibility, this variable may also be just a list
  1727. of keywords - in this case the interpretation (sequence or type) will be
  1728. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1729. :group 'org-todo
  1730. :group 'org-keywords
  1731. :type '(choice
  1732. (repeat :tag "Old syntax, just keywords"
  1733. (string :tag "Keyword"))
  1734. (repeat :tag "New syntax"
  1735. (cons
  1736. (choice
  1737. :tag "Interpretation"
  1738. ;;Quick and dirty way to see
  1739. ;;`org-todo-interpretations'. This takes the
  1740. ;;place of item arguments
  1741. :convert-widget
  1742. (lambda (widget)
  1743. (widget-put widget
  1744. :args (mapcar
  1745. #'(lambda (x)
  1746. (widget-convert
  1747. (cons 'const x)))
  1748. org-todo-interpretation-widgets))
  1749. widget))
  1750. (repeat
  1751. (string :tag "Keyword"))))))
  1752. (defvar org-todo-keywords-1 nil
  1753. "All TODO and DONE keywords active in a buffer.")
  1754. (make-variable-buffer-local 'org-todo-keywords-1)
  1755. (defvar org-todo-keywords-for-agenda nil)
  1756. (defvar org-done-keywords-for-agenda nil)
  1757. (defvar org-drawers-for-agenda nil)
  1758. (defvar org-todo-keyword-alist-for-agenda nil)
  1759. (defvar org-tag-alist-for-agenda nil)
  1760. (defvar org-agenda-contributing-files nil)
  1761. (defvar org-not-done-keywords nil)
  1762. (make-variable-buffer-local 'org-not-done-keywords)
  1763. (defvar org-done-keywords nil)
  1764. (make-variable-buffer-local 'org-done-keywords)
  1765. (defvar org-todo-heads nil)
  1766. (make-variable-buffer-local 'org-todo-heads)
  1767. (defvar org-todo-sets nil)
  1768. (make-variable-buffer-local 'org-todo-sets)
  1769. (defvar org-todo-log-states nil)
  1770. (make-variable-buffer-local 'org-todo-log-states)
  1771. (defvar org-todo-kwd-alist nil)
  1772. (make-variable-buffer-local 'org-todo-kwd-alist)
  1773. (defvar org-todo-key-alist nil)
  1774. (make-variable-buffer-local 'org-todo-key-alist)
  1775. (defvar org-todo-key-trigger nil)
  1776. (make-variable-buffer-local 'org-todo-key-trigger)
  1777. (defcustom org-todo-interpretation 'sequence
  1778. "Controls how TODO keywords are interpreted.
  1779. This variable is in principle obsolete and is only used for
  1780. backward compatibility, if the interpretation of todo keywords is
  1781. not given already in `org-todo-keywords'. See that variable for
  1782. more information."
  1783. :group 'org-todo
  1784. :group 'org-keywords
  1785. :type '(choice (const sequence)
  1786. (const type)))
  1787. (defcustom org-use-fast-todo-selection t
  1788. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1789. This variable describes if and under what circumstances the cycling
  1790. mechanism for TODO keywords will be replaced by a single-key, direct
  1791. selection scheme.
  1792. When nil, fast selection is never used.
  1793. When the symbol `prefix', it will be used when `org-todo' is called with
  1794. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1795. in an agenda buffer.
  1796. When t, fast selection is used by default. In this case, the prefix
  1797. argument forces cycling instead.
  1798. In all cases, the special interface is only used if access keys have actually
  1799. been assigned by the user, i.e. if keywords in the configuration are followed
  1800. by a letter in parenthesis, like TODO(t)."
  1801. :group 'org-todo
  1802. :type '(choice
  1803. (const :tag "Never" nil)
  1804. (const :tag "By default" t)
  1805. (const :tag "Only with C-u C-c C-t" prefix)))
  1806. (defcustom org-provide-todo-statistics t
  1807. "Non-nil means update todo statistics after insert and toggle.
  1808. ALL-HEADLINES means update todo statistics by including headlines
  1809. with no TODO keyword as well, counting them as not done.
  1810. A list of TODO keywords means the same, but skip keywords that are
  1811. not in this list.
  1812. When this is set, todo statistics is updated in the parent of the
  1813. current entry each time a todo state is changed."
  1814. :group 'org-todo
  1815. :type '(choice
  1816. (const :tag "Yes, only for TODO entries" t)
  1817. (const :tag "Yes, including all entries" 'all-headlines)
  1818. (repeat :tag "Yes, for TODOs in this list"
  1819. (string :tag "TODO keyword"))
  1820. (other :tag "No TODO statistics" nil)))
  1821. (defcustom org-hierarchical-todo-statistics t
  1822. "Non-nil means TODO statistics covers just direct children.
  1823. When nil, all entries in the subtree are considered.
  1824. This has only an effect if `org-provide-todo-statistics' is set.
  1825. To set this to nil for only a single subtree, use a COOKIE_DATA
  1826. property and include the word \"recursive\" into the value."
  1827. :group 'org-todo
  1828. :type 'boolean)
  1829. (defcustom org-after-todo-state-change-hook nil
  1830. "Hook which is run after the state of a TODO item was changed.
  1831. The new state (a string with a TODO keyword, or nil) is available in the
  1832. Lisp variable `state'."
  1833. :group 'org-todo
  1834. :type 'hook)
  1835. (defvar org-blocker-hook nil
  1836. "Hook for functions that are allowed to block a state change.
  1837. Each function gets as its single argument a property list, see
  1838. `org-trigger-hook' for more information about this list.
  1839. If any of the functions in this hook returns nil, the state change
  1840. is blocked.")
  1841. (defvar org-trigger-hook nil
  1842. "Hook for functions that are triggered by a state change.
  1843. Each function gets as its single argument a property list with at least
  1844. the following elements:
  1845. (:type type-of-change :position pos-at-entry-start
  1846. :from old-state :to new-state)
  1847. Depending on the type, more properties may be present.
  1848. This mechanism is currently implemented for:
  1849. TODO state changes
  1850. ------------------
  1851. :type todo-state-change
  1852. :from previous state (keyword as a string), or nil, or a symbol
  1853. 'todo' or 'done', to indicate the general type of state.
  1854. :to new state, like in :from")
  1855. (defcustom org-enforce-todo-dependencies nil
  1856. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1857. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1858. be blocked if any prior sibling is not yet done.
  1859. Finally, if the parent is blocked because of ordered siblings of its own,
  1860. the child will also be blocked.
  1861. This variable needs to be set before org.el is loaded, and you need to
  1862. restart Emacs after a change to make the change effective. The only way
  1863. to change is while Emacs is running is through the customize interface."
  1864. :set (lambda (var val)
  1865. (set var val)
  1866. (if val
  1867. (add-hook 'org-blocker-hook
  1868. 'org-block-todo-from-children-or-siblings-or-parent)
  1869. (remove-hook 'org-blocker-hook
  1870. 'org-block-todo-from-children-or-siblings-or-parent)))
  1871. :group 'org-todo
  1872. :type 'boolean)
  1873. (defcustom org-enforce-todo-checkbox-dependencies nil
  1874. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1875. When this is nil, checkboxes have no influence on switching TODO states.
  1876. When non-nil, you first need to check off all check boxes before the TODO
  1877. entry can be switched to DONE.
  1878. This variable needs to be set before org.el is loaded, and you need to
  1879. restart Emacs after a change to make the change effective. The only way
  1880. to change is while Emacs is running is through the customize interface."
  1881. :set (lambda (var val)
  1882. (set var val)
  1883. (if val
  1884. (add-hook 'org-blocker-hook
  1885. 'org-block-todo-from-checkboxes)
  1886. (remove-hook 'org-blocker-hook
  1887. 'org-block-todo-from-checkboxes)))
  1888. :group 'org-todo
  1889. :type 'boolean)
  1890. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1891. "Non-nil means inserting a TODO heading is treated as state change.
  1892. So when the command \\[org-insert-todo-heading] is used, state change
  1893. logging will apply if appropriate. When nil, the new TODO item will
  1894. be inserted directly, and no logging will take place."
  1895. :group 'org-todo
  1896. :type 'boolean)
  1897. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1898. "Non-nil means switching TODO states with S-cursor counts as state change.
  1899. This is the default behavior. However, setting this to nil allows a
  1900. convenient way to select a TODO state and bypass any logging associated
  1901. with that."
  1902. :group 'org-todo
  1903. :type 'boolean)
  1904. (defcustom org-todo-state-tags-triggers nil
  1905. "Tag changes that should be triggered by TODO state changes.
  1906. This is a list. Each entry is
  1907. (state-change (tag . flag) .......)
  1908. State-change can be a string with a state, and empty string to indicate the
  1909. state that has no TODO keyword, or it can be one of the symbols `todo'
  1910. or `done', meaning any not-done or done state, respectively."
  1911. :group 'org-todo
  1912. :group 'org-tags
  1913. :type '(repeat
  1914. (cons (choice :tag "When changing to"
  1915. (const :tag "Not-done state" todo)
  1916. (const :tag "Done state" done)
  1917. (string :tag "State"))
  1918. (repeat
  1919. (cons :tag "Tag action"
  1920. (string :tag "Tag")
  1921. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1922. (defcustom org-log-done nil
  1923. "Information to record when a task moves to the DONE state.
  1924. Possible values are:
  1925. nil Don't add anything, just change the keyword
  1926. time Add a time stamp to the task
  1927. note Prompt for a note and add it with template `org-log-note-headings'
  1928. This option can also be set with on a per-file-basis with
  1929. #+STARTUP: nologdone
  1930. #+STARTUP: logdone
  1931. #+STARTUP: lognotedone
  1932. You can have local logging settings for a subtree by setting the LOGGING
  1933. property to one or more of these keywords."
  1934. :group 'org-todo
  1935. :group 'org-progress
  1936. :type '(choice
  1937. (const :tag "No logging" nil)
  1938. (const :tag "Record CLOSED timestamp" time)
  1939. (const :tag "Record CLOSED timestamp with note." note)))
  1940. ;; Normalize old uses of org-log-done.
  1941. (cond
  1942. ((eq org-log-done t) (setq org-log-done 'time))
  1943. ((and (listp org-log-done) (memq 'done org-log-done))
  1944. (setq org-log-done 'note)))
  1945. (defcustom org-log-reschedule nil
  1946. "Information to record when the scheduling date of a tasks is modified.
  1947. Possible values are:
  1948. nil Don't add anything, just change the date
  1949. time Add a time stamp to the task
  1950. note Prompt for a note and add it with template `org-log-note-headings'
  1951. This option can also be set with on a per-file-basis with
  1952. #+STARTUP: nologreschedule
  1953. #+STARTUP: logreschedule
  1954. #+STARTUP: lognotereschedule"
  1955. :group 'org-todo
  1956. :group 'org-progress
  1957. :type '(choice
  1958. (const :tag "No logging" nil)
  1959. (const :tag "Record timestamp" time)
  1960. (const :tag "Record timestamp with note." note)))
  1961. (defcustom org-log-redeadline nil
  1962. "Information to record when the deadline date of a tasks is modified.
  1963. Possible values are:
  1964. nil Don't add anything, just change the date
  1965. time Add a time stamp to the task
  1966. note Prompt for a note and add it with template `org-log-note-headings'
  1967. This option can also be set with on a per-file-basis with
  1968. #+STARTUP: nologredeadline
  1969. #+STARTUP: logredeadline
  1970. #+STARTUP: lognoteredeadline
  1971. You can have local logging settings for a subtree by setting the LOGGING
  1972. property to one or more of these keywords."
  1973. :group 'org-todo
  1974. :group 'org-progress
  1975. :type '(choice
  1976. (const :tag "No logging" nil)
  1977. (const :tag "Record timestamp" time)
  1978. (const :tag "Record timestamp with note." note)))
  1979. (defcustom org-log-note-clock-out nil
  1980. "Non-nil means record a note when clocking out of an item.
  1981. This can also be configured on a per-file basis by adding one of
  1982. the following lines anywhere in the buffer:
  1983. #+STARTUP: lognoteclock-out
  1984. #+STARTUP: nolognoteclock-out"
  1985. :group 'org-todo
  1986. :group 'org-progress
  1987. :type 'boolean)
  1988. (defcustom org-log-done-with-time t
  1989. "Non-nil means the CLOSED time stamp will contain date and time.
  1990. When nil, only the date will be recorded."
  1991. :group 'org-progress
  1992. :type 'boolean)
  1993. (defcustom org-log-note-headings
  1994. '((done . "CLOSING NOTE %t")
  1995. (state . "State %-12s from %-12S %t")
  1996. (note . "Note taken on %t")
  1997. (reschedule . "Rescheduled from %S on %t")
  1998. (delschedule . "Not scheduled, was %S on %t")
  1999. (redeadline . "New deadline from %S on %t")
  2000. (deldeadline . "Removed deadline, was %S on %t")
  2001. (refile . "Refiled on %t")
  2002. (clock-out . ""))
  2003. "Headings for notes added to entries.
  2004. The value is an alist, with the car being a symbol indicating the note
  2005. context, and the cdr is the heading to be used. The heading may also be the
  2006. empty string.
  2007. %t in the heading will be replaced by a time stamp.
  2008. %T will be an active time stamp instead the default inactive one
  2009. %s will be replaced by the new TODO state, in double quotes.
  2010. %S will be replaced by the old TODO state, in double quotes.
  2011. %u will be replaced by the user name.
  2012. %U will be replaced by the full user name.
  2013. In fact, it is not a good idea to change the `state' entry, because
  2014. agenda log mode depends on the format of these entries."
  2015. :group 'org-todo
  2016. :group 'org-progress
  2017. :type '(list :greedy t
  2018. (cons (const :tag "Heading when closing an item" done) string)
  2019. (cons (const :tag
  2020. "Heading when changing todo state (todo sequence only)"
  2021. state) string)
  2022. (cons (const :tag "Heading when just taking a note" note) string)
  2023. (cons (const :tag "Heading when clocking out" clock-out) string)
  2024. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2025. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2026. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2027. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2028. (cons (const :tag "Heading when refiling" refile) string)))
  2029. (unless (assq 'note org-log-note-headings)
  2030. (push '(note . "%t") org-log-note-headings))
  2031. (defcustom org-log-into-drawer nil
  2032. "Non-nil means insert state change notes and time stamps into a drawer.
  2033. When nil, state changes notes will be inserted after the headline and
  2034. any scheduling and clock lines, but not inside a drawer.
  2035. The value of this variable should be the name of the drawer to use.
  2036. LOGBOOK is proposed at the default drawer for this purpose, you can
  2037. also set this to a string to define the drawer of your choice.
  2038. A value of t is also allowed, representing \"LOGBOOK\".
  2039. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2040. will be ignored.
  2041. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2042. a subtree."
  2043. :group 'org-todo
  2044. :group 'org-progress
  2045. :type '(choice
  2046. (const :tag "Not into a drawer" nil)
  2047. (const :tag "LOGBOOK" t)
  2048. (string :tag "Other")))
  2049. (if (fboundp 'defvaralias)
  2050. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2051. (defun org-log-into-drawer ()
  2052. "Return the value of `org-log-into-drawer', but let properties overrule.
  2053. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2054. used instead of the default value."
  2055. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2056. (cond
  2057. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2058. ((equal p "t") "LOGBOOK")
  2059. (t p))))
  2060. (defcustom org-log-state-notes-insert-after-drawers nil
  2061. "Non-nil means insert state change notes after any drawers in entry.
  2062. Only the drawers that *immediately* follow the headline and the
  2063. deadline/scheduled line are skipped.
  2064. When nil, insert notes right after the heading and perhaps the line
  2065. with deadline/scheduling if present.
  2066. This variable will have no effect if `org-log-into-drawer' is
  2067. set."
  2068. :group 'org-todo
  2069. :group 'org-progress
  2070. :type 'boolean)
  2071. (defcustom org-log-states-order-reversed t
  2072. "Non-nil means the latest state note will be directly after heading.
  2073. When nil, the state change notes will be ordered according to time."
  2074. :group 'org-todo
  2075. :group 'org-progress
  2076. :type 'boolean)
  2077. (defcustom org-todo-repeat-to-state nil
  2078. "The TODO state to which a repeater should return the repeating task.
  2079. By default this is the first task in a TODO sequence, or the previous state
  2080. in a TODO_TYP set. But you can specify another task here.
  2081. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2082. :group 'org-todo
  2083. :type '(choice (const :tag "Head of sequence" nil)
  2084. (string :tag "Specific state")))
  2085. (defcustom org-log-repeat 'time
  2086. "Non-nil means record moving through the DONE state when triggering repeat.
  2087. An auto-repeating task is immediately switched back to TODO when
  2088. marked DONE. If you are not logging state changes (by adding \"@\"
  2089. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2090. record a closing note, there will be no record of the task moving
  2091. through DONE. This variable forces taking a note anyway.
  2092. nil Don't force a record
  2093. time Record a time stamp
  2094. note Record a note
  2095. This option can also be set with on a per-file-basis with
  2096. #+STARTUP: logrepeat
  2097. #+STARTUP: lognoterepeat
  2098. #+STARTUP: nologrepeat
  2099. You can have local logging settings for a subtree by setting the LOGGING
  2100. property to one or more of these keywords."
  2101. :group 'org-todo
  2102. :group 'org-progress
  2103. :type '(choice
  2104. (const :tag "Don't force a record" nil)
  2105. (const :tag "Force recording the DONE state" time)
  2106. (const :tag "Force recording a note with the DONE state" note)))
  2107. (defgroup org-priorities nil
  2108. "Priorities in Org-mode."
  2109. :tag "Org Priorities"
  2110. :group 'org-todo)
  2111. (defcustom org-enable-priority-commands t
  2112. "Non-nil means priority commands are active.
  2113. When nil, these commands will be disabled, so that you never accidentally
  2114. set a priority."
  2115. :group 'org-priorities
  2116. :type 'boolean)
  2117. (defcustom org-highest-priority ?A
  2118. "The highest priority of TODO items. A character like ?A, ?B etc.
  2119. Must have a smaller ASCII number than `org-lowest-priority'."
  2120. :group 'org-priorities
  2121. :type 'character)
  2122. (defcustom org-lowest-priority ?C
  2123. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2124. Must have a larger ASCII number than `org-highest-priority'."
  2125. :group 'org-priorities
  2126. :type 'character)
  2127. (defcustom org-default-priority ?B
  2128. "The default priority of TODO items.
  2129. This is the priority an item get if no explicit priority is given."
  2130. :group 'org-priorities
  2131. :type 'character)
  2132. (defcustom org-priority-start-cycle-with-default t
  2133. "Non-nil means start with default priority when starting to cycle.
  2134. When this is nil, the first step in the cycle will be (depending on the
  2135. command used) one higher or lower that the default priority."
  2136. :group 'org-priorities
  2137. :type 'boolean)
  2138. (defgroup org-time nil
  2139. "Options concerning time stamps and deadlines in Org-mode."
  2140. :tag "Org Time"
  2141. :group 'org)
  2142. (defcustom org-insert-labeled-timestamps-at-point nil
  2143. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2144. When nil, these labeled time stamps are forces into the second line of an
  2145. entry, just after the headline. When scheduling from the global TODO list,
  2146. the time stamp will always be forced into the second line."
  2147. :group 'org-time
  2148. :type 'boolean)
  2149. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2150. "Formats for `format-time-string' which are used for time stamps.
  2151. It is not recommended to change this constant.")
  2152. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2153. "Number of minutes to round time stamps to.
  2154. These are two values, the first applies when first creating a time stamp.
  2155. The second applies when changing it with the commands `S-up' and `S-down'.
  2156. When changing the time stamp, this means that it will change in steps
  2157. of N minutes, as given by the second value.
  2158. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2159. numbers should be factors of 60, so for example 5, 10, 15.
  2160. When this is larger than 1, you can still force an exact time stamp by using
  2161. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2162. and by using a prefix arg to `S-up/down' to specify the exact number
  2163. of minutes to shift."
  2164. :group 'org-time
  2165. :get '(lambda (var) ; Make sure both elements are there
  2166. (if (integerp (default-value var))
  2167. (list (default-value var) 5)
  2168. (default-value var)))
  2169. :type '(list
  2170. (integer :tag "when inserting times")
  2171. (integer :tag "when modifying times")))
  2172. ;; Normalize old customizations of this variable.
  2173. (when (integerp org-time-stamp-rounding-minutes)
  2174. (setq org-time-stamp-rounding-minutes
  2175. (list org-time-stamp-rounding-minutes
  2176. org-time-stamp-rounding-minutes)))
  2177. (defcustom org-display-custom-times nil
  2178. "Non-nil means overlay custom formats over all time stamps.
  2179. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2180. To turn this on on a per-file basis, insert anywhere in the file:
  2181. #+STARTUP: customtime"
  2182. :group 'org-time
  2183. :set 'set-default
  2184. :type 'sexp)
  2185. (make-variable-buffer-local 'org-display-custom-times)
  2186. (defcustom org-time-stamp-custom-formats
  2187. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2188. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2189. These are overlayed over the default ISO format if the variable
  2190. `org-display-custom-times' is set. Time like %H:%M should be at the
  2191. end of the second format. The custom formats are also honored by export
  2192. commands, if custom time display is turned on at the time of export."
  2193. :group 'org-time
  2194. :type 'sexp)
  2195. (defun org-time-stamp-format (&optional long inactive)
  2196. "Get the right format for a time string."
  2197. (let ((f (if long (cdr org-time-stamp-formats)
  2198. (car org-time-stamp-formats))))
  2199. (if inactive
  2200. (concat "[" (substring f 1 -1) "]")
  2201. f)))
  2202. (defcustom org-time-clocksum-format "%d:%02d"
  2203. "The format string used when creating CLOCKSUM lines.
  2204. This is also used when org-mode generates a time duration."
  2205. :group 'org-time
  2206. :type 'string)
  2207. (defcustom org-time-clocksum-use-fractional nil
  2208. "If non-nil, \\[org-clock-display] uses fractional times.
  2209. org-mode generates a time duration."
  2210. :group 'org-time
  2211. :type 'boolean)
  2212. (defcustom org-time-clocksum-fractional-format "%.2f"
  2213. "The format string used when creating CLOCKSUM lines, or when
  2214. org-mode generates a time duration."
  2215. :group 'org-time
  2216. :type 'string)
  2217. (defcustom org-deadline-warning-days 14
  2218. "No. of days before expiration during which a deadline becomes active.
  2219. This variable governs the display in sparse trees and in the agenda.
  2220. When 0 or negative, it means use this number (the absolute value of it)
  2221. even if a deadline has a different individual lead time specified.
  2222. Custom commands can set this variable in the options section."
  2223. :group 'org-time
  2224. :group 'org-agenda-daily/weekly
  2225. :type 'integer)
  2226. (defcustom org-read-date-prefer-future t
  2227. "Non-nil means assume future for incomplete date input from user.
  2228. This affects the following situations:
  2229. 1. The user gives a month but not a year.
  2230. For example, if it is April and you enter \"feb 2\", this will be read
  2231. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2232. 2. The user gives a day, but no month.
  2233. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2234. read this as the third of *next* month. However, if you enter \"17\",
  2235. it will be considered as *this* month.
  2236. If you set this variable to the symbol `time', then also the following
  2237. will work:
  2238. 3. If the user gives a time, but no day. If the time is before now,
  2239. to will be interpreted as tomorrow.
  2240. Currently none of this works for ISO week specifications.
  2241. When this option is nil, the current day, month and year will always be
  2242. used as defaults."
  2243. :group 'org-time
  2244. :type '(choice
  2245. (const :tag "Never" nil)
  2246. (const :tag "Check month and day" t)
  2247. (const :tag "Check month, day, and time" time)))
  2248. (defcustom org-read-date-display-live t
  2249. "Non-nil means display current interpretation of date prompt live.
  2250. This display will be in an overlay, in the minibuffer."
  2251. :group 'org-time
  2252. :type 'boolean)
  2253. (defcustom org-read-date-popup-calendar t
  2254. "Non-nil means pop up a calendar when prompting for a date.
  2255. In the calendar, the date can be selected with mouse-1. However, the
  2256. minibuffer will also be active, and you can simply enter the date as well.
  2257. When nil, only the minibuffer will be available."
  2258. :group 'org-time
  2259. :type 'boolean)
  2260. (if (fboundp 'defvaralias)
  2261. (defvaralias 'org-popup-calendar-for-date-prompt
  2262. 'org-read-date-popup-calendar))
  2263. (defcustom org-read-date-minibuffer-setup-hook nil
  2264. "Hook to be used to set up keys for the date/time interface.
  2265. Add key definitions to `minibuffer-local-map', which will be a temporary
  2266. copy."
  2267. :group 'org-time
  2268. :type 'hook)
  2269. (defcustom org-extend-today-until 0
  2270. "The hour when your day really ends. Must be an integer.
  2271. This has influence for the following applications:
  2272. - When switching the agenda to \"today\". It it is still earlier than
  2273. the time given here, the day recognized as TODAY is actually yesterday.
  2274. - When a date is read from the user and it is still before the time given
  2275. here, the current date and time will be assumed to be yesterday, 23:59.
  2276. Also, timestamps inserted in remember templates follow this rule.
  2277. IMPORTANT: This is a feature whose implementation is and likely will
  2278. remain incomplete. Really, it is only here because past midnight seems to
  2279. be the favorite working time of John Wiegley :-)"
  2280. :group 'org-time
  2281. :type 'integer)
  2282. (defcustom org-edit-timestamp-down-means-later nil
  2283. "Non-nil means S-down will increase the time in a time stamp.
  2284. When nil, S-up will increase."
  2285. :group 'org-time
  2286. :type 'boolean)
  2287. (defcustom org-calendar-follow-timestamp-change t
  2288. "Non-nil means make the calendar window follow timestamp changes.
  2289. When a timestamp is modified and the calendar window is visible, it will be
  2290. moved to the new date."
  2291. :group 'org-time
  2292. :type 'boolean)
  2293. (defgroup org-tags nil
  2294. "Options concerning tags in Org-mode."
  2295. :tag "Org Tags"
  2296. :group 'org)
  2297. (defcustom org-tag-alist nil
  2298. "List of tags allowed in Org-mode files.
  2299. When this list is nil, Org-mode will base TAG input on what is already in the
  2300. buffer.
  2301. The value of this variable is an alist, the car of each entry must be a
  2302. keyword as a string, the cdr may be a character that is used to select
  2303. that tag through the fast-tag-selection interface.
  2304. See the manual for details."
  2305. :group 'org-tags
  2306. :type '(repeat
  2307. (choice
  2308. (cons (string :tag "Tag name")
  2309. (character :tag "Access char"))
  2310. (list :tag "Start radio group"
  2311. (const :startgroup)
  2312. (option (string :tag "Group description")))
  2313. (list :tag "End radio group"
  2314. (const :endgroup)
  2315. (option (string :tag "Group description")))
  2316. (const :tag "New line" (:newline)))))
  2317. (defcustom org-tag-persistent-alist nil
  2318. "List of tags that will always appear in all Org-mode files.
  2319. This is in addition to any in buffer settings or customizations
  2320. of `org-tag-alist'.
  2321. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2322. The value of this variable is an alist, the car of each entry must be a
  2323. keyword as a string, the cdr may be a character that is used to select
  2324. that tag through the fast-tag-selection interface.
  2325. See the manual for details.
  2326. To disable these tags on a per-file basis, insert anywhere in the file:
  2327. #+STARTUP: noptag"
  2328. :group 'org-tags
  2329. :type '(repeat
  2330. (choice
  2331. (cons (string :tag "Tag name")
  2332. (character :tag "Access char"))
  2333. (const :tag "Start radio group" (:startgroup))
  2334. (const :tag "End radio group" (:endgroup))
  2335. (const :tag "New line" (:newline)))))
  2336. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2337. "If non-nil, always offer completion for all tags of all agenda files.
  2338. Instead of customizing this variable directly, you might want to
  2339. set it locally for remember buffers, because there no list of
  2340. tags in that file can be created dynamically (there are none).
  2341. (add-hook 'org-remember-mode-hook
  2342. (lambda ()
  2343. (set (make-local-variable
  2344. 'org-complete-tags-always-offer-all-agenda-tags)
  2345. t)))"
  2346. :group 'org-tags
  2347. :type 'boolean)
  2348. (defvar org-file-tags nil
  2349. "List of tags that can be inherited by all entries in the file.
  2350. The tags will be inherited if the variable `org-use-tag-inheritance'
  2351. says they should be.
  2352. This variable is populated from #+FILETAGS lines.")
  2353. (defcustom org-use-fast-tag-selection 'auto
  2354. "Non-nil means use fast tag selection scheme.
  2355. This is a special interface to select and deselect tags with single keys.
  2356. When nil, fast selection is never used.
  2357. When the symbol `auto', fast selection is used if and only if selection
  2358. characters for tags have been configured, either through the variable
  2359. `org-tag-alist' or through a #+TAGS line in the buffer.
  2360. When t, fast selection is always used and selection keys are assigned
  2361. automatically if necessary."
  2362. :group 'org-tags
  2363. :type '(choice
  2364. (const :tag "Always" t)
  2365. (const :tag "Never" nil)
  2366. (const :tag "When selection characters are configured" 'auto)))
  2367. (defcustom org-fast-tag-selection-single-key nil
  2368. "Non-nil means fast tag selection exits after first change.
  2369. When nil, you have to press RET to exit it.
  2370. During fast tag selection, you can toggle this flag with `C-c'.
  2371. This variable can also have the value `expert'. In this case, the window
  2372. displaying the tags menu is not even shown, until you press C-c again."
  2373. :group 'org-tags
  2374. :type '(choice
  2375. (const :tag "No" nil)
  2376. (const :tag "Yes" t)
  2377. (const :tag "Expert" expert)))
  2378. (defvar org-fast-tag-selection-include-todo nil
  2379. "Non-nil means fast tags selection interface will also offer TODO states.
  2380. This is an undocumented feature, you should not rely on it.")
  2381. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2382. "The column to which tags should be indented in a headline.
  2383. If this number is positive, it specifies the column. If it is negative,
  2384. it means that the tags should be flushright to that column. For example,
  2385. -80 works well for a normal 80 character screen."
  2386. :group 'org-tags
  2387. :type 'integer)
  2388. (defcustom org-auto-align-tags t
  2389. "Non-nil means realign tags after pro/demotion of TODO state change.
  2390. These operations change the length of a headline and therefore shift
  2391. the tags around. With this options turned on, after each such operation
  2392. the tags are again aligned to `org-tags-column'."
  2393. :group 'org-tags
  2394. :type 'boolean)
  2395. (defcustom org-use-tag-inheritance t
  2396. "Non-nil means tags in levels apply also for sublevels.
  2397. When nil, only the tags directly given in a specific line apply there.
  2398. This may also be a list of tags that should be inherited, or a regexp that
  2399. matches tags that should be inherited. Additional control is possible
  2400. with the variable `org-tags-exclude-from-inheritance' which gives an
  2401. explicit list of tags to be excluded from inheritance., even if the value of
  2402. `org-use-tag-inheritance' would select it for inheritance.
  2403. If this option is t, a match early-on in a tree can lead to a large
  2404. number of matches in the subtree when constructing the agenda or creating
  2405. a sparse tree. If you only want to see the first match in a tree during
  2406. a search, check out the variable `org-tags-match-list-sublevels'."
  2407. :group 'org-tags
  2408. :type '(choice
  2409. (const :tag "Not" nil)
  2410. (const :tag "Always" t)
  2411. (repeat :tag "Specific tags" (string :tag "Tag"))
  2412. (regexp :tag "Tags matched by regexp")))
  2413. (defcustom org-tags-exclude-from-inheritance nil
  2414. "List of tags that should never be inherited.
  2415. This is a way to exclude a few tags from inheritance. For way to do
  2416. the opposite, to actively allow inheritance for selected tags,
  2417. see the variable `org-use-tag-inheritance'."
  2418. :group 'org-tags
  2419. :type '(repeat (string :tag "Tag")))
  2420. (defun org-tag-inherit-p (tag)
  2421. "Check if TAG is one that should be inherited."
  2422. (cond
  2423. ((member tag org-tags-exclude-from-inheritance) nil)
  2424. ((eq org-use-tag-inheritance t) t)
  2425. ((not org-use-tag-inheritance) nil)
  2426. ((stringp org-use-tag-inheritance)
  2427. (string-match org-use-tag-inheritance tag))
  2428. ((listp org-use-tag-inheritance)
  2429. (member tag org-use-tag-inheritance))
  2430. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2431. (defcustom org-tags-match-list-sublevels t
  2432. "Non-nil means list also sublevels of headlines matching a search.
  2433. This variable applies to tags/property searches, and also to stuck
  2434. projects because this search is based on a tags match as well.
  2435. When set to the symbol `indented', sublevels are indented with
  2436. leading dots.
  2437. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2438. the sublevels of a headline matching a tag search often also match
  2439. the same search. Listing all of them can create very long lists.
  2440. Setting this variable to nil causes subtrees of a match to be skipped.
  2441. This variable is semi-obsolete and probably should always be true. It
  2442. is better to limit inheritance to certain tags using the variables
  2443. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2444. :group 'org-tags
  2445. :type '(choice
  2446. (const :tag "No, don't list them" nil)
  2447. (const :tag "Yes, do list them" t)
  2448. (const :tag "List them, indented with leading dots" indented)))
  2449. (defcustom org-tags-sort-function nil
  2450. "When set, tags are sorted using this function as a comparator."
  2451. :group 'org-tags
  2452. :type '(choice
  2453. (const :tag "No sorting" nil)
  2454. (const :tag "Alphabetical" string<)
  2455. (const :tag "Reverse alphabetical" string>)
  2456. (function :tag "Custom function" nil)))
  2457. (defvar org-tags-history nil
  2458. "History of minibuffer reads for tags.")
  2459. (defvar org-last-tags-completion-table nil
  2460. "The last used completion table for tags.")
  2461. (defvar org-after-tags-change-hook nil
  2462. "Hook that is run after the tags in a line have changed.")
  2463. (defgroup org-properties nil
  2464. "Options concerning properties in Org-mode."
  2465. :tag "Org Properties"
  2466. :group 'org)
  2467. (defcustom org-property-format "%-10s %s"
  2468. "How property key/value pairs should be formatted by `indent-line'.
  2469. When `indent-line' hits a property definition, it will format the line
  2470. according to this format, mainly to make sure that the values are
  2471. lined-up with respect to each other."
  2472. :group 'org-properties
  2473. :type 'string)
  2474. (defcustom org-use-property-inheritance nil
  2475. "Non-nil means properties apply also for sublevels.
  2476. This setting is chiefly used during property searches. Turning it on can
  2477. cause significant overhead when doing a search, which is why it is not
  2478. on by default.
  2479. When nil, only the properties directly given in the current entry count.
  2480. When t, every property is inherited. The value may also be a list of
  2481. properties that should have inheritance, or a regular expression matching
  2482. properties that should be inherited.
  2483. However, note that some special properties use inheritance under special
  2484. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2485. and the properties ending in \"_ALL\" when they are used as descriptor
  2486. for valid values of a property.
  2487. Note for programmers:
  2488. When querying an entry with `org-entry-get', you can control if inheritance
  2489. should be used. By default, `org-entry-get' looks only at the local
  2490. properties. You can request inheritance by setting the inherit argument
  2491. to t (to force inheritance) or to `selective' (to respect the setting
  2492. in this variable)."
  2493. :group 'org-properties
  2494. :type '(choice
  2495. (const :tag "Not" nil)
  2496. (const :tag "Always" t)
  2497. (repeat :tag "Specific properties" (string :tag "Property"))
  2498. (regexp :tag "Properties matched by regexp")))
  2499. (defun org-property-inherit-p (property)
  2500. "Check if PROPERTY is one that should be inherited."
  2501. (cond
  2502. ((eq org-use-property-inheritance t) t)
  2503. ((not org-use-property-inheritance) nil)
  2504. ((stringp org-use-property-inheritance)
  2505. (string-match org-use-property-inheritance property))
  2506. ((listp org-use-property-inheritance)
  2507. (member property org-use-property-inheritance))
  2508. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2509. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2510. "The default column format, if no other format has been defined.
  2511. This variable can be set on the per-file basis by inserting a line
  2512. #+COLUMNS: %25ITEM ....."
  2513. :group 'org-properties
  2514. :type 'string)
  2515. (defcustom org-columns-ellipses ".."
  2516. "The ellipses to be used when a field in column view is truncated.
  2517. When this is the empty string, as many characters as possible are shown,
  2518. but then there will be no visual indication that the field has been truncated.
  2519. When this is a string of length N, the last N characters of a truncated
  2520. field are replaced by this string. If the column is narrower than the
  2521. ellipses string, only part of the ellipses string will be shown."
  2522. :group 'org-properties
  2523. :type 'string)
  2524. (defcustom org-columns-modify-value-for-display-function nil
  2525. "Function that modifies values for display in column view.
  2526. For example, it can be used to cut out a certain part from a time stamp.
  2527. The function must take 2 arguments:
  2528. column-title The title of the column (*not* the property name)
  2529. value The value that should be modified.
  2530. The function should return the value that should be displayed,
  2531. or nil if the normal value should be used."
  2532. :group 'org-properties
  2533. :type 'function)
  2534. (defcustom org-effort-property "Effort"
  2535. "The property that is being used to keep track of effort estimates.
  2536. Effort estimates given in this property need to have the format H:MM."
  2537. :group 'org-properties
  2538. :group 'org-progress
  2539. :type '(string :tag "Property"))
  2540. (defconst org-global-properties-fixed
  2541. '(("VISIBILITY_ALL" . "folded children content all")
  2542. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2543. "List of property/value pairs that can be inherited by any entry.
  2544. These are fixed values, for the preset properties. The user variable
  2545. that can be used to add to this list is `org-global-properties'.
  2546. The entries in this list are cons cells where the car is a property
  2547. name and cdr is a string with the value. If the value represents
  2548. multiple items like an \"_ALL\" property, separate the items by
  2549. spaces.")
  2550. (defcustom org-global-properties nil
  2551. "List of property/value pairs that can be inherited by any entry.
  2552. This list will be combined with the constant `org-global-properties-fixed'.
  2553. The entries in this list are cons cells where the car is a property
  2554. name and cdr is a string with the value.
  2555. You can set buffer-local values for the same purpose in the variable
  2556. `org-file-properties' this by adding lines like
  2557. #+PROPERTY: NAME VALUE"
  2558. :group 'org-properties
  2559. :type '(repeat
  2560. (cons (string :tag "Property")
  2561. (string :tag "Value"))))
  2562. (defvar org-file-properties nil
  2563. "List of property/value pairs that can be inherited by any entry.
  2564. Valid for the current buffer.
  2565. This variable is populated from #+PROPERTY lines.")
  2566. (make-variable-buffer-local 'org-file-properties)
  2567. (defgroup org-agenda nil
  2568. "Options concerning agenda views in Org-mode."
  2569. :tag "Org Agenda"
  2570. :group 'org)
  2571. (defvar org-category nil
  2572. "Variable used by org files to set a category for agenda display.
  2573. Such files should use a file variable to set it, for example
  2574. # -*- mode: org; org-category: \"ELisp\"
  2575. or contain a special line
  2576. #+CATEGORY: ELisp
  2577. If the file does not specify a category, then file's base name
  2578. is used instead.")
  2579. (make-variable-buffer-local 'org-category)
  2580. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2581. (defcustom org-agenda-files nil
  2582. "The files to be used for agenda display.
  2583. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2584. \\[org-remove-file]. You can also use customize to edit the list.
  2585. If an entry is a directory, all files in that directory that are matched by
  2586. `org-agenda-file-regexp' will be part of the file list.
  2587. If the value of the variable is not a list but a single file name, then
  2588. the list of agenda files is actually stored and maintained in that file, one
  2589. agenda file per line. In this file paths can be given relative to
  2590. `org-directory'. Tilde expansion and environment variable substitution
  2591. are also made."
  2592. :group 'org-agenda
  2593. :type '(choice
  2594. (repeat :tag "List of files and directories" file)
  2595. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2596. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2597. "Regular expression to match files for `org-agenda-files'.
  2598. If any element in the list in that variable contains a directory instead
  2599. of a normal file, all files in that directory that are matched by this
  2600. regular expression will be included."
  2601. :group 'org-agenda
  2602. :type 'regexp)
  2603. (defcustom org-agenda-text-search-extra-files nil
  2604. "List of extra files to be searched by text search commands.
  2605. These files will be search in addition to the agenda files by the
  2606. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2607. Note that these files will only be searched for text search commands,
  2608. not for the other agenda views like todo lists, tag searches or the weekly
  2609. agenda. This variable is intended to list notes and possibly archive files
  2610. that should also be searched by these two commands.
  2611. In fact, if the first element in the list is the symbol `agenda-archives',
  2612. than all archive files of all agenda files will be added to the search
  2613. scope."
  2614. :group 'org-agenda
  2615. :type '(set :greedy t
  2616. (const :tag "Agenda Archives" agenda-archives)
  2617. (repeat :inline t (file))))
  2618. (if (fboundp 'defvaralias)
  2619. (defvaralias 'org-agenda-multi-occur-extra-files
  2620. 'org-agenda-text-search-extra-files))
  2621. (defcustom org-agenda-skip-unavailable-files nil
  2622. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2623. A nil value means to remove them, after a query, from the list."
  2624. :group 'org-agenda
  2625. :type 'boolean)
  2626. (defcustom org-calendar-to-agenda-key [?c]
  2627. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2628. The command `org-calendar-goto-agenda' will be bound to this key. The
  2629. default is the character `c' because then `c' can be used to switch back and
  2630. forth between agenda and calendar."
  2631. :group 'org-agenda
  2632. :type 'sexp)
  2633. (defcustom org-calendar-agenda-action-key [?k]
  2634. "The key to be installed in `calendar-mode-map' for agenda-action.
  2635. The command `org-agenda-action' will be bound to this key. The
  2636. default is the character `k' because we use the same key in the agenda."
  2637. :group 'org-agenda
  2638. :type 'sexp)
  2639. (defcustom org-calendar-insert-diary-entry-key [?i]
  2640. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2641. This option is irrelevant until `org-agenda-diary-file' has been configured
  2642. to point to an Org-mode file. When that is the case, the command
  2643. `org-agenda-diary-entry' will be bound to the key given here, by default
  2644. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2645. if you want to continue doing this, you need to change this to a different
  2646. key."
  2647. :group 'org-agenda
  2648. :type 'sexp)
  2649. (defcustom org-agenda-diary-file 'diary-file
  2650. "File to which to add new entries with the `i' key in agenda and calendar.
  2651. When this is the symbol `diary-file', the functionality in the Emacs
  2652. calendar will be used to add entries to the `diary-file'. But when this
  2653. points to a file, `org-agenda-diary-entry' will be used instead."
  2654. :group 'org-agenda
  2655. :type '(choice
  2656. (const :tag "The standard Emacs diary file" diary-file)
  2657. (file :tag "Special Org file diary entries")))
  2658. (eval-after-load "calendar"
  2659. '(progn
  2660. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2661. 'org-calendar-goto-agenda)
  2662. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2663. 'org-agenda-action)
  2664. (add-hook 'calendar-mode-hook
  2665. (lambda ()
  2666. (unless (eq org-agenda-diary-file 'diary-file)
  2667. (define-key calendar-mode-map
  2668. org-calendar-insert-diary-entry-key
  2669. 'org-agenda-diary-entry))))))
  2670. (defgroup org-latex nil
  2671. "Options for embedding LaTeX code into Org-mode."
  2672. :tag "Org LaTeX"
  2673. :group 'org)
  2674. (defcustom org-format-latex-options
  2675. '(:foreground default :background default :scale 1.0
  2676. :html-foreground "Black" :html-background "Transparent"
  2677. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2678. "Options for creating images from LaTeX fragments.
  2679. This is a property list with the following properties:
  2680. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2681. `default' means use the foreground of the default face.
  2682. :background the background color, or \"Transparent\".
  2683. `default' means use the background of the default face.
  2684. :scale a scaling factor for the size of the images, to get more pixels
  2685. :html-foreground, :html-background, :html-scale
  2686. the same numbers for HTML export.
  2687. :matchers a list indicating which matchers should be used to
  2688. find LaTeX fragments. Valid members of this list are:
  2689. \"begin\" find environments
  2690. \"$1\" find single characters surrounded by $.$
  2691. \"$\" find math expressions surrounded by $...$
  2692. \"$$\" find math expressions surrounded by $$....$$
  2693. \"\\(\" find math expressions surrounded by \\(...\\)
  2694. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2695. :group 'org-latex
  2696. :type 'plist)
  2697. (defcustom org-format-latex-signal-error t
  2698. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2699. When nil, just push out a message."
  2700. :group 'org-latex
  2701. :type 'boolean)
  2702. (defcustom org-format-latex-header "\\documentclass{article}
  2703. \\usepackage[usenames]{color}
  2704. \\usepackage{amsmath}
  2705. \\usepackage[mathscr]{eucal}
  2706. \\pagestyle{empty} % do not remove
  2707. \[PACKAGES]
  2708. \[DEFAULT-PACKAGES]
  2709. % The settings below are copied from fullpage.sty
  2710. \\setlength{\\textwidth}{\\paperwidth}
  2711. \\addtolength{\\textwidth}{-3cm}
  2712. \\setlength{\\oddsidemargin}{1.5cm}
  2713. \\addtolength{\\oddsidemargin}{-2.54cm}
  2714. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2715. \\setlength{\\textheight}{\\paperheight}
  2716. \\addtolength{\\textheight}{-\\headheight}
  2717. \\addtolength{\\textheight}{-\\headsep}
  2718. \\addtolength{\\textheight}{-\\footskip}
  2719. \\addtolength{\\textheight}{-3cm}
  2720. \\setlength{\\topmargin}{1.5cm}
  2721. \\addtolength{\\topmargin}{-2.54cm}"
  2722. "The document header used for processing LaTeX fragments.
  2723. It is imperative that this header make sure that no page number
  2724. appears on the page. The package defined in the variables
  2725. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2726. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2727. will be appended."
  2728. :group 'org-latex
  2729. :type 'string)
  2730. (defvar org-format-latex-header-extra nil)
  2731. (defun org-set-packages-alist (var val)
  2732. "Set the packages alist and make sure it has 3 elements per entry."
  2733. (set var (mapcar (lambda (x)
  2734. (if (and (consp x) (= (length x) 2))
  2735. (list (car x) (nth 1 x) t)
  2736. x))
  2737. val)))
  2738. (defun org-get-packages-alist (var)
  2739. "Get the packages alist and make sure it has 3 elements per entry."
  2740. (mapcar (lambda (x)
  2741. (if (and (consp x) (= (length x) 2))
  2742. (list (car x) (nth 1 x) t)
  2743. x))
  2744. (default-value var)))
  2745. ;; The following variables are defined here because is it also used
  2746. ;; when formatting latex fragments. Originally it was part of the
  2747. ;; LaTeX exporter, which is why the name includes "export".
  2748. (defcustom org-export-latex-default-packages-alist
  2749. '(("AUTO" "inputenc" t)
  2750. ("T1" "fontenc" t)
  2751. ("" "fixltx2e" nil)
  2752. ("" "graphicx" t)
  2753. ("" "longtable" nil)
  2754. ("" "float" nil)
  2755. ("" "wrapfig" nil)
  2756. ("" "soul" t)
  2757. ("" "t1enc" t)
  2758. ("" "textcomp" t)
  2759. ("" "marvosym" t)
  2760. ("" "wasysym" t)
  2761. ("" "latexsym" t)
  2762. ("" "amssymb" t)
  2763. ("" "hyperref" nil)
  2764. "\\tolerance=1000"
  2765. )
  2766. "Alist of default packages to be inserted in the header.
  2767. Change this only if one of the packages here causes an incompatibility
  2768. with another package you are using.
  2769. The packages in this list are needed by one part or another of Org-mode
  2770. to function properly.
  2771. - inputenc, fontenc, t1enc: for basic font and character selection
  2772. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2773. for interpreting the entities in `org-entities'. You can skip some of these
  2774. packages if you don't use any of the symbols in it.
  2775. - graphicx: for including images
  2776. - float, wrapfig: for figure placement
  2777. - longtable: for long tables
  2778. - hyperref: for cross references
  2779. Therefore you should not modify this variable unless you know what you
  2780. are doing. The one reason to change it anyway is that you might be loading
  2781. some other package that conflicts with one of the default packages.
  2782. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2783. If SNIPPET-FLAG is t, the package also needs to be included when
  2784. compiling LaTeX snippets into images for inclusion into HTML."
  2785. :group 'org-export-latex
  2786. :set 'org-set-packages-alist
  2787. :get 'org-get-packages-alist
  2788. :type '(repeat
  2789. (choice
  2790. (list :tag "options/package pair"
  2791. (string :tag "options")
  2792. (string :tag "package")
  2793. (boolean :tag "Snippet"))
  2794. (string :tag "A line of LaTeX"))))
  2795. (defcustom org-export-latex-packages-alist nil
  2796. "Alist of packages to be inserted in every LaTeX header.
  2797. These will be inserted after `org-export-latex-default-packages-alist'.
  2798. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2799. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2800. turning LaTeX snippets into images for inclusion into HTML.
  2801. Make sure that you only list packages here which:
  2802. - you want in every file
  2803. - do not conflict with the default packages in
  2804. `org-export-latex-default-packages-alist'
  2805. - do not conflict with the setup in `org-format-latex-header'."
  2806. :group 'org-export-latex
  2807. :set 'org-set-packages-alist
  2808. :get 'org-get-packages-alist
  2809. :type '(repeat
  2810. (choice
  2811. (list :tag "options/package pair"
  2812. (string :tag "options")
  2813. (string :tag "package")
  2814. (boolean :tag "Snippet"))
  2815. (string :tag "A line of LaTeX"))))
  2816. (defgroup org-appearance nil
  2817. "Settings for Org-mode appearance."
  2818. :tag "Org Appearance"
  2819. :group 'org)
  2820. (defcustom org-level-color-stars-only nil
  2821. "Non-nil means fontify only the stars in each headline.
  2822. When nil, the entire headline is fontified.
  2823. Changing it requires restart of `font-lock-mode' to become effective
  2824. also in regions already fontified."
  2825. :group 'org-appearance
  2826. :type 'boolean)
  2827. (defcustom org-hide-leading-stars nil
  2828. "Non-nil means hide the first N-1 stars in a headline.
  2829. This works by using the face `org-hide' for these stars. This
  2830. face is white for a light background, and black for a dark
  2831. background. You may have to customize the face `org-hide' to
  2832. make this work.
  2833. Changing it requires restart of `font-lock-mode' to become effective
  2834. also in regions already fontified.
  2835. You may also set this on a per-file basis by adding one of the following
  2836. lines to the buffer:
  2837. #+STARTUP: hidestars
  2838. #+STARTUP: showstars"
  2839. :group 'org-appearance
  2840. :type 'boolean)
  2841. (defcustom org-hidden-keywords nil
  2842. "List of keywords that should be hidden when typed in the org buffer.
  2843. For example, add #+TITLE to this list in order to make the
  2844. document title appear in the buffer without the initial #+TITLE:
  2845. keyword."
  2846. :group 'org-appearance
  2847. :type '(set (const :tag "#+AUTHOR" author)
  2848. (const :tag "#+DATE" date)
  2849. (const :tag "#+EMAIL" email)
  2850. (const :tag "#+TITLE" title)))
  2851. (defcustom org-fontify-done-headline nil
  2852. "Non-nil means change the face of a headline if it is marked DONE.
  2853. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2854. When this is non-nil, the headline after the keyword is set to the
  2855. `org-headline-done' as an additional indication."
  2856. :group 'org-appearance
  2857. :type 'boolean)
  2858. (defcustom org-fontify-emphasized-text t
  2859. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2860. Changing this variable requires a restart of Emacs to take effect."
  2861. :group 'org-appearance
  2862. :type 'boolean)
  2863. (defcustom org-fontify-whole-heading-line nil
  2864. "Non-nil means fontify the whole line for headings.
  2865. This is useful when setting a background color for the
  2866. org-level-* faces."
  2867. :group 'org-appearance
  2868. :type 'boolean)
  2869. (defcustom org-highlight-latex-fragments-and-specials nil
  2870. "Non-nil means fontify what is treated specially by the exporters."
  2871. :group 'org-appearance
  2872. :type 'boolean)
  2873. (defcustom org-hide-emphasis-markers nil
  2874. "Non-nil mean font-lock should hide the emphasis marker characters."
  2875. :group 'org-appearance
  2876. :type 'boolean)
  2877. (defcustom org-pretty-entities nil
  2878. "Non-nil means show entities as UTF8 characters.
  2879. When nil, the \\name form remains in the buffer."
  2880. :group 'org-appearance
  2881. :type 'boolean)
  2882. (defcustom org-pretty-entities-include-sub-superscripts t
  2883. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2884. :group 'org-appearance
  2885. :type 'boolean)
  2886. (defvar org-emph-re nil
  2887. "Regular expression for matching emphasis.
  2888. After a match, the match groups contain these elements:
  2889. 0 The match of the full regular expression, including the characters
  2890. before and after the proper match
  2891. 1 The character before the proper match, or empty at beginning of line
  2892. 2 The proper match, including the leading and trailing markers
  2893. 3 The leading marker like * or /, indicating the type of highlighting
  2894. 4 The text between the emphasis markers, not including the markers
  2895. 5 The character after the match, empty at the end of a line")
  2896. (defvar org-verbatim-re nil
  2897. "Regular expression for matching verbatim text.")
  2898. (defvar org-emphasis-regexp-components) ; defined just below
  2899. (defvar org-emphasis-alist) ; defined just below
  2900. (defun org-set-emph-re (var val)
  2901. "Set variable and compute the emphasis regular expression."
  2902. (set var val)
  2903. (when (and (boundp 'org-emphasis-alist)
  2904. (boundp 'org-emphasis-regexp-components)
  2905. org-emphasis-alist org-emphasis-regexp-components)
  2906. (let* ((e org-emphasis-regexp-components)
  2907. (pre (car e))
  2908. (post (nth 1 e))
  2909. (border (nth 2 e))
  2910. (body (nth 3 e))
  2911. (nl (nth 4 e))
  2912. (body1 (concat body "*?"))
  2913. (markers (mapconcat 'car org-emphasis-alist ""))
  2914. (vmarkers (mapconcat
  2915. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2916. org-emphasis-alist "")))
  2917. ;; make sure special characters appear at the right position in the class
  2918. (if (string-match "\\^" markers)
  2919. (setq markers (concat (replace-match "" t t markers) "^")))
  2920. (if (string-match "-" markers)
  2921. (setq markers (concat (replace-match "" t t markers) "-")))
  2922. (if (string-match "\\^" vmarkers)
  2923. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2924. (if (string-match "-" vmarkers)
  2925. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2926. (if (> nl 0)
  2927. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2928. (int-to-string nl) "\\}")))
  2929. ;; Make the regexp
  2930. (setq org-emph-re
  2931. (concat "\\([" pre "]\\|^\\)"
  2932. "\\("
  2933. "\\([" markers "]\\)"
  2934. "\\("
  2935. "[^" border "]\\|"
  2936. "[^" border "]"
  2937. body1
  2938. "[^" border "]"
  2939. "\\)"
  2940. "\\3\\)"
  2941. "\\([" post "]\\|$\\)"))
  2942. (setq org-verbatim-re
  2943. (concat "\\([" pre "]\\|^\\)"
  2944. "\\("
  2945. "\\([" vmarkers "]\\)"
  2946. "\\("
  2947. "[^" border "]\\|"
  2948. "[^" border "]"
  2949. body1
  2950. "[^" border "]"
  2951. "\\)"
  2952. "\\3\\)"
  2953. "\\([" post "]\\|$\\)")))))
  2954. (defcustom org-emphasis-regexp-components
  2955. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2956. "Components used to build the regular expression for emphasis.
  2957. This is a list with 6 entries. Terminology: In an emphasis string
  2958. like \" *strong word* \", we call the initial space PREMATCH, the final
  2959. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2960. and \"trong wor\" is the body. The different components in this variable
  2961. specify what is allowed/forbidden in each part:
  2962. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2963. post Chars allowed as postmatch. End of line will be allowed too.
  2964. border The chars *forbidden* as border characters.
  2965. body-regexp A regexp like \".\" to match a body character. Don't use
  2966. non-shy groups here, and don't allow newline here.
  2967. newline The maximum number of newlines allowed in an emphasis exp.
  2968. Use customize to modify this, or restart Emacs after changing it."
  2969. :group 'org-appearance
  2970. :set 'org-set-emph-re
  2971. :type '(list
  2972. (sexp :tag "Allowed chars in pre ")
  2973. (sexp :tag "Allowed chars in post ")
  2974. (sexp :tag "Forbidden chars in border ")
  2975. (sexp :tag "Regexp for body ")
  2976. (integer :tag "number of newlines allowed")
  2977. (option (boolean :tag "Please ignore this button"))))
  2978. (defcustom org-emphasis-alist
  2979. `(("*" bold "<b>" "</b>")
  2980. ("/" italic "<i>" "</i>")
  2981. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2982. ("=" org-code "<code>" "</code>" verbatim)
  2983. ("~" org-verbatim "<code>" "</code>" verbatim)
  2984. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2985. "<del>" "</del>")
  2986. )
  2987. "Special syntax for emphasized text.
  2988. Text starting and ending with a special character will be emphasized, for
  2989. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2990. characters, the face to be used by font-lock for highlighting in Org-mode
  2991. Emacs buffers, and the HTML tags to be used for this.
  2992. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2993. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  2994. Use customize to modify this, or restart Emacs after changing it."
  2995. :group 'org-appearance
  2996. :set 'org-set-emph-re
  2997. :type '(repeat
  2998. (list
  2999. (string :tag "Marker character")
  3000. (choice
  3001. (face :tag "Font-lock-face")
  3002. (plist :tag "Face property list"))
  3003. (string :tag "HTML start tag")
  3004. (string :tag "HTML end tag")
  3005. (option (const verbatim)))))
  3006. (defvar org-protecting-blocks
  3007. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3008. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3009. This is needed for font-lock setup.")
  3010. ;;; Miscellaneous options
  3011. (defgroup org-completion nil
  3012. "Completion in Org-mode."
  3013. :tag "Org Completion"
  3014. :group 'org)
  3015. (defcustom org-completion-use-ido nil
  3016. "Non-nil means use ido completion wherever possible.
  3017. Note that `ido-mode' must be active for this variable to be relevant.
  3018. If you decide to turn this variable on, you might well want to turn off
  3019. `org-outline-path-complete-in-steps'.
  3020. See also `org-completion-use-iswitchb'."
  3021. :group 'org-completion
  3022. :type 'boolean)
  3023. (defcustom org-completion-use-iswitchb nil
  3024. "Non-nil means use iswitchb completion wherever possible.
  3025. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3026. If you decide to turn this variable on, you might well want to turn off
  3027. `org-outline-path-complete-in-steps'.
  3028. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3029. :group 'org-completion
  3030. :type 'boolean)
  3031. (defcustom org-completion-fallback-command 'hippie-expand
  3032. "The expansion command called by \\[org-complete] in normal context.
  3033. Normal means no org-mode-specific context."
  3034. :group 'org-completion
  3035. :type 'function)
  3036. ;;; Functions and variables from their packages
  3037. ;; Declared here to avoid compiler warnings
  3038. ;; XEmacs only
  3039. (defvar outline-mode-menu-heading)
  3040. (defvar outline-mode-menu-show)
  3041. (defvar outline-mode-menu-hide)
  3042. (defvar zmacs-regions) ; XEmacs regions
  3043. ;; Emacs only
  3044. (defvar mark-active)
  3045. ;; Various packages
  3046. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3047. (declare-function calendar-forward-day "cal-move" (arg))
  3048. (declare-function calendar-goto-date "cal-move" (date))
  3049. (declare-function calendar-goto-today "cal-move" ())
  3050. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3051. (defvar calc-embedded-close-formula)
  3052. (defvar calc-embedded-open-formula)
  3053. (declare-function cdlatex-tab "ext:cdlatex" ())
  3054. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3055. (defvar font-lock-unfontify-region-function)
  3056. (declare-function iswitchb-read-buffer "iswitchb"
  3057. (prompt &optional default require-match start matches-set))
  3058. (defvar iswitchb-temp-buflist)
  3059. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3060. (defvar org-agenda-tags-todo-honor-ignore-options)
  3061. (declare-function org-agenda-skip "org-agenda" ())
  3062. (declare-function
  3063. org-format-agenda-item "org-agenda"
  3064. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3065. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3066. (declare-function org-agenda-change-all-lines "org-agenda"
  3067. (newhead hdmarker &optional fixface just-this))
  3068. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3069. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3070. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3071. (beg end))
  3072. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3073. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3074. "org-agenda" (&optional end))
  3075. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3076. (declare-function org-indent-mode "org-indent" (&optional arg))
  3077. (declare-function parse-time-string "parse-time" (string))
  3078. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3079. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3080. (defvar remember-data-file)
  3081. (defvar texmathp-why)
  3082. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3083. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3084. (defvar w3m-current-url)
  3085. (defvar w3m-current-title)
  3086. (defvar org-latex-regexps)
  3087. ;;; Autoload and prepare some org modules
  3088. ;; Some table stuff that needs to be defined here, because it is used
  3089. ;; by the functions setting up org-mode or checking for table context.
  3090. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3091. "Detect an org-type or table-type table.")
  3092. (defconst org-table-line-regexp "^[ \t]*|"
  3093. "Detect an org-type table line.")
  3094. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3095. "Detect an org-type table line.")
  3096. (defconst org-table-hline-regexp "^[ \t]*|-"
  3097. "Detect an org-type table hline.")
  3098. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3099. "Detect a table-type table hline.")
  3100. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3101. "Detect the first line outside a table when searching from within it.
  3102. This works for both table types.")
  3103. ;; Autoload the functions in org-table.el that are needed by functions here.
  3104. (eval-and-compile
  3105. (org-autoload "org-table"
  3106. '(org-table-align org-table-begin org-table-blank-field
  3107. org-table-convert org-table-convert-region org-table-copy-down
  3108. org-table-copy-region org-table-create
  3109. org-table-create-or-convert-from-region
  3110. org-table-create-with-table.el org-table-current-dline
  3111. org-table-cut-region org-table-delete-column org-table-edit-field
  3112. org-table-edit-formulas org-table-end org-table-eval-formula
  3113. org-table-export org-table-field-info
  3114. org-table-get-stored-formulas org-table-goto-column
  3115. org-table-hline-and-move org-table-import org-table-insert-column
  3116. org-table-insert-hline org-table-insert-row org-table-iterate
  3117. org-table-justify-field-maybe org-table-kill-row
  3118. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3119. org-table-move-column org-table-move-column-left
  3120. org-table-move-column-right org-table-move-row
  3121. org-table-move-row-down org-table-move-row-up
  3122. org-table-next-field org-table-next-row org-table-paste-rectangle
  3123. org-table-previous-field org-table-recalculate
  3124. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3125. org-table-toggle-coordinate-overlays
  3126. org-table-toggle-formula-debugger org-table-wrap-region
  3127. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3128. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3129. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3130. (defun org-at-table-p (&optional table-type)
  3131. "Return t if the cursor is inside an org-type table.
  3132. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3133. (if org-enable-table-editor
  3134. (save-excursion
  3135. (beginning-of-line 1)
  3136. (looking-at (if table-type org-table-any-line-regexp
  3137. org-table-line-regexp)))
  3138. nil))
  3139. (defsubst org-table-p () (org-at-table-p))
  3140. (defun org-at-table.el-p ()
  3141. "Return t if and only if we are at a table.el table."
  3142. (and (org-at-table-p 'any)
  3143. (save-excursion
  3144. (goto-char (org-table-begin 'any))
  3145. (looking-at org-table1-hline-regexp))))
  3146. (defun org-table-recognize-table.el ()
  3147. "If there is a table.el table nearby, recognize it and move into it."
  3148. (if org-table-tab-recognizes-table.el
  3149. (if (org-at-table.el-p)
  3150. (progn
  3151. (beginning-of-line 1)
  3152. (if (looking-at org-table-dataline-regexp)
  3153. nil
  3154. (if (looking-at org-table1-hline-regexp)
  3155. (progn
  3156. (beginning-of-line 2)
  3157. (if (looking-at org-table-any-border-regexp)
  3158. (beginning-of-line -1)))))
  3159. (if (re-search-forward "|" (org-table-end t) t)
  3160. (progn
  3161. (require 'table)
  3162. (if (table--at-cell-p (point))
  3163. t
  3164. (message "recognizing table.el table...")
  3165. (table-recognize-table)
  3166. (message "recognizing table.el table...done")))
  3167. (error "This should not happen"))
  3168. t)
  3169. nil)
  3170. nil))
  3171. (defun org-at-table-hline-p ()
  3172. "Return t if the cursor is inside a hline in a table."
  3173. (if org-enable-table-editor
  3174. (save-excursion
  3175. (beginning-of-line 1)
  3176. (looking-at org-table-hline-regexp))
  3177. nil))
  3178. (defvar org-table-clean-did-remove-column nil)
  3179. (defun org-table-map-tables (function &optional quietly)
  3180. "Apply FUNCTION to the start of all tables in the buffer."
  3181. (save-excursion
  3182. (save-restriction
  3183. (widen)
  3184. (goto-char (point-min))
  3185. (while (re-search-forward org-table-any-line-regexp nil t)
  3186. (unless quietly
  3187. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3188. (beginning-of-line 1)
  3189. (when (looking-at org-table-line-regexp)
  3190. (save-excursion (funcall function))
  3191. (or (looking-at org-table-line-regexp)
  3192. (forward-char 1)))
  3193. (re-search-forward org-table-any-border-regexp nil 1))))
  3194. (unless quietly (message "Mapping tables: done")))
  3195. ;; Declare and autoload functions from org-exp.el & Co
  3196. (declare-function org-default-export-plist "org-exp")
  3197. (declare-function org-infile-export-plist "org-exp")
  3198. (declare-function org-get-current-options "org-exp")
  3199. (eval-and-compile
  3200. (org-autoload "org-exp"
  3201. '(org-export org-export-visible
  3202. org-insert-export-options-template
  3203. org-table-clean-before-export))
  3204. (org-autoload "org-ascii"
  3205. '(org-export-as-ascii org-export-ascii-preprocess
  3206. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3207. org-export-region-as-ascii))
  3208. (org-autoload "org-latex"
  3209. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3210. org-replace-region-by-latex org-export-region-as-latex
  3211. org-export-as-latex org-export-as-pdf
  3212. org-export-as-pdf-and-open))
  3213. (org-autoload "org-html"
  3214. '(org-export-as-html-and-open
  3215. org-export-as-html-batch org-export-as-html-to-buffer
  3216. org-replace-region-by-html org-export-region-as-html
  3217. org-export-as-html))
  3218. (org-autoload "org-docbook"
  3219. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3220. org-replace-region-by-docbook org-export-region-as-docbook
  3221. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3222. org-export-as-docbook))
  3223. (org-autoload "org-icalendar"
  3224. '(org-export-icalendar-this-file
  3225. org-export-icalendar-all-agenda-files
  3226. org-export-icalendar-combine-agenda-files))
  3227. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3228. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3229. ;; Declare and autoload functions from org-agenda.el
  3230. (eval-and-compile
  3231. (org-autoload "org-agenda"
  3232. '(org-agenda org-agenda-list org-search-view
  3233. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3234. org-diary org-agenda-to-appt
  3235. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3236. ;; Autoload org-remember
  3237. (eval-and-compile
  3238. (org-autoload "org-remember"
  3239. '(org-remember-insinuate org-remember-annotation
  3240. org-remember-apply-template org-remember org-remember-handler)))
  3241. (eval-and-compile
  3242. (org-autoload "org-capture"
  3243. '(org-capture org-capture-insert-template-here
  3244. org-capture-import-remember-templates)))
  3245. ;; Autoload org-clock.el
  3246. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3247. (beg end))
  3248. (declare-function org-clock-update-mode-line "org-clock" ())
  3249. (declare-function org-resolve-clocks "org-clock"
  3250. (&optional also-non-dangling-p prompt last-valid))
  3251. (defvar org-clock-start-time)
  3252. (defvar org-clock-marker (make-marker)
  3253. "Marker recording the last clock-in.")
  3254. (defvar org-clock-hd-marker (make-marker)
  3255. "Marker recording the last clock-in, but the headline position.")
  3256. (defvar org-clock-heading ""
  3257. "The heading of the current clock entry.")
  3258. (defun org-clock-is-active ()
  3259. "Return non-nil if clock is currently running.
  3260. The return value is actually the clock marker."
  3261. (marker-buffer org-clock-marker))
  3262. (eval-and-compile
  3263. (org-autoload
  3264. "org-clock"
  3265. '(org-clock-in org-clock-out org-clock-cancel
  3266. org-clock-goto org-clock-sum org-clock-display
  3267. org-clock-remove-overlays org-clock-report
  3268. org-clocktable-shift org-dblock-write:clocktable
  3269. org-get-clocktable org-resolve-clocks)))
  3270. (defun org-clock-update-time-maybe ()
  3271. "If this is a CLOCK line, update it and return t.
  3272. Otherwise, return nil."
  3273. (interactive)
  3274. (save-excursion
  3275. (beginning-of-line 1)
  3276. (skip-chars-forward " \t")
  3277. (when (looking-at org-clock-string)
  3278. (let ((re (concat "[ \t]*" org-clock-string
  3279. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3280. "\\([ \t]*=>.*\\)?\\)?"))
  3281. ts te h m s neg)
  3282. (cond
  3283. ((not (looking-at re))
  3284. nil)
  3285. ((not (match-end 2))
  3286. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3287. (> org-clock-marker (point))
  3288. (<= org-clock-marker (point-at-eol)))
  3289. ;; The clock is running here
  3290. (setq org-clock-start-time
  3291. (apply 'encode-time
  3292. (org-parse-time-string (match-string 1))))
  3293. (org-clock-update-mode-line)))
  3294. (t
  3295. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3296. (end-of-line 1)
  3297. (setq ts (match-string 1)
  3298. te (match-string 3))
  3299. (setq s (- (org-float-time
  3300. (apply 'encode-time (org-parse-time-string te)))
  3301. (org-float-time
  3302. (apply 'encode-time (org-parse-time-string ts))))
  3303. neg (< s 0)
  3304. s (abs s)
  3305. h (floor (/ s 3600))
  3306. s (- s (* 3600 h))
  3307. m (floor (/ s 60))
  3308. s (- s (* 60 s)))
  3309. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3310. t))))))
  3311. (defun org-check-running-clock ()
  3312. "Check if the current buffer contains the running clock.
  3313. If yes, offer to stop it and to save the buffer with the changes."
  3314. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3315. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3316. (buffer-name))))
  3317. (org-clock-out)
  3318. (when (y-or-n-p "Save changed buffer?")
  3319. (save-buffer))))
  3320. (defun org-clocktable-try-shift (dir n)
  3321. "Check if this line starts a clock table, if yes, shift the time block."
  3322. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3323. (org-clocktable-shift dir n)))
  3324. ;; Autoload org-timer.el
  3325. (eval-and-compile
  3326. (org-autoload
  3327. "org-timer"
  3328. '(org-timer-start org-timer org-timer-item
  3329. org-timer-change-times-in-region
  3330. org-timer-set-timer
  3331. org-timer-reset-timers
  3332. org-timer-show-remaining-time)))
  3333. ;; Autoload org-feed.el
  3334. (eval-and-compile
  3335. (org-autoload
  3336. "org-feed"
  3337. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3338. ;; Autoload org-indent.el
  3339. ;; Define the variable already here, to make sure we have it.
  3340. (defvar org-indent-mode nil
  3341. "Non-nil if Org-Indent mode is enabled.
  3342. Use the command `org-indent-mode' to change this variable.")
  3343. (eval-and-compile
  3344. (org-autoload
  3345. "org-indent"
  3346. '(org-indent-mode)))
  3347. ;; Autoload org-mobile.el
  3348. (eval-and-compile
  3349. (org-autoload
  3350. "org-mobile"
  3351. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3352. ;; Autoload archiving code
  3353. ;; The stuff that is needed for cycling and tags has to be defined here.
  3354. (defgroup org-archive nil
  3355. "Options concerning archiving in Org-mode."
  3356. :tag "Org Archive"
  3357. :group 'org-structure)
  3358. (defcustom org-archive-location "%s_archive::"
  3359. "The location where subtrees should be archived.
  3360. The value of this variable is a string, consisting of two parts,
  3361. separated by a double-colon. The first part is a filename and
  3362. the second part is a headline.
  3363. When the filename is omitted, archiving happens in the same file.
  3364. %s in the filename will be replaced by the current file
  3365. name (without the directory part). Archiving to a different file
  3366. is useful to keep archived entries from contributing to the
  3367. Org-mode Agenda.
  3368. The archived entries will be filed as subtrees of the specified
  3369. headline. When the headline is omitted, the subtrees are simply
  3370. filed away at the end of the file, as top-level entries. Also in
  3371. the heading you can use %s to represent the file name, this can be
  3372. useful when using the same archive for a number of different files.
  3373. Here are a few examples:
  3374. \"%s_archive::\"
  3375. If the current file is Projects.org, archive in file
  3376. Projects.org_archive, as top-level trees. This is the default.
  3377. \"::* Archived Tasks\"
  3378. Archive in the current file, under the top-level headline
  3379. \"* Archived Tasks\".
  3380. \"~/org/archive.org::\"
  3381. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3382. \"~/org/archive.org::From %s\"
  3383. Archive in file ~/org/archive.org (absolute path), under headlines
  3384. \"From FILENAME\" where file name is the current file name.
  3385. \"basement::** Finished Tasks\"
  3386. Archive in file ./basement (relative path), as level 3 trees
  3387. below the level 2 heading \"** Finished Tasks\".
  3388. You may set this option on a per-file basis by adding to the buffer a
  3389. line like
  3390. #+ARCHIVE: basement::** Finished Tasks
  3391. You may also define it locally for a subtree by setting an ARCHIVE property
  3392. in the entry. If such a property is found in an entry, or anywhere up
  3393. the hierarchy, it will be used."
  3394. :group 'org-archive
  3395. :type 'string)
  3396. (defcustom org-archive-tag "ARCHIVE"
  3397. "The tag that marks a subtree as archived.
  3398. An archived subtree does not open during visibility cycling, and does
  3399. not contribute to the agenda listings.
  3400. After changing this, font-lock must be restarted in the relevant buffers to
  3401. get the proper fontification."
  3402. :group 'org-archive
  3403. :group 'org-keywords
  3404. :type 'string)
  3405. (defcustom org-agenda-skip-archived-trees t
  3406. "Non-nil means the agenda will skip any items located in archived trees.
  3407. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3408. variable is no longer recommended, you should leave it at the value t.
  3409. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3410. :group 'org-archive
  3411. :group 'org-agenda-skip
  3412. :type 'boolean)
  3413. (defcustom org-columns-skip-archived-trees t
  3414. "Non-nil means ignore archived trees when creating column view."
  3415. :group 'org-archive
  3416. :group 'org-properties
  3417. :type 'boolean)
  3418. (defcustom org-cycle-open-archived-trees nil
  3419. "Non-nil means `org-cycle' will open archived trees.
  3420. An archived tree is a tree marked with the tag ARCHIVE.
  3421. When nil, archived trees will stay folded. You can still open them with
  3422. normal outline commands like `show-all', but not with the cycling commands."
  3423. :group 'org-archive
  3424. :group 'org-cycle
  3425. :type 'boolean)
  3426. (defcustom org-sparse-tree-open-archived-trees nil
  3427. "Non-nil means sparse tree construction shows matches in archived trees.
  3428. When nil, matches in these trees are highlighted, but the trees are kept in
  3429. collapsed state."
  3430. :group 'org-archive
  3431. :group 'org-sparse-trees
  3432. :type 'boolean)
  3433. (defun org-cycle-hide-archived-subtrees (state)
  3434. "Re-hide all archived subtrees after a visibility state change."
  3435. (when (and (not org-cycle-open-archived-trees)
  3436. (not (memq state '(overview folded))))
  3437. (save-excursion
  3438. (let* ((globalp (memq state '(contents all)))
  3439. (beg (if globalp (point-min) (point)))
  3440. (end (if globalp (point-max) (org-end-of-subtree t))))
  3441. (org-hide-archived-subtrees beg end)
  3442. (goto-char beg)
  3443. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3444. (message "%s" (substitute-command-keys
  3445. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3446. (defun org-force-cycle-archived ()
  3447. "Cycle subtree even if it is archived."
  3448. (interactive)
  3449. (setq this-command 'org-cycle)
  3450. (let ((org-cycle-open-archived-trees t))
  3451. (call-interactively 'org-cycle)))
  3452. (defun org-hide-archived-subtrees (beg end)
  3453. "Re-hide all archived subtrees after a visibility state change."
  3454. (save-excursion
  3455. (let* ((re (concat ":" org-archive-tag ":")))
  3456. (goto-char beg)
  3457. (while (re-search-forward re end t)
  3458. (when (org-on-heading-p)
  3459. (org-flag-subtree t)
  3460. (org-end-of-subtree t))))))
  3461. (defun org-flag-subtree (flag)
  3462. (save-excursion
  3463. (org-back-to-heading t)
  3464. (outline-end-of-heading)
  3465. (outline-flag-region (point)
  3466. (progn (org-end-of-subtree t) (point))
  3467. flag)))
  3468. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3469. (eval-and-compile
  3470. (org-autoload "org-archive"
  3471. '(org-add-archive-files org-archive-subtree
  3472. org-archive-to-archive-sibling org-toggle-archive-tag
  3473. org-archive-subtree-default
  3474. org-archive-subtree-default-with-confirmation)))
  3475. ;; Autoload Column View Code
  3476. (declare-function org-columns-number-to-string "org-colview")
  3477. (declare-function org-columns-get-format-and-top-level "org-colview")
  3478. (declare-function org-columns-compute "org-colview")
  3479. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3480. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3481. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3482. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3483. ;; Autoload ID code
  3484. (declare-function org-id-store-link "org-id")
  3485. (declare-function org-id-locations-load "org-id")
  3486. (declare-function org-id-locations-save "org-id")
  3487. (defvar org-id-track-globally)
  3488. (org-autoload "org-id"
  3489. '(org-id-get-create org-id-new org-id-copy org-id-get
  3490. org-id-get-with-outline-path-completion
  3491. org-id-get-with-outline-drilling org-id-store-link
  3492. org-id-goto org-id-find org-id-store-link))
  3493. ;; Autoload Plotting Code
  3494. (org-autoload "org-plot"
  3495. '(org-plot/gnuplot))
  3496. ;;; Variables for pre-computed regular expressions, all buffer local
  3497. (defvar org-drawer-regexp nil
  3498. "Matches first line of a hidden block.")
  3499. (make-variable-buffer-local 'org-drawer-regexp)
  3500. (defvar org-todo-regexp nil
  3501. "Matches any of the TODO state keywords.")
  3502. (make-variable-buffer-local 'org-todo-regexp)
  3503. (defvar org-not-done-regexp nil
  3504. "Matches any of the TODO state keywords except the last one.")
  3505. (make-variable-buffer-local 'org-not-done-regexp)
  3506. (defvar org-not-done-heading-regexp nil
  3507. "Matches a TODO headline that is not done.")
  3508. (make-variable-buffer-local 'org-not-done-regexp)
  3509. (defvar org-todo-line-regexp nil
  3510. "Matches a headline and puts TODO state into group 2 if present.")
  3511. (make-variable-buffer-local 'org-todo-line-regexp)
  3512. (defvar org-complex-heading-regexp nil
  3513. "Matches a headline and puts everything into groups:
  3514. group 1: the stars
  3515. group 2: The todo keyword, maybe
  3516. group 3: Priority cookie
  3517. group 4: True headline
  3518. group 5: Tags")
  3519. (make-variable-buffer-local 'org-complex-heading-regexp)
  3520. (defvar org-complex-heading-regexp-format nil
  3521. "Printf format to make regexp to match an exact headline.
  3522. This regexp will match the headline of any node which hase the exact
  3523. headline text that is put into the format, but may have any TODO state,
  3524. priority and tags.")
  3525. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3526. (defvar org-todo-line-tags-regexp nil
  3527. "Matches a headline and puts TODO state into group 2 if present.
  3528. Also put tags into group 4 if tags are present.")
  3529. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3530. (defvar org-nl-done-regexp nil
  3531. "Matches newline followed by a headline with the DONE keyword.")
  3532. (make-variable-buffer-local 'org-nl-done-regexp)
  3533. (defvar org-looking-at-done-regexp nil
  3534. "Matches the DONE keyword a point.")
  3535. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3536. (defvar org-ds-keyword-length 12
  3537. "Maximum length of the Deadline and SCHEDULED keywords.")
  3538. (make-variable-buffer-local 'org-ds-keyword-length)
  3539. (defvar org-deadline-regexp nil
  3540. "Matches the DEADLINE keyword.")
  3541. (make-variable-buffer-local 'org-deadline-regexp)
  3542. (defvar org-deadline-time-regexp nil
  3543. "Matches the DEADLINE keyword together with a time stamp.")
  3544. (make-variable-buffer-local 'org-deadline-time-regexp)
  3545. (defvar org-deadline-line-regexp nil
  3546. "Matches the DEADLINE keyword and the rest of the line.")
  3547. (make-variable-buffer-local 'org-deadline-line-regexp)
  3548. (defvar org-scheduled-regexp nil
  3549. "Matches the SCHEDULED keyword.")
  3550. (make-variable-buffer-local 'org-scheduled-regexp)
  3551. (defvar org-scheduled-time-regexp nil
  3552. "Matches the SCHEDULED keyword together with a time stamp.")
  3553. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3554. (defvar org-closed-time-regexp nil
  3555. "Matches the CLOSED keyword together with a time stamp.")
  3556. (make-variable-buffer-local 'org-closed-time-regexp)
  3557. (defvar org-keyword-time-regexp nil
  3558. "Matches any of the 4 keywords, together with the time stamp.")
  3559. (make-variable-buffer-local 'org-keyword-time-regexp)
  3560. (defvar org-keyword-time-not-clock-regexp nil
  3561. "Matches any of the 3 keywords, together with the time stamp.")
  3562. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3563. (defvar org-maybe-keyword-time-regexp nil
  3564. "Matches a timestamp, possibly preceded by a keyword.")
  3565. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3566. (defvar org-planning-or-clock-line-re nil
  3567. "Matches a line with planning or clock info.")
  3568. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3569. (defvar org-all-time-keywords nil
  3570. "List of time keywords.")
  3571. (make-variable-buffer-local 'org-all-time-keywords)
  3572. (defconst org-plain-time-of-day-regexp
  3573. (concat
  3574. "\\(\\<[012]?[0-9]"
  3575. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3576. "\\(--?"
  3577. "\\(\\<[012]?[0-9]"
  3578. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3579. "\\)?")
  3580. "Regular expression to match a plain time or time range.
  3581. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3582. groups carry important information:
  3583. 0 the full match
  3584. 1 the first time, range or not
  3585. 8 the second time, if it is a range.")
  3586. (defconst org-plain-time-extension-regexp
  3587. (concat
  3588. "\\(\\<[012]?[0-9]"
  3589. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3590. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3591. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3592. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3593. groups carry important information:
  3594. 0 the full match
  3595. 7 hours of duration
  3596. 9 minutes of duration")
  3597. (defconst org-stamp-time-of-day-regexp
  3598. (concat
  3599. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3600. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3601. "\\(--?"
  3602. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3603. "Regular expression to match a timestamp time or time range.
  3604. After a match, the following groups carry important information:
  3605. 0 the full match
  3606. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3607. 2 the first time, range or not
  3608. 4 the second time, if it is a range.")
  3609. (defconst org-startup-options
  3610. '(("fold" org-startup-folded t)
  3611. ("overview" org-startup-folded t)
  3612. ("nofold" org-startup-folded nil)
  3613. ("showall" org-startup-folded nil)
  3614. ("showeverything" org-startup-folded showeverything)
  3615. ("content" org-startup-folded content)
  3616. ("indent" org-startup-indented t)
  3617. ("noindent" org-startup-indented nil)
  3618. ("hidestars" org-hide-leading-stars t)
  3619. ("showstars" org-hide-leading-stars nil)
  3620. ("odd" org-odd-levels-only t)
  3621. ("oddeven" org-odd-levels-only nil)
  3622. ("align" org-startup-align-all-tables t)
  3623. ("noalign" org-startup-align-all-tables nil)
  3624. ("customtime" org-display-custom-times t)
  3625. ("logdone" org-log-done time)
  3626. ("lognotedone" org-log-done note)
  3627. ("nologdone" org-log-done nil)
  3628. ("lognoteclock-out" org-log-note-clock-out t)
  3629. ("nolognoteclock-out" org-log-note-clock-out nil)
  3630. ("logrepeat" org-log-repeat state)
  3631. ("lognoterepeat" org-log-repeat note)
  3632. ("nologrepeat" org-log-repeat nil)
  3633. ("logreschedule" org-log-reschedule time)
  3634. ("lognotereschedule" org-log-reschedule note)
  3635. ("nologreschedule" org-log-reschedule nil)
  3636. ("logredeadline" org-log-redeadline time)
  3637. ("lognoteredeadline" org-log-redeadline note)
  3638. ("nologredeadline" org-log-redeadline nil)
  3639. ("logrefile" org-log-refile time)
  3640. ("lognoterefile" org-log-refile note)
  3641. ("nologrefile" org-log-refile nil)
  3642. ("fninline" org-footnote-define-inline t)
  3643. ("nofninline" org-footnote-define-inline nil)
  3644. ("fnlocal" org-footnote-section nil)
  3645. ("fnauto" org-footnote-auto-label t)
  3646. ("fnprompt" org-footnote-auto-label nil)
  3647. ("fnconfirm" org-footnote-auto-label confirm)
  3648. ("fnplain" org-footnote-auto-label plain)
  3649. ("fnadjust" org-footnote-auto-adjust t)
  3650. ("nofnadjust" org-footnote-auto-adjust nil)
  3651. ("constcgs" constants-unit-system cgs)
  3652. ("constSI" constants-unit-system SI)
  3653. ("noptag" org-tag-persistent-alist nil)
  3654. ("hideblocks" org-hide-block-startup t)
  3655. ("nohideblocks" org-hide-block-startup nil)
  3656. ("beamer" org-startup-with-beamer-mode t)
  3657. ("entitiespretty" org-pretty-entities t)
  3658. ("entitiesplain" org-pretty-entities nil))
  3659. "Variable associated with STARTUP options for org-mode.
  3660. Each element is a list of three items: The startup options as written
  3661. in the #+STARTUP line, the corresponding variable, and the value to
  3662. set this variable to if the option is found. An optional forth element PUSH
  3663. means to push this value onto the list in the variable.")
  3664. (defun org-set-regexps-and-options ()
  3665. "Precompute regular expressions for current buffer."
  3666. (when (org-mode-p)
  3667. (org-set-local 'org-todo-kwd-alist nil)
  3668. (org-set-local 'org-todo-key-alist nil)
  3669. (org-set-local 'org-todo-key-trigger nil)
  3670. (org-set-local 'org-todo-keywords-1 nil)
  3671. (org-set-local 'org-done-keywords nil)
  3672. (org-set-local 'org-todo-heads nil)
  3673. (org-set-local 'org-todo-sets nil)
  3674. (org-set-local 'org-todo-log-states nil)
  3675. (org-set-local 'org-file-properties nil)
  3676. (org-set-local 'org-file-tags nil)
  3677. (let ((re (org-make-options-regexp
  3678. '("CATEGORY" "TODO" "COLUMNS"
  3679. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3680. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3681. "OPTIONS")
  3682. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3683. (splitre "[ \t]+")
  3684. (scripts org-use-sub-superscripts)
  3685. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3686. tail sep kws1 prio props ftags drawers beamer-p
  3687. ext-setup-or-nil setup-contents (start 0))
  3688. (save-excursion
  3689. (save-restriction
  3690. (widen)
  3691. (goto-char (point-min))
  3692. (while (or (and ext-setup-or-nil
  3693. (string-match re ext-setup-or-nil start)
  3694. (setq start (match-end 0)))
  3695. (and (setq ext-setup-or-nil nil start 0)
  3696. (re-search-forward re nil t)))
  3697. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3698. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3699. (if (stringp value) (setq value (org-trim value)))
  3700. (cond
  3701. ((equal key "CATEGORY")
  3702. (setq cat value))
  3703. ((member key '("SEQ_TODO" "TODO"))
  3704. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3705. ((equal key "TYP_TODO")
  3706. (push (cons 'type (org-split-string value splitre)) kwds))
  3707. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3708. ;; general TODO-like setup
  3709. (push (cons (intern (downcase (match-string 1 key)))
  3710. (org-split-string value splitre)) kwds))
  3711. ((equal key "TAGS")
  3712. (setq tags (append tags (if tags '("\\n") nil)
  3713. (org-split-string value splitre))))
  3714. ((equal key "COLUMNS")
  3715. (org-set-local 'org-columns-default-format value))
  3716. ((equal key "LINK")
  3717. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3718. (push (cons (match-string 1 value)
  3719. (org-trim (match-string 2 value)))
  3720. links)))
  3721. ((equal key "PRIORITIES")
  3722. (setq prio (org-split-string value " +")))
  3723. ((equal key "PROPERTY")
  3724. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3725. (push (cons (match-string 1 value) (match-string 2 value))
  3726. props)))
  3727. ((equal key "FILETAGS")
  3728. (when (string-match "\\S-" value)
  3729. (setq ftags
  3730. (append
  3731. ftags
  3732. (apply 'append
  3733. (mapcar (lambda (x) (org-split-string x ":"))
  3734. (org-split-string value)))))))
  3735. ((equal key "DRAWERS")
  3736. (setq drawers (org-split-string value splitre)))
  3737. ((equal key "CONSTANTS")
  3738. (setq const (append const (org-split-string value splitre))))
  3739. ((equal key "STARTUP")
  3740. (let ((opts (org-split-string value splitre))
  3741. l var val)
  3742. (while (setq l (pop opts))
  3743. (when (setq l (assoc l org-startup-options))
  3744. (setq var (nth 1 l) val (nth 2 l))
  3745. (if (not (nth 3 l))
  3746. (set (make-local-variable var) val)
  3747. (if (not (listp (symbol-value var)))
  3748. (set (make-local-variable var) nil))
  3749. (set (make-local-variable var) (symbol-value var))
  3750. (add-to-list var val))))))
  3751. ((equal key "ARCHIVE")
  3752. (setq arch value)
  3753. (remove-text-properties 0 (length arch)
  3754. '(face t fontified t) arch))
  3755. ((equal key "LATEX_CLASS")
  3756. (setq beamer-p (equal value "beamer")))
  3757. ((equal key "OPTIONS")
  3758. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3759. (setq scripts (read (match-string 2 value)))))
  3760. ((equal key "SETUPFILE")
  3761. (setq setup-contents (org-file-contents
  3762. (expand-file-name
  3763. (org-remove-double-quotes value))
  3764. 'noerror))
  3765. (if (not ext-setup-or-nil)
  3766. (setq ext-setup-or-nil setup-contents start 0)
  3767. (setq ext-setup-or-nil
  3768. (concat (substring ext-setup-or-nil 0 start)
  3769. "\n" setup-contents "\n"
  3770. (substring ext-setup-or-nil start)))))
  3771. ))))
  3772. (org-set-local 'org-use-sub-superscripts scripts)
  3773. (when cat
  3774. (org-set-local 'org-category (intern cat))
  3775. (push (cons "CATEGORY" cat) props))
  3776. (when prio
  3777. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3778. (setq prio (mapcar 'string-to-char prio))
  3779. (org-set-local 'org-highest-priority (nth 0 prio))
  3780. (org-set-local 'org-lowest-priority (nth 1 prio))
  3781. (org-set-local 'org-default-priority (nth 2 prio)))
  3782. (and props (org-set-local 'org-file-properties (nreverse props)))
  3783. (and ftags (org-set-local 'org-file-tags
  3784. (mapcar 'org-add-prop-inherited ftags)))
  3785. (and drawers (org-set-local 'org-drawers drawers))
  3786. (and arch (org-set-local 'org-archive-location arch))
  3787. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3788. ;; Process the TODO keywords
  3789. (unless kwds
  3790. ;; Use the global values as if they had been given locally.
  3791. (setq kwds (default-value 'org-todo-keywords))
  3792. (if (stringp (car kwds))
  3793. (setq kwds (list (cons org-todo-interpretation
  3794. (default-value 'org-todo-keywords)))))
  3795. (setq kwds (reverse kwds)))
  3796. (setq kwds (nreverse kwds))
  3797. (let (inter kws kw)
  3798. (while (setq kws (pop kwds))
  3799. (let ((kws (or
  3800. (run-hook-with-args-until-success
  3801. 'org-todo-setup-filter-hook kws)
  3802. kws)))
  3803. (setq inter (pop kws) sep (member "|" kws)
  3804. kws0 (delete "|" (copy-sequence kws))
  3805. kwsa nil
  3806. kws1 (mapcar
  3807. (lambda (x)
  3808. ;; 1 2
  3809. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3810. (progn
  3811. (setq kw (match-string 1 x)
  3812. key (and (match-end 2) (match-string 2 x))
  3813. log (org-extract-log-state-settings x))
  3814. (push (cons kw (and key (string-to-char key))) kwsa)
  3815. (and log (push log org-todo-log-states))
  3816. kw)
  3817. (error "Invalid TODO keyword %s" x)))
  3818. kws0)
  3819. kwsa (if kwsa (append '((:startgroup))
  3820. (nreverse kwsa)
  3821. '((:endgroup))))
  3822. hw (car kws1)
  3823. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3824. tail (list inter hw (car dws) (org-last dws))))
  3825. (add-to-list 'org-todo-heads hw 'append)
  3826. (push kws1 org-todo-sets)
  3827. (setq org-done-keywords (append org-done-keywords dws nil))
  3828. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3829. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3830. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3831. (setq org-todo-sets (nreverse org-todo-sets)
  3832. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3833. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3834. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3835. ;; Process the constants
  3836. (when const
  3837. (let (e cst)
  3838. (while (setq e (pop const))
  3839. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3840. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3841. (setq org-table-formula-constants-local cst)))
  3842. ;; Process the tags.
  3843. (when tags
  3844. (let (e tgs)
  3845. (while (setq e (pop tags))
  3846. (cond
  3847. ((equal e "{") (push '(:startgroup) tgs))
  3848. ((equal e "}") (push '(:endgroup) tgs))
  3849. ((equal e "\\n") (push '(:newline) tgs))
  3850. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3851. (push (cons (match-string 1 e)
  3852. (string-to-char (match-string 2 e)))
  3853. tgs))
  3854. (t (push (list e) tgs))))
  3855. (org-set-local 'org-tag-alist nil)
  3856. (while (setq e (pop tgs))
  3857. (or (and (stringp (car e))
  3858. (assoc (car e) org-tag-alist))
  3859. (push e org-tag-alist)))))
  3860. ;; Compute the regular expressions and other local variables
  3861. (if (not org-done-keywords)
  3862. (setq org-done-keywords (and org-todo-keywords-1
  3863. (list (org-last org-todo-keywords-1)))))
  3864. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3865. (length org-scheduled-string)
  3866. (length org-clock-string)
  3867. (length org-closed-string)))
  3868. org-drawer-regexp
  3869. (concat "^[ \t]*:\\("
  3870. (mapconcat 'regexp-quote org-drawers "\\|")
  3871. "\\):[ \t]*$")
  3872. org-not-done-keywords
  3873. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3874. org-todo-regexp
  3875. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3876. "\\|") "\\)\\>")
  3877. org-not-done-regexp
  3878. (concat "\\<\\("
  3879. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3880. "\\)\\>")
  3881. org-not-done-heading-regexp
  3882. (concat "^\\(\\*+\\)[ \t]+\\("
  3883. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3884. "\\)\\>")
  3885. org-todo-line-regexp
  3886. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3887. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3888. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3889. org-complex-heading-regexp
  3890. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3891. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3892. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3893. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3894. org-complex-heading-regexp-format
  3895. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3896. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3897. "\\)\\>\\)?"
  3898. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3899. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3900. "[ \t]*\\(%s\\)"
  3901. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3902. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3903. org-nl-done-regexp
  3904. (concat "\n\\*+[ \t]+"
  3905. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3906. "\\)" "\\>")
  3907. org-todo-line-tags-regexp
  3908. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3909. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3910. (org-re
  3911. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3912. org-looking-at-done-regexp
  3913. (concat "^" "\\(?:"
  3914. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3915. "\\>")
  3916. org-deadline-regexp (concat "\\<" org-deadline-string)
  3917. org-deadline-time-regexp
  3918. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3919. org-deadline-line-regexp
  3920. (concat "\\<\\(" org-deadline-string "\\).*")
  3921. org-scheduled-regexp
  3922. (concat "\\<" org-scheduled-string)
  3923. org-scheduled-time-regexp
  3924. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3925. org-closed-time-regexp
  3926. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3927. org-keyword-time-regexp
  3928. (concat "\\<\\(" org-scheduled-string
  3929. "\\|" org-deadline-string
  3930. "\\|" org-closed-string
  3931. "\\|" org-clock-string "\\)"
  3932. " *[[<]\\([^]>]+\\)[]>]")
  3933. org-keyword-time-not-clock-regexp
  3934. (concat "\\<\\(" org-scheduled-string
  3935. "\\|" org-deadline-string
  3936. "\\|" org-closed-string
  3937. "\\)"
  3938. " *[[<]\\([^]>]+\\)[]>]")
  3939. org-maybe-keyword-time-regexp
  3940. (concat "\\(\\<\\(" org-scheduled-string
  3941. "\\|" org-deadline-string
  3942. "\\|" org-closed-string
  3943. "\\|" org-clock-string "\\)\\)?"
  3944. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3945. org-planning-or-clock-line-re
  3946. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3947. "\\|" org-deadline-string
  3948. "\\|" org-closed-string "\\|" org-clock-string
  3949. "\\)\\>\\)")
  3950. org-all-time-keywords
  3951. (mapcar (lambda (w) (substring w 0 -1))
  3952. (list org-scheduled-string org-deadline-string
  3953. org-clock-string org-closed-string))
  3954. )
  3955. (org-compute-latex-and-specials-regexp)
  3956. (org-set-font-lock-defaults))))
  3957. (defun org-file-contents (file &optional noerror)
  3958. "Return the contents of FILE, as a string."
  3959. (if (or (not file)
  3960. (not (file-readable-p file)))
  3961. (if noerror
  3962. (progn
  3963. (message "Cannot read file \"%s\"" file)
  3964. (ding) (sit-for 2)
  3965. "")
  3966. (error "Cannot read file \"%s\"" file))
  3967. (with-temp-buffer
  3968. (insert-file-contents file)
  3969. (buffer-string))))
  3970. (defun org-extract-log-state-settings (x)
  3971. "Extract the log state setting from a TODO keyword string.
  3972. This will extract info from a string like \"WAIT(w@/!)\"."
  3973. (let (kw key log1 log2)
  3974. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3975. (setq kw (match-string 1 x)
  3976. key (and (match-end 2) (match-string 2 x))
  3977. log1 (and (match-end 3) (match-string 3 x))
  3978. log2 (and (match-end 4) (match-string 4 x)))
  3979. (and (or log1 log2)
  3980. (list kw
  3981. (and log1 (if (equal log1 "!") 'time 'note))
  3982. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3983. (defun org-remove-keyword-keys (list)
  3984. "Remove a pair of parenthesis at the end of each string in LIST."
  3985. (mapcar (lambda (x)
  3986. (if (string-match "(.*)$" x)
  3987. (substring x 0 (match-beginning 0))
  3988. x))
  3989. list))
  3990. (defun org-assign-fast-keys (alist)
  3991. "Assign fast keys to a keyword-key alist.
  3992. Respect keys that are already there."
  3993. (let (new e (alt ?0))
  3994. (while (setq e (pop alist))
  3995. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3996. (cdr e)) ;; Key already assigned.
  3997. (push e new)
  3998. (let ((clist (string-to-list (downcase (car e))))
  3999. (used (append new alist)))
  4000. (when (= (car clist) ?@)
  4001. (pop clist))
  4002. (while (and clist (rassoc (car clist) used))
  4003. (pop clist))
  4004. (unless clist
  4005. (while (rassoc alt used)
  4006. (incf alt)))
  4007. (push (cons (car e) (or (car clist) alt)) new))))
  4008. (nreverse new)))
  4009. ;;; Some variables used in various places
  4010. (defvar org-window-configuration nil
  4011. "Used in various places to store a window configuration.")
  4012. (defvar org-selected-window nil
  4013. "Used in various places to store a window configuration.")
  4014. (defvar org-finish-function nil
  4015. "Function to be called when `C-c C-c' is used.
  4016. This is for getting out of special buffers like remember.")
  4017. ;; FIXME: Occasionally check by commenting these, to make sure
  4018. ;; no other functions uses these, forgetting to let-bind them.
  4019. (defvar entry)
  4020. (defvar last-state)
  4021. (defvar date)
  4022. ;; Defined somewhere in this file, but used before definition.
  4023. (defvar org-entities) ;; defined in org-entities.el
  4024. (defvar org-struct-menu)
  4025. (defvar org-org-menu)
  4026. (defvar org-tbl-menu)
  4027. ;;;; Define the Org-mode
  4028. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4029. (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"))
  4030. ;; We use a before-change function to check if a table might need
  4031. ;; an update.
  4032. (defvar org-table-may-need-update t
  4033. "Indicates that a table might need an update.
  4034. This variable is set by `org-before-change-function'.
  4035. `org-table-align' sets it back to nil.")
  4036. (defun org-before-change-function (beg end)
  4037. "Every change indicates that a table might need an update."
  4038. (setq org-table-may-need-update t))
  4039. (defvar org-mode-map)
  4040. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4041. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4042. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4043. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4044. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4045. (defvar org-table-buffer-is-an nil)
  4046. (defconst org-outline-regexp "\\*+ ")
  4047. ;;;###autoload
  4048. (define-derived-mode org-mode outline-mode "Org"
  4049. "Outline-based notes management and organizer, alias
  4050. \"Carsten's outline-mode for keeping track of everything.\"
  4051. Org-mode develops organizational tasks around a NOTES file which
  4052. contains information about projects as plain text. Org-mode is
  4053. implemented on top of outline-mode, which is ideal to keep the content
  4054. of large files well structured. It supports ToDo items, deadlines and
  4055. time stamps, which magically appear in the diary listing of the Emacs
  4056. calendar. Tables are easily created with a built-in table editor.
  4057. Plain text URL-like links connect to websites, emails (VM), Usenet
  4058. messages (Gnus), BBDB entries, and any files related to the project.
  4059. For printing and sharing of notes, an Org-mode file (or a part of it)
  4060. can be exported as a structured ASCII or HTML file.
  4061. The following commands are available:
  4062. \\{org-mode-map}"
  4063. ;; Get rid of Outline menus, they are not needed
  4064. ;; Need to do this here because define-derived-mode sets up
  4065. ;; the keymap so late. Still, it is a waste to call this each time
  4066. ;; we switch another buffer into org-mode.
  4067. (if (featurep 'xemacs)
  4068. (when (boundp 'outline-mode-menu-heading)
  4069. ;; Assume this is Greg's port, it uses easymenu
  4070. (easy-menu-remove outline-mode-menu-heading)
  4071. (easy-menu-remove outline-mode-menu-show)
  4072. (easy-menu-remove outline-mode-menu-hide))
  4073. (define-key org-mode-map [menu-bar headings] 'undefined)
  4074. (define-key org-mode-map [menu-bar hide] 'undefined)
  4075. (define-key org-mode-map [menu-bar show] 'undefined))
  4076. (org-load-modules-maybe)
  4077. (easy-menu-add org-org-menu)
  4078. (easy-menu-add org-tbl-menu)
  4079. (org-install-agenda-files-menu)
  4080. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4081. (add-to-invisibility-spec '(org-cwidth))
  4082. (add-to-invisibility-spec '(org-hide-block . t))
  4083. (when (featurep 'xemacs)
  4084. (org-set-local 'line-move-ignore-invisible t))
  4085. (org-set-local 'outline-regexp org-outline-regexp)
  4086. (org-set-local 'outline-level 'org-outline-level)
  4087. (when (and org-ellipsis
  4088. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4089. (fboundp 'make-glyph-code))
  4090. (unless org-display-table
  4091. (setq org-display-table (make-display-table)))
  4092. (set-display-table-slot
  4093. org-display-table 4
  4094. (vconcat (mapcar
  4095. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4096. org-ellipsis)))
  4097. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4098. (setq buffer-display-table org-display-table))
  4099. (org-set-regexps-and-options)
  4100. (when (and org-tag-faces (not org-tags-special-faces-re))
  4101. ;; tag faces set outside customize.... force initialization.
  4102. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4103. ;; Calc embedded
  4104. (org-set-local 'calc-embedded-open-mode "# ")
  4105. (modify-syntax-entry ?@ "w")
  4106. (if org-startup-truncated (setq truncate-lines t))
  4107. (org-set-local 'font-lock-unfontify-region-function
  4108. 'org-unfontify-region)
  4109. ;; Activate before-change-function
  4110. (org-set-local 'org-table-may-need-update t)
  4111. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4112. 'local)
  4113. ;; Check for running clock before killing a buffer
  4114. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4115. ;; Paragraphs and auto-filling
  4116. (org-set-autofill-regexps)
  4117. (setq indent-line-function 'org-indent-line-function)
  4118. (org-update-radio-target-regexp)
  4119. ;; Beginning/end of defun
  4120. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4121. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4122. ;; Make sure dependence stuff works reliably, even for users who set it
  4123. ;; too late :-(
  4124. (if org-enforce-todo-dependencies
  4125. (add-hook 'org-blocker-hook
  4126. 'org-block-todo-from-children-or-siblings-or-parent)
  4127. (remove-hook 'org-blocker-hook
  4128. 'org-block-todo-from-children-or-siblings-or-parent))
  4129. (if org-enforce-todo-checkbox-dependencies
  4130. (add-hook 'org-blocker-hook
  4131. 'org-block-todo-from-checkboxes)
  4132. (remove-hook 'org-blocker-hook
  4133. 'org-block-todo-from-checkboxes))
  4134. ;; Comment characters
  4135. (org-set-local 'comment-start "#")
  4136. (org-set-local 'comment-padding " ")
  4137. ;; Align options lines
  4138. (org-set-local
  4139. 'align-mode-rules-list
  4140. '((org-in-buffer-settings
  4141. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4142. (modes . '(org-mode)))))
  4143. ;; Imenu
  4144. (org-set-local 'imenu-create-index-function
  4145. 'org-imenu-get-tree)
  4146. ;; Make isearch reveal context
  4147. (if (or (featurep 'xemacs)
  4148. (not (boundp 'outline-isearch-open-invisible-function)))
  4149. ;; Emacs 21 and XEmacs make use of the hook
  4150. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4151. ;; Emacs 22 deals with this through a special variable
  4152. (org-set-local 'outline-isearch-open-invisible-function
  4153. (lambda (&rest ignore) (org-show-context 'isearch))))
  4154. ;; Turn on org-beamer-mode?
  4155. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4156. ;; If empty file that did not turn on org-mode automatically, make it to.
  4157. (if (and org-insert-mode-line-in-empty-file
  4158. (interactive-p)
  4159. (= (point-min) (point-max)))
  4160. (insert "# -*- mode: org -*-\n\n"))
  4161. (unless org-inhibit-startup
  4162. (when org-startup-align-all-tables
  4163. (let ((bmp (buffer-modified-p)))
  4164. (org-table-map-tables 'org-table-align 'quietly)
  4165. (set-buffer-modified-p bmp)))
  4166. (when org-startup-indented
  4167. (require 'org-indent)
  4168. (org-indent-mode 1))
  4169. (unless org-inhibit-startup-visibility-stuff
  4170. (org-set-startup-visibility))))
  4171. (when (fboundp 'abbrev-table-put)
  4172. (abbrev-table-put org-mode-abbrev-table
  4173. :parents (list text-mode-abbrev-table)))
  4174. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4175. (defun org-current-time ()
  4176. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4177. (if (> (car org-time-stamp-rounding-minutes) 1)
  4178. (let ((r (car org-time-stamp-rounding-minutes))
  4179. (time (decode-time)))
  4180. (apply 'encode-time
  4181. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4182. (nthcdr 2 time))))
  4183. (current-time)))
  4184. ;;;; Font-Lock stuff, including the activators
  4185. (defvar org-mouse-map (make-sparse-keymap))
  4186. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4187. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4188. (when org-mouse-1-follows-link
  4189. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4190. (when org-tab-follows-link
  4191. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4192. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4193. (require 'font-lock)
  4194. (defconst org-non-link-chars "]\t\n\r<>")
  4195. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4196. "shell" "elisp" "doi"))
  4197. (defvar org-link-types-re nil
  4198. "Matches a link that has a url-like prefix like \"http:\"")
  4199. (defvar org-link-re-with-space nil
  4200. "Matches a link with spaces, optional angular brackets around it.")
  4201. (defvar org-link-re-with-space2 nil
  4202. "Matches a link with spaces, optional angular brackets around it.")
  4203. (defvar org-link-re-with-space3 nil
  4204. "Matches a link with spaces, only for internal part in bracket links.")
  4205. (defvar org-angle-link-re nil
  4206. "Matches link with angular brackets, spaces are allowed.")
  4207. (defvar org-plain-link-re nil
  4208. "Matches plain link, without spaces.")
  4209. (defvar org-bracket-link-regexp nil
  4210. "Matches a link in double brackets.")
  4211. (defvar org-bracket-link-analytic-regexp nil
  4212. "Regular expression used to analyze links.
  4213. Here is what the match groups contain after a match:
  4214. 1: http:
  4215. 2: http
  4216. 3: path
  4217. 4: [desc]
  4218. 5: desc")
  4219. (defvar org-bracket-link-analytic-regexp++ nil
  4220. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4221. (defvar org-any-link-re nil
  4222. "Regular expression matching any link.")
  4223. (defcustom org-match-sexp-depth 3
  4224. "Number of stacked braces for sub/superscript matching.
  4225. This has to be set before loading org.el to be effective."
  4226. :group 'org-export-translation ; ??????????????????????????/
  4227. :type 'integer)
  4228. (defun org-create-multibrace-regexp (left right n)
  4229. "Create a regular expression which will match a balanced sexp.
  4230. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4231. as single character strings.
  4232. The regexp returned will match the entire expression including the
  4233. delimiters. It will also define a single group which contains the
  4234. match except for the outermost delimiters. The maximum depth of
  4235. stacked delimiters is N. Escaping delimiters is not possible."
  4236. (let* ((nothing (concat "[^" left right "]*?"))
  4237. (or "\\|")
  4238. (re nothing)
  4239. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4240. (while (> n 1)
  4241. (setq n (1- n)
  4242. re (concat re or next)
  4243. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4244. (concat left "\\(" re "\\)" right)))
  4245. (defvar org-match-substring-regexp
  4246. (concat
  4247. "\\([^\\]\\)\\([_^]\\)\\("
  4248. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4249. "\\|"
  4250. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4251. "\\|"
  4252. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4253. "The regular expression matching a sub- or superscript.")
  4254. (defvar org-match-substring-with-braces-regexp
  4255. (concat
  4256. "\\([^\\]\\)\\([_^]\\)\\("
  4257. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4258. "\\)")
  4259. "The regular expression matching a sub- or superscript, forcing braces.")
  4260. (defun org-make-link-regexps ()
  4261. "Update the link regular expressions.
  4262. This should be called after the variable `org-link-types' has changed."
  4263. (setq org-link-types-re
  4264. (concat
  4265. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4266. org-link-re-with-space
  4267. (concat
  4268. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4269. "\\([^" org-non-link-chars " ]"
  4270. "[^" org-non-link-chars "]*"
  4271. "[^" org-non-link-chars " ]\\)>?")
  4272. org-link-re-with-space2
  4273. (concat
  4274. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4275. "\\([^" org-non-link-chars " ]"
  4276. "[^\t\n\r]*"
  4277. "[^" org-non-link-chars " ]\\)>?")
  4278. org-link-re-with-space3
  4279. (concat
  4280. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4281. "\\([^" org-non-link-chars " ]"
  4282. "[^\t\n\r]*\\)")
  4283. org-angle-link-re
  4284. (concat
  4285. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4286. "\\([^" org-non-link-chars " ]"
  4287. "[^" org-non-link-chars "]*"
  4288. "\\)>")
  4289. org-plain-link-re
  4290. (concat
  4291. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4292. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4293. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4294. org-bracket-link-regexp
  4295. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4296. org-bracket-link-analytic-regexp
  4297. (concat
  4298. "\\[\\["
  4299. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4300. "\\([^]]+\\)"
  4301. "\\]"
  4302. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4303. "\\]")
  4304. org-bracket-link-analytic-regexp++
  4305. (concat
  4306. "\\[\\["
  4307. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4308. "\\([^]]+\\)"
  4309. "\\]"
  4310. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4311. "\\]")
  4312. org-any-link-re
  4313. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4314. org-angle-link-re "\\)\\|\\("
  4315. org-plain-link-re "\\)")))
  4316. (org-make-link-regexps)
  4317. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4318. "Regular expression for fast time stamp matching.")
  4319. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4320. "Regular expression for fast time stamp matching.")
  4321. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4322. "Regular expression matching time strings for analysis.
  4323. This one does not require the space after the date, so it can be used
  4324. on a string that terminates immediately after the date.")
  4325. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4326. "Regular expression matching time strings for analysis.")
  4327. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4328. "Regular expression matching time stamps, with groups.")
  4329. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4330. "Regular expression matching time stamps (also [..]), with groups.")
  4331. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4332. "Regular expression matching a time stamp range.")
  4333. (defconst org-tr-regexp-both
  4334. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4335. "Regular expression matching a time stamp range.")
  4336. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4337. org-ts-regexp "\\)?")
  4338. "Regular expression matching a time stamp or time stamp range.")
  4339. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4340. org-ts-regexp-both "\\)?")
  4341. "Regular expression matching a time stamp or time stamp range.
  4342. The time stamps may be either active or inactive.")
  4343. (defvar org-emph-face nil)
  4344. (defun org-do-emphasis-faces (limit)
  4345. "Run through the buffer and add overlays to links."
  4346. (let (rtn a)
  4347. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4348. (if (not (= (char-after (match-beginning 3))
  4349. (char-after (match-beginning 4))))
  4350. (progn
  4351. (setq rtn t)
  4352. (setq a (assoc (match-string 3) org-emphasis-alist))
  4353. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4354. 'face
  4355. (nth 1 a))
  4356. (and (nth 4 a)
  4357. (org-remove-flyspell-overlays-in
  4358. (match-beginning 0) (match-end 0)))
  4359. (add-text-properties (match-beginning 2) (match-end 2)
  4360. '(font-lock-multiline t org-emphasis t))
  4361. (when org-hide-emphasis-markers
  4362. (add-text-properties (match-end 4) (match-beginning 5)
  4363. '(invisible org-link))
  4364. (add-text-properties (match-beginning 3) (match-end 3)
  4365. '(invisible org-link)))))
  4366. (backward-char 1))
  4367. rtn))
  4368. (defun org-emphasize (&optional char)
  4369. "Insert or change an emphasis, i.e. a font like bold or italic.
  4370. If there is an active region, change that region to a new emphasis.
  4371. If there is no region, just insert the marker characters and position
  4372. the cursor between them.
  4373. CHAR should be either the marker character, or the first character of the
  4374. HTML tag associated with that emphasis. If CHAR is a space, the means
  4375. to remove the emphasis of the selected region.
  4376. If char is not given (for example in an interactive call) it
  4377. will be prompted for."
  4378. (interactive)
  4379. (let ((eal org-emphasis-alist) e det
  4380. (erc org-emphasis-regexp-components)
  4381. (prompt "")
  4382. (string "") beg end move tag c s)
  4383. (if (org-region-active-p)
  4384. (setq beg (region-beginning) end (region-end)
  4385. string (buffer-substring beg end))
  4386. (setq move t))
  4387. (while (setq e (pop eal))
  4388. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4389. c (aref tag 0))
  4390. (push (cons c (string-to-char (car e))) det)
  4391. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4392. (substring tag 1)))))
  4393. (setq det (nreverse det))
  4394. (unless char
  4395. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4396. (setq char (read-char-exclusive)))
  4397. (setq char (or (cdr (assoc char det)) char))
  4398. (if (equal char ?\ )
  4399. (setq s "" move nil)
  4400. (unless (assoc (char-to-string char) org-emphasis-alist)
  4401. (error "No such emphasis marker: \"%c\"" char))
  4402. (setq s (char-to-string char)))
  4403. (while (and (> (length string) 1)
  4404. (equal (substring string 0 1) (substring string -1))
  4405. (assoc (substring string 0 1) org-emphasis-alist))
  4406. (setq string (substring string 1 -1)))
  4407. (setq string (concat s string s))
  4408. (if beg (delete-region beg end))
  4409. (unless (or (bolp)
  4410. (string-match (concat "[" (nth 0 erc) "\n]")
  4411. (char-to-string (char-before (point)))))
  4412. (insert " "))
  4413. (unless (or (eobp)
  4414. (string-match (concat "[" (nth 1 erc) "\n]")
  4415. (char-to-string (char-after (point)))))
  4416. (insert " ") (backward-char 1))
  4417. (insert string)
  4418. (and move (backward-char 1))))
  4419. (defconst org-nonsticky-props
  4420. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4421. (defsubst org-rear-nonsticky-at (pos)
  4422. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4423. (defun org-activate-plain-links (limit)
  4424. "Run through the buffer and add overlays to links."
  4425. (catch 'exit
  4426. (let (f)
  4427. (if (re-search-forward org-plain-link-re limit t)
  4428. (progn
  4429. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4430. (setq f (get-text-property (match-beginning 0) 'face))
  4431. (if (or (eq f 'org-tag)
  4432. (and (listp f) (memq 'org-tag f)))
  4433. nil
  4434. (add-text-properties (match-beginning 0) (match-end 0)
  4435. (list 'mouse-face 'highlight
  4436. 'face 'org-link
  4437. 'keymap org-mouse-map))
  4438. (org-rear-nonsticky-at (match-end 0)))
  4439. t)))))
  4440. (defun org-activate-code (limit)
  4441. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4442. (progn
  4443. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4444. (remove-text-properties (match-beginning 0) (match-end 0)
  4445. '(display t invisible t intangible t))
  4446. t)))
  4447. (defun org-fontify-meta-lines-and-blocks (limit)
  4448. "Fontify #+ lines and blocks, in the correct ways."
  4449. (let ((case-fold-search t))
  4450. (if (re-search-forward
  4451. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4452. limit t)
  4453. (let ((beg (match-beginning 0))
  4454. (beg1 (line-beginning-position 2))
  4455. (dc1 (downcase (match-string 2)))
  4456. (dc3 (downcase (match-string 3)))
  4457. end end1 quoting block-type)
  4458. (cond
  4459. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4460. ;; a single line of backend-specific content
  4461. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4462. (remove-text-properties (match-beginning 0) (match-end 0)
  4463. '(display t invisible t intangible t))
  4464. (add-text-properties (match-beginning 1) (match-end 3)
  4465. '(font-lock-fontified t face org-meta-line))
  4466. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4467. '(font-lock-fontified t face org-block))
  4468. ; for backend-specific code
  4469. t)
  4470. ((and (match-end 4) (equal dc3 "begin"))
  4471. ;; Truly a block
  4472. (setq block-type (downcase (match-string 5))
  4473. quoting (member block-type org-protecting-blocks))
  4474. (when (re-search-forward
  4475. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4476. nil t) ;; on purpose, we look further than LIMIT
  4477. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4478. (when quoting
  4479. (remove-text-properties beg end
  4480. '(display t invisible t intangible t)))
  4481. (add-text-properties
  4482. beg end
  4483. '(font-lock-fontified t font-lock-multiline t))
  4484. (add-text-properties beg beg1 '(face org-meta-line))
  4485. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4486. ; for end_src
  4487. (cond
  4488. (quoting
  4489. (add-text-properties beg1 (+ end1 1) '(face
  4490. org-block)))
  4491. ; end of source block
  4492. ((not org-fontify-quote-and-verse-blocks))
  4493. ((string= block-type "quote")
  4494. (add-text-properties beg1 end1 '(face org-quote)))
  4495. ((string= block-type "verse")
  4496. (add-text-properties beg1 end1 '(face org-verse))))
  4497. t))
  4498. ((member dc1 '("title:" "author:" "email:" "date:"))
  4499. (add-text-properties
  4500. beg (match-end 3)
  4501. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4502. '(font-lock-fontified t invisible t)
  4503. '(font-lock-fontified t face org-document-info-keyword)))
  4504. (add-text-properties
  4505. (match-beginning 6) (match-end 6)
  4506. (if (string-equal dc1 "title:")
  4507. '(font-lock-fontified t face org-document-title)
  4508. '(font-lock-fontified t face org-document-info))))
  4509. ((not (member (char-after beg) '(?\ ?\t)))
  4510. ;; just any other in-buffer setting, but not indented
  4511. (add-text-properties
  4512. beg (match-end 0)
  4513. '(font-lock-fontified t face org-meta-line))
  4514. t)
  4515. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4516. "orgtbl:" "tblfm:" "tblname:" "result:"
  4517. "results:" "source:" "srcname:" "call:"))
  4518. (and (match-end 4) (equal dc3 "attr")))
  4519. (add-text-properties
  4520. beg (match-end 0)
  4521. '(font-lock-fontified t face org-meta-line))
  4522. t)
  4523. ((member dc3 '(" " ""))
  4524. (add-text-properties
  4525. beg (match-end 0)
  4526. '(font-lock-fontified t face font-lock-comment-face)))
  4527. (t nil))))))
  4528. (defun org-activate-angle-links (limit)
  4529. "Run through the buffer and add overlays to links."
  4530. (if (re-search-forward org-angle-link-re limit t)
  4531. (progn
  4532. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4533. (add-text-properties (match-beginning 0) (match-end 0)
  4534. (list 'mouse-face 'highlight
  4535. 'keymap org-mouse-map))
  4536. (org-rear-nonsticky-at (match-end 0))
  4537. t)))
  4538. (defun org-activate-footnote-links (limit)
  4539. "Run through the buffer and add overlays to links."
  4540. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4541. limit t)
  4542. (progn
  4543. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4544. (add-text-properties (match-beginning 2) (match-end 2)
  4545. (list 'mouse-face 'highlight
  4546. 'keymap org-mouse-map
  4547. 'help-echo
  4548. (if (= (point-at-bol) (match-beginning 2))
  4549. "Footnote definition"
  4550. "Footnote reference")
  4551. ))
  4552. (org-rear-nonsticky-at (match-end 2))
  4553. t)))
  4554. (defun org-activate-bracket-links (limit)
  4555. "Run through the buffer and add overlays to bracketed links."
  4556. (if (re-search-forward org-bracket-link-regexp limit t)
  4557. (let* ((help (concat "LINK: "
  4558. (org-match-string-no-properties 1)))
  4559. ;; FIXME: above we should remove the escapes.
  4560. ;; but that requires another match, protecting match data,
  4561. ;; a lot of overhead for font-lock.
  4562. (ip (org-maybe-intangible
  4563. (list 'invisible 'org-link
  4564. 'keymap org-mouse-map 'mouse-face 'highlight
  4565. 'font-lock-multiline t 'help-echo help)))
  4566. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4567. 'font-lock-multiline t 'help-echo help)))
  4568. ;; We need to remove the invisible property here. Table narrowing
  4569. ;; may have made some of this invisible.
  4570. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4571. (remove-text-properties (match-beginning 0) (match-end 0)
  4572. '(invisible nil))
  4573. (if (match-end 3)
  4574. (progn
  4575. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4576. (org-rear-nonsticky-at (match-beginning 3))
  4577. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4578. (org-rear-nonsticky-at (match-end 3))
  4579. (add-text-properties (match-end 3) (match-end 0) ip)
  4580. (org-rear-nonsticky-at (match-end 0)))
  4581. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4582. (org-rear-nonsticky-at (match-beginning 1))
  4583. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4584. (org-rear-nonsticky-at (match-end 1))
  4585. (add-text-properties (match-end 1) (match-end 0) ip)
  4586. (org-rear-nonsticky-at (match-end 0)))
  4587. t)))
  4588. (defun org-activate-dates (limit)
  4589. "Run through the buffer and add overlays to dates."
  4590. (if (re-search-forward org-tsr-regexp-both limit t)
  4591. (progn
  4592. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4593. (add-text-properties (match-beginning 0) (match-end 0)
  4594. (list 'mouse-face 'highlight
  4595. 'keymap org-mouse-map))
  4596. (org-rear-nonsticky-at (match-end 0))
  4597. (when org-display-custom-times
  4598. (if (match-end 3)
  4599. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4600. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4601. t)))
  4602. (defvar org-target-link-regexp nil
  4603. "Regular expression matching radio targets in plain text.")
  4604. (make-variable-buffer-local 'org-target-link-regexp)
  4605. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4606. "Regular expression matching a link target.")
  4607. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4608. "Regular expression matching a radio target.")
  4609. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4610. "Regular expression matching any target.")
  4611. (defun org-activate-target-links (limit)
  4612. "Run through the buffer and add overlays to target matches."
  4613. (when org-target-link-regexp
  4614. (let ((case-fold-search t))
  4615. (if (re-search-forward org-target-link-regexp limit t)
  4616. (progn
  4617. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4618. (add-text-properties (match-beginning 0) (match-end 0)
  4619. (list 'mouse-face 'highlight
  4620. 'keymap org-mouse-map
  4621. 'help-echo "Radio target link"
  4622. 'org-linked-text t))
  4623. (org-rear-nonsticky-at (match-end 0))
  4624. t)))))
  4625. (defun org-update-radio-target-regexp ()
  4626. "Find all radio targets in this file and update the regular expression."
  4627. (interactive)
  4628. (when (memq 'radio org-activate-links)
  4629. (setq org-target-link-regexp
  4630. (org-make-target-link-regexp (org-all-targets 'radio)))
  4631. (org-restart-font-lock)))
  4632. (defun org-hide-wide-columns (limit)
  4633. (let (s e)
  4634. (setq s (text-property-any (point) (or limit (point-max))
  4635. 'org-cwidth t))
  4636. (when s
  4637. (setq e (next-single-property-change s 'org-cwidth))
  4638. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4639. (goto-char e)
  4640. t)))
  4641. (defvar org-latex-and-specials-regexp nil
  4642. "Regular expression for highlighting export special stuff.")
  4643. (defvar org-match-substring-regexp)
  4644. (defvar org-match-substring-with-braces-regexp)
  4645. ;; This should be with the exporter code, but we also use if for font-locking
  4646. (defconst org-export-html-special-string-regexps
  4647. '(("\\\\-" . "&shy;")
  4648. ("---\\([^-]\\)" . "&mdash;\\1")
  4649. ("--\\([^-]\\)" . "&ndash;\\1")
  4650. ("\\.\\.\\." . "&hellip;"))
  4651. "Regular expressions for special string conversion.")
  4652. (defun org-compute-latex-and-specials-regexp ()
  4653. "Compute regular expression for stuff treated specially by exporters."
  4654. (if (not org-highlight-latex-fragments-and-specials)
  4655. (org-set-local 'org-latex-and-specials-regexp nil)
  4656. (require 'org-exp)
  4657. (let*
  4658. ((matchers (plist-get org-format-latex-options :matchers))
  4659. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4660. org-latex-regexps)))
  4661. (org-export-allow-BIND nil)
  4662. (options (org-combine-plists (org-default-export-plist)
  4663. (org-infile-export-plist)))
  4664. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4665. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4666. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4667. (org-export-html-expand (plist-get options :expand-quoted-html))
  4668. (org-export-with-special-strings (plist-get options :special-strings))
  4669. (re-sub
  4670. (cond
  4671. ((equal org-export-with-sub-superscripts '{})
  4672. (list org-match-substring-with-braces-regexp))
  4673. (org-export-with-sub-superscripts
  4674. (list org-match-substring-regexp))
  4675. (t nil)))
  4676. (re-latex
  4677. (if org-export-with-LaTeX-fragments
  4678. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4679. (re-macros
  4680. (if org-export-with-TeX-macros
  4681. (list (concat "\\\\"
  4682. (regexp-opt
  4683. (append
  4684. (delq nil
  4685. (mapcar 'car-safe
  4686. (append org-entities-user
  4687. org-entities)))
  4688. (if (boundp 'org-latex-entities)
  4689. (mapcar (lambda (x)
  4690. (or (car-safe x) x))
  4691. org-latex-entities)
  4692. nil))
  4693. 'words))) ; FIXME
  4694. ))
  4695. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4696. (re-special (if org-export-with-special-strings
  4697. (mapcar (lambda (x) (car x))
  4698. org-export-html-special-string-regexps)))
  4699. (re-rest
  4700. (delq nil
  4701. (list
  4702. (if org-export-html-expand "@<[^>\n]+>")
  4703. ))))
  4704. (org-set-local
  4705. 'org-latex-and-specials-regexp
  4706. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4707. re-rest) "\\|")))))
  4708. (defun org-do-latex-and-special-faces (limit)
  4709. "Run through the buffer and add overlays to links."
  4710. (when org-latex-and-specials-regexp
  4711. (let (rtn d)
  4712. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4713. limit t))
  4714. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4715. 'face))
  4716. '(org-code org-verbatim underline)))
  4717. (progn
  4718. (setq rtn t
  4719. d (cond ((member (char-after (1+ (match-beginning 0)))
  4720. '(?_ ?^)) 1)
  4721. (t 0)))
  4722. (font-lock-prepend-text-property
  4723. (+ d (match-beginning 0)) (match-end 0)
  4724. 'face 'org-latex-and-export-specials)
  4725. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4726. '(font-lock-multiline t)))))
  4727. rtn)))
  4728. (defun org-restart-font-lock ()
  4729. "Restart `font-lock-mode', to force refontification."
  4730. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4731. (font-lock-mode -1)
  4732. (font-lock-mode 1)))
  4733. (defun org-all-targets (&optional radio)
  4734. "Return a list of all targets in this file.
  4735. With optional argument RADIO, only find radio targets."
  4736. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4737. rtn)
  4738. (save-excursion
  4739. (goto-char (point-min))
  4740. (while (re-search-forward re nil t)
  4741. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4742. rtn)))
  4743. (defun org-make-target-link-regexp (targets)
  4744. "Make regular expression matching all strings in TARGETS.
  4745. The regular expression finds the targets also if there is a line break
  4746. between words."
  4747. (and targets
  4748. (concat
  4749. "\\<\\("
  4750. (mapconcat
  4751. (lambda (x)
  4752. (while (string-match " +" x)
  4753. (setq x (replace-match "\\s-+" t t x)))
  4754. x)
  4755. targets
  4756. "\\|")
  4757. "\\)\\>")))
  4758. (defun org-activate-tags (limit)
  4759. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4760. (progn
  4761. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4762. (add-text-properties (match-beginning 1) (match-end 1)
  4763. (list 'mouse-face 'highlight
  4764. 'keymap org-mouse-map))
  4765. (org-rear-nonsticky-at (match-end 1))
  4766. t)))
  4767. (defun org-outline-level ()
  4768. "Compute the outline level of the heading at point.
  4769. This function assumes that the cursor is at the beginning of a line matched
  4770. by `outline-regexp'. Otherwise it returns garbage.
  4771. If this is called at a normal headline, the level is the number of stars.
  4772. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4773. For plain list items, if they are matched by `outline-regexp', this returns
  4774. 1000 plus the line indentation."
  4775. (save-excursion
  4776. (looking-at outline-regexp)
  4777. (if (match-beginning 1)
  4778. (+ (org-get-string-indentation (match-string 1)) 1000)
  4779. (1- (- (match-end 0) (match-beginning 0))))))
  4780. (defvar org-font-lock-keywords nil)
  4781. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4782. "Regular expression matching a property line.")
  4783. (defvar org-font-lock-hook nil
  4784. "Functions to be called for special font lock stuff.")
  4785. (defvar org-font-lock-set-keywords-hook nil
  4786. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4787. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4788. it is installed to be used by font lock. This can be useful if something
  4789. needs to be inserted at a specific position in the font-lock sequence.")
  4790. (defun org-font-lock-hook (limit)
  4791. (run-hook-with-args 'org-font-lock-hook limit))
  4792. (defun org-set-font-lock-defaults ()
  4793. (let* ((em org-fontify-emphasized-text)
  4794. (lk org-activate-links)
  4795. (org-font-lock-extra-keywords
  4796. (list
  4797. ;; Call the hook
  4798. '(org-font-lock-hook)
  4799. ;; Headlines
  4800. `(,(if org-fontify-whole-heading-line
  4801. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4802. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4803. (1 (org-get-level-face 1))
  4804. (2 (org-get-level-face 2))
  4805. (3 (org-get-level-face 3)))
  4806. ;; Table lines
  4807. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4808. (1 'org-table t))
  4809. ;; Table internals
  4810. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4811. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4812. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4813. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4814. ;; Drawers
  4815. (list org-drawer-regexp '(0 'org-special-keyword t))
  4816. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4817. ;; Properties
  4818. (list org-property-re
  4819. '(1 'org-special-keyword t)
  4820. '(3 'org-property-value t))
  4821. ;; Links
  4822. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4823. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4824. (if (memq 'plain lk) '(org-activate-plain-links))
  4825. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4826. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4827. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4828. (if (memq 'footnote lk) '(org-activate-footnote-links
  4829. (2 'org-footnote t)))
  4830. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4831. '(org-hide-wide-columns (0 nil append))
  4832. ;; TODO lines
  4833. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4834. '(1 (org-get-todo-face 1) t))
  4835. ;; DONE
  4836. (if org-fontify-done-headline
  4837. (list (concat "^[*]+ +\\<\\("
  4838. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4839. "\\)\\(.*\\)")
  4840. '(2 'org-headline-done t))
  4841. nil)
  4842. ;; Priorities
  4843. '(org-font-lock-add-priority-faces)
  4844. ;; Tags
  4845. '(org-font-lock-add-tag-faces)
  4846. ;; Special keywords
  4847. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4848. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4849. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4850. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4851. ;; Emphasis
  4852. (if em
  4853. (if (featurep 'xemacs)
  4854. '(org-do-emphasis-faces (0 nil append))
  4855. '(org-do-emphasis-faces)))
  4856. ;; Checkboxes
  4857. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4858. 2 'org-checkbox prepend)
  4859. (if org-provide-checkbox-statistics
  4860. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4861. (0 (org-get-checkbox-statistics-face) t)))
  4862. ;; Description list items
  4863. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4864. 2 'bold prepend)
  4865. ;; ARCHIVEd headings
  4866. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4867. '(1 'org-archived prepend))
  4868. ;; Specials
  4869. '(org-do-latex-and-special-faces)
  4870. '(org-fontify-entities)
  4871. '(org-raise-scripts)
  4872. ;; Code
  4873. '(org-activate-code (1 'org-code t))
  4874. ;; COMMENT
  4875. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4876. "\\|" org-quote-string "\\)\\>")
  4877. '(1 'org-special-keyword t))
  4878. '("^#.*" (0 'font-lock-comment-face t))
  4879. ;; Blocks and meta lines
  4880. '(org-fontify-meta-lines-and-blocks)
  4881. )))
  4882. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4883. (run-hooks 'org-font-lock-set-keywords-hook)
  4884. ;; Now set the full font-lock-keywords
  4885. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4886. (org-set-local 'font-lock-defaults
  4887. '(org-font-lock-keywords t nil nil backward-paragraph))
  4888. (kill-local-variable 'font-lock-keywords) nil))
  4889. (defun org-toggle-pretty-entities ()
  4890. "Toggle the composition display of entities as UTF8 characters."
  4891. (interactive)
  4892. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4893. (org-restart-font-lock)
  4894. (if org-pretty-entities
  4895. (message "Entities are displayed as UTF8 characers")
  4896. (save-restriction
  4897. (widen)
  4898. (decompose-region (point-min) (point-max))
  4899. (message "Entities are displayed plain"))))
  4900. (defun org-fontify-entities (limit)
  4901. "Find an entity to fontify."
  4902. (let (ee)
  4903. (when org-pretty-entities
  4904. (catch 'match
  4905. (while (re-search-forward
  4906. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4907. limit t)
  4908. (if (and (not (org-in-indented-comment-line))
  4909. (setq ee (org-entity-get (match-string 1)))
  4910. (= (length (nth 6 ee)) 1))
  4911. (progn
  4912. (add-text-properties
  4913. (match-beginning 0) (match-end 1)
  4914. (list 'font-lock-fontified t))
  4915. (compose-region (match-beginning 0) (match-end 1)
  4916. (nth 6 ee) nil)
  4917. (backward-char 1)
  4918. (throw 'match t))))
  4919. nil))))
  4920. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4921. "Fontify string S like in Org-mode."
  4922. (with-temp-buffer
  4923. (insert s)
  4924. (let ((org-odd-levels-only odd-levels))
  4925. (org-mode)
  4926. (font-lock-fontify-buffer)
  4927. (buffer-string))))
  4928. (defvar org-m nil)
  4929. (defvar org-l nil)
  4930. (defvar org-f nil)
  4931. (defun org-get-level-face (n)
  4932. "Get the right face for match N in font-lock matching of headlines."
  4933. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4934. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4935. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4936. (cond
  4937. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4938. ((eq n 2) org-f)
  4939. (t (if org-level-color-stars-only nil org-f))))
  4940. (defun org-get-todo-face (kwd)
  4941. "Get the right face for a TODO keyword KWD.
  4942. If KWD is a number, get the corresponding match group."
  4943. (if (numberp kwd) (setq kwd (match-string kwd)))
  4944. (or (org-face-from-face-or-color
  4945. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4946. (and (member kwd org-done-keywords) 'org-done)
  4947. 'org-todo))
  4948. (defun org-face-from-face-or-color (context inherit face-or-color)
  4949. "Create a face list that inherits INHERIT, but sets the foreground color.
  4950. When FACE-OR-COLOR is not a string, just return it."
  4951. (if (stringp face-or-color)
  4952. (list :inherit inherit
  4953. (cdr (assoc context org-faces-easy-properties))
  4954. face-or-color)
  4955. face-or-color))
  4956. (defun org-font-lock-add-tag-faces (limit)
  4957. "Add the special tag faces."
  4958. (when (and org-tag-faces org-tags-special-faces-re)
  4959. (while (re-search-forward org-tags-special-faces-re limit t)
  4960. (add-text-properties (match-beginning 1) (match-end 1)
  4961. (list 'face (org-get-tag-face 1)
  4962. 'font-lock-fontified t))
  4963. (backward-char 1))))
  4964. (defun org-font-lock-add-priority-faces (limit)
  4965. "Add the special priority faces."
  4966. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4967. (add-text-properties
  4968. (match-beginning 0) (match-end 0)
  4969. (list 'face (or (org-face-from-face-or-color
  4970. 'priority 'org-special-keyword
  4971. (cdr (assoc (char-after (match-beginning 1))
  4972. org-priority-faces)))
  4973. 'org-special-keyword)
  4974. 'font-lock-fontified t))))
  4975. (defun org-get-tag-face (kwd)
  4976. "Get the right face for a TODO keyword KWD.
  4977. If KWD is a number, get the corresponding match group."
  4978. (if (numberp kwd) (setq kwd (match-string kwd)))
  4979. (or (org-face-from-face-or-color
  4980. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4981. 'org-tag))
  4982. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4983. "Remove fontification and activation overlays from links."
  4984. (font-lock-default-unfontify-region beg end)
  4985. (let* ((buffer-undo-list t)
  4986. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4987. (inhibit-modification-hooks t)
  4988. deactivate-mark buffer-file-name buffer-file-truename)
  4989. (decompose-region beg end)
  4990. (remove-text-properties
  4991. beg end
  4992. (if org-indent-mode
  4993. ;; also remove line-prefix and wrap-prefix properties
  4994. '(mouse-face t keymap t org-linked-text t
  4995. invisible t intangible t
  4996. line-prefix t wrap-prefix t
  4997. org-no-flyspell t org-emphasis t)
  4998. '(mouse-face t keymap t org-linked-text t
  4999. invisible t intangible t
  5000. org-no-flyspell t org-emphasis t)))
  5001. (org-remove-font-lock-display-properties beg end)))
  5002. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5003. ((raise 0.3) (height 0.7))
  5004. ((raise -0.5))
  5005. ((raise 0.5)))
  5006. "Display properties for showing superscripts and subscripts.")
  5007. (defun org-remove-font-lock-display-properties (beg end)
  5008. "Remove specific display properties that have been added by font lock.
  5009. The will remove the raise properties that are used to show superscripts
  5010. and subscripts."
  5011. (let (next prop)
  5012. (while (< beg end)
  5013. (setq next (next-single-property-change beg 'display nil end)
  5014. prop (get-text-property beg 'display))
  5015. (if (member prop org-script-display)
  5016. (put-text-property beg next 'display nil))
  5017. (setq beg next))))
  5018. (defun org-raise-scripts (limit)
  5019. "Add raise properties to sub/superscripts."
  5020. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5021. (if (re-search-forward
  5022. (if (eq org-use-sub-superscripts t)
  5023. org-match-substring-regexp
  5024. org-match-substring-with-braces-regexp)
  5025. limit t)
  5026. (let* ((pos (point)) table-p comment-p
  5027. (mpos (match-beginning 3))
  5028. (emph-p (get-text-property mpos 'org-emphasis))
  5029. (link-p (get-text-property mpos 'mouse-face))
  5030. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5031. (goto-char (point-at-bol))
  5032. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5033. comment-p (org-looking-at-p "[ \t]*#"))
  5034. (goto-char pos)
  5035. ;; FIXME: Should we go back one character here, for a_b^c
  5036. ;; (goto-char (1- pos)) ;????????????????????
  5037. (if (or comment-p emph-p link-p keyw-p)
  5038. t
  5039. (put-text-property (match-beginning 3) (match-end 0)
  5040. 'display
  5041. (if (equal (char-after (match-beginning 2)) ?^)
  5042. (nth (if table-p 3 1) org-script-display)
  5043. (nth (if table-p 2 0) org-script-display)))
  5044. (add-text-properties (match-beginning 2) (match-end 2)
  5045. (list 'invisible t
  5046. 'org-dwidth t 'org-dwidth-n 1))
  5047. (if (and (eq (char-after (match-beginning 3)) ?{)
  5048. (eq (char-before (match-end 3)) ?}))
  5049. (progn
  5050. (add-text-properties
  5051. (match-beginning 3) (1+ (match-beginning 3))
  5052. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5053. (add-text-properties
  5054. (1- (match-end 3)) (match-end 3)
  5055. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5056. t)))))
  5057. ;;;; Visibility cycling, including org-goto and indirect buffer
  5058. ;;; Cycling
  5059. (defvar org-cycle-global-status nil)
  5060. (make-variable-buffer-local 'org-cycle-global-status)
  5061. (defvar org-cycle-subtree-status nil)
  5062. (make-variable-buffer-local 'org-cycle-subtree-status)
  5063. ;;;###autoload
  5064. (defvar org-inlinetask-min-level)
  5065. (defun org-cycle (&optional arg)
  5066. "TAB-action and visibility cycling for Org-mode.
  5067. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5068. is outline visibility cycling, but it also invokes other actions
  5069. in special contexts.
  5070. - When this function is called with a prefix argument, rotate the entire
  5071. buffer through 3 states (global cycling)
  5072. 1. OVERVIEW: Show only top-level headlines.
  5073. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5074. 3. SHOW ALL: Show everything.
  5075. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5076. determined by the variable `org-startup-folded', and by any VISIBILITY
  5077. properties in the buffer.
  5078. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5079. including any drawers.
  5080. - When inside a table, re-align the table and move to the next field.
  5081. - When point is at the beginning of a headline, rotate the subtree started
  5082. by this line through 3 different states (local cycling)
  5083. 1. FOLDED: Only the main headline is shown.
  5084. 2. CHILDREN: The main headline and the direct children are shown.
  5085. From this state, you can move to one of the children
  5086. and zoom in further.
  5087. 3. SUBTREE: Show the entire subtree, including body text.
  5088. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5089. - When point is at the beginning of an empty headline and the variable
  5090. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5091. of the headline by demoting and promoting it to likely levels. This
  5092. speeds up creation document structure by pressing TAB once or several
  5093. times right after creating a new headline.
  5094. - When there is a numeric prefix, go up to a heading with level ARG, do
  5095. a `show-subtree' and return to the previous cursor position. If ARG
  5096. is negative, go up that many levels.
  5097. - When point is not at the beginning of a headline, execute the global
  5098. binding for TAB, which is re-indenting the line. See the option
  5099. `org-cycle-emulate-tab' for details.
  5100. - Special case: if point is at the beginning of the buffer and there is
  5101. no headline in line 1, this function will act as if called with prefix arg.
  5102. But only if also the variable `org-cycle-global-at-bob' is t."
  5103. (interactive "P")
  5104. (org-load-modules-maybe)
  5105. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5106. (and org-cycle-level-after-item/entry-creation
  5107. (or (org-cycle-level)
  5108. (org-cycle-item-indentation))))
  5109. (let* ((limit-level
  5110. (or org-cycle-max-level
  5111. (and (boundp 'org-inlinetask-min-level)
  5112. org-inlinetask-min-level
  5113. (1- org-inlinetask-min-level))))
  5114. (nstars (and limit-level
  5115. (if org-odd-levels-only
  5116. (and limit-level (1- (* limit-level 2)))
  5117. limit-level)))
  5118. (outline-regexp
  5119. (cond
  5120. ((not (org-mode-p)) outline-regexp)
  5121. ((or (eq org-cycle-include-plain-lists 'integrate)
  5122. (and org-cycle-include-plain-lists (org-at-item-p)))
  5123. (concat "\\(?:\\*"
  5124. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5125. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5126. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5127. (bob-special (and org-cycle-global-at-bob (bobp)
  5128. (not (looking-at outline-regexp))))
  5129. (org-cycle-hook
  5130. (if bob-special
  5131. (delq 'org-optimize-window-after-visibility-change
  5132. (copy-sequence org-cycle-hook))
  5133. org-cycle-hook))
  5134. (pos (point)))
  5135. (if (or bob-special (equal arg '(4)))
  5136. ;; special case: use global cycling
  5137. (setq arg t))
  5138. (cond
  5139. ((equal arg '(16))
  5140. (org-set-startup-visibility)
  5141. (message "Startup visibility, plus VISIBILITY properties"))
  5142. ((equal arg '(64))
  5143. (show-all)
  5144. (message "Entire buffer visible, including drawers"))
  5145. ((org-at-table-p 'any)
  5146. ;; Enter the table or move to the next field in the table
  5147. (if (org-at-table.el-p)
  5148. (message "Use C-c ' to edit table.el tables")
  5149. (if arg (org-table-edit-field t)
  5150. (org-table-justify-field-maybe)
  5151. (call-interactively 'org-table-next-field))))
  5152. ((run-hook-with-args-until-success
  5153. 'org-tab-after-check-for-table-hook))
  5154. ((eq arg t) ;; Global cycling
  5155. (org-cycle-internal-global))
  5156. ((and org-drawers org-drawer-regexp
  5157. (save-excursion
  5158. (beginning-of-line 1)
  5159. (looking-at org-drawer-regexp)))
  5160. ;; Toggle block visibility
  5161. (org-flag-drawer
  5162. (not (get-char-property (match-end 0) 'invisible))))
  5163. ((integerp arg)
  5164. ;; Show-subtree, ARG levels up from here.
  5165. (save-excursion
  5166. (org-back-to-heading)
  5167. (outline-up-heading (if (< arg 0) (- arg)
  5168. (- (funcall outline-level) arg)))
  5169. (org-show-subtree)))
  5170. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5171. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5172. (org-cycle-internal-local))
  5173. ;; TAB emulation and template completion
  5174. (buffer-read-only (org-back-to-heading))
  5175. ((run-hook-with-args-until-success
  5176. 'org-tab-after-check-for-cycling-hook))
  5177. ((org-try-structure-completion))
  5178. ((org-try-cdlatex-tab))
  5179. ((run-hook-with-args-until-success
  5180. 'org-tab-before-tab-emulation-hook))
  5181. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5182. (or (not (bolp))
  5183. (not (looking-at outline-regexp))))
  5184. (call-interactively (global-key-binding "\t")))
  5185. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5186. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5187. (or (and (eq org-cycle-emulate-tab 'white)
  5188. (= (match-end 0) (point-at-eol)))
  5189. (and (eq org-cycle-emulate-tab 'whitestart)
  5190. (>= (match-end 0) pos))))
  5191. t
  5192. (eq org-cycle-emulate-tab t))
  5193. (call-interactively (global-key-binding "\t")))
  5194. (t (save-excursion
  5195. (org-back-to-heading)
  5196. (org-cycle)))))))
  5197. (defun org-cycle-internal-global ()
  5198. "Do the global cycling action."
  5199. (cond
  5200. ((and (eq last-command this-command)
  5201. (eq org-cycle-global-status 'overview))
  5202. ;; We just created the overview - now do table of contents
  5203. ;; This can be slow in very large buffers, so indicate action
  5204. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5205. (message "CONTENTS...")
  5206. (org-content)
  5207. (message "CONTENTS...done")
  5208. (setq org-cycle-global-status 'contents)
  5209. (run-hook-with-args 'org-cycle-hook 'contents))
  5210. ((and (eq last-command this-command)
  5211. (eq org-cycle-global-status 'contents))
  5212. ;; We just showed the table of contents - now show everything
  5213. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5214. (show-all)
  5215. (message "SHOW ALL")
  5216. (setq org-cycle-global-status 'all)
  5217. (run-hook-with-args 'org-cycle-hook 'all))
  5218. (t
  5219. ;; Default action: go to overview
  5220. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5221. (org-overview)
  5222. (message "OVERVIEW")
  5223. (setq org-cycle-global-status 'overview)
  5224. (run-hook-with-args 'org-cycle-hook 'overview))))
  5225. (defun org-cycle-internal-local ()
  5226. "Do the local cycling action."
  5227. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5228. ;; First, some boundaries
  5229. (save-excursion
  5230. (org-back-to-heading)
  5231. (setq level (funcall outline-level))
  5232. (save-excursion
  5233. (beginning-of-line 2)
  5234. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5235. ; XEmacs does not have `next-single-char-property-change'
  5236. ; I'm not sure about Emacs 21.
  5237. (while (and (not (eobp)) ;; this is like `next-line'
  5238. (get-char-property (1- (point)) 'invisible))
  5239. (beginning-of-line 2))
  5240. (while (and (not (eobp)) ;; this is like `next-line'
  5241. (get-char-property (1- (point)) 'invisible))
  5242. (goto-char (next-single-char-property-change (point) 'invisible))
  5243. (and (eolp) (beginning-of-line 2))))
  5244. (setq eol (point)))
  5245. (outline-end-of-heading) (setq eoh (point))
  5246. (save-excursion
  5247. (outline-next-heading)
  5248. (setq has-children (and (org-at-heading-p t)
  5249. (> (funcall outline-level) level))))
  5250. (org-end-of-subtree t)
  5251. (unless (eobp)
  5252. (skip-chars-forward " \t\n")
  5253. (beginning-of-line 1) ; in case this is an item
  5254. )
  5255. (setq eos (if (eobp) (point) (1- (point)))))
  5256. ;; Find out what to do next and set `this-command'
  5257. (cond
  5258. ((= eos eoh)
  5259. ;; Nothing is hidden behind this heading
  5260. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5261. (message "EMPTY ENTRY")
  5262. (setq org-cycle-subtree-status nil)
  5263. (save-excursion
  5264. (goto-char eos)
  5265. (outline-next-heading)
  5266. (if (org-invisible-p) (org-flag-heading nil))))
  5267. ((and (or (>= eol eos)
  5268. (not (string-match "\\S-" (buffer-substring eol eos))))
  5269. (or has-children
  5270. (not (setq children-skipped
  5271. org-cycle-skip-children-state-if-no-children))))
  5272. ;; Entire subtree is hidden in one line: children view
  5273. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5274. (org-show-entry)
  5275. (show-children)
  5276. (message "CHILDREN")
  5277. (save-excursion
  5278. (goto-char eos)
  5279. (outline-next-heading)
  5280. (if (org-invisible-p) (org-flag-heading nil)))
  5281. (setq org-cycle-subtree-status 'children)
  5282. (run-hook-with-args 'org-cycle-hook 'children))
  5283. ((or children-skipped
  5284. (and (eq last-command this-command)
  5285. (eq org-cycle-subtree-status 'children)))
  5286. ;; We just showed the children, or no children are there,
  5287. ;; now show everything.
  5288. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5289. (org-show-subtree)
  5290. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5291. (setq org-cycle-subtree-status 'subtree)
  5292. (run-hook-with-args 'org-cycle-hook 'subtree))
  5293. (t
  5294. ;; Default action: hide the subtree.
  5295. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5296. (hide-subtree)
  5297. (message "FOLDED")
  5298. (setq org-cycle-subtree-status 'folded)
  5299. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5300. ;;;###autoload
  5301. (defun org-global-cycle (&optional arg)
  5302. "Cycle the global visibility. For details see `org-cycle'.
  5303. With \\[universal-argument] prefix arg, switch to startup visibility.
  5304. With a numeric prefix, show all headlines up to that level."
  5305. (interactive "P")
  5306. (let ((org-cycle-include-plain-lists
  5307. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5308. (cond
  5309. ((integerp arg)
  5310. (show-all)
  5311. (hide-sublevels arg)
  5312. (setq org-cycle-global-status 'contents))
  5313. ((equal arg '(4))
  5314. (org-set-startup-visibility)
  5315. (message "Startup visibility, plus VISIBILITY properties."))
  5316. (t
  5317. (org-cycle '(4))))))
  5318. (defun org-set-startup-visibility ()
  5319. "Set the visibility required by startup options and properties."
  5320. (cond
  5321. ((eq org-startup-folded t)
  5322. (org-cycle '(4)))
  5323. ((eq org-startup-folded 'content)
  5324. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5325. (org-cycle '(4)) (org-cycle '(4)))))
  5326. (unless (eq org-startup-folded 'showeverything)
  5327. (if org-hide-block-startup (org-hide-block-all))
  5328. (org-set-visibility-according-to-property 'no-cleanup)
  5329. (org-cycle-hide-archived-subtrees 'all)
  5330. (org-cycle-hide-drawers 'all)
  5331. (org-cycle-show-empty-lines t)))
  5332. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5333. "Switch subtree visibilities according to :VISIBILITY: property."
  5334. (interactive)
  5335. (let (org-show-entry-below state)
  5336. (save-excursion
  5337. (goto-char (point-max))
  5338. (while (re-search-backward
  5339. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5340. nil t)
  5341. (setq state (match-string 1))
  5342. (save-excursion
  5343. (org-back-to-heading t)
  5344. (hide-subtree)
  5345. (org-reveal)
  5346. (cond
  5347. ((equal state '("fold" "folded"))
  5348. (hide-subtree))
  5349. ((equal state "children")
  5350. (org-show-hidden-entry)
  5351. (show-children))
  5352. ((equal state "content")
  5353. (save-excursion
  5354. (save-restriction
  5355. (org-narrow-to-subtree)
  5356. (org-content))))
  5357. ((member state '("all" "showall"))
  5358. (show-subtree)))))
  5359. (unless no-cleanup
  5360. (org-cycle-hide-archived-subtrees 'all)
  5361. (org-cycle-hide-drawers 'all)
  5362. (org-cycle-show-empty-lines 'all)))))
  5363. (defun org-overview ()
  5364. "Switch to overview mode, showing only top-level headlines.
  5365. Really, this shows all headlines with level equal or greater than the level
  5366. of the first headline in the buffer. This is important, because if the
  5367. first headline is not level one, then (hide-sublevels 1) gives confusing
  5368. results."
  5369. (interactive)
  5370. (let ((level (save-excursion
  5371. (goto-char (point-min))
  5372. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5373. (progn
  5374. (goto-char (match-beginning 0))
  5375. (funcall outline-level))))))
  5376. (and level (hide-sublevels level))))
  5377. (defun org-content (&optional arg)
  5378. "Show all headlines in the buffer, like a table of contents.
  5379. With numerical argument N, show content up to level N."
  5380. (interactive "P")
  5381. (save-excursion
  5382. ;; Visit all headings and show their offspring
  5383. (and (integerp arg) (org-overview))
  5384. (goto-char (point-max))
  5385. (catch 'exit
  5386. (while (and (progn (condition-case nil
  5387. (outline-previous-visible-heading 1)
  5388. (error (goto-char (point-min))))
  5389. t)
  5390. (looking-at outline-regexp))
  5391. (if (integerp arg)
  5392. (show-children (1- arg))
  5393. (show-branches))
  5394. (if (bobp) (throw 'exit nil))))))
  5395. (defun org-optimize-window-after-visibility-change (state)
  5396. "Adjust the window after a change in outline visibility.
  5397. This function is the default value of the hook `org-cycle-hook'."
  5398. (when (get-buffer-window (current-buffer))
  5399. (cond
  5400. ((eq state 'content) nil)
  5401. ((eq state 'all) nil)
  5402. ((eq state 'folded) nil)
  5403. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5404. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5405. (defun org-remove-empty-overlays-at (pos)
  5406. "Remove outline overlays that do not contain non-white stuff."
  5407. (mapc
  5408. (lambda (o)
  5409. (and (eq 'outline (overlay-get o 'invisible))
  5410. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5411. (overlay-end o))))
  5412. (delete-overlay o)))
  5413. (overlays-at pos)))
  5414. (defun org-clean-visibility-after-subtree-move ()
  5415. "Fix visibility issues after moving a subtree."
  5416. ;; First, find a reasonable region to look at:
  5417. ;; Start two siblings above, end three below
  5418. (let* ((beg (save-excursion
  5419. (and (org-get-last-sibling)
  5420. (org-get-last-sibling))
  5421. (point)))
  5422. (end (save-excursion
  5423. (and (org-get-next-sibling)
  5424. (org-get-next-sibling)
  5425. (org-get-next-sibling))
  5426. (if (org-at-heading-p)
  5427. (point-at-eol)
  5428. (point))))
  5429. (level (looking-at "\\*+"))
  5430. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5431. (save-excursion
  5432. (save-restriction
  5433. (narrow-to-region beg end)
  5434. (when re
  5435. ;; Properly fold already folded siblings
  5436. (goto-char (point-min))
  5437. (while (re-search-forward re nil t)
  5438. (if (and (not (org-invisible-p))
  5439. (save-excursion
  5440. (goto-char (point-at-eol)) (org-invisible-p)))
  5441. (hide-entry))))
  5442. (org-cycle-show-empty-lines 'overview)
  5443. (org-cycle-hide-drawers 'overview)))))
  5444. (defun org-cycle-show-empty-lines (state)
  5445. "Show empty lines above all visible headlines.
  5446. The region to be covered depends on STATE when called through
  5447. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5448. entire buffer covered. Note that an empty line is only shown if there
  5449. are at least `org-cycle-separator-lines' empty lines before the headline."
  5450. (when (not (= org-cycle-separator-lines 0))
  5451. (save-excursion
  5452. (let* ((n (abs org-cycle-separator-lines))
  5453. (re (cond
  5454. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5455. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5456. (t (let ((ns (number-to-string (- n 2))))
  5457. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5458. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5459. beg end b e)
  5460. (cond
  5461. ((memq state '(overview contents t))
  5462. (setq beg (point-min) end (point-max)))
  5463. ((memq state '(children folded))
  5464. (setq beg (point) end (progn (org-end-of-subtree t t)
  5465. (beginning-of-line 2)
  5466. (point)))))
  5467. (when beg
  5468. (goto-char beg)
  5469. (while (re-search-forward re end t)
  5470. (unless (get-char-property (match-end 1) 'invisible)
  5471. (setq e (match-end 1))
  5472. (if (< org-cycle-separator-lines 0)
  5473. (setq b (save-excursion
  5474. (goto-char (match-beginning 0))
  5475. (org-back-over-empty-lines)
  5476. (if (save-excursion
  5477. (goto-char (max (point-min) (1- (point))))
  5478. (org-on-heading-p))
  5479. (1- (point))
  5480. (point))))
  5481. (setq b (match-beginning 1)))
  5482. (outline-flag-region b e nil)))))))
  5483. ;; Never hide empty lines at the end of the file.
  5484. (save-excursion
  5485. (goto-char (point-max))
  5486. (outline-previous-heading)
  5487. (outline-end-of-heading)
  5488. (if (and (looking-at "[ \t\n]+")
  5489. (= (match-end 0) (point-max)))
  5490. (outline-flag-region (point) (match-end 0) nil))))
  5491. (defun org-show-empty-lines-in-parent ()
  5492. "Move to the parent and re-show empty lines before visible headlines."
  5493. (save-excursion
  5494. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5495. (org-cycle-show-empty-lines context))))
  5496. (defun org-files-list ()
  5497. "Return `org-agenda-files' list, plus all open org-mode files.
  5498. This is useful for operations that need to scan all of a user's
  5499. open and agenda-wise Org files."
  5500. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5501. (dolist (buf (buffer-list))
  5502. (with-current-buffer buf
  5503. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5504. (let ((file (expand-file-name (buffer-file-name))))
  5505. (unless (member file files)
  5506. (push file files))))))
  5507. files))
  5508. (defsubst org-entry-beginning-position ()
  5509. "Return the beginning position of the current entry."
  5510. (save-excursion (outline-back-to-heading t) (point)))
  5511. (defsubst org-entry-end-position ()
  5512. "Return the end position of the current entry."
  5513. (save-excursion (outline-next-heading) (point)))
  5514. (defun org-cycle-hide-drawers (state)
  5515. "Re-hide all drawers after a visibility state change."
  5516. (when (and (org-mode-p)
  5517. (not (memq state '(overview folded contents))))
  5518. (save-excursion
  5519. (let* ((globalp (memq state '(contents all)))
  5520. (beg (if globalp (point-min) (point)))
  5521. (end (if globalp (point-max)
  5522. (if (eq state 'children)
  5523. (save-excursion (outline-next-heading) (point))
  5524. (org-end-of-subtree t)))))
  5525. (goto-char beg)
  5526. (while (re-search-forward org-drawer-regexp end t)
  5527. (org-flag-drawer t))))))
  5528. (defun org-flag-drawer (flag)
  5529. (save-excursion
  5530. (beginning-of-line 1)
  5531. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5532. (let ((b (match-end 0))
  5533. (outline-regexp org-outline-regexp))
  5534. (if (re-search-forward
  5535. "^[ \t]*:END:"
  5536. (save-excursion (outline-next-heading) (point)) t)
  5537. (outline-flag-region b (point-at-eol) flag)
  5538. (error ":END: line missing at position %s" b))))))
  5539. (defun org-subtree-end-visible-p ()
  5540. "Is the end of the current subtree visible?"
  5541. (pos-visible-in-window-p
  5542. (save-excursion (org-end-of-subtree t) (point))))
  5543. (defun org-first-headline-recenter (&optional N)
  5544. "Move cursor to the first headline and recenter the headline.
  5545. Optional argument N means put the headline into the Nth line of the window."
  5546. (goto-char (point-min))
  5547. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5548. (beginning-of-line)
  5549. (recenter (prefix-numeric-value N))))
  5550. ;;; Saving and restoring visibility
  5551. (defun org-outline-overlay-data (&optional use-markers)
  5552. "Return a list of the locations of all outline overlays.
  5553. The are overlays with the `invisible' property value `outline'.
  5554. The return values is a list of cons cells, with start and stop
  5555. positions for each overlay.
  5556. If USE-MARKERS is set, return the positions as markers."
  5557. (let (beg end)
  5558. (save-excursion
  5559. (save-restriction
  5560. (widen)
  5561. (delq nil
  5562. (mapcar (lambda (o)
  5563. (when (eq (overlay-get o 'invisible) 'outline)
  5564. (setq beg (overlay-start o)
  5565. end (overlay-end o))
  5566. (and beg end (> end beg)
  5567. (if use-markers
  5568. (cons (move-marker (make-marker) beg)
  5569. (move-marker (make-marker) end))
  5570. (cons beg end)))))
  5571. (overlays-in (point-min) (point-max))))))))
  5572. (defun org-set-outline-overlay-data (data)
  5573. "Create visibility overlays for all positions in DATA.
  5574. DATA should have been made by `org-outline-overlay-data'."
  5575. (let (o)
  5576. (save-excursion
  5577. (save-restriction
  5578. (widen)
  5579. (show-all)
  5580. (mapc (lambda (c)
  5581. (setq o (make-overlay (car c) (cdr c)))
  5582. (overlay-put o 'invisible 'outline))
  5583. data)))))
  5584. (defmacro org-save-outline-visibility (use-markers &rest body)
  5585. "Save and restore outline visibility around BODY.
  5586. If USE-MARKERS is non-nil, use markers for the positions.
  5587. This means that the buffer may change while running BODY,
  5588. but it also means that the buffer should stay alive
  5589. during the operation, because otherwise all these markers will
  5590. point nowhere."
  5591. (declare (indent 1))
  5592. `(let ((data (org-outline-overlay-data ,use-markers)))
  5593. (unwind-protect
  5594. (progn
  5595. ,@body
  5596. (org-set-outline-overlay-data data))
  5597. (when ,use-markers
  5598. (mapc (lambda (c)
  5599. (and (markerp (car c)) (move-marker (car c) nil))
  5600. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5601. data)))))
  5602. ;;; Folding of blocks
  5603. (defconst org-block-regexp
  5604. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5605. "Regular expression for hiding blocks.")
  5606. (defvar org-hide-block-overlays nil
  5607. "Overlays hiding blocks.")
  5608. (make-variable-buffer-local 'org-hide-block-overlays)
  5609. (defun org-block-map (function &optional start end)
  5610. "Call FUNCTION at the head of all source blocks in the current buffer.
  5611. Optional arguments START and END can be used to limit the range."
  5612. (let ((start (or start (point-min)))
  5613. (end (or end (point-max))))
  5614. (save-excursion
  5615. (goto-char start)
  5616. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5617. (save-excursion
  5618. (save-match-data
  5619. (goto-char (match-beginning 0))
  5620. (funcall function)))))))
  5621. (defun org-hide-block-toggle-all ()
  5622. "Toggle the visibility of all blocks in the current buffer."
  5623. (org-block-map #'org-hide-block-toggle))
  5624. (defun org-hide-block-all ()
  5625. "Fold all blocks in the current buffer."
  5626. (interactive)
  5627. (org-show-block-all)
  5628. (org-block-map #'org-hide-block-toggle-maybe))
  5629. (defun org-show-block-all ()
  5630. "Unfold all blocks in the current buffer."
  5631. (interactive)
  5632. (mapc 'delete-overlay org-hide-block-overlays)
  5633. (setq org-hide-block-overlays nil))
  5634. (defun org-hide-block-toggle-maybe ()
  5635. "Toggle visibility of block at point."
  5636. (interactive)
  5637. (let ((case-fold-search t))
  5638. (if (save-excursion
  5639. (beginning-of-line 1)
  5640. (looking-at org-block-regexp))
  5641. (progn (org-hide-block-toggle)
  5642. t) ;; to signal that we took action
  5643. nil))) ;; to signal that we did not
  5644. (defun org-hide-block-toggle (&optional force)
  5645. "Toggle the visibility of the current block."
  5646. (interactive)
  5647. (save-excursion
  5648. (beginning-of-line)
  5649. (if (re-search-forward org-block-regexp nil t)
  5650. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5651. (end (match-end 0)) ;; end of entire body
  5652. ov)
  5653. (if (memq t (mapcar (lambda (overlay)
  5654. (eq (overlay-get overlay 'invisible)
  5655. 'org-hide-block))
  5656. (overlays-at start)))
  5657. (if (or (not force) (eq force 'off))
  5658. (mapc (lambda (ov)
  5659. (when (member ov org-hide-block-overlays)
  5660. (setq org-hide-block-overlays
  5661. (delq ov org-hide-block-overlays)))
  5662. (when (eq (overlay-get ov 'invisible)
  5663. 'org-hide-block)
  5664. (delete-overlay ov)))
  5665. (overlays-at start)))
  5666. (setq ov (make-overlay start end))
  5667. (overlay-put ov 'invisible 'org-hide-block)
  5668. ;; make the block accessible to isearch
  5669. (overlay-put
  5670. ov 'isearch-open-invisible
  5671. (lambda (ov)
  5672. (when (member ov org-hide-block-overlays)
  5673. (setq org-hide-block-overlays
  5674. (delq ov org-hide-block-overlays)))
  5675. (when (eq (overlay-get ov 'invisible)
  5676. 'org-hide-block)
  5677. (delete-overlay ov))))
  5678. (push ov org-hide-block-overlays)))
  5679. (error "Not looking at a source block"))))
  5680. ;; org-tab-after-check-for-cycling-hook
  5681. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5682. ;; Remove overlays when changing major mode
  5683. (add-hook 'org-mode-hook
  5684. (lambda () (org-add-hook 'change-major-mode-hook
  5685. 'org-show-block-all 'append 'local)))
  5686. ;;; Org-goto
  5687. (defvar org-goto-window-configuration nil)
  5688. (defvar org-goto-marker nil)
  5689. (defvar org-goto-map
  5690. (let ((map (make-sparse-keymap)))
  5691. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5692. (while (setq cmd (pop cmds))
  5693. (substitute-key-definition cmd cmd map global-map)))
  5694. (suppress-keymap map)
  5695. (org-defkey map "\C-m" 'org-goto-ret)
  5696. (org-defkey map [(return)] 'org-goto-ret)
  5697. (org-defkey map [(left)] 'org-goto-left)
  5698. (org-defkey map [(right)] 'org-goto-right)
  5699. (org-defkey map [(control ?g)] 'org-goto-quit)
  5700. (org-defkey map "\C-i" 'org-cycle)
  5701. (org-defkey map [(tab)] 'org-cycle)
  5702. (org-defkey map [(down)] 'outline-next-visible-heading)
  5703. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5704. (if org-goto-auto-isearch
  5705. (if (fboundp 'define-key-after)
  5706. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5707. nil)
  5708. (org-defkey map "q" 'org-goto-quit)
  5709. (org-defkey map "n" 'outline-next-visible-heading)
  5710. (org-defkey map "p" 'outline-previous-visible-heading)
  5711. (org-defkey map "f" 'outline-forward-same-level)
  5712. (org-defkey map "b" 'outline-backward-same-level)
  5713. (org-defkey map "u" 'outline-up-heading))
  5714. (org-defkey map "/" 'org-occur)
  5715. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5716. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5717. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5718. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5719. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5720. map))
  5721. (defconst org-goto-help
  5722. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5723. RET=jump to location [Q]uit and return to previous location
  5724. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5725. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5726. ;; FIXME: Docstring does not mention both interfaces
  5727. (defun org-goto (&optional alternative-interface)
  5728. "Look up a different location in the current file, keeping current visibility.
  5729. When you want look-up or go to a different location in a document, the
  5730. fastest way is often to fold the entire buffer and then dive into the tree.
  5731. This method has the disadvantage, that the previous location will be folded,
  5732. which may not be what you want.
  5733. This command works around this by showing a copy of the current buffer
  5734. in an indirect buffer, in overview mode. You can dive into the tree in
  5735. that copy, use org-occur and incremental search to find a location.
  5736. When pressing RET or `Q', the command returns to the original buffer in
  5737. which the visibility is still unchanged. After RET is will also jump to
  5738. the location selected in the indirect buffer and expose the
  5739. the headline hierarchy above."
  5740. (interactive "P")
  5741. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5742. (org-refile-use-outline-path t)
  5743. (org-refile-target-verify-function nil)
  5744. (interface
  5745. (if (not alternative-interface)
  5746. org-goto-interface
  5747. (if (eq org-goto-interface 'outline)
  5748. 'outline-path-completion
  5749. 'outline)))
  5750. (org-goto-start-pos (point))
  5751. (selected-point
  5752. (if (eq interface 'outline)
  5753. (car (org-get-location (current-buffer) org-goto-help))
  5754. (let ((pa (org-refile-get-location "Goto: ")))
  5755. (org-refile-check-position pa)
  5756. (nth 3 pa)))))
  5757. (if selected-point
  5758. (progn
  5759. (org-mark-ring-push org-goto-start-pos)
  5760. (goto-char selected-point)
  5761. (if (or (org-invisible-p) (org-invisible-p2))
  5762. (org-show-context 'org-goto)))
  5763. (message "Quit"))))
  5764. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5765. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5766. (defvar org-goto-local-auto-isearch-map) ; defined below
  5767. (defun org-get-location (buf help)
  5768. "Let the user select a location in the Org-mode buffer BUF.
  5769. This function uses a recursive edit. It returns the selected position
  5770. or nil."
  5771. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5772. (isearch-hide-immediately nil)
  5773. (isearch-search-fun-function
  5774. (lambda () 'org-goto-local-search-headings))
  5775. (org-goto-selected-point org-goto-exit-command)
  5776. (pop-up-frames nil)
  5777. (special-display-buffer-names nil)
  5778. (special-display-regexps nil)
  5779. (special-display-function nil))
  5780. (save-excursion
  5781. (save-window-excursion
  5782. (delete-other-windows)
  5783. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5784. (switch-to-buffer
  5785. (condition-case nil
  5786. (make-indirect-buffer (current-buffer) "*org-goto*")
  5787. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5788. (with-output-to-temp-buffer "*Help*"
  5789. (princ help))
  5790. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5791. (setq buffer-read-only nil)
  5792. (let ((org-startup-truncated t)
  5793. (org-startup-folded nil)
  5794. (org-startup-align-all-tables nil))
  5795. (org-mode)
  5796. (org-overview))
  5797. (setq buffer-read-only t)
  5798. (if (and (boundp 'org-goto-start-pos)
  5799. (integer-or-marker-p org-goto-start-pos))
  5800. (let ((org-show-hierarchy-above t)
  5801. (org-show-siblings t)
  5802. (org-show-following-heading t))
  5803. (goto-char org-goto-start-pos)
  5804. (and (org-invisible-p) (org-show-context)))
  5805. (goto-char (point-min)))
  5806. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5807. (message "Select location and press RET")
  5808. (use-local-map org-goto-map)
  5809. (recursive-edit)
  5810. ))
  5811. (kill-buffer "*org-goto*")
  5812. (cons org-goto-selected-point org-goto-exit-command)))
  5813. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5814. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5815. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5816. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5817. (defun org-goto-local-search-headings (string bound noerror)
  5818. "Search and make sure that any matches are in headlines."
  5819. (catch 'return
  5820. (while (if isearch-forward
  5821. (search-forward string bound noerror)
  5822. (search-backward string bound noerror))
  5823. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5824. (and (member :headline context)
  5825. (not (member :tags context))))
  5826. (throw 'return (point))))))
  5827. (defun org-goto-local-auto-isearch ()
  5828. "Start isearch."
  5829. (interactive)
  5830. (goto-char (point-min))
  5831. (let ((keys (this-command-keys)))
  5832. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5833. (isearch-mode t)
  5834. (isearch-process-search-char (string-to-char keys)))))
  5835. (defun org-goto-ret (&optional arg)
  5836. "Finish `org-goto' by going to the new location."
  5837. (interactive "P")
  5838. (setq org-goto-selected-point (point)
  5839. org-goto-exit-command 'return)
  5840. (throw 'exit nil))
  5841. (defun org-goto-left ()
  5842. "Finish `org-goto' by going to the new location."
  5843. (interactive)
  5844. (if (org-on-heading-p)
  5845. (progn
  5846. (beginning-of-line 1)
  5847. (setq org-goto-selected-point (point)
  5848. org-goto-exit-command 'left)
  5849. (throw 'exit nil))
  5850. (error "Not on a heading")))
  5851. (defun org-goto-right ()
  5852. "Finish `org-goto' by going to the new location."
  5853. (interactive)
  5854. (if (org-on-heading-p)
  5855. (progn
  5856. (setq org-goto-selected-point (point)
  5857. org-goto-exit-command 'right)
  5858. (throw 'exit nil))
  5859. (error "Not on a heading")))
  5860. (defun org-goto-quit ()
  5861. "Finish `org-goto' without cursor motion."
  5862. (interactive)
  5863. (setq org-goto-selected-point nil)
  5864. (setq org-goto-exit-command 'quit)
  5865. (throw 'exit nil))
  5866. ;;; Indirect buffer display of subtrees
  5867. (defvar org-indirect-dedicated-frame nil
  5868. "This is the frame being used for indirect tree display.")
  5869. (defvar org-last-indirect-buffer nil)
  5870. (defun org-tree-to-indirect-buffer (&optional arg)
  5871. "Create indirect buffer and narrow it to current subtree.
  5872. With numerical prefix ARG, go up to this level and then take that tree.
  5873. If ARG is negative, go up that many levels.
  5874. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5875. indirect buffer previously made with this command, to avoid proliferation of
  5876. indirect buffers. However, when you call the command with a \
  5877. \\[universal-argument] prefix, or
  5878. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5879. is kept so that you can work with several indirect buffers at the same time.
  5880. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5881. \\[universal-argument] prefix also
  5882. requests that a new frame be made for the new buffer, so that the dedicated
  5883. frame is not changed."
  5884. (interactive "P")
  5885. (let ((cbuf (current-buffer))
  5886. (cwin (selected-window))
  5887. (pos (point))
  5888. beg end level heading ibuf)
  5889. (save-excursion
  5890. (org-back-to-heading t)
  5891. (when (numberp arg)
  5892. (setq level (org-outline-level))
  5893. (if (< arg 0) (setq arg (+ level arg)))
  5894. (while (> (setq level (org-outline-level)) arg)
  5895. (outline-up-heading 1 t)))
  5896. (setq beg (point)
  5897. heading (org-get-heading))
  5898. (org-end-of-subtree t t)
  5899. (if (org-on-heading-p) (backward-char 1))
  5900. (setq end (point)))
  5901. (if (and (buffer-live-p org-last-indirect-buffer)
  5902. (not (eq org-indirect-buffer-display 'new-frame))
  5903. (not arg))
  5904. (kill-buffer org-last-indirect-buffer))
  5905. (setq ibuf (org-get-indirect-buffer cbuf)
  5906. org-last-indirect-buffer ibuf)
  5907. (cond
  5908. ((or (eq org-indirect-buffer-display 'new-frame)
  5909. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5910. (select-frame (make-frame))
  5911. (delete-other-windows)
  5912. (switch-to-buffer ibuf)
  5913. (org-set-frame-title heading))
  5914. ((eq org-indirect-buffer-display 'dedicated-frame)
  5915. (raise-frame
  5916. (select-frame (or (and org-indirect-dedicated-frame
  5917. (frame-live-p org-indirect-dedicated-frame)
  5918. org-indirect-dedicated-frame)
  5919. (setq org-indirect-dedicated-frame (make-frame)))))
  5920. (delete-other-windows)
  5921. (switch-to-buffer ibuf)
  5922. (org-set-frame-title (concat "Indirect: " heading)))
  5923. ((eq org-indirect-buffer-display 'current-window)
  5924. (switch-to-buffer ibuf))
  5925. ((eq org-indirect-buffer-display 'other-window)
  5926. (pop-to-buffer ibuf))
  5927. (t (error "Invalid value")))
  5928. (if (featurep 'xemacs)
  5929. (save-excursion (org-mode) (turn-on-font-lock)))
  5930. (narrow-to-region beg end)
  5931. (show-all)
  5932. (goto-char pos)
  5933. (and (window-live-p cwin) (select-window cwin))))
  5934. (defun org-get-indirect-buffer (&optional buffer)
  5935. (setq buffer (or buffer (current-buffer)))
  5936. (let ((n 1) (base (buffer-name buffer)) bname)
  5937. (while (buffer-live-p
  5938. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5939. (setq n (1+ n)))
  5940. (condition-case nil
  5941. (make-indirect-buffer buffer bname 'clone)
  5942. (error (make-indirect-buffer buffer bname)))))
  5943. (defun org-set-frame-title (title)
  5944. "Set the title of the current frame to the string TITLE."
  5945. ;; FIXME: how to name a single frame in XEmacs???
  5946. (unless (featurep 'xemacs)
  5947. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5948. ;;;; Structure editing
  5949. ;;; Inserting headlines
  5950. (defun org-previous-line-empty-p ()
  5951. (save-excursion
  5952. (and (not (bobp))
  5953. (or (beginning-of-line 0) t)
  5954. (save-match-data
  5955. (looking-at "[ \t]*$")))))
  5956. (defun org-insert-heading (&optional force-heading invisible-ok)
  5957. "Insert a new heading or item with same depth at point.
  5958. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5959. If point is at the beginning of a headline, insert a sibling before the
  5960. current headline. If point is not at the beginning, do not split the line,
  5961. but create the new headline after the current line.
  5962. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5963. This is important for non-interactive uses of the command."
  5964. (interactive "P")
  5965. (if (or (= (buffer-size) 0)
  5966. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5967. (org-on-heading-p))))
  5968. (not (org-in-item-p))))
  5969. (insert "\n* ")
  5970. (when (or force-heading (not (org-insert-item)))
  5971. (let* ((empty-line-p nil)
  5972. (head (save-excursion
  5973. (condition-case nil
  5974. (progn
  5975. (org-back-to-heading invisible-ok)
  5976. (setq empty-line-p (org-previous-line-empty-p))
  5977. (match-string 0))
  5978. (error "*"))))
  5979. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5980. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5981. pos hide-previous previous-pos)
  5982. (cond
  5983. ((and (org-on-heading-p) (bolp)
  5984. (or (bobp)
  5985. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5986. ;; insert before the current line
  5987. (open-line (if blank 2 1)))
  5988. ((and (bolp)
  5989. (not org-insert-heading-respect-content)
  5990. (or (bobp)
  5991. (save-excursion
  5992. (backward-char 1) (not (org-invisible-p)))))
  5993. ;; insert right here
  5994. nil)
  5995. (t
  5996. ;; somewhere in the line
  5997. (save-excursion
  5998. (setq previous-pos (point-at-bol))
  5999. (end-of-line)
  6000. (setq hide-previous (org-invisible-p)))
  6001. (and org-insert-heading-respect-content (org-show-subtree))
  6002. (let ((split
  6003. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6004. (save-excursion
  6005. (let ((p (point)))
  6006. (goto-char (point-at-bol))
  6007. (and (looking-at org-complex-heading-regexp)
  6008. (> p (match-beginning 4)))))))
  6009. tags pos)
  6010. (cond
  6011. (org-insert-heading-respect-content
  6012. (org-end-of-subtree nil t)
  6013. (or (bolp) (newline))
  6014. (or (org-previous-line-empty-p)
  6015. (and blank (newline)))
  6016. (open-line 1))
  6017. ((org-on-heading-p)
  6018. (when hide-previous
  6019. (show-children)
  6020. (org-show-entry))
  6021. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  6022. (setq tags (and (match-end 2) (match-string 2)))
  6023. (and (match-end 1)
  6024. (delete-region (match-beginning 1) (match-end 1)))
  6025. (setq pos (point-at-bol))
  6026. (or split (end-of-line 1))
  6027. (delete-horizontal-space)
  6028. (if (string-match "\\`\\*+\\'"
  6029. (buffer-substring (point-at-bol) (point)))
  6030. (insert " "))
  6031. (newline (if blank 2 1))
  6032. (when tags
  6033. (save-excursion
  6034. (goto-char pos)
  6035. (end-of-line 1)
  6036. (insert " " tags)
  6037. (org-set-tags nil 'align))))
  6038. (t
  6039. (or split (end-of-line 1))
  6040. (newline (if blank 2 1)))))))
  6041. (insert head) (just-one-space)
  6042. (setq pos (point))
  6043. (end-of-line 1)
  6044. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6045. (when (and org-insert-heading-respect-content hide-previous)
  6046. (save-excursion
  6047. (goto-char previous-pos)
  6048. (hide-subtree)))
  6049. (run-hooks 'org-insert-heading-hook)))))
  6050. (defun org-get-heading (&optional no-tags)
  6051. "Return the heading of the current entry, without the stars."
  6052. (save-excursion
  6053. (org-back-to-heading t)
  6054. (if (looking-at
  6055. (if no-tags
  6056. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  6057. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6058. (match-string 1) "")))
  6059. (defun org-heading-components ()
  6060. "Return the components of the current heading.
  6061. This is a list with the following elements:
  6062. - the level as an integer
  6063. - the reduced level, different if `org-odd-levels-only' is set.
  6064. - the TODO keyword, or nil
  6065. - the priority character, like ?A, or nil if no priority is given
  6066. - the headline text itself, or the tags string if no headline text
  6067. - the tags string, or nil."
  6068. (save-excursion
  6069. (org-back-to-heading t)
  6070. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6071. (list (length (match-string 1))
  6072. (org-reduced-level (length (match-string 1)))
  6073. (org-match-string-no-properties 2)
  6074. (and (match-end 3) (aref (match-string 3) 2))
  6075. (org-match-string-no-properties 4)
  6076. (org-match-string-no-properties 5)))))
  6077. (defun org-get-entry ()
  6078. "Get the entry text, after heading, entire subtree."
  6079. (save-excursion
  6080. (org-back-to-heading t)
  6081. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6082. (defun org-insert-heading-after-current ()
  6083. "Insert a new heading with same level as current, after current subtree."
  6084. (interactive)
  6085. (org-back-to-heading)
  6086. (org-insert-heading)
  6087. (org-move-subtree-down)
  6088. (end-of-line 1))
  6089. (defun org-insert-heading-respect-content ()
  6090. (interactive)
  6091. (let ((org-insert-heading-respect-content t))
  6092. (org-insert-heading t)))
  6093. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6094. (interactive "P")
  6095. (let ((org-insert-heading-respect-content t))
  6096. (org-insert-todo-heading force-state t)))
  6097. (defun org-insert-todo-heading (arg &optional force-heading)
  6098. "Insert a new heading with the same level and TODO state as current heading.
  6099. If the heading has no TODO state, or if the state is DONE, use the first
  6100. state (TODO by default). Also with prefix arg, force first state."
  6101. (interactive "P")
  6102. (when (or force-heading (not (org-insert-item 'checkbox)))
  6103. (org-insert-heading force-heading)
  6104. (save-excursion
  6105. (org-back-to-heading)
  6106. (outline-previous-heading)
  6107. (looking-at org-todo-line-regexp))
  6108. (let*
  6109. ((new-mark-x
  6110. (if (or arg
  6111. (not (match-beginning 2))
  6112. (member (match-string 2) org-done-keywords))
  6113. (car org-todo-keywords-1)
  6114. (match-string 2)))
  6115. (new-mark
  6116. (or
  6117. (run-hook-with-args-until-success
  6118. 'org-todo-get-default-hook new-mark-x nil)
  6119. new-mark-x)))
  6120. (beginning-of-line 1)
  6121. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6122. (if org-treat-insert-todo-heading-as-state-change
  6123. (org-todo new-mark)
  6124. (insert new-mark " "))))
  6125. (when org-provide-todo-statistics
  6126. (org-update-parent-todo-statistics))))
  6127. (defun org-insert-subheading (arg)
  6128. "Insert a new subheading and demote it.
  6129. Works for outline headings and for plain lists alike."
  6130. (interactive "P")
  6131. (org-insert-heading arg)
  6132. (cond
  6133. ((org-on-heading-p) (org-do-demote))
  6134. ((org-at-item-p) (org-indent-item 1))))
  6135. (defun org-insert-todo-subheading (arg)
  6136. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6137. Works for outline headings and for plain lists alike."
  6138. (interactive "P")
  6139. (org-insert-todo-heading arg)
  6140. (cond
  6141. ((org-on-heading-p) (org-do-demote))
  6142. ((org-at-item-p) (org-indent-item 1))))
  6143. ;;; Promotion and Demotion
  6144. (defvar org-after-demote-entry-hook nil
  6145. "Hook run after an entry has been demoted.
  6146. The cursor will be at the beginning of the entry.
  6147. When a subtree is being demoted, the hook will be called for each node.")
  6148. (defvar org-after-promote-entry-hook nil
  6149. "Hook run after an entry has been promoted.
  6150. The cursor will be at the beginning of the entry.
  6151. When a subtree is being promoted, the hook will be called for each node.")
  6152. (defun org-promote-subtree ()
  6153. "Promote the entire subtree.
  6154. See also `org-promote'."
  6155. (interactive)
  6156. (save-excursion
  6157. (org-map-tree 'org-promote))
  6158. (org-fix-position-after-promote))
  6159. (defun org-demote-subtree ()
  6160. "Demote the entire subtree. See `org-demote'.
  6161. See also `org-promote'."
  6162. (interactive)
  6163. (save-excursion
  6164. (org-map-tree 'org-demote))
  6165. (org-fix-position-after-promote))
  6166. (defun org-do-promote ()
  6167. "Promote the current heading higher up the tree.
  6168. If the region is active in `transient-mark-mode', promote all headings
  6169. in the region."
  6170. (interactive)
  6171. (save-excursion
  6172. (if (org-region-active-p)
  6173. (org-map-region 'org-promote (region-beginning) (region-end))
  6174. (org-promote)))
  6175. (org-fix-position-after-promote))
  6176. (defun org-do-demote ()
  6177. "Demote the current heading lower down the tree.
  6178. If the region is active in `transient-mark-mode', demote all headings
  6179. in the region."
  6180. (interactive)
  6181. (save-excursion
  6182. (if (org-region-active-p)
  6183. (org-map-region 'org-demote (region-beginning) (region-end))
  6184. (org-demote)))
  6185. (org-fix-position-after-promote))
  6186. (defun org-fix-position-after-promote ()
  6187. "Make sure that after pro/demotion cursor position is right."
  6188. (let ((pos (point)))
  6189. (when (save-excursion
  6190. (beginning-of-line 1)
  6191. (looking-at org-todo-line-regexp)
  6192. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6193. (cond ((eobp) (insert " "))
  6194. ((eolp) (insert " "))
  6195. ((equal (char-after) ?\ ) (forward-char 1))))))
  6196. (defun org-current-level ()
  6197. "Return the level of the current entry, or nil if before the first headline.
  6198. The level is the number of stars at the beginning of the headline."
  6199. (save-excursion
  6200. (condition-case nil
  6201. (progn
  6202. (org-back-to-heading t)
  6203. (funcall outline-level))
  6204. (error nil))))
  6205. (defun org-get-previous-line-level ()
  6206. "Return the outline depth of the last headline before the current line.
  6207. Returns 0 for the first headline in the buffer, and nil if before the
  6208. first headline."
  6209. (let ((current-level (org-current-level))
  6210. (prev-level (when (> (line-number-at-pos) 1)
  6211. (save-excursion
  6212. (beginning-of-line 0)
  6213. (org-current-level)))))
  6214. (cond ((null current-level) nil) ; Before first headline
  6215. ((null prev-level) 0) ; At first headline
  6216. (prev-level))))
  6217. (defun org-reduced-level (l)
  6218. "Compute the effective level of a heading.
  6219. This takes into account the setting of `org-odd-levels-only'."
  6220. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6221. (defun org-level-increment ()
  6222. "Return the number of stars that will be added or removed at a
  6223. time to headlines when structure editing, based on the value of
  6224. `org-odd-levels-only'."
  6225. (if org-odd-levels-only 2 1))
  6226. (defun org-get-valid-level (level &optional change)
  6227. "Rectify a level change under the influence of `org-odd-levels-only'
  6228. LEVEL is a current level, CHANGE is by how much the level should be
  6229. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6230. even level numbers will become the next higher odd number."
  6231. (if org-odd-levels-only
  6232. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6233. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6234. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6235. (max 1 (+ level (or change 0)))))
  6236. (if (boundp 'define-obsolete-function-alias)
  6237. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6238. (define-obsolete-function-alias 'org-get-legal-level
  6239. 'org-get-valid-level)
  6240. (define-obsolete-function-alias 'org-get-legal-level
  6241. 'org-get-valid-level "23.1")))
  6242. (defun org-promote ()
  6243. "Promote the current heading higher up the tree.
  6244. If the region is active in `transient-mark-mode', promote all headings
  6245. in the region."
  6246. (org-back-to-heading t)
  6247. (let* ((level (save-match-data (funcall outline-level)))
  6248. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6249. (diff (abs (- level (length up-head) -1))))
  6250. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6251. (replace-match up-head nil t)
  6252. ;; Fixup tag positioning
  6253. (and org-auto-align-tags (org-set-tags nil t))
  6254. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6255. (run-hooks 'org-after-promote-entry-hook)))
  6256. (defun org-demote ()
  6257. "Demote the current heading lower down the tree.
  6258. If the region is active in `transient-mark-mode', demote all headings
  6259. in the region."
  6260. (org-back-to-heading t)
  6261. (let* ((level (save-match-data (funcall outline-level)))
  6262. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6263. (diff (abs (- level (length down-head) -1))))
  6264. (replace-match down-head nil t)
  6265. ;; Fixup tag positioning
  6266. (and org-auto-align-tags (org-set-tags nil t))
  6267. (if org-adapt-indentation (org-fixup-indentation diff))
  6268. (run-hooks 'org-after-demote-entry-hook)))
  6269. (defun org-cycle-level ()
  6270. "Cycle the level of an empty headline through possible states.
  6271. This goes first to child, then to parent, level, then up the hierarchy.
  6272. After top level, it switches back to sibling level."
  6273. (interactive)
  6274. (let ((org-adapt-indentation nil))
  6275. (when (org-point-at-end-of-empty-headline)
  6276. (setq this-command 'org-cycle-level) ; Only needed for caching
  6277. (let ((cur-level (org-current-level))
  6278. (prev-level (org-get-previous-line-level)))
  6279. (cond
  6280. ;; If first headline in file, promote to top-level.
  6281. ((= prev-level 0)
  6282. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6283. do (org-do-promote)))
  6284. ;; If same level as prev, demote one.
  6285. ((= prev-level cur-level)
  6286. (org-do-demote))
  6287. ;; If parent is top-level, promote to top level if not already.
  6288. ((= prev-level 1)
  6289. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6290. do (org-do-promote)))
  6291. ;; If top-level, return to prev-level.
  6292. ((= cur-level 1)
  6293. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6294. do (org-do-demote)))
  6295. ;; If less than prev-level, promote one.
  6296. ((< cur-level prev-level)
  6297. (org-do-promote))
  6298. ;; If deeper than prev-level, promote until higher than
  6299. ;; prev-level.
  6300. ((> cur-level prev-level)
  6301. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6302. do (org-do-promote))))
  6303. t))))
  6304. (defun org-map-tree (fun)
  6305. "Call FUN for every heading underneath the current one."
  6306. (org-back-to-heading)
  6307. (let ((level (funcall outline-level)))
  6308. (save-excursion
  6309. (funcall fun)
  6310. (while (and (progn
  6311. (outline-next-heading)
  6312. (> (funcall outline-level) level))
  6313. (not (eobp)))
  6314. (funcall fun)))))
  6315. (defun org-map-region (fun beg end)
  6316. "Call FUN for every heading between BEG and END."
  6317. (let ((org-ignore-region t))
  6318. (save-excursion
  6319. (setq end (copy-marker end))
  6320. (goto-char beg)
  6321. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6322. (< (point) end))
  6323. (funcall fun))
  6324. (while (and (progn
  6325. (outline-next-heading)
  6326. (< (point) end))
  6327. (not (eobp)))
  6328. (funcall fun)))))
  6329. (defun org-fixup-indentation (diff)
  6330. "Change the indentation in the current entry by DIFF.
  6331. However, if any line in the current entry has no indentation, or if it
  6332. would end up with no indentation after the change, nothing at all is done."
  6333. (save-excursion
  6334. (let ((end (save-excursion (outline-next-heading)
  6335. (point-marker)))
  6336. (prohibit (if (> diff 0)
  6337. "^\\S-"
  6338. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6339. col)
  6340. (unless (save-excursion (end-of-line 1)
  6341. (re-search-forward prohibit end t))
  6342. (while (and (< (point) end)
  6343. (re-search-forward "^[ \t]+" end t))
  6344. (goto-char (match-end 0))
  6345. (setq col (current-column))
  6346. (if (< diff 0) (replace-match ""))
  6347. (org-indent-to-column (+ diff col))))
  6348. (move-marker end nil))))
  6349. (defun org-convert-to-odd-levels ()
  6350. "Convert an org-mode file with all levels allowed to one with odd levels.
  6351. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6352. level 5 etc."
  6353. (interactive)
  6354. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6355. (let ((outline-regexp org-outline-regexp)
  6356. (outline-level 'org-outline-level)
  6357. (org-odd-levels-only nil) n)
  6358. (save-excursion
  6359. (goto-char (point-min))
  6360. (while (re-search-forward "^\\*\\*+ " nil t)
  6361. (setq n (- (length (match-string 0)) 2))
  6362. (while (>= (setq n (1- n)) 0)
  6363. (org-demote))
  6364. (end-of-line 1))))))
  6365. (defun org-convert-to-oddeven-levels ()
  6366. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6367. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6368. file contains a section with an even level, conversion would
  6369. destroy the structure of the file. An error is signaled in this
  6370. case."
  6371. (interactive)
  6372. (goto-char (point-min))
  6373. ;; First check if there are no even levels
  6374. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6375. (org-show-context t)
  6376. (error "Not all levels are odd in this file. Conversion not possible"))
  6377. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6378. (let ((outline-regexp org-outline-regexp)
  6379. (outline-level 'org-outline-level)
  6380. (org-odd-levels-only nil) n)
  6381. (save-excursion
  6382. (goto-char (point-min))
  6383. (while (re-search-forward "^\\*\\*+ " nil t)
  6384. (setq n (/ (1- (length (match-string 0))) 2))
  6385. (while (>= (setq n (1- n)) 0)
  6386. (org-promote))
  6387. (end-of-line 1))))))
  6388. (defun org-tr-level (n)
  6389. "Make N odd if required."
  6390. (if org-odd-levels-only (1+ (/ n 2)) n))
  6391. ;;; Vertical tree motion, cutting and pasting of subtrees
  6392. (defun org-move-subtree-up (&optional arg)
  6393. "Move the current subtree up past ARG headlines of the same level."
  6394. (interactive "p")
  6395. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6396. (defun org-move-subtree-down (&optional arg)
  6397. "Move the current subtree down past ARG headlines of the same level."
  6398. (interactive "p")
  6399. (setq arg (prefix-numeric-value arg))
  6400. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6401. 'org-get-last-sibling))
  6402. (ins-point (make-marker))
  6403. (cnt (abs arg))
  6404. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6405. ;; Select the tree
  6406. (org-back-to-heading)
  6407. (setq beg0 (point))
  6408. (save-excursion
  6409. (setq ne-beg (org-back-over-empty-lines))
  6410. (setq beg (point)))
  6411. (save-match-data
  6412. (save-excursion (outline-end-of-heading)
  6413. (setq folded (org-invisible-p)))
  6414. (outline-end-of-subtree))
  6415. (outline-next-heading)
  6416. (setq ne-end (org-back-over-empty-lines))
  6417. (setq end (point))
  6418. (goto-char beg0)
  6419. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6420. ;; include less whitespace
  6421. (save-excursion
  6422. (goto-char beg)
  6423. (forward-line (- ne-beg ne-end))
  6424. (setq beg (point))))
  6425. ;; Find insertion point, with error handling
  6426. (while (> cnt 0)
  6427. (or (and (funcall movfunc) (looking-at outline-regexp))
  6428. (progn (goto-char beg0)
  6429. (error "Cannot move past superior level or buffer limit")))
  6430. (setq cnt (1- cnt)))
  6431. (if (> arg 0)
  6432. ;; Moving forward - still need to move over subtree
  6433. (progn (org-end-of-subtree t t)
  6434. (save-excursion
  6435. (org-back-over-empty-lines)
  6436. (or (bolp) (newline)))))
  6437. (setq ne-ins (org-back-over-empty-lines))
  6438. (move-marker ins-point (point))
  6439. (setq txt (buffer-substring beg end))
  6440. (org-save-markers-in-region beg end)
  6441. (delete-region beg end)
  6442. (org-remove-empty-overlays-at beg)
  6443. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6444. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6445. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6446. (let ((bbb (point)))
  6447. (insert-before-markers txt)
  6448. (org-reinstall-markers-in-region bbb)
  6449. (move-marker ins-point bbb))
  6450. (or (bolp) (insert "\n"))
  6451. (setq ins-end (point))
  6452. (goto-char ins-point)
  6453. (org-skip-whitespace)
  6454. (when (and (< arg 0)
  6455. (org-first-sibling-p)
  6456. (> ne-ins ne-beg))
  6457. ;; Move whitespace back to beginning
  6458. (save-excursion
  6459. (goto-char ins-end)
  6460. (let ((kill-whole-line t))
  6461. (kill-line (- ne-ins ne-beg)) (point)))
  6462. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6463. (move-marker ins-point nil)
  6464. (if folded
  6465. (hide-subtree)
  6466. (org-show-entry)
  6467. (show-children)
  6468. (org-cycle-hide-drawers 'children))
  6469. (org-clean-visibility-after-subtree-move)))
  6470. (defvar org-subtree-clip ""
  6471. "Clipboard for cut and paste of subtrees.
  6472. This is actually only a copy of the kill, because we use the normal kill
  6473. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6474. (defvar org-subtree-clip-folded nil
  6475. "Was the last copied subtree folded?
  6476. This is used to fold the tree back after pasting.")
  6477. (defun org-cut-subtree (&optional n)
  6478. "Cut the current subtree into the clipboard.
  6479. With prefix arg N, cut this many sequential subtrees.
  6480. This is a short-hand for marking the subtree and then cutting it."
  6481. (interactive "p")
  6482. (org-copy-subtree n 'cut))
  6483. (defun org-copy-subtree (&optional n cut force-store-markers)
  6484. "Cut the current subtree into the clipboard.
  6485. With prefix arg N, cut this many sequential subtrees.
  6486. This is a short-hand for marking the subtree and then copying it.
  6487. If CUT is non-nil, actually cut the subtree.
  6488. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6489. of some markers in the region, even if CUT is non-nil. This is
  6490. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6491. (interactive "p")
  6492. (let (beg end folded (beg0 (point)))
  6493. (if (interactive-p)
  6494. (org-back-to-heading nil) ; take what looks like a subtree
  6495. (org-back-to-heading t)) ; take what is really there
  6496. (org-back-over-empty-lines)
  6497. (setq beg (point))
  6498. (skip-chars-forward " \t\r\n")
  6499. (save-match-data
  6500. (save-excursion (outline-end-of-heading)
  6501. (setq folded (org-invisible-p)))
  6502. (condition-case nil
  6503. (org-forward-same-level (1- n) t)
  6504. (error nil))
  6505. (org-end-of-subtree t t))
  6506. (org-back-over-empty-lines)
  6507. (setq end (point))
  6508. (goto-char beg0)
  6509. (when (> end beg)
  6510. (setq org-subtree-clip-folded folded)
  6511. (when (or cut force-store-markers)
  6512. (org-save-markers-in-region beg end))
  6513. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6514. (setq org-subtree-clip (current-kill 0))
  6515. (message "%s: Subtree(s) with %d characters"
  6516. (if cut "Cut" "Copied")
  6517. (length org-subtree-clip)))))
  6518. (defun org-paste-subtree (&optional level tree for-yank)
  6519. "Paste the clipboard as a subtree, with modification of headline level.
  6520. The entire subtree is promoted or demoted in order to match a new headline
  6521. level.
  6522. If the cursor is at the beginning of a headline, the same level as
  6523. that headline is used to paste the tree
  6524. If not, the new level is derived from the *visible* headings
  6525. before and after the insertion point, and taken to be the inferior headline
  6526. level of the two. So if the previous visible heading is level 3 and the
  6527. next is level 4 (or vice versa), level 4 will be used for insertion.
  6528. This makes sure that the subtree remains an independent subtree and does
  6529. not swallow low level entries.
  6530. You can also force a different level, either by using a numeric prefix
  6531. argument, or by inserting the heading marker by hand. For example, if the
  6532. cursor is after \"*****\", then the tree will be shifted to level 5.
  6533. If optional TREE is given, use this text instead of the kill ring.
  6534. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6535. move back over whitespace before inserting, and move point to the end of
  6536. the inserted text when done."
  6537. (interactive "P")
  6538. (setq tree (or tree (and kill-ring (current-kill 0))))
  6539. (unless (org-kill-is-subtree-p tree)
  6540. (error "%s"
  6541. (substitute-command-keys
  6542. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6543. (let* ((visp (not (org-invisible-p)))
  6544. (txt tree)
  6545. (^re (concat "^\\(" outline-regexp "\\)"))
  6546. (re (concat "\\(" outline-regexp "\\)"))
  6547. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6548. (old-level (if (string-match ^re txt)
  6549. (- (match-end 0) (match-beginning 0) 1)
  6550. -1))
  6551. (force-level (cond (level (prefix-numeric-value level))
  6552. ((and (looking-at "[ \t]*$")
  6553. (string-match
  6554. ^re_ (buffer-substring
  6555. (point-at-bol) (point))))
  6556. (- (match-end 1) (match-beginning 1)))
  6557. ((and (bolp)
  6558. (looking-at org-outline-regexp))
  6559. (- (match-end 0) (point) 1))
  6560. (t nil)))
  6561. (previous-level (save-excursion
  6562. (condition-case nil
  6563. (progn
  6564. (outline-previous-visible-heading 1)
  6565. (if (looking-at re)
  6566. (- (match-end 0) (match-beginning 0) 1)
  6567. 1))
  6568. (error 1))))
  6569. (next-level (save-excursion
  6570. (condition-case nil
  6571. (progn
  6572. (or (looking-at outline-regexp)
  6573. (outline-next-visible-heading 1))
  6574. (if (looking-at re)
  6575. (- (match-end 0) (match-beginning 0) 1)
  6576. 1))
  6577. (error 1))))
  6578. (new-level (or force-level (max previous-level next-level)))
  6579. (shift (if (or (= old-level -1)
  6580. (= new-level -1)
  6581. (= old-level new-level))
  6582. 0
  6583. (- new-level old-level)))
  6584. (delta (if (> shift 0) -1 1))
  6585. (func (if (> shift 0) 'org-demote 'org-promote))
  6586. (org-odd-levels-only nil)
  6587. beg end newend)
  6588. ;; Remove the forced level indicator
  6589. (if force-level
  6590. (delete-region (point-at-bol) (point)))
  6591. ;; Paste
  6592. (beginning-of-line 1)
  6593. (unless for-yank (org-back-over-empty-lines))
  6594. (setq beg (point))
  6595. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6596. (insert-before-markers txt)
  6597. (unless (string-match "\n\\'" txt) (insert "\n"))
  6598. (setq newend (point))
  6599. (org-reinstall-markers-in-region beg)
  6600. (setq end (point))
  6601. (goto-char beg)
  6602. (skip-chars-forward " \t\n\r")
  6603. (setq beg (point))
  6604. (if (and (org-invisible-p) visp)
  6605. (save-excursion (outline-show-heading)))
  6606. ;; Shift if necessary
  6607. (unless (= shift 0)
  6608. (save-restriction
  6609. (narrow-to-region beg end)
  6610. (while (not (= shift 0))
  6611. (org-map-region func (point-min) (point-max))
  6612. (setq shift (+ delta shift)))
  6613. (goto-char (point-min))
  6614. (setq newend (point-max))))
  6615. (when (or (interactive-p) for-yank)
  6616. (message "Clipboard pasted as level %d subtree" new-level))
  6617. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6618. kill-ring
  6619. (eq org-subtree-clip (current-kill 0))
  6620. org-subtree-clip-folded)
  6621. ;; The tree was folded before it was killed/copied
  6622. (hide-subtree))
  6623. (and for-yank (goto-char newend))))
  6624. (defun org-kill-is-subtree-p (&optional txt)
  6625. "Check if the current kill is an outline subtree, or a set of trees.
  6626. Returns nil if kill does not start with a headline, or if the first
  6627. headline level is not the largest headline level in the tree.
  6628. So this will actually accept several entries of equal levels as well,
  6629. which is OK for `org-paste-subtree'.
  6630. If optional TXT is given, check this string instead of the current kill."
  6631. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6632. (start-level (and kill
  6633. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6634. org-outline-regexp "\\)")
  6635. kill)
  6636. (- (match-end 2) (match-beginning 2) 1)))
  6637. (re (concat "^" org-outline-regexp))
  6638. (start (1+ (or (match-beginning 2) -1))))
  6639. (if (not start-level)
  6640. (progn
  6641. nil) ;; does not even start with a heading
  6642. (catch 'exit
  6643. (while (setq start (string-match re kill (1+ start)))
  6644. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6645. (throw 'exit nil)))
  6646. t))))
  6647. (defvar org-markers-to-move nil
  6648. "Markers that should be moved with a cut-and-paste operation.
  6649. Those markers are stored together with their positions relative to
  6650. the start of the region.")
  6651. (defun org-save-markers-in-region (beg end)
  6652. "Check markers in region.
  6653. If these markers are between BEG and END, record their position relative
  6654. to BEG, so that after moving the block of text, we can put the markers back
  6655. into place.
  6656. This function gets called just before an entry or tree gets cut from the
  6657. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6658. called immediately, to move the markers with the entries."
  6659. (setq org-markers-to-move nil)
  6660. (when (featurep 'org-clock)
  6661. (org-clock-save-markers-for-cut-and-paste beg end))
  6662. (when (featurep 'org-agenda)
  6663. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6664. (defun org-check-and-save-marker (marker beg end)
  6665. "Check if MARKER is between BEG and END.
  6666. If yes, remember the marker and the distance to BEG."
  6667. (when (and (marker-buffer marker)
  6668. (equal (marker-buffer marker) (current-buffer)))
  6669. (if (and (>= marker beg) (< marker end))
  6670. (push (cons marker (- marker beg)) org-markers-to-move))))
  6671. (defun org-reinstall-markers-in-region (beg)
  6672. "Move all remembered markers to their position relative to BEG."
  6673. (mapc (lambda (x)
  6674. (move-marker (car x) (+ beg (cdr x))))
  6675. org-markers-to-move)
  6676. (setq org-markers-to-move nil))
  6677. (defun org-narrow-to-subtree ()
  6678. "Narrow buffer to the current subtree."
  6679. (interactive)
  6680. (save-excursion
  6681. (save-match-data
  6682. (narrow-to-region
  6683. (progn (org-back-to-heading t) (point))
  6684. (progn (org-end-of-subtree t t)
  6685. (if (org-on-heading-p) (backward-char 1))
  6686. (point))))))
  6687. (eval-when-compile
  6688. (defvar org-property-drawer-re))
  6689. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6690. "Clone the task (subtree) at point N times.
  6691. The clones will be inserted as siblings.
  6692. In interactive use, the user will be prompted for the number of
  6693. clones to be produced, and for a time SHIFT, which may be a
  6694. repeater as used in time stamps, for example `+3d'.
  6695. When a valid repeater is given and the entry contains any time
  6696. stamps, the clones will become a sequence in time, with time
  6697. stamps in the subtree shifted for each clone produced. If SHIFT
  6698. is nil or the empty string, time stamps will be left alone. The
  6699. ID property of the original subtree is removed.
  6700. If the original subtree did contain time stamps with a repeater,
  6701. the following will happen:
  6702. - the repeater will be removed in each clone
  6703. - an additional clone will be produced, with the current, unshifted
  6704. date(s) in the entry.
  6705. - the original entry will be placed *after* all the clones, with
  6706. repeater intact.
  6707. - the start days in the repeater in the original entry will be shifted
  6708. to past the last clone.
  6709. I this way you can spell out a number of instances of a repeating task,
  6710. and still retain the repeater to cover future instances of the task."
  6711. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6712. (let (beg end template task idprop
  6713. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6714. (if (not (and (integerp n) (> n 0)))
  6715. (error "Invalid number of replications %s" n))
  6716. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6717. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6718. shift)))
  6719. (error "Invalid shift specification %s" shift))
  6720. (when doshift
  6721. (setq shift-n (string-to-number (match-string 1 shift))
  6722. shift-what (cdr (assoc (match-string 2 shift)
  6723. '(("d" . day) ("w" . week)
  6724. ("m" . month) ("y" . year))))))
  6725. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6726. (setq nmin 1 nmax n)
  6727. (org-back-to-heading t)
  6728. (setq beg (point))
  6729. (setq idprop (org-entry-get nil "ID"))
  6730. (org-end-of-subtree t t)
  6731. (or (bolp) (insert "\n"))
  6732. (setq end (point))
  6733. (setq template (buffer-substring beg end))
  6734. (when (and doshift
  6735. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6736. (delete-region beg end)
  6737. (setq end beg)
  6738. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6739. (goto-char end)
  6740. (loop for n from nmin to nmax do
  6741. ;; prepare clone
  6742. (with-temp-buffer
  6743. (insert template)
  6744. (org-mode)
  6745. (goto-char (point-min))
  6746. (and idprop (if org-clone-delete-id
  6747. (org-entry-delete nil "ID")
  6748. (org-id-get-create t)))
  6749. (while (re-search-forward org-property-drawer-re nil t)
  6750. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6751. (goto-char (point-min))
  6752. (when doshift
  6753. (while (re-search-forward org-ts-regexp-both nil t)
  6754. (org-timestamp-change (* n shift-n) shift-what))
  6755. (unless (= n n-no-remove)
  6756. (goto-char (point-min))
  6757. (while (re-search-forward org-ts-regexp nil t)
  6758. (save-excursion
  6759. (goto-char (match-beginning 0))
  6760. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6761. (delete-region (match-beginning 1) (match-end 1)))))))
  6762. (setq task (buffer-string)))
  6763. (insert task))
  6764. (goto-char beg)))
  6765. ;;; Outline Sorting
  6766. (defun org-sort (with-case)
  6767. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6768. Optional argument WITH-CASE means sort case-sensitively.
  6769. With a double prefix argument, also remove duplicate entries."
  6770. (interactive "P")
  6771. (if (org-at-table-p)
  6772. (org-call-with-arg 'org-table-sort-lines with-case)
  6773. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6774. (defun org-sort-remove-invisible (s)
  6775. (remove-text-properties 0 (length s) org-rm-props s)
  6776. (while (string-match org-bracket-link-regexp s)
  6777. (setq s (replace-match (if (match-end 2)
  6778. (match-string 3 s)
  6779. (match-string 1 s)) t t s)))
  6780. s)
  6781. (defvar org-priority-regexp) ; defined later in the file
  6782. (defvar org-after-sorting-entries-or-items-hook nil
  6783. "Hook that is run after a bunch of entries or items have been sorted.
  6784. When children are sorted, the cursor is in the parent line when this
  6785. hook gets called. When a region or a plain list is sorted, the cursor
  6786. will be in the first entry of the sorted region/list.")
  6787. (defun org-sort-entries-or-items
  6788. (&optional with-case sorting-type getkey-func compare-func property)
  6789. "Sort entries on a certain level of an outline tree, or plain list items.
  6790. If there is an active region, the entries in the region are sorted.
  6791. Else, if the cursor is before the first entry, sort the top-level items.
  6792. Else, the children of the entry at point are sorted.
  6793. If the cursor is at the first item in a plain list, the list items will be
  6794. sorted.
  6795. Sorting can be alphabetically, numerically, by date/time as given by
  6796. a time stamp, by a property or by priority.
  6797. The command prompts for the sorting type unless it has been given to the
  6798. function through the SORTING-TYPE argument, which needs to be a character,
  6799. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6800. precise meaning of each character:
  6801. n Numerically, by converting the beginning of the entry/item to a number.
  6802. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6803. t By date/time, either the first active time stamp in the entry, or, if
  6804. none exist, by the first inactive one.
  6805. In items, only the first line will be checked.
  6806. s By the scheduled date/time.
  6807. d By deadline date/time.
  6808. c By creation time, which is assumed to be the first inactive time stamp
  6809. at the beginning of a line.
  6810. p By priority according to the cookie.
  6811. r By the value of a property.
  6812. Capital letters will reverse the sort order.
  6813. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6814. called with point at the beginning of the record. It must return either
  6815. a string or a number that should serve as the sorting key for that record.
  6816. Comparing entries ignores case by default. However, with an optional argument
  6817. WITH-CASE, the sorting considers case as well."
  6818. (interactive "P")
  6819. (let ((case-func (if with-case 'identity 'downcase))
  6820. start beg end stars re re2
  6821. txt what tmp plain-list-p)
  6822. ;; Find beginning and end of region to sort
  6823. (cond
  6824. ((org-region-active-p)
  6825. ;; we will sort the region
  6826. (setq end (region-end)
  6827. what "region")
  6828. (goto-char (region-beginning))
  6829. (if (not (org-on-heading-p)) (outline-next-heading))
  6830. (setq start (point)))
  6831. ((org-at-item-p)
  6832. ;; we will sort this plain list
  6833. (org-beginning-of-item-list) (setq start (point))
  6834. (org-end-of-item-list)
  6835. (or (bolp) (insert "\n"))
  6836. (setq end (point))
  6837. (goto-char start)
  6838. (setq plain-list-p t
  6839. what "plain list"))
  6840. ((or (org-on-heading-p)
  6841. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6842. ;; we will sort the children of the current headline
  6843. (org-back-to-heading)
  6844. (setq start (point)
  6845. end (progn (org-end-of-subtree t t)
  6846. (or (bolp) (insert "\n"))
  6847. (org-back-over-empty-lines)
  6848. (point))
  6849. what "children")
  6850. (goto-char start)
  6851. (show-subtree)
  6852. (outline-next-heading))
  6853. (t
  6854. ;; we will sort the top-level entries in this file
  6855. (goto-char (point-min))
  6856. (or (org-on-heading-p) (outline-next-heading))
  6857. (setq start (point))
  6858. (goto-char (point-max))
  6859. (beginning-of-line 1)
  6860. (when (looking-at ".*?\\S-")
  6861. ;; File ends in a non-white line
  6862. (end-of-line 1)
  6863. (insert "\n"))
  6864. (setq end (point-max))
  6865. (setq what "top-level")
  6866. (goto-char start)
  6867. (show-all)))
  6868. (setq beg (point))
  6869. (if (>= beg end) (error "Nothing to sort"))
  6870. (unless plain-list-p
  6871. (looking-at "\\(\\*+\\)")
  6872. (setq stars (match-string 1)
  6873. re (concat "^" (regexp-quote stars) " +")
  6874. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6875. txt (buffer-substring beg end))
  6876. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6877. (if (and (not (equal stars "*")) (string-match re2 txt))
  6878. (error "Region to sort contains a level above the first entry")))
  6879. (unless sorting-type
  6880. (message
  6881. (if plain-list-p
  6882. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6883. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6884. [t]ime [s]cheduled [d]eadline [c]reated
  6885. A/N/T/S/D/C/P/O/F means reversed:")
  6886. what)
  6887. (setq sorting-type (read-char-exclusive))
  6888. (and (= (downcase sorting-type) ?f)
  6889. (setq getkey-func
  6890. (org-icompleting-read "Sort using function: "
  6891. obarray 'fboundp t nil nil))
  6892. (setq getkey-func (intern getkey-func)))
  6893. (and (= (downcase sorting-type) ?r)
  6894. (setq property
  6895. (org-icompleting-read "Property: "
  6896. (mapcar 'list (org-buffer-property-keys t))
  6897. nil t))))
  6898. (message "Sorting entries...")
  6899. (save-restriction
  6900. (narrow-to-region start end)
  6901. (let ((dcst (downcase sorting-type))
  6902. (case-fold-search nil)
  6903. (now (current-time)))
  6904. (sort-subr
  6905. (/= dcst sorting-type)
  6906. ;; This function moves to the beginning character of the "record" to
  6907. ;; be sorted.
  6908. (if plain-list-p
  6909. (lambda nil
  6910. (if (org-at-item-p) t (goto-char (point-max))))
  6911. (lambda nil
  6912. (if (re-search-forward re nil t)
  6913. (goto-char (match-beginning 0))
  6914. (goto-char (point-max)))))
  6915. ;; This function moves to the last character of the "record" being
  6916. ;; sorted.
  6917. (if plain-list-p
  6918. 'org-end-of-item
  6919. (lambda nil
  6920. (save-match-data
  6921. (condition-case nil
  6922. (outline-forward-same-level 1)
  6923. (error
  6924. (goto-char (point-max)))))))
  6925. ;; This function returns the value that gets sorted against.
  6926. (if plain-list-p
  6927. (lambda nil
  6928. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6929. (cond
  6930. ((= dcst ?n)
  6931. (string-to-number (buffer-substring (match-end 0)
  6932. (point-at-eol))))
  6933. ((= dcst ?a)
  6934. (buffer-substring (match-end 0) (point-at-eol)))
  6935. ((= dcst ?t)
  6936. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6937. (re-search-forward org-ts-regexp-both
  6938. (point-at-eol) t))
  6939. (org-time-string-to-seconds (match-string 0))
  6940. (org-float-time now)))
  6941. ((= dcst ?f)
  6942. (if getkey-func
  6943. (progn
  6944. (setq tmp (funcall getkey-func))
  6945. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6946. tmp)
  6947. (error "Invalid key function `%s'" getkey-func)))
  6948. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6949. (lambda nil
  6950. (cond
  6951. ((= dcst ?n)
  6952. (if (looking-at org-complex-heading-regexp)
  6953. (string-to-number (match-string 4))
  6954. nil))
  6955. ((= dcst ?a)
  6956. (if (looking-at org-complex-heading-regexp)
  6957. (funcall case-func (match-string 4))
  6958. nil))
  6959. ((= dcst ?t)
  6960. (let ((end (save-excursion (outline-next-heading) (point))))
  6961. (if (or (re-search-forward org-ts-regexp end t)
  6962. (re-search-forward org-ts-regexp-both end t))
  6963. (org-time-string-to-seconds (match-string 0))
  6964. (org-float-time now))))
  6965. ((= dcst ?c)
  6966. (let ((end (save-excursion (outline-next-heading) (point))))
  6967. (if (re-search-forward
  6968. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6969. end t)
  6970. (org-time-string-to-seconds (match-string 0))
  6971. (org-float-time now))))
  6972. ((= dcst ?s)
  6973. (let ((end (save-excursion (outline-next-heading) (point))))
  6974. (if (re-search-forward org-scheduled-time-regexp end t)
  6975. (org-time-string-to-seconds (match-string 1))
  6976. (org-float-time now))))
  6977. ((= dcst ?d)
  6978. (let ((end (save-excursion (outline-next-heading) (point))))
  6979. (if (re-search-forward org-deadline-time-regexp end t)
  6980. (org-time-string-to-seconds (match-string 1))
  6981. (org-float-time now))))
  6982. ((= dcst ?p)
  6983. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6984. (string-to-char (match-string 2))
  6985. org-default-priority))
  6986. ((= dcst ?r)
  6987. (or (org-entry-get nil property) ""))
  6988. ((= dcst ?o)
  6989. (if (looking-at org-complex-heading-regexp)
  6990. (- 9999 (length (member (match-string 2)
  6991. org-todo-keywords-1)))))
  6992. ((= dcst ?f)
  6993. (if getkey-func
  6994. (progn
  6995. (setq tmp (funcall getkey-func))
  6996. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6997. tmp)
  6998. (error "Invalid key function `%s'" getkey-func)))
  6999. (t (error "Invalid sorting type `%c'" sorting-type)))))
  7000. nil
  7001. (cond
  7002. ((= dcst ?a) 'string<)
  7003. ((= dcst ?f) compare-func)
  7004. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7005. (t nil)))))
  7006. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7007. (message "Sorting entries...done")))
  7008. (defun org-do-sort (table what &optional with-case sorting-type)
  7009. "Sort TABLE of WHAT according to SORTING-TYPE.
  7010. The user will be prompted for the SORTING-TYPE if the call to this
  7011. function does not specify it. WHAT is only for the prompt, to indicate
  7012. what is being sorted. The sorting key will be extracted from
  7013. the car of the elements of the table.
  7014. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7015. (unless sorting-type
  7016. (message
  7017. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7018. what)
  7019. (setq sorting-type (read-char-exclusive)))
  7020. (let ((dcst (downcase sorting-type))
  7021. extractfun comparefun)
  7022. ;; Define the appropriate functions
  7023. (cond
  7024. ((= dcst ?n)
  7025. (setq extractfun 'string-to-number
  7026. comparefun (if (= dcst sorting-type) '< '>)))
  7027. ((= dcst ?a)
  7028. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7029. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7030. comparefun (if (= dcst sorting-type)
  7031. 'string<
  7032. (lambda (a b) (and (not (string< a b))
  7033. (not (string= a b)))))))
  7034. ((= dcst ?t)
  7035. (setq extractfun
  7036. (lambda (x)
  7037. (if (or (string-match org-ts-regexp x)
  7038. (string-match org-ts-regexp-both x))
  7039. (org-float-time
  7040. (org-time-string-to-time (match-string 0 x)))
  7041. 0))
  7042. comparefun (if (= dcst sorting-type) '< '>)))
  7043. (t (error "Invalid sorting type `%c'" sorting-type)))
  7044. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7045. table)
  7046. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7047. ;;; The orgstruct minor mode
  7048. ;; Define a minor mode which can be used in other modes in order to
  7049. ;; integrate the org-mode structure editing commands.
  7050. ;; This is really a hack, because the org-mode structure commands use
  7051. ;; keys which normally belong to the major mode. Here is how it
  7052. ;; works: The minor mode defines all the keys necessary to operate the
  7053. ;; structure commands, but wraps the commands into a function which
  7054. ;; tests if the cursor is currently at a headline or a plain list
  7055. ;; item. If that is the case, the structure command is used,
  7056. ;; temporarily setting many Org-mode variables like regular
  7057. ;; expressions for filling etc. However, when any of those keys is
  7058. ;; used at a different location, function uses `key-binding' to look
  7059. ;; up if the key has an associated command in another currently active
  7060. ;; keymap (minor modes, major mode, global), and executes that
  7061. ;; command. There might be problems if any of the keys is otherwise
  7062. ;; used as a prefix key.
  7063. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7064. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7065. ;; addresses this by checking explicitly for both bindings.
  7066. (defvar orgstruct-mode-map (make-sparse-keymap)
  7067. "Keymap for the minor `orgstruct-mode'.")
  7068. (defvar org-local-vars nil
  7069. "List of local variables, for use by `orgstruct-mode'.")
  7070. ;;;###autoload
  7071. (define-minor-mode orgstruct-mode
  7072. "Toggle the minor mode `orgstruct-mode'.
  7073. This mode is for using Org-mode structure commands in other
  7074. modes. The following keys behave as if Org-mode were active, if
  7075. the cursor is on a headline, or on a plain list item (both as
  7076. defined by Org-mode).
  7077. M-up Move entry/item up
  7078. M-down Move entry/item down
  7079. M-left Promote
  7080. M-right Demote
  7081. M-S-up Move entry/item up
  7082. M-S-down Move entry/item down
  7083. M-S-left Promote subtree
  7084. M-S-right Demote subtree
  7085. M-q Fill paragraph and items like in Org-mode
  7086. C-c ^ Sort entries
  7087. C-c - Cycle list bullet
  7088. TAB Cycle item visibility
  7089. M-RET Insert new heading/item
  7090. S-M-RET Insert new TODO heading / Checkbox item
  7091. C-c C-c Set tags / toggle checkbox"
  7092. nil " OrgStruct" nil
  7093. (org-load-modules-maybe)
  7094. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7095. ;;;###autoload
  7096. (defun turn-on-orgstruct ()
  7097. "Unconditionally turn on `orgstruct-mode'."
  7098. (orgstruct-mode 1))
  7099. (defun orgstruct++-mode (&optional arg)
  7100. "Toggle `orgstruct-mode', the enhanced version of it.
  7101. In addition to setting orgstruct-mode, this also exports all indentation
  7102. and autofilling variables from org-mode into the buffer. It will also
  7103. recognize item context in multiline items.
  7104. Note that turning off orgstruct-mode will *not* remove the
  7105. indentation/paragraph settings. This can only be done by refreshing the
  7106. major mode, for example with \\[normal-mode]."
  7107. (interactive "P")
  7108. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7109. (if (< arg 1)
  7110. (orgstruct-mode -1)
  7111. (orgstruct-mode 1)
  7112. (let (var val)
  7113. (mapc
  7114. (lambda (x)
  7115. (when (string-match
  7116. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7117. (symbol-name (car x)))
  7118. (setq var (car x) val (nth 1 x))
  7119. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7120. org-local-vars)
  7121. (org-set-local 'orgstruct-is-++ t))))
  7122. (defvar orgstruct-is-++ nil
  7123. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7124. (make-variable-buffer-local 'orgstruct-is-++)
  7125. ;;;###autoload
  7126. (defun turn-on-orgstruct++ ()
  7127. "Unconditionally turn on `orgstruct++-mode'."
  7128. (orgstruct++-mode 1))
  7129. (defun orgstruct-error ()
  7130. "Error when there is no default binding for a structure key."
  7131. (interactive)
  7132. (error "This key has no function outside structure elements"))
  7133. (defun orgstruct-setup ()
  7134. "Setup orgstruct keymaps."
  7135. (let ((nfunc 0)
  7136. (bindings
  7137. (list
  7138. '([(meta up)] org-metaup)
  7139. '([(meta down)] org-metadown)
  7140. '([(meta left)] org-metaleft)
  7141. '([(meta right)] org-metaright)
  7142. '([(meta shift up)] org-shiftmetaup)
  7143. '([(meta shift down)] org-shiftmetadown)
  7144. '([(meta shift left)] org-shiftmetaleft)
  7145. '([(meta shift right)] org-shiftmetaright)
  7146. '([?\e (up)] org-metaup)
  7147. '([?\e (down)] org-metadown)
  7148. '([?\e (left)] org-metaleft)
  7149. '([?\e (right)] org-metaright)
  7150. '([?\e (shift up)] org-shiftmetaup)
  7151. '([?\e (shift down)] org-shiftmetadown)
  7152. '([?\e (shift left)] org-shiftmetaleft)
  7153. '([?\e (shift right)] org-shiftmetaright)
  7154. '([(shift up)] org-shiftup)
  7155. '([(shift down)] org-shiftdown)
  7156. '([(shift left)] org-shiftleft)
  7157. '([(shift right)] org-shiftright)
  7158. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7159. '("\M-q" fill-paragraph)
  7160. '("\C-c^" org-sort)
  7161. '("\C-c-" org-cycle-list-bullet)))
  7162. elt key fun cmd)
  7163. (while (setq elt (pop bindings))
  7164. (setq nfunc (1+ nfunc))
  7165. (setq key (org-key (car elt))
  7166. fun (nth 1 elt)
  7167. cmd (orgstruct-make-binding fun nfunc key))
  7168. (org-defkey orgstruct-mode-map key cmd))
  7169. ;; Special treatment needed for TAB and RET
  7170. (org-defkey orgstruct-mode-map [(tab)]
  7171. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7172. (org-defkey orgstruct-mode-map "\C-i"
  7173. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7174. (org-defkey orgstruct-mode-map "\M-\C-m"
  7175. (orgstruct-make-binding 'org-insert-heading 105
  7176. "\M-\C-m" [(meta return)]))
  7177. (org-defkey orgstruct-mode-map [(meta return)]
  7178. (orgstruct-make-binding 'org-insert-heading 106
  7179. [(meta return)] "\M-\C-m"))
  7180. (org-defkey orgstruct-mode-map [(shift meta return)]
  7181. (orgstruct-make-binding 'org-insert-todo-heading 107
  7182. [(meta return)] "\M-\C-m"))
  7183. (org-defkey orgstruct-mode-map "\e\C-m"
  7184. (orgstruct-make-binding 'org-insert-heading 108
  7185. "\e\C-m" [?\e (return)]))
  7186. (org-defkey orgstruct-mode-map [?\e (return)]
  7187. (orgstruct-make-binding 'org-insert-heading 109
  7188. [?\e (return)] "\e\C-m"))
  7189. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7190. (orgstruct-make-binding 'org-insert-todo-heading 110
  7191. [?\e (return)] "\e\C-m"))
  7192. (unless org-local-vars
  7193. (setq org-local-vars (org-get-local-variables)))
  7194. t))
  7195. (defun orgstruct-make-binding (fun n &rest keys)
  7196. "Create a function for binding in the structure minor mode.
  7197. FUN is the command to call inside a table. N is used to create a unique
  7198. command name. KEYS are keys that should be checked in for a command
  7199. to execute outside of tables."
  7200. (eval
  7201. (list 'defun
  7202. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7203. '(arg)
  7204. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7205. "Outside of structure, run the binding of `"
  7206. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7207. "'.")
  7208. '(interactive "p")
  7209. (list 'if
  7210. `(org-context-p 'headline 'item
  7211. (and orgstruct-is-++
  7212. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7213. 'item-body))
  7214. (list 'org-run-like-in-org-mode (list 'quote fun))
  7215. (list 'let '(orgstruct-mode)
  7216. (list 'call-interactively
  7217. (append '(or)
  7218. (mapcar (lambda (k)
  7219. (list 'key-binding k))
  7220. keys)
  7221. '('orgstruct-error))))))))
  7222. (defun org-context-p (&rest contexts)
  7223. "Check if local context is any of CONTEXTS.
  7224. Possible values in the list of contexts are `table', `headline', and `item'."
  7225. (let ((pos (point)))
  7226. (goto-char (point-at-bol))
  7227. (prog1 (or (and (memq 'table contexts)
  7228. (looking-at "[ \t]*|"))
  7229. (and (memq 'headline contexts)
  7230. ;;????????? (looking-at "\\*+"))
  7231. (looking-at outline-regexp))
  7232. (and (memq 'item contexts)
  7233. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7234. (and (memq 'item-body contexts)
  7235. (org-in-item-p)))
  7236. (goto-char pos))))
  7237. (defun org-get-local-variables ()
  7238. "Return a list of all local variables in an org-mode buffer."
  7239. (let (varlist)
  7240. (with-current-buffer (get-buffer-create "*Org tmp*")
  7241. (erase-buffer)
  7242. (org-mode)
  7243. (setq varlist (buffer-local-variables)))
  7244. (kill-buffer "*Org tmp*")
  7245. (delq nil
  7246. (mapcar
  7247. (lambda (x)
  7248. (setq x
  7249. (if (symbolp x)
  7250. (list x)
  7251. (list (car x) (list 'quote (cdr x)))))
  7252. (if (string-match
  7253. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7254. (symbol-name (car x)))
  7255. x nil))
  7256. varlist))))
  7257. ;;;###autoload
  7258. (defun org-run-like-in-org-mode (cmd)
  7259. "Run a command, pretending that the current buffer is in Org-mode.
  7260. This will temporarily bind local variables that are typically bound in
  7261. Org-mode to the values they have in Org-mode, and then interactively
  7262. call CMD."
  7263. (org-load-modules-maybe)
  7264. (unless org-local-vars
  7265. (setq org-local-vars (org-get-local-variables)))
  7266. (eval (list 'let org-local-vars
  7267. (list 'call-interactively (list 'quote cmd)))))
  7268. ;;;; Archiving
  7269. (defun org-get-category (&optional pos)
  7270. "Get the category applying to position POS."
  7271. (get-text-property (or pos (point)) 'org-category))
  7272. (defun org-refresh-category-properties ()
  7273. "Refresh category text properties in the buffer."
  7274. (let ((def-cat (cond
  7275. ((null org-category)
  7276. (if buffer-file-name
  7277. (file-name-sans-extension
  7278. (file-name-nondirectory buffer-file-name))
  7279. "???"))
  7280. ((symbolp org-category) (symbol-name org-category))
  7281. (t org-category)))
  7282. beg end cat pos optionp)
  7283. (org-unmodified
  7284. (save-excursion
  7285. (save-restriction
  7286. (widen)
  7287. (goto-char (point-min))
  7288. (put-text-property (point) (point-max) 'org-category def-cat)
  7289. (while (re-search-forward
  7290. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7291. (setq pos (match-end 0)
  7292. optionp (equal (char-after (match-beginning 0)) ?#)
  7293. cat (org-trim (match-string 2)))
  7294. (if optionp
  7295. (setq beg (point-at-bol) end (point-max))
  7296. (org-back-to-heading t)
  7297. (setq beg (point) end (org-end-of-subtree t t)))
  7298. (put-text-property beg end 'org-category cat)
  7299. (goto-char pos)))))))
  7300. ;;;; Link Stuff
  7301. ;;; Link abbreviations
  7302. (defun org-link-expand-abbrev (link)
  7303. "Apply replacements as defined in `org-link-abbrev-alist."
  7304. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7305. (let* ((key (match-string 1 link))
  7306. (as (or (assoc key org-link-abbrev-alist-local)
  7307. (assoc key org-link-abbrev-alist)))
  7308. (tag (and (match-end 2) (match-string 3 link)))
  7309. rpl)
  7310. (if (not as)
  7311. link
  7312. (setq rpl (cdr as))
  7313. (cond
  7314. ((symbolp rpl) (funcall rpl tag))
  7315. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7316. ((string-match "%h" rpl)
  7317. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7318. (t (concat rpl tag)))))
  7319. link))
  7320. ;;; Storing and inserting links
  7321. (defvar org-insert-link-history nil
  7322. "Minibuffer history for links inserted with `org-insert-link'.")
  7323. (defvar org-stored-links nil
  7324. "Contains the links stored with `org-store-link'.")
  7325. (defvar org-store-link-plist nil
  7326. "Plist with info about the most recently link created with `org-store-link'.")
  7327. (defvar org-link-protocols nil
  7328. "Link protocols added to Org-mode using `org-add-link-type'.")
  7329. (defvar org-store-link-functions nil
  7330. "List of functions that are called to create and store a link.
  7331. Each function will be called in turn until one returns a non-nil
  7332. value. Each function should check if it is responsible for creating
  7333. this link (for example by looking at the major mode).
  7334. If not, it must exit and return nil.
  7335. If yes, it should return a non-nil value after a calling
  7336. `org-store-link-props' with a list of properties and values.
  7337. Special properties are:
  7338. :type The link prefix, like \"http\". This must be given.
  7339. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7340. This is obligatory as well.
  7341. :description Optional default description for the second pair
  7342. of brackets in an Org-mode link. The user can still change
  7343. this when inserting this link into an Org-mode buffer.
  7344. In addition to these, any additional properties can be specified
  7345. and then used in remember templates.")
  7346. (defun org-add-link-type (type &optional follow export)
  7347. "Add TYPE to the list of `org-link-types'.
  7348. Re-compute all regular expressions depending on `org-link-types'
  7349. FOLLOW and EXPORT are two functions.
  7350. FOLLOW should take the link path as the single argument and do whatever
  7351. is necessary to follow the link, for example find a file or display
  7352. a mail message.
  7353. EXPORT should format the link path for export to one of the export formats.
  7354. It should be a function accepting three arguments:
  7355. path the path of the link, the text after the prefix (like \"http:\")
  7356. desc the description of the link, if any, nil if there was no description
  7357. format the export format, a symbol like `html' or `latex'.
  7358. The function may use the FORMAT information to return different values
  7359. depending on the format. The return value will be put literally into
  7360. the exported file.
  7361. Org-mode has a built-in default for exporting links. If you are happy with
  7362. this default, there is no need to define an export function for the link
  7363. type. For a simple example of an export function, see `org-bbdb.el'."
  7364. (add-to-list 'org-link-types type t)
  7365. (org-make-link-regexps)
  7366. (if (assoc type org-link-protocols)
  7367. (setcdr (assoc type org-link-protocols) (list follow export))
  7368. (push (list type follow export) org-link-protocols)))
  7369. (defvar org-agenda-buffer-name)
  7370. ;;;###autoload
  7371. (defun org-store-link (arg)
  7372. "\\<org-mode-map>Store an org-link to the current location.
  7373. This link is added to `org-stored-links' and can later be inserted
  7374. into an org-buffer with \\[org-insert-link].
  7375. For some link types, a prefix arg is interpreted:
  7376. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7377. For file links, arg negates `org-context-in-file-links'."
  7378. (interactive "P")
  7379. (org-load-modules-maybe)
  7380. (setq org-store-link-plist nil) ; reset
  7381. (let ((outline-regexp (org-get-limited-outline-regexp))
  7382. link cpltxt desc description search txt custom-id agenda-link)
  7383. (cond
  7384. ((run-hook-with-args-until-success 'org-store-link-functions)
  7385. (setq link (plist-get org-store-link-plist :link)
  7386. desc (or (plist-get org-store-link-plist :description) link)))
  7387. ((equal (buffer-name) "*Org Edit Src Example*")
  7388. (let (label gc)
  7389. (while (or (not label)
  7390. (save-excursion
  7391. (save-restriction
  7392. (widen)
  7393. (goto-char (point-min))
  7394. (re-search-forward
  7395. (regexp-quote (format org-coderef-label-format label))
  7396. nil t))))
  7397. (when label (message "Label exists already") (sit-for 2))
  7398. (setq label (read-string "Code line label: " label)))
  7399. (end-of-line 1)
  7400. (setq link (format org-coderef-label-format label))
  7401. (setq gc (- 79 (length link)))
  7402. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7403. (insert link)
  7404. (setq link (concat "(" label ")") desc nil)))
  7405. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7406. ;; We are in the agenda, link to referenced location
  7407. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7408. (get-text-property (point) 'org-marker))))
  7409. (when m
  7410. (org-with-point-at m
  7411. (setq agenda-link
  7412. (if (interactive-p)
  7413. (call-interactively 'org-store-link)
  7414. (org-store-link nil)))))))
  7415. ((eq major-mode 'calendar-mode)
  7416. (let ((cd (calendar-cursor-to-date)))
  7417. (setq link
  7418. (format-time-string
  7419. (car org-time-stamp-formats)
  7420. (apply 'encode-time
  7421. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7422. nil nil nil))))
  7423. (org-store-link-props :type "calendar" :date cd)))
  7424. ((eq major-mode 'w3-mode)
  7425. (setq cpltxt (if (and (buffer-name)
  7426. (not (string-match "Untitled" (buffer-name))))
  7427. (buffer-name)
  7428. (url-view-url t))
  7429. link (org-make-link (url-view-url t)))
  7430. (org-store-link-props :type "w3" :url (url-view-url t)))
  7431. ((eq major-mode 'w3m-mode)
  7432. (setq cpltxt (or w3m-current-title w3m-current-url)
  7433. link (org-make-link w3m-current-url))
  7434. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7435. ((setq search (run-hook-with-args-until-success
  7436. 'org-create-file-search-functions))
  7437. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7438. "::" search))
  7439. (setq cpltxt (or description link)))
  7440. ((eq major-mode 'image-mode)
  7441. (setq cpltxt (concat "file:"
  7442. (abbreviate-file-name buffer-file-name))
  7443. link (org-make-link cpltxt))
  7444. (org-store-link-props :type "image" :file buffer-file-name))
  7445. ((eq major-mode 'dired-mode)
  7446. ;; link to the file in the current line
  7447. (let ((file (dired-get-filename nil t)))
  7448. (setq file (if file
  7449. (abbreviate-file-name
  7450. (expand-file-name (dired-get-filename nil t)))
  7451. ;; otherwise, no file so use current directory.
  7452. default-directory))
  7453. (setq cpltxt (concat "file:" file)
  7454. link (org-make-link cpltxt))))
  7455. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7456. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7457. (cond
  7458. ((org-in-regexp "<<\\(.*?\\)>>")
  7459. (setq cpltxt
  7460. (concat "file:"
  7461. (abbreviate-file-name
  7462. (buffer-file-name (buffer-base-buffer)))
  7463. "::" (match-string 1))
  7464. link (org-make-link cpltxt)))
  7465. ((and (featurep 'org-id)
  7466. (or (eq org-link-to-org-use-id t)
  7467. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7468. (interactive-p))
  7469. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7470. (interactive-p)
  7471. (not custom-id))
  7472. (and org-link-to-org-use-id
  7473. (condition-case nil
  7474. (org-entry-get nil "ID")
  7475. (error nil)))))
  7476. ;; We can make a link using the ID.
  7477. (setq link (condition-case nil
  7478. (prog1 (org-id-store-link)
  7479. (setq desc (plist-get org-store-link-plist
  7480. :description)))
  7481. (error
  7482. ;; probably before first headline, link to file only
  7483. (concat "file:"
  7484. (abbreviate-file-name
  7485. (buffer-file-name (buffer-base-buffer))))))))
  7486. (t
  7487. ;; Just link to current headline
  7488. (setq cpltxt (concat "file:"
  7489. (abbreviate-file-name
  7490. (buffer-file-name (buffer-base-buffer)))))
  7491. ;; Add a context search string
  7492. (when (org-xor org-context-in-file-links arg)
  7493. (setq txt (cond
  7494. ((org-on-heading-p) nil)
  7495. ((org-region-active-p)
  7496. (buffer-substring (region-beginning) (region-end)))
  7497. (t nil)))
  7498. (when (or (null txt) (string-match "\\S-" txt))
  7499. (setq cpltxt
  7500. (concat cpltxt "::"
  7501. (condition-case nil
  7502. (org-make-org-heading-search-string txt)
  7503. (error "")))
  7504. desc (or (nth 4 (ignore-errors
  7505. (org-heading-components))) "NONE"))))
  7506. (if (string-match "::\\'" cpltxt)
  7507. (setq cpltxt (substring cpltxt 0 -2)))
  7508. (setq link (org-make-link cpltxt)))))
  7509. ((buffer-file-name (buffer-base-buffer))
  7510. ;; Just link to this file here.
  7511. (setq cpltxt (concat "file:"
  7512. (abbreviate-file-name
  7513. (buffer-file-name (buffer-base-buffer)))))
  7514. ;; Add a context string
  7515. (when (org-xor org-context-in-file-links arg)
  7516. (setq txt (if (org-region-active-p)
  7517. (buffer-substring (region-beginning) (region-end))
  7518. (buffer-substring (point-at-bol) (point-at-eol))))
  7519. ;; Only use search option if there is some text.
  7520. (when (string-match "\\S-" txt)
  7521. (setq cpltxt
  7522. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7523. desc "NONE")))
  7524. (setq link (org-make-link cpltxt)))
  7525. ((interactive-p)
  7526. (error "Cannot link to a buffer which is not visiting a file"))
  7527. (t (setq link nil)))
  7528. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7529. (setq link (or link cpltxt)
  7530. desc (or desc cpltxt))
  7531. (if (equal desc "NONE") (setq desc nil))
  7532. (if (and (or (interactive-p) executing-kbd-macro) link)
  7533. (progn
  7534. (setq org-stored-links
  7535. (cons (list link desc) org-stored-links))
  7536. (message "Stored: %s" (or desc link))
  7537. (when custom-id
  7538. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7539. "::#" custom-id))
  7540. (setq org-stored-links
  7541. (cons (list link desc) org-stored-links))))
  7542. (or agenda-link (and link (org-make-link-string link desc))))))
  7543. (defun org-store-link-props (&rest plist)
  7544. "Store link properties, extract names and addresses."
  7545. (let (x adr)
  7546. (when (setq x (plist-get plist :from))
  7547. (setq adr (mail-extract-address-components x))
  7548. (setq plist (plist-put plist :fromname (car adr)))
  7549. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7550. (when (setq x (plist-get plist :to))
  7551. (setq adr (mail-extract-address-components x))
  7552. (setq plist (plist-put plist :toname (car adr)))
  7553. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7554. (let ((from (plist-get plist :from))
  7555. (to (plist-get plist :to)))
  7556. (when (and from to org-from-is-user-regexp)
  7557. (setq plist
  7558. (plist-put plist :fromto
  7559. (if (string-match org-from-is-user-regexp from)
  7560. (concat "to %t")
  7561. (concat "from %f"))))))
  7562. (setq org-store-link-plist plist))
  7563. (defun org-add-link-props (&rest plist)
  7564. "Add these properties to the link property list."
  7565. (let (key value)
  7566. (while plist
  7567. (setq key (pop plist) value (pop plist))
  7568. (setq org-store-link-plist
  7569. (plist-put org-store-link-plist key value)))))
  7570. (defun org-email-link-description (&optional fmt)
  7571. "Return the description part of an email link.
  7572. This takes information from `org-store-link-plist' and formats it
  7573. according to FMT (default from `org-email-link-description-format')."
  7574. (setq fmt (or fmt org-email-link-description-format))
  7575. (let* ((p org-store-link-plist)
  7576. (to (plist-get p :toaddress))
  7577. (from (plist-get p :fromaddress))
  7578. (table
  7579. (list
  7580. (cons "%c" (plist-get p :fromto))
  7581. (cons "%F" (plist-get p :from))
  7582. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7583. (cons "%T" (plist-get p :to))
  7584. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7585. (cons "%s" (plist-get p :subject))
  7586. (cons "%m" (plist-get p :message-id)))))
  7587. (when (string-match "%c" fmt)
  7588. ;; Check if the user wrote this message
  7589. (if (and org-from-is-user-regexp from to
  7590. (save-match-data (string-match org-from-is-user-regexp from)))
  7591. (setq fmt (replace-match "to %t" t t fmt))
  7592. (setq fmt (replace-match "from %f" t t fmt))))
  7593. (org-replace-escapes fmt table)))
  7594. (defun org-make-org-heading-search-string (&optional string heading)
  7595. "Make search string for STRING or current headline."
  7596. (interactive)
  7597. (let ((s (or string (org-get-heading))))
  7598. (unless (and string (not heading))
  7599. ;; We are using a headline, clean up garbage in there.
  7600. (if (string-match org-todo-regexp s)
  7601. (setq s (replace-match "" t t s)))
  7602. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7603. (setq s (replace-match "" t t s)))
  7604. (setq s (org-trim s))
  7605. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7606. org-comment-string "\\)") s)
  7607. (setq s (replace-match "" t t s)))
  7608. (while (string-match org-ts-regexp s)
  7609. (setq s (replace-match "" t t s))))
  7610. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7611. (setq s (replace-match " " t t s)))
  7612. (or string (setq s (concat "*" s))) ; Add * for headlines
  7613. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7614. (defun org-make-link (&rest strings)
  7615. "Concatenate STRINGS."
  7616. (apply 'concat strings))
  7617. (defun org-make-link-string (link &optional description)
  7618. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7619. (unless (string-match "\\S-" link)
  7620. (error "Empty link"))
  7621. (when (and description
  7622. (stringp description)
  7623. (not (string-match "\\S-" description)))
  7624. (setq description nil))
  7625. (when (stringp description)
  7626. ;; Remove brackets from the description, they are fatal.
  7627. (while (string-match "\\[" description)
  7628. (setq description (replace-match "{" t t description)))
  7629. (while (string-match "\\]" description)
  7630. (setq description (replace-match "}" t t description))))
  7631. (when (equal (org-link-escape link) description)
  7632. ;; No description needed, it is identical
  7633. (setq description nil))
  7634. (when (and (not description)
  7635. (not (equal link (org-link-escape link))))
  7636. (setq description (org-extract-attributes link)))
  7637. (concat "[[" (org-link-escape link) "]"
  7638. (if description (concat "[" description "]") "")
  7639. "]"))
  7640. (defconst org-link-escape-chars
  7641. '((?\ . "%20")
  7642. (?\[ . "%5B")
  7643. (?\] . "%5D")
  7644. (?\340 . "%E0") ; `a
  7645. (?\342 . "%E2") ; ^a
  7646. (?\347 . "%E7") ; ,c
  7647. (?\350 . "%E8") ; `e
  7648. (?\351 . "%E9") ; 'e
  7649. (?\352 . "%EA") ; ^e
  7650. (?\356 . "%EE") ; ^i
  7651. (?\364 . "%F4") ; ^o
  7652. (?\371 . "%F9") ; `u
  7653. (?\373 . "%FB") ; ^u
  7654. (?\; . "%3B")
  7655. ;; (?? . "%3F")
  7656. (?= . "%3D")
  7657. (?+ . "%2B")
  7658. )
  7659. "Association list of escapes for some characters problematic in links.
  7660. This is the list that is used for internal purposes.")
  7661. (defvar org-url-encoding-use-url-hexify nil)
  7662. (defconst org-link-escape-chars-browser
  7663. '((?\ . "%20")) ; 32 for the SPC char
  7664. "Association list of escapes for some characters problematic in links.
  7665. This is the list that is used before handing over to the browser.")
  7666. (defun org-link-escape (text &optional table)
  7667. "Escape characters in TEXT that are problematic for links."
  7668. (if (and org-url-encoding-use-url-hexify (not table))
  7669. (url-hexify-string text)
  7670. (setq table (or table org-link-escape-chars))
  7671. (when text
  7672. (let ((re (mapconcat (lambda (x) (regexp-quote
  7673. (char-to-string (car x))))
  7674. table "\\|")))
  7675. (while (string-match re text)
  7676. (setq text
  7677. (replace-match
  7678. (cdr (assoc (string-to-char (match-string 0 text))
  7679. table))
  7680. t t text)))
  7681. text))))
  7682. (defun org-link-unescape (text &optional table)
  7683. "Reverse the action of `org-link-escape'."
  7684. (if (and org-url-encoding-use-url-hexify (not table))
  7685. (url-unhex-string text)
  7686. (setq table (or table org-link-escape-chars))
  7687. (when text
  7688. (let ((case-fold-search t)
  7689. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7690. table "\\|")))
  7691. (while (string-match re text)
  7692. (setq text
  7693. (replace-match
  7694. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7695. table)))
  7696. t t text)))
  7697. text))))
  7698. (defun org-xor (a b)
  7699. "Exclusive or."
  7700. (if a (not b) b))
  7701. (defun org-fixup-message-id-for-http (s)
  7702. "Replace special characters in a message id, so it can be used in an http query."
  7703. (when (string-match "%" s)
  7704. (setq s (mapconcat (lambda (c)
  7705. (if (eq c ?%)
  7706. "%25"
  7707. (char-to-string c)))
  7708. s "")))
  7709. (while (string-match "<" s)
  7710. (setq s (replace-match "%3C" t t s)))
  7711. (while (string-match ">" s)
  7712. (setq s (replace-match "%3E" t t s)))
  7713. (while (string-match "@" s)
  7714. (setq s (replace-match "%40" t t s)))
  7715. s)
  7716. ;;;###autoload
  7717. (defun org-insert-link-global ()
  7718. "Insert a link like Org-mode does.
  7719. This command can be called in any mode to insert a link in Org-mode syntax."
  7720. (interactive)
  7721. (org-load-modules-maybe)
  7722. (org-run-like-in-org-mode 'org-insert-link))
  7723. (defun org-insert-link (&optional complete-file link-location)
  7724. "Insert a link. At the prompt, enter the link.
  7725. Completion can be used to insert any of the link protocol prefixes like
  7726. http or ftp in use.
  7727. The history can be used to select a link previously stored with
  7728. `org-store-link'. When the empty string is entered (i.e. if you just
  7729. press RET at the prompt), the link defaults to the most recently
  7730. stored link. As SPC triggers completion in the minibuffer, you need to
  7731. use M-SPC or C-q SPC to force the insertion of a space character.
  7732. You will also be prompted for a description, and if one is given, it will
  7733. be displayed in the buffer instead of the link.
  7734. If there is already a link at point, this command will allow you to edit link
  7735. and description parts.
  7736. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7737. be selected using completion. The path to the file will be relative to the
  7738. current directory if the file is in the current directory or a subdirectory.
  7739. Otherwise, the link will be the absolute path as completed in the minibuffer
  7740. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7741. option `org-link-file-path-type'.
  7742. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7743. the current directory or below.
  7744. With three \\[universal-argument] prefixes, negate the meaning of
  7745. `org-keep-stored-link-after-insertion'.
  7746. If `org-make-link-description-function' is non-nil, this function will be
  7747. called with the link target, and the result will be the default
  7748. link description.
  7749. If the LINK-LOCATION parameter is non-nil, this value will be
  7750. used as the link location instead of reading one interactively."
  7751. (interactive "P")
  7752. (let* ((wcf (current-window-configuration))
  7753. (region (if (org-region-active-p)
  7754. (buffer-substring (region-beginning) (region-end))))
  7755. (remove (and region (list (region-beginning) (region-end))))
  7756. (desc region)
  7757. tmphist ; byte-compile incorrectly complains about this
  7758. (link link-location)
  7759. entry file all-prefixes)
  7760. (cond
  7761. (link-location) ; specified by arg, just use it.
  7762. ((org-in-regexp org-bracket-link-regexp 1)
  7763. ;; We do have a link at point, and we are going to edit it.
  7764. (setq remove (list (match-beginning 0) (match-end 0)))
  7765. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7766. (setq link (read-string "Link: "
  7767. (org-link-unescape
  7768. (org-match-string-no-properties 1)))))
  7769. ((or (org-in-regexp org-angle-link-re)
  7770. (org-in-regexp org-plain-link-re))
  7771. ;; Convert to bracket link
  7772. (setq remove (list (match-beginning 0) (match-end 0))
  7773. link (read-string "Link: "
  7774. (org-remove-angle-brackets (match-string 0)))))
  7775. ((member complete-file '((4) (16)))
  7776. ;; Completing read for file names.
  7777. (setq link (org-file-complete-link complete-file)))
  7778. (t
  7779. ;; Read link, with completion for stored links.
  7780. (with-output-to-temp-buffer "*Org Links*"
  7781. (princ "Insert a link.
  7782. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7783. (when org-stored-links
  7784. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7785. (princ (mapconcat
  7786. (lambda (x)
  7787. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7788. (reverse org-stored-links) "\n"))))
  7789. (let ((cw (selected-window)))
  7790. (select-window (get-buffer-window "*Org Links*" 'visible))
  7791. (setq truncate-lines t)
  7792. (unless (pos-visible-in-window-p (point-max))
  7793. (org-fit-window-to-buffer))
  7794. (and (window-live-p cw) (select-window cw)))
  7795. ;; Fake a link history, containing the stored links.
  7796. (setq tmphist (append (mapcar 'car org-stored-links)
  7797. org-insert-link-history))
  7798. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7799. (mapcar 'car org-link-abbrev-alist)
  7800. org-link-types))
  7801. (unwind-protect
  7802. (progn
  7803. (setq link
  7804. (let ((org-completion-use-ido nil)
  7805. (org-completion-use-iswitchb nil))
  7806. (org-completing-read
  7807. "Link: "
  7808. (append
  7809. (mapcar (lambda (x) (list (concat x ":")))
  7810. all-prefixes)
  7811. (mapcar 'car org-stored-links))
  7812. nil nil nil
  7813. 'tmphist
  7814. (car (car org-stored-links)))))
  7815. (if (not (string-match "\\S-" link))
  7816. (error "No link selected"))
  7817. (if (or (member link all-prefixes)
  7818. (and (equal ":" (substring link -1))
  7819. (member (substring link 0 -1) all-prefixes)
  7820. (setq link (substring link 0 -1))))
  7821. (setq link (org-link-try-special-completion link))))
  7822. (set-window-configuration wcf)
  7823. (kill-buffer "*Org Links*"))
  7824. (setq entry (assoc link org-stored-links))
  7825. (or entry (push link org-insert-link-history))
  7826. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7827. (not org-keep-stored-link-after-insertion))
  7828. (setq org-stored-links (delq (assoc link org-stored-links)
  7829. org-stored-links)))
  7830. (setq desc (or desc (nth 1 entry)))))
  7831. (if (string-match org-plain-link-re link)
  7832. ;; URL-like link, normalize the use of angular brackets.
  7833. (setq link (org-make-link (org-remove-angle-brackets link))))
  7834. ;; Check if we are linking to the current file with a search option
  7835. ;; If yes, simplify the link by using only the search option.
  7836. (when (and buffer-file-name
  7837. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7838. (let* ((path (match-string 1 link))
  7839. (case-fold-search nil)
  7840. (search (match-string 2 link)))
  7841. (save-match-data
  7842. (if (equal (file-truename buffer-file-name) (file-truename path))
  7843. ;; We are linking to this same file, with a search option
  7844. (setq link search)))))
  7845. ;; Check if we can/should use a relative path. If yes, simplify the link
  7846. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7847. (let* ((type (match-string 1 link))
  7848. (path (match-string 2 link))
  7849. (origpath path)
  7850. (case-fold-search nil))
  7851. (cond
  7852. ((or (eq org-link-file-path-type 'absolute)
  7853. (equal complete-file '(16)))
  7854. (setq path (abbreviate-file-name (expand-file-name path))))
  7855. ((eq org-link-file-path-type 'noabbrev)
  7856. (setq path (expand-file-name path)))
  7857. ((eq org-link-file-path-type 'relative)
  7858. (setq path (file-relative-name path)))
  7859. (t
  7860. (save-match-data
  7861. (if (string-match (concat "^" (regexp-quote
  7862. (expand-file-name
  7863. (file-name-as-directory
  7864. default-directory))))
  7865. (expand-file-name path))
  7866. ;; We are linking a file with relative path name.
  7867. (setq path (substring (expand-file-name path)
  7868. (match-end 0)))
  7869. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7870. (setq link (concat type path))
  7871. (if (equal desc origpath)
  7872. (setq desc path))))
  7873. (if org-make-link-description-function
  7874. (setq desc (funcall org-make-link-description-function link desc)))
  7875. (setq desc (read-string "Description: " desc))
  7876. (unless (string-match "\\S-" desc) (setq desc nil))
  7877. (if remove (apply 'delete-region remove))
  7878. (insert (org-make-link-string link desc))))
  7879. (defun org-link-try-special-completion (type)
  7880. "If there is completion support for link type TYPE, offer it."
  7881. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7882. (if (functionp fun)
  7883. (funcall fun)
  7884. (read-string "Link (no completion support): " (concat type ":")))))
  7885. (defun org-file-complete-link (&optional arg)
  7886. "Create a file link using completion."
  7887. (let (file link)
  7888. (setq file (read-file-name "File: "))
  7889. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7890. (pwd1 (file-name-as-directory (abbreviate-file-name
  7891. (expand-file-name ".")))))
  7892. (cond
  7893. ((equal arg '(16))
  7894. (setq link (org-make-link
  7895. "file:"
  7896. (abbreviate-file-name (expand-file-name file)))))
  7897. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7898. (setq link (org-make-link "file:" (match-string 1 file))))
  7899. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7900. (expand-file-name file))
  7901. (setq link (org-make-link
  7902. "file:" (match-string 1 (expand-file-name file)))))
  7903. (t (setq link (org-make-link "file:" file)))))
  7904. link))
  7905. (defun org-completing-read (&rest args)
  7906. "Completing-read with SPACE being a normal character."
  7907. (let ((minibuffer-local-completion-map
  7908. (copy-keymap minibuffer-local-completion-map)))
  7909. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7910. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7911. (apply 'org-icompleting-read args)))
  7912. (defun org-completing-read-no-i (&rest args)
  7913. (let (org-completion-use-ido org-completion-use-iswitchb)
  7914. (apply 'org-completing-read args)))
  7915. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7916. "Use iswitch as a completing-read replacement to choose from choices.
  7917. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7918. from."
  7919. (let* ((iswitchb-use-virtual-buffers nil)
  7920. (iswitchb-make-buflist-hook
  7921. (lambda ()
  7922. (setq iswitchb-temp-buflist choices))))
  7923. (iswitchb-read-buffer prompt)))
  7924. (defun org-icompleting-read (&rest args)
  7925. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7926. (org-without-partial-completion
  7927. (if (and org-completion-use-ido
  7928. (fboundp 'ido-completing-read)
  7929. (boundp 'ido-mode) ido-mode
  7930. (listp (second args)))
  7931. (let ((ido-enter-matching-directory nil))
  7932. (apply 'ido-completing-read (concat (car args))
  7933. (if (consp (car (nth 1 args)))
  7934. (mapcar (lambda (x) (car x)) (nth 1 args))
  7935. (nth 1 args))
  7936. (cddr args)))
  7937. (if (and org-completion-use-iswitchb
  7938. (boundp 'iswitchb-mode) iswitchb-mode
  7939. (listp (second args)))
  7940. (apply 'org-iswitchb-completing-read (concat (car args))
  7941. (if (consp (car (nth 1 args)))
  7942. (mapcar (lambda (x) (car x)) (nth 1 args))
  7943. (nth 1 args))
  7944. (cddr args))
  7945. (apply 'completing-read args)))))
  7946. (defun org-extract-attributes (s)
  7947. "Extract the attributes cookie from a string and set as text property."
  7948. (let (a attr (start 0) key value)
  7949. (save-match-data
  7950. (when (string-match "{{\\([^}]+\\)}}$" s)
  7951. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7952. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7953. (setq key (match-string 1 a) value (match-string 2 a)
  7954. start (match-end 0)
  7955. attr (plist-put attr (intern key) value))))
  7956. (org-add-props s nil 'org-attr attr))
  7957. s))
  7958. (defun org-extract-attributes-from-string (tag)
  7959. (let (key value attr)
  7960. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7961. (setq key (match-string 1 tag) value (match-string 2 tag)
  7962. tag (replace-match "" t t tag)
  7963. attr (plist-put attr (intern key) value)))
  7964. (cons tag attr)))
  7965. (defun org-attributes-to-string (plist)
  7966. "Format a property list into an HTML attribute list."
  7967. (let ((s "") key value)
  7968. (while plist
  7969. (setq key (pop plist) value (pop plist))
  7970. (and value
  7971. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7972. s))
  7973. ;;; Opening/following a link
  7974. (defvar org-link-search-failed nil)
  7975. (defvar org-open-link-functions nil
  7976. "Hook for functions finding a plain text link.
  7977. These functions must take a single argument, the link content.
  7978. They will be called for links that look like [[link text][description]]
  7979. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7980. like a filename (e.g. \"./blue.png\").
  7981. These functions will be called *before* Org attempts to resolve the
  7982. link by doing text searches in the current buffer - so if you want a
  7983. link \"[[target]]\" to still find \"<<target>>\", your function should
  7984. handle this as a special case.
  7985. When the function does handle the link, it must return a non-nil value.
  7986. If it decides that it is not responsible for this link, it must return
  7987. nil to indicate that that Org-mode can continue with other options
  7988. like exact and fuzzy text search.")
  7989. (defun org-next-link ()
  7990. "Move forward to the next link.
  7991. If the link is in hidden text, expose it."
  7992. (interactive)
  7993. (when (and org-link-search-failed (eq this-command last-command))
  7994. (goto-char (point-min))
  7995. (message "Link search wrapped back to beginning of buffer"))
  7996. (setq org-link-search-failed nil)
  7997. (let* ((pos (point))
  7998. (ct (org-context))
  7999. (a (assoc :link ct)))
  8000. (if a (goto-char (nth 2 a)))
  8001. (if (re-search-forward org-any-link-re nil t)
  8002. (progn
  8003. (goto-char (match-beginning 0))
  8004. (if (org-invisible-p) (org-show-context)))
  8005. (goto-char pos)
  8006. (setq org-link-search-failed t)
  8007. (error "No further link found"))))
  8008. (defun org-previous-link ()
  8009. "Move backward to the previous link.
  8010. If the link is in hidden text, expose it."
  8011. (interactive)
  8012. (when (and org-link-search-failed (eq this-command last-command))
  8013. (goto-char (point-max))
  8014. (message "Link search wrapped back to end of buffer"))
  8015. (setq org-link-search-failed nil)
  8016. (let* ((pos (point))
  8017. (ct (org-context))
  8018. (a (assoc :link ct)))
  8019. (if a (goto-char (nth 1 a)))
  8020. (if (re-search-backward org-any-link-re nil t)
  8021. (progn
  8022. (goto-char (match-beginning 0))
  8023. (if (org-invisible-p) (org-show-context)))
  8024. (goto-char pos)
  8025. (setq org-link-search-failed t)
  8026. (error "No further link found"))))
  8027. (defun org-translate-link (s)
  8028. "Translate a link string if a translation function has been defined."
  8029. (if (and org-link-translation-function
  8030. (fboundp org-link-translation-function)
  8031. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8032. (progn
  8033. (setq s (funcall org-link-translation-function
  8034. (match-string 1) (match-string 2)))
  8035. (concat (car s) ":" (cdr s)))
  8036. s))
  8037. (defun org-translate-link-from-planner (type path)
  8038. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8039. This is still an experimental function, your mileage may vary."
  8040. (cond
  8041. ((member type '("http" "https" "news" "ftp"))
  8042. ;; standard Internet links are the same.
  8043. nil)
  8044. ((and (equal type "irc") (string-match "^//" path))
  8045. ;; Planner has two / at the beginning of an irc link, we have 1.
  8046. ;; We should have zero, actually....
  8047. (setq path (substring path 1)))
  8048. ((and (equal type "lisp") (string-match "^/" path))
  8049. ;; Planner has a slash, we do not.
  8050. (setq type "elisp" path (substring path 1)))
  8051. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8052. ;; A typical message link. Planner has the id after the final slash,
  8053. ;; we separate it with a hash mark
  8054. (setq path (concat (match-string 1 path) "#"
  8055. (org-remove-angle-brackets (match-string 2 path)))))
  8056. )
  8057. (cons type path))
  8058. (defun org-find-file-at-mouse (ev)
  8059. "Open file link or URL at mouse."
  8060. (interactive "e")
  8061. (mouse-set-point ev)
  8062. (org-open-at-point 'in-emacs))
  8063. (defun org-open-at-mouse (ev)
  8064. "Open file link or URL at mouse."
  8065. (interactive "e")
  8066. (mouse-set-point ev)
  8067. (if (eq major-mode 'org-agenda-mode)
  8068. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8069. (org-open-at-point))
  8070. (defvar org-window-config-before-follow-link nil
  8071. "The window configuration before following a link.
  8072. This is saved in case the need arises to restore it.")
  8073. (defvar org-open-link-marker (make-marker)
  8074. "Marker pointing to the location where `org-open-at-point; was called.")
  8075. ;;;###autoload
  8076. (defun org-open-at-point-global ()
  8077. "Follow a link like Org-mode does.
  8078. This command can be called in any mode to follow a link that has
  8079. Org-mode syntax."
  8080. (interactive)
  8081. (org-run-like-in-org-mode 'org-open-at-point))
  8082. ;;;###autoload
  8083. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8084. "Open a link in the string S, as if it was in Org-mode."
  8085. (interactive "sLink: \nP")
  8086. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8087. (with-temp-buffer
  8088. (let ((org-inhibit-startup t))
  8089. (org-mode)
  8090. (insert s)
  8091. (goto-char (point-min))
  8092. (when reference-buffer
  8093. (setq org-link-abbrev-alist-local
  8094. (with-current-buffer reference-buffer
  8095. org-link-abbrev-alist-local)))
  8096. (org-open-at-point arg reference-buffer)))))
  8097. (defvar org-open-at-point-functions nil
  8098. "Hook that is run when following a link at point.
  8099. Functions in this hook must return t if they identify and follow
  8100. a link at point. If they don't find anything interesting at point,
  8101. they must return nil.")
  8102. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8103. "Open link at or after point.
  8104. If there is no link at point, this function will search forward up to
  8105. the end of the current line.
  8106. Normally, files will be opened by an appropriate application. If the
  8107. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8108. With a double prefix argument, try to open outside of Emacs, in the
  8109. application the system uses for this file type."
  8110. (interactive "P")
  8111. ;; if in a code block, then open the block's results
  8112. (unless (call-interactively #'org-babel-open-src-block-result)
  8113. (org-load-modules-maybe)
  8114. (move-marker org-open-link-marker (point))
  8115. (setq org-window-config-before-follow-link (current-window-configuration))
  8116. (org-remove-occur-highlights nil nil t)
  8117. (cond
  8118. ((and (org-on-heading-p)
  8119. (not (org-in-regexp
  8120. (concat org-plain-link-re "\\|"
  8121. org-bracket-link-regexp "\\|"
  8122. org-angle-link-re "\\|"
  8123. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8124. (not (get-text-property (point) 'org-linked-text)))
  8125. (or (org-offer-links-in-entry in-emacs)
  8126. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8127. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8128. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8129. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8130. (org-footnote-action))
  8131. (t
  8132. (let (type path link line search (pos (point)))
  8133. (catch 'match
  8134. (save-excursion
  8135. (skip-chars-forward "^]\n\r")
  8136. (when (org-in-regexp org-bracket-link-regexp 1)
  8137. (setq link (org-extract-attributes
  8138. (org-link-unescape (org-match-string-no-properties 1))))
  8139. (while (string-match " *\n *" link)
  8140. (setq link (replace-match " " t t link)))
  8141. (setq link (org-link-expand-abbrev link))
  8142. (cond
  8143. ((or (file-name-absolute-p link)
  8144. (string-match "^\\.\\.?/" link))
  8145. (setq type "file" path link))
  8146. ((string-match org-link-re-with-space3 link)
  8147. (setq type (match-string 1 link) path (match-string 2 link)))
  8148. (t (setq type "thisfile" path link)))
  8149. (throw 'match t)))
  8150. (when (get-text-property (point) 'org-linked-text)
  8151. (setq type "thisfile"
  8152. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8153. (1+ (point)) (point))
  8154. path (buffer-substring
  8155. (previous-single-property-change pos 'org-linked-text)
  8156. (next-single-property-change pos 'org-linked-text)))
  8157. (throw 'match t))
  8158. (save-excursion
  8159. (when (or (org-in-regexp org-angle-link-re)
  8160. (org-in-regexp org-plain-link-re))
  8161. (setq type (match-string 1) path (match-string 2))
  8162. (throw 'match t)))
  8163. (save-excursion
  8164. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  8165. (setq type "tags"
  8166. path (match-string 1))
  8167. (while (string-match ":" path)
  8168. (setq path (replace-match "+" t t path)))
  8169. (throw 'match t)))
  8170. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8171. (setq type "tree-match"
  8172. path (match-string 1))
  8173. (throw 'match t)))
  8174. (unless path
  8175. (error "No link found"))
  8176. ;; switch back to reference buffer
  8177. ;; needed when if called in a temporary buffer through
  8178. ;; org-open-link-from-string
  8179. (with-current-buffer (or reference-buffer (current-buffer))
  8180. ;; Remove any trailing spaces in path
  8181. (if (string-match " +\\'" path)
  8182. (setq path (replace-match "" t t path)))
  8183. (if (and org-link-translation-function
  8184. (fboundp org-link-translation-function))
  8185. ;; Check if we need to translate the link
  8186. (let ((tmp (funcall org-link-translation-function type path)))
  8187. (setq type (car tmp) path (cdr tmp))))
  8188. (cond
  8189. ((assoc type org-link-protocols)
  8190. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8191. ((equal type "mailto")
  8192. (let ((cmd (car org-link-mailto-program))
  8193. (args (cdr org-link-mailto-program)) args1
  8194. (address path) (subject "") a)
  8195. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8196. (setq address (match-string 1 path)
  8197. subject (org-link-escape (match-string 2 path))))
  8198. (while args
  8199. (cond
  8200. ((not (stringp (car args))) (push (pop args) args1))
  8201. (t (setq a (pop args))
  8202. (if (string-match "%a" a)
  8203. (setq a (replace-match address t t a)))
  8204. (if (string-match "%s" a)
  8205. (setq a (replace-match subject t t a)))
  8206. (push a args1))))
  8207. (apply cmd (nreverse args1))))
  8208. ((member type '("http" "https" "ftp" "news"))
  8209. (browse-url (concat type ":" (org-link-escape
  8210. path org-link-escape-chars-browser))))
  8211. ((string= type "doi")
  8212. (browse-url (concat "http://dx.doi.org/"
  8213. (org-link-escape
  8214. path org-link-escape-chars-browser))))
  8215. ((member type '("message"))
  8216. (browse-url (concat type ":" path)))
  8217. ((string= type "tags")
  8218. (org-tags-view in-emacs path))
  8219. ((string= type "tree-match")
  8220. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8221. ((string= type "file")
  8222. (if (string-match "::\\([0-9]+\\)\\'" path)
  8223. (setq line (string-to-number (match-string 1 path))
  8224. path (substring path 0 (match-beginning 0)))
  8225. (if (string-match "::\\(.+\\)\\'" path)
  8226. (setq search (match-string 1 path)
  8227. path (substring path 0 (match-beginning 0)))))
  8228. (if (string-match "[*?{]" (file-name-nondirectory path))
  8229. (dired path)
  8230. (org-open-file path in-emacs line search)))
  8231. ((string= type "news")
  8232. (require 'org-gnus)
  8233. (org-gnus-follow-link path))
  8234. ((string= type "shell")
  8235. (let ((cmd path))
  8236. (if (or (not org-confirm-shell-link-function)
  8237. (funcall org-confirm-shell-link-function
  8238. (format "Execute \"%s\" in shell? "
  8239. (org-add-props cmd nil
  8240. 'face 'org-warning))))
  8241. (progn
  8242. (message "Executing %s" cmd)
  8243. (shell-command cmd))
  8244. (error "Abort"))))
  8245. ((string= type "elisp")
  8246. (let ((cmd path))
  8247. (if (or (not org-confirm-elisp-link-function)
  8248. (funcall org-confirm-elisp-link-function
  8249. (format "Execute \"%s\" as elisp? "
  8250. (org-add-props cmd nil
  8251. 'face 'org-warning))))
  8252. (message "%s => %s" cmd
  8253. (if (equal (string-to-char cmd) ?\()
  8254. (eval (read cmd))
  8255. (call-interactively (read cmd))))
  8256. (error "Abort"))))
  8257. ((and (string= type "thisfile")
  8258. (run-hook-with-args-until-success
  8259. 'org-open-link-functions path)))
  8260. ((string= type "thisfile")
  8261. (if in-emacs
  8262. (switch-to-buffer-other-window
  8263. (org-get-buffer-for-internal-link (current-buffer)))
  8264. (org-mark-ring-push))
  8265. (let ((cmd `(org-link-search
  8266. ,path
  8267. ,(cond ((equal in-emacs '(4)) 'occur)
  8268. ((equal in-emacs '(16)) 'org-occur)
  8269. (t nil))
  8270. ,pos)))
  8271. (condition-case nil (eval cmd)
  8272. (error (progn (widen) (eval cmd))))))
  8273. (t
  8274. (browse-url-at-point)))))))
  8275. (move-marker org-open-link-marker nil)
  8276. (run-hook-with-args 'org-follow-link-hook)))
  8277. (defun org-offer-links-in-entry (&optional nth zero)
  8278. "Offer links in the current entry and follow the selected link.
  8279. If there is only one link, follow it immediately as well.
  8280. If NTH is an integer, immediately pick the NTH link found.
  8281. If ZERO is a string, check also this string for a link, and if
  8282. there is one, offer it as link number zero."
  8283. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8284. "\\(" org-angle-link-re "\\)\\|"
  8285. "\\(" org-plain-link-re "\\)"))
  8286. (cnt ?0)
  8287. (in-emacs (if (integerp nth) nil nth))
  8288. have-zero end links link c)
  8289. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8290. (push (match-string 0 zero) links)
  8291. (setq cnt (1- cnt) have-zero t))
  8292. (save-excursion
  8293. (org-back-to-heading t)
  8294. (setq end (save-excursion (outline-next-heading) (point)))
  8295. (while (re-search-forward re end t)
  8296. (push (match-string 0) links))
  8297. (setq links (org-uniquify (reverse links))))
  8298. (cond
  8299. ((null links)
  8300. (message "No links"))
  8301. ((equal (length links) 1)
  8302. (setq link (list (car links))))
  8303. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8304. (setq link (nth (if have-zero nth (1- nth)) links)))
  8305. (t ; we have to select a link
  8306. (save-excursion
  8307. (save-window-excursion
  8308. (delete-other-windows)
  8309. (with-output-to-temp-buffer "*Select Link*"
  8310. (mapc (lambda (l)
  8311. (if (not (string-match org-bracket-link-regexp l))
  8312. (princ (format "[%c] %s\n" (incf cnt)
  8313. (org-remove-angle-brackets l)))
  8314. (if (match-end 3)
  8315. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8316. (match-string 3 l) (match-string 1 l)))
  8317. (princ (format "[%c] %s\n" (incf cnt)
  8318. (match-string 1 l))))))
  8319. links))
  8320. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8321. (message "Select link to open, RET to open all:")
  8322. (setq c (read-char-exclusive))
  8323. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8324. (when (equal c ?q) (error "Abort"))
  8325. (if (equal c ?\C-m)
  8326. (setq link links)
  8327. (setq nth (- c ?0))
  8328. (if have-zero (setq nth (1+ nth)))
  8329. (unless (and (integerp nth) (>= (length links) nth))
  8330. (error "Invalid link selection"))
  8331. (setq link (list (nth (1- nth) links))))))
  8332. (if link
  8333. (let ((buf (current-buffer)))
  8334. (dolist (l link)
  8335. (org-open-link-from-string l in-emacs buf))
  8336. t)
  8337. nil)))
  8338. ;; Add special file links that specify the way of opening
  8339. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8340. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8341. (defun org-open-file-with-system (path)
  8342. "Open file at PATH using the system way of opening it."
  8343. (org-open-file path 'system))
  8344. (defun org-open-file-with-emacs (path)
  8345. "Open file at PATH in Emacs."
  8346. (org-open-file path 'emacs))
  8347. (defun org-remove-file-link-modifiers ()
  8348. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8349. (goto-char (point-min))
  8350. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8351. (org-if-unprotected
  8352. (replace-match "file:" t t))))
  8353. (eval-after-load "org-exp"
  8354. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8355. 'org-remove-file-link-modifiers))
  8356. ;;;; Time estimates
  8357. (defun org-get-effort (&optional pom)
  8358. "Get the effort estimate for the current entry."
  8359. (org-entry-get pom org-effort-property))
  8360. ;;; File search
  8361. (defvar org-create-file-search-functions nil
  8362. "List of functions to construct the right search string for a file link.
  8363. These functions are called in turn with point at the location to
  8364. which the link should point.
  8365. A function in the hook should first test if it would like to
  8366. handle this file type, for example by checking the `major-mode'
  8367. or the file extension. If it decides not to handle this file, it
  8368. should just return nil to give other functions a chance. If it
  8369. does handle the file, it must return the search string to be used
  8370. when following the link. The search string will be part of the
  8371. file link, given after a double colon, and `org-open-at-point'
  8372. will automatically search for it. If special measures must be
  8373. taken to make the search successful, another function should be
  8374. added to the companion hook `org-execute-file-search-functions',
  8375. which see.
  8376. A function in this hook may also use `setq' to set the variable
  8377. `description' to provide a suggestion for the descriptive text to
  8378. be used for this link when it gets inserted into an Org-mode
  8379. buffer with \\[org-insert-link].")
  8380. (defvar org-execute-file-search-functions nil
  8381. "List of functions to execute a file search triggered by a link.
  8382. Functions added to this hook must accept a single argument, the
  8383. search string that was part of the file link, the part after the
  8384. double colon. The function must first check if it would like to
  8385. handle this search, for example by checking the `major-mode' or
  8386. the file extension. If it decides not to handle this search, it
  8387. should just return nil to give other functions a chance. If it
  8388. does handle the search, it must return a non-nil value to keep
  8389. other functions from trying.
  8390. Each function can access the current prefix argument through the
  8391. variable `current-prefix-argument'. Note that a single prefix is
  8392. used to force opening a link in Emacs, so it may be good to only
  8393. use a numeric or double prefix to guide the search function.
  8394. In case this is needed, a function in this hook can also restore
  8395. the window configuration before `org-open-at-point' was called using:
  8396. (set-window-configuration org-window-config-before-follow-link)")
  8397. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8398. (defun org-link-search (s &optional type avoid-pos)
  8399. "Search for a link search option.
  8400. If S is surrounded by forward slashes, it is interpreted as a
  8401. regular expression. In org-mode files, this will create an `org-occur'
  8402. sparse tree. In ordinary files, `occur' will be used to list matches.
  8403. If the current buffer is in `dired-mode', grep will be used to search
  8404. in all files. If AVOID-POS is given, ignore matches near that position."
  8405. (let ((case-fold-search t)
  8406. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8407. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8408. (append '(("") (" ") ("\t") ("\n"))
  8409. org-emphasis-alist)
  8410. "\\|") "\\)"))
  8411. (pos (point))
  8412. (pre nil) (post nil)
  8413. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8414. (cond
  8415. ;; First check if there are any special search functions
  8416. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8417. ;; Now try the builtin stuff
  8418. ((and (equal (string-to-char s0) ?#)
  8419. (> (length s0) 1)
  8420. (save-excursion
  8421. (goto-char (point-min))
  8422. (and
  8423. (re-search-forward
  8424. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8425. (setq type 'dedicated
  8426. pos (match-beginning 0))))
  8427. ;; There is an exact target for this
  8428. (goto-char pos)
  8429. (org-back-to-heading t)))
  8430. ((save-excursion
  8431. (goto-char (point-min))
  8432. (and
  8433. (re-search-forward
  8434. (concat "<<" (regexp-quote s0) ">>") nil t)
  8435. (setq type 'dedicated
  8436. pos (match-beginning 0))))
  8437. ;; There is an exact target for this
  8438. (goto-char pos))
  8439. ((and (string-match "^(\\(.*\\))$" s0)
  8440. (save-excursion
  8441. (goto-char (point-min))
  8442. (and
  8443. (re-search-forward
  8444. (concat "[^[]" (regexp-quote
  8445. (format org-coderef-label-format
  8446. (match-string 1 s0))))
  8447. nil t)
  8448. (setq type 'dedicated
  8449. pos (1+ (match-beginning 0))))))
  8450. ;; There is a coderef target for this
  8451. (goto-char pos))
  8452. ((string-match "^/\\(.*\\)/$" s)
  8453. ;; A regular expression
  8454. (cond
  8455. ((org-mode-p)
  8456. (org-occur (match-string 1 s)))
  8457. ;;((eq major-mode 'dired-mode)
  8458. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8459. (t (org-do-occur (match-string 1 s)))))
  8460. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8461. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8462. (goto-char (point-min))
  8463. (cond
  8464. ((let (case-fold-search)
  8465. (re-search-forward (format org-complex-heading-regexp-format
  8466. (regexp-quote s))
  8467. nil t))
  8468. ;; OK, found a match
  8469. (goto-char (match-beginning 0)))
  8470. ((and (not org-link-search-inhibit-query)
  8471. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8472. (y-or-n-p "No match - create this as a new heading? "))
  8473. (goto-char (point-max))
  8474. (or (bolp) (newline))
  8475. (insert "* " s "\n")
  8476. (beginning-of-line 0))
  8477. (t
  8478. (goto-char pos)
  8479. (error "No match"))))
  8480. (t
  8481. ;; A normal search string
  8482. (when (equal (string-to-char s) ?*)
  8483. ;; Anchor on headlines, post may include tags.
  8484. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8485. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8486. s (substring s 1)))
  8487. (remove-text-properties
  8488. 0 (length s)
  8489. '(face nil mouse-face nil keymap nil fontified nil) s)
  8490. ;; Make a series of regular expressions to find a match
  8491. (setq words (org-split-string s "[ \n\r\t]+")
  8492. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8493. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8494. "\\)" markers)
  8495. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8496. re2a (concat "[ \t\r\n]" re2a_)
  8497. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8498. re4 (concat "[^a-zA-Z_]" re4_)
  8499. re1 (concat pre re2 post)
  8500. re3 (concat pre (if pre re4_ re4) post)
  8501. re5 (concat pre ".*" re4)
  8502. re2 (concat pre re2)
  8503. re2a (concat pre (if pre re2a_ re2a))
  8504. re4 (concat pre (if pre re4_ re4))
  8505. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8506. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8507. re5 "\\)"
  8508. ))
  8509. (cond
  8510. ((eq type 'org-occur) (org-occur reall))
  8511. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8512. (t (goto-char (point-min))
  8513. (setq type 'fuzzy)
  8514. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8515. (org-search-not-self 1 re1 nil t)
  8516. (org-search-not-self 1 re2 nil t)
  8517. (org-search-not-self 1 re2a nil t)
  8518. (org-search-not-self 1 re3 nil t)
  8519. (org-search-not-self 1 re4 nil t)
  8520. (org-search-not-self 1 re5 nil t)
  8521. )
  8522. (goto-char (match-beginning 1))
  8523. (goto-char pos)
  8524. (error "No match"))))))
  8525. (and (org-mode-p) (org-show-context 'link-search))
  8526. type))
  8527. (defun org-search-not-self (group &rest args)
  8528. "Execute `re-search-forward', but only accept matches that do not
  8529. enclose the position of `org-open-link-marker'."
  8530. (let ((m org-open-link-marker))
  8531. (catch 'exit
  8532. (while (apply 're-search-forward args)
  8533. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8534. (goto-char (match-end group))
  8535. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8536. (> (match-beginning 0) (marker-position m))
  8537. (< (match-end 0) (marker-position m)))
  8538. (save-match-data
  8539. (or (not (org-in-regexp
  8540. org-bracket-link-analytic-regexp 1))
  8541. (not (match-end 4)) ; no description
  8542. (and (<= (match-beginning 4) (point))
  8543. (>= (match-end 4) (point))))))
  8544. (throw 'exit (point))))))))
  8545. (defun org-get-buffer-for-internal-link (buffer)
  8546. "Return a buffer to be used for displaying the link target of internal links."
  8547. (cond
  8548. ((not org-display-internal-link-with-indirect-buffer)
  8549. buffer)
  8550. ((string-match "(Clone)$" (buffer-name buffer))
  8551. (message "Buffer is already a clone, not making another one")
  8552. ;; we also do not modify visibility in this case
  8553. buffer)
  8554. (t ; make a new indirect buffer for displaying the link
  8555. (let* ((bn (buffer-name buffer))
  8556. (ibn (concat bn "(Clone)"))
  8557. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8558. (with-current-buffer ib (org-overview))
  8559. ib))))
  8560. (defun org-do-occur (regexp &optional cleanup)
  8561. "Call the Emacs command `occur'.
  8562. If CLEANUP is non-nil, remove the printout of the regular expression
  8563. in the *Occur* buffer. This is useful if the regex is long and not useful
  8564. to read."
  8565. (occur regexp)
  8566. (when cleanup
  8567. (let ((cwin (selected-window)) win beg end)
  8568. (when (setq win (get-buffer-window "*Occur*"))
  8569. (select-window win))
  8570. (goto-char (point-min))
  8571. (when (re-search-forward "match[a-z]+" nil t)
  8572. (setq beg (match-end 0))
  8573. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8574. (setq end (1- (match-beginning 0)))))
  8575. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8576. (goto-char (point-min))
  8577. (select-window cwin))))
  8578. ;;; The mark ring for links jumps
  8579. (defvar org-mark-ring nil
  8580. "Mark ring for positions before jumps in Org-mode.")
  8581. (defvar org-mark-ring-last-goto nil
  8582. "Last position in the mark ring used to go back.")
  8583. ;; Fill and close the ring
  8584. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8585. (loop for i from 1 to org-mark-ring-length do
  8586. (push (make-marker) org-mark-ring))
  8587. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8588. org-mark-ring)
  8589. (defun org-mark-ring-push (&optional pos buffer)
  8590. "Put the current position or POS into the mark ring and rotate it."
  8591. (interactive)
  8592. (setq pos (or pos (point)))
  8593. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8594. (move-marker (car org-mark-ring)
  8595. (or pos (point))
  8596. (or buffer (current-buffer)))
  8597. (message "%s"
  8598. (substitute-command-keys
  8599. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8600. (defun org-mark-ring-goto (&optional n)
  8601. "Jump to the previous position in the mark ring.
  8602. With prefix arg N, jump back that many stored positions. When
  8603. called several times in succession, walk through the entire ring.
  8604. Org-mode commands jumping to a different position in the current file,
  8605. or to another Org-mode file, automatically push the old position
  8606. onto the ring."
  8607. (interactive "p")
  8608. (let (p m)
  8609. (if (eq last-command this-command)
  8610. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8611. (setq p org-mark-ring))
  8612. (setq org-mark-ring-last-goto p)
  8613. (setq m (car p))
  8614. (switch-to-buffer (marker-buffer m))
  8615. (goto-char m)
  8616. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8617. (defun org-remove-angle-brackets (s)
  8618. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8619. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8620. s)
  8621. (defun org-add-angle-brackets (s)
  8622. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8623. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8624. s)
  8625. (defun org-remove-double-quotes (s)
  8626. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8627. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8628. s)
  8629. ;;; Following specific links
  8630. (defun org-follow-timestamp-link ()
  8631. (cond
  8632. ((org-at-date-range-p t)
  8633. (let ((org-agenda-start-on-weekday)
  8634. (t1 (match-string 1))
  8635. (t2 (match-string 2)))
  8636. (setq t1 (time-to-days (org-time-string-to-time t1))
  8637. t2 (time-to-days (org-time-string-to-time t2)))
  8638. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8639. ((org-at-timestamp-p t)
  8640. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8641. (substring (match-string 1) 0 10)))
  8642. 1))
  8643. (t (error "This should not happen"))))
  8644. ;;; Following file links
  8645. (defvar org-wait nil)
  8646. (defun org-open-file (path &optional in-emacs line search)
  8647. "Open the file at PATH.
  8648. First, this expands any special file name abbreviations. Then the
  8649. configuration variable `org-file-apps' is checked if it contains an
  8650. entry for this file type, and if yes, the corresponding command is launched.
  8651. If no application is found, Emacs simply visits the file.
  8652. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8653. With a double \\[universal-argument] \\[universal-argument] \
  8654. prefix arg, Org tries to avoid opening in Emacs
  8655. and to use an external application to visit the file.
  8656. Optional LINE specifies a line to go to, optional SEARCH a string
  8657. to search for. If LINE or SEARCH is given, the file will be
  8658. opened in Emacs, unless an entry from org-file-apps that makes
  8659. use of groups in a regexp matches.
  8660. If the file does not exist, an error is thrown."
  8661. (let* ((file (if (equal path "")
  8662. buffer-file-name
  8663. (substitute-in-file-name (expand-file-name path))))
  8664. (file-apps (append org-file-apps (org-default-apps)))
  8665. (apps (org-remove-if
  8666. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8667. (apps-dlink (org-remove-if-not
  8668. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8669. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8670. (dirp (if remp nil (file-directory-p file)))
  8671. (file (if (and dirp org-open-directory-means-index-dot-org)
  8672. (concat (file-name-as-directory file) "index.org")
  8673. file))
  8674. (a-m-a-p (assq 'auto-mode apps))
  8675. (dfile (downcase file))
  8676. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8677. (link (cond ((and (eq line nil)
  8678. (eq search nil))
  8679. file)
  8680. (line
  8681. (concat file "::" (number-to-string line)))
  8682. (search
  8683. (concat file "::" search))))
  8684. (dlink (downcase link))
  8685. (old-buffer (current-buffer))
  8686. (old-pos (point))
  8687. (old-mode major-mode)
  8688. ext cmd link-match-data)
  8689. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8690. (setq ext (match-string 1 dfile))
  8691. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8692. (setq ext (match-string 1 dfile))))
  8693. (cond
  8694. ((member in-emacs '((16) system))
  8695. (setq cmd (cdr (assoc 'system apps))))
  8696. (in-emacs (setq cmd 'emacs))
  8697. (t
  8698. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8699. (and dirp (cdr (assoc 'directory apps)))
  8700. ; first, try matching against apps-dlink
  8701. ; if we get a match here, store the match data for later
  8702. (let ((match (assoc-default dlink apps-dlink
  8703. 'string-match)))
  8704. (if match
  8705. (progn (setq link-match-data (match-data))
  8706. match)
  8707. (progn (setq in-emacs (or in-emacs line search))
  8708. nil))) ; if we have no match in apps-dlink,
  8709. ; always open the file in emacs if line or search
  8710. ; is given (for backwards compatibility)
  8711. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8712. 'string-match)
  8713. (cdr (assoc ext apps))
  8714. (cdr (assoc t apps))))))
  8715. (when (eq cmd 'system)
  8716. (setq cmd (cdr (assoc 'system apps))))
  8717. (when (eq cmd 'default)
  8718. (setq cmd (cdr (assoc t apps))))
  8719. (when (eq cmd 'mailcap)
  8720. (require 'mailcap)
  8721. (mailcap-parse-mailcaps)
  8722. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8723. (command (mailcap-mime-info mime-type)))
  8724. (if (stringp command)
  8725. (setq cmd command)
  8726. (setq cmd 'emacs))))
  8727. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8728. (not (file-exists-p file))
  8729. (not org-open-non-existing-files))
  8730. (error "No such file: %s" file))
  8731. (cond
  8732. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8733. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8734. (while (string-match "['\"]%s['\"]" cmd)
  8735. (setq cmd (replace-match "%s" t t cmd)))
  8736. (while (string-match "%s" cmd)
  8737. (setq cmd (replace-match
  8738. (save-match-data
  8739. (shell-quote-argument
  8740. (convert-standard-filename file)))
  8741. t t cmd)))
  8742. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8743. (save-match-data
  8744. (let ((match-index 1)
  8745. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8746. (set-match-data link-match-data)
  8747. (while (<= match-index number-of-groups)
  8748. (let ((regex (concat "%" (number-to-string match-index)))
  8749. (replace-with (match-string match-index dlink)))
  8750. (while (string-match regex cmd)
  8751. (setq cmd (replace-match replace-with t t cmd))))
  8752. (setq match-index (+ match-index 1)))))
  8753. (save-window-excursion
  8754. (start-process-shell-command cmd nil cmd)
  8755. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8756. ))
  8757. ((or (stringp cmd)
  8758. (eq cmd 'emacs))
  8759. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8760. (widen)
  8761. (if line (org-goto-line line)
  8762. (if search (org-link-search search))))
  8763. ((consp cmd)
  8764. (let ((file (convert-standard-filename file)))
  8765. (save-match-data
  8766. (set-match-data link-match-data)
  8767. (eval cmd))))
  8768. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8769. (and (org-mode-p) (eq old-mode 'org-mode)
  8770. (or (not (equal old-buffer (current-buffer)))
  8771. (not (equal old-pos (point))))
  8772. (org-mark-ring-push old-pos old-buffer))))
  8773. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8774. "This function returns non-nil if `entry' uses a regular
  8775. expression which should be matched against the whole link by
  8776. org-open-file.
  8777. It assumes that is the case when the entry uses a regular
  8778. expression which has at least one grouping construct and the
  8779. action is either a lisp form or a command string containing
  8780. '%1', i.e. using at least one subexpression match as a
  8781. parameter."
  8782. (let ((selector (car entry))
  8783. (action (cdr entry)))
  8784. (if (stringp selector)
  8785. (and (> (regexp-opt-depth selector) 0)
  8786. (or (and (stringp action)
  8787. (string-match "%[0-9]" action))
  8788. (consp action)))
  8789. nil)))
  8790. (defun org-default-apps ()
  8791. "Return the default applications for this operating system."
  8792. (cond
  8793. ((eq system-type 'darwin)
  8794. org-file-apps-defaults-macosx)
  8795. ((eq system-type 'windows-nt)
  8796. org-file-apps-defaults-windowsnt)
  8797. (t org-file-apps-defaults-gnu)))
  8798. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8799. "Convert extensions to regular expressions in the cars of LIST.
  8800. Also, weed out any non-string entries, because the return value is used
  8801. only for regexp matching.
  8802. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8803. point to the symbol `emacs', indicating that the file should
  8804. be opened in Emacs."
  8805. (append
  8806. (delq nil
  8807. (mapcar (lambda (x)
  8808. (if (not (stringp (car x)))
  8809. nil
  8810. (if (string-match "\\W" (car x))
  8811. x
  8812. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8813. list))
  8814. (if add-auto-mode
  8815. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8816. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8817. (defun org-file-remote-p (file)
  8818. "Test whether FILE specifies a location on a remote system.
  8819. Return non-nil if the location is indeed remote.
  8820. For example, the filename \"/user@host:/foo\" specifies a location
  8821. on the system \"/user@host:\"."
  8822. (cond ((fboundp 'file-remote-p)
  8823. (file-remote-p file))
  8824. ((fboundp 'tramp-handle-file-remote-p)
  8825. (tramp-handle-file-remote-p file))
  8826. ((and (boundp 'ange-ftp-name-format)
  8827. (string-match (car ange-ftp-name-format) file))
  8828. t)
  8829. (t nil)))
  8830. ;;;; Refiling
  8831. (defun org-get-org-file ()
  8832. "Read a filename, with default directory `org-directory'."
  8833. (let ((default (or org-default-notes-file remember-data-file)))
  8834. (read-file-name (format "File name [%s]: " default)
  8835. (file-name-as-directory org-directory)
  8836. default)))
  8837. (defun org-notes-order-reversed-p ()
  8838. "Check if the current file should receive notes in reversed order."
  8839. (cond
  8840. ((not org-reverse-note-order) nil)
  8841. ((eq t org-reverse-note-order) t)
  8842. ((not (listp org-reverse-note-order)) nil)
  8843. (t (catch 'exit
  8844. (let ((all org-reverse-note-order)
  8845. entry)
  8846. (while (setq entry (pop all))
  8847. (if (string-match (car entry) buffer-file-name)
  8848. (throw 'exit (cdr entry))))
  8849. nil)))))
  8850. (defvar org-refile-target-table nil
  8851. "The list of refile targets, created by `org-refile'.")
  8852. (defvar org-agenda-new-buffers nil
  8853. "Buffers created to visit agenda files.")
  8854. (defvar org-refile-cache nil
  8855. "Cache for refile targets.")
  8856. (defvar org-refile-markers nil
  8857. "All the markers used for caching refile locations.")
  8858. (defun org-refile-marker (pos)
  8859. "Get a new refile marker, but only if caching is in use."
  8860. (if (not org-refile-use-cache)
  8861. pos
  8862. (let ((m (make-marker)))
  8863. (move-marker m pos)
  8864. (push m org-refile-markers)
  8865. m)))
  8866. (defun org-refile-cache-clear ()
  8867. "Clear the refile cache and disable all the markers."
  8868. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8869. (setq org-refile-markers nil)
  8870. (setq org-refile-cache nil)
  8871. (message "Refile cache has been cleared"))
  8872. (defun org-refile-cache-check-set (set)
  8873. "Check if all the markers in the cache still have live buffers."
  8874. (let (marker)
  8875. (catch 'exit
  8876. (while (and set (setq marker (nth 3 (pop set))))
  8877. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8878. (when (and marker (null (marker-buffer marker)))
  8879. (message "not found") (sit-for 3)
  8880. (throw 'exit nil)))
  8881. t)))
  8882. (defun org-refile-cache-put (set &rest identifiers)
  8883. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8884. (let* ((key (sha1 (prin1-to-string identifiers)))
  8885. (entry (assoc key org-refile-cache)))
  8886. (if entry
  8887. (setcdr entry set)
  8888. (push (cons key set) org-refile-cache))))
  8889. (defun org-refile-cache-get (&rest identifiers)
  8890. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8891. (cond
  8892. ((not org-refile-cache) nil)
  8893. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8894. (t
  8895. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8896. org-refile-cache))))
  8897. (and set (org-refile-cache-check-set set) set)))))
  8898. (defun org-get-refile-targets (&optional default-buffer)
  8899. "Produce a table with refile targets."
  8900. (let ((case-fold-search nil)
  8901. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8902. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8903. targets tgs txt re files f desc descre fast-path-p level pos0)
  8904. (message "Getting targets...")
  8905. (with-current-buffer (or default-buffer (current-buffer))
  8906. (while (setq entry (pop entries))
  8907. (setq files (car entry) desc (cdr entry))
  8908. (setq fast-path-p nil)
  8909. (cond
  8910. ((null files) (setq files (list (current-buffer))))
  8911. ((eq files 'org-agenda-files)
  8912. (setq files (org-agenda-files 'unrestricted)))
  8913. ((and (symbolp files) (fboundp files))
  8914. (setq files (funcall files)))
  8915. ((and (symbolp files) (boundp files))
  8916. (setq files (symbol-value files))))
  8917. (if (stringp files) (setq files (list files)))
  8918. (cond
  8919. ((eq (car desc) :tag)
  8920. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8921. ((eq (car desc) :todo)
  8922. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8923. ((eq (car desc) :regexp)
  8924. (setq descre (cdr desc)))
  8925. ((eq (car desc) :level)
  8926. (setq descre (concat "^\\*\\{" (number-to-string
  8927. (if org-odd-levels-only
  8928. (1- (* 2 (cdr desc)))
  8929. (cdr desc)))
  8930. "\\}[ \t]")))
  8931. ((eq (car desc) :maxlevel)
  8932. (setq fast-path-p t)
  8933. (setq descre (concat "^\\*\\{1," (number-to-string
  8934. (if org-odd-levels-only
  8935. (1- (* 2 (cdr desc)))
  8936. (cdr desc)))
  8937. "\\}[ \t]")))
  8938. (t (error "Bad refiling target description %s" desc)))
  8939. (while (setq f (pop files))
  8940. (with-current-buffer
  8941. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8942. (or
  8943. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8944. (progn
  8945. (if (bufferp f) (setq f (buffer-file-name
  8946. (buffer-base-buffer f))))
  8947. (setq f (and f (expand-file-name f)))
  8948. (if (eq org-refile-use-outline-path 'file)
  8949. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8950. (save-excursion
  8951. (save-restriction
  8952. (widen)
  8953. (goto-char (point-min))
  8954. (while (re-search-forward descre nil t)
  8955. (goto-char (setq pos0 (point-at-bol)))
  8956. (catch 'next
  8957. (when org-refile-target-verify-function
  8958. (save-match-data
  8959. (or (funcall org-refile-target-verify-function)
  8960. (throw 'next t))))
  8961. (when (looking-at org-complex-heading-regexp)
  8962. (setq level (org-reduced-level
  8963. (- (match-end 1) (match-beginning 1)))
  8964. txt (org-link-display-format (match-string 4))
  8965. re (format org-complex-heading-regexp-format
  8966. (regexp-quote (match-string 4))))
  8967. (when org-refile-use-outline-path
  8968. (setq txt (mapconcat
  8969. 'org-protect-slash
  8970. (append
  8971. (if (eq org-refile-use-outline-path
  8972. 'file)
  8973. (list (file-name-nondirectory
  8974. (buffer-file-name
  8975. (buffer-base-buffer))))
  8976. (if (eq org-refile-use-outline-path
  8977. 'full-file-path)
  8978. (list (buffer-file-name
  8979. (buffer-base-buffer)))))
  8980. (org-get-outline-path fast-path-p
  8981. level txt)
  8982. (list txt))
  8983. "/")))
  8984. (push (list txt f re (org-refile-marker (point)))
  8985. tgs)))
  8986. (when (= (point) pos0)
  8987. ;; verification function has not moved point
  8988. (goto-char (point-at-eol))))))))
  8989. (when org-refile-use-cache
  8990. (org-refile-cache-put tgs (buffer-file-name) descre))
  8991. (setq targets (append tgs targets))
  8992. ))))
  8993. (message "Getting targets...done")
  8994. (nreverse targets)))
  8995. (defun org-protect-slash (s)
  8996. (while (string-match "/" s)
  8997. (setq s (replace-match "\\" t t s)))
  8998. s)
  8999. (defvar org-olpa (make-vector 20 nil))
  9000. (defun org-get-outline-path (&optional fastp level heading)
  9001. "Return the outline path to the current entry, as a list.
  9002. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9003. routine which makes outline path derivations for an entire file,
  9004. avoiding backtracing. Refile target collection makes use of that."
  9005. (if fastp
  9006. (progn
  9007. (if (> level 19)
  9008. (error "Outline path failure, more than 19 levels"))
  9009. (loop for i from level upto 19 do
  9010. (aset org-olpa i nil))
  9011. (prog1
  9012. (delq nil (append org-olpa nil))
  9013. (aset org-olpa level heading)))
  9014. (let (rtn case-fold-search)
  9015. (save-excursion
  9016. (save-restriction
  9017. (widen)
  9018. (while (org-up-heading-safe)
  9019. (when (looking-at org-complex-heading-regexp)
  9020. (push (org-match-string-no-properties 4) rtn)))
  9021. rtn)))))
  9022. (defun org-format-outline-path (path &optional width prefix)
  9023. "Format the outline path PATH for display.
  9024. Width is the maximum number of characters that is available.
  9025. Prefix is a prefix to be included in the returned string,
  9026. such as the file name."
  9027. (setq width (or width 79))
  9028. (if prefix (setq width (- width (length prefix))))
  9029. (if (not path)
  9030. (or prefix "")
  9031. (let* ((nsteps (length path))
  9032. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9033. (maxwidth (if (<= total-width width)
  9034. 10000 ;; everything fits
  9035. ;; we need to shorten the level headings
  9036. (/ (- width nsteps) nsteps)))
  9037. (org-odd-levels-only nil)
  9038. (n 0)
  9039. (total (1+ (length prefix))))
  9040. (setq maxwidth (max maxwidth 10))
  9041. (concat prefix
  9042. (mapconcat
  9043. (lambda (h)
  9044. (setq n (1+ n))
  9045. (if (and (= n nsteps) (< maxwidth 10000))
  9046. (setq maxwidth (- total-width total)))
  9047. (if (< (length h) maxwidth)
  9048. (progn (setq total (+ total (length h) 1)) h)
  9049. (setq h (substring h 0 (- maxwidth 2))
  9050. total (+ total maxwidth 1))
  9051. (if (string-match "[ \t]+\\'" h)
  9052. (setq h (substring h 0 (match-beginning 0))))
  9053. (setq h (concat h "..")))
  9054. (org-add-props h nil 'face
  9055. (nth (% (1- n) org-n-level-faces)
  9056. org-level-faces))
  9057. h)
  9058. path "/")))))
  9059. (defun org-display-outline-path (&optional file current)
  9060. "Display the current outline path in the echo area."
  9061. (interactive "P")
  9062. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9063. (case-fold-search nil)
  9064. (path (and (org-mode-p) (org-get-outline-path))))
  9065. (if current (setq path (append path
  9066. (save-excursion
  9067. (org-back-to-heading t)
  9068. (if (looking-at org-complex-heading-regexp)
  9069. (list (match-string 4)))))))
  9070. (message "%s"
  9071. (org-format-outline-path
  9072. path
  9073. (1- (frame-width))
  9074. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9075. (defvar org-refile-history nil
  9076. "History for refiling operations.")
  9077. (defvar org-after-refile-insert-hook nil
  9078. "Hook run after `org-refile' has inserted its stuff at the new location.
  9079. Note that this is still *before* the stuff will be removed from
  9080. the *old* location.")
  9081. (defvar org-capture-last-stored-marker)
  9082. (defun org-refile (&optional goto default-buffer rfloc)
  9083. "Move the entry at point to another heading.
  9084. The list of target headings is compiled using the information in
  9085. `org-refile-targets', which see. This list is created before each use
  9086. and will therefore always be up-to-date.
  9087. At the target location, the entry is filed as a subitem of the target heading.
  9088. Depending on `org-reverse-note-order', the new subitem will either be the
  9089. first or the last subitem.
  9090. If there is an active region, all entries in that region will be moved.
  9091. However, the region must fulfill the requirement that the first heading
  9092. is the first one sets the top-level of the moved text - at most siblings
  9093. below it are allowed.
  9094. With prefix arg GOTO, the command will only visit the target location,
  9095. not actually move anything.
  9096. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9097. go to the location where the last refiling
  9098. operation has put the subtree.
  9099. With a prefix argument of `2', refile to the running clock.
  9100. RFLOC can be a refile location obtained in a different way.
  9101. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9102. If you are using target caching (see `org-refile-use-cache'),
  9103. You have to clear the target cache in order to find new targets.
  9104. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9105. (interactive "P")
  9106. (if (member goto '(0 (64)))
  9107. (org-refile-cache-clear)
  9108. (let* ((cbuf (current-buffer))
  9109. (regionp (org-region-active-p))
  9110. (region-start (and regionp (region-beginning)))
  9111. (region-end (and regionp (region-end)))
  9112. (region-length (and regionp (- region-end region-start)))
  9113. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9114. pos it nbuf file re level reversed)
  9115. (setq last-command nil)
  9116. (when regionp
  9117. (goto-char region-start)
  9118. (or (bolp) (goto-char (point-at-bol)))
  9119. (setq region-start (point))
  9120. (unless (org-kill-is-subtree-p
  9121. (buffer-substring region-start region-end))
  9122. (error "The region is not a (sequence of) subtree(s)")))
  9123. (if (equal goto '(16))
  9124. (org-refile-goto-last-stored)
  9125. (when (or
  9126. (and (equal goto 2)
  9127. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9128. (prog1
  9129. (setq it (list (or org-clock-heading "running clock")
  9130. (buffer-file-name
  9131. (marker-buffer org-clock-hd-marker))
  9132. ""
  9133. (marker-position org-clock-hd-marker)))
  9134. (setq goto nil)))
  9135. (setq it (or rfloc
  9136. (save-excursion
  9137. (org-refile-get-location
  9138. (if goto "Goto: " "Refile to: ") default-buffer
  9139. org-refile-allow-creating-parent-nodes)))))
  9140. (setq file (nth 1 it)
  9141. re (nth 2 it)
  9142. pos (nth 3 it))
  9143. (if (and (not goto)
  9144. pos
  9145. (equal (buffer-file-name) file)
  9146. (if regionp
  9147. (and (>= pos region-start)
  9148. (<= pos region-end))
  9149. (and (>= pos (point))
  9150. (< pos (save-excursion
  9151. (org-end-of-subtree t t))))))
  9152. (error "Cannot refile to position inside the tree or region"))
  9153. (setq nbuf (or (find-buffer-visiting file)
  9154. (find-file-noselect file)))
  9155. (if goto
  9156. (progn
  9157. (switch-to-buffer nbuf)
  9158. (goto-char pos)
  9159. (org-show-context 'org-goto))
  9160. (if regionp
  9161. (progn
  9162. (org-kill-new (buffer-substring region-start region-end))
  9163. (org-save-markers-in-region region-start region-end))
  9164. (org-copy-subtree 1 nil t))
  9165. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9166. (find-file-noselect file)))
  9167. (setq reversed (org-notes-order-reversed-p))
  9168. (save-excursion
  9169. (save-restriction
  9170. (widen)
  9171. (if pos
  9172. (progn
  9173. (goto-char pos)
  9174. (looking-at outline-regexp)
  9175. (setq level (org-get-valid-level (funcall outline-level) 1))
  9176. (goto-char
  9177. (if reversed
  9178. (or (outline-next-heading) (point-max))
  9179. (or (save-excursion (org-get-next-sibling))
  9180. (org-end-of-subtree t t)
  9181. (point-max)))))
  9182. (setq level 1)
  9183. (if (not reversed)
  9184. (goto-char (point-max))
  9185. (goto-char (point-min))
  9186. (or (outline-next-heading) (goto-char (point-max)))))
  9187. (if (not (bolp)) (newline))
  9188. (org-paste-subtree level)
  9189. (when org-log-refile
  9190. (org-add-log-setup 'refile nil nil 'findpos
  9191. org-log-refile)
  9192. (unless (eq org-log-refile 'note)
  9193. (save-excursion (org-add-log-note))))
  9194. (and org-auto-align-tags (org-set-tags nil t))
  9195. (bookmark-set "org-refile-last-stored")
  9196. ;; If we are refiling for capture, make sure that the
  9197. ;; last-capture pointers point here
  9198. (when (org-bound-and-true-p org-refile-for-capture)
  9199. (bookmark-set "org-capture-last-stored-marker")
  9200. (move-marker org-capture-last-stored-marker (point)))
  9201. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9202. (run-hooks 'org-after-refile-insert-hook))))
  9203. (if regionp
  9204. (delete-region (point) (+ (point) region-length))
  9205. (org-cut-subtree))
  9206. (when (featurep 'org-inlinetask)
  9207. (org-inlinetask-remove-END-maybe))
  9208. (setq org-markers-to-move nil)
  9209. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9210. (defun org-refile-goto-last-stored ()
  9211. "Go to the location where the last refile was stored."
  9212. (interactive)
  9213. (bookmark-jump "org-refile-last-stored")
  9214. (message "This is the location of the last refile"))
  9215. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9216. "Prompt the user for a refile location, using PROMPT."
  9217. (let ((org-refile-targets org-refile-targets)
  9218. (org-refile-use-outline-path org-refile-use-outline-path))
  9219. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9220. (unless org-refile-target-table
  9221. (error "No refile targets"))
  9222. (let* ((cbuf (current-buffer))
  9223. (partial-completion-mode nil)
  9224. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9225. (cfunc (if (and org-refile-use-outline-path
  9226. org-outline-path-complete-in-steps)
  9227. 'org-olpath-completing-read
  9228. 'org-icompleting-read))
  9229. (extra (if org-refile-use-outline-path "/" ""))
  9230. (filename (and cfn (expand-file-name cfn)))
  9231. (tbl (mapcar
  9232. (lambda (x)
  9233. (if (and (not (member org-refile-use-outline-path
  9234. '(file full-file-path)))
  9235. (not (equal filename (nth 1 x))))
  9236. (cons (concat (car x) extra " ("
  9237. (file-name-nondirectory (nth 1 x)) ")")
  9238. (cdr x))
  9239. (cons (concat (car x) extra) (cdr x))))
  9240. org-refile-target-table))
  9241. (completion-ignore-case t)
  9242. pa answ parent-target child parent old-hist)
  9243. (setq old-hist org-refile-history)
  9244. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9245. nil 'org-refile-history))
  9246. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9247. (org-refile-check-position pa)
  9248. (if pa
  9249. (progn
  9250. (when (or (not org-refile-history)
  9251. (not (eq old-hist org-refile-history))
  9252. (not (equal (car pa) (car org-refile-history))))
  9253. (setq org-refile-history
  9254. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9255. org-refile-history
  9256. (cdr org-refile-history))))
  9257. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9258. (pop org-refile-history)))
  9259. pa)
  9260. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9261. (progn
  9262. (setq parent (match-string 1 answ)
  9263. child (match-string 2 answ))
  9264. (setq parent-target (or (assoc parent tbl)
  9265. (assoc (concat parent "/") tbl)))
  9266. (when (and parent-target
  9267. (or (eq new-nodes t)
  9268. (and (eq new-nodes 'confirm)
  9269. (y-or-n-p (format "Create new node \"%s\"? "
  9270. child)))))
  9271. (org-refile-new-child parent-target child)))
  9272. (error "Invalid target location")))))
  9273. (defun org-refile-check-position (refile-pointer)
  9274. "Check if the refile pointer matches the readline to which it points."
  9275. (let* ((file (nth 1 refile-pointer))
  9276. (re (nth 2 refile-pointer))
  9277. (pos (nth 3 refile-pointer))
  9278. buffer)
  9279. (when (org-string-nw-p re)
  9280. (setq buffer (if (markerp pos)
  9281. (marker-buffer pos)
  9282. (or (find-buffer-visiting file)
  9283. (find-file-noselect file))))
  9284. (with-current-buffer buffer
  9285. (save-excursion
  9286. (save-restriction
  9287. (widen)
  9288. (goto-char pos)
  9289. (beginning-of-line 1)
  9290. (unless (org-looking-at-p re)
  9291. (error "Invalid refile position, please rebuild the cache"))))))))
  9292. (defun org-refile-new-child (parent-target child)
  9293. "Use refile target PARENT-TARGET to add new CHILD below it."
  9294. (unless parent-target
  9295. (error "Cannot find parent for new node"))
  9296. (let ((file (nth 1 parent-target))
  9297. (pos (nth 3 parent-target))
  9298. level)
  9299. (with-current-buffer (or (find-buffer-visiting file)
  9300. (find-file-noselect file))
  9301. (save-excursion
  9302. (save-restriction
  9303. (widen)
  9304. (if pos
  9305. (goto-char pos)
  9306. (goto-char (point-max))
  9307. (if (not (bolp)) (newline)))
  9308. (when (looking-at outline-regexp)
  9309. (setq level (funcall outline-level))
  9310. (org-end-of-subtree t t))
  9311. (org-back-over-empty-lines)
  9312. (insert "\n" (make-string
  9313. (if pos (org-get-valid-level level 1) 1) ?*)
  9314. " " child "\n")
  9315. (beginning-of-line 0)
  9316. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9317. (defun org-olpath-completing-read (prompt collection &rest args)
  9318. "Read an outline path like a file name."
  9319. (let ((thetable collection)
  9320. (org-completion-use-ido nil) ; does not work with ido.
  9321. (org-completion-use-iswitchb nil)) ; or iswitchb
  9322. (apply
  9323. 'org-icompleting-read prompt
  9324. (lambda (string predicate &optional flag)
  9325. (let (rtn r f (l (length string)))
  9326. (cond
  9327. ((eq flag nil)
  9328. ;; try completion
  9329. (try-completion string thetable))
  9330. ((eq flag t)
  9331. ;; all-completions
  9332. (setq rtn (all-completions string thetable predicate))
  9333. (mapcar
  9334. (lambda (x)
  9335. (setq r (substring x l))
  9336. (if (string-match " ([^)]*)$" x)
  9337. (setq f (match-string 0 x))
  9338. (setq f ""))
  9339. (if (string-match "/" r)
  9340. (concat string (substring r 0 (match-end 0)) f)
  9341. x))
  9342. rtn))
  9343. ((eq flag 'lambda)
  9344. ;; exact match?
  9345. (assoc string thetable)))
  9346. ))
  9347. args)))
  9348. ;;;; Dynamic blocks
  9349. (defun org-find-dblock (name)
  9350. "Find the first dynamic block with name NAME in the buffer.
  9351. If not found, stay at current position and return nil."
  9352. (let (pos)
  9353. (save-excursion
  9354. (goto-char (point-min))
  9355. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9356. nil t)
  9357. (match-beginning 0))))
  9358. (if pos (goto-char pos))
  9359. pos))
  9360. (defconst org-dblock-start-re
  9361. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9362. "Matches the start line of a dynamic block, with parameters.")
  9363. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9364. "Matches the end of a dynamic block.")
  9365. (defun org-create-dblock (plist)
  9366. "Create a dynamic block section, with parameters taken from PLIST.
  9367. PLIST must contain a :name entry which is used as name of the block."
  9368. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9369. (end-of-line 1)
  9370. (newline))
  9371. (let ((col (current-column))
  9372. (name (plist-get plist :name)))
  9373. (insert "#+BEGIN: " name)
  9374. (while plist
  9375. (if (eq (car plist) :name)
  9376. (setq plist (cddr plist))
  9377. (insert " " (prin1-to-string (pop plist)))))
  9378. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9379. (beginning-of-line -2)))
  9380. (defun org-prepare-dblock ()
  9381. "Prepare dynamic block for refresh.
  9382. This empties the block, puts the cursor at the insert position and returns
  9383. the property list including an extra property :name with the block name."
  9384. (unless (looking-at org-dblock-start-re)
  9385. (error "Not at a dynamic block"))
  9386. (let* ((begdel (1+ (match-end 0)))
  9387. (name (org-no-properties (match-string 1)))
  9388. (params (append (list :name name)
  9389. (read (concat "(" (match-string 3) ")")))))
  9390. (save-excursion
  9391. (beginning-of-line 1)
  9392. (skip-chars-forward " \t")
  9393. (setq params (plist-put params :indentation-column (current-column))))
  9394. (unless (re-search-forward org-dblock-end-re nil t)
  9395. (error "Dynamic block not terminated"))
  9396. (setq params
  9397. (append params
  9398. (list :content (buffer-substring
  9399. begdel (match-beginning 0)))))
  9400. (delete-region begdel (match-beginning 0))
  9401. (goto-char begdel)
  9402. (open-line 1)
  9403. params))
  9404. (defun org-map-dblocks (&optional command)
  9405. "Apply COMMAND to all dynamic blocks in the current buffer.
  9406. If COMMAND is not given, use `org-update-dblock'."
  9407. (let ((cmd (or command 'org-update-dblock)))
  9408. (save-excursion
  9409. (goto-char (point-min))
  9410. (while (re-search-forward org-dblock-start-re nil t)
  9411. (goto-char (match-beginning 0))
  9412. (save-excursion
  9413. (condition-case nil
  9414. (funcall cmd)
  9415. (error (message "Error during update of dynamic block"))))
  9416. (unless (re-search-forward org-dblock-end-re nil t)
  9417. (error "Dynamic block not terminated"))))))
  9418. (defun org-dblock-update (&optional arg)
  9419. "User command for updating dynamic blocks.
  9420. Update the dynamic block at point. With prefix ARG, update all dynamic
  9421. blocks in the buffer."
  9422. (interactive "P")
  9423. (if arg
  9424. (org-update-all-dblocks)
  9425. (or (looking-at org-dblock-start-re)
  9426. (org-beginning-of-dblock))
  9427. (org-update-dblock)))
  9428. (defun org-update-dblock ()
  9429. "Update the dynamic block at point.
  9430. This means to empty the block, parse for parameters and then call
  9431. the correct writing function."
  9432. (save-window-excursion
  9433. (let* ((pos (point))
  9434. (line (org-current-line))
  9435. (params (org-prepare-dblock))
  9436. (name (plist-get params :name))
  9437. (indent (plist-get params :indentation-column))
  9438. (cmd (intern (concat "org-dblock-write:" name))))
  9439. (message "Updating dynamic block `%s' at line %d..." name line)
  9440. (funcall cmd params)
  9441. (message "Updating dynamic block `%s' at line %d...done" name line)
  9442. (goto-char pos)
  9443. (when (and indent (> indent 0))
  9444. (setq indent (make-string indent ?\ ))
  9445. (save-excursion
  9446. (org-beginning-of-dblock)
  9447. (forward-line 1)
  9448. (while (not (looking-at org-dblock-end-re))
  9449. (insert indent)
  9450. (beginning-of-line 2))
  9451. (when (looking-at org-dblock-end-re)
  9452. (and (looking-at "[ \t]+")
  9453. (replace-match ""))
  9454. (insert indent)))))))
  9455. (defun org-beginning-of-dblock ()
  9456. "Find the beginning of the dynamic block at point.
  9457. Error if there is no such block at point."
  9458. (let ((pos (point))
  9459. beg)
  9460. (end-of-line 1)
  9461. (if (and (re-search-backward org-dblock-start-re nil t)
  9462. (setq beg (match-beginning 0))
  9463. (re-search-forward org-dblock-end-re nil t)
  9464. (> (match-end 0) pos))
  9465. (goto-char beg)
  9466. (goto-char pos)
  9467. (error "Not in a dynamic block"))))
  9468. (defun org-update-all-dblocks ()
  9469. "Update all dynamic blocks in the buffer.
  9470. This function can be used in a hook."
  9471. (when (org-mode-p)
  9472. (org-map-dblocks 'org-update-dblock)))
  9473. ;;;; Completion
  9474. (defconst org-additional-option-like-keywords
  9475. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9476. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9477. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9478. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9479. "BEGIN:" "END:"
  9480. "ORGTBL" "TBLFM:" "TBLNAME:"
  9481. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9482. "BEGIN_QUOTE" "END_QUOTE"
  9483. "BEGIN_VERSE" "END_VERSE"
  9484. "BEGIN_CENTER" "END_CENTER"
  9485. "BEGIN_SRC" "END_SRC"
  9486. "CATEGORY" "COLUMNS"
  9487. "CAPTION" "LABEL"
  9488. "SETUPFILE"
  9489. "BIND"
  9490. "MACRO"))
  9491. (defcustom org-structure-template-alist
  9492. '(
  9493. ("s" "#+begin_src ?\n\n#+end_src"
  9494. "<src lang=\"?\">\n\n</src>")
  9495. ("e" "#+begin_example\n?\n#+end_example"
  9496. "<example>\n?\n</example>")
  9497. ("q" "#+begin_quote\n?\n#+end_quote"
  9498. "<quote>\n?\n</quote>")
  9499. ("v" "#+begin_verse\n?\n#+end_verse"
  9500. "<verse>\n?\n/verse>")
  9501. ("c" "#+begin_center\n?\n#+end_center"
  9502. "<center>\n?\n/center>")
  9503. ("l" "#+begin_latex\n?\n#+end_latex"
  9504. "<literal style=\"latex\">\n?\n</literal>")
  9505. ("L" "#+latex: "
  9506. "<literal style=\"latex\">?</literal>")
  9507. ("h" "#+begin_html\n?\n#+end_html"
  9508. "<literal style=\"html\">\n?\n</literal>")
  9509. ("H" "#+html: "
  9510. "<literal style=\"html\">?</literal>")
  9511. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9512. ("A" "#+ascii: ")
  9513. ("i" "#+include %file ?"
  9514. "<include file=%file markup=\"?\">")
  9515. )
  9516. "Structure completion elements.
  9517. This is a list of abbreviation keys and values. The value gets inserted
  9518. if you type `<' followed by the key and then press the completion key,
  9519. usually `M-TAB'. %file will be replaced by a file name after prompting
  9520. for the file using completion.
  9521. There are two templates for each key, the first uses the original Org syntax,
  9522. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9523. the default when the /org-mtags.el/ module has been loaded. See also the
  9524. variable `org-mtags-prefer-muse-templates'.
  9525. This is an experimental feature, it is undecided if it is going to stay in."
  9526. :group 'org-completion
  9527. :type '(repeat
  9528. (string :tag "Key")
  9529. (string :tag "Template")
  9530. (string :tag "Muse Template")))
  9531. (defun org-try-structure-completion ()
  9532. "Try to complete a structure template before point.
  9533. This looks for strings like \"<e\" on an otherwise empty line and
  9534. expands them."
  9535. (let ((l (buffer-substring (point-at-bol) (point)))
  9536. a)
  9537. (when (and (looking-at "[ \t]*$")
  9538. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9539. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9540. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9541. (match-beginning 1)) a)
  9542. t)))
  9543. (defun org-complete-expand-structure-template (start cell)
  9544. "Expand a structure template."
  9545. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9546. (rpl (nth (if musep 2 1) cell))
  9547. (ind ""))
  9548. (delete-region start (point))
  9549. (when (string-match "\\`#\\+" rpl)
  9550. (cond
  9551. ((bolp))
  9552. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9553. (setq ind (buffer-substring (point-at-bol) (point))))
  9554. (t (newline))))
  9555. (setq start (point))
  9556. (if (string-match "%file" rpl)
  9557. (setq rpl (replace-match
  9558. (concat
  9559. "\""
  9560. (save-match-data
  9561. (abbreviate-file-name (read-file-name "Include file: ")))
  9562. "\"")
  9563. t t rpl)))
  9564. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9565. (concat "\n" ind)))
  9566. (insert rpl)
  9567. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9568. (defun org-complete (&optional arg)
  9569. "Perform completion on word at point.
  9570. At the beginning of a headline, this completes TODO keywords as given in
  9571. `org-todo-keywords'.
  9572. If the current word is preceded by a backslash, completes the TeX symbols
  9573. that are supported for HTML support.
  9574. If the current word is preceded by \"#+\", completes special words for
  9575. setting file options.
  9576. In the line after \"#+STARTUP:, complete valid keywords.\"
  9577. At all other locations, this simply calls the value of
  9578. `org-completion-fallback-command'."
  9579. (interactive "P")
  9580. (org-without-partial-completion
  9581. (catch 'exit
  9582. (let* ((a nil)
  9583. (end (point))
  9584. (beg1 (save-excursion
  9585. (skip-chars-backward (org-re "[:alnum:]_@"))
  9586. (point)))
  9587. (beg (save-excursion
  9588. (skip-chars-backward "a-zA-Z0-9_:$")
  9589. (point)))
  9590. (confirm (lambda (x) (stringp (car x))))
  9591. (searchhead (equal (char-before beg) ?*))
  9592. (struct
  9593. (when (and (member (char-before beg1) '(?. ?<))
  9594. (setq a (assoc (buffer-substring beg1 (point))
  9595. org-structure-template-alist)))
  9596. (org-complete-expand-structure-template (1- beg1) a)
  9597. (throw 'exit t)))
  9598. (tag (and (equal (char-before beg1) ?:)
  9599. (equal (char-after (point-at-bol)) ?*)))
  9600. (prop (and (equal (char-before beg1) ?:)
  9601. (not (equal (char-after (point-at-bol)) ?*))))
  9602. (texp (equal (char-before beg) ?\\))
  9603. (link (equal (char-before beg) ?\[))
  9604. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9605. beg)
  9606. "#+"))
  9607. (startup (string-match "^#\\+STARTUP:.*"
  9608. (buffer-substring (point-at-bol) (point))))
  9609. (completion-ignore-case opt)
  9610. (type nil)
  9611. (tbl nil)
  9612. (table (cond
  9613. (opt
  9614. (setq type :opt)
  9615. (require 'org-exp)
  9616. (append
  9617. (delq nil
  9618. (mapcar
  9619. (lambda (x)
  9620. (if (string-match
  9621. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9622. (cons (match-string 2 x)
  9623. (match-string 1 x))))
  9624. (org-split-string (org-get-current-options) "\n")))
  9625. (mapcar 'list org-additional-option-like-keywords)))
  9626. (startup
  9627. (setq type :startup)
  9628. org-startup-options)
  9629. (link (append org-link-abbrev-alist-local
  9630. org-link-abbrev-alist))
  9631. (texp
  9632. (setq type :tex)
  9633. (append org-entities-user org-entities))
  9634. ((string-match "\\`\\*+[ \t]+\\'"
  9635. (buffer-substring (point-at-bol) beg))
  9636. (setq type :todo)
  9637. (mapcar 'list org-todo-keywords-1))
  9638. (searchhead
  9639. (setq type :searchhead)
  9640. (save-excursion
  9641. (goto-char (point-min))
  9642. (while (re-search-forward org-todo-line-regexp nil t)
  9643. (push (list
  9644. (org-make-org-heading-search-string
  9645. (match-string 3) t))
  9646. tbl)))
  9647. tbl)
  9648. (tag (setq type :tag beg beg1)
  9649. (or org-tag-alist (org-get-buffer-tags)))
  9650. (prop (setq type :prop beg beg1)
  9651. (mapcar 'list (org-buffer-property-keys nil t t)))
  9652. (t (progn
  9653. (call-interactively org-completion-fallback-command)
  9654. (throw 'exit nil)))))
  9655. (pattern (buffer-substring-no-properties beg end))
  9656. (completion (try-completion pattern table confirm)))
  9657. (cond ((eq completion t)
  9658. (if (not (assoc (upcase pattern) table))
  9659. (message "Already complete")
  9660. (if (and (equal type :opt)
  9661. (not (member (car (assoc (upcase pattern) table))
  9662. org-additional-option-like-keywords)))
  9663. (insert (substring (cdr (assoc (upcase pattern) table))
  9664. (length pattern)))
  9665. (if (memq type '(:tag :prop)) (insert ":")))))
  9666. ((null completion)
  9667. (message "Can't find completion for \"%s\"" pattern)
  9668. (ding))
  9669. ((not (string= pattern completion))
  9670. (delete-region beg end)
  9671. (if (string-match " +$" completion)
  9672. (setq completion (replace-match "" t t completion)))
  9673. (insert completion)
  9674. (if (get-buffer-window "*Completions*")
  9675. (delete-window (get-buffer-window "*Completions*")))
  9676. (if (assoc completion table)
  9677. (if (eq type :todo) (insert " ")
  9678. (if (memq type '(:tag :prop)) (insert ":"))))
  9679. (if (and (equal type :opt) (assoc completion table))
  9680. (message "%s" (substitute-command-keys
  9681. "Press \\[org-complete] again to insert example settings"))))
  9682. (t
  9683. (message "Making completion list...")
  9684. (let ((list (sort (all-completions pattern table confirm)
  9685. 'string<)))
  9686. (with-output-to-temp-buffer "*Completions*"
  9687. (condition-case nil
  9688. ;; Protection needed for XEmacs and emacs 21
  9689. (display-completion-list list pattern)
  9690. (error (display-completion-list list)))))
  9691. (message "Making completion list...%s" "done")))))))
  9692. ;;;; TODO, DEADLINE, Comments
  9693. (defun org-toggle-comment ()
  9694. "Change the COMMENT state of an entry."
  9695. (interactive)
  9696. (save-excursion
  9697. (org-back-to-heading)
  9698. (let (case-fold-search)
  9699. (if (looking-at (concat outline-regexp
  9700. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9701. (replace-match "" t t nil 1)
  9702. (if (looking-at outline-regexp)
  9703. (progn
  9704. (goto-char (match-end 0))
  9705. (insert org-comment-string " ")))))))
  9706. (defvar org-last-todo-state-is-todo nil
  9707. "This is non-nil when the last TODO state change led to a TODO state.
  9708. If the last change removed the TODO tag or switched to DONE, then
  9709. this is nil.")
  9710. (defvar org-setting-tags nil) ; dynamically skipped
  9711. (defun org-parse-local-options (string var)
  9712. "Parse STRING for startup setting relevant for variable VAR."
  9713. (let ((rtn (symbol-value var))
  9714. e opts)
  9715. (save-match-data
  9716. (if (or (not string) (not (string-match "\\S-" string)))
  9717. rtn
  9718. (setq opts (delq nil (mapcar (lambda (x)
  9719. (setq e (assoc x org-startup-options))
  9720. (if (eq (nth 1 e) var) e nil))
  9721. (org-split-string string "[ \t]+"))))
  9722. (if (not opts)
  9723. rtn
  9724. (setq rtn nil)
  9725. (while (setq e (pop opts))
  9726. (if (not (nth 3 e))
  9727. (setq rtn (nth 2 e))
  9728. (if (not (listp rtn)) (setq rtn nil))
  9729. (push (nth 2 e) rtn)))
  9730. rtn)))))
  9731. (defvar org-todo-setup-filter-hook nil
  9732. "Hook for functions that pre-filter todo specs.
  9733. Each function takes a todo spec and returns either nil or the spec
  9734. transformed into canonical form." )
  9735. (defvar org-todo-get-default-hook nil
  9736. "Hook for functions that get a default item for todo.
  9737. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9738. nil or a string to be used for the todo mark." )
  9739. (defvar org-agenda-headline-snapshot-before-repeat)
  9740. (defun org-todo (&optional arg)
  9741. "Change the TODO state of an item.
  9742. The state of an item is given by a keyword at the start of the heading,
  9743. like
  9744. *** TODO Write paper
  9745. *** DONE Call mom
  9746. The different keywords are specified in the variable `org-todo-keywords'.
  9747. By default the available states are \"TODO\" and \"DONE\".
  9748. So for this example: when the item starts with TODO, it is changed to DONE.
  9749. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9750. DONE are present, add TODO at the beginning of the heading.
  9751. With \\[universal-argument] prefix arg, use completion to determine the new \
  9752. state.
  9753. With numeric prefix arg, switch to that state.
  9754. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9755. keywords (nextset).
  9756. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9757. For calling through lisp, arg is also interpreted in the following way:
  9758. 'none -> empty state
  9759. \"\"(empty string) -> switch to empty state
  9760. 'done -> switch to DONE
  9761. 'nextset -> switch to the next set of keywords
  9762. 'previousset -> switch to the previous set of keywords
  9763. \"WAITING\" -> switch to the specified keyword, but only if it
  9764. really is a member of `org-todo-keywords'."
  9765. (interactive "P")
  9766. (if (equal arg '(16)) (setq arg 'nextset))
  9767. (let ((org-blocker-hook org-blocker-hook)
  9768. (case-fold-search nil))
  9769. (when (equal arg '(64))
  9770. (setq arg nil org-blocker-hook nil))
  9771. (when (and org-blocker-hook
  9772. (or org-inhibit-blocking
  9773. (org-entry-get nil "NOBLOCKING")))
  9774. (setq org-blocker-hook nil))
  9775. (save-excursion
  9776. (catch 'exit
  9777. (org-back-to-heading t)
  9778. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9779. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9780. (looking-at " *"))
  9781. (let* ((match-data (match-data))
  9782. (startpos (point-at-bol))
  9783. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9784. (org-log-done org-log-done)
  9785. (org-log-repeat org-log-repeat)
  9786. (org-todo-log-states org-todo-log-states)
  9787. (this (match-string 1))
  9788. (hl-pos (match-beginning 0))
  9789. (head (org-get-todo-sequence-head this))
  9790. (ass (assoc head org-todo-kwd-alist))
  9791. (interpret (nth 1 ass))
  9792. (done-word (nth 3 ass))
  9793. (final-done-word (nth 4 ass))
  9794. (last-state (or this ""))
  9795. (completion-ignore-case t)
  9796. (member (member this org-todo-keywords-1))
  9797. (tail (cdr member))
  9798. (state (cond
  9799. ((and org-todo-key-trigger
  9800. (or (and (equal arg '(4))
  9801. (eq org-use-fast-todo-selection 'prefix))
  9802. (and (not arg) org-use-fast-todo-selection
  9803. (not (eq org-use-fast-todo-selection
  9804. 'prefix)))))
  9805. ;; Use fast selection
  9806. (org-fast-todo-selection))
  9807. ((and (equal arg '(4))
  9808. (or (not org-use-fast-todo-selection)
  9809. (not org-todo-key-trigger)))
  9810. ;; Read a state with completion
  9811. (org-icompleting-read
  9812. "State: " (mapcar (lambda(x) (list x))
  9813. org-todo-keywords-1)
  9814. nil t))
  9815. ((eq arg 'right)
  9816. (if this
  9817. (if tail (car tail) nil)
  9818. (car org-todo-keywords-1)))
  9819. ((eq arg 'left)
  9820. (if (equal member org-todo-keywords-1)
  9821. nil
  9822. (if this
  9823. (nth (- (length org-todo-keywords-1)
  9824. (length tail) 2)
  9825. org-todo-keywords-1)
  9826. (org-last org-todo-keywords-1))))
  9827. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9828. (setq arg nil))) ; hack to fall back to cycling
  9829. (arg
  9830. ;; user or caller requests a specific state
  9831. (cond
  9832. ((equal arg "") nil)
  9833. ((eq arg 'none) nil)
  9834. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9835. ((eq arg 'nextset)
  9836. (or (car (cdr (member head org-todo-heads)))
  9837. (car org-todo-heads)))
  9838. ((eq arg 'previousset)
  9839. (let ((org-todo-heads (reverse org-todo-heads)))
  9840. (or (car (cdr (member head org-todo-heads)))
  9841. (car org-todo-heads))))
  9842. ((car (member arg org-todo-keywords-1)))
  9843. ((stringp arg)
  9844. (error "State `%s' not valid in this file" arg))
  9845. ((nth (1- (prefix-numeric-value arg))
  9846. org-todo-keywords-1))))
  9847. ((null member) (or head (car org-todo-keywords-1)))
  9848. ((equal this final-done-word) nil) ;; -> make empty
  9849. ((null tail) nil) ;; -> first entry
  9850. ((memq interpret '(type priority))
  9851. (if (eq this-command last-command)
  9852. (car tail)
  9853. (if (> (length tail) 0)
  9854. (or done-word (car org-done-keywords))
  9855. nil)))
  9856. (t
  9857. (car tail))))
  9858. (state (or
  9859. (run-hook-with-args-until-success
  9860. 'org-todo-get-default-hook state last-state)
  9861. state))
  9862. (next (if state (concat " " state " ") " "))
  9863. (change-plist (list :type 'todo-state-change :from this :to state
  9864. :position startpos))
  9865. dolog now-done-p)
  9866. (when org-blocker-hook
  9867. (setq org-last-todo-state-is-todo
  9868. (not (member this org-done-keywords)))
  9869. (unless (save-excursion
  9870. (save-match-data
  9871. (run-hook-with-args-until-failure
  9872. 'org-blocker-hook change-plist)))
  9873. (if (interactive-p)
  9874. (error "TODO state change from %s to %s blocked" this state)
  9875. ;; fail silently
  9876. (message "TODO state change from %s to %s blocked" this state)
  9877. (throw 'exit nil))))
  9878. (store-match-data match-data)
  9879. (replace-match next t t)
  9880. (unless (pos-visible-in-window-p hl-pos)
  9881. (message "TODO state changed to %s" (org-trim next)))
  9882. (unless head
  9883. (setq head (org-get-todo-sequence-head state)
  9884. ass (assoc head org-todo-kwd-alist)
  9885. interpret (nth 1 ass)
  9886. done-word (nth 3 ass)
  9887. final-done-word (nth 4 ass)))
  9888. (when (memq arg '(nextset previousset))
  9889. (message "Keyword-Set %d/%d: %s"
  9890. (- (length org-todo-sets) -1
  9891. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9892. (length org-todo-sets)
  9893. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9894. (setq org-last-todo-state-is-todo
  9895. (not (member state org-done-keywords)))
  9896. (setq now-done-p (and (member state org-done-keywords)
  9897. (not (member this org-done-keywords))))
  9898. (and logging (org-local-logging logging))
  9899. (when (and (or org-todo-log-states org-log-done)
  9900. (not (eq org-inhibit-logging t))
  9901. (not (memq arg '(nextset previousset))))
  9902. ;; we need to look at recording a time and note
  9903. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9904. (nth 2 (assoc this org-todo-log-states))))
  9905. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9906. (setq dolog 'time))
  9907. (when (and state
  9908. (member state org-not-done-keywords)
  9909. (not (member this org-not-done-keywords)))
  9910. ;; This is now a todo state and was not one before
  9911. ;; If there was a CLOSED time stamp, get rid of it.
  9912. (org-add-planning-info nil nil 'closed))
  9913. (when (and now-done-p org-log-done)
  9914. ;; It is now done, and it was not done before
  9915. (org-add-planning-info 'closed (org-current-time))
  9916. (if (and (not dolog) (eq 'note org-log-done))
  9917. (org-add-log-setup 'done state this 'findpos 'note)))
  9918. (when (and state dolog)
  9919. ;; This is a non-nil state, and we need to log it
  9920. (org-add-log-setup 'state state this 'findpos dolog)))
  9921. ;; Fixup tag positioning
  9922. (org-todo-trigger-tag-changes state)
  9923. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9924. (when org-provide-todo-statistics
  9925. (org-update-parent-todo-statistics))
  9926. (run-hooks 'org-after-todo-state-change-hook)
  9927. (if (and arg (not (member state org-done-keywords)))
  9928. (setq head (org-get-todo-sequence-head state)))
  9929. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9930. ;; Do we need to trigger a repeat?
  9931. (when now-done-p
  9932. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9933. ;; This is for the agenda, take a snapshot of the headline.
  9934. (save-match-data
  9935. (setq org-agenda-headline-snapshot-before-repeat
  9936. (org-get-heading))))
  9937. (org-auto-repeat-maybe state))
  9938. ;; Fixup cursor location if close to the keyword
  9939. (if (and (outline-on-heading-p)
  9940. (not (bolp))
  9941. (save-excursion (beginning-of-line 1)
  9942. (looking-at org-todo-line-regexp))
  9943. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9944. (progn
  9945. (goto-char (or (match-end 2) (match-end 1)))
  9946. (and (looking-at " ") (just-one-space))))
  9947. (when org-trigger-hook
  9948. (save-excursion
  9949. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9950. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9951. "Block turning an entry into a TODO, using the hierarchy.
  9952. This checks whether the current task should be blocked from state
  9953. changes. Such blocking occurs when:
  9954. 1. The task has children which are not all in a completed state.
  9955. 2. A task has a parent with the property :ORDERED:, and there
  9956. are siblings prior to the current task with incomplete
  9957. status.
  9958. 3. The parent of the task is blocked because it has siblings that should
  9959. be done first, or is child of a block grandparent TODO entry."
  9960. (if (not org-enforce-todo-dependencies)
  9961. t ; if locally turned off don't block
  9962. (catch 'dont-block
  9963. ;; If this is not a todo state change, or if this entry is already DONE,
  9964. ;; do not block
  9965. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9966. (member (plist-get change-plist :from)
  9967. (cons 'done org-done-keywords))
  9968. (member (plist-get change-plist :to)
  9969. (cons 'todo org-not-done-keywords))
  9970. (not (plist-get change-plist :to)))
  9971. (throw 'dont-block t))
  9972. ;; If this task has children, and any are undone, it's blocked
  9973. (save-excursion
  9974. (org-back-to-heading t)
  9975. (let ((this-level (funcall outline-level)))
  9976. (outline-next-heading)
  9977. (let ((child-level (funcall outline-level)))
  9978. (while (and (not (eobp))
  9979. (> child-level this-level))
  9980. ;; this todo has children, check whether they are all
  9981. ;; completed
  9982. (if (and (not (org-entry-is-done-p))
  9983. (org-entry-is-todo-p))
  9984. (throw 'dont-block nil))
  9985. (outline-next-heading)
  9986. (setq child-level (funcall outline-level))))))
  9987. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9988. ;; any previous siblings are undone, it's blocked
  9989. (save-excursion
  9990. (org-back-to-heading t)
  9991. (let* ((pos (point))
  9992. (parent-pos (and (org-up-heading-safe) (point))))
  9993. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9994. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9995. (forward-line 1)
  9996. (re-search-forward org-not-done-heading-regexp pos t))
  9997. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9998. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9999. (while t
  10000. (goto-char parent-pos)
  10001. (if (not (looking-at org-not-done-heading-regexp))
  10002. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10003. (setq pos (point))
  10004. (setq parent-pos (and (org-up-heading-safe) (point)))
  10005. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10006. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10007. (forward-line 1)
  10008. (re-search-forward org-not-done-heading-regexp pos t))
  10009. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10010. (defcustom org-track-ordered-property-with-tag nil
  10011. "Should the ORDERED property also be shown as a tag?
  10012. The ORDERED property decides if an entry should require subtasks to be
  10013. completed in sequence. Since a property is not very visible, setting
  10014. this option means that toggling the ORDERED property with the command
  10015. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10016. not relevant for the behavior, but it makes things more visible.
  10017. Note that toggling the tag with tags commands will not change the property
  10018. and therefore not influence behavior!
  10019. This can be t, meaning the tag ORDERED should be used, It can also be a
  10020. string to select a different tag for this task."
  10021. :group 'org-todo
  10022. :type '(choice
  10023. (const :tag "No tracking" nil)
  10024. (const :tag "Track with ORDERED tag" t)
  10025. (string :tag "Use other tag")))
  10026. (defun org-toggle-ordered-property ()
  10027. "Toggle the ORDERED property of the current entry.
  10028. For better visibility, you can track the value of this property with a tag.
  10029. See variable `org-track-ordered-property-with-tag'."
  10030. (interactive)
  10031. (let* ((t1 org-track-ordered-property-with-tag)
  10032. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10033. (save-excursion
  10034. (org-back-to-heading)
  10035. (if (org-entry-get nil "ORDERED")
  10036. (progn
  10037. (org-delete-property "ORDERED")
  10038. (and tag (org-toggle-tag tag 'off))
  10039. (message "Subtasks can be completed in arbitrary order"))
  10040. (org-entry-put nil "ORDERED" "t")
  10041. (and tag (org-toggle-tag tag 'on))
  10042. (message "Subtasks must be completed in sequence")))))
  10043. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10044. (defun org-block-todo-from-checkboxes (change-plist)
  10045. "Block turning an entry into a TODO, using checkboxes.
  10046. This checks whether the current task should be blocked from state
  10047. changes because there are unchecked boxes in this entry."
  10048. (if (not org-enforce-todo-checkbox-dependencies)
  10049. t ; if locally turned off don't block
  10050. (catch 'dont-block
  10051. ;; If this is not a todo state change, or if this entry is already DONE,
  10052. ;; do not block
  10053. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10054. (member (plist-get change-plist :from)
  10055. (cons 'done org-done-keywords))
  10056. (member (plist-get change-plist :to)
  10057. (cons 'todo org-not-done-keywords))
  10058. (not (plist-get change-plist :to)))
  10059. (throw 'dont-block t))
  10060. ;; If this task has checkboxes that are not checked, it's blocked
  10061. (save-excursion
  10062. (org-back-to-heading t)
  10063. (let ((beg (point)) end)
  10064. (outline-next-heading)
  10065. (setq end (point))
  10066. (goto-char beg)
  10067. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10068. end t)
  10069. (progn
  10070. (if (boundp 'org-blocked-by-checkboxes)
  10071. (setq org-blocked-by-checkboxes t))
  10072. (throw 'dont-block nil)))))
  10073. t))) ; do not block
  10074. (defun org-entry-blocked-p ()
  10075. "Is the current entry blocked?"
  10076. (if (org-entry-get nil "NOBLOCKING")
  10077. nil ;; Never block this entry
  10078. (not
  10079. (run-hook-with-args-until-failure
  10080. 'org-blocker-hook
  10081. (list :type 'todo-state-change
  10082. :position (point)
  10083. :from 'todo
  10084. :to 'done)))))
  10085. (defun org-update-statistics-cookies (all)
  10086. "Update the statistics cookie, either from TODO or from checkboxes.
  10087. This should be called with the cursor in a line with a statistics cookie."
  10088. (interactive "P")
  10089. (if all
  10090. (progn
  10091. (org-update-checkbox-count 'all)
  10092. (org-map-entries 'org-update-parent-todo-statistics))
  10093. (if (not (org-on-heading-p))
  10094. (org-update-checkbox-count)
  10095. (let ((pos (move-marker (make-marker) (point)))
  10096. end l1 l2)
  10097. (ignore-errors (org-back-to-heading t))
  10098. (if (not (org-on-heading-p))
  10099. (org-update-checkbox-count)
  10100. (setq l1 (org-outline-level))
  10101. (setq end (save-excursion
  10102. (outline-next-heading)
  10103. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10104. (point)))
  10105. (if (and (save-excursion
  10106. (re-search-forward
  10107. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10108. (not (save-excursion (re-search-forward
  10109. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10110. (org-update-checkbox-count)
  10111. (if (and l2 (> l2 l1))
  10112. (progn
  10113. (goto-char end)
  10114. (org-update-parent-todo-statistics))
  10115. (goto-char pos)
  10116. (beginning-of-line 1)
  10117. (while (re-search-forward
  10118. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10119. (point-at-eol) t)
  10120. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10121. (goto-char pos)
  10122. (move-marker pos nil)))))
  10123. (defvar org-entry-property-inherited-from) ;; defined below
  10124. (defun org-update-parent-todo-statistics ()
  10125. "Update any statistics cookie in the parent of the current headline.
  10126. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10127. the entire subtree and this will travel up the hierarchy and update
  10128. statistics everywhere."
  10129. (interactive)
  10130. (let* ((lim 0) prop
  10131. (recursive (or (not org-hierarchical-todo-statistics)
  10132. (string-match
  10133. "\\<recursive\\>"
  10134. (or (setq prop (org-entry-get
  10135. nil "COOKIE_DATA" 'inherit)) ""))))
  10136. (lim (or (and prop (marker-position
  10137. org-entry-property-inherited-from))
  10138. lim))
  10139. (first t)
  10140. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10141. level ltoggle l1 new ndel
  10142. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10143. (catch 'exit
  10144. (save-excursion
  10145. (beginning-of-line 1)
  10146. (if (org-at-heading-p)
  10147. (setq ltoggle (funcall outline-level))
  10148. (error "This should not happen"))
  10149. (while (and (setq level (org-up-heading-safe))
  10150. (or recursive first)
  10151. (>= (point) lim))
  10152. (setq first nil cookie-present nil)
  10153. (unless (and level
  10154. (not (string-match
  10155. "\\<checkbox\\>"
  10156. (downcase
  10157. (or (org-entry-get
  10158. nil "COOKIE_DATA")
  10159. "")))))
  10160. (throw 'exit nil))
  10161. (while (re-search-forward box-re (point-at-eol) t)
  10162. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10163. (setq is-percent (match-end 2))
  10164. (save-match-data
  10165. (unless (outline-next-heading) (throw 'exit nil))
  10166. (while (and (looking-at org-complex-heading-regexp)
  10167. (> (setq l1 (length (match-string 1))) level))
  10168. (setq kwd (and (or recursive (= l1 ltoggle))
  10169. (match-string 2)))
  10170. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10171. (and (listp org-provide-todo-statistics)
  10172. (or (member kwd org-provide-todo-statistics)
  10173. (member kwd org-done-keywords))))
  10174. (setq cnt-all (1+ cnt-all))
  10175. (if (eq org-provide-todo-statistics t)
  10176. (and kwd (setq cnt-all (1+ cnt-all)))))
  10177. (and (member kwd org-done-keywords)
  10178. (setq cnt-done (1+ cnt-done)))
  10179. (outline-next-heading)))
  10180. (setq new
  10181. (if is-percent
  10182. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10183. (format "[%d/%d]" cnt-done cnt-all))
  10184. ndel (- (match-end 0) (match-beginning 0)))
  10185. (goto-char (match-beginning 0))
  10186. (insert new)
  10187. (delete-region (point) (+ (point) ndel)))
  10188. (when cookie-present
  10189. (run-hook-with-args 'org-after-todo-statistics-hook
  10190. cnt-done (- cnt-all cnt-done))))))
  10191. (run-hooks 'org-todo-statistics-hook)))
  10192. (defvar org-after-todo-statistics-hook nil
  10193. "Hook that is called after a TODO statistics cookie has been updated.
  10194. Each function is called with two arguments: the number of not-done entries
  10195. and the number of done entries.
  10196. For example, the following function, when added to this hook, will switch
  10197. an entry to DONE when all children are done, and back to TODO when new
  10198. entries are set to a TODO status. Note that this hook is only called
  10199. when there is a statistics cookie in the headline!
  10200. (defun org-summary-todo (n-done n-not-done)
  10201. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10202. (let (org-log-done org-log-states) ; turn off logging
  10203. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10204. ")
  10205. (defvar org-todo-statistics-hook nil
  10206. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10207. This hook runs even if there is no statistics cookie present, in which case
  10208. `org-after-todo-statistics-hook' would not run.")
  10209. (defun org-todo-trigger-tag-changes (state)
  10210. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10211. (let ((l org-todo-state-tags-triggers)
  10212. changes)
  10213. (when (or (not state) (equal state ""))
  10214. (setq changes (append changes (cdr (assoc "" l)))))
  10215. (when (and (stringp state) (> (length state) 0))
  10216. (setq changes (append changes (cdr (assoc state l)))))
  10217. (when (member state org-not-done-keywords)
  10218. (setq changes (append changes (cdr (assoc 'todo l)))))
  10219. (when (member state org-done-keywords)
  10220. (setq changes (append changes (cdr (assoc 'done l)))))
  10221. (dolist (c changes)
  10222. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10223. (defun org-local-logging (value)
  10224. "Get logging settings from a property VALUE."
  10225. (let* (words w a)
  10226. ;; directly set the variables, they are already local.
  10227. (setq org-log-done nil
  10228. org-log-repeat nil
  10229. org-todo-log-states nil)
  10230. (setq words (org-split-string value))
  10231. (while (setq w (pop words))
  10232. (cond
  10233. ((setq a (assoc w org-startup-options))
  10234. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10235. (set (nth 1 a) (nth 2 a))))
  10236. ((setq a (org-extract-log-state-settings w))
  10237. (and (member (car a) org-todo-keywords-1)
  10238. (push a org-todo-log-states)))))))
  10239. (defun org-get-todo-sequence-head (kwd)
  10240. "Return the head of the TODO sequence to which KWD belongs.
  10241. If KWD is not set, check if there is a text property remembering the
  10242. right sequence."
  10243. (let (p)
  10244. (cond
  10245. ((not kwd)
  10246. (or (get-text-property (point-at-bol) 'org-todo-head)
  10247. (progn
  10248. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10249. nil (point-at-eol)))
  10250. (get-text-property p 'org-todo-head))))
  10251. ((not (member kwd org-todo-keywords-1))
  10252. (car org-todo-keywords-1))
  10253. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10254. (defun org-fast-todo-selection ()
  10255. "Fast TODO keyword selection with single keys.
  10256. Returns the new TODO keyword, or nil if no state change should occur."
  10257. (let* ((fulltable org-todo-key-alist)
  10258. (done-keywords org-done-keywords) ;; needed for the faces.
  10259. (maxlen (apply 'max (mapcar
  10260. (lambda (x)
  10261. (if (stringp (car x)) (string-width (car x)) 0))
  10262. fulltable)))
  10263. (expert nil)
  10264. (fwidth (+ maxlen 3 1 3))
  10265. (ncol (/ (- (window-width) 4) fwidth))
  10266. tg cnt e c tbl
  10267. groups ingroup)
  10268. (save-excursion
  10269. (save-window-excursion
  10270. (if expert
  10271. (set-buffer (get-buffer-create " *Org todo*"))
  10272. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10273. (erase-buffer)
  10274. (org-set-local 'org-done-keywords done-keywords)
  10275. (setq tbl fulltable cnt 0)
  10276. (while (setq e (pop tbl))
  10277. (cond
  10278. ((equal e '(:startgroup))
  10279. (push '() groups) (setq ingroup t)
  10280. (when (not (= cnt 0))
  10281. (setq cnt 0)
  10282. (insert "\n"))
  10283. (insert "{ "))
  10284. ((equal e '(:endgroup))
  10285. (setq ingroup nil cnt 0)
  10286. (insert "}\n"))
  10287. ((equal e '(:newline))
  10288. (when (not (= cnt 0))
  10289. (setq cnt 0)
  10290. (insert "\n")
  10291. (setq e (car tbl))
  10292. (while (equal (car tbl) '(:newline))
  10293. (insert "\n")
  10294. (setq tbl (cdr tbl)))))
  10295. (t
  10296. (setq tg (car e) c (cdr e))
  10297. (if ingroup (push tg (car groups)))
  10298. (setq tg (org-add-props tg nil 'face
  10299. (org-get-todo-face tg)))
  10300. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10301. (insert "[" c "] " tg (make-string
  10302. (- fwidth 4 (length tg)) ?\ ))
  10303. (when (= (setq cnt (1+ cnt)) ncol)
  10304. (insert "\n")
  10305. (if ingroup (insert " "))
  10306. (setq cnt 0)))))
  10307. (insert "\n")
  10308. (goto-char (point-min))
  10309. (if (not expert) (org-fit-window-to-buffer))
  10310. (message "[a-z..]:Set [SPC]:clear")
  10311. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10312. (cond
  10313. ((or (= c ?\C-g)
  10314. (and (= c ?q) (not (rassoc c fulltable))))
  10315. (setq quit-flag t))
  10316. ((= c ?\ ) nil)
  10317. ((setq e (rassoc c fulltable) tg (car e))
  10318. tg)
  10319. (t (setq quit-flag t)))))))
  10320. (defun org-entry-is-todo-p ()
  10321. (member (org-get-todo-state) org-not-done-keywords))
  10322. (defun org-entry-is-done-p ()
  10323. (member (org-get-todo-state) org-done-keywords))
  10324. (defun org-get-todo-state ()
  10325. (save-excursion
  10326. (org-back-to-heading t)
  10327. (and (looking-at org-todo-line-regexp)
  10328. (match-end 2)
  10329. (match-string 2))))
  10330. (defun org-at-date-range-p (&optional inactive-ok)
  10331. "Is the cursor inside a date range?"
  10332. (interactive)
  10333. (save-excursion
  10334. (catch 'exit
  10335. (let ((pos (point)))
  10336. (skip-chars-backward "^[<\r\n")
  10337. (skip-chars-backward "<[")
  10338. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10339. (>= (match-end 0) pos)
  10340. (throw 'exit t))
  10341. (skip-chars-backward "^<[\r\n")
  10342. (skip-chars-backward "<[")
  10343. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10344. (>= (match-end 0) pos)
  10345. (throw 'exit t)))
  10346. nil)))
  10347. (defun org-get-repeat (&optional tagline)
  10348. "Check if there is a deadline/schedule with repeater in this entry."
  10349. (save-match-data
  10350. (save-excursion
  10351. (org-back-to-heading t)
  10352. (and (re-search-forward (if tagline
  10353. (concat tagline "\\s-*" org-repeat-re)
  10354. org-repeat-re)
  10355. (org-entry-end-position) t)
  10356. (match-string-no-properties 1)))))
  10357. (defvar org-last-changed-timestamp)
  10358. (defvar org-last-inserted-timestamp)
  10359. (defvar org-log-post-message)
  10360. (defvar org-log-note-purpose)
  10361. (defvar org-log-note-how)
  10362. (defvar org-log-note-extra)
  10363. (defun org-auto-repeat-maybe (done-word)
  10364. "Check if the current headline contains a repeated deadline/schedule.
  10365. If yes, set TODO state back to what it was and change the base date
  10366. of repeating deadline/scheduled time stamps to new date.
  10367. This function is run automatically after each state change to a DONE state."
  10368. ;; last-state is dynamically scoped into this function
  10369. (let* ((repeat (org-get-repeat))
  10370. (aa (assoc last-state org-todo-kwd-alist))
  10371. (interpret (nth 1 aa))
  10372. (head (nth 2 aa))
  10373. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10374. (msg "Entry repeats: ")
  10375. (org-log-done nil)
  10376. (org-todo-log-states nil)
  10377. (nshiftmax 10) (nshift 0)
  10378. re type n what ts time to-state)
  10379. (when repeat
  10380. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10381. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10382. org-todo-repeat-to-state))
  10383. (unless (and to-state (member to-state org-todo-keywords-1))
  10384. (setq to-state (if (eq interpret 'type) last-state head)))
  10385. (org-todo to-state)
  10386. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10387. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10388. (org-time-stamp-format t t))))
  10389. (when org-log-repeat
  10390. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10391. (memq 'org-add-log-note post-command-hook))
  10392. ;; OK, we are already setup for some record
  10393. (if (eq org-log-repeat 'note)
  10394. ;; make sure we take a note, not only a time stamp
  10395. (setq org-log-note-how 'note))
  10396. ;; Set up for taking a record
  10397. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10398. last-state
  10399. 'findpos org-log-repeat)))
  10400. (org-back-to-heading t)
  10401. (org-add-planning-info nil nil 'closed)
  10402. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10403. org-deadline-time-regexp "\\)\\|\\("
  10404. org-ts-regexp "\\)"))
  10405. (while (re-search-forward
  10406. re (save-excursion (outline-next-heading) (point)) t)
  10407. (setq type (if (match-end 1) org-scheduled-string
  10408. (if (match-end 3) org-deadline-string "Plain:"))
  10409. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10410. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10411. (setq n (string-to-number (match-string 2 ts))
  10412. what (match-string 3 ts))
  10413. (if (equal what "w") (setq n (* n 7) what "d"))
  10414. ;; Preparation, see if we need to modify the start date for the change
  10415. (when (match-end 1)
  10416. (setq time (save-match-data (org-time-string-to-time ts)))
  10417. (cond
  10418. ((equal (match-string 1 ts) ".")
  10419. ;; Shift starting date to today
  10420. (org-timestamp-change
  10421. (- (time-to-days (current-time)) (time-to-days time))
  10422. 'day))
  10423. ((equal (match-string 1 ts) "+")
  10424. (while (or (= nshift 0)
  10425. (<= (time-to-days time) (time-to-days (current-time))))
  10426. (when (= (incf nshift) nshiftmax)
  10427. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10428. (error "Abort")))
  10429. (org-timestamp-change n (cdr (assoc what whata)))
  10430. (org-at-timestamp-p t)
  10431. (setq ts (match-string 1))
  10432. (setq time (save-match-data (org-time-string-to-time ts))))
  10433. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10434. ;; rematch, so that we have everything in place for the real shift
  10435. (org-at-timestamp-p t)
  10436. (setq ts (match-string 1))
  10437. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10438. (org-timestamp-change n (cdr (assoc what whata)))
  10439. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10440. (setq org-log-post-message msg)
  10441. (message "%s" msg))))
  10442. (defun org-show-todo-tree (arg)
  10443. "Make a compact tree which shows all headlines marked with TODO.
  10444. The tree will show the lines where the regexp matches, and all higher
  10445. headlines above the match.
  10446. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10447. With a numeric prefix N, construct a sparse tree for the Nth element
  10448. of `org-todo-keywords-1'."
  10449. (interactive "P")
  10450. (let ((case-fold-search nil)
  10451. (kwd-re
  10452. (cond ((null arg) org-not-done-regexp)
  10453. ((equal arg '(4))
  10454. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10455. (mapcar 'list org-todo-keywords-1))))
  10456. (concat "\\("
  10457. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10458. "\\)\\>")))
  10459. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10460. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10461. org-todo-keywords-1)))
  10462. (t (error "Invalid prefix argument: %s" arg)))))
  10463. (message "%d TODO entries found"
  10464. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10465. (defun org-deadline (&optional remove time)
  10466. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10467. With argument REMOVE, remove any deadline from the item.
  10468. When TIME is set, it should be an internal time specification, and the
  10469. scheduling will use the corresponding date."
  10470. (interactive "P")
  10471. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10472. (repeater (and old-date
  10473. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10474. (match-string 1 old-date))))
  10475. (if remove
  10476. (progn
  10477. (when (and old-date org-log-redeadline)
  10478. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10479. org-log-redeadline))
  10480. (org-remove-timestamp-with-keyword org-deadline-string)
  10481. (message "Item no longer has a deadline."))
  10482. (org-add-planning-info 'deadline time 'closed)
  10483. (when (and old-date org-log-redeadline
  10484. (not (equal old-date
  10485. (substring org-last-inserted-timestamp 1 -1))))
  10486. (org-add-log-setup 'redeadline nil old-date 'findpos
  10487. org-log-redeadline))
  10488. (when repeater
  10489. (save-excursion
  10490. (org-back-to-heading t)
  10491. (when (re-search-forward (concat org-deadline-string " "
  10492. org-last-inserted-timestamp)
  10493. (save-excursion
  10494. (outline-next-heading) (point)) t)
  10495. (goto-char (1- (match-end 0)))
  10496. (insert " " repeater)
  10497. (setq org-last-inserted-timestamp
  10498. (concat (substring org-last-inserted-timestamp 0 -1)
  10499. " " repeater
  10500. (substring org-last-inserted-timestamp -1))))))
  10501. (message "Deadline on %s" org-last-inserted-timestamp))))
  10502. (defun org-schedule (&optional remove time)
  10503. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10504. With argument REMOVE, remove any scheduling date from the item.
  10505. When TIME is set, it should be an internal time specification, and the
  10506. scheduling will use the corresponding date."
  10507. (interactive "P")
  10508. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10509. (repeater (and old-date
  10510. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10511. (match-string 1 old-date))))
  10512. (if remove
  10513. (progn
  10514. (when (and old-date org-log-reschedule)
  10515. (org-add-log-setup 'delschedule nil old-date 'findpos
  10516. org-log-reschedule))
  10517. (org-remove-timestamp-with-keyword org-scheduled-string)
  10518. (message "Item is no longer scheduled."))
  10519. (org-add-planning-info 'scheduled time 'closed)
  10520. (when (and old-date org-log-reschedule
  10521. (not (equal old-date
  10522. (substring org-last-inserted-timestamp 1 -1))))
  10523. (org-add-log-setup 'reschedule nil old-date 'findpos
  10524. org-log-reschedule))
  10525. (when repeater
  10526. (save-excursion
  10527. (org-back-to-heading t)
  10528. (when (re-search-forward (concat org-scheduled-string " "
  10529. org-last-inserted-timestamp)
  10530. (save-excursion
  10531. (outline-next-heading) (point)) t)
  10532. (goto-char (1- (match-end 0)))
  10533. (insert " " repeater)
  10534. (setq org-last-inserted-timestamp
  10535. (concat (substring org-last-inserted-timestamp 0 -1)
  10536. " " repeater
  10537. (substring org-last-inserted-timestamp -1))))))
  10538. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10539. (defun org-get-scheduled-time (pom &optional inherit)
  10540. "Get the scheduled time as a time tuple, of a format suitable
  10541. for calling org-schedule with, or if there is no scheduling,
  10542. returns nil."
  10543. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10544. (when time
  10545. (apply 'encode-time (org-parse-time-string time)))))
  10546. (defun org-get-deadline-time (pom &optional inherit)
  10547. "Get the deadline as a time tuple, of a format suitable for
  10548. calling org-deadline with, or if there is no scheduling, returns
  10549. nil."
  10550. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10551. (when time
  10552. (apply 'encode-time (org-parse-time-string time)))))
  10553. (defun org-remove-timestamp-with-keyword (keyword)
  10554. "Remove all time stamps with KEYWORD in the current entry."
  10555. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10556. beg)
  10557. (save-excursion
  10558. (org-back-to-heading t)
  10559. (setq beg (point))
  10560. (outline-next-heading)
  10561. (while (re-search-backward re beg t)
  10562. (replace-match "")
  10563. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10564. (equal (char-before) ?\ ))
  10565. (backward-delete-char 1)
  10566. (if (string-match "^[ \t]*$" (buffer-substring
  10567. (point-at-bol) (point-at-eol)))
  10568. (delete-region (point-at-bol)
  10569. (min (point-max) (1+ (point-at-eol))))))))))
  10570. (defun org-add-planning-info (what &optional time &rest remove)
  10571. "Insert new timestamp with keyword in the line directly after the headline.
  10572. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10573. If non is given, the user is prompted for a date.
  10574. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10575. be removed."
  10576. (interactive)
  10577. (let (org-time-was-given org-end-time-was-given ts
  10578. end default-time default-input)
  10579. (catch 'exit
  10580. (when (and (not time) (memq what '(scheduled deadline)))
  10581. ;; Try to get a default date/time from existing timestamp
  10582. (save-excursion
  10583. (org-back-to-heading t)
  10584. (setq end (save-excursion (outline-next-heading) (point)))
  10585. (when (re-search-forward (if (eq what 'scheduled)
  10586. org-scheduled-time-regexp
  10587. org-deadline-time-regexp)
  10588. end t)
  10589. (setq ts (match-string 1)
  10590. default-time
  10591. (apply 'encode-time (org-parse-time-string ts))
  10592. default-input (and ts (org-get-compact-tod ts))))))
  10593. (when what
  10594. ;; If necessary, get the time from the user
  10595. (setq time (or time (org-read-date nil 'to-time nil nil
  10596. default-time default-input))))
  10597. (when (and org-insert-labeled-timestamps-at-point
  10598. (member what '(scheduled deadline)))
  10599. (insert
  10600. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10601. (org-insert-time-stamp time org-time-was-given
  10602. nil nil nil (list org-end-time-was-given))
  10603. (setq what nil))
  10604. (save-excursion
  10605. (save-restriction
  10606. (let (col list elt ts buffer-invisibility-spec)
  10607. (org-back-to-heading t)
  10608. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10609. (goto-char (match-end 1))
  10610. (setq col (current-column))
  10611. (goto-char (match-end 0))
  10612. (if (eobp) (insert "\n") (forward-char 1))
  10613. (when (and (not what)
  10614. (not (looking-at
  10615. (concat "[ \t]*"
  10616. org-keyword-time-not-clock-regexp))))
  10617. ;; Nothing to add, nothing to remove...... :-)
  10618. (throw 'exit nil))
  10619. (if (and (not (looking-at outline-regexp))
  10620. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10621. "[^\r\n]*"))
  10622. (not (equal (match-string 1) org-clock-string)))
  10623. (narrow-to-region (match-beginning 0) (match-end 0))
  10624. (insert-before-markers "\n")
  10625. (backward-char 1)
  10626. (narrow-to-region (point) (point))
  10627. (and org-adapt-indentation (org-indent-to-column col)))
  10628. ;; Check if we have to remove something.
  10629. (setq list (cons what remove))
  10630. (while list
  10631. (setq elt (pop list))
  10632. (goto-char (point-min))
  10633. (when (or (and (eq elt 'scheduled)
  10634. (re-search-forward org-scheduled-time-regexp nil t))
  10635. (and (eq elt 'deadline)
  10636. (re-search-forward org-deadline-time-regexp nil t))
  10637. (and (eq elt 'closed)
  10638. (re-search-forward org-closed-time-regexp nil t)))
  10639. (replace-match "")
  10640. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10641. (skip-chars-backward " ")
  10642. (if (looking-at " +") (replace-match ""))))
  10643. (goto-char (point-max))
  10644. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10645. (when what
  10646. (insert
  10647. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10648. (cond ((eq what 'scheduled) org-scheduled-string)
  10649. ((eq what 'deadline) org-deadline-string)
  10650. ((eq what 'closed) org-closed-string))
  10651. " ")
  10652. (setq ts (org-insert-time-stamp
  10653. time
  10654. (or org-time-was-given
  10655. (and (eq what 'closed) org-log-done-with-time))
  10656. (eq what 'closed)
  10657. nil nil (list org-end-time-was-given)))
  10658. (end-of-line 1))
  10659. (goto-char (point-min))
  10660. (widen)
  10661. (if (and (looking-at "[ \t]*\n")
  10662. (equal (char-before) ?\n))
  10663. (delete-region (1- (point)) (point-at-eol)))
  10664. ts))))))
  10665. (defvar org-log-note-marker (make-marker))
  10666. (defvar org-log-note-purpose nil)
  10667. (defvar org-log-note-state nil)
  10668. (defvar org-log-note-previous-state nil)
  10669. (defvar org-log-note-how nil)
  10670. (defvar org-log-note-extra nil)
  10671. (defvar org-log-note-window-configuration nil)
  10672. (defvar org-log-note-return-to (make-marker))
  10673. (defvar org-log-post-message nil
  10674. "Message to be displayed after a log note has been stored.
  10675. The auto-repeater uses this.")
  10676. (defun org-add-note ()
  10677. "Add a note to the current entry.
  10678. This is done in the same way as adding a state change note."
  10679. (interactive)
  10680. (org-add-log-setup 'note nil nil 'findpos nil))
  10681. (defvar org-property-end-re)
  10682. (defun org-add-log-setup (&optional purpose state prev-state
  10683. findpos how &optional extra)
  10684. "Set up the post command hook to take a note.
  10685. If this is about to TODO state change, the new state is expected in STATE.
  10686. When FINDPOS is non-nil, find the correct position for the note in
  10687. the current entry. If not, assume that it can be inserted at point.
  10688. HOW is an indicator what kind of note should be created.
  10689. EXTRA is additional text that will be inserted into the notes buffer."
  10690. (let* ((org-log-into-drawer (org-log-into-drawer))
  10691. (drawer (cond ((stringp org-log-into-drawer)
  10692. org-log-into-drawer)
  10693. (org-log-into-drawer "LOGBOOK")
  10694. (t nil))))
  10695. (save-restriction
  10696. (save-excursion
  10697. (when findpos
  10698. (org-back-to-heading t)
  10699. (narrow-to-region (point) (save-excursion
  10700. (outline-next-heading) (point)))
  10701. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10702. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10703. "[^\r\n]*\\)?"))
  10704. (goto-char (match-end 0))
  10705. (cond
  10706. (drawer
  10707. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10708. nil t)
  10709. (progn
  10710. (goto-char (match-end 0))
  10711. (or org-log-states-order-reversed
  10712. (and (re-search-forward org-property-end-re nil t)
  10713. (goto-char (1- (match-beginning 0))))))
  10714. (insert "\n:" drawer ":\n:END:")
  10715. (beginning-of-line 0)
  10716. (org-indent-line-function)
  10717. (beginning-of-line 2)
  10718. (org-indent-line-function)
  10719. (end-of-line 0)))
  10720. ((and org-log-state-notes-insert-after-drawers
  10721. (save-excursion
  10722. (forward-line) (looking-at org-drawer-regexp)))
  10723. (forward-line)
  10724. (while (looking-at org-drawer-regexp)
  10725. (goto-char (match-end 0))
  10726. (re-search-forward org-property-end-re (point-max) t)
  10727. (forward-line))
  10728. (forward-line -1)))
  10729. (unless org-log-states-order-reversed
  10730. (and (= (char-after) ?\n) (forward-char 1))
  10731. (org-skip-over-state-notes)
  10732. (skip-chars-backward " \t\n\r")))
  10733. (move-marker org-log-note-marker (point))
  10734. (setq org-log-note-purpose purpose
  10735. org-log-note-state state
  10736. org-log-note-previous-state prev-state
  10737. org-log-note-how how
  10738. org-log-note-extra extra)
  10739. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10740. (defun org-skip-over-state-notes ()
  10741. "Skip past the list of State notes in an entry."
  10742. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10743. (while (looking-at "[ \t]*- State")
  10744. (condition-case nil
  10745. (org-next-item)
  10746. (error (org-end-of-item)))))
  10747. (defun org-add-log-note (&optional purpose)
  10748. "Pop up a window for taking a note, and add this note later at point."
  10749. (remove-hook 'post-command-hook 'org-add-log-note)
  10750. (setq org-log-note-window-configuration (current-window-configuration))
  10751. (delete-other-windows)
  10752. (move-marker org-log-note-return-to (point))
  10753. (switch-to-buffer (marker-buffer org-log-note-marker))
  10754. (goto-char org-log-note-marker)
  10755. (org-switch-to-buffer-other-window "*Org Note*")
  10756. (erase-buffer)
  10757. (if (memq org-log-note-how '(time state))
  10758. (let (current-prefix-arg) (org-store-log-note))
  10759. (let ((org-inhibit-startup t)) (org-mode))
  10760. (insert (format "# Insert note for %s.
  10761. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10762. (cond
  10763. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10764. ((eq org-log-note-purpose 'done) "closed todo item")
  10765. ((eq org-log-note-purpose 'state)
  10766. (format "state change from \"%s\" to \"%s\""
  10767. (or org-log-note-previous-state "")
  10768. (or org-log-note-state "")))
  10769. ((eq org-log-note-purpose 'reschedule)
  10770. "rescheduling")
  10771. ((eq org-log-note-purpose 'delschedule)
  10772. "no longer scheduled")
  10773. ((eq org-log-note-purpose 'redeadline)
  10774. "changing deadline")
  10775. ((eq org-log-note-purpose 'deldeadline)
  10776. "removing deadline")
  10777. ((eq org-log-note-purpose 'refile)
  10778. "refiling")
  10779. ((eq org-log-note-purpose 'note)
  10780. "this entry")
  10781. (t (error "This should not happen")))))
  10782. (if org-log-note-extra (insert org-log-note-extra))
  10783. (org-set-local 'org-finish-function 'org-store-log-note)))
  10784. (defvar org-note-abort nil) ; dynamically scoped
  10785. (defun org-store-log-note ()
  10786. "Finish taking a log note, and insert it to where it belongs."
  10787. (let ((txt (buffer-string))
  10788. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10789. lines ind)
  10790. (kill-buffer (current-buffer))
  10791. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10792. (setq txt (replace-match "" t t txt)))
  10793. (if (string-match "\\s-+\\'" txt)
  10794. (setq txt (replace-match "" t t txt)))
  10795. (setq lines (org-split-string txt "\n"))
  10796. (when (and note (string-match "\\S-" note))
  10797. (setq note
  10798. (org-replace-escapes
  10799. note
  10800. (list (cons "%u" (user-login-name))
  10801. (cons "%U" user-full-name)
  10802. (cons "%t" (format-time-string
  10803. (org-time-stamp-format 'long 'inactive)
  10804. (current-time)))
  10805. (cons "%T" (format-time-string
  10806. (org-time-stamp-format 'long nil)
  10807. (current-time)))
  10808. (cons "%s" (if org-log-note-state
  10809. (concat "\"" org-log-note-state "\"")
  10810. ""))
  10811. (cons "%S" (if org-log-note-previous-state
  10812. (concat "\"" org-log-note-previous-state "\"")
  10813. "\"\"")))))
  10814. (if lines (setq note (concat note " \\\\")))
  10815. (push note lines))
  10816. (when (or current-prefix-arg org-note-abort)
  10817. (when org-log-into-drawer
  10818. (org-remove-empty-drawer-at
  10819. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10820. org-log-note-marker))
  10821. (setq lines nil))
  10822. (when lines
  10823. (with-current-buffer (marker-buffer org-log-note-marker)
  10824. (save-excursion
  10825. (goto-char org-log-note-marker)
  10826. (move-marker org-log-note-marker nil)
  10827. (end-of-line 1)
  10828. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10829. (insert "- " (pop lines))
  10830. (org-indent-line-function)
  10831. (beginning-of-line 1)
  10832. (looking-at "[ \t]*")
  10833. (setq ind (concat (match-string 0) " "))
  10834. (end-of-line 1)
  10835. (while lines (insert "\n" ind (pop lines)))
  10836. (message "Note stored")
  10837. (org-back-to-heading t)
  10838. (org-cycle-hide-drawers 'children)))))
  10839. (set-window-configuration org-log-note-window-configuration)
  10840. (with-current-buffer (marker-buffer org-log-note-return-to)
  10841. (goto-char org-log-note-return-to))
  10842. (move-marker org-log-note-return-to nil)
  10843. (and org-log-post-message (message "%s" org-log-post-message)))
  10844. (defun org-remove-empty-drawer-at (drawer pos)
  10845. "Remove an empty drawer DRAWER at position POS.
  10846. POS may also be a marker."
  10847. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10848. (save-excursion
  10849. (save-restriction
  10850. (widen)
  10851. (goto-char pos)
  10852. (if (org-in-regexp
  10853. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10854. (replace-match ""))))))
  10855. (defun org-sparse-tree (&optional arg)
  10856. "Create a sparse tree, prompt for the details.
  10857. This command can create sparse trees. You first need to select the type
  10858. of match used to create the tree:
  10859. t Show all TODO entries.
  10860. T Show entries with a specific TODO keyword.
  10861. m Show entries selected by a tags/property match.
  10862. p Enter a property name and its value (both with completion on existing
  10863. names/values) and show entries with that property.
  10864. / Show entries matching a regular expression (`r' can be used as well)
  10865. d Show deadlines due within `org-deadline-warning-days'.
  10866. b Show deadlines and scheduled items before a date.
  10867. a Show deadlines and scheduled items after a date."
  10868. (interactive "P")
  10869. (let (ans kwd value)
  10870. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10871. (setq ans (read-char-exclusive))
  10872. (cond
  10873. ((equal ans ?d)
  10874. (call-interactively 'org-check-deadlines))
  10875. ((equal ans ?b)
  10876. (call-interactively 'org-check-before-date))
  10877. ((equal ans ?a)
  10878. (call-interactively 'org-check-after-date))
  10879. ((equal ans ?t)
  10880. (org-show-todo-tree nil))
  10881. ((equal ans ?T)
  10882. (org-show-todo-tree '(4)))
  10883. ((member ans '(?T ?m))
  10884. (call-interactively 'org-match-sparse-tree))
  10885. ((member ans '(?p ?P))
  10886. (setq kwd (org-icompleting-read "Property: "
  10887. (mapcar 'list (org-buffer-property-keys))))
  10888. (setq value (org-icompleting-read "Value: "
  10889. (mapcar 'list (org-property-values kwd))))
  10890. (unless (string-match "\\`{.*}\\'" value)
  10891. (setq value (concat "\"" value "\"")))
  10892. (org-match-sparse-tree arg (concat kwd "=" value)))
  10893. ((member ans '(?r ?R ?/))
  10894. (call-interactively 'org-occur))
  10895. (t (error "No such sparse tree command \"%c\"" ans)))))
  10896. (defvar org-occur-highlights nil
  10897. "List of overlays used for occur matches.")
  10898. (make-variable-buffer-local 'org-occur-highlights)
  10899. (defvar org-occur-parameters nil
  10900. "Parameters of the active org-occur calls.
  10901. This is a list, each call to org-occur pushes as cons cell,
  10902. containing the regular expression and the callback, onto the list.
  10903. The list can contain several entries if `org-occur' has been called
  10904. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10905. will only contain one set of parameters. When the highlights are
  10906. removed (for example with `C-c C-c', or with the next edit (depending
  10907. on `org-remove-highlights-with-change'), this variable is emptied
  10908. as well.")
  10909. (make-variable-buffer-local 'org-occur-parameters)
  10910. (defun org-occur (regexp &optional keep-previous callback)
  10911. "Make a compact tree which shows all matches of REGEXP.
  10912. The tree will show the lines where the regexp matches, and all higher
  10913. headlines above the match. It will also show the heading after the match,
  10914. to make sure editing the matching entry is easy.
  10915. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10916. call to `org-occur' will be kept, to allow stacking of calls to this
  10917. command.
  10918. If CALLBACK is non-nil, it is a function which is called to confirm
  10919. that the match should indeed be shown."
  10920. (interactive "sRegexp: \nP")
  10921. (when (equal regexp "")
  10922. (error "Regexp cannot be empty"))
  10923. (unless keep-previous
  10924. (org-remove-occur-highlights nil nil t))
  10925. (push (cons regexp callback) org-occur-parameters)
  10926. (let ((cnt 0))
  10927. (save-excursion
  10928. (goto-char (point-min))
  10929. (if (or (not keep-previous) ; do not want to keep
  10930. (not org-occur-highlights)) ; no previous matches
  10931. ;; hide everything
  10932. (org-overview))
  10933. (while (re-search-forward regexp nil t)
  10934. (when (or (not callback)
  10935. (save-match-data (funcall callback)))
  10936. (setq cnt (1+ cnt))
  10937. (when org-highlight-sparse-tree-matches
  10938. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10939. (org-show-context 'occur-tree))))
  10940. (when org-remove-highlights-with-change
  10941. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10942. nil 'local))
  10943. (unless org-sparse-tree-open-archived-trees
  10944. (org-hide-archived-subtrees (point-min) (point-max)))
  10945. (run-hooks 'org-occur-hook)
  10946. (if (interactive-p)
  10947. (message "%d match(es) for regexp %s" cnt regexp))
  10948. cnt))
  10949. (defun org-show-context (&optional key)
  10950. "Make sure point and context are visible.
  10951. How much context is shown depends upon the variables
  10952. `org-show-hierarchy-above', `org-show-following-heading'. and
  10953. `org-show-siblings'."
  10954. (let ((heading-p (org-on-heading-p t))
  10955. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10956. (following-p (org-get-alist-option org-show-following-heading key))
  10957. (entry-p (org-get-alist-option org-show-entry-below key))
  10958. (siblings-p (org-get-alist-option org-show-siblings key)))
  10959. (catch 'exit
  10960. ;; Show heading or entry text
  10961. (if (and heading-p (not entry-p))
  10962. (org-flag-heading nil) ; only show the heading
  10963. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10964. (org-show-hidden-entry))) ; show entire entry
  10965. (when following-p
  10966. ;; Show next sibling, or heading below text
  10967. (save-excursion
  10968. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10969. (org-flag-heading nil))))
  10970. (when siblings-p (org-show-siblings))
  10971. (when hierarchy-p
  10972. ;; show all higher headings, possibly with siblings
  10973. (save-excursion
  10974. (while (and (condition-case nil
  10975. (progn (org-up-heading-all 1) t)
  10976. (error nil))
  10977. (not (bobp)))
  10978. (org-flag-heading nil)
  10979. (when siblings-p (org-show-siblings))))))))
  10980. (defvar org-reveal-start-hook nil
  10981. "Hook run before revealing a location.")
  10982. (defun org-reveal (&optional siblings)
  10983. "Show current entry, hierarchy above it, and the following headline.
  10984. This can be used to show a consistent set of context around locations
  10985. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10986. not t for the search context.
  10987. With optional argument SIBLINGS, on each level of the hierarchy all
  10988. siblings are shown. This repairs the tree structure to what it would
  10989. look like when opened with hierarchical calls to `org-cycle'.
  10990. With double optional argument \\[universal-argument] \\[universal-argument], \
  10991. go to the parent and show the
  10992. entire tree."
  10993. (interactive "P")
  10994. (run-hooks 'org-reveal-start-hook)
  10995. (let ((org-show-hierarchy-above t)
  10996. (org-show-following-heading t)
  10997. (org-show-siblings (if siblings t org-show-siblings)))
  10998. (org-show-context nil))
  10999. (when (equal siblings '(16))
  11000. (save-excursion
  11001. (when (org-up-heading-safe)
  11002. (org-show-subtree)
  11003. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11004. (defun org-highlight-new-match (beg end)
  11005. "Highlight from BEG to END and mark the highlight is an occur headline."
  11006. (let ((ov (make-overlay beg end)))
  11007. (overlay-put ov 'face 'secondary-selection)
  11008. (push ov org-occur-highlights)))
  11009. (defun org-remove-occur-highlights (&optional beg end noremove)
  11010. "Remove the occur highlights from the buffer.
  11011. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11012. from the `before-change-functions' in the current buffer."
  11013. (interactive)
  11014. (unless org-inhibit-highlight-removal
  11015. (mapc 'delete-overlay org-occur-highlights)
  11016. (setq org-occur-highlights nil)
  11017. (setq org-occur-parameters nil)
  11018. (unless noremove
  11019. (remove-hook 'before-change-functions
  11020. 'org-remove-occur-highlights 'local))))
  11021. ;;;; Priorities
  11022. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11023. "Regular expression matching the priority indicator.")
  11024. (defvar org-remove-priority-next-time nil)
  11025. (defun org-priority-up ()
  11026. "Increase the priority of the current item."
  11027. (interactive)
  11028. (org-priority 'up))
  11029. (defun org-priority-down ()
  11030. "Decrease the priority of the current item."
  11031. (interactive)
  11032. (org-priority 'down))
  11033. (defun org-priority (&optional action)
  11034. "Change the priority of an item by ARG.
  11035. ACTION can be `set', `up', `down', or a character."
  11036. (interactive)
  11037. (unless org-enable-priority-commands
  11038. (error "Priority commands are disabled"))
  11039. (setq action (or action 'set))
  11040. (let (current new news have remove)
  11041. (save-excursion
  11042. (org-back-to-heading t)
  11043. (if (looking-at org-priority-regexp)
  11044. (setq current (string-to-char (match-string 2))
  11045. have t)
  11046. (setq current org-default-priority))
  11047. (cond
  11048. ((eq action 'remove)
  11049. (setq remove t new ?\ ))
  11050. ((or (eq action 'set)
  11051. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11052. (if (not (eq action 'set))
  11053. (setq new action)
  11054. (message "Priority %c-%c, SPC to remove: "
  11055. org-highest-priority org-lowest-priority)
  11056. (setq new (read-char-exclusive)))
  11057. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11058. (= (upcase org-lowest-priority) org-lowest-priority))
  11059. (setq new (upcase new)))
  11060. (cond ((equal new ?\ ) (setq remove t))
  11061. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11062. (error "Priority must be between `%c' and `%c'"
  11063. org-highest-priority org-lowest-priority))))
  11064. ((eq action 'up)
  11065. (if (and (not have) (eq last-command this-command))
  11066. (setq new org-lowest-priority)
  11067. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11068. org-default-priority (1- current)))))
  11069. ((eq action 'down)
  11070. (if (and (not have) (eq last-command this-command))
  11071. (setq new org-highest-priority)
  11072. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11073. org-default-priority (1+ current)))))
  11074. (t (error "Invalid action")))
  11075. (if (or (< (upcase new) org-highest-priority)
  11076. (> (upcase new) org-lowest-priority))
  11077. (setq remove t))
  11078. (setq news (format "%c" new))
  11079. (if have
  11080. (if remove
  11081. (replace-match "" t t nil 1)
  11082. (replace-match news t t nil 2))
  11083. (if remove
  11084. (error "No priority cookie found in line")
  11085. (let ((case-fold-search nil))
  11086. (looking-at org-todo-line-regexp))
  11087. (if (match-end 2)
  11088. (progn
  11089. (goto-char (match-end 2))
  11090. (insert " [#" news "]"))
  11091. (goto-char (match-beginning 3))
  11092. (insert "[#" news "] "))))
  11093. (org-preserve-lc (org-set-tags nil 'align)))
  11094. (if remove
  11095. (message "Priority removed")
  11096. (message "Priority of current item set to %s" news))))
  11097. (defun org-get-priority (s)
  11098. "Find priority cookie and return priority."
  11099. (save-match-data
  11100. (if (not (string-match org-priority-regexp s))
  11101. (* 1000 (- org-lowest-priority org-default-priority))
  11102. (* 1000 (- org-lowest-priority
  11103. (string-to-char (match-string 2 s)))))))
  11104. ;;;; Tags
  11105. (defvar org-agenda-archives-mode)
  11106. (defvar org-map-continue-from nil
  11107. "Position from where mapping should continue.
  11108. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11109. (defvar org-scanner-tags nil
  11110. "The current tag list while the tags scanner is running.")
  11111. (defvar org-trust-scanner-tags nil
  11112. "Should `org-get-tags-at' use the tags fro the scanner.
  11113. This is for internal dynamical scoping only.
  11114. When this is non-nil, the function `org-get-tags-at' will return the value
  11115. of `org-scanner-tags' instead of building the list by itself. This
  11116. can lead to large speed-ups when the tags scanner is used in a file with
  11117. many entries, and when the list of tags is retrieved, for example to
  11118. obtain a list of properties. Building the tags list for each entry in such
  11119. a file becomes an N^2 operation - but with this variable set, it scales
  11120. as N.")
  11121. (defun org-scan-tags (action matcher &optional todo-only)
  11122. "Scan headline tags with inheritance and produce output ACTION.
  11123. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11124. or `agenda' to produce an entry list for an agenda view. It can also be
  11125. a Lisp form or a function that should be called at each matched headline, in
  11126. this case the return value is a list of all return values from these calls.
  11127. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11128. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11129. only lines with a TODO keyword are included in the output."
  11130. (require 'org-agenda)
  11131. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11132. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11133. (org-re
  11134. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  11135. (props (list 'face 'default
  11136. 'done-face 'org-agenda-done
  11137. 'undone-face 'default
  11138. 'mouse-face 'highlight
  11139. 'org-not-done-regexp org-not-done-regexp
  11140. 'org-todo-regexp org-todo-regexp
  11141. 'help-echo
  11142. (format "mouse-2 or RET jump to org file %s"
  11143. (abbreviate-file-name
  11144. (or (buffer-file-name (buffer-base-buffer))
  11145. (buffer-name (buffer-base-buffer)))))))
  11146. (case-fold-search nil)
  11147. (org-map-continue-from nil)
  11148. lspos tags tags-list
  11149. (tags-alist (list (cons 0 org-file-tags)))
  11150. (llast 0) rtn rtn1 level category i txt
  11151. todo marker entry priority)
  11152. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11153. (setq action (list 'lambda nil action)))
  11154. (save-excursion
  11155. (goto-char (point-min))
  11156. (when (eq action 'sparse-tree)
  11157. (org-overview)
  11158. (org-remove-occur-highlights))
  11159. (while (re-search-forward re nil t)
  11160. (catch :skip
  11161. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11162. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11163. (goto-char (setq lspos (match-beginning 0)))
  11164. (setq level (org-reduced-level (funcall outline-level))
  11165. category (org-get-category))
  11166. (setq i llast llast level)
  11167. ;; remove tag lists from same and sublevels
  11168. (while (>= i level)
  11169. (when (setq entry (assoc i tags-alist))
  11170. (setq tags-alist (delete entry tags-alist)))
  11171. (setq i (1- i)))
  11172. ;; add the next tags
  11173. (when tags
  11174. (setq tags (org-split-string tags ":")
  11175. tags-alist
  11176. (cons (cons level tags) tags-alist)))
  11177. ;; compile tags for current headline
  11178. (setq tags-list
  11179. (if org-use-tag-inheritance
  11180. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11181. tags)
  11182. org-scanner-tags tags-list)
  11183. (when org-use-tag-inheritance
  11184. (setcdr (car tags-alist)
  11185. (mapcar (lambda (x)
  11186. (setq x (copy-sequence x))
  11187. (org-add-prop-inherited x))
  11188. (cdar tags-alist))))
  11189. (when (and tags org-use-tag-inheritance
  11190. (or (not (eq t org-use-tag-inheritance))
  11191. org-tags-exclude-from-inheritance))
  11192. ;; selective inheritance, remove uninherited ones
  11193. (setcdr (car tags-alist)
  11194. (org-remove-uniherited-tags (cdar tags-alist))))
  11195. (when (and (or (not todo-only)
  11196. (and (member todo org-not-done-keywords)
  11197. (or (not org-agenda-tags-todo-honor-ignore-options)
  11198. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11199. (let ((case-fold-search t)) (eval matcher))
  11200. (or
  11201. (not (member org-archive-tag tags-list))
  11202. ;; we have an archive tag, should we use this anyway?
  11203. (or (not org-agenda-skip-archived-trees)
  11204. (and (eq action 'agenda) org-agenda-archives-mode))))
  11205. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11206. ;; select this headline
  11207. (cond
  11208. ((eq action 'sparse-tree)
  11209. (and org-highlight-sparse-tree-matches
  11210. (org-get-heading) (match-end 0)
  11211. (org-highlight-new-match
  11212. (match-beginning 0) (match-beginning 1)))
  11213. (org-show-context 'tags-tree))
  11214. ((eq action 'agenda)
  11215. (setq txt (org-format-agenda-item
  11216. ""
  11217. (concat
  11218. (if (eq org-tags-match-list-sublevels 'indented)
  11219. (make-string (1- level) ?.) "")
  11220. (org-get-heading))
  11221. category
  11222. tags-list
  11223. )
  11224. priority (org-get-priority txt))
  11225. (goto-char lspos)
  11226. (setq marker (org-agenda-new-marker))
  11227. (org-add-props txt props
  11228. 'org-marker marker 'org-hd-marker marker 'org-category category
  11229. 'todo-state todo
  11230. 'priority priority 'type "tagsmatch")
  11231. (push txt rtn))
  11232. ((functionp action)
  11233. (setq org-map-continue-from nil)
  11234. (save-excursion
  11235. (setq rtn1 (funcall action))
  11236. (push rtn1 rtn)))
  11237. (t (error "Invalid action")))
  11238. ;; if we are to skip sublevels, jump to end of subtree
  11239. (unless org-tags-match-list-sublevels
  11240. (org-end-of-subtree t)
  11241. (backward-char 1))))
  11242. ;; Get the correct position from where to continue
  11243. (if org-map-continue-from
  11244. (goto-char org-map-continue-from)
  11245. (and (= (point) lspos) (end-of-line 1)))))
  11246. (when (and (eq action 'sparse-tree)
  11247. (not org-sparse-tree-open-archived-trees))
  11248. (org-hide-archived-subtrees (point-min) (point-max)))
  11249. (nreverse rtn)))
  11250. (defun org-remove-uniherited-tags (tags)
  11251. "Remove all tags that are not inherited from the list TAGS."
  11252. (cond
  11253. ((eq org-use-tag-inheritance t)
  11254. (if org-tags-exclude-from-inheritance
  11255. (org-delete-all org-tags-exclude-from-inheritance tags)
  11256. tags))
  11257. ((not org-use-tag-inheritance) nil)
  11258. ((stringp org-use-tag-inheritance)
  11259. (delq nil (mapcar
  11260. (lambda (x)
  11261. (if (and (string-match org-use-tag-inheritance x)
  11262. (not (member x org-tags-exclude-from-inheritance)))
  11263. x nil))
  11264. tags)))
  11265. ((listp org-use-tag-inheritance)
  11266. (delq nil (mapcar
  11267. (lambda (x)
  11268. (if (member x org-use-tag-inheritance) x nil))
  11269. tags)))))
  11270. (defvar todo-only) ;; dynamically scoped
  11271. (defun org-match-sparse-tree (&optional todo-only match)
  11272. "Create a sparse tree according to tags string MATCH.
  11273. MATCH can contain positive and negative selection of tags, like
  11274. \"+WORK+URGENT-WITHBOSS\".
  11275. If optional argument TODO-ONLY is non-nil, only select lines that are
  11276. also TODO lines."
  11277. (interactive "P")
  11278. (org-prepare-agenda-buffers (list (current-buffer)))
  11279. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11280. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11281. (defvar org-cached-props nil)
  11282. (defun org-cached-entry-get (pom property)
  11283. (if (or (eq t org-use-property-inheritance)
  11284. (and (stringp org-use-property-inheritance)
  11285. (string-match org-use-property-inheritance property))
  11286. (and (listp org-use-property-inheritance)
  11287. (member property org-use-property-inheritance)))
  11288. ;; Caching is not possible, check it directly
  11289. (org-entry-get pom property 'inherit)
  11290. ;; Get all properties, so that we can do complicated checks easily
  11291. (cdr (assoc property (or org-cached-props
  11292. (setq org-cached-props
  11293. (org-entry-properties pom)))))))
  11294. (defun org-global-tags-completion-table (&optional files)
  11295. "Return the list of all tags in all agenda buffer/files."
  11296. (save-excursion
  11297. (org-uniquify
  11298. (delq nil
  11299. (apply 'append
  11300. (mapcar
  11301. (lambda (file)
  11302. (set-buffer (find-file-noselect file))
  11303. (append (org-get-buffer-tags)
  11304. (mapcar (lambda (x) (if (stringp (car-safe x))
  11305. (list (car-safe x)) nil))
  11306. org-tag-alist)))
  11307. (if (and files (car files))
  11308. files
  11309. (org-agenda-files))))))))
  11310. (defun org-make-tags-matcher (match)
  11311. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11312. ;; todo-only is scoped dynamically into this function, and the function
  11313. ;; may change it if the matcher asks for it.
  11314. (unless match
  11315. ;; Get a new match request, with completion
  11316. (let ((org-last-tags-completion-table
  11317. (org-global-tags-completion-table)))
  11318. (setq match (org-completing-read-no-i
  11319. "Match: " 'org-tags-completion-function nil nil nil
  11320. 'org-tags-history))))
  11321. ;; Parse the string and create a lisp form
  11322. (let ((match0 match)
  11323. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  11324. minus tag mm
  11325. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11326. orterms term orlist re-p str-p level-p level-op time-p
  11327. prop-p pn pv po cat-p gv rest)
  11328. (if (string-match "/+" match)
  11329. ;; match contains also a todo-matching request
  11330. (progn
  11331. (setq tagsmatch (substring match 0 (match-beginning 0))
  11332. todomatch (substring match (match-end 0)))
  11333. (if (string-match "^!" todomatch)
  11334. (setq todo-only t todomatch (substring todomatch 1)))
  11335. (if (string-match "^\\s-*$" todomatch)
  11336. (setq todomatch nil)))
  11337. ;; only matching tags
  11338. (setq tagsmatch match todomatch nil))
  11339. ;; Make the tags matcher
  11340. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11341. (setq tagsmatcher t)
  11342. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11343. (while (setq term (pop orterms))
  11344. (while (and (equal (substring term -1) "\\") orterms)
  11345. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11346. (while (string-match re term)
  11347. (setq rest (substring term (match-end 0))
  11348. minus (and (match-end 1)
  11349. (equal (match-string 1 term) "-"))
  11350. tag (save-match-data (replace-regexp-in-string
  11351. "\\\\-" "-"
  11352. (match-string 2 term)))
  11353. re-p (equal (string-to-char tag) ?{)
  11354. level-p (match-end 4)
  11355. prop-p (match-end 5)
  11356. mm (cond
  11357. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11358. (level-p
  11359. (setq level-op (org-op-to-function (match-string 3 term)))
  11360. `(,level-op level ,(string-to-number
  11361. (match-string 4 term))))
  11362. (prop-p
  11363. (setq pn (match-string 5 term)
  11364. po (match-string 6 term)
  11365. pv (match-string 7 term)
  11366. cat-p (equal pn "CATEGORY")
  11367. re-p (equal (string-to-char pv) ?{)
  11368. str-p (equal (string-to-char pv) ?\")
  11369. time-p (save-match-data
  11370. (string-match "^\"[[<].*[]>]\"$" pv))
  11371. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11372. (if time-p (setq pv (org-matcher-time pv)))
  11373. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11374. (cond
  11375. ((equal pn "CATEGORY")
  11376. (setq gv '(get-text-property (point) 'org-category)))
  11377. ((equal pn "TODO")
  11378. (setq gv 'todo))
  11379. (t
  11380. (setq gv `(org-cached-entry-get nil ,pn))))
  11381. (if re-p
  11382. (if (eq po 'org<>)
  11383. `(not (string-match ,pv (or ,gv "")))
  11384. `(string-match ,pv (or ,gv "")))
  11385. (if str-p
  11386. `(,po (or ,gv "") ,pv)
  11387. `(,po (string-to-number (or ,gv ""))
  11388. ,(string-to-number pv) ))))
  11389. (t `(member ,tag tags-list)))
  11390. mm (if minus (list 'not mm) mm)
  11391. term rest)
  11392. (push mm tagsmatcher))
  11393. (push (if (> (length tagsmatcher) 1)
  11394. (cons 'and tagsmatcher)
  11395. (car tagsmatcher))
  11396. orlist)
  11397. (setq tagsmatcher nil))
  11398. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11399. (setq tagsmatcher
  11400. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11401. ;; Make the todo matcher
  11402. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11403. (setq todomatcher t)
  11404. (setq orterms (org-split-string todomatch "|") orlist nil)
  11405. (while (setq term (pop orterms))
  11406. (while (string-match re term)
  11407. (setq minus (and (match-end 1)
  11408. (equal (match-string 1 term) "-"))
  11409. kwd (match-string 2 term)
  11410. re-p (equal (string-to-char kwd) ?{)
  11411. term (substring term (match-end 0))
  11412. mm (if re-p
  11413. `(string-match ,(substring kwd 1 -1) todo)
  11414. (list 'equal 'todo kwd))
  11415. mm (if minus (list 'not mm) mm))
  11416. (push mm todomatcher))
  11417. (push (if (> (length todomatcher) 1)
  11418. (cons 'and todomatcher)
  11419. (car todomatcher))
  11420. orlist)
  11421. (setq todomatcher nil))
  11422. (setq todomatcher (if (> (length orlist) 1)
  11423. (cons 'or orlist) (car orlist))))
  11424. ;; Return the string and lisp forms of the matcher
  11425. (setq matcher (if todomatcher
  11426. (list 'and tagsmatcher todomatcher)
  11427. tagsmatcher))
  11428. (cons match0 matcher)))
  11429. (defun org-op-to-function (op &optional stringp)
  11430. "Turn an operator into the appropriate function."
  11431. (setq op
  11432. (cond
  11433. ((equal op "<" ) '(< string< org-time<))
  11434. ((equal op ">" ) '(> org-string> org-time>))
  11435. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11436. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11437. ((member op '("=" "==")) '(= string= org-time=))
  11438. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11439. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11440. (defun org<> (a b) (not (= a b)))
  11441. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11442. (defun org-string>= (a b) (not (string< a b)))
  11443. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11444. (defun org-string<> (a b) (not (string= 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) (>= a b)))
  11450. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11451. (defun org-2ft (s)
  11452. "Convert S to a floating point time.
  11453. If S is already a number, just return it. If it is a string, parse
  11454. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11455. (cond
  11456. ((numberp s) s)
  11457. ((stringp s)
  11458. (condition-case nil
  11459. (float-time (apply 'encode-time (org-parse-time-string s)))
  11460. (error 0.)))
  11461. (t 0.)))
  11462. (defun org-time-today ()
  11463. "Time in seconds today at 0:00.
  11464. Returns the float number of seconds since the beginning of the
  11465. epoch to the beginning of today (00:00)."
  11466. (float-time (apply 'encode-time
  11467. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11468. (defun org-matcher-time (s)
  11469. "Interpret a time comparison value."
  11470. (save-match-data
  11471. (cond
  11472. ((string= s "<now>") (float-time))
  11473. ((string= s "<today>") (org-time-today))
  11474. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11475. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11476. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11477. (+ (org-time-today)
  11478. (* (string-to-number (match-string 1 s))
  11479. (cdr (assoc (match-string 2 s)
  11480. '(("d" . 86400.0) ("w" . 604800.0)
  11481. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11482. (t (org-2ft s)))))
  11483. (defun org-match-any-p (re list)
  11484. "Does re match any element of list?"
  11485. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11486. (delq nil list))
  11487. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11488. (defvar org-tags-overlay (make-overlay 1 1))
  11489. (org-detach-overlay org-tags-overlay)
  11490. (defun org-get-local-tags-at (&optional pos)
  11491. "Get a list of tags defined in the current headline."
  11492. (org-get-tags-at pos 'local))
  11493. (defun org-get-local-tags ()
  11494. "Get a list of tags defined in the current headline."
  11495. (org-get-tags-at nil 'local))
  11496. (defun org-get-tags-at (&optional pos local)
  11497. "Get a list of all headline tags applicable at POS.
  11498. POS defaults to point. If tags are inherited, the list contains
  11499. the targets in the same sequence as the headlines appear, i.e.
  11500. the tags of the current headline come last.
  11501. When LOCAL is non-nil, only return tags from the current headline,
  11502. ignore inherited ones."
  11503. (interactive)
  11504. (if (and org-trust-scanner-tags
  11505. (or (not pos) (equal pos (point)))
  11506. (not local))
  11507. org-scanner-tags
  11508. (let (tags ltags lastpos parent)
  11509. (save-excursion
  11510. (save-restriction
  11511. (widen)
  11512. (goto-char (or pos (point)))
  11513. (save-match-data
  11514. (catch 'done
  11515. (condition-case nil
  11516. (progn
  11517. (org-back-to-heading t)
  11518. (while (not (equal lastpos (point)))
  11519. (setq lastpos (point))
  11520. (when (looking-at
  11521. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11522. (setq ltags (org-split-string
  11523. (org-match-string-no-properties 1) ":"))
  11524. (when parent
  11525. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11526. (setq tags (append
  11527. (if parent
  11528. (org-remove-uniherited-tags ltags)
  11529. ltags)
  11530. tags)))
  11531. (or org-use-tag-inheritance (throw 'done t))
  11532. (if local (throw 'done t))
  11533. (or (org-up-heading-safe) (error nil))
  11534. (setq parent t)))
  11535. (error nil)))))
  11536. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11537. (defun org-add-prop-inherited (s)
  11538. (add-text-properties 0 (length s) '(inherited t) s)
  11539. s)
  11540. (defun org-toggle-tag (tag &optional onoff)
  11541. "Toggle the tag TAG for the current line.
  11542. If ONOFF is `on' or `off', don't toggle but set to this state."
  11543. (let (res current)
  11544. (save-excursion
  11545. (org-back-to-heading t)
  11546. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11547. (point-at-eol) t)
  11548. (progn
  11549. (setq current (match-string 1))
  11550. (replace-match ""))
  11551. (setq current ""))
  11552. (setq current (nreverse (org-split-string current ":")))
  11553. (cond
  11554. ((eq onoff 'on)
  11555. (setq res t)
  11556. (or (member tag current) (push tag current)))
  11557. ((eq onoff 'off)
  11558. (or (not (member tag current)) (setq current (delete tag current))))
  11559. (t (if (member tag current)
  11560. (setq current (delete tag current))
  11561. (setq res t)
  11562. (push tag current))))
  11563. (end-of-line 1)
  11564. (if current
  11565. (progn
  11566. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11567. (org-set-tags nil t))
  11568. (delete-horizontal-space))
  11569. (run-hooks 'org-after-tags-change-hook))
  11570. res))
  11571. (defun org-align-tags-here (to-col)
  11572. ;; Assumes that this is a headline
  11573. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11574. (beginning-of-line 1)
  11575. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11576. (< pos (match-beginning 2)))
  11577. (progn
  11578. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11579. (goto-char (match-beginning 1))
  11580. (insert " ")
  11581. (delete-region (point) (1+ (match-beginning 2)))
  11582. (setq ncol (max (1+ (current-column))
  11583. (1+ col)
  11584. (if (> to-col 0)
  11585. to-col
  11586. (- (abs to-col) tags-l))))
  11587. (setq p (point))
  11588. (insert (make-string (- ncol (current-column)) ?\ ))
  11589. (setq ncol (current-column))
  11590. (when indent-tabs-mode (tabify p (point-at-eol)))
  11591. (org-move-to-column (min ncol col) t))
  11592. (goto-char pos))))
  11593. (defun org-set-tags-command (&optional arg just-align)
  11594. "Call the set-tags command for the current entry."
  11595. (interactive "P")
  11596. (if (org-on-heading-p)
  11597. (org-set-tags arg just-align)
  11598. (save-excursion
  11599. (org-back-to-heading t)
  11600. (org-set-tags arg just-align))))
  11601. (defun org-set-tags-to (data)
  11602. "Set the tags of the current entry to DATA, replacing the current tags.
  11603. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11604. If DATA is nil or the empty string, any tags will be removed."
  11605. (interactive "sTags: ")
  11606. (setq data
  11607. (cond
  11608. ((eq data nil) "")
  11609. ((equal data "") "")
  11610. ((stringp data)
  11611. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11612. ":"))
  11613. ((listp data)
  11614. (concat ":" (mapconcat 'identity data ":") ":"))
  11615. (t nil)))
  11616. (when data
  11617. (save-excursion
  11618. (org-back-to-heading t)
  11619. (when (looking-at org-complex-heading-regexp)
  11620. (if (match-end 5)
  11621. (progn
  11622. (goto-char (match-beginning 5))
  11623. (insert data)
  11624. (delete-region (point) (point-at-eol))
  11625. (org-set-tags nil 'align))
  11626. (goto-char (point-at-eol))
  11627. (insert " " data)
  11628. (org-set-tags nil 'align)))
  11629. (beginning-of-line 1)
  11630. (if (looking-at ".*?\\([ \t]+\\)$")
  11631. (delete-region (match-beginning 1) (match-end 1))))))
  11632. (defun org-align-all-tags ()
  11633. "Align the tags i all headings."
  11634. (interactive)
  11635. (save-excursion
  11636. (or (ignore-errors (org-back-to-heading t))
  11637. (outline-next-heading))
  11638. (if (org-on-heading-p)
  11639. (org-set-tags t)
  11640. (message "No headings"))))
  11641. (defvar org-indent-indentation-per-level)
  11642. (defun org-set-tags (&optional arg just-align)
  11643. "Set the tags for the current headline.
  11644. With prefix ARG, realign all tags in headings in the current buffer."
  11645. (interactive "P")
  11646. (let* ((re (concat "^" outline-regexp))
  11647. (current (org-get-tags-string))
  11648. (col (current-column))
  11649. (org-setting-tags t)
  11650. table current-tags inherited-tags ; computed below when needed
  11651. tags p0 c0 c1 rpl di tc level)
  11652. (if arg
  11653. (save-excursion
  11654. (goto-char (point-min))
  11655. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11656. (while (re-search-forward re nil t)
  11657. (org-set-tags nil t)
  11658. (end-of-line 1)))
  11659. (message "All tags realigned to column %d" org-tags-column))
  11660. (if just-align
  11661. (setq tags current)
  11662. ;; Get a new set of tags from the user
  11663. (save-excursion
  11664. (setq table (append org-tag-persistent-alist
  11665. (or org-tag-alist (org-get-buffer-tags))
  11666. (and org-complete-tags-always-offer-all-agenda-tags
  11667. (org-global-tags-completion-table (org-agenda-files))))
  11668. org-last-tags-completion-table table
  11669. current-tags (org-split-string current ":")
  11670. inherited-tags (nreverse
  11671. (nthcdr (length current-tags)
  11672. (nreverse (org-get-tags-at))))
  11673. tags
  11674. (if (or (eq t org-use-fast-tag-selection)
  11675. (and org-use-fast-tag-selection
  11676. (delq nil (mapcar 'cdr table))))
  11677. (org-fast-tag-selection
  11678. current-tags inherited-tags table
  11679. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11680. (let ((org-add-colon-after-tag-completion t))
  11681. (org-trim
  11682. (org-without-partial-completion
  11683. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11684. nil nil current 'org-tags-history)))))))
  11685. (while (string-match "[-+&]+" tags)
  11686. ;; No boolean logic, just a list
  11687. (setq tags (replace-match ":" t t tags))))
  11688. (if org-tags-sort-function
  11689. (setq tags (mapconcat 'identity
  11690. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11691. org-tags-sort-function) ":")))
  11692. (if (string-match "\\`[\t ]*\\'" tags)
  11693. (setq tags "")
  11694. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11695. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11696. ;; Insert new tags at the correct column
  11697. (beginning-of-line 1)
  11698. (setq level (or (and (looking-at org-outline-regexp)
  11699. (- (match-end 0) (point) 1))
  11700. 1))
  11701. (cond
  11702. ((and (equal current "") (equal tags "")))
  11703. ((re-search-forward
  11704. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11705. (point-at-eol) t)
  11706. (if (equal tags "")
  11707. (setq rpl "")
  11708. (goto-char (match-beginning 0))
  11709. (setq c0 (current-column)
  11710. ;; compute offset for the case of org-indent-mode active
  11711. di (if org-indent-mode
  11712. (* (1- org-indent-indentation-per-level) (1- level))
  11713. 0)
  11714. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11715. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11716. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11717. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11718. (replace-match rpl t t)
  11719. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11720. tags)
  11721. (t (error "Tags alignment failed")))
  11722. (org-move-to-column col)
  11723. (unless just-align
  11724. (run-hooks 'org-after-tags-change-hook)))))
  11725. (defun org-change-tag-in-region (beg end tag off)
  11726. "Add or remove TAG for each entry in the region.
  11727. This works in the agenda, and also in an org-mode buffer."
  11728. (interactive
  11729. (list (region-beginning) (region-end)
  11730. (let ((org-last-tags-completion-table
  11731. (if (org-mode-p)
  11732. (org-get-buffer-tags)
  11733. (org-global-tags-completion-table))))
  11734. (org-icompleting-read
  11735. "Tag: " 'org-tags-completion-function nil nil nil
  11736. 'org-tags-history))
  11737. (progn
  11738. (message "[s]et or [r]emove? ")
  11739. (equal (read-char-exclusive) ?r))))
  11740. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11741. (let ((agendap (equal major-mode 'org-agenda-mode))
  11742. l1 l2 m buf pos newhead (cnt 0))
  11743. (goto-char end)
  11744. (setq l2 (1- (org-current-line)))
  11745. (goto-char beg)
  11746. (setq l1 (org-current-line))
  11747. (loop for l from l1 to l2 do
  11748. (org-goto-line l)
  11749. (setq m (get-text-property (point) 'org-hd-marker))
  11750. (when (or (and (org-mode-p) (org-on-heading-p))
  11751. (and agendap m))
  11752. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11753. pos (if agendap m (point)))
  11754. (with-current-buffer buf
  11755. (save-excursion
  11756. (save-restriction
  11757. (goto-char pos)
  11758. (setq cnt (1+ cnt))
  11759. (org-toggle-tag tag (if off 'off 'on))
  11760. (setq newhead (org-get-heading)))))
  11761. (and agendap (org-agenda-change-all-lines newhead m))))
  11762. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11763. (defun org-tags-completion-function (string predicate &optional flag)
  11764. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11765. (confirm (lambda (x) (stringp (car x)))))
  11766. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11767. (setq s1 (match-string 1 string)
  11768. s2 (match-string 2 string))
  11769. (setq s1 "" s2 string))
  11770. (cond
  11771. ((eq flag nil)
  11772. ;; try completion
  11773. (setq rtn (try-completion s2 ctable confirm))
  11774. (if (stringp rtn)
  11775. (setq rtn
  11776. (concat s1 s2 (substring rtn (length s2))
  11777. (if (and org-add-colon-after-tag-completion
  11778. (assoc rtn ctable))
  11779. ":" ""))))
  11780. rtn)
  11781. ((eq flag t)
  11782. ;; all-completions
  11783. (all-completions s2 ctable confirm)
  11784. )
  11785. ((eq flag 'lambda)
  11786. ;; exact match?
  11787. (assoc s2 ctable)))
  11788. ))
  11789. (defun org-fast-tag-insert (kwd tags face &optional end)
  11790. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11791. (insert (format "%-12s" (concat kwd ":"))
  11792. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11793. (or end "")))
  11794. (defun org-fast-tag-show-exit (flag)
  11795. (save-excursion
  11796. (org-goto-line 3)
  11797. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11798. (replace-match ""))
  11799. (when flag
  11800. (end-of-line 1)
  11801. (org-move-to-column (- (window-width) 19) t)
  11802. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11803. (defun org-set-current-tags-overlay (current prefix)
  11804. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11805. (if (featurep 'xemacs)
  11806. (org-overlay-display org-tags-overlay (concat prefix s)
  11807. 'secondary-selection)
  11808. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11809. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11810. (defvar org-last-tag-selection-key nil)
  11811. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11812. "Fast tag selection with single keys.
  11813. CURRENT is the current list of tags in the headline, INHERITED is the
  11814. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11815. possibly with grouping information. TODO-TABLE is a similar table with
  11816. TODO keywords, should these have keys assigned to them.
  11817. If the keys are nil, a-z are automatically assigned.
  11818. Returns the new tags string, or nil to not change the current settings."
  11819. (let* ((fulltable (append table todo-table))
  11820. (maxlen (apply 'max (mapcar
  11821. (lambda (x)
  11822. (if (stringp (car x)) (string-width (car x)) 0))
  11823. fulltable)))
  11824. (buf (current-buffer))
  11825. (expert (eq org-fast-tag-selection-single-key 'expert))
  11826. (buffer-tags nil)
  11827. (fwidth (+ maxlen 3 1 3))
  11828. (ncol (/ (- (window-width) 4) fwidth))
  11829. (i-face 'org-done)
  11830. (c-face 'org-todo)
  11831. tg cnt e c char c1 c2 ntable tbl rtn
  11832. ov-start ov-end ov-prefix
  11833. (exit-after-next org-fast-tag-selection-single-key)
  11834. (done-keywords org-done-keywords)
  11835. groups ingroup)
  11836. (save-excursion
  11837. (beginning-of-line 1)
  11838. (if (looking-at
  11839. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11840. (setq ov-start (match-beginning 1)
  11841. ov-end (match-end 1)
  11842. ov-prefix "")
  11843. (setq ov-start (1- (point-at-eol))
  11844. ov-end (1+ ov-start))
  11845. (skip-chars-forward "^\n\r")
  11846. (setq ov-prefix
  11847. (concat
  11848. (buffer-substring (1- (point)) (point))
  11849. (if (> (current-column) org-tags-column)
  11850. " "
  11851. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11852. (move-overlay org-tags-overlay ov-start ov-end)
  11853. (save-window-excursion
  11854. (if expert
  11855. (set-buffer (get-buffer-create " *Org tags*"))
  11856. (delete-other-windows)
  11857. (split-window-vertically)
  11858. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11859. (erase-buffer)
  11860. (org-set-local 'org-done-keywords done-keywords)
  11861. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11862. (org-fast-tag-insert "Current" current c-face "\n\n")
  11863. (org-fast-tag-show-exit exit-after-next)
  11864. (org-set-current-tags-overlay current ov-prefix)
  11865. (setq tbl fulltable char ?a cnt 0)
  11866. (while (setq e (pop tbl))
  11867. (cond
  11868. ((equal (car e) :startgroup)
  11869. (push '() groups) (setq ingroup t)
  11870. (when (not (= cnt 0))
  11871. (setq cnt 0)
  11872. (insert "\n"))
  11873. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11874. ((equal (car e) :endgroup)
  11875. (setq ingroup nil cnt 0)
  11876. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11877. ((equal e '(:newline))
  11878. (when (not (= cnt 0))
  11879. (setq cnt 0)
  11880. (insert "\n")
  11881. (setq e (car tbl))
  11882. (while (equal (car tbl) '(:newline))
  11883. (insert "\n")
  11884. (setq tbl (cdr tbl)))))
  11885. (t
  11886. (setq tg (copy-sequence (car e)) c2 nil)
  11887. (if (cdr e)
  11888. (setq c (cdr e))
  11889. ;; automatically assign a character.
  11890. (setq c1 (string-to-char
  11891. (downcase (substring
  11892. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11893. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11894. (while (or (rassoc char ntable) (rassoc char table))
  11895. (setq char (1+ char)))
  11896. (setq c2 c1))
  11897. (setq c (or c2 char)))
  11898. (if ingroup (push tg (car groups)))
  11899. (setq tg (org-add-props tg nil 'face
  11900. (cond
  11901. ((not (assoc tg table))
  11902. (org-get-todo-face tg))
  11903. ((member tg current) c-face)
  11904. ((member tg inherited) i-face)
  11905. (t nil))))
  11906. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11907. (insert "[" c "] " tg (make-string
  11908. (- fwidth 4 (length tg)) ?\ ))
  11909. (push (cons tg c) ntable)
  11910. (when (= (setq cnt (1+ cnt)) ncol)
  11911. (insert "\n")
  11912. (if ingroup (insert " "))
  11913. (setq cnt 0)))))
  11914. (setq ntable (nreverse ntable))
  11915. (insert "\n")
  11916. (goto-char (point-min))
  11917. (if (not expert) (org-fit-window-to-buffer))
  11918. (setq rtn
  11919. (catch 'exit
  11920. (while t
  11921. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11922. (if (not groups) "no " "")
  11923. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11924. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11925. (setq org-last-tag-selection-key c)
  11926. (cond
  11927. ((= c ?\r) (throw 'exit t))
  11928. ((= c ?!)
  11929. (setq groups (not groups))
  11930. (goto-char (point-min))
  11931. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11932. ((= c ?\C-c)
  11933. (if (not expert)
  11934. (org-fast-tag-show-exit
  11935. (setq exit-after-next (not exit-after-next)))
  11936. (setq expert nil)
  11937. (delete-other-windows)
  11938. (split-window-vertically)
  11939. (org-switch-to-buffer-other-window " *Org tags*")
  11940. (org-fit-window-to-buffer)))
  11941. ((or (= c ?\C-g)
  11942. (and (= c ?q) (not (rassoc c ntable))))
  11943. (org-detach-overlay org-tags-overlay)
  11944. (setq quit-flag t))
  11945. ((= c ?\ )
  11946. (setq current nil)
  11947. (if exit-after-next (setq exit-after-next 'now)))
  11948. ((= c ?\t)
  11949. (condition-case nil
  11950. (setq tg (org-icompleting-read
  11951. "Tag: "
  11952. (or buffer-tags
  11953. (with-current-buffer buf
  11954. (org-get-buffer-tags)))))
  11955. (quit (setq tg "")))
  11956. (when (string-match "\\S-" tg)
  11957. (add-to-list 'buffer-tags (list tg))
  11958. (if (member tg current)
  11959. (setq current (delete tg current))
  11960. (push tg current)))
  11961. (if exit-after-next (setq exit-after-next 'now)))
  11962. ((setq e (rassoc c todo-table) tg (car e))
  11963. (with-current-buffer buf
  11964. (save-excursion (org-todo tg)))
  11965. (if exit-after-next (setq exit-after-next 'now)))
  11966. ((setq e (rassoc c ntable) tg (car e))
  11967. (if (member tg current)
  11968. (setq current (delete tg current))
  11969. (loop for g in groups do
  11970. (if (member tg g)
  11971. (mapc (lambda (x)
  11972. (setq current (delete x current)))
  11973. g)))
  11974. (push tg current))
  11975. (if exit-after-next (setq exit-after-next 'now))))
  11976. ;; Create a sorted list
  11977. (setq current
  11978. (sort current
  11979. (lambda (a b)
  11980. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11981. (if (eq exit-after-next 'now) (throw 'exit t))
  11982. (goto-char (point-min))
  11983. (beginning-of-line 2)
  11984. (delete-region (point) (point-at-eol))
  11985. (org-fast-tag-insert "Current" current c-face)
  11986. (org-set-current-tags-overlay current ov-prefix)
  11987. (while (re-search-forward
  11988. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11989. (setq tg (match-string 1))
  11990. (add-text-properties
  11991. (match-beginning 1) (match-end 1)
  11992. (list 'face
  11993. (cond
  11994. ((member tg current) c-face)
  11995. ((member tg inherited) i-face)
  11996. (t (get-text-property (match-beginning 1) 'face))))))
  11997. (goto-char (point-min)))))
  11998. (org-detach-overlay org-tags-overlay)
  11999. (if rtn
  12000. (mapconcat 'identity current ":")
  12001. nil))))
  12002. (defun org-get-tags-string ()
  12003. "Get the TAGS string in the current headline."
  12004. (unless (org-on-heading-p t)
  12005. (error "Not on a heading"))
  12006. (save-excursion
  12007. (beginning-of-line 1)
  12008. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12009. (org-match-string-no-properties 1)
  12010. "")))
  12011. (defun org-get-tags ()
  12012. "Get the list of tags specified in the current headline."
  12013. (org-split-string (org-get-tags-string) ":"))
  12014. (defun org-get-buffer-tags ()
  12015. "Get a table of all tags used in the buffer, for completion."
  12016. (let (tags)
  12017. (save-excursion
  12018. (goto-char (point-min))
  12019. (while (re-search-forward
  12020. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12021. (when (equal (char-after (point-at-bol 0)) ?*)
  12022. (mapc (lambda (x) (add-to-list 'tags x))
  12023. (org-split-string (org-match-string-no-properties 1) ":")))))
  12024. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12025. (mapcar 'list tags)))
  12026. ;;;; The mapping API
  12027. ;;;###autoload
  12028. (defun org-map-entries (func &optional match scope &rest skip)
  12029. "Call FUNC at each headline selected by MATCH in SCOPE.
  12030. FUNC is a function or a lisp form. The function will be called without
  12031. arguments, with the cursor positioned at the beginning of the headline.
  12032. The return values of all calls to the function will be collected and
  12033. returned as a list.
  12034. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12035. does not need to preserve point. After evaluation, the cursor will be
  12036. moved to the end of the line (presumably of the headline of the
  12037. processed entry) and search continues from there. Under some
  12038. circumstances, this may not produce the wanted results. For example,
  12039. if you have removed (e.g. archived) the current (sub)tree it could
  12040. mean that the next entry will be skipped entirely. In such cases, you
  12041. can specify the position from where search should continue by making
  12042. FUNC set the variable `org-map-continue-from' to the desired buffer
  12043. position.
  12044. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12045. Only headlines that are matched by this query will be considered during
  12046. the iteration. When MATCH is nil or t, all headlines will be
  12047. visited by the iteration.
  12048. SCOPE determines the scope of this command. It can be any of:
  12049. nil The current buffer, respecting the restriction if any
  12050. tree The subtree started with the entry at point
  12051. file The current buffer, without restriction
  12052. file-with-archives
  12053. The current buffer, and any archives associated with it
  12054. agenda All agenda files
  12055. agenda-with-archives
  12056. All agenda files with any archive files associated with them
  12057. \(file1 file2 ...)
  12058. If this is a list, all files in the list will be scanned
  12059. The remaining args are treated as settings for the skipping facilities of
  12060. the scanner. The following items can be given here:
  12061. archive skip trees with the archive tag.
  12062. comment skip trees with the COMMENT keyword
  12063. function or Emacs Lisp form:
  12064. will be used as value for `org-agenda-skip-function', so whenever
  12065. the function returns t, FUNC will not be called for that
  12066. entry and search will continue from the point where the
  12067. function leaves it.
  12068. If your function needs to retrieve the tags including inherited tags
  12069. at the *current* entry, you can use the value of the variable
  12070. `org-scanner-tags' which will be much faster than getting the value
  12071. with `org-get-tags-at'. If your function gets properties with
  12072. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12073. to t around the call to `org-entry-properties' to get the same speedup.
  12074. Note that if your function moves around to retrieve tags and properties at
  12075. a *different* entry, you cannot use these techniques."
  12076. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12077. (org-agenda-skip-archived-trees (memq 'archive skip))
  12078. (org-agenda-skip-comment-trees (memq 'comment skip))
  12079. (org-agenda-skip-function
  12080. (car (org-delete-all '(comment archive) skip)))
  12081. (org-tags-match-list-sublevels t)
  12082. matcher file res
  12083. org-todo-keywords-for-agenda
  12084. org-done-keywords-for-agenda
  12085. org-todo-keyword-alist-for-agenda
  12086. org-drawers-for-agenda
  12087. org-tag-alist-for-agenda)
  12088. (cond
  12089. ((eq match t) (setq matcher t))
  12090. ((eq match nil) (setq matcher t))
  12091. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12092. (save-excursion
  12093. (save-restriction
  12094. (when (eq scope 'tree)
  12095. (org-back-to-heading t)
  12096. (org-narrow-to-subtree)
  12097. (setq scope nil))
  12098. (if (not scope)
  12099. (progn
  12100. (org-prepare-agenda-buffers
  12101. (list (buffer-file-name (current-buffer))))
  12102. (setq res (org-scan-tags func matcher)))
  12103. ;; Get the right scope
  12104. (cond
  12105. ((and scope (listp scope) (symbolp (car scope)))
  12106. (setq scope (eval scope)))
  12107. ((eq scope 'agenda)
  12108. (setq scope (org-agenda-files t)))
  12109. ((eq scope 'agenda-with-archives)
  12110. (setq scope (org-agenda-files t))
  12111. (setq scope (org-add-archive-files scope)))
  12112. ((eq scope 'file)
  12113. (setq scope (list (buffer-file-name))))
  12114. ((eq scope 'file-with-archives)
  12115. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12116. (org-prepare-agenda-buffers scope)
  12117. (while (setq file (pop scope))
  12118. (with-current-buffer (org-find-base-buffer-visiting file)
  12119. (save-excursion
  12120. (save-restriction
  12121. (widen)
  12122. (goto-char (point-min))
  12123. (setq res (append res (org-scan-tags func matcher))))))))))
  12124. res))
  12125. ;;;; Properties
  12126. ;;; Setting and retrieving properties
  12127. (defconst org-special-properties
  12128. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12129. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12130. "The special properties valid in Org-mode.
  12131. These are properties that are not defined in the property drawer,
  12132. but in some other way.")
  12133. (defconst org-default-properties
  12134. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12135. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12136. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12137. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12138. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12139. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12140. "Some properties that are used by Org-mode for various purposes.
  12141. Being in this list makes sure that they are offered for completion.")
  12142. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12143. "Regular expression matching the first line of a property drawer.")
  12144. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12145. "Regular expression matching the last line of a property drawer.")
  12146. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12147. "Regular expression matching the first line of a property drawer.")
  12148. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12149. "Regular expression matching the first line of a property drawer.")
  12150. (defconst org-property-drawer-re
  12151. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12152. org-property-end-re "\\)\n?")
  12153. "Matches an entire property drawer.")
  12154. (defconst org-clock-drawer-re
  12155. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12156. org-property-end-re "\\)\n?")
  12157. "Matches an entire clock drawer.")
  12158. (defun org-property-action ()
  12159. "Do an action on properties."
  12160. (interactive)
  12161. (let (c)
  12162. (org-at-property-p)
  12163. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12164. (setq c (read-char-exclusive))
  12165. (cond
  12166. ((equal c ?s)
  12167. (call-interactively 'org-set-property))
  12168. ((equal c ?d)
  12169. (call-interactively 'org-delete-property))
  12170. ((equal c ?D)
  12171. (call-interactively 'org-delete-property-globally))
  12172. ((equal c ?c)
  12173. (call-interactively 'org-compute-property-at-point))
  12174. (t (error "No such property action %c" c)))))
  12175. (defun org-set-effort (&optional value)
  12176. "Set the effort property of the current entry.
  12177. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12178. allowed value."
  12179. (interactive "P")
  12180. (if (equal value 0) (setq value 10))
  12181. (let* ((completion-ignore-case t)
  12182. (prop org-effort-property)
  12183. (cur (org-entry-get nil prop))
  12184. (allowed (org-property-get-allowed-values nil prop 'table))
  12185. (existing (mapcar 'list (org-property-values prop)))
  12186. rpl
  12187. (val (cond
  12188. ((stringp value) value)
  12189. ((and allowed (integerp value))
  12190. (or (car (nth (1- value) allowed))
  12191. (car (org-last allowed))))
  12192. (allowed
  12193. (message "Select 1-9,0, [RET%s]: %s"
  12194. (if cur (concat "=" cur) "")
  12195. (mapconcat 'car allowed " "))
  12196. (setq rpl (read-char-exclusive))
  12197. (if (equal rpl ?\r)
  12198. cur
  12199. (setq rpl (- rpl ?0))
  12200. (if (equal rpl 0) (setq rpl 10))
  12201. (if (and (> rpl 0) (<= rpl (length allowed)))
  12202. (car (nth (1- rpl) allowed))
  12203. (org-completing-read "Effort: " allowed nil))))
  12204. (t
  12205. (let (org-completion-use-ido org-completion-use-iswitchb)
  12206. (org-completing-read
  12207. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12208. (concat "[" cur "]") "")
  12209. ": ")
  12210. existing nil nil "" nil cur))))))
  12211. (unless (equal (org-entry-get nil prop) val)
  12212. (org-entry-put nil prop val))
  12213. (message "%s is now %s" prop val)))
  12214. (defun org-at-property-p ()
  12215. "Is cursor inside a property drawer?"
  12216. (save-excursion
  12217. (beginning-of-line 1)
  12218. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12219. (save-match-data ;; Used by calling procedures
  12220. (let ((p (point))
  12221. (range (unless (org-before-first-heading-p)
  12222. (org-get-property-block))))
  12223. (and range (<= (car range) p) (< p (cdr range))))))))
  12224. (defun org-get-property-block (&optional beg end force)
  12225. "Return the (beg . end) range of the body of the property drawer.
  12226. BEG and END can be beginning and end of subtree, if not given
  12227. they will be found.
  12228. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12229. (catch 'exit
  12230. (save-excursion
  12231. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12232. (end (or end (progn (outline-next-heading) (point)))))
  12233. (goto-char beg)
  12234. (if (re-search-forward org-property-start-re end t)
  12235. (setq beg (1+ (match-end 0)))
  12236. (if force
  12237. (save-excursion
  12238. (org-insert-property-drawer)
  12239. (setq end (progn (outline-next-heading) (point))))
  12240. (throw 'exit nil))
  12241. (goto-char beg)
  12242. (if (re-search-forward org-property-start-re end t)
  12243. (setq beg (1+ (match-end 0)))))
  12244. (if (re-search-forward org-property-end-re end t)
  12245. (setq end (match-beginning 0))
  12246. (or force (throw 'exit nil))
  12247. (goto-char beg)
  12248. (setq end beg)
  12249. (org-indent-line-function)
  12250. (insert ":END:\n"))
  12251. (cons beg end)))))
  12252. (defun org-entry-properties (&optional pom which specific)
  12253. "Get all properties of the entry at point-or-marker POM.
  12254. This includes the TODO keyword, the tags, time strings for deadline,
  12255. scheduled, and clocking, and any additional properties defined in the
  12256. entry. The return value is an alist, keys may occur multiple times
  12257. if the property key was used several times.
  12258. POM may also be nil, in which case the current entry is used.
  12259. If WHICH is nil or `all', get all properties. If WHICH is
  12260. `special' or `standard', only get that subclass. If WHICH
  12261. is a string only get exactly this property. Specific can be a string, the
  12262. specific property we are interested in. Specifying it can speed
  12263. things up because then unnecessary parsing is avoided."
  12264. (setq which (or which 'all))
  12265. (org-with-point-at pom
  12266. (let ((clockstr (substring org-clock-string 0 -1))
  12267. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12268. (case-fold-search nil)
  12269. beg end range props sum-props key key1 value string clocksum)
  12270. (save-excursion
  12271. (when (condition-case nil
  12272. (and (org-mode-p) (org-back-to-heading t))
  12273. (error nil))
  12274. (setq beg (point))
  12275. (setq sum-props (get-text-property (point) 'org-summaries))
  12276. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12277. (outline-next-heading)
  12278. (setq end (point))
  12279. (when (memq which '(all special))
  12280. ;; Get the special properties, like TODO and tags
  12281. (goto-char beg)
  12282. (when (and (or (not specific) (string= specific "TODO"))
  12283. (looking-at org-todo-line-regexp) (match-end 2))
  12284. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12285. (when (and (or (not specific) (string= specific "PRIORITY"))
  12286. (looking-at org-priority-regexp))
  12287. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12288. (when (and (or (not specific) (string= specific "TAGS"))
  12289. (setq value (org-get-tags-string))
  12290. (string-match "\\S-" value))
  12291. (push (cons "TAGS" value) props))
  12292. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12293. (setq value (org-get-tags-at)))
  12294. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12295. ":"))
  12296. props))
  12297. (when (or (not specific) (string= specific "BLOCKED"))
  12298. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12299. (when (or (not specific)
  12300. (member specific
  12301. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12302. "TIMESTAMP" "TIMESTAMP_IA")))
  12303. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12304. (setq key (if (match-end 1)
  12305. (substring (org-match-string-no-properties 1)
  12306. 0 -1))
  12307. string (if (equal key clockstr)
  12308. (org-no-properties
  12309. (org-trim
  12310. (buffer-substring
  12311. (match-beginning 3) (goto-char
  12312. (point-at-eol)))))
  12313. (substring (org-match-string-no-properties 3)
  12314. 1 -1)))
  12315. ;; Get the correct property name from the key. This is
  12316. ;; necessary if the user has configured time keywords.
  12317. (setq key1 (concat key ":"))
  12318. (cond
  12319. ((not key)
  12320. (setq key
  12321. (if (= (char-after (match-beginning 3)) ?\[)
  12322. "TIMESTAMP_IA" "TIMESTAMP")))
  12323. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12324. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12325. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12326. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12327. (when (or (equal key "CLOCK") (not (assoc key props)))
  12328. (push (cons key string) props))))
  12329. )
  12330. (when (memq which '(all standard))
  12331. ;; Get the standard properties, like :PROP: ...
  12332. (setq range (org-get-property-block beg end))
  12333. (when range
  12334. (goto-char (car range))
  12335. (while (re-search-forward
  12336. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12337. (cdr range) t)
  12338. (setq key (org-match-string-no-properties 1)
  12339. value (org-trim (or (org-match-string-no-properties 2) "")))
  12340. (unless (member key excluded)
  12341. (push (cons key (or value "")) props)))))
  12342. (if clocksum
  12343. (push (cons "CLOCKSUM"
  12344. (org-columns-number-to-string (/ (float clocksum) 60.)
  12345. 'add_times))
  12346. props))
  12347. (unless (assoc "CATEGORY" props)
  12348. (setq value (or (org-get-category)
  12349. (progn (org-refresh-category-properties)
  12350. (org-get-category))))
  12351. (push (cons "CATEGORY" value) props))
  12352. (append sum-props (nreverse props)))))))
  12353. (defun org-entry-get (pom property &optional inherit literal-nil)
  12354. "Get value of PROPERTY for entry at point-or-marker POM.
  12355. If INHERIT is non-nil and the entry does not have the property,
  12356. then also check higher levels of the hierarchy.
  12357. If INHERIT is the symbol `selective', use inheritance only if the setting
  12358. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12359. If the property is present but empty, the return value is the empty string.
  12360. If the property is not present at all, nil is returned.
  12361. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12362. do not interpret it as the list atom nil. This is used for inheritance
  12363. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12364. (org-with-point-at pom
  12365. (if (and inherit (if (eq inherit 'selective)
  12366. (org-property-inherit-p property)
  12367. t))
  12368. (org-entry-get-with-inheritance property literal-nil)
  12369. (if (member property org-special-properties)
  12370. ;; We need a special property. Use `org-entry-properties' to
  12371. ;; retrieve it, but specify the wanted property
  12372. (cdr (assoc property (org-entry-properties nil 'special property)))
  12373. (let ((range (org-get-property-block)))
  12374. (if (and range
  12375. (goto-char (car range))
  12376. (re-search-forward
  12377. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12378. (cdr range) t))
  12379. ;; Found the property, return it.
  12380. (if (match-end 1)
  12381. (if literal-nil
  12382. (org-match-string-no-properties 1)
  12383. (org-not-nil (org-match-string-no-properties 1)))
  12384. "")))))))
  12385. (defun org-property-or-variable-value (var &optional inherit)
  12386. "Check if there is a property fixing the value of VAR.
  12387. If yes, return this value. If not, return the current value of the variable."
  12388. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12389. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12390. (read prop)
  12391. (symbol-value var))))
  12392. (defun org-entry-delete (pom property)
  12393. "Delete the property PROPERTY from entry at point-or-marker POM."
  12394. (org-with-point-at pom
  12395. (if (member property org-special-properties)
  12396. nil ; cannot delete these properties.
  12397. (let ((range (org-get-property-block)))
  12398. (if (and range
  12399. (goto-char (car range))
  12400. (re-search-forward
  12401. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12402. (cdr range) t))
  12403. (progn
  12404. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12405. t)
  12406. nil)))))
  12407. ;; Multi-values properties are properties that contain multiple values
  12408. ;; These values are assumed to be single words, separated by whitespace.
  12409. (defun org-entry-add-to-multivalued-property (pom property value)
  12410. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12411. (let* ((old (org-entry-get pom property))
  12412. (values (and old (org-split-string old "[ \t]"))))
  12413. (setq value (org-entry-protect-space value))
  12414. (unless (member value values)
  12415. (setq values (cons value values))
  12416. (org-entry-put pom property
  12417. (mapconcat 'identity values " ")))))
  12418. (defun org-entry-remove-from-multivalued-property (pom property value)
  12419. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12420. (let* ((old (org-entry-get pom property))
  12421. (values (and old (org-split-string old "[ \t]"))))
  12422. (setq value (org-entry-protect-space value))
  12423. (when (member value values)
  12424. (setq values (delete value values))
  12425. (org-entry-put pom property
  12426. (mapconcat 'identity values " ")))))
  12427. (defun org-entry-member-in-multivalued-property (pom property value)
  12428. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12429. (let* ((old (org-entry-get pom property))
  12430. (values (and old (org-split-string old "[ \t]"))))
  12431. (setq value (org-entry-protect-space value))
  12432. (member value values)))
  12433. (defun org-entry-get-multivalued-property (pom property)
  12434. "Return a list of values in a multivalued property."
  12435. (let* ((value (org-entry-get pom property))
  12436. (values (and value (org-split-string value "[ \t]"))))
  12437. (mapcar 'org-entry-restore-space values)))
  12438. (defun org-entry-put-multivalued-property (pom property &rest values)
  12439. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12440. VALUES should be a list of strings. Spaces will be protected."
  12441. (org-entry-put pom property
  12442. (mapconcat 'org-entry-protect-space values " "))
  12443. (let* ((value (org-entry-get pom property))
  12444. (values (and value (org-split-string value "[ \t]"))))
  12445. (mapcar 'org-entry-restore-space values)))
  12446. (defun org-entry-protect-space (s)
  12447. "Protect spaces and newline in string S."
  12448. (while (string-match " " s)
  12449. (setq s (replace-match "%20" t t s)))
  12450. (while (string-match "\n" s)
  12451. (setq s (replace-match "%0A" t t s)))
  12452. s)
  12453. (defun org-entry-restore-space (s)
  12454. "Restore spaces and newline in string S."
  12455. (while (string-match "%20" s)
  12456. (setq s (replace-match " " t t s)))
  12457. (while (string-match "%0A" s)
  12458. (setq s (replace-match "\n" t t s)))
  12459. s)
  12460. (defvar org-entry-property-inherited-from (make-marker)
  12461. "Marker pointing to the entry from where a property was inherited.
  12462. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12463. location of the entry where the inheritance search matched. If there was
  12464. no match, the marker will point nowhere.
  12465. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12466. is set.")
  12467. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12468. "Get entry property, and search higher levels if not present.
  12469. The search will stop at the first ancestor which has the property defined.
  12470. If the value found is \"nil\", return nil to show that the property
  12471. should be considered as undefined (this is the meaning of nil here).
  12472. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12473. (move-marker org-entry-property-inherited-from nil)
  12474. (let (tmp)
  12475. (save-excursion
  12476. (save-restriction
  12477. (widen)
  12478. (catch 'ex
  12479. (while t
  12480. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12481. (org-back-to-heading t)
  12482. (move-marker org-entry-property-inherited-from (point))
  12483. (throw 'ex tmp))
  12484. (or (org-up-heading-safe) (throw 'ex nil)))))
  12485. (setq tmp (or tmp
  12486. (cdr (assoc property org-file-properties))
  12487. (cdr (assoc property org-global-properties))
  12488. (cdr (assoc property org-global-properties-fixed))))
  12489. (if literal-nil tmp (org-not-nil tmp)))))
  12490. (defvar org-property-changed-functions nil
  12491. "Hook called when the value of a property has changed.
  12492. Each hook function should accept two arguments, the name of the property
  12493. and the new value.")
  12494. (defun org-entry-put (pom property value)
  12495. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12496. (org-with-point-at pom
  12497. (org-back-to-heading t)
  12498. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12499. range)
  12500. (cond
  12501. ((equal property "TODO")
  12502. (when (and (stringp value) (string-match "\\S-" value)
  12503. (not (member value org-todo-keywords-1)))
  12504. (error "\"%s\" is not a valid TODO state" value))
  12505. (if (or (not value)
  12506. (not (string-match "\\S-" value)))
  12507. (setq value 'none))
  12508. (org-todo value)
  12509. (org-set-tags nil 'align))
  12510. ((equal property "PRIORITY")
  12511. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12512. (string-to-char value) ?\ ))
  12513. (org-set-tags nil 'align))
  12514. ((equal property "SCHEDULED")
  12515. (if (re-search-forward org-scheduled-time-regexp end t)
  12516. (cond
  12517. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12518. ((eq value 'later) (org-timestamp-change 1 'day))
  12519. (t (call-interactively 'org-schedule)))
  12520. (call-interactively 'org-schedule)))
  12521. ((equal property "DEADLINE")
  12522. (if (re-search-forward org-deadline-time-regexp end t)
  12523. (cond
  12524. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12525. ((eq value 'later) (org-timestamp-change 1 'day))
  12526. (t (call-interactively 'org-deadline)))
  12527. (call-interactively 'org-deadline)))
  12528. ((member property org-special-properties)
  12529. (error "The %s property can not yet be set with `org-entry-put'"
  12530. property))
  12531. (t ; a non-special property
  12532. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12533. (setq range (org-get-property-block beg end 'force))
  12534. (goto-char (car range))
  12535. (if (re-search-forward
  12536. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12537. (progn
  12538. (delete-region (match-beginning 1) (match-end 1))
  12539. (goto-char (match-beginning 1)))
  12540. (goto-char (cdr range))
  12541. (insert "\n")
  12542. (backward-char 1)
  12543. (org-indent-line-function)
  12544. (insert ":" property ":"))
  12545. (and value (insert " " value))
  12546. (org-indent-line-function)))))
  12547. (run-hook-with-args 'org-property-changed-functions property value)))
  12548. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12549. "Get all property keys in the current buffer.
  12550. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12551. like tags and TODO state.
  12552. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12553. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12554. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12555. formats in the current buffer."
  12556. (let (rtn range cfmt s p)
  12557. (save-excursion
  12558. (save-restriction
  12559. (widen)
  12560. (goto-char (point-min))
  12561. (while (re-search-forward org-property-start-re nil t)
  12562. (setq range (org-get-property-block))
  12563. (goto-char (car range))
  12564. (while (re-search-forward
  12565. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12566. (cdr range) t)
  12567. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12568. (outline-next-heading))))
  12569. (when include-specials
  12570. (setq rtn (append org-special-properties rtn)))
  12571. (when include-defaults
  12572. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12573. (add-to-list 'rtn org-effort-property))
  12574. (when include-columns
  12575. (save-excursion
  12576. (save-restriction
  12577. (widen)
  12578. (goto-char (point-min))
  12579. (while (re-search-forward
  12580. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12581. nil t)
  12582. (setq cfmt (match-string 2) s 0)
  12583. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12584. cfmt s)
  12585. (setq s (match-end 0)
  12586. p (match-string 1 cfmt))
  12587. (unless (or (equal p "ITEM")
  12588. (member p org-special-properties))
  12589. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12590. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12591. (defun org-property-values (key)
  12592. "Return a list of all values of property KEY."
  12593. (save-excursion
  12594. (save-restriction
  12595. (widen)
  12596. (goto-char (point-min))
  12597. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12598. values)
  12599. (while (re-search-forward re nil t)
  12600. (add-to-list 'values (org-trim (match-string 1))))
  12601. (delete "" values)))))
  12602. (defun org-insert-property-drawer ()
  12603. "Insert a property drawer into the current entry."
  12604. (interactive)
  12605. (org-back-to-heading t)
  12606. (looking-at outline-regexp)
  12607. (let ((indent (if org-adapt-indentation
  12608. (- (match-end 0)(match-beginning 0))
  12609. 0))
  12610. (beg (point))
  12611. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12612. end hiddenp)
  12613. (outline-next-heading)
  12614. (setq end (point))
  12615. (goto-char beg)
  12616. (while (re-search-forward re end t))
  12617. (setq hiddenp (org-invisible-p))
  12618. (end-of-line 1)
  12619. (and (equal (char-after) ?\n) (forward-char 1))
  12620. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12621. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12622. ;; just skip this line
  12623. (beginning-of-line 2)
  12624. ;; Drawer start, find the end
  12625. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12626. (beginning-of-line 1)))
  12627. (org-skip-over-state-notes)
  12628. (skip-chars-backward " \t\n\r")
  12629. (if (eq (char-before) ?*) (forward-char 1))
  12630. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12631. (beginning-of-line 0)
  12632. (org-indent-to-column indent)
  12633. (beginning-of-line 2)
  12634. (org-indent-to-column indent)
  12635. (beginning-of-line 0)
  12636. (if hiddenp
  12637. (save-excursion
  12638. (org-back-to-heading t)
  12639. (hide-entry))
  12640. (org-flag-drawer t))))
  12641. (defun org-set-property (property value)
  12642. "In the current entry, set PROPERTY to VALUE.
  12643. When called interactively, this will prompt for a property name, offering
  12644. completion on existing and default properties. And then it will prompt
  12645. for a value, offering completion either on allowed values (via an inherited
  12646. xxx_ALL property) or on existing values in other instances of this property
  12647. in the current file."
  12648. (interactive
  12649. (let* ((completion-ignore-case t)
  12650. (keys (org-buffer-property-keys nil t t))
  12651. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12652. (prop (if (member prop0 keys)
  12653. prop0
  12654. (or (cdr (assoc (downcase prop0)
  12655. (mapcar (lambda (x) (cons (downcase x) x))
  12656. keys)))
  12657. prop0)))
  12658. (cur (org-entry-get nil prop))
  12659. (prompt (concat prop " value"
  12660. (if (and cur (string-match "\\S-" cur))
  12661. (concat " [" cur "]") "") ": "))
  12662. (allowed (org-property-get-allowed-values nil prop 'table))
  12663. (existing (mapcar 'list (org-property-values prop)))
  12664. (val (if allowed
  12665. (org-completing-read prompt allowed nil
  12666. (not (get-text-property 0 'org-unrestricted
  12667. (caar allowed))))
  12668. (let (org-completion-use-ido org-completion-use-iswitchb)
  12669. (org-completing-read prompt existing nil nil "" nil cur)))))
  12670. (list prop (if (equal val "") cur val))))
  12671. (unless (equal (org-entry-get nil property) value)
  12672. (org-entry-put nil property value)))
  12673. (defun org-delete-property (property)
  12674. "In the current entry, delete PROPERTY."
  12675. (interactive
  12676. (let* ((completion-ignore-case t)
  12677. (prop (org-icompleting-read "Property: "
  12678. (org-entry-properties nil 'standard))))
  12679. (list prop)))
  12680. (message "Property %s %s" property
  12681. (if (org-entry-delete nil property)
  12682. "deleted"
  12683. "was not present in the entry")))
  12684. (defun org-delete-property-globally (property)
  12685. "Remove PROPERTY globally, from all entries."
  12686. (interactive
  12687. (let* ((completion-ignore-case t)
  12688. (prop (org-icompleting-read
  12689. "Globally remove property: "
  12690. (mapcar 'list (org-buffer-property-keys)))))
  12691. (list prop)))
  12692. (save-excursion
  12693. (save-restriction
  12694. (widen)
  12695. (goto-char (point-min))
  12696. (let ((cnt 0))
  12697. (while (re-search-forward
  12698. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12699. nil t)
  12700. (setq cnt (1+ cnt))
  12701. (replace-match ""))
  12702. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12703. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12704. (defun org-compute-property-at-point ()
  12705. "Compute the property at point.
  12706. This looks for an enclosing column format, extracts the operator and
  12707. then applies it to the property in the column format's scope."
  12708. (interactive)
  12709. (unless (org-at-property-p)
  12710. (error "Not at a property"))
  12711. (let ((prop (org-match-string-no-properties 2)))
  12712. (org-columns-get-format-and-top-level)
  12713. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12714. (error "No operator defined for property %s" prop))
  12715. (org-columns-compute prop)))
  12716. (defvar org-property-allowed-value-functions nil
  12717. "Hook for functions supplying allowed values for a specific property.
  12718. The functions must take a single argument, the name of the property, and
  12719. return a flat list of allowed values. If \":ETC\" is one of
  12720. the values, this means that these values are intended as defaults for
  12721. completion, but that other values should be allowed too.
  12722. The functions must return nil if they are not responsible for this
  12723. property.")
  12724. (defun org-property-get-allowed-values (pom property &optional table)
  12725. "Get allowed values for the property PROPERTY.
  12726. When TABLE is non-nil, return an alist that can directly be used for
  12727. completion."
  12728. (let (vals)
  12729. (cond
  12730. ((equal property "TODO")
  12731. (setq vals (org-with-point-at pom
  12732. (append org-todo-keywords-1 '("")))))
  12733. ((equal property "PRIORITY")
  12734. (let ((n org-lowest-priority))
  12735. (while (>= n org-highest-priority)
  12736. (push (char-to-string n) vals)
  12737. (setq n (1- n)))))
  12738. ((member property org-special-properties))
  12739. ((setq vals (run-hook-with-args-until-success
  12740. 'org-property-allowed-value-functions property)))
  12741. (t
  12742. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12743. (when (and vals (string-match "\\S-" vals))
  12744. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12745. (setq vals (mapcar (lambda (x)
  12746. (cond ((stringp x) x)
  12747. ((numberp x) (number-to-string x))
  12748. ((symbolp x) (symbol-name x))
  12749. (t "???")))
  12750. vals)))))
  12751. (when (member ":ETC" vals)
  12752. (setq vals (remove ":ETC" vals))
  12753. (org-add-props (car vals) '(org-unrestricted t)))
  12754. (if table (mapcar 'list vals) vals)))
  12755. (defun org-property-previous-allowed-value (&optional previous)
  12756. "Switch to the next allowed value for this property."
  12757. (interactive)
  12758. (org-property-next-allowed-value t))
  12759. (defun org-property-next-allowed-value (&optional previous)
  12760. "Switch to the next allowed value for this property."
  12761. (interactive)
  12762. (unless (org-at-property-p)
  12763. (error "Not at a property"))
  12764. (let* ((key (match-string 2))
  12765. (value (match-string 3))
  12766. (allowed (or (org-property-get-allowed-values (point) key)
  12767. (and (member value '("[ ]" "[-]" "[X]"))
  12768. '("[ ]" "[X]"))))
  12769. nval)
  12770. (unless allowed
  12771. (error "Allowed values for this property have not been defined"))
  12772. (if previous (setq allowed (reverse allowed)))
  12773. (if (member value allowed)
  12774. (setq nval (car (cdr (member value allowed)))))
  12775. (setq nval (or nval (car allowed)))
  12776. (if (equal nval value)
  12777. (error "Only one allowed value for this property"))
  12778. (org-at-property-p)
  12779. (replace-match (concat " :" key ": " nval) t t)
  12780. (org-indent-line-function)
  12781. (beginning-of-line 1)
  12782. (skip-chars-forward " \t")
  12783. (run-hook-with-args 'org-property-changed-functions key nval)))
  12784. (defun org-find-olp (path &optional this-buffer)
  12785. "Return a marker pointing to the entry at outline path OLP.
  12786. If anything goes wrong, throw an error.
  12787. You can wrap this call to catch the error like this:
  12788. (condition-case msg
  12789. (org-mobile-locate-entry (match-string 4))
  12790. (error (nth 1 msg)))
  12791. The return value will then be either a string with the error message,
  12792. or a marker if everything is OK.
  12793. If THIS-BUFFER is set, the outline path does not contain a file,
  12794. only headings."
  12795. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12796. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12797. (level 1)
  12798. (lmin 1)
  12799. (lmax 1)
  12800. limit re end found pos heading cnt)
  12801. (unless buffer (error "File not found :%s" file))
  12802. (with-current-buffer buffer
  12803. (save-excursion
  12804. (save-restriction
  12805. (widen)
  12806. (setq limit (point-max))
  12807. (goto-char (point-min))
  12808. (while (setq heading (pop path))
  12809. (setq re (format org-complex-heading-regexp-format
  12810. (regexp-quote heading)))
  12811. (setq cnt 0 pos (point))
  12812. (while (re-search-forward re end t)
  12813. (setq level (- (match-end 1) (match-beginning 1)))
  12814. (if (and (>= level lmin) (<= level lmax))
  12815. (setq found (match-beginning 0) cnt (1+ cnt))))
  12816. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12817. lmax heading))
  12818. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12819. lmax heading))
  12820. (goto-char found)
  12821. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12822. (setq end (save-excursion (org-end-of-subtree t t))))
  12823. (when (org-on-heading-p)
  12824. (move-marker (make-marker) (point))))))))
  12825. (defun org-find-exact-headling-in-buffer (heading &optional buffer pos-only)
  12826. "Find node HEADING in BUFFER.
  12827. Return a marker to the heading if it was found, or nil if not.
  12828. If POS-ONLY is set, return just the position instead of a marker.
  12829. The heading text must match exact, but it may have a TODO keyword,
  12830. a priority cookie and tags in the standard locations."
  12831. (with-current-buffer (or buffer (current-buffer))
  12832. (save-excursion
  12833. (save-restriction
  12834. (widen)
  12835. (goto-char (point-min))
  12836. (let (case-fold-search)
  12837. (if (re-search-forward
  12838. (format org-complex-heading-regexp-format
  12839. (regexp-quote heading)) nil t)
  12840. (if pos-only
  12841. (match-beginning 0)
  12842. (move-marker (make-marker) (match-beginning 0)))))))))
  12843. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12844. "Find Org node headline HEADING in all .org files in directory DIR.
  12845. When the target headline is found, return a marker to this location."
  12846. (let ((files (directory-files (or dir default-directory)
  12847. nil "\\`[^.#].*\\.org\\'"))
  12848. file visiting m buffer)
  12849. (catch 'found
  12850. (while (setq file (pop files))
  12851. (message "trying %s" file)
  12852. (setq visiting (org-find-base-buffer-visiting file))
  12853. (setq buffer (or visiting (find-file-noselect file)))
  12854. (setq m (org-find-exact-headling-in-buffer
  12855. heading buffer))
  12856. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12857. (and m (throw 'found m))))))
  12858. (defun org-find-entry-with-id (ident)
  12859. "Locate the entry that contains the ID property with exact value IDENT.
  12860. IDENT can be a string, a symbol or a number, this function will search for
  12861. the string representation of it.
  12862. Return the position where this entry starts, or nil if there is no such entry."
  12863. (interactive "sID: ")
  12864. (let ((id (cond
  12865. ((stringp ident) ident)
  12866. ((symbol-name ident) (symbol-name ident))
  12867. ((numberp ident) (number-to-string ident))
  12868. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12869. (case-fold-search nil))
  12870. (save-excursion
  12871. (save-restriction
  12872. (widen)
  12873. (goto-char (point-min))
  12874. (when (re-search-forward
  12875. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12876. nil t)
  12877. (org-back-to-heading t)
  12878. (point))))))
  12879. ;;;; Timestamps
  12880. (defvar org-last-changed-timestamp nil)
  12881. (defvar org-last-inserted-timestamp nil
  12882. "The last time stamp inserted with `org-insert-time-stamp'.")
  12883. (defvar org-time-was-given) ; dynamically scoped parameter
  12884. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12885. (defvar org-ts-what) ; dynamically scoped parameter
  12886. (defun org-time-stamp (arg &optional inactive)
  12887. "Prompt for a date/time and insert a time stamp.
  12888. If the user specifies a time like HH:MM, or if this command is called
  12889. with a prefix argument, the time stamp will contain date and time.
  12890. Otherwise, only the date will be included. All parts of a date not
  12891. specified by the user will be filled in from the current date/time.
  12892. So if you press just return without typing anything, the time stamp
  12893. will represent the current date/time. If there is already a timestamp
  12894. at the cursor, it will be modified."
  12895. (interactive "P")
  12896. (let* ((ts nil)
  12897. (default-time
  12898. ;; Default time is either today, or, when entering a range,
  12899. ;; the range start.
  12900. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12901. (save-excursion
  12902. (re-search-backward
  12903. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12904. (- (point) 20) t)))
  12905. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12906. (current-time)))
  12907. (default-input (and ts (org-get-compact-tod ts)))
  12908. org-time-was-given org-end-time-was-given time)
  12909. (cond
  12910. ((and (org-at-timestamp-p t)
  12911. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12912. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12913. (insert "--")
  12914. (setq time (let ((this-command this-command))
  12915. (org-read-date arg 'totime nil nil
  12916. default-time default-input)))
  12917. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12918. ((org-at-timestamp-p t)
  12919. (setq time (let ((this-command this-command))
  12920. (org-read-date arg 'totime nil nil default-time default-input)))
  12921. (when (org-at-timestamp-p t) ; just to get the match data
  12922. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12923. (replace-match "")
  12924. (setq org-last-changed-timestamp
  12925. (org-insert-time-stamp
  12926. time (or org-time-was-given arg)
  12927. inactive nil nil (list org-end-time-was-given))))
  12928. (message "Timestamp updated"))
  12929. (t
  12930. (setq time (let ((this-command this-command))
  12931. (org-read-date arg 'totime nil nil default-time default-input)))
  12932. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12933. nil nil (list org-end-time-was-given))))))
  12934. ;; FIXME: can we use this for something else, like computing time differences?
  12935. (defun org-get-compact-tod (s)
  12936. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12937. (let* ((t1 (match-string 1 s))
  12938. (h1 (string-to-number (match-string 2 s)))
  12939. (m1 (string-to-number (match-string 3 s)))
  12940. (t2 (and (match-end 4) (match-string 5 s)))
  12941. (h2 (and t2 (string-to-number (match-string 6 s))))
  12942. (m2 (and t2 (string-to-number (match-string 7 s))))
  12943. dh dm)
  12944. (if (not t2)
  12945. t1
  12946. (setq dh (- h2 h1) dm (- m2 m1))
  12947. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12948. (concat t1 "+" (number-to-string dh)
  12949. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12950. (defun org-time-stamp-inactive (&optional arg)
  12951. "Insert an inactive time stamp.
  12952. An inactive time stamp is enclosed in square brackets instead of angle
  12953. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12954. does not link to the calendar and cannot be changed with the S-cursor keys.
  12955. So these are more for recording a certain time/date."
  12956. (interactive "P")
  12957. (org-time-stamp arg 'inactive))
  12958. (defvar org-date-ovl (make-overlay 1 1))
  12959. (overlay-put org-date-ovl 'face 'org-warning)
  12960. (org-detach-overlay org-date-ovl)
  12961. (defvar org-ans1) ; dynamically scoped parameter
  12962. (defvar org-ans2) ; dynamically scoped parameter
  12963. (defvar org-plain-time-of-day-regexp) ; defined below
  12964. (defvar org-overriding-default-time nil) ; dynamically scoped
  12965. (defvar org-read-date-overlay nil)
  12966. (defvar org-dcst nil) ; dynamically scoped
  12967. (defvar org-read-date-history nil)
  12968. (defvar org-read-date-final-answer nil)
  12969. (defun org-read-date (&optional with-time to-time from-string prompt
  12970. default-time default-input)
  12971. "Read a date, possibly a time, and make things smooth for the user.
  12972. The prompt will suggest to enter an ISO date, but you can also enter anything
  12973. which will at least partially be understood by `parse-time-string'.
  12974. Unrecognized parts of the date will default to the current day, month, year,
  12975. hour and minute. If this command is called to replace a timestamp at point,
  12976. of to enter the second timestamp of a range, the default time is taken
  12977. from the existing stamp. Furthermore, the command prefers the future,
  12978. so if you are giving a date where the year is not given, and the day-month
  12979. combination is already past in the current year, it will assume you
  12980. mean next year. For details, see the manual. A few examples:
  12981. 3-2-5 --> 2003-02-05
  12982. feb 15 --> currentyear-02-15
  12983. 2/15 --> currentyear-02-15
  12984. sep 12 9 --> 2009-09-12
  12985. 12:45 --> today 12:45
  12986. 22 sept 0:34 --> currentyear-09-22 0:34
  12987. 12 --> currentyear-currentmonth-12
  12988. Fri --> nearest Friday (today or later)
  12989. etc.
  12990. Furthermore you can specify a relative date by giving, as the *first* thing
  12991. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12992. change in days weeks, months, years.
  12993. With a single plus or minus, the date is relative to today. With a double
  12994. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12995. +4d --> four days from today
  12996. +4 --> same as above
  12997. +2w --> two weeks from today
  12998. ++5 --> five days from default date
  12999. The function understands only English month and weekday abbreviations,
  13000. but this can be configured with the variables `parse-time-months' and
  13001. `parse-time-weekdays'.
  13002. While prompting, a calendar is popped up - you can also select the
  13003. date with the mouse (button 1). The calendar shows a period of three
  13004. months. To scroll it to other months, use the keys `>' and `<'.
  13005. If you don't like the calendar, turn it off with
  13006. \(setq org-read-date-popup-calendar nil)
  13007. With optional argument TO-TIME, the date will immediately be converted
  13008. to an internal time.
  13009. With an optional argument WITH-TIME, the prompt will suggest to also
  13010. insert a time. Note that when WITH-TIME is not set, you can still
  13011. enter a time, and this function will inform the calling routine about
  13012. this change. The calling routine may then choose to change the format
  13013. used to insert the time stamp into the buffer to include the time.
  13014. With optional argument FROM-STRING, read from this string instead from
  13015. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13016. the time/date that is used for everything that is not specified by the
  13017. user."
  13018. (require 'parse-time)
  13019. (let* ((org-time-stamp-rounding-minutes
  13020. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13021. (org-dcst org-display-custom-times)
  13022. (ct (org-current-time))
  13023. (def (or org-overriding-default-time default-time ct))
  13024. (defdecode (decode-time def))
  13025. (dummy (progn
  13026. (when (< (nth 2 defdecode) org-extend-today-until)
  13027. (setcar (nthcdr 2 defdecode) -1)
  13028. (setcar (nthcdr 1 defdecode) 59)
  13029. (setq def (apply 'encode-time defdecode)
  13030. defdecode (decode-time def)))))
  13031. (calendar-frame-setup nil)
  13032. (calendar-setup nil)
  13033. (calendar-move-hook nil)
  13034. (calendar-view-diary-initially-flag nil)
  13035. (calendar-view-holidays-initially-flag nil)
  13036. (timestr (format-time-string
  13037. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13038. (prompt (concat (if prompt (concat prompt " ") "")
  13039. (format "Date+time [%s]: " timestr)))
  13040. ans (org-ans0 "") org-ans1 org-ans2 final)
  13041. (cond
  13042. (from-string (setq ans from-string))
  13043. (org-read-date-popup-calendar
  13044. (save-excursion
  13045. (save-window-excursion
  13046. (calendar)
  13047. (calendar-forward-day (- (time-to-days def)
  13048. (calendar-absolute-from-gregorian
  13049. (calendar-current-date))))
  13050. (org-eval-in-calendar nil t)
  13051. (let* ((old-map (current-local-map))
  13052. (map (copy-keymap calendar-mode-map))
  13053. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13054. (org-defkey map (kbd "RET") 'org-calendar-select)
  13055. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13056. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13057. (org-defkey minibuffer-local-map [(meta shift left)]
  13058. (lambda () (interactive)
  13059. (org-eval-in-calendar '(calendar-backward-month 1))))
  13060. (org-defkey minibuffer-local-map [(meta shift right)]
  13061. (lambda () (interactive)
  13062. (org-eval-in-calendar '(calendar-forward-month 1))))
  13063. (org-defkey minibuffer-local-map [(meta shift up)]
  13064. (lambda () (interactive)
  13065. (org-eval-in-calendar '(calendar-backward-year 1))))
  13066. (org-defkey minibuffer-local-map [(meta shift down)]
  13067. (lambda () (interactive)
  13068. (org-eval-in-calendar '(calendar-forward-year 1))))
  13069. (org-defkey minibuffer-local-map [?\e (shift left)]
  13070. (lambda () (interactive)
  13071. (org-eval-in-calendar '(calendar-backward-month 1))))
  13072. (org-defkey minibuffer-local-map [?\e (shift right)]
  13073. (lambda () (interactive)
  13074. (org-eval-in-calendar '(calendar-forward-month 1))))
  13075. (org-defkey minibuffer-local-map [?\e (shift up)]
  13076. (lambda () (interactive)
  13077. (org-eval-in-calendar '(calendar-backward-year 1))))
  13078. (org-defkey minibuffer-local-map [?\e (shift down)]
  13079. (lambda () (interactive)
  13080. (org-eval-in-calendar '(calendar-forward-year 1))))
  13081. (org-defkey minibuffer-local-map [(shift up)]
  13082. (lambda () (interactive)
  13083. (org-eval-in-calendar '(calendar-backward-week 1))))
  13084. (org-defkey minibuffer-local-map [(shift down)]
  13085. (lambda () (interactive)
  13086. (org-eval-in-calendar '(calendar-forward-week 1))))
  13087. (org-defkey minibuffer-local-map [(shift left)]
  13088. (lambda () (interactive)
  13089. (org-eval-in-calendar '(calendar-backward-day 1))))
  13090. (org-defkey minibuffer-local-map [(shift right)]
  13091. (lambda () (interactive)
  13092. (org-eval-in-calendar '(calendar-forward-day 1))))
  13093. (org-defkey minibuffer-local-map ">"
  13094. (lambda () (interactive)
  13095. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13096. (org-defkey minibuffer-local-map "<"
  13097. (lambda () (interactive)
  13098. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13099. (org-defkey minibuffer-local-map "\C-v"
  13100. (lambda () (interactive)
  13101. (org-eval-in-calendar
  13102. '(calendar-scroll-left-three-months 1))))
  13103. (org-defkey minibuffer-local-map "\M-v"
  13104. (lambda () (interactive)
  13105. (org-eval-in-calendar
  13106. '(calendar-scroll-right-three-months 1))))
  13107. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13108. (unwind-protect
  13109. (progn
  13110. (use-local-map map)
  13111. (add-hook 'post-command-hook 'org-read-date-display)
  13112. (setq org-ans0 (read-string prompt default-input
  13113. 'org-read-date-history nil))
  13114. ;; org-ans0: from prompt
  13115. ;; org-ans1: from mouse click
  13116. ;; org-ans2: from calendar motion
  13117. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13118. (remove-hook 'post-command-hook 'org-read-date-display)
  13119. (use-local-map old-map)
  13120. (when org-read-date-overlay
  13121. (delete-overlay org-read-date-overlay)
  13122. (setq org-read-date-overlay nil)))))))
  13123. (t ; Naked prompt only
  13124. (unwind-protect
  13125. (setq ans (read-string prompt default-input
  13126. 'org-read-date-history timestr))
  13127. (when org-read-date-overlay
  13128. (delete-overlay org-read-date-overlay)
  13129. (setq org-read-date-overlay nil)))))
  13130. (setq final (org-read-date-analyze ans def defdecode))
  13131. (setq org-read-date-final-answer ans)
  13132. (if to-time
  13133. (apply 'encode-time final)
  13134. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13135. (format "%04d-%02d-%02d %02d:%02d"
  13136. (nth 5 final) (nth 4 final) (nth 3 final)
  13137. (nth 2 final) (nth 1 final))
  13138. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13139. (defvar def)
  13140. (defvar defdecode)
  13141. (defvar with-time)
  13142. (defvar org-read-date-analyze-futurep nil)
  13143. (defun org-read-date-display ()
  13144. "Display the current date prompt interpretation in the minibuffer."
  13145. (when org-read-date-display-live
  13146. (when org-read-date-overlay
  13147. (delete-overlay org-read-date-overlay))
  13148. (let ((p (point)))
  13149. (end-of-line 1)
  13150. (while (not (equal (buffer-substring
  13151. (max (point-min) (- (point) 4)) (point))
  13152. " "))
  13153. (insert " "))
  13154. (goto-char p))
  13155. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13156. " " (or org-ans1 org-ans2)))
  13157. (org-end-time-was-given nil)
  13158. (f (org-read-date-analyze ans def defdecode))
  13159. (fmts (if org-dcst
  13160. org-time-stamp-custom-formats
  13161. org-time-stamp-formats))
  13162. (fmt (if (or with-time
  13163. (and (boundp 'org-time-was-given) org-time-was-given))
  13164. (cdr fmts)
  13165. (car fmts)))
  13166. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13167. (when (and org-end-time-was-given
  13168. (string-match org-plain-time-of-day-regexp txt))
  13169. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13170. org-end-time-was-given
  13171. (substring txt (match-end 0)))))
  13172. (when org-read-date-analyze-futurep
  13173. (setq txt (concat txt " (=>F)")))
  13174. (setq org-read-date-overlay
  13175. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13176. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13177. (defun org-read-date-analyze (ans def defdecode)
  13178. "Analyze the combined answer of the date prompt."
  13179. ;; FIXME: cleanup and comment
  13180. (let ((nowdecode (decode-time (current-time)))
  13181. delta deltan deltaw deltadef year month day
  13182. hour minute second wday pm h2 m2 tl wday1
  13183. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13184. (setq org-read-date-analyze-futurep nil)
  13185. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13186. (setq ans "+0"))
  13187. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13188. (setq ans (replace-match "" t t ans)
  13189. deltan (car delta)
  13190. deltaw (nth 1 delta)
  13191. deltadef (nth 2 delta)))
  13192. ;; Check if there is an iso week date in there
  13193. ;; If yes, store the info and postpone interpreting it until the rest
  13194. ;; of the parsing is done
  13195. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13196. (setq iso-year (if (match-end 1)
  13197. (org-small-year-to-year
  13198. (string-to-number (match-string 1 ans))))
  13199. iso-weekday (if (match-end 3)
  13200. (string-to-number (match-string 3 ans)))
  13201. iso-week (string-to-number (match-string 2 ans)))
  13202. (setq ans (replace-match "" t t ans)))
  13203. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13204. (when (string-match
  13205. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13206. (setq year (if (match-end 2)
  13207. (string-to-number (match-string 2 ans))
  13208. (progn (setq kill-year t)
  13209. (string-to-number (format-time-string "%Y"))))
  13210. month (string-to-number (match-string 3 ans))
  13211. day (string-to-number (match-string 4 ans)))
  13212. (if (< year 100) (setq year (+ 2000 year)))
  13213. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13214. t nil ans)))
  13215. ;; Help matching american dates, like 5/30 or 5/30/7
  13216. (when (string-match
  13217. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13218. (setq year (if (match-end 4)
  13219. (string-to-number (match-string 4 ans))
  13220. (progn (setq kill-year t)
  13221. (string-to-number (format-time-string "%Y"))))
  13222. month (string-to-number (match-string 1 ans))
  13223. day (string-to-number (match-string 2 ans)))
  13224. (if (< year 100) (setq year (+ 2000 year)))
  13225. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13226. t nil ans)))
  13227. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13228. ;; If there is a time with am/pm, and *no* time without it, we convert
  13229. ;; so that matching will be successful.
  13230. (loop for i from 1 to 2 do ; twice, for end time as well
  13231. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13232. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13233. (setq hour (string-to-number (match-string 1 ans))
  13234. minute (if (match-end 3)
  13235. (string-to-number (match-string 3 ans))
  13236. 0)
  13237. pm (equal ?p
  13238. (string-to-char (downcase (match-string 4 ans)))))
  13239. (if (and (= hour 12) (not pm))
  13240. (setq hour 0)
  13241. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13242. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13243. t t ans))))
  13244. ;; Check if a time range is given as a duration
  13245. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13246. (setq hour (string-to-number (match-string 1 ans))
  13247. h2 (+ hour (string-to-number (match-string 3 ans)))
  13248. minute (string-to-number (match-string 2 ans))
  13249. m2 (+ minute (if (match-end 5) (string-to-number
  13250. (match-string 5 ans))0)))
  13251. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13252. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13253. t t ans)))
  13254. ;; Check if there is a time range
  13255. (when (boundp 'org-end-time-was-given)
  13256. (setq org-time-was-given nil)
  13257. (when (and (string-match org-plain-time-of-day-regexp ans)
  13258. (match-end 8))
  13259. (setq org-end-time-was-given (match-string 8 ans))
  13260. (setq ans (concat (substring ans 0 (match-beginning 7))
  13261. (substring ans (match-end 7))))))
  13262. (setq tl (parse-time-string ans)
  13263. day (or (nth 3 tl) (nth 3 defdecode))
  13264. month (or (nth 4 tl)
  13265. (if (and org-read-date-prefer-future
  13266. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13267. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13268. (nth 4 defdecode)))
  13269. year (or (and (not kill-year) (nth 5 tl))
  13270. (if (and org-read-date-prefer-future
  13271. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13272. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13273. (nth 5 defdecode)))
  13274. hour (or (nth 2 tl) (nth 2 defdecode))
  13275. minute (or (nth 1 tl) (nth 1 defdecode))
  13276. second (or (nth 0 tl) 0)
  13277. wday (nth 6 tl))
  13278. (when (and (eq org-read-date-prefer-future 'time)
  13279. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13280. (equal day (nth 3 nowdecode))
  13281. (equal month (nth 4 nowdecode))
  13282. (equal year (nth 5 nowdecode))
  13283. (nth 2 tl)
  13284. (or (< (nth 2 tl) (nth 2 nowdecode))
  13285. (and (= (nth 2 tl) (nth 2 nowdecode))
  13286. (nth 1 tl)
  13287. (< (nth 1 tl) (nth 1 nowdecode)))))
  13288. (setq day (1+ day)
  13289. futurep t))
  13290. ;; Special date definitions below
  13291. (cond
  13292. (iso-week
  13293. ;; There was an iso week
  13294. (require 'cal-iso)
  13295. (setq futurep nil)
  13296. (setq year (or iso-year year)
  13297. day (or iso-weekday wday 1)
  13298. wday nil ; to make sure that the trigger below does not match
  13299. iso-date (calendar-gregorian-from-absolute
  13300. (calendar-absolute-from-iso
  13301. (list iso-week day year))))
  13302. ; FIXME: Should we also push ISO weeks into the future?
  13303. ; (when (and org-read-date-prefer-future
  13304. ; (not iso-year)
  13305. ; (< (calendar-absolute-from-gregorian iso-date)
  13306. ; (time-to-days (current-time))))
  13307. ; (setq year (1+ year)
  13308. ; iso-date (calendar-gregorian-from-absolute
  13309. ; (calendar-absolute-from-iso
  13310. ; (list iso-week day year)))))
  13311. (setq month (car iso-date)
  13312. year (nth 2 iso-date)
  13313. day (nth 1 iso-date)))
  13314. (deltan
  13315. (setq futurep nil)
  13316. (unless deltadef
  13317. (let ((now (decode-time (current-time))))
  13318. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13319. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13320. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13321. ((equal deltaw "m") (setq month (+ month deltan)))
  13322. ((equal deltaw "y") (setq year (+ year deltan)))))
  13323. ((and wday (not (nth 3 tl)))
  13324. (setq futurep nil)
  13325. ;; Weekday was given, but no day, so pick that day in the week
  13326. ;; on or after the derived date.
  13327. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13328. (unless (equal wday wday1)
  13329. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13330. (if (and (boundp 'org-time-was-given)
  13331. (nth 2 tl))
  13332. (setq org-time-was-given t))
  13333. (if (< year 100) (setq year (+ 2000 year)))
  13334. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13335. (setq org-read-date-analyze-futurep futurep)
  13336. (list second minute hour day month year)))
  13337. (defvar parse-time-weekdays)
  13338. (defun org-read-date-get-relative (s today default)
  13339. "Check string S for special relative date string.
  13340. TODAY and DEFAULT are internal times, for today and for a default.
  13341. Return shift list (N what def-flag)
  13342. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13343. N is the number of WHATs to shift.
  13344. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13345. the DEFAULT date rather than TODAY."
  13346. (when (and
  13347. (string-match
  13348. (concat
  13349. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13350. "\\([0-9]+\\)?"
  13351. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13352. "\\([ \t]\\|$\\)") s)
  13353. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13354. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13355. (string-to-char (substring (match-string 1 s) -1))
  13356. ?+))
  13357. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13358. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13359. (what (if (match-end 3) (match-string 3 s) "d"))
  13360. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13361. (date (if rel default today))
  13362. (wday (nth 6 (decode-time date)))
  13363. delta)
  13364. (if wday1
  13365. (progn
  13366. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13367. (if (= dir ?-) (setq delta (- delta 7)))
  13368. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13369. (list delta "d" rel))
  13370. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13371. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13372. "Turn a user-specified date into the internal representation.
  13373. The internal representation needed by the calendar is (month day year).
  13374. This is a wrapper to handle the brain-dead convention in calendar that
  13375. user function argument order change dependent on argument order."
  13376. (if (boundp 'calendar-date-style)
  13377. (cond
  13378. ((eq calendar-date-style 'american)
  13379. (list arg1 arg2 arg3))
  13380. ((eq calendar-date-style 'european)
  13381. (list arg2 arg1 arg3))
  13382. ((eq calendar-date-style 'iso)
  13383. (list arg2 arg3 arg1)))
  13384. (if (org-bound-and-true-p european-calendar-style)
  13385. (list arg2 arg1 arg3)
  13386. (list arg1 arg2 arg3))))
  13387. (defun org-eval-in-calendar (form &optional keepdate)
  13388. "Eval FORM in the calendar window and return to current window.
  13389. Also, store the cursor date in variable org-ans2."
  13390. (let ((sf (selected-frame))
  13391. (sw (selected-window)))
  13392. (select-window (get-buffer-window "*Calendar*" t))
  13393. (eval form)
  13394. (when (and (not keepdate) (calendar-cursor-to-date))
  13395. (let* ((date (calendar-cursor-to-date))
  13396. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13397. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13398. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13399. (select-window sw)
  13400. (org-select-frame-set-input-focus sf)))
  13401. (defun org-calendar-select ()
  13402. "Return to `org-read-date' with the date currently selected.
  13403. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13404. (interactive)
  13405. (when (calendar-cursor-to-date)
  13406. (let* ((date (calendar-cursor-to-date))
  13407. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13408. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13409. (if (active-minibuffer-window) (exit-minibuffer))))
  13410. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13411. "Insert a date stamp for the date given by the internal TIME.
  13412. WITH-HM means use the stamp format that includes the time of the day.
  13413. INACTIVE means use square brackets instead of angular ones, so that the
  13414. stamp will not contribute to the agenda.
  13415. PRE and POST are optional strings to be inserted before and after the
  13416. stamp.
  13417. The command returns the inserted time stamp."
  13418. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13419. stamp)
  13420. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13421. (insert-before-markers (or pre ""))
  13422. (when (listp extra)
  13423. (setq extra (car extra))
  13424. (if (and (stringp extra)
  13425. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13426. (setq extra (format "-%02d:%02d"
  13427. (string-to-number (match-string 1 extra))
  13428. (string-to-number (match-string 2 extra))))
  13429. (setq extra nil)))
  13430. (when extra
  13431. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13432. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13433. (insert-before-markers (or post ""))
  13434. (setq org-last-inserted-timestamp stamp)))
  13435. (defun org-toggle-time-stamp-overlays ()
  13436. "Toggle the use of custom time stamp formats."
  13437. (interactive)
  13438. (setq org-display-custom-times (not org-display-custom-times))
  13439. (unless org-display-custom-times
  13440. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13441. (while (setq p (next-single-property-change p 'display))
  13442. (if (and (get-text-property p 'display)
  13443. (eq (get-text-property p 'face) 'org-date))
  13444. (remove-text-properties
  13445. p (setq p (next-single-property-change p 'display))
  13446. '(display t))))
  13447. (set-buffer-modified-p bmp)))
  13448. (if (featurep 'xemacs)
  13449. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13450. (org-restart-font-lock)
  13451. (setq org-table-may-need-update t)
  13452. (if org-display-custom-times
  13453. (message "Time stamps are overlayed with custom format")
  13454. (message "Time stamp overlays removed")))
  13455. (defun org-display-custom-time (beg end)
  13456. "Overlay modified time stamp format over timestamp between BEG and END."
  13457. (let* ((ts (buffer-substring beg end))
  13458. t1 w1 with-hm tf time str w2 (off 0))
  13459. (save-match-data
  13460. (setq t1 (org-parse-time-string ts t))
  13461. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13462. (setq off (- (match-end 0) (match-beginning 0)))))
  13463. (setq end (- end off))
  13464. (setq w1 (- end beg)
  13465. with-hm (and (nth 1 t1) (nth 2 t1))
  13466. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13467. time (org-fix-decoded-time t1)
  13468. str (org-add-props
  13469. (format-time-string
  13470. (substring tf 1 -1) (apply 'encode-time time))
  13471. nil 'mouse-face 'highlight)
  13472. w2 (length str))
  13473. (if (not (= w2 w1))
  13474. (add-text-properties (1+ beg) (+ 2 beg)
  13475. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13476. (if (featurep 'xemacs)
  13477. (progn
  13478. (put-text-property beg end 'invisible t)
  13479. (put-text-property beg end 'end-glyph (make-glyph str)))
  13480. (put-text-property beg end 'display str))))
  13481. (defun org-translate-time (string)
  13482. "Translate all timestamps in STRING to custom format.
  13483. But do this only if the variable `org-display-custom-times' is set."
  13484. (when org-display-custom-times
  13485. (save-match-data
  13486. (let* ((start 0)
  13487. (re org-ts-regexp-both)
  13488. t1 with-hm inactive tf time str beg end)
  13489. (while (setq start (string-match re string start))
  13490. (setq beg (match-beginning 0)
  13491. end (match-end 0)
  13492. t1 (save-match-data
  13493. (org-parse-time-string (substring string beg end) t))
  13494. with-hm (and (nth 1 t1) (nth 2 t1))
  13495. inactive (equal (substring string beg (1+ beg)) "[")
  13496. tf (funcall (if with-hm 'cdr 'car)
  13497. org-time-stamp-custom-formats)
  13498. time (org-fix-decoded-time t1)
  13499. str (format-time-string
  13500. (concat
  13501. (if inactive "[" "<") (substring tf 1 -1)
  13502. (if inactive "]" ">"))
  13503. (apply 'encode-time time))
  13504. string (replace-match str t t string)
  13505. start (+ start (length str)))))))
  13506. string)
  13507. (defun org-fix-decoded-time (time)
  13508. "Set 0 instead of nil for the first 6 elements of time.
  13509. Don't touch the rest."
  13510. (let ((n 0))
  13511. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13512. (defun org-days-to-time (timestamp-string)
  13513. "Difference between TIMESTAMP-STRING and now in days."
  13514. (- (time-to-days (org-time-string-to-time timestamp-string))
  13515. (time-to-days (current-time))))
  13516. (defun org-deadline-close (timestamp-string &optional ndays)
  13517. "Is the time in TIMESTAMP-STRING close to the current date?"
  13518. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13519. (and (< (org-days-to-time timestamp-string) ndays)
  13520. (not (org-entry-is-done-p))))
  13521. (defun org-get-wdays (ts)
  13522. "Get the deadline lead time appropriate for timestring TS."
  13523. (cond
  13524. ((<= org-deadline-warning-days 0)
  13525. ;; 0 or negative, enforce this value no matter what
  13526. (- org-deadline-warning-days))
  13527. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13528. ;; lead time is specified.
  13529. (floor (* (string-to-number (match-string 1 ts))
  13530. (cdr (assoc (match-string 2 ts)
  13531. '(("d" . 1) ("w" . 7)
  13532. ("m" . 30.4) ("y" . 365.25)))))))
  13533. ;; go for the default.
  13534. (t org-deadline-warning-days)))
  13535. (defun org-calendar-select-mouse (ev)
  13536. "Return to `org-read-date' with the date currently selected.
  13537. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13538. (interactive "e")
  13539. (mouse-set-point ev)
  13540. (when (calendar-cursor-to-date)
  13541. (let* ((date (calendar-cursor-to-date))
  13542. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13543. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13544. (if (active-minibuffer-window) (exit-minibuffer))))
  13545. (defun org-check-deadlines (ndays)
  13546. "Check if there are any deadlines due or past due.
  13547. A deadline is considered due if it happens within `org-deadline-warning-days'
  13548. days from today's date. If the deadline appears in an entry marked DONE,
  13549. it is not shown. The prefix arg NDAYS can be used to test that many
  13550. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13551. (interactive "P")
  13552. (let* ((org-warn-days
  13553. (cond
  13554. ((equal ndays '(4)) 100000)
  13555. (ndays (prefix-numeric-value ndays))
  13556. (t (abs org-deadline-warning-days))))
  13557. (case-fold-search nil)
  13558. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13559. (callback
  13560. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13561. (message "%d deadlines past-due or due within %d days"
  13562. (org-occur regexp nil callback)
  13563. org-warn-days)))
  13564. (defun org-check-before-date (date)
  13565. "Check if there are deadlines or scheduled entries before DATE."
  13566. (interactive (list (org-read-date)))
  13567. (let ((case-fold-search nil)
  13568. (regexp (concat "\\<\\(" org-deadline-string
  13569. "\\|" org-scheduled-string
  13570. "\\) *<\\([^>]+\\)>"))
  13571. (callback
  13572. (lambda () (time-less-p
  13573. (org-time-string-to-time (match-string 2))
  13574. (org-time-string-to-time date)))))
  13575. (message "%d entries before %s"
  13576. (org-occur regexp nil callback) date)))
  13577. (defun org-check-after-date (date)
  13578. "Check if there are deadlines or scheduled entries after DATE."
  13579. (interactive (list (org-read-date)))
  13580. (let ((case-fold-search nil)
  13581. (regexp (concat "\\<\\(" org-deadline-string
  13582. "\\|" org-scheduled-string
  13583. "\\) *<\\([^>]+\\)>"))
  13584. (callback
  13585. (lambda () (not
  13586. (time-less-p
  13587. (org-time-string-to-time (match-string 2))
  13588. (org-time-string-to-time date))))))
  13589. (message "%d entries after %s"
  13590. (org-occur regexp nil callback) date)))
  13591. (defun org-evaluate-time-range (&optional to-buffer)
  13592. "Evaluate a time range by computing the difference between start and end.
  13593. Normally the result is just printed in the echo area, but with prefix arg
  13594. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13595. If the time range is actually in a table, the result is inserted into the
  13596. next column.
  13597. For time difference computation, a year is assumed to be exactly 365
  13598. days in order to avoid rounding problems."
  13599. (interactive "P")
  13600. (or
  13601. (org-clock-update-time-maybe)
  13602. (save-excursion
  13603. (unless (org-at-date-range-p t)
  13604. (goto-char (point-at-bol))
  13605. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13606. (if (not (org-at-date-range-p t))
  13607. (error "Not at a time-stamp range, and none found in current line")))
  13608. (let* ((ts1 (match-string 1))
  13609. (ts2 (match-string 2))
  13610. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13611. (match-end (match-end 0))
  13612. (time1 (org-time-string-to-time ts1))
  13613. (time2 (org-time-string-to-time ts2))
  13614. (t1 (org-float-time time1))
  13615. (t2 (org-float-time time2))
  13616. (diff (abs (- t2 t1)))
  13617. (negative (< (- t2 t1) 0))
  13618. ;; (ys (floor (* 365 24 60 60)))
  13619. (ds (* 24 60 60))
  13620. (hs (* 60 60))
  13621. (fy "%dy %dd %02d:%02d")
  13622. (fy1 "%dy %dd")
  13623. (fd "%dd %02d:%02d")
  13624. (fd1 "%dd")
  13625. (fh "%02d:%02d")
  13626. y d h m align)
  13627. (if havetime
  13628. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13629. y 0
  13630. d (floor (/ diff ds)) diff (mod diff ds)
  13631. h (floor (/ diff hs)) diff (mod diff hs)
  13632. m (floor (/ diff 60)))
  13633. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13634. y 0
  13635. d (floor (+ (/ diff ds) 0.5))
  13636. h 0 m 0))
  13637. (if (not to-buffer)
  13638. (message "%s" (org-make-tdiff-string y d h m))
  13639. (if (org-at-table-p)
  13640. (progn
  13641. (goto-char match-end)
  13642. (setq align t)
  13643. (and (looking-at " *|") (goto-char (match-end 0))))
  13644. (goto-char match-end))
  13645. (if (looking-at
  13646. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13647. (replace-match ""))
  13648. (if negative (insert " -"))
  13649. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13650. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13651. (insert " " (format fh h m))))
  13652. (if align (org-table-align))
  13653. (message "Time difference inserted")))))
  13654. (defun org-make-tdiff-string (y d h m)
  13655. (let ((fmt "")
  13656. (l nil))
  13657. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13658. l (push y l)))
  13659. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13660. l (push d l)))
  13661. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13662. l (push h l)))
  13663. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13664. l (push m l)))
  13665. (apply 'format fmt (nreverse l))))
  13666. (defun org-time-string-to-time (s)
  13667. (apply 'encode-time (org-parse-time-string s)))
  13668. (defun org-time-string-to-seconds (s)
  13669. (org-float-time (org-time-string-to-time s)))
  13670. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13671. "Convert a time stamp to an absolute day number.
  13672. If there is a specifier for a cyclic time stamp, get the closest date to
  13673. DAYNR.
  13674. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13675. the variable date is bound by the calendar when this is called."
  13676. (cond
  13677. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13678. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13679. daynr
  13680. (+ daynr 1000)))
  13681. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13682. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13683. (time-to-days (current-time))) (match-string 0 s)
  13684. prefer show-all))
  13685. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13686. (defun org-days-to-iso-week (days)
  13687. "Return the iso week number."
  13688. (require 'cal-iso)
  13689. (car (calendar-iso-from-absolute days)))
  13690. (defun org-small-year-to-year (year)
  13691. "Convert 2-digit years into 4-digit years.
  13692. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13693. The year 2000 cannot be abbreviated. Any year larger than 99
  13694. is returned unchanged."
  13695. (if (< year 38)
  13696. (setq year (+ 2000 year))
  13697. (if (< year 100)
  13698. (setq year (+ 1900 year))))
  13699. year)
  13700. (defun org-time-from-absolute (d)
  13701. "Return the time corresponding to date D.
  13702. D may be an absolute day number, or a calendar-type list (month day year)."
  13703. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13704. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13705. (defun org-calendar-holiday ()
  13706. "List of holidays, for Diary display in Org-mode."
  13707. (require 'holidays)
  13708. (let ((hl (funcall
  13709. (if (fboundp 'calendar-check-holidays)
  13710. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13711. (if hl (mapconcat 'identity hl "; "))))
  13712. (defun org-diary-sexp-entry (sexp entry date)
  13713. "Process a SEXP diary ENTRY for DATE."
  13714. (require 'diary-lib)
  13715. (let ((result (if calendar-debug-sexp
  13716. (let ((stack-trace-on-error t))
  13717. (eval (car (read-from-string sexp))))
  13718. (condition-case nil
  13719. (eval (car (read-from-string sexp)))
  13720. (error
  13721. (beep)
  13722. (message "Bad sexp at line %d in %s: %s"
  13723. (org-current-line)
  13724. (buffer-file-name) sexp)
  13725. (sleep-for 2))))))
  13726. (cond ((stringp result) result)
  13727. ((and (consp result)
  13728. (stringp (cdr result))) (cdr result))
  13729. (result entry)
  13730. (t nil))))
  13731. (defun org-diary-to-ical-string (frombuf)
  13732. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13733. This uses the icalendar.el library."
  13734. (let* ((tmpdir (if (featurep 'xemacs)
  13735. (temp-directory)
  13736. temporary-file-directory))
  13737. (tmpfile (make-temp-name
  13738. (expand-file-name "orgics" tmpdir)))
  13739. buf rtn b e)
  13740. (with-current-buffer frombuf
  13741. (icalendar-export-region (point-min) (point-max) tmpfile)
  13742. (setq buf (find-buffer-visiting tmpfile))
  13743. (set-buffer buf)
  13744. (goto-char (point-min))
  13745. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13746. (setq b (match-beginning 0)))
  13747. (goto-char (point-max))
  13748. (if (re-search-backward "^END:VEVENT" nil t)
  13749. (setq e (match-end 0)))
  13750. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13751. (kill-buffer buf)
  13752. (delete-file tmpfile)
  13753. rtn))
  13754. (defun org-closest-date (start current change prefer show-all)
  13755. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13756. When PREFER is `past' return a date that is either CURRENT or past.
  13757. When PREFER is `future', return a date that is either CURRENT or future.
  13758. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13759. ;; Make the proper lists from the dates
  13760. (catch 'exit
  13761. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13762. dn dw sday cday n1 n2 n0
  13763. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13764. (setq start (org-date-to-gregorian start)
  13765. current (org-date-to-gregorian
  13766. (if show-all
  13767. current
  13768. (time-to-days (current-time))))
  13769. sday (calendar-absolute-from-gregorian start)
  13770. cday (calendar-absolute-from-gregorian current))
  13771. (if (<= cday sday) (throw 'exit sday))
  13772. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13773. (setq dn (string-to-number (match-string 1 change))
  13774. dw (cdr (assoc (match-string 2 change) a1)))
  13775. (error "Invalid change specifier: %s" change))
  13776. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13777. (cond
  13778. ((eq dw 'day)
  13779. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13780. n2 (+ n1 dn)))
  13781. ((eq dw 'year)
  13782. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13783. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13784. (setq date1 (list m d y1)
  13785. n1 (calendar-absolute-from-gregorian date1)
  13786. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13787. n2 (calendar-absolute-from-gregorian date2)))
  13788. ((eq dw 'month)
  13789. ;; approx number of month between the two dates
  13790. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13791. ;; How often does dn fit in there?
  13792. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13793. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13794. m (+ m nm)
  13795. ny (floor (/ m 12))
  13796. y (+ y ny)
  13797. m (- m (* ny 12)))
  13798. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13799. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13800. (setq m2 (+ m dn) y2 y)
  13801. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13802. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13803. (while (<= n2 cday)
  13804. (setq n1 n2 m m2 y y2)
  13805. (setq m2 (+ m dn) y2 y)
  13806. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13807. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13808. ;; Make sure n1 is the earlier date
  13809. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13810. (if show-all
  13811. (cond
  13812. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13813. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13814. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13815. (cond
  13816. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13817. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13818. (t (if (= cday n1) n1 n2)))))))
  13819. (defun org-date-to-gregorian (date)
  13820. "Turn any specification of DATE into a Gregorian date for the calendar."
  13821. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13822. ((and (listp date) (= (length date) 3)) date)
  13823. ((stringp date)
  13824. (setq date (org-parse-time-string date))
  13825. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13826. ((listp date)
  13827. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13828. (defun org-parse-time-string (s &optional nodefault)
  13829. "Parse the standard Org-mode time string.
  13830. This should be a lot faster than the normal `parse-time-string'.
  13831. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13832. hour and minute fields will be nil if not given."
  13833. (if (string-match org-ts-regexp0 s)
  13834. (list 0
  13835. (if (or (match-beginning 8) (not nodefault))
  13836. (string-to-number (or (match-string 8 s) "0")))
  13837. (if (or (match-beginning 7) (not nodefault))
  13838. (string-to-number (or (match-string 7 s) "0")))
  13839. (string-to-number (match-string 4 s))
  13840. (string-to-number (match-string 3 s))
  13841. (string-to-number (match-string 2 s))
  13842. nil nil nil)
  13843. (error "Not a standard Org-mode time string: %s" s)))
  13844. (defun org-timestamp-up (&optional arg)
  13845. "Increase the date item at the cursor by one.
  13846. If the cursor is on the year, change the year. If it is on the month or
  13847. the day, change that.
  13848. With prefix ARG, change by that many units."
  13849. (interactive "p")
  13850. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13851. (defun org-timestamp-down (&optional arg)
  13852. "Decrease the date item at the cursor by one.
  13853. If the cursor is on the year, change the year. If it is on the month or
  13854. the day, change that.
  13855. With prefix ARG, change by that many units."
  13856. (interactive "p")
  13857. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13858. (defun org-timestamp-up-day (&optional arg)
  13859. "Increase the date in the time stamp by one day.
  13860. With prefix ARG, change that many days."
  13861. (interactive "p")
  13862. (if (and (not (org-at-timestamp-p t))
  13863. (org-on-heading-p))
  13864. (org-todo 'up)
  13865. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13866. (defun org-timestamp-down-day (&optional arg)
  13867. "Decrease the date in the time stamp by one day.
  13868. With prefix ARG, change that many days."
  13869. (interactive "p")
  13870. (if (and (not (org-at-timestamp-p t))
  13871. (org-on-heading-p))
  13872. (org-todo 'down)
  13873. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13874. (defun org-at-timestamp-p (&optional inactive-ok)
  13875. "Determine if the cursor is in or at a timestamp."
  13876. (interactive)
  13877. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13878. (pos (point))
  13879. (ans (or (looking-at tsr)
  13880. (save-excursion
  13881. (skip-chars-backward "^[<\n\r\t")
  13882. (if (> (point) (point-min)) (backward-char 1))
  13883. (and (looking-at tsr)
  13884. (> (- (match-end 0) pos) -1))))))
  13885. (and ans
  13886. (boundp 'org-ts-what)
  13887. (setq org-ts-what
  13888. (cond
  13889. ((= pos (match-beginning 0)) 'bracket)
  13890. ((= pos (1- (match-end 0))) 'bracket)
  13891. ((org-pos-in-match-range pos 2) 'year)
  13892. ((org-pos-in-match-range pos 3) 'month)
  13893. ((org-pos-in-match-range pos 7) 'hour)
  13894. ((org-pos-in-match-range pos 8) 'minute)
  13895. ((or (org-pos-in-match-range pos 4)
  13896. (org-pos-in-match-range pos 5)) 'day)
  13897. ((and (> pos (or (match-end 8) (match-end 5)))
  13898. (< pos (match-end 0)))
  13899. (- pos (or (match-end 8) (match-end 5))))
  13900. (t 'day))))
  13901. ans))
  13902. (defun org-toggle-timestamp-type ()
  13903. "Toggle the type (<active> or [inactive]) of a time stamp."
  13904. (interactive)
  13905. (when (org-at-timestamp-p t)
  13906. (let ((beg (match-beginning 0)) (end (match-end 0))
  13907. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13908. (save-excursion
  13909. (goto-char beg)
  13910. (while (re-search-forward "[][<>]" end t)
  13911. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13912. t t)))
  13913. (message "Timestamp is now %sactive"
  13914. (if (equal (char-after beg) ?<) "" "in")))))
  13915. (defun org-timestamp-change (n &optional what updown)
  13916. "Change the date in the time stamp at point.
  13917. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13918. `year', `minute', `second'. If WHAT is not given, the cursor position
  13919. in the timestamp determines what will be changed."
  13920. (let ((pos (point))
  13921. with-hm inactive
  13922. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13923. org-ts-what
  13924. extra rem
  13925. ts time time0)
  13926. (if (not (org-at-timestamp-p t))
  13927. (error "Not at a timestamp"))
  13928. (if (and (not what) (eq org-ts-what 'bracket))
  13929. (org-toggle-timestamp-type)
  13930. (if (and (not what) (not (eq org-ts-what 'day))
  13931. org-display-custom-times
  13932. (get-text-property (point) 'display)
  13933. (not (get-text-property (1- (point)) 'display)))
  13934. (setq org-ts-what 'day))
  13935. (setq org-ts-what (or what org-ts-what)
  13936. inactive (= (char-after (match-beginning 0)) ?\[)
  13937. ts (match-string 0))
  13938. (replace-match "")
  13939. (if (string-match
  13940. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13941. ts)
  13942. (setq extra (match-string 1 ts)))
  13943. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13944. (setq with-hm t))
  13945. (setq time0 (org-parse-time-string ts))
  13946. (when (and updown
  13947. (eq org-ts-what 'minute)
  13948. (not current-prefix-arg))
  13949. ;; This looks like s-up and s-down. Change by one rounding step.
  13950. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13951. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13952. (setcar (cdr time0) (+ (nth 1 time0)
  13953. (if (> n 0) (- rem) (- dm rem))))))
  13954. (setq time
  13955. (encode-time (or (car time0) 0)
  13956. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13957. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13958. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13959. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13960. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13961. (nthcdr 6 time0)))
  13962. (when (and (member org-ts-what '(hour minute))
  13963. extra
  13964. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13965. (setq extra (org-modify-ts-extra
  13966. extra
  13967. (if (eq org-ts-what 'hour) 2 5)
  13968. n dm)))
  13969. (when (integerp org-ts-what)
  13970. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13971. (if (eq what 'calendar)
  13972. (let ((cal-date (org-get-date-from-calendar)))
  13973. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13974. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13975. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13976. (setcar time0 (or (car time0) 0))
  13977. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13978. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13979. (setq time (apply 'encode-time time0))))
  13980. (setq org-last-changed-timestamp
  13981. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13982. (org-clock-update-time-maybe)
  13983. (goto-char pos)
  13984. ;; Try to recenter the calendar window, if any
  13985. (if (and org-calendar-follow-timestamp-change
  13986. (get-buffer-window "*Calendar*" t)
  13987. (memq org-ts-what '(day month year)))
  13988. (org-recenter-calendar (time-to-days time))))))
  13989. (defun org-modify-ts-extra (s pos n dm)
  13990. "Change the different parts of the lead-time and repeat fields in timestamp."
  13991. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13992. ng h m new rem)
  13993. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13994. (cond
  13995. ((or (org-pos-in-match-range pos 2)
  13996. (org-pos-in-match-range pos 3))
  13997. (setq m (string-to-number (match-string 3 s))
  13998. h (string-to-number (match-string 2 s)))
  13999. (if (org-pos-in-match-range pos 2)
  14000. (setq h (+ h n))
  14001. (setq n (* dm (org-no-warnings (signum n))))
  14002. (when (not (= 0 (setq rem (% m dm))))
  14003. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14004. (setq m (+ m n)))
  14005. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14006. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14007. (setq h (min 24 (max 0 h)))
  14008. (setq ng 1 new (format "-%02d:%02d" h m)))
  14009. ((org-pos-in-match-range pos 6)
  14010. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14011. ((org-pos-in-match-range pos 5)
  14012. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14013. ((org-pos-in-match-range pos 9)
  14014. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14015. ((org-pos-in-match-range pos 8)
  14016. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14017. (when ng
  14018. (setq s (concat
  14019. (substring s 0 (match-beginning ng))
  14020. new
  14021. (substring s (match-end ng))))))
  14022. s))
  14023. (defun org-recenter-calendar (date)
  14024. "If the calendar is visible, recenter it to DATE."
  14025. (let* ((win (selected-window))
  14026. (cwin (get-buffer-window "*Calendar*" t))
  14027. (calendar-move-hook nil))
  14028. (when cwin
  14029. (select-window cwin)
  14030. (calendar-goto-date (if (listp date) date
  14031. (calendar-gregorian-from-absolute date)))
  14032. (select-window win))))
  14033. (defun org-goto-calendar (&optional arg)
  14034. "Go to the Emacs calendar at the current date.
  14035. If there is a time stamp in the current line, go to that date.
  14036. A prefix ARG can be used to force the current date."
  14037. (interactive "P")
  14038. (let ((tsr org-ts-regexp) diff
  14039. (calendar-move-hook nil)
  14040. (calendar-view-holidays-initially-flag nil)
  14041. (calendar-view-diary-initially-flag nil))
  14042. (if (or (org-at-timestamp-p)
  14043. (save-excursion
  14044. (beginning-of-line 1)
  14045. (looking-at (concat ".*" tsr))))
  14046. (let ((d1 (time-to-days (current-time)))
  14047. (d2 (time-to-days
  14048. (org-time-string-to-time (match-string 1)))))
  14049. (setq diff (- d2 d1))))
  14050. (calendar)
  14051. (calendar-goto-today)
  14052. (if (and diff (not arg)) (calendar-forward-day diff))))
  14053. (defun org-get-date-from-calendar ()
  14054. "Return a list (month day year) of date at point in calendar."
  14055. (with-current-buffer "*Calendar*"
  14056. (save-match-data
  14057. (calendar-cursor-to-date))))
  14058. (defun org-date-from-calendar ()
  14059. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14060. If there is already a time stamp at the cursor position, update it."
  14061. (interactive)
  14062. (if (org-at-timestamp-p t)
  14063. (org-timestamp-change 0 'calendar)
  14064. (let ((cal-date (org-get-date-from-calendar)))
  14065. (org-insert-time-stamp
  14066. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14067. (defun org-minutes-to-hh:mm-string (m)
  14068. "Compute H:MM from a number of minutes."
  14069. (let ((h (/ m 60)))
  14070. (setq m (- m (* 60 h)))
  14071. (format org-time-clocksum-format h m)))
  14072. (defun org-hh:mm-string-to-minutes (s)
  14073. "Convert a string H:MM to a number of minutes.
  14074. If the string is just a number, interpret it as minutes.
  14075. In fact, the first hh:mm or number in the string will be taken,
  14076. there can be extra stuff in the string.
  14077. If no number is found, the return value is 0."
  14078. (cond
  14079. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14080. (+ (* (string-to-number (match-string 1 s)) 60)
  14081. (string-to-number (match-string 2 s))))
  14082. ((string-match "\\([0-9]+\\)" s)
  14083. (string-to-number (match-string 1 s)))
  14084. (t 0)))
  14085. ;;;; Files
  14086. (defun org-save-all-org-buffers ()
  14087. "Save all Org-mode buffers without user confirmation."
  14088. (interactive)
  14089. (message "Saving all Org-mode buffers...")
  14090. (save-some-buffers t 'org-mode-p)
  14091. (when (featurep 'org-id) (org-id-locations-save))
  14092. (message "Saving all Org-mode buffers... done"))
  14093. (defun org-revert-all-org-buffers ()
  14094. "Revert all Org-mode buffers.
  14095. Prompt for confirmation when there are unsaved changes.
  14096. Be sure you know what you are doing before letting this function
  14097. overwrite your changes.
  14098. This function is useful in a setup where one tracks org files
  14099. with a version control system, to revert on one machine after pulling
  14100. changes from another. I believe the procedure must be like this:
  14101. 1. M-x org-save-all-org-buffers
  14102. 2. Pull changes from the other machine, resolve conflicts
  14103. 3. M-x org-revert-all-org-buffers"
  14104. (interactive)
  14105. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14106. (error "Abort"))
  14107. (save-excursion
  14108. (save-window-excursion
  14109. (mapc
  14110. (lambda (b)
  14111. (when (and (with-current-buffer b (org-mode-p))
  14112. (with-current-buffer b buffer-file-name))
  14113. (switch-to-buffer b)
  14114. (revert-buffer t 'no-confirm)))
  14115. (buffer-list))
  14116. (when (and (featurep 'org-id) org-id-track-globally)
  14117. (org-id-locations-load)))))
  14118. ;;;; Agenda files
  14119. ;;;###autoload
  14120. (defun org-switchb (&optional arg)
  14121. "Switch between Org buffers.
  14122. With a prefix argument, restrict available to files.
  14123. With two prefix arguments, restrict available buffers to agenda files.
  14124. Defaults to `iswitchb' for buffer name completion.
  14125. Set `org-completion-use-ido' to make it use ido instead."
  14126. (interactive "P")
  14127. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14128. ((equal arg '(16)) (org-buffer-list 'agenda))
  14129. (t (org-buffer-list))))
  14130. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14131. (org-completion-use-ido org-completion-use-ido))
  14132. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14133. (setq org-completion-use-iswitchb t))
  14134. (switch-to-buffer
  14135. (org-icompleting-read "Org buffer: "
  14136. (mapcar 'list (mapcar 'buffer-name blist))
  14137. nil t))))
  14138. ;;; Define some older names previously used for this functionality
  14139. ;;;###autoload
  14140. (defalias 'org-ido-switchb 'org-switchb)
  14141. ;;;###autoload
  14142. (defalias 'org-iswitchb 'org-switchb)
  14143. (defun org-buffer-list (&optional predicate exclude-tmp)
  14144. "Return a list of Org buffers.
  14145. PREDICATE can be `export', `files' or `agenda'.
  14146. export restrict the list to Export buffers.
  14147. files restrict the list to buffers visiting Org files.
  14148. agenda restrict the list to buffers visiting agenda files.
  14149. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14150. (let* ((bfn nil)
  14151. (agenda-files (and (eq predicate 'agenda)
  14152. (mapcar 'file-truename (org-agenda-files t))))
  14153. (filter
  14154. (cond
  14155. ((eq predicate 'files)
  14156. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14157. ((eq predicate 'export)
  14158. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14159. ((eq predicate 'agenda)
  14160. (lambda (b)
  14161. (with-current-buffer b
  14162. (and (eq major-mode 'org-mode)
  14163. (setq bfn (buffer-file-name b))
  14164. (member (file-truename bfn) agenda-files)))))
  14165. (t (lambda (b) (with-current-buffer b
  14166. (or (eq major-mode 'org-mode)
  14167. (string-match "\*Org .*Export"
  14168. (buffer-name b)))))))))
  14169. (delq nil
  14170. (mapcar
  14171. (lambda(b)
  14172. (if (and (funcall filter b)
  14173. (or (not exclude-tmp)
  14174. (not (string-match "tmp" (buffer-name b)))))
  14175. b
  14176. nil))
  14177. (buffer-list)))))
  14178. (defun org-agenda-files (&optional unrestricted archives)
  14179. "Get the list of agenda files.
  14180. Optional UNRESTRICTED means return the full list even if a restriction
  14181. is currently in place.
  14182. When ARCHIVES is t, include all archive files that are really being
  14183. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14184. `org-agenda-archives-mode' is t."
  14185. (let ((files
  14186. (cond
  14187. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14188. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14189. ((listp org-agenda-files) org-agenda-files)
  14190. (t (error "Invalid value of `org-agenda-files'")))))
  14191. (setq files (apply 'append
  14192. (mapcar (lambda (f)
  14193. (if (file-directory-p f)
  14194. (directory-files
  14195. f t org-agenda-file-regexp)
  14196. (list f)))
  14197. files)))
  14198. (when org-agenda-skip-unavailable-files
  14199. (setq files (delq nil
  14200. (mapcar (function
  14201. (lambda (file)
  14202. (and (file-readable-p file) file)))
  14203. files))))
  14204. (when (or (eq archives t)
  14205. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14206. (setq files (org-add-archive-files files)))
  14207. files))
  14208. (defun org-agenda-file-p (&optional file)
  14209. "Return non-nil, if FILE is an agenda file.
  14210. If FILE is omitted, use the file associated with the current
  14211. buffer."
  14212. (member (or file (buffer-file-name))
  14213. (org-agenda-files t)))
  14214. (defun org-edit-agenda-file-list ()
  14215. "Edit the list of agenda files.
  14216. Depending on setup, this either uses customize to edit the variable
  14217. `org-agenda-files', or it visits the file that is holding the list. In the
  14218. latter case, the buffer is set up in a way that saving it automatically kills
  14219. the buffer and restores the previous window configuration."
  14220. (interactive)
  14221. (if (stringp org-agenda-files)
  14222. (let ((cw (current-window-configuration)))
  14223. (find-file org-agenda-files)
  14224. (org-set-local 'org-window-configuration cw)
  14225. (org-add-hook 'after-save-hook
  14226. (lambda ()
  14227. (set-window-configuration
  14228. (prog1 org-window-configuration
  14229. (kill-buffer (current-buffer))))
  14230. (org-install-agenda-files-menu)
  14231. (message "New agenda file list installed"))
  14232. nil 'local)
  14233. (message "%s" (substitute-command-keys
  14234. "Edit list and finish with \\[save-buffer]")))
  14235. (customize-variable 'org-agenda-files)))
  14236. (defun org-store-new-agenda-file-list (list)
  14237. "Set new value for the agenda file list and save it correctly."
  14238. (if (stringp org-agenda-files)
  14239. (let ((fe (org-read-agenda-file-list t)) b u)
  14240. (while (setq b (find-buffer-visiting org-agenda-files))
  14241. (kill-buffer b))
  14242. (with-temp-file org-agenda-files
  14243. (insert
  14244. (mapconcat
  14245. (lambda (f) ;; Keep un-expanded entries.
  14246. (if (setq u (assoc f fe))
  14247. (cdr u)
  14248. f))
  14249. list "\n")
  14250. "\n")))
  14251. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14252. (org-insert-mode-line-in-empty-file nil))
  14253. (setq org-agenda-files list)
  14254. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14255. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14256. "Read the list of agenda files from a file.
  14257. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14258. filenames, used by `org-store-new-agenda-file-list' to write back
  14259. un-expanded file names."
  14260. (when (file-directory-p org-agenda-files)
  14261. (error "`org-agenda-files' cannot be a single directory"))
  14262. (when (stringp org-agenda-files)
  14263. (with-temp-buffer
  14264. (insert-file-contents org-agenda-files)
  14265. (mapcar
  14266. (lambda (f)
  14267. (let ((e (expand-file-name (substitute-in-file-name f)
  14268. org-directory)))
  14269. (if pair-with-expansion
  14270. (cons e f)
  14271. e)))
  14272. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14273. ;;;###autoload
  14274. (defun org-cycle-agenda-files ()
  14275. "Cycle through the files in `org-agenda-files'.
  14276. If the current buffer visits an agenda file, find the next one in the list.
  14277. If the current buffer does not, find the first agenda file."
  14278. (interactive)
  14279. (let* ((fs (org-agenda-files t))
  14280. (files (append fs (list (car fs))))
  14281. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14282. file)
  14283. (unless files (error "No agenda files"))
  14284. (catch 'exit
  14285. (while (setq file (pop files))
  14286. (if (equal (file-truename file) tcf)
  14287. (when (car files)
  14288. (find-file (car files))
  14289. (throw 'exit t))))
  14290. (find-file (car fs)))
  14291. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14292. (defun org-agenda-file-to-front (&optional to-end)
  14293. "Move/add the current file to the top of the agenda file list.
  14294. If the file is not present in the list, it is added to the front. If it is
  14295. present, it is moved there. With optional argument TO-END, add/move to the
  14296. end of the list."
  14297. (interactive "P")
  14298. (let ((org-agenda-skip-unavailable-files nil)
  14299. (file-alist (mapcar (lambda (x)
  14300. (cons (file-truename x) x))
  14301. (org-agenda-files t)))
  14302. (ctf (file-truename buffer-file-name))
  14303. x had)
  14304. (setq x (assoc ctf file-alist) had x)
  14305. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14306. (if to-end
  14307. (setq file-alist (append (delq x file-alist) (list x)))
  14308. (setq file-alist (cons x (delq x file-alist))))
  14309. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14310. (org-install-agenda-files-menu)
  14311. (message "File %s to %s of agenda file list"
  14312. (if had "moved" "added") (if to-end "end" "front"))))
  14313. (defun org-remove-file (&optional file)
  14314. "Remove current file from the list of files in variable `org-agenda-files'.
  14315. These are the files which are being checked for agenda entries.
  14316. Optional argument FILE means use this file instead of the current."
  14317. (interactive)
  14318. (let* ((org-agenda-skip-unavailable-files nil)
  14319. (file (or file buffer-file-name))
  14320. (true-file (file-truename file))
  14321. (afile (abbreviate-file-name file))
  14322. (files (delq nil (mapcar
  14323. (lambda (x)
  14324. (if (equal true-file
  14325. (file-truename x))
  14326. nil x))
  14327. (org-agenda-files t)))))
  14328. (if (not (= (length files) (length (org-agenda-files t))))
  14329. (progn
  14330. (org-store-new-agenda-file-list files)
  14331. (org-install-agenda-files-menu)
  14332. (message "Removed file: %s" afile))
  14333. (message "File was not in list: %s (not removed)" afile))))
  14334. (defun org-file-menu-entry (file)
  14335. (vector file (list 'find-file file) t))
  14336. (defun org-check-agenda-file (file)
  14337. "Make sure FILE exists. If not, ask user what to do."
  14338. (when (not (file-exists-p file))
  14339. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14340. (abbreviate-file-name file))
  14341. (let ((r (downcase (read-char-exclusive))))
  14342. (cond
  14343. ((equal r ?r)
  14344. (org-remove-file file)
  14345. (throw 'nextfile t))
  14346. (t (error "Abort"))))))
  14347. (defun org-get-agenda-file-buffer (file)
  14348. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14349. it to the list of buffers which might be released later."
  14350. (let ((buf (org-find-base-buffer-visiting file)))
  14351. (if buf
  14352. buf ; just return it
  14353. ;; Make a new buffer and remember it
  14354. (setq buf (find-file-noselect file))
  14355. (if buf (push buf org-agenda-new-buffers))
  14356. buf)))
  14357. (defun org-release-buffers (blist)
  14358. "Release all buffers in list, asking the user for confirmation when needed.
  14359. When a buffer is unmodified, it is just killed. When modified, it is saved
  14360. \(if the user agrees) and then killed."
  14361. (let (buf file)
  14362. (while (setq buf (pop blist))
  14363. (setq file (buffer-file-name buf))
  14364. (when (and (buffer-modified-p buf)
  14365. file
  14366. (y-or-n-p (format "Save file %s? " file)))
  14367. (with-current-buffer buf (save-buffer)))
  14368. (kill-buffer buf))))
  14369. (defun org-prepare-agenda-buffers (files)
  14370. "Create buffers for all agenda files, protect archived trees and comments."
  14371. (interactive)
  14372. (let ((pa '(:org-archived t))
  14373. (pc '(:org-comment t))
  14374. (pall '(:org-archived t :org-comment t))
  14375. (inhibit-read-only t)
  14376. (rea (concat ":" org-archive-tag ":"))
  14377. bmp file re)
  14378. (save-excursion
  14379. (save-restriction
  14380. (while (setq file (pop files))
  14381. (catch 'nextfile
  14382. (if (bufferp file)
  14383. (set-buffer file)
  14384. (org-check-agenda-file file)
  14385. (set-buffer (org-get-agenda-file-buffer file)))
  14386. (widen)
  14387. (setq bmp (buffer-modified-p))
  14388. (org-refresh-category-properties)
  14389. (setq org-todo-keywords-for-agenda
  14390. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14391. (setq org-done-keywords-for-agenda
  14392. (append org-done-keywords-for-agenda org-done-keywords))
  14393. (setq org-todo-keyword-alist-for-agenda
  14394. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14395. (setq org-drawers-for-agenda
  14396. (append org-drawers-for-agenda org-drawers))
  14397. (setq org-tag-alist-for-agenda
  14398. (append org-tag-alist-for-agenda org-tag-alist))
  14399. (save-excursion
  14400. (remove-text-properties (point-min) (point-max) pall)
  14401. (when org-agenda-skip-archived-trees
  14402. (goto-char (point-min))
  14403. (while (re-search-forward rea nil t)
  14404. (if (org-on-heading-p t)
  14405. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14406. (goto-char (point-min))
  14407. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14408. (while (re-search-forward re nil t)
  14409. (add-text-properties
  14410. (match-beginning 0) (org-end-of-subtree t) pc)))
  14411. (set-buffer-modified-p bmp)))))
  14412. (setq org-todo-keywords-for-agenda
  14413. (org-uniquify org-todo-keywords-for-agenda))
  14414. (setq org-todo-keyword-alist-for-agenda
  14415. (org-uniquify org-todo-keyword-alist-for-agenda)
  14416. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14417. ;;;; Embedded LaTeX
  14418. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14419. "Keymap for the minor `org-cdlatex-mode'.")
  14420. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14421. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14422. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14423. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14424. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14425. (defvar org-cdlatex-texmathp-advice-is-done nil
  14426. "Flag remembering if we have applied the advice to texmathp already.")
  14427. (define-minor-mode org-cdlatex-mode
  14428. "Toggle the minor `org-cdlatex-mode'.
  14429. This mode supports entering LaTeX environment and math in LaTeX fragments
  14430. in Org-mode.
  14431. \\{org-cdlatex-mode-map}"
  14432. nil " OCDL" nil
  14433. (when org-cdlatex-mode (require 'cdlatex))
  14434. (unless org-cdlatex-texmathp-advice-is-done
  14435. (setq org-cdlatex-texmathp-advice-is-done t)
  14436. (defadvice texmathp (around org-math-always-on activate)
  14437. "Always return t in org-mode buffers.
  14438. This is because we want to insert math symbols without dollars even outside
  14439. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14440. an embedded LaTeX fragment, let texmathp do its job.
  14441. \\[org-cdlatex-mode-map]"
  14442. (interactive)
  14443. (let (p)
  14444. (cond
  14445. ((not (org-mode-p)) ad-do-it)
  14446. ((eq this-command 'cdlatex-math-symbol)
  14447. (setq ad-return-value t
  14448. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14449. (t
  14450. (let ((p (org-inside-LaTeX-fragment-p)))
  14451. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14452. (setq ad-return-value t
  14453. texmathp-why '("Org-mode embedded math" . 0))
  14454. (if p ad-do-it)))))))))
  14455. (defun turn-on-org-cdlatex ()
  14456. "Unconditionally turn on `org-cdlatex-mode'."
  14457. (org-cdlatex-mode 1))
  14458. (defun org-inside-LaTeX-fragment-p ()
  14459. "Test if point is inside a LaTeX fragment.
  14460. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14461. sequence appearing also before point.
  14462. Even though the matchers for math are configurable, this function assumes
  14463. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14464. delimiters are skipped when they have been removed by customization.
  14465. The return value is nil, or a cons cell with the delimiter and
  14466. and the position of this delimiter.
  14467. This function does a reasonably good job, but can locally be fooled by
  14468. for example currency specifications. For example it will assume being in
  14469. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14470. fragments that are properly closed, but during editing, we have to live
  14471. with the uncertainty caused by missing closing delimiters. This function
  14472. looks only before point, not after."
  14473. (catch 'exit
  14474. (let ((pos (point))
  14475. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14476. (lim (progn
  14477. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14478. (point)))
  14479. dd-on str (start 0) m re)
  14480. (goto-char pos)
  14481. (when dodollar
  14482. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14483. re (nth 1 (assoc "$" org-latex-regexps)))
  14484. (while (string-match re str start)
  14485. (cond
  14486. ((= (match-end 0) (length str))
  14487. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14488. ((= (match-end 0) (- (length str) 5))
  14489. (throw 'exit nil))
  14490. (t (setq start (match-end 0))))))
  14491. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14492. (goto-char pos)
  14493. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14494. (and (match-beginning 2) (throw 'exit nil))
  14495. ;; count $$
  14496. (while (re-search-backward "\\$\\$" lim t)
  14497. (setq dd-on (not dd-on)))
  14498. (goto-char pos)
  14499. (if dd-on (cons "$$" m))))))
  14500. (defun org-inside-latex-macro-p ()
  14501. "Is point inside a LaTeX macro or its arguments?"
  14502. (save-match-data
  14503. (org-in-regexp
  14504. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14505. (defun org-try-cdlatex-tab ()
  14506. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14507. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14508. - inside a LaTeX fragment, or
  14509. - after the first word in a line, where an abbreviation expansion could
  14510. insert a LaTeX environment."
  14511. (when org-cdlatex-mode
  14512. (cond
  14513. ((save-excursion
  14514. (skip-chars-backward "a-zA-Z0-9*")
  14515. (skip-chars-backward " \t")
  14516. (bolp))
  14517. (cdlatex-tab) t)
  14518. ((org-inside-LaTeX-fragment-p)
  14519. (cdlatex-tab) t)
  14520. (t nil))))
  14521. (defun org-cdlatex-underscore-caret (&optional arg)
  14522. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14523. Revert to the normal definition outside of these fragments."
  14524. (interactive "P")
  14525. (if (org-inside-LaTeX-fragment-p)
  14526. (call-interactively 'cdlatex-sub-superscript)
  14527. (let (org-cdlatex-mode)
  14528. (call-interactively (key-binding (vector last-input-event))))))
  14529. (defun org-cdlatex-math-modify (&optional arg)
  14530. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14531. Revert to the normal definition outside of these fragments."
  14532. (interactive "P")
  14533. (if (org-inside-LaTeX-fragment-p)
  14534. (call-interactively 'cdlatex-math-modify)
  14535. (let (org-cdlatex-mode)
  14536. (call-interactively (key-binding (vector last-input-event))))))
  14537. (defvar org-latex-fragment-image-overlays nil
  14538. "List of overlays carrying the images of latex fragments.")
  14539. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14540. (defun org-remove-latex-fragment-image-overlays ()
  14541. "Remove all overlays with LaTeX fragment images in current buffer."
  14542. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14543. (setq org-latex-fragment-image-overlays nil))
  14544. (defun org-preview-latex-fragment (&optional subtree)
  14545. "Preview the LaTeX fragment at point, or all locally or globally.
  14546. If the cursor is in a LaTeX fragment, create the image and overlay
  14547. it over the source code. If there is no fragment at point, display
  14548. all fragments in the current text, from one headline to the next. With
  14549. prefix SUBTREE, display all fragments in the current subtree. With a
  14550. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14551. the cursor is before the first headline,
  14552. display all fragments in the buffer.
  14553. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14554. (interactive "P")
  14555. (org-remove-latex-fragment-image-overlays)
  14556. (save-excursion
  14557. (save-restriction
  14558. (let (beg end at msg)
  14559. (cond
  14560. ((or (equal subtree '(16))
  14561. (not (save-excursion
  14562. (re-search-backward (concat "^" outline-regexp) nil t))))
  14563. (setq beg (point-min) end (point-max)
  14564. msg "Creating images for buffer...%s"))
  14565. ((equal subtree '(4))
  14566. (org-back-to-heading)
  14567. (setq beg (point) end (org-end-of-subtree t)
  14568. msg "Creating images for subtree...%s"))
  14569. (t
  14570. (if (setq at (org-inside-LaTeX-fragment-p))
  14571. (goto-char (max (point-min) (- (cdr at) 2)))
  14572. (org-back-to-heading))
  14573. (setq beg (point) end (progn (outline-next-heading) (point))
  14574. msg (if at "Creating image...%s"
  14575. "Creating images for entry...%s"))))
  14576. (message msg "")
  14577. (narrow-to-region beg end)
  14578. (goto-char beg)
  14579. (org-format-latex
  14580. (concat "ltxpng/" (file-name-sans-extension
  14581. (file-name-nondirectory
  14582. buffer-file-name)))
  14583. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14584. (message msg "done. Use `C-c C-c' to remove images.")))))
  14585. (defvar org-latex-regexps
  14586. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14587. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14588. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14589. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14590. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14591. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14592. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14593. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14594. "Regular expressions for matching embedded LaTeX.")
  14595. (defvar org-export-have-math nil) ;; dynamic scoping
  14596. (defun org-format-latex (prefix &optional dir overlays msg at
  14597. forbuffer processing-type)
  14598. "Replace LaTeX fragments with links to an image, and produce images.
  14599. Some of the options can be changed using the variable
  14600. `org-format-latex-options'."
  14601. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14602. (let* ((prefixnodir (file-name-nondirectory prefix))
  14603. (absprefix (expand-file-name prefix dir))
  14604. (todir (file-name-directory absprefix))
  14605. (opt org-format-latex-options)
  14606. (matchers (plist-get opt :matchers))
  14607. (re-list org-latex-regexps)
  14608. (org-format-latex-header-extra
  14609. (plist-get (org-infile-export-plist) :latex-header-extra))
  14610. (cnt 0) txt hash link beg end re e checkdir
  14611. executables-checked string
  14612. m n block linkfile movefile ov)
  14613. ;; Check the different regular expressions
  14614. (while (setq e (pop re-list))
  14615. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14616. block (if (nth 3 e) "\n\n" ""))
  14617. (when (member m matchers)
  14618. (goto-char (point-min))
  14619. (while (re-search-forward re nil t)
  14620. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14621. (not (get-text-property (match-beginning n)
  14622. 'org-protected))
  14623. (or (not overlays)
  14624. (not (eq (get-char-property (match-beginning n)
  14625. 'org-overlay-type)
  14626. 'org-latex-overlay))))
  14627. (setq org-export-have-math t)
  14628. (cond
  14629. ((eq processing-type 'verbatim)
  14630. ;; Leave the text verbatim, just protect it
  14631. (add-text-properties (match-beginning n) (match-end n)
  14632. '(org-protected t)))
  14633. ((eq processing-type 'mathjax)
  14634. ;; Prepare for MathJax processing
  14635. (setq string (match-string n))
  14636. (if (equal m "$")
  14637. (save-excursion
  14638. (delete-region (match-beginning n) (match-end n))
  14639. (goto-char (match-beginning n))
  14640. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14641. "\\)")
  14642. '(org-protected t))))
  14643. (add-text-properties (match-beginning n) (match-end n)
  14644. '(org-protected t))))
  14645. ((or (eq processing-type 'dvipng) t)
  14646. ;; Process to an image
  14647. (setq txt (match-string n)
  14648. beg (match-beginning n) end (match-end n)
  14649. cnt (1+ cnt))
  14650. (let (print-length print-level) ; make sure full list is printed
  14651. (setq hash (sha1 (prin1-to-string
  14652. (list org-format-latex-header
  14653. org-format-latex-header-extra
  14654. org-export-latex-default-packages-alist
  14655. org-export-latex-packages-alist
  14656. org-format-latex-options
  14657. forbuffer txt)))
  14658. linkfile (format "%s_%s.png" prefix hash)
  14659. movefile (format "%s_%s.png" absprefix hash)))
  14660. (setq link (concat block "[[file:" linkfile "]]" block))
  14661. (if msg (message msg cnt))
  14662. (goto-char beg)
  14663. (unless checkdir ; make sure the directory exists
  14664. (setq checkdir t)
  14665. (or (file-directory-p todir) (make-directory todir t)))
  14666. (unless executables-checked
  14667. (org-check-external-command
  14668. "latex" "needed to convert LaTeX fragments to images")
  14669. (org-check-external-command
  14670. "dvipng" "needed to convert LaTeX fragments to images")
  14671. (setq executables-checked t))
  14672. (unless (file-exists-p movefile)
  14673. (org-create-formula-image
  14674. txt movefile opt forbuffer))
  14675. (if overlays
  14676. (progn
  14677. (mapc (lambda (o)
  14678. (if (eq (overlay-get o 'org-overlay-type)
  14679. 'org-latex-overlay)
  14680. (delete-overlay o)))
  14681. (overlays-in beg end))
  14682. (setq ov (make-overlay beg end))
  14683. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14684. (if (featurep 'xemacs)
  14685. (progn
  14686. (overlay-put ov 'invisible t)
  14687. (overlay-put
  14688. ov 'end-glyph
  14689. (make-glyph (vector 'png :file movefile))))
  14690. (overlay-put
  14691. ov 'display
  14692. (list 'image :type 'png :file movefile :ascent 'center)))
  14693. (push ov org-latex-fragment-image-overlays)
  14694. (goto-char end))
  14695. (delete-region beg end)
  14696. (insert (org-add-props link
  14697. (list 'org-latex-src
  14698. (replace-regexp-in-string
  14699. "\"" "" txt)))))))))))))
  14700. ;; This function borrows from Ganesh Swami's latex2png.el
  14701. (defun org-create-formula-image (string tofile options buffer)
  14702. "This calls dvipng."
  14703. (require 'org-latex)
  14704. (let* ((tmpdir (if (featurep 'xemacs)
  14705. (temp-directory)
  14706. temporary-file-directory))
  14707. (texfilebase (make-temp-name
  14708. (expand-file-name "orgtex" tmpdir)))
  14709. (texfile (concat texfilebase ".tex"))
  14710. (dvifile (concat texfilebase ".dvi"))
  14711. (pngfile (concat texfilebase ".png"))
  14712. (fnh (if (featurep 'xemacs)
  14713. (font-height (get-face-font 'default))
  14714. (face-attribute 'default :height nil)))
  14715. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14716. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14717. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14718. "Black"))
  14719. (bg (or (plist-get options (if buffer :background :html-background))
  14720. "Transparent")))
  14721. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14722. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14723. (with-temp-file texfile
  14724. (insert (org-splice-latex-header
  14725. org-format-latex-header
  14726. org-export-latex-default-packages-alist
  14727. org-export-latex-packages-alist t
  14728. org-format-latex-header-extra))
  14729. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14730. (require 'org-latex)
  14731. (org-export-latex-fix-inputenc))
  14732. (let ((dir default-directory))
  14733. (condition-case nil
  14734. (progn
  14735. (cd tmpdir)
  14736. (call-process "latex" nil nil nil texfile))
  14737. (error nil))
  14738. (cd dir))
  14739. (if (not (file-exists-p dvifile))
  14740. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14741. (condition-case nil
  14742. (call-process "dvipng" nil nil nil
  14743. "-fg" fg "-bg" bg
  14744. "-D" dpi
  14745. ;;"-x" scale "-y" scale
  14746. "-T" "tight"
  14747. "-o" pngfile
  14748. dvifile)
  14749. (error nil))
  14750. (if (not (file-exists-p pngfile))
  14751. (if org-format-latex-signal-error
  14752. (error "Failed to create png file from %s" texfile)
  14753. (message "Failed to create png file from %s" texfile)
  14754. nil)
  14755. ;; Use the requested file name and clean up
  14756. (copy-file pngfile tofile 'replace)
  14757. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14758. (delete-file (concat texfilebase e)))
  14759. pngfile))))
  14760. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14761. "Fill a LaTeX header template TPL.
  14762. In the template, the following place holders will be recognized:
  14763. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14764. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14765. [PACKAGES] \\usepackage statements for PKG
  14766. [NO-PACKAGES] do not include PKG
  14767. [EXTRA] the string EXTRA
  14768. [NO-EXTRA] do not include EXTRA
  14769. For backward compatibility, if both the positive and the negative place
  14770. holder is missing, the positive one (without the \"NO-\") will be
  14771. assumed to be present at the end of the template.
  14772. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14773. EXTRA is a string.
  14774. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14775. (let (rpl (end ""))
  14776. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14777. (setq rpl (if (or (match-end 1) (not def-pkg))
  14778. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14779. tpl (replace-match rpl t t tpl))
  14780. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14781. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14782. (setq rpl (if (or (match-end 1) (not pkg))
  14783. "" (org-latex-packages-to-string pkg snippets-p t))
  14784. tpl (replace-match rpl t t tpl))
  14785. (if pkg (setq end
  14786. (concat end "\n"
  14787. (org-latex-packages-to-string pkg snippets-p)))))
  14788. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14789. (setq rpl (if (or (match-end 1) (not extra))
  14790. "" (concat extra "\n"))
  14791. tpl (replace-match rpl t t tpl))
  14792. (if (and extra (string-match "\\S-" extra))
  14793. (setq end (concat end "\n" extra))))
  14794. (if (string-match "\\S-" end)
  14795. (concat tpl "\n" end)
  14796. tpl)))
  14797. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14798. "Turn an alist of packages into a string with the \\usepackage macros."
  14799. (setq pkg (mapconcat (lambda(p)
  14800. (cond
  14801. ((stringp p) p)
  14802. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14803. (format "%% Package %s omitted" (cadr p)))
  14804. ((equal "" (car p))
  14805. (format "\\usepackage{%s}" (cadr p)))
  14806. (t
  14807. (format "\\usepackage[%s]{%s}"
  14808. (car p) (cadr p)))))
  14809. pkg
  14810. "\n"))
  14811. (if newline (concat pkg "\n") pkg))
  14812. (defun org-dvipng-color (attr)
  14813. "Return an rgb color specification for dvipng."
  14814. (apply 'format "rgb %s %s %s"
  14815. (mapcar 'org-normalize-color
  14816. (color-values (face-attribute 'default attr nil)))))
  14817. (defun org-normalize-color (value)
  14818. "Return string to be used as color value for an RGB component."
  14819. (format "%g" (/ value 65535.0)))
  14820. ;; Image display
  14821. (defvar org-inline-image-overlays nil)
  14822. (make-variable-buffer-local 'org-inline-image-overlays)
  14823. (defun org-toggle-inline-images (&optional include-linked)
  14824. "Toggle the display of inline images.
  14825. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14826. (interactive "P")
  14827. (if org-inline-image-overlays
  14828. (progn
  14829. (org-remove-inline-images)
  14830. (message "Inline image display turned off"))
  14831. (org-display-inline-images include-linked)
  14832. (if org-inline-image-overlays
  14833. (message "%d images displayed inline"
  14834. (length org-inline-image-overlays))
  14835. (message "No images to display inline"))))
  14836. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14837. "Display inline images.
  14838. Normally only links without a description part are inlined, because this
  14839. is how it will work for export. When INCLUDE-LINKED is set, also links
  14840. with a description part will be inlined. This can be nice for a quick
  14841. look at those images, but it does not reflect what exported files will look
  14842. like.
  14843. When REFRESH is set, refresh existing images between BEG and END.
  14844. This will create new image displays only if necessary.
  14845. BEG and END default to the buffer boundaries."
  14846. (interactive "P")
  14847. (unless refresh
  14848. (org-remove-inline-images)
  14849. (clear-image-cache))
  14850. (save-excursion
  14851. (save-restriction
  14852. (widen)
  14853. (setq beg (or beg (point-min)) end (or end (point-max)))
  14854. (goto-char (point-min))
  14855. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
  14856. (substring (org-image-file-name-regexp) 0 -2)
  14857. "\\)\\]" (if include-linked "" "\\]")))
  14858. old file ov img)
  14859. (while (re-search-forward re end t)
  14860. (setq old (get-char-property-and-overlay (match-beginning 1)
  14861. 'org-image-overlay))
  14862. (setq file (expand-file-name
  14863. (concat (or (match-string 3) "") (match-string 4))))
  14864. (when (file-exists-p file)
  14865. (if (and (car-safe old) refresh)
  14866. (image-refresh (overlay-get (cdr old) 'display))
  14867. (setq img (create-image file))
  14868. (when img
  14869. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14870. (overlay-put ov 'display img)
  14871. (overlay-put ov 'face 'default)
  14872. (overlay-put ov 'org-image-overlay t)
  14873. (overlay-put ov 'modification-hooks
  14874. (list 'org-display-inline-modification-hook))
  14875. (push ov org-inline-image-overlays)))))))))
  14876. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14877. "Remove inline-display overlay if a corresponding region is modified."
  14878. (let ((inhibit-modification-hooks t))
  14879. (when (and ov after)
  14880. (delete ov org-inline-image-overlays)
  14881. (delete-overlay ov))))
  14882. (defun org-remove-inline-images ()
  14883. "Remove inline display of images."
  14884. (interactive)
  14885. (mapc 'delete-overlay org-inline-image-overlays)
  14886. (setq org-inline-image-overlays nil))
  14887. ;;;; Key bindings
  14888. ;; Make `C-c C-x' a prefix key
  14889. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14890. ;; TAB key with modifiers
  14891. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14892. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14893. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14894. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14895. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14896. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14897. ;; The following line is necessary under Suse GNU/Linux
  14898. (unless (featurep 'xemacs)
  14899. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14900. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14901. (define-key org-mode-map [backtab] 'org-shifttab)
  14902. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14903. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14904. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14905. ;; Cursor keys with modifiers
  14906. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14907. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14908. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14909. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14910. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14911. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14912. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14913. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14914. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14915. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14916. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14917. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14918. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14919. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14920. ;; Babel keys
  14921. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14922. (mapc (lambda (pair)
  14923. (define-key org-babel-map (car pair) (cdr pair)))
  14924. org-babel-key-bindings)
  14925. ;;; Extra keys for tty access.
  14926. ;; We only set them when really needed because otherwise the
  14927. ;; menus don't show the simple keys
  14928. (when (or org-use-extra-keys
  14929. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14930. (not window-system))
  14931. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14932. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14933. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14934. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14935. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14936. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14937. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14938. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14939. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14940. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14941. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14942. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14943. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14944. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14945. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14946. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14947. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14948. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14949. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14950. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14951. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14952. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14953. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14954. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14955. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14956. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14957. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14958. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14959. ;; All the other keys
  14960. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14961. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14962. (if (boundp 'narrow-map)
  14963. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14964. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14965. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14966. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14967. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14968. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14969. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14970. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14971. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14972. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14973. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14974. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14975. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14976. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14977. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14978. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14979. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14980. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14981. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14982. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14983. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14984. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14985. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14986. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14987. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14988. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14989. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14990. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14991. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14992. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14993. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14994. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14995. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14996. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14997. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14998. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14999. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15000. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15001. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15002. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15003. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15004. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15005. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15006. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15007. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15008. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15009. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15010. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15011. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15012. (org-defkey org-mode-map "\C-m" 'org-return)
  15013. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15014. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15015. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15016. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15017. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15018. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15019. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15020. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15021. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15022. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15023. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15024. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15025. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15026. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15027. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15028. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15029. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15030. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15031. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15032. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15033. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15034. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15035. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15036. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15037. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15038. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15039. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15040. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15041. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15042. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15043. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15044. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15045. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15046. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15047. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15048. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15049. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15050. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15051. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15052. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15053. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15054. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15055. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15056. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15057. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15058. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15059. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15060. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15061. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15062. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15063. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15064. (when (featurep 'xemacs)
  15065. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15066. (defconst org-speed-commands-default
  15067. '(
  15068. ("Outline Navigation")
  15069. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15070. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15071. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15072. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15073. ("u" . (org-speed-move-safe 'outline-up-heading))
  15074. ("j" . org-goto)
  15075. ("g" . (org-refile t))
  15076. ("Outline Visibility")
  15077. ("c" . org-cycle)
  15078. ("C" . org-shifttab)
  15079. (" " . org-display-outline-path)
  15080. ("Outline Structure Editing")
  15081. ("U" . org-shiftmetaup)
  15082. ("D" . org-shiftmetadown)
  15083. ("r" . org-metaright)
  15084. ("l" . org-metaleft)
  15085. ("R" . org-shiftmetaright)
  15086. ("L" . org-shiftmetaleft)
  15087. ("i" . (progn (forward-char 1) (call-interactively
  15088. 'org-insert-heading-respect-content)))
  15089. ("^" . org-sort)
  15090. ("w" . org-refile)
  15091. ("a" . org-archive-subtree-default-with-confirmation)
  15092. ("." . outline-mark-subtree)
  15093. ("Clock Commands")
  15094. ("I" . org-clock-in)
  15095. ("O" . org-clock-out)
  15096. ("Meta Data Editing")
  15097. ("t" . org-todo)
  15098. ("0" . (org-priority ?\ ))
  15099. ("1" . (org-priority ?A))
  15100. ("2" . (org-priority ?B))
  15101. ("3" . (org-priority ?C))
  15102. (";" . org-set-tags-command)
  15103. ("e" . org-set-effort)
  15104. ("Agenda Views etc")
  15105. ("v" . org-agenda)
  15106. ("/" . org-sparse-tree)
  15107. ("Misc")
  15108. ("o" . org-open-at-point)
  15109. ("?" . org-speed-command-help)
  15110. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15111. (">" . (org-agenda-remove-restriction-lock))
  15112. )
  15113. "The default speed commands.")
  15114. (defun org-print-speed-command (e)
  15115. (if (> (length (car e)) 1)
  15116. (progn
  15117. (princ "\n")
  15118. (princ (car e))
  15119. (princ "\n")
  15120. (princ (make-string (length (car e)) ?-))
  15121. (princ "\n"))
  15122. (princ (car e))
  15123. (princ " ")
  15124. (if (symbolp (cdr e))
  15125. (princ (symbol-name (cdr e)))
  15126. (prin1 (cdr e)))
  15127. (princ "\n")))
  15128. (defun org-speed-command-help ()
  15129. "Show the available speed commands."
  15130. (interactive)
  15131. (if (not org-use-speed-commands)
  15132. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15133. (with-output-to-temp-buffer "*Help*"
  15134. (princ "User-defined Speed commands\n===========================\n")
  15135. (mapc 'org-print-speed-command org-speed-commands-user)
  15136. (princ "\n")
  15137. (princ "Built-in Speed commands\n=======================\n")
  15138. (mapc 'org-print-speed-command org-speed-commands-default))
  15139. (with-current-buffer "*Help*"
  15140. (setq truncate-lines t))))
  15141. (defun org-speed-move-safe (cmd)
  15142. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15143. If not, return to the original position and throw an error."
  15144. (interactive)
  15145. (let ((pos (point)))
  15146. (call-interactively cmd)
  15147. (unless (and (bolp) (org-on-heading-p))
  15148. (goto-char pos)
  15149. (error "Boundary reached while executing %s" cmd))))
  15150. (defvar org-self-insert-command-undo-counter 0)
  15151. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15152. (defvar org-speed-command nil)
  15153. (defun org-self-insert-command (N)
  15154. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15155. If the cursor is in a table looking at whitespace, the whitespace is
  15156. overwritten, and the table is not marked as requiring realignment."
  15157. (interactive "p")
  15158. (cond
  15159. ((and org-use-speed-commands
  15160. (or (and (bolp) (looking-at outline-regexp))
  15161. (and (functionp org-use-speed-commands)
  15162. (funcall org-use-speed-commands)))
  15163. (setq
  15164. org-speed-command
  15165. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  15166. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  15167. (cond
  15168. ((commandp org-speed-command)
  15169. (setq this-command org-speed-command)
  15170. (call-interactively org-speed-command))
  15171. ((functionp org-speed-command)
  15172. (funcall org-speed-command))
  15173. ((and org-speed-command (listp org-speed-command))
  15174. (eval org-speed-command))
  15175. (t (let (org-use-speed-commands)
  15176. (call-interactively 'org-self-insert-command)))))
  15177. ((and
  15178. (org-table-p)
  15179. (progn
  15180. ;; check if we blank the field, and if that triggers align
  15181. (and (featurep 'org-table) org-table-auto-blank-field
  15182. (member last-command
  15183. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15184. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15185. ;; got extra space, this field does not determine column width
  15186. (let (org-table-may-need-update) (org-table-blank-field))
  15187. ;; no extra space, this field may determine column width
  15188. (org-table-blank-field)))
  15189. t)
  15190. (eq N 1)
  15191. (looking-at "[^|\n]* |"))
  15192. (let (org-table-may-need-update)
  15193. (goto-char (1- (match-end 0)))
  15194. (delete-backward-char 1)
  15195. (goto-char (match-beginning 0))
  15196. (self-insert-command N)))
  15197. (t
  15198. (setq org-table-may-need-update t)
  15199. (self-insert-command N)
  15200. (org-fix-tags-on-the-fly)
  15201. (if org-self-insert-cluster-for-undo
  15202. (if (not (eq last-command 'org-self-insert-command))
  15203. (setq org-self-insert-command-undo-counter 1)
  15204. (if (>= org-self-insert-command-undo-counter 20)
  15205. (setq org-self-insert-command-undo-counter 1)
  15206. (and (> org-self-insert-command-undo-counter 0)
  15207. buffer-undo-list
  15208. (not (cadr buffer-undo-list)) ; remove nil entry
  15209. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15210. (setq org-self-insert-command-undo-counter
  15211. (1+ org-self-insert-command-undo-counter))))))))
  15212. (defun org-fix-tags-on-the-fly ()
  15213. (when (and (equal (char-after (point-at-bol)) ?*)
  15214. (org-on-heading-p))
  15215. (org-align-tags-here org-tags-column)))
  15216. (defun org-delete-backward-char (N)
  15217. "Like `delete-backward-char', insert whitespace at field end in tables.
  15218. When deleting backwards, in tables this function will insert whitespace in
  15219. front of the next \"|\" separator, to keep the table aligned. The table will
  15220. still be marked for re-alignment if the field did fill the entire column,
  15221. because, in this case the deletion might narrow the column."
  15222. (interactive "p")
  15223. (if (and (org-table-p)
  15224. (eq N 1)
  15225. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15226. (looking-at ".*?|"))
  15227. (let ((pos (point))
  15228. (noalign (looking-at "[^|\n\r]* |"))
  15229. (c org-table-may-need-update))
  15230. (backward-delete-char N)
  15231. (skip-chars-forward "^|")
  15232. (insert " ")
  15233. (goto-char (1- pos))
  15234. ;; noalign: if there were two spaces at the end, this field
  15235. ;; does not determine the width of the column.
  15236. (if noalign (setq org-table-may-need-update c)))
  15237. (backward-delete-char N)
  15238. (org-fix-tags-on-the-fly)))
  15239. (defun org-delete-char (N)
  15240. "Like `delete-char', but insert whitespace at field end in tables.
  15241. When deleting characters, in tables this function will insert whitespace in
  15242. front of the next \"|\" separator, to keep the table aligned. The table will
  15243. still be marked for re-alignment if the field did fill the entire column,
  15244. because, in this case the deletion might narrow the column."
  15245. (interactive "p")
  15246. (if (and (org-table-p)
  15247. (not (bolp))
  15248. (not (= (char-after) ?|))
  15249. (eq N 1))
  15250. (if (looking-at ".*?|")
  15251. (let ((pos (point))
  15252. (noalign (looking-at "[^|\n\r]* |"))
  15253. (c org-table-may-need-update))
  15254. (replace-match (concat
  15255. (substring (match-string 0) 1 -1)
  15256. " |"))
  15257. (goto-char pos)
  15258. ;; noalign: if there were two spaces at the end, this field
  15259. ;; does not determine the width of the column.
  15260. (if noalign (setq org-table-may-need-update c)))
  15261. (delete-char N))
  15262. (delete-char N)
  15263. (org-fix-tags-on-the-fly)))
  15264. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15265. (put 'org-self-insert-command 'delete-selection t)
  15266. (put 'orgtbl-self-insert-command 'delete-selection t)
  15267. (put 'org-delete-char 'delete-selection 'supersede)
  15268. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15269. (put 'org-yank 'delete-selection 'yank)
  15270. ;; Make `flyspell-mode' delay after some commands
  15271. (put 'org-self-insert-command 'flyspell-delayed t)
  15272. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15273. (put 'org-delete-char 'flyspell-delayed t)
  15274. (put 'org-delete-backward-char 'flyspell-delayed t)
  15275. ;; Make pabbrev-mode expand after org-mode commands
  15276. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15277. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15278. ;; How to do this: Measure non-white length of current string
  15279. ;; If equal to column width, we should realign.
  15280. (defun org-remap (map &rest commands)
  15281. "In MAP, remap the functions given in COMMANDS.
  15282. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15283. (let (new old)
  15284. (while commands
  15285. (setq old (pop commands) new (pop commands))
  15286. (if (fboundp 'command-remapping)
  15287. (org-defkey map (vector 'remap old) new)
  15288. (substitute-key-definition old new map global-map)))))
  15289. (when (eq org-enable-table-editor 'optimized)
  15290. ;; If the user wants maximum table support, we need to hijack
  15291. ;; some standard editing functions
  15292. (org-remap org-mode-map
  15293. 'self-insert-command 'org-self-insert-command
  15294. 'delete-char 'org-delete-char
  15295. 'delete-backward-char 'org-delete-backward-char)
  15296. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15297. (defvar org-ctrl-c-ctrl-c-hook nil
  15298. "Hook for functions attaching themselves to `C-c C-c'.
  15299. This can be used to add additional functionality to the C-c C-c key which
  15300. executes context-dependent commands.
  15301. Each function will be called with no arguments. The function must check
  15302. if the context is appropriate for it to act. If yes, it should do its
  15303. thing and then return a non-nil value. If the context is wrong,
  15304. just do nothing and return nil.")
  15305. (defvar org-tab-first-hook nil
  15306. "Hook for functions to attach themselves to TAB.
  15307. See `org-ctrl-c-ctrl-c-hook' for more information.
  15308. This hook runs as the first action when TAB is pressed, even before
  15309. `org-cycle' messes around with the `outline-regexp' to cater for
  15310. inline tasks and plain list item folding.
  15311. If any function in this hook returns t, any other actions that
  15312. would have been caused by TAB (such as table field motion or visibility
  15313. cycling) will not occur.")
  15314. (defvar org-tab-after-check-for-table-hook nil
  15315. "Hook for functions to attach themselves to TAB.
  15316. See `org-ctrl-c-ctrl-c-hook' for more information.
  15317. This hook runs after it has been established that the cursor is not in a
  15318. table, but before checking if the cursor is in a headline or if global cycling
  15319. should be done.
  15320. If any function in this hook returns t, not other actions like visibility
  15321. cycling will be done.")
  15322. (defvar org-tab-after-check-for-cycling-hook nil
  15323. "Hook for functions to attach themselves to TAB.
  15324. See `org-ctrl-c-ctrl-c-hook' for more information.
  15325. This hook runs after it has been established that not table field motion and
  15326. not visibility should be done because of current context. This is probably
  15327. the place where a package like yasnippets can hook in.")
  15328. (defvar org-tab-before-tab-emulation-hook nil
  15329. "Hook for functions to attach themselves to TAB.
  15330. See `org-ctrl-c-ctrl-c-hook' for more information.
  15331. This hook runs after every other options for TAB have been exhausted, but
  15332. before indentation and \t insertion takes place.")
  15333. (defvar org-metaleft-hook nil
  15334. "Hook for functions attaching themselves to `M-left'.
  15335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15336. (defvar org-metaright-hook nil
  15337. "Hook for functions attaching themselves to `M-right'.
  15338. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15339. (defvar org-metaup-hook nil
  15340. "Hook for functions attaching themselves to `M-up'.
  15341. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15342. (defvar org-metadown-hook nil
  15343. "Hook for functions attaching themselves to `M-down'.
  15344. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15345. (defvar org-shiftmetaleft-hook nil
  15346. "Hook for functions attaching themselves to `M-S-left'.
  15347. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15348. (defvar org-shiftmetaright-hook nil
  15349. "Hook for functions attaching themselves to `M-S-right'.
  15350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15351. (defvar org-shiftmetaup-hook nil
  15352. "Hook for functions attaching themselves to `M-S-up'.
  15353. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15354. (defvar org-shiftmetadown-hook nil
  15355. "Hook for functions attaching themselves to `M-S-down'.
  15356. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15357. (defvar org-metareturn-hook nil
  15358. "Hook for functions attaching themselves to `M-RET'.
  15359. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15360. (defvar org-shiftup-hook nil
  15361. "Hook for functions attaching themselves to `S-up'.
  15362. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15363. (defvar org-shiftup-final-hook nil
  15364. "Hook for functions attaching themselves to `S-up'.
  15365. This one runs after all other options except shift-select have been excluded.
  15366. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15367. (defvar org-shiftdown-hook nil
  15368. "Hook for functions attaching themselves to `S-down'.
  15369. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15370. (defvar org-shiftdown-final-hook nil
  15371. "Hook for functions attaching themselves to `S-down'.
  15372. This one runs after all other options except shift-select have been excluded.
  15373. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15374. (defvar org-shiftleft-hook nil
  15375. "Hook for functions attaching themselves to `S-left'.
  15376. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15377. (defvar org-shiftleft-final-hook nil
  15378. "Hook for functions attaching themselves to `S-left'.
  15379. This one runs after all other options except shift-select have been excluded.
  15380. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15381. (defvar org-shiftright-hook nil
  15382. "Hook for functions attaching themselves to `S-right'.
  15383. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15384. (defvar org-shiftright-final-hook nil
  15385. "Hook for functions attaching themselves to `S-right'.
  15386. This one runs after all other options except shift-select have been excluded.
  15387. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15388. (defun org-modifier-cursor-error ()
  15389. "Throw an error, a modified cursor command was applied in wrong context."
  15390. (error "This command is active in special context like tables, headlines or items"))
  15391. (defun org-shiftselect-error ()
  15392. "Throw an error because Shift-Cursor command was applied in wrong context."
  15393. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15394. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15395. (error "This command works only in special context like headlines or timestamps")))
  15396. (defun org-call-for-shift-select (cmd)
  15397. (let ((this-command-keys-shift-translated t))
  15398. (call-interactively cmd)))
  15399. (defun org-shifttab (&optional arg)
  15400. "Global visibility cycling or move to previous table field.
  15401. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15402. on context.
  15403. See the individual commands for more information."
  15404. (interactive "P")
  15405. (cond
  15406. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15407. ((integerp arg)
  15408. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15409. (message "Content view to level: %d" arg)
  15410. (org-content (prefix-numeric-value arg2))
  15411. (setq org-cycle-global-status 'overview)))
  15412. (t (call-interactively 'org-global-cycle))))
  15413. (defun org-shiftmetaleft ()
  15414. "Promote subtree or delete table column.
  15415. Calls `org-promote-subtree', `org-outdent-item',
  15416. or `org-table-delete-column', depending on context.
  15417. See the individual commands for more information."
  15418. (interactive)
  15419. (cond
  15420. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15421. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15422. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15423. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15424. (t (org-modifier-cursor-error))))
  15425. (defun org-shiftmetaright ()
  15426. "Demote subtree or insert table column.
  15427. Calls `org-demote-subtree', `org-indent-item',
  15428. or `org-table-insert-column', depending on context.
  15429. See the individual commands for more information."
  15430. (interactive)
  15431. (cond
  15432. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15433. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15434. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15435. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15436. (t (org-modifier-cursor-error))))
  15437. (defun org-shiftmetaup (&optional arg)
  15438. "Move subtree up or kill table row.
  15439. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15440. `org-move-item-up' depending on context. See the individual commands
  15441. for more information."
  15442. (interactive "P")
  15443. (cond
  15444. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15445. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15446. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15447. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15448. (t (org-modifier-cursor-error))))
  15449. (defun org-shiftmetadown (&optional arg)
  15450. "Move subtree down or insert table row.
  15451. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15452. `org-move-item-down', depending on context. See the individual
  15453. commands for more information."
  15454. (interactive "P")
  15455. (cond
  15456. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15457. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15458. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15459. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15460. (t (org-modifier-cursor-error))))
  15461. (defsubst org-hidden-tree-error ()
  15462. (error
  15463. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15464. (defun org-metaleft (&optional arg)
  15465. "Promote heading or move table column to left.
  15466. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15467. With no specific context, calls the Emacs default `backward-word'.
  15468. See the individual commands for more information."
  15469. (interactive "P")
  15470. (cond
  15471. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15472. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15473. ((or (org-on-heading-p)
  15474. (and (org-region-active-p)
  15475. (save-excursion
  15476. (goto-char (region-beginning))
  15477. (org-on-heading-p))))
  15478. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15479. (call-interactively 'org-do-promote))
  15480. ((or (org-at-item-p)
  15481. (and (org-region-active-p)
  15482. (save-excursion
  15483. (goto-char (region-beginning))
  15484. (org-at-item-p))))
  15485. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15486. (call-interactively 'org-outdent-item))
  15487. (t (call-interactively 'backward-word))))
  15488. (defun org-metaright (&optional arg)
  15489. "Demote subtree or move table column to right.
  15490. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15491. With no specific context, calls the Emacs default `forward-word'.
  15492. See the individual commands for more information."
  15493. (interactive "P")
  15494. (cond
  15495. ((run-hook-with-args-until-success 'org-metaright-hook))
  15496. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15497. ((or (org-on-heading-p)
  15498. (and (org-region-active-p)
  15499. (save-excursion
  15500. (goto-char (region-beginning))
  15501. (org-on-heading-p))))
  15502. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15503. (call-interactively 'org-do-demote))
  15504. ((or (org-at-item-p)
  15505. (and (org-region-active-p)
  15506. (save-excursion
  15507. (goto-char (region-beginning))
  15508. (org-at-item-p))))
  15509. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15510. (call-interactively 'org-indent-item))
  15511. (t (call-interactively 'forward-word))))
  15512. (defun org-check-for-hidden (what)
  15513. "Check if there are hidden headlines/items in the current visual line.
  15514. WHAT can be either `headlines' or `items'. If the current line is
  15515. an outline or item heading and it has a folded subtree below it,
  15516. this function returns t, nil otherwise."
  15517. (let ((re (cond
  15518. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15519. ((eq what 'items) (concat "^" (org-item-re t)))
  15520. (t (error "This should not happen"))))
  15521. beg end)
  15522. (save-excursion
  15523. (catch 'exit
  15524. (unless (org-region-active-p)
  15525. (setq beg (point-at-bol))
  15526. (beginning-of-line 2)
  15527. (while (and (not (eobp)) ;; this is like `next-line'
  15528. (get-char-property (1- (point)) 'invisible))
  15529. (beginning-of-line 2))
  15530. (setq end (point))
  15531. (goto-char beg)
  15532. (goto-char (point-at-eol))
  15533. (setq end (max end (point)))
  15534. (while (re-search-forward re end t)
  15535. (if (get-char-property (match-beginning 0) 'invisible)
  15536. (throw 'exit t))))
  15537. nil))))
  15538. (defun org-metaup (&optional arg)
  15539. "Move subtree up or move table row up.
  15540. Calls `org-move-subtree-up' or `org-table-move-row' or
  15541. `org-move-item-up', depending on context. See the individual commands
  15542. for more information."
  15543. (interactive "P")
  15544. (cond
  15545. ((run-hook-with-args-until-success 'org-metaup-hook))
  15546. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15547. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15548. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15549. (t (transpose-lines 1) (beginning-of-line -1))))
  15550. (defun org-metadown (&optional arg)
  15551. "Move subtree down or move table row down.
  15552. Calls `org-move-subtree-down' or `org-table-move-row' or
  15553. `org-move-item-down', depending on context. See the individual
  15554. commands for more information."
  15555. (interactive "P")
  15556. (cond
  15557. ((run-hook-with-args-until-success 'org-metadown-hook))
  15558. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15559. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15560. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15561. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15562. (defun org-shiftup (&optional arg)
  15563. "Increase item in timestamp or increase priority of current headline.
  15564. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15565. depending on context. See the individual commands for more information."
  15566. (interactive "P")
  15567. (cond
  15568. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15569. ((and org-support-shift-select (org-region-active-p))
  15570. (org-call-for-shift-select 'previous-line))
  15571. ((org-at-timestamp-p t)
  15572. (call-interactively (if org-edit-timestamp-down-means-later
  15573. 'org-timestamp-down 'org-timestamp-up)))
  15574. ((and (not (eq org-support-shift-select 'always))
  15575. org-enable-priority-commands
  15576. (org-on-heading-p))
  15577. (call-interactively 'org-priority-up))
  15578. ((and (not org-support-shift-select) (org-at-item-p))
  15579. (call-interactively 'org-previous-item))
  15580. ((org-clocktable-try-shift 'up arg))
  15581. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15582. (org-support-shift-select
  15583. (org-call-for-shift-select 'previous-line))
  15584. (t (org-shiftselect-error))))
  15585. (defun org-shiftdown (&optional arg)
  15586. "Decrease item in timestamp or decrease priority of current headline.
  15587. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15588. depending on context. See the individual commands for more information."
  15589. (interactive "P")
  15590. (cond
  15591. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15592. ((and org-support-shift-select (org-region-active-p))
  15593. (org-call-for-shift-select 'next-line))
  15594. ((org-at-timestamp-p t)
  15595. (call-interactively (if org-edit-timestamp-down-means-later
  15596. 'org-timestamp-up 'org-timestamp-down)))
  15597. ((and (not (eq org-support-shift-select 'always))
  15598. org-enable-priority-commands
  15599. (org-on-heading-p))
  15600. (call-interactively 'org-priority-down))
  15601. ((and (not org-support-shift-select) (org-at-item-p))
  15602. (call-interactively 'org-next-item))
  15603. ((org-clocktable-try-shift 'down arg))
  15604. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15605. (org-support-shift-select
  15606. (org-call-for-shift-select 'next-line))
  15607. (t (org-shiftselect-error))))
  15608. (defun org-shiftright (&optional arg)
  15609. "Cycle the thing at point or in the current line, depending on context.
  15610. Depending on context, this does one of the following:
  15611. - switch a timestamp at point one day into the future
  15612. - on a headline, switch to the next TODO keyword.
  15613. - on an item, switch entire list to the next bullet type
  15614. - on a property line, switch to the next allowed value
  15615. - on a clocktable definition line, move time block into the future"
  15616. (interactive "P")
  15617. (cond
  15618. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15619. ((and org-support-shift-select (org-region-active-p))
  15620. (org-call-for-shift-select 'forward-char))
  15621. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15622. ((and (not (eq org-support-shift-select 'always))
  15623. (org-on-heading-p))
  15624. (let ((org-inhibit-logging
  15625. (not org-treat-S-cursor-todo-selection-as-state-change))
  15626. (org-inhibit-blocking
  15627. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15628. (org-call-with-arg 'org-todo 'right)))
  15629. ((or (and org-support-shift-select
  15630. (not (eq org-support-shift-select 'always))
  15631. (org-at-item-bullet-p))
  15632. (and (not org-support-shift-select) (org-at-item-p)))
  15633. (org-call-with-arg 'org-cycle-list-bullet nil))
  15634. ((and (not (eq org-support-shift-select 'always))
  15635. (org-at-property-p))
  15636. (call-interactively 'org-property-next-allowed-value))
  15637. ((org-clocktable-try-shift 'right arg))
  15638. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15639. (org-support-shift-select
  15640. (org-call-for-shift-select 'forward-char))
  15641. (t (org-shiftselect-error))))
  15642. (defun org-shiftleft (&optional arg)
  15643. "Cycle the thing at point or in the current line, depending on context.
  15644. Depending on context, this does one of the following:
  15645. - switch a timestamp at point one day into the past
  15646. - on a headline, switch to the previous TODO keyword.
  15647. - on an item, switch entire list to the previous bullet type
  15648. - on a property line, switch to the previous allowed value
  15649. - on a clocktable definition line, move time block into the past"
  15650. (interactive "P")
  15651. (cond
  15652. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15653. ((and org-support-shift-select (org-region-active-p))
  15654. (org-call-for-shift-select 'backward-char))
  15655. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15656. ((and (not (eq org-support-shift-select 'always))
  15657. (org-on-heading-p))
  15658. (let ((org-inhibit-logging
  15659. (not org-treat-S-cursor-todo-selection-as-state-change))
  15660. (org-inhibit-blocking
  15661. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15662. (org-call-with-arg 'org-todo 'left)))
  15663. ((or (and org-support-shift-select
  15664. (not (eq org-support-shift-select 'always))
  15665. (org-at-item-bullet-p))
  15666. (and (not org-support-shift-select) (org-at-item-p)))
  15667. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15668. ((and (not (eq org-support-shift-select 'always))
  15669. (org-at-property-p))
  15670. (call-interactively 'org-property-previous-allowed-value))
  15671. ((org-clocktable-try-shift 'left arg))
  15672. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15673. (org-support-shift-select
  15674. (org-call-for-shift-select 'backward-char))
  15675. (t (org-shiftselect-error))))
  15676. (defun org-shiftcontrolright ()
  15677. "Switch to next TODO set."
  15678. (interactive)
  15679. (cond
  15680. ((and org-support-shift-select (org-region-active-p))
  15681. (org-call-for-shift-select 'forward-word))
  15682. ((and (not (eq org-support-shift-select 'always))
  15683. (org-on-heading-p))
  15684. (org-call-with-arg 'org-todo 'nextset))
  15685. (org-support-shift-select
  15686. (org-call-for-shift-select 'forward-word))
  15687. (t (org-shiftselect-error))))
  15688. (defun org-shiftcontrolleft ()
  15689. "Switch to previous TODO set."
  15690. (interactive)
  15691. (cond
  15692. ((and org-support-shift-select (org-region-active-p))
  15693. (org-call-for-shift-select 'backward-word))
  15694. ((and (not (eq org-support-shift-select 'always))
  15695. (org-on-heading-p))
  15696. (org-call-with-arg 'org-todo 'previousset))
  15697. (org-support-shift-select
  15698. (org-call-for-shift-select 'backward-word))
  15699. (t (org-shiftselect-error))))
  15700. (defun org-ctrl-c-ret ()
  15701. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15702. (interactive)
  15703. (cond
  15704. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15705. (t (call-interactively 'org-insert-heading))))
  15706. (defun org-copy-special ()
  15707. "Copy region in table or copy current subtree.
  15708. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15709. See the individual commands for more information."
  15710. (interactive)
  15711. (call-interactively
  15712. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15713. (defun org-cut-special ()
  15714. "Cut region in table or cut current subtree.
  15715. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15716. See the individual commands for more information."
  15717. (interactive)
  15718. (call-interactively
  15719. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15720. (defun org-paste-special (arg)
  15721. "Paste rectangular region into table, or past subtree relative to level.
  15722. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15723. See the individual commands for more information."
  15724. (interactive "P")
  15725. (if (org-at-table-p)
  15726. (org-table-paste-rectangle)
  15727. (org-paste-subtree arg)))
  15728. (defun org-edit-special (&optional arg)
  15729. "Call a special editor for the stuff at point.
  15730. When at a table, call the formula editor with `org-table-edit-formulas'.
  15731. When at the first line of an src example, call `org-edit-src-code'.
  15732. When in an #+include line, visit the include file. Otherwise call
  15733. `ffap' to visit the file at point."
  15734. (interactive)
  15735. ;; possibly prep session before editing source
  15736. (when arg
  15737. (let* ((info (org-babel-get-src-block-info))
  15738. (lang (nth 0 info))
  15739. (params (nth 2 info))
  15740. (session (cdr (assoc :session params))))
  15741. (when (and info session) ;; we are in a source-code block with a session
  15742. (funcall
  15743. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15744. (cond ;; proceed with `org-edit-special'
  15745. ((save-excursion
  15746. (beginning-of-line 1)
  15747. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15748. (find-file (org-trim (match-string 1))))
  15749. ((org-edit-src-code))
  15750. ((org-edit-fixed-width-region))
  15751. ((org-at-table.el-p)
  15752. (org-edit-src-code))
  15753. ((org-at-table-p)
  15754. (call-interactively 'org-table-edit-formulas))
  15755. (t (call-interactively 'ffap))))
  15756. (defun org-ctrl-c-ctrl-c (&optional arg)
  15757. "Set tags in headline, or update according to changed information at point.
  15758. This command does many different things, depending on context:
  15759. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15760. this is what we do.
  15761. - If the cursor is on a statistics cookie, update it.
  15762. - If the cursor is in a headline, prompt for tags and insert them
  15763. into the current line, aligned to `org-tags-column'. When called
  15764. with prefix arg, realign all tags in the current buffer.
  15765. - If the cursor is in one of the special #+KEYWORD lines, this
  15766. triggers scanning the buffer for these lines and updating the
  15767. information.
  15768. - If the cursor is inside a table, realign the table. This command
  15769. works even if the automatic table editor has been turned off.
  15770. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15771. the entire table.
  15772. - If the cursor is at a footnote reference or definition, jump to
  15773. the corresponding definition or references, respectively.
  15774. - If the cursor is a the beginning of a dynamic block, update it.
  15775. - If the current buffer is a capture buffer, close note and file it.
  15776. - If the cursor is on a <<<target>>>, update radio targets and
  15777. corresponding links in this buffer.
  15778. - If the cursor is on a numbered item in a plain list, renumber the
  15779. ordered list.
  15780. - If the cursor is on a checkbox, toggle it.
  15781. - If the cursor is on a code block, evaluate it. The variable
  15782. `org-confirm-babel-evaluate' can be used to control prompting
  15783. before code block evaluation, by default every code block
  15784. evaluation requires confirmation. Code block evaluation can be
  15785. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15786. (interactive "P")
  15787. (let ((org-enable-table-editor t))
  15788. (cond
  15789. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15790. org-occur-highlights
  15791. org-latex-fragment-image-overlays)
  15792. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15793. (org-remove-occur-highlights)
  15794. (org-remove-latex-fragment-image-overlays)
  15795. (message "Temporary highlights/overlays removed from current buffer"))
  15796. ((and (local-variable-p 'org-finish-function (current-buffer))
  15797. (fboundp org-finish-function))
  15798. (funcall org-finish-function))
  15799. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15800. ((or (looking-at org-property-start-re)
  15801. (org-at-property-p))
  15802. (call-interactively 'org-property-action))
  15803. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15804. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15805. (or (org-on-heading-p) (org-at-item-p)))
  15806. (call-interactively 'org-update-statistics-cookies))
  15807. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15808. ((org-at-table.el-p)
  15809. (message "Use C-c ' to edit table.el tables"))
  15810. ((org-at-table-p)
  15811. (org-table-maybe-eval-formula)
  15812. (if arg
  15813. (call-interactively 'org-table-recalculate)
  15814. (org-table-maybe-recalculate-line))
  15815. (call-interactively 'org-table-align))
  15816. ((or (org-footnote-at-reference-p)
  15817. (org-footnote-at-definition-p))
  15818. (call-interactively 'org-footnote-action))
  15819. ((org-at-item-checkbox-p)
  15820. (call-interactively 'org-toggle-checkbox)
  15821. (org-list-send-list 'maybe))
  15822. ((org-at-item-p)
  15823. (if arg
  15824. (call-interactively 'org-toggle-checkbox)
  15825. (call-interactively 'org-maybe-renumber-ordered-list))
  15826. (org-list-send-list 'maybe))
  15827. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15828. ;; Dynamic block
  15829. (beginning-of-line 1)
  15830. (save-excursion (org-update-dblock)))
  15831. ((save-excursion
  15832. (beginning-of-line 1)
  15833. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15834. (cond
  15835. ((equal (match-string 1) "TBLFM")
  15836. ;; Recalculate the table before this line
  15837. (save-excursion
  15838. (beginning-of-line 1)
  15839. (skip-chars-backward " \r\n\t")
  15840. (if (org-at-table-p)
  15841. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15842. (t
  15843. (let ((org-inhibit-startup-visibility-stuff t)
  15844. (org-startup-align-all-tables nil))
  15845. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15846. (message "Local setup has been refreshed"))))
  15847. ((org-clock-update-time-maybe))
  15848. (t (error "C-c C-c can do nothing useful at this location")))))
  15849. (defun org-mode-restart ()
  15850. "Restart Org-mode, to scan again for special lines.
  15851. Also updates the keyword regular expressions."
  15852. (interactive)
  15853. (org-mode)
  15854. (message "Org-mode restarted"))
  15855. (defun org-kill-note-or-show-branches ()
  15856. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15857. (interactive)
  15858. (if (not org-finish-function)
  15859. (progn
  15860. (hide-subtree)
  15861. (call-interactively 'show-branches))
  15862. (let ((org-note-abort t))
  15863. (funcall org-finish-function))))
  15864. (defun org-return (&optional indent)
  15865. "Goto next table row or insert a newline.
  15866. Calls `org-table-next-row' or `newline', depending on context.
  15867. See the individual commands for more information."
  15868. (interactive)
  15869. (cond
  15870. ((bobp) (if indent (newline-and-indent) (newline)))
  15871. ((org-at-table-p)
  15872. (org-table-justify-field-maybe)
  15873. (call-interactively 'org-table-next-row))
  15874. ((and org-return-follows-link
  15875. (eq (get-text-property (point) 'face) 'org-link))
  15876. (call-interactively 'org-open-at-point))
  15877. ((and (org-at-heading-p)
  15878. (looking-at
  15879. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15880. (org-show-entry)
  15881. (end-of-line 1)
  15882. (newline))
  15883. (t (if indent (newline-and-indent) (newline)))))
  15884. (defun org-return-indent ()
  15885. "Goto next table row or insert a newline and indent.
  15886. Calls `org-table-next-row' or `newline-and-indent', depending on
  15887. context. See the individual commands for more information."
  15888. (interactive)
  15889. (org-return t))
  15890. (defun org-ctrl-c-star ()
  15891. "Compute table, or change heading status of lines.
  15892. Calls `org-table-recalculate' or `org-toggle-heading',
  15893. depending on context."
  15894. (interactive)
  15895. (cond
  15896. ((org-at-table-p)
  15897. (call-interactively 'org-table-recalculate))
  15898. (t
  15899. ;; Convert all lines in region to list items
  15900. (call-interactively 'org-toggle-heading))))
  15901. (defun org-ctrl-c-minus ()
  15902. "Insert separator line in table or modify bullet status of line.
  15903. Also turns a plain line or a region of lines into list items.
  15904. Calls `org-table-insert-hline', `org-toggle-item', or
  15905. `org-cycle-list-bullet', depending on context."
  15906. (interactive)
  15907. (cond
  15908. ((org-at-table-p)
  15909. (call-interactively 'org-table-insert-hline))
  15910. ((org-region-active-p)
  15911. (call-interactively 'org-toggle-item))
  15912. ((org-in-item-p)
  15913. (call-interactively 'org-cycle-list-bullet))
  15914. (t
  15915. (call-interactively 'org-toggle-item))))
  15916. (defun org-toggle-item ()
  15917. "Convert headings or normal lines to items, items to normal lines.
  15918. If there is no active region, only the current line is considered.
  15919. If the first line in the region is a headline, convert all headlines to items.
  15920. If the first line in the region is an item, convert all items to normal lines.
  15921. If the first line is normal text, add an item bullet to each line."
  15922. (interactive)
  15923. (let (l2 l beg end)
  15924. (if (org-region-active-p)
  15925. (setq beg (region-beginning) end (region-end))
  15926. (setq beg (point-at-bol)
  15927. end (min (1+ (point-at-eol)) (point-max))))
  15928. (save-excursion
  15929. (goto-char end)
  15930. (setq l2 (org-current-line))
  15931. (goto-char beg)
  15932. (beginning-of-line 1)
  15933. (setq l (1- (org-current-line)))
  15934. (if (org-at-item-p)
  15935. ;; We already have items, de-itemize
  15936. (while (< (setq l (1+ l)) l2)
  15937. (when (org-at-item-p)
  15938. (goto-char (match-beginning 2))
  15939. (delete-region (match-beginning 2) (match-end 2))
  15940. (and (looking-at "[ \t]+") (replace-match "")))
  15941. (beginning-of-line 2))
  15942. (if (org-on-heading-p)
  15943. ;; Headings, convert to items
  15944. (while (< (setq l (1+ l)) l2)
  15945. (if (looking-at org-outline-regexp)
  15946. (replace-match "- " t t))
  15947. (beginning-of-line 2))
  15948. ;; normal lines, turn them into items
  15949. (while (< (setq l (1+ l)) l2)
  15950. (unless (org-at-item-p)
  15951. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15952. (replace-match "\\1- \\2")))
  15953. (beginning-of-line 2)))))))
  15954. (defun org-toggle-heading (&optional nstars)
  15955. "Convert headings to normal text, or items or text to headings.
  15956. If there is no active region, only the current line is considered.
  15957. If the first line is a heading, remove the stars from all headlines
  15958. in the region.
  15959. If the first line is a plain list item, turn all plain list items
  15960. into headings.
  15961. If the first line is a normal line, turn each and every line in the
  15962. region into a heading.
  15963. When converting a line into a heading, the number of stars is chosen
  15964. such that the lines become children of the current entry. However,
  15965. when a prefix argument is given, its value determines the number of
  15966. stars to add."
  15967. (interactive "P")
  15968. (let (l2 l itemp beg end)
  15969. (if (org-region-active-p)
  15970. (setq beg (region-beginning) end (region-end))
  15971. (setq beg (point-at-bol)
  15972. end (min (1+ (point-at-eol)) (point-max))))
  15973. (save-excursion
  15974. (goto-char end)
  15975. (setq l2 (org-current-line))
  15976. (goto-char beg)
  15977. (beginning-of-line 1)
  15978. (setq l (1- (org-current-line)))
  15979. (if (org-on-heading-p)
  15980. ;; We already have headlines, de-star them
  15981. (while (< (setq l (1+ l)) l2)
  15982. (when (org-on-heading-p t)
  15983. (and (looking-at outline-regexp) (replace-match "")))
  15984. (beginning-of-line 2))
  15985. (setq itemp (org-at-item-p))
  15986. (let* ((stars
  15987. (if nstars
  15988. (make-string (prefix-numeric-value current-prefix-arg)
  15989. ?*)
  15990. (save-excursion
  15991. (if (re-search-backward org-complex-heading-regexp nil t)
  15992. (match-string 1) ""))))
  15993. (add-stars (cond (nstars "")
  15994. ((equal stars "") "*")
  15995. (org-odd-levels-only "**")
  15996. (t "*")))
  15997. (rpl (concat stars add-stars " ")))
  15998. (while (< (setq l (1+ l)) l2)
  15999. (if itemp
  16000. (and (org-at-item-p) (replace-match rpl t t))
  16001. (unless (org-on-heading-p)
  16002. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16003. (replace-match (concat rpl (match-string 2))))))
  16004. (beginning-of-line 2)))))))
  16005. (defun org-meta-return (&optional arg)
  16006. "Insert a new heading or wrap a region in a table.
  16007. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16008. See the individual commands for more information."
  16009. (interactive "P")
  16010. (cond
  16011. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16012. ((org-at-table-p)
  16013. (call-interactively 'org-table-wrap-region))
  16014. (t (call-interactively 'org-insert-heading))))
  16015. ;;; Menu entries
  16016. ;; Define the Org-mode menus
  16017. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16018. '("Tbl"
  16019. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16020. ["Next Field" org-cycle (org-at-table-p)]
  16021. ["Previous Field" org-shifttab (org-at-table-p)]
  16022. ["Next Row" org-return (org-at-table-p)]
  16023. "--"
  16024. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16025. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16026. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16027. "--"
  16028. ("Column"
  16029. ["Move Column Left" org-metaleft (org-at-table-p)]
  16030. ["Move Column Right" org-metaright (org-at-table-p)]
  16031. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16032. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16033. ("Row"
  16034. ["Move Row Up" org-metaup (org-at-table-p)]
  16035. ["Move Row Down" org-metadown (org-at-table-p)]
  16036. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16037. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16038. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16039. "--"
  16040. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16041. ("Rectangle"
  16042. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16043. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16044. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16045. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16046. "--"
  16047. ("Calculate"
  16048. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16049. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16050. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16051. "--"
  16052. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16053. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16054. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16055. "--"
  16056. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16057. "--"
  16058. ["Sum Column/Rectangle" org-table-sum
  16059. (or (org-at-table-p) (org-region-active-p))]
  16060. ["Which Column?" org-table-current-column (org-at-table-p)])
  16061. ["Debug Formulas"
  16062. org-table-toggle-formula-debugger
  16063. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16064. ["Show Col/Row Numbers"
  16065. org-table-toggle-coordinate-overlays
  16066. :style toggle
  16067. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16068. "--"
  16069. ["Create" org-table-create (and (not (org-at-table-p))
  16070. org-enable-table-editor)]
  16071. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16072. ["Import from File" org-table-import (not (org-at-table-p))]
  16073. ["Export to File" org-table-export (org-at-table-p)]
  16074. "--"
  16075. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16076. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16077. '("Org"
  16078. ("Show/Hide"
  16079. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16080. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16081. ["Sparse Tree..." org-sparse-tree t]
  16082. ["Reveal Context" org-reveal t]
  16083. ["Show All" show-all t]
  16084. "--"
  16085. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16086. "--"
  16087. ["New Heading" org-insert-heading t]
  16088. ("Navigate Headings"
  16089. ["Up" outline-up-heading t]
  16090. ["Next" outline-next-visible-heading t]
  16091. ["Previous" outline-previous-visible-heading t]
  16092. ["Next Same Level" outline-forward-same-level t]
  16093. ["Previous Same Level" outline-backward-same-level t]
  16094. "--"
  16095. ["Jump" org-goto t])
  16096. ("Edit Structure"
  16097. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16098. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16099. "--"
  16100. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16101. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16102. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16103. "--"
  16104. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16105. "--"
  16106. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16107. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16108. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16109. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16110. "--"
  16111. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16112. "--"
  16113. ["Convert to odd levels" org-convert-to-odd-levels t]
  16114. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16115. ("Editing"
  16116. ["Emphasis..." org-emphasize t]
  16117. ["Edit Source Example" org-edit-special t]
  16118. "--"
  16119. ["Footnote new/jump" org-footnote-action t]
  16120. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16121. ("Archive"
  16122. ["Archive (default method)" org-archive-subtree-default t]
  16123. "--"
  16124. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16125. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16126. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16127. )
  16128. "--"
  16129. ("Hyperlinks"
  16130. ["Store Link (Global)" org-store-link t]
  16131. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16132. ["Insert Link" org-insert-link t]
  16133. ["Follow Link" org-open-at-point t]
  16134. "--"
  16135. ["Next link" org-next-link t]
  16136. ["Previous link" org-previous-link t]
  16137. "--"
  16138. ["Descriptive Links"
  16139. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16140. :style radio
  16141. :selected (member '(org-link) buffer-invisibility-spec)]
  16142. ["Literal Links"
  16143. (progn
  16144. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16145. :style radio
  16146. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16147. "--"
  16148. ("TODO Lists"
  16149. ["TODO/DONE/-" org-todo t]
  16150. ("Select keyword"
  16151. ["Next keyword" org-shiftright (org-on-heading-p)]
  16152. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16153. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  16154. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16155. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16156. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16157. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16158. "--"
  16159. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16160. :selected org-enforce-todo-dependencies :style toggle :active t]
  16161. "Settings for tree at point"
  16162. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16163. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16164. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16165. ["Do Children parallel" org-toggle-ordered-property :style radio
  16166. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16167. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16168. "--"
  16169. ["Set Priority" org-priority t]
  16170. ["Priority Up" org-shiftup t]
  16171. ["Priority Down" org-shiftdown t]
  16172. "--"
  16173. ["Get news from all feeds" org-feed-update-all t]
  16174. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16175. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16176. ("TAGS and Properties"
  16177. ["Set Tags" org-set-tags-command t]
  16178. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16179. "--"
  16180. ["Set property" org-set-property t]
  16181. ["Column view of properties" org-columns t]
  16182. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16183. ("Dates and Scheduling"
  16184. ["Timestamp" org-time-stamp t]
  16185. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16186. ("Change Date"
  16187. ["1 Day Later" org-shiftright t]
  16188. ["1 Day Earlier" org-shiftleft t]
  16189. ["1 ... Later" org-shiftup t]
  16190. ["1 ... Earlier" org-shiftdown t])
  16191. ["Compute Time Range" org-evaluate-time-range t]
  16192. ["Schedule Item" org-schedule t]
  16193. ["Deadline" org-deadline t]
  16194. "--"
  16195. ["Custom time format" org-toggle-time-stamp-overlays
  16196. :style radio :selected org-display-custom-times]
  16197. "--"
  16198. ["Goto Calendar" org-goto-calendar t]
  16199. ["Date from Calendar" org-date-from-calendar t]
  16200. "--"
  16201. ["Start/Restart Timer" org-timer-start t]
  16202. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16203. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16204. ["Insert Timer String" org-timer t]
  16205. ["Insert Timer Item" org-timer-item t])
  16206. ("Logging work"
  16207. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16208. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16209. ["Clock out" org-clock-out t]
  16210. ["Clock cancel" org-clock-cancel t]
  16211. "--"
  16212. ["Mark as default task" org-clock-mark-default-task t]
  16213. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16214. ["Goto running clock" org-clock-goto t]
  16215. "--"
  16216. ["Display times" org-clock-display t]
  16217. ["Create clock table" org-clock-report t]
  16218. "--"
  16219. ["Record DONE time"
  16220. (progn (setq org-log-done (not org-log-done))
  16221. (message "Switching to %s will %s record a timestamp"
  16222. (car org-done-keywords)
  16223. (if org-log-done "automatically" "not")))
  16224. :style toggle :selected org-log-done])
  16225. "--"
  16226. ["Agenda Command..." org-agenda t]
  16227. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16228. ("File List for Agenda")
  16229. ("Special views current file"
  16230. ["TODO Tree" org-show-todo-tree t]
  16231. ["Check Deadlines" org-check-deadlines t]
  16232. ["Timeline" org-timeline t]
  16233. ["Tags/Property tree" org-match-sparse-tree t])
  16234. "--"
  16235. ["Export/Publish..." org-export t]
  16236. ("LaTeX"
  16237. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16238. :selected org-cdlatex-mode]
  16239. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16240. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16241. ["Modify math symbol" org-cdlatex-math-modify
  16242. (org-inside-LaTeX-fragment-p)]
  16243. ["Insert citation" org-reftex-citation t]
  16244. "--"
  16245. ["Template for BEAMER" org-insert-beamer-options-template t])
  16246. "--"
  16247. ("MobileOrg"
  16248. ["Push Files and Views" org-mobile-push t]
  16249. ["Get Captured and Flagged" org-mobile-pull t]
  16250. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16251. "--"
  16252. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16253. "--"
  16254. ("Documentation"
  16255. ["Show Version" org-version t]
  16256. ["Info Documentation" org-info t])
  16257. ("Customize"
  16258. ["Browse Org Group" org-customize t]
  16259. "--"
  16260. ["Expand This Menu" org-create-customize-menu
  16261. (fboundp 'customize-menu-create)])
  16262. ["Send bug report" org-submit-bug-report t]
  16263. "--"
  16264. ("Refresh/Reload"
  16265. ["Refresh setup current buffer" org-mode-restart t]
  16266. ["Reload Org (after update)" org-reload t]
  16267. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16268. ))
  16269. (defun org-info (&optional node)
  16270. "Read documentation for Org-mode in the info system.
  16271. With optional NODE, go directly to that node."
  16272. (interactive)
  16273. (info (format "(org)%s" (or node ""))))
  16274. ;;;###autoload
  16275. (defun org-submit-bug-report ()
  16276. "Submit a bug report on Org-mode via mail.
  16277. Don't hesitate to report any problems or inaccurate documentation.
  16278. If you don't have setup sending mail from (X)Emacs, please copy the
  16279. output buffer into your mail program, as it gives us important
  16280. information about your Org-mode version and configuration."
  16281. (interactive)
  16282. (require 'reporter)
  16283. (org-load-modules-maybe)
  16284. (org-require-autoloaded-modules)
  16285. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16286. (reporter-submit-bug-report
  16287. "emacs-orgmode@gnu.org"
  16288. (org-version)
  16289. (let (list)
  16290. (save-window-excursion
  16291. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16292. (delete-other-windows)
  16293. (erase-buffer)
  16294. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16295. We would like to add your full Org-mode and Outline configuration to the
  16296. bug report. This greatly simplifies the work of the maintainer and
  16297. other experts on the mailing list.
  16298. HOWEVER, some variables you have customized may contain private
  16299. information. The names of customers, colleagues, or friends, might
  16300. appear in the form of file names, tags, todo states, or search strings.
  16301. If you answer yes to the prompt, you might want to check and remove
  16302. such private information before sending the email.")
  16303. (add-text-properties (point-min) (point-max) '(face org-warning))
  16304. (when (yes-or-no-p "Include your Org-mode configuration ")
  16305. (mapatoms
  16306. (lambda (v)
  16307. (and (boundp v)
  16308. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16309. (or (and (symbol-value v)
  16310. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16311. (and
  16312. (get v 'custom-type) (get v 'standard-value)
  16313. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16314. (push v list)))))
  16315. (kill-buffer (get-buffer "*Warn about privacy*"))
  16316. list))
  16317. nil nil
  16318. "Remember to cover the basics, that is, what you expected to happen and
  16319. what in fact did happen. You don't know how to make a good report? See
  16320. http://orgmode.org/manual/Feedback.html#Feedback
  16321. Your bug report will be posted to the Org-mode mailing list.
  16322. ------------------------------------------------------------------------")
  16323. (save-excursion
  16324. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16325. (replace-match "\\1Bug: \\3 [\\2]")))))
  16326. (defun org-install-agenda-files-menu ()
  16327. (let ((bl (buffer-list)))
  16328. (save-excursion
  16329. (while bl
  16330. (set-buffer (pop bl))
  16331. (if (org-mode-p) (setq bl nil)))
  16332. (when (org-mode-p)
  16333. (easy-menu-change
  16334. '("Org") "File List for Agenda"
  16335. (append
  16336. (list
  16337. ["Edit File List" (org-edit-agenda-file-list) t]
  16338. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16339. ["Remove Current File from List" org-remove-file t]
  16340. ["Cycle through agenda files" org-cycle-agenda-files t]
  16341. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16342. "--")
  16343. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16344. ;;;; Documentation
  16345. ;;;###autoload
  16346. (defun org-require-autoloaded-modules ()
  16347. (interactive)
  16348. (mapc 'require
  16349. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16350. org-docbook org-exp org-html org-icalendar
  16351. org-id org-latex
  16352. org-publish org-remember org-table
  16353. org-timer org-xoxo)))
  16354. ;;;###autoload
  16355. (defun org-reload (&optional uncompiled)
  16356. "Reload all org lisp files.
  16357. With prefix arg UNCOMPILED, load the uncompiled versions."
  16358. (interactive "P")
  16359. (require 'find-func)
  16360. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16361. (dir-org (file-name-directory (org-find-library-name "org")))
  16362. (dir-org-contrib (ignore-errors
  16363. (file-name-directory
  16364. (org-find-library-name "org-contribdir"))))
  16365. (babel-files
  16366. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16367. (append (list nil "comint" "eval" "exp" "keys"
  16368. "lob" "ref" "table" "tangle")
  16369. (delq nil
  16370. (mapcar
  16371. (lambda (lang)
  16372. (when (cdr lang) (symbol-name (car lang))))
  16373. org-babel-load-languages)))))
  16374. (files
  16375. (append (directory-files dir-org t file-re)
  16376. babel-files
  16377. (and dir-org-contrib
  16378. (directory-files dir-org-contrib t file-re))))
  16379. (remove-re (concat (if (featurep 'xemacs)
  16380. "org-colview" "org-colview-xemacs")
  16381. "\\'")))
  16382. (setq files (mapcar 'file-name-sans-extension files))
  16383. (setq files (mapcar
  16384. (lambda (x) (if (string-match remove-re x) nil x))
  16385. files))
  16386. (setq files (delq nil files))
  16387. (mapc
  16388. (lambda (f)
  16389. (when (featurep (intern (file-name-nondirectory f)))
  16390. (if (and (not uncompiled)
  16391. (file-exists-p (concat f ".elc")))
  16392. (load (concat f ".elc") nil nil t)
  16393. (load (concat f ".el") nil nil t))))
  16394. files))
  16395. (org-version))
  16396. ;;;###autoload
  16397. (defun org-customize ()
  16398. "Call the customize function with org as argument."
  16399. (interactive)
  16400. (org-load-modules-maybe)
  16401. (org-require-autoloaded-modules)
  16402. (customize-browse 'org))
  16403. (defun org-create-customize-menu ()
  16404. "Create a full customization menu for Org-mode, insert it into the menu."
  16405. (interactive)
  16406. (org-load-modules-maybe)
  16407. (org-require-autoloaded-modules)
  16408. (if (fboundp 'customize-menu-create)
  16409. (progn
  16410. (easy-menu-change
  16411. '("Org") "Customize"
  16412. `(["Browse Org group" org-customize t]
  16413. "--"
  16414. ,(customize-menu-create 'org)
  16415. ["Set" Custom-set t]
  16416. ["Save" Custom-save t]
  16417. ["Reset to Current" Custom-reset-current t]
  16418. ["Reset to Saved" Custom-reset-saved t]
  16419. ["Reset to Standard Settings" Custom-reset-standard t]))
  16420. (message "\"Org\"-menu now contains full customization menu"))
  16421. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16422. ;;;; Miscellaneous stuff
  16423. ;;; Generally useful functions
  16424. (defun org-get-at-bol (property)
  16425. "Get text property PROPERTY at beginning of line."
  16426. (get-text-property (point-at-bol) property))
  16427. (defun org-find-text-property-in-string (prop s)
  16428. "Return the first non-nil value of property PROP in string S."
  16429. (or (get-text-property 0 prop s)
  16430. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16431. prop s)))
  16432. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16433. "Display the given MESSAGE as a warning."
  16434. (if (fboundp 'display-warning)
  16435. (display-warning 'org message
  16436. (if (featurep 'xemacs) 'warning :warning))
  16437. (let ((buf (get-buffer-create "*Org warnings*")))
  16438. (with-current-buffer buf
  16439. (goto-char (point-max))
  16440. (insert "Warning (Org): " message)
  16441. (unless (bolp)
  16442. (newline)))
  16443. (display-buffer buf)
  16444. (sit-for 0))))
  16445. (defun org-in-commented-line ()
  16446. "Is point in a line starting with `#'?"
  16447. (equal (char-after (point-at-bol)) ?#))
  16448. (defun org-in-indented-comment-line ()
  16449. "Is point in a line starting with `#' after some white space?"
  16450. (save-excursion
  16451. (save-match-data
  16452. (goto-char (point-at-bol))
  16453. (looking-at "[ \t]*#"))))
  16454. (defun org-in-verbatim-emphasis ()
  16455. (save-match-data
  16456. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16457. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16458. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16459. (if (and marker (marker-buffer marker)
  16460. (buffer-live-p (marker-buffer marker)))
  16461. (progn
  16462. (switch-to-buffer (marker-buffer marker))
  16463. (if (or (> marker (point-max)) (< marker (point-min)))
  16464. (widen))
  16465. (goto-char marker)
  16466. (org-show-context 'org-goto))
  16467. (if bookmark
  16468. (bookmark-jump bookmark)
  16469. (error "Cannot find location"))))
  16470. (defun org-quote-csv-field (s)
  16471. "Quote field for inclusion in CSV material."
  16472. (if (string-match "[\",]" s)
  16473. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16474. s))
  16475. (defun org-plist-delete (plist property)
  16476. "Delete PROPERTY from PLIST.
  16477. This is in contrast to merely setting it to 0."
  16478. (let (p)
  16479. (while plist
  16480. (if (not (eq property (car plist)))
  16481. (setq p (plist-put p (car plist) (nth 1 plist))))
  16482. (setq plist (cddr plist)))
  16483. p))
  16484. (defun org-force-self-insert (N)
  16485. "Needed to enforce self-insert under remapping."
  16486. (interactive "p")
  16487. (self-insert-command N))
  16488. (defun org-string-width (s)
  16489. "Compute width of string, ignoring invisible characters.
  16490. This ignores character with invisibility property `org-link', and also
  16491. characters with property `org-cwidth', because these will become invisible
  16492. upon the next fontification round."
  16493. (let (b l)
  16494. (when (or (eq t buffer-invisibility-spec)
  16495. (assq 'org-link buffer-invisibility-spec))
  16496. (while (setq b (text-property-any 0 (length s)
  16497. 'invisible 'org-link s))
  16498. (setq s (concat (substring s 0 b)
  16499. (substring s (or (next-single-property-change
  16500. b 'invisible s) (length s)))))))
  16501. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16502. (setq s (concat (substring s 0 b)
  16503. (substring s (or (next-single-property-change
  16504. b 'org-cwidth s) (length s))))))
  16505. (setq l (string-width s) b -1)
  16506. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16507. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16508. l))
  16509. (defun org-get-indentation (&optional line)
  16510. "Get the indentation of the current line, interpreting tabs.
  16511. When LINE is given, assume it represents a line and compute its indentation."
  16512. (if line
  16513. (if (string-match "^ *" (org-remove-tabs line))
  16514. (match-end 0))
  16515. (save-excursion
  16516. (beginning-of-line 1)
  16517. (skip-chars-forward " \t")
  16518. (current-column))))
  16519. (defun org-remove-tabs (s &optional width)
  16520. "Replace tabulators in S with spaces.
  16521. Assumes that s is a single line, starting in column 0."
  16522. (setq width (or width tab-width))
  16523. (while (string-match "\t" s)
  16524. (setq s (replace-match
  16525. (make-string
  16526. (- (* width (/ (+ (match-beginning 0) width) width))
  16527. (match-beginning 0)) ?\ )
  16528. t t s)))
  16529. s)
  16530. (defun org-fix-indentation (line ind)
  16531. "Fix indentation in LINE.
  16532. IND is a cons cell with target and minimum indentation.
  16533. If the current indentation in LINE is smaller than the minimum,
  16534. leave it alone. If it is larger than ind, set it to the target."
  16535. (let* ((l (org-remove-tabs line))
  16536. (i (org-get-indentation l))
  16537. (i1 (car ind)) (i2 (cdr ind)))
  16538. (if (>= i i2) (setq l (substring line i2)))
  16539. (if (> i1 0)
  16540. (concat (make-string i1 ?\ ) l)
  16541. l)))
  16542. (defun org-remove-indentation (code &optional n)
  16543. "Remove the maximum common indentation from the lines in CODE.
  16544. N may optionally be the number of spaces to remove."
  16545. (with-temp-buffer
  16546. (insert code)
  16547. (org-do-remove-indentation n)
  16548. (buffer-string)))
  16549. (defun org-do-remove-indentation (&optional n)
  16550. "Remove the maximum common indentation from the buffer."
  16551. (untabify (point-min) (point-max))
  16552. (let ((min 10000) re)
  16553. (if n
  16554. (setq min n)
  16555. (goto-char (point-min))
  16556. (while (re-search-forward "^ *[^ \n]" nil t)
  16557. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16558. (unless (or (= min 0) (= min 10000))
  16559. (setq re (format "^ \\{%d\\}" min))
  16560. (goto-char (point-min))
  16561. (while (re-search-forward re nil t)
  16562. (replace-match "")
  16563. (end-of-line 1))
  16564. min)))
  16565. (defun org-fill-template (template alist)
  16566. "Find each %key of ALIST in TEMPLATE and replace it."
  16567. (let ((case-fold-search nil)
  16568. entry key value)
  16569. (setq alist (sort (copy-sequence alist)
  16570. (lambda (a b) (< (length (car a)) (length (car b))))))
  16571. (while (setq entry (pop alist))
  16572. (setq template
  16573. (replace-regexp-in-string
  16574. (concat "%" (regexp-quote (car entry)))
  16575. (cdr entry) template t t)))
  16576. template))
  16577. (defun org-base-buffer (buffer)
  16578. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16579. (if (not buffer)
  16580. buffer
  16581. (or (buffer-base-buffer buffer)
  16582. buffer)))
  16583. (defun org-trim (s)
  16584. "Remove whitespace at beginning and end of string."
  16585. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16586. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16587. s)
  16588. (defun org-wrap (string &optional width lines)
  16589. "Wrap string to either a number of lines, or a width in characters.
  16590. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16591. that costs. If there is a word longer than WIDTH, the text is actually
  16592. wrapped to the length of that word.
  16593. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16594. many lines, whatever width that takes.
  16595. The return value is a list of lines, without newlines at the end."
  16596. (let* ((words (org-split-string string "[ \t\n]+"))
  16597. (maxword (apply 'max (mapcar 'org-string-width words)))
  16598. w ll)
  16599. (cond (width
  16600. (org-do-wrap words (max maxword width)))
  16601. (lines
  16602. (setq w maxword)
  16603. (setq ll (org-do-wrap words maxword))
  16604. (if (<= (length ll) lines)
  16605. ll
  16606. (setq ll words)
  16607. (while (> (length ll) lines)
  16608. (setq w (1+ w))
  16609. (setq ll (org-do-wrap words w)))
  16610. ll))
  16611. (t (error "Cannot wrap this")))))
  16612. (defun org-do-wrap (words width)
  16613. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16614. (let (lines line)
  16615. (while words
  16616. (setq line (pop words))
  16617. (while (and words (< (+ (length line) (length (car words))) width))
  16618. (setq line (concat line " " (pop words))))
  16619. (setq lines (push line lines)))
  16620. (nreverse lines)))
  16621. (defun org-split-string (string &optional separators)
  16622. "Splits STRING into substrings at SEPARATORS.
  16623. No empty strings are returned if there are matches at the beginning
  16624. and end of string."
  16625. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16626. (start 0)
  16627. notfirst
  16628. (list nil))
  16629. (while (and (string-match rexp string
  16630. (if (and notfirst
  16631. (= start (match-beginning 0))
  16632. (< start (length string)))
  16633. (1+ start) start))
  16634. (< (match-beginning 0) (length string)))
  16635. (setq notfirst t)
  16636. (or (eq (match-beginning 0) 0)
  16637. (and (eq (match-beginning 0) (match-end 0))
  16638. (eq (match-beginning 0) start))
  16639. (setq list
  16640. (cons (substring string start (match-beginning 0))
  16641. list)))
  16642. (setq start (match-end 0)))
  16643. (or (eq start (length string))
  16644. (setq list
  16645. (cons (substring string start)
  16646. list)))
  16647. (nreverse list)))
  16648. (defun org-quote-vert (s)
  16649. "Replace \"|\" with \"\\vert\"."
  16650. (while (string-match "|" s)
  16651. (setq s (replace-match "\\vert" t t s)))
  16652. s)
  16653. (defun org-uuidgen-p (s)
  16654. "Is S an ID created by UUIDGEN?"
  16655. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16656. (defun org-context ()
  16657. "Return a list of contexts of the current cursor position.
  16658. If several contexts apply, all are returned.
  16659. Each context entry is a list with a symbol naming the context, and
  16660. two positions indicating start and end of the context. Possible
  16661. contexts are:
  16662. :headline anywhere in a headline
  16663. :headline-stars on the leading stars in a headline
  16664. :todo-keyword on a TODO keyword (including DONE) in a headline
  16665. :tags on the TAGS in a headline
  16666. :priority on the priority cookie in a headline
  16667. :item on the first line of a plain list item
  16668. :item-bullet on the bullet/number of a plain list item
  16669. :checkbox on the checkbox in a plain list item
  16670. :table in an org-mode table
  16671. :table-special on a special filed in a table
  16672. :table-table in a table.el table
  16673. :link on a hyperlink
  16674. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16675. :target on a <<target>>
  16676. :radio-target on a <<<radio-target>>>
  16677. :latex-fragment on a LaTeX fragment
  16678. :latex-preview on a LaTeX fragment with overlayed preview image
  16679. This function expects the position to be visible because it uses font-lock
  16680. faces as a help to recognize the following contexts: :table-special, :link,
  16681. and :keyword."
  16682. (let* ((f (get-text-property (point) 'face))
  16683. (faces (if (listp f) f (list f)))
  16684. (p (point)) clist o)
  16685. ;; First the large context
  16686. (cond
  16687. ((org-on-heading-p t)
  16688. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16689. (when (progn
  16690. (beginning-of-line 1)
  16691. (looking-at org-todo-line-tags-regexp))
  16692. (push (org-point-in-group p 1 :headline-stars) clist)
  16693. (push (org-point-in-group p 2 :todo-keyword) clist)
  16694. (push (org-point-in-group p 4 :tags) clist))
  16695. (goto-char p)
  16696. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16697. (if (looking-at "\\[#[A-Z0-9]\\]")
  16698. (push (org-point-in-group p 0 :priority) clist)))
  16699. ((org-at-item-p)
  16700. (push (org-point-in-group p 2 :item-bullet) clist)
  16701. (push (list :item (point-at-bol)
  16702. (save-excursion (org-end-of-item) (point)))
  16703. clist)
  16704. (and (org-at-item-checkbox-p)
  16705. (push (org-point-in-group p 0 :checkbox) clist)))
  16706. ((org-at-table-p)
  16707. (push (list :table (org-table-begin) (org-table-end)) clist)
  16708. (if (memq 'org-formula faces)
  16709. (push (list :table-special
  16710. (previous-single-property-change p 'face)
  16711. (next-single-property-change p 'face)) clist)))
  16712. ((org-at-table-p 'any)
  16713. (push (list :table-table) clist)))
  16714. (goto-char p)
  16715. ;; Now the small context
  16716. (cond
  16717. ((org-at-timestamp-p)
  16718. (push (org-point-in-group p 0 :timestamp) clist))
  16719. ((memq 'org-link faces)
  16720. (push (list :link
  16721. (previous-single-property-change p 'face)
  16722. (next-single-property-change p 'face)) clist))
  16723. ((memq 'org-special-keyword faces)
  16724. (push (list :keyword
  16725. (previous-single-property-change p 'face)
  16726. (next-single-property-change p 'face)) clist))
  16727. ((org-on-target-p)
  16728. (push (org-point-in-group p 0 :target) clist)
  16729. (goto-char (1- (match-beginning 0)))
  16730. (if (looking-at org-radio-target-regexp)
  16731. (push (org-point-in-group p 0 :radio-target) clist))
  16732. (goto-char p))
  16733. ((setq o (car (delq nil
  16734. (mapcar
  16735. (lambda (x)
  16736. (if (memq x org-latex-fragment-image-overlays) x))
  16737. (overlays-at (point))))))
  16738. (push (list :latex-fragment
  16739. (overlay-start o) (overlay-end o)) clist)
  16740. (push (list :latex-preview
  16741. (overlay-start o) (overlay-end o)) clist))
  16742. ((org-inside-LaTeX-fragment-p)
  16743. ;; FIXME: positions wrong.
  16744. (push (list :latex-fragment (point) (point)) clist)))
  16745. (setq clist (nreverse (delq nil clist)))
  16746. clist))
  16747. ;; FIXME: Compare with at-regexp-p Do we need both?
  16748. (defun org-in-regexp (re &optional nlines visually)
  16749. "Check if point is inside a match of regexp.
  16750. Normally only the current line is checked, but you can include NLINES extra
  16751. lines both before and after point into the search.
  16752. If VISUALLY is set, require that the cursor is not after the match but
  16753. really on, so that the block visually is on the match."
  16754. (catch 'exit
  16755. (let ((pos (point))
  16756. (eol (point-at-eol (+ 1 (or nlines 0))))
  16757. (inc (if visually 1 0)))
  16758. (save-excursion
  16759. (beginning-of-line (- 1 (or nlines 0)))
  16760. (while (re-search-forward re eol t)
  16761. (if (and (<= (match-beginning 0) pos)
  16762. (>= (+ inc (match-end 0)) pos))
  16763. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16764. (defun org-at-regexp-p (regexp)
  16765. "Is point inside a match of REGEXP in the current line?"
  16766. (catch 'exit
  16767. (save-excursion
  16768. (let ((pos (point)) (end (point-at-eol)))
  16769. (beginning-of-line 1)
  16770. (while (re-search-forward regexp end t)
  16771. (if (and (<= (match-beginning 0) pos)
  16772. (>= (match-end 0) pos))
  16773. (throw 'exit t)))
  16774. nil))))
  16775. (defun org-in-regexps-block-p (start-re end-re)
  16776. "Return t if the current point is between matches of START-RE and END-RE.
  16777. This will also return to if point is on one of the two matches."
  16778. (interactive)
  16779. (let ((p (point)))
  16780. (save-excursion
  16781. (and (or (org-at-regexp-p start-re)
  16782. (re-search-backward start-re nil t))
  16783. (re-search-forward end-re nil t)
  16784. (>= (point) p)))))
  16785. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16786. "Call `multi-occur' with buffers for all agenda files."
  16787. (interactive "sOrg-files matching: \np")
  16788. (let* ((files (org-agenda-files))
  16789. (tnames (mapcar 'file-truename files))
  16790. (extra org-agenda-text-search-extra-files)
  16791. f)
  16792. (when (eq (car extra) 'agenda-archives)
  16793. (setq extra (cdr extra))
  16794. (setq files (org-add-archive-files files)))
  16795. (while (setq f (pop extra))
  16796. (unless (member (file-truename f) tnames)
  16797. (add-to-list 'files f 'append)
  16798. (add-to-list 'tnames (file-truename f) 'append)))
  16799. (multi-occur
  16800. (mapcar (lambda (x)
  16801. (with-current-buffer
  16802. (or (get-file-buffer x) (find-file-noselect x))
  16803. (widen)
  16804. (current-buffer)))
  16805. files)
  16806. regexp)))
  16807. (if (boundp 'occur-mode-find-occurrence-hook)
  16808. ;; Emacs 23
  16809. (add-hook 'occur-mode-find-occurrence-hook
  16810. (lambda ()
  16811. (when (org-mode-p)
  16812. (org-reveal))))
  16813. ;; Emacs 22
  16814. (defadvice occur-mode-goto-occurrence
  16815. (after org-occur-reveal activate)
  16816. (and (org-mode-p) (org-reveal)))
  16817. (defadvice occur-mode-goto-occurrence-other-window
  16818. (after org-occur-reveal activate)
  16819. (and (org-mode-p) (org-reveal)))
  16820. (defadvice occur-mode-display-occurrence
  16821. (after org-occur-reveal activate)
  16822. (when (org-mode-p)
  16823. (let ((pos (occur-mode-find-occurrence)))
  16824. (with-current-buffer (marker-buffer pos)
  16825. (save-excursion
  16826. (goto-char pos)
  16827. (org-reveal)))))))
  16828. (defun org-occur-link-in-agenda-files ()
  16829. "Create a link and search for it in the agendas.
  16830. The link is not stored in `org-stored-links', it is just created
  16831. for the search purpose."
  16832. (interactive)
  16833. (let ((link (condition-case nil
  16834. (org-store-link nil)
  16835. (error "Unable to create a link to here"))))
  16836. (org-occur-in-agenda-files (regexp-quote link))))
  16837. (defun org-uniquify (list)
  16838. "Remove duplicate elements from LIST."
  16839. (let (res)
  16840. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16841. res))
  16842. (defun org-delete-all (elts list)
  16843. "Remove all elements in ELTS from LIST."
  16844. (while elts
  16845. (setq list (delete (pop elts) list)))
  16846. list)
  16847. (defun org-count (cl-item cl-seq)
  16848. "Count the number of occurrences of ITEM in SEQ.
  16849. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16850. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16851. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16852. (while (< cl-start cl-end)
  16853. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16854. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16855. (setq cl-start (1+ cl-start)))
  16856. cl-count))
  16857. (defun org-remove-if (predicate seq)
  16858. "Remove everything from SEQ that fulfills PREDICATE."
  16859. (let (res e)
  16860. (while seq
  16861. (setq e (pop seq))
  16862. (if (not (funcall predicate e)) (push e res)))
  16863. (nreverse res)))
  16864. (defun org-remove-if-not (predicate seq)
  16865. "Remove everything from SEQ that does not fulfill PREDICATE."
  16866. (let (res e)
  16867. (while seq
  16868. (setq e (pop seq))
  16869. (if (funcall predicate e) (push e res)))
  16870. (nreverse res)))
  16871. (defun org-back-over-empty-lines ()
  16872. "Move backwards over whitespace, to the beginning of the first empty line.
  16873. Returns the number of empty lines passed."
  16874. (let ((pos (point)))
  16875. (skip-chars-backward " \t\n\r")
  16876. (beginning-of-line 2)
  16877. (goto-char (min (point) pos))
  16878. (count-lines (point) pos)))
  16879. (defun org-skip-whitespace ()
  16880. (skip-chars-forward " \t\n\r"))
  16881. (defun org-point-in-group (point group &optional context)
  16882. "Check if POINT is in match-group GROUP.
  16883. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16884. match. If the match group does not exist or point is not inside it,
  16885. return nil."
  16886. (and (match-beginning group)
  16887. (>= point (match-beginning group))
  16888. (<= point (match-end group))
  16889. (if context
  16890. (list context (match-beginning group) (match-end group))
  16891. t)))
  16892. (defun org-switch-to-buffer-other-window (&rest args)
  16893. "Switch to buffer in a second window on the current frame.
  16894. In particular, do not allow pop-up frames.
  16895. Returns the newly created buffer."
  16896. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16897. special-display-function)
  16898. (apply 'switch-to-buffer-other-window args)))
  16899. (defun org-combine-plists (&rest plists)
  16900. "Create a single property list from all plists in PLISTS.
  16901. The process starts by copying the first list, and then setting properties
  16902. from the other lists. Settings in the last list are the most significant
  16903. ones and overrule settings in the other lists."
  16904. (let ((rtn (copy-sequence (pop plists)))
  16905. p v ls)
  16906. (while plists
  16907. (setq ls (pop plists))
  16908. (while ls
  16909. (setq p (pop ls) v (pop ls))
  16910. (setq rtn (plist-put rtn p v))))
  16911. rtn))
  16912. (defun org-move-line-down (arg)
  16913. "Move the current line down. With prefix argument, move it past ARG lines."
  16914. (interactive "p")
  16915. (let ((col (current-column))
  16916. beg end pos)
  16917. (beginning-of-line 1) (setq beg (point))
  16918. (beginning-of-line 2) (setq end (point))
  16919. (beginning-of-line (+ 1 arg))
  16920. (setq pos (move-marker (make-marker) (point)))
  16921. (insert (delete-and-extract-region beg end))
  16922. (goto-char pos)
  16923. (org-move-to-column col)))
  16924. (defun org-move-line-up (arg)
  16925. "Move the current line up. With prefix argument, move it past ARG lines."
  16926. (interactive "p")
  16927. (let ((col (current-column))
  16928. beg end pos)
  16929. (beginning-of-line 1) (setq beg (point))
  16930. (beginning-of-line 2) (setq end (point))
  16931. (beginning-of-line (- arg))
  16932. (setq pos (move-marker (make-marker) (point)))
  16933. (insert (delete-and-extract-region beg end))
  16934. (goto-char pos)
  16935. (org-move-to-column col)))
  16936. (defun org-replace-escapes (string table)
  16937. "Replace %-escapes in STRING with values in TABLE.
  16938. TABLE is an association list with keys like \"%a\" and string values.
  16939. The sequences in STRING may contain normal field width and padding information,
  16940. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16941. so values can contain further %-escapes if they are define later in TABLE."
  16942. (let ((tbl (copy-alist table))
  16943. (case-fold-search nil)
  16944. (pchg 0)
  16945. e re rpl)
  16946. (while (setq e (pop tbl))
  16947. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16948. (when (and (cdr e) (string-match re (cdr e)))
  16949. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16950. (safe "SREF"))
  16951. (add-text-properties 0 3 (list 'sref sref) safe)
  16952. (setcdr e (replace-match safe t t (cdr e)))))
  16953. (while (string-match re string)
  16954. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16955. (cdr e)))
  16956. (setq string (replace-match rpl t t string))))
  16957. (while (setq pchg (next-property-change pchg string))
  16958. (let ((sref (get-text-property pchg 'sref string)))
  16959. (when (and sref (string-match "SREF" string pchg))
  16960. (setq string (replace-match sref t t string)))))
  16961. string))
  16962. (defun org-sublist (list start end)
  16963. "Return a section of LIST, from START to END.
  16964. Counting starts at 1."
  16965. (let (rtn (c start))
  16966. (setq list (nthcdr (1- start) list))
  16967. (while (and list (<= c end))
  16968. (push (pop list) rtn)
  16969. (setq c (1+ c)))
  16970. (nreverse rtn)))
  16971. (defun org-find-base-buffer-visiting (file)
  16972. "Like `find-buffer-visiting' but always return the base buffer and
  16973. not an indirect buffer."
  16974. (let ((buf (or (get-file-buffer file)
  16975. (find-buffer-visiting file))))
  16976. (if buf
  16977. (or (buffer-base-buffer buf) buf)
  16978. nil)))
  16979. (defun org-image-file-name-regexp (&optional extensions)
  16980. "Return regexp matching the file names of images.
  16981. If EXTENSIONS is given, only match these."
  16982. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16983. (image-file-name-regexp)
  16984. (let ((image-file-name-extensions
  16985. (or extensions
  16986. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16987. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16988. (concat "\\."
  16989. (regexp-opt (nconc (mapcar 'upcase
  16990. image-file-name-extensions)
  16991. image-file-name-extensions)
  16992. t)
  16993. "\\'"))))
  16994. (defun org-file-image-p (file &optional extensions)
  16995. "Return non-nil if FILE is an image."
  16996. (save-match-data
  16997. (string-match (org-image-file-name-regexp extensions) file)))
  16998. (defun org-get-cursor-date ()
  16999. "Return the date at cursor in as a time.
  17000. This works in the calendar and in the agenda, anywhere else it just
  17001. returns the current time."
  17002. (let (date day defd)
  17003. (cond
  17004. ((eq major-mode 'calendar-mode)
  17005. (setq date (calendar-cursor-to-date)
  17006. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17007. ((eq major-mode 'org-agenda-mode)
  17008. (setq day (get-text-property (point) 'day))
  17009. (if day
  17010. (setq date (calendar-gregorian-from-absolute day)
  17011. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17012. (nth 2 date))))))
  17013. (or defd (current-time))))
  17014. (defvar org-agenda-action-marker (make-marker)
  17015. "Marker pointing to the entry for the next agenda action.")
  17016. (defun org-mark-entry-for-agenda-action ()
  17017. "Mark the current entry as target of an agenda action.
  17018. Agenda actions are actions executed from the agenda with the key `k',
  17019. which make use of the date at the cursor."
  17020. (interactive)
  17021. (move-marker org-agenda-action-marker
  17022. (save-excursion (org-back-to-heading t) (point))
  17023. (current-buffer))
  17024. (message
  17025. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17026. ;;; Paragraph filling stuff.
  17027. ;; We want this to be just right, so use the full arsenal.
  17028. (defun org-indent-line-function ()
  17029. "Indent line like previous, but further if previous was headline or item."
  17030. (interactive)
  17031. (let* ((pos (point))
  17032. (itemp (org-at-item-p))
  17033. (case-fold-search t)
  17034. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17035. column bpos bcol tpos tcol bullet btype bullet-type)
  17036. ;; Find the previous relevant line
  17037. (beginning-of-line 1)
  17038. (cond
  17039. ((looking-at "#") (setq column 0))
  17040. ((looking-at "\\*+ ") (setq column 0))
  17041. ((and (looking-at "[ \t]*:END:")
  17042. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17043. (save-excursion
  17044. (goto-char (1- (match-beginning 1)))
  17045. (setq column (current-column))))
  17046. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  17047. (save-excursion
  17048. (re-search-backward
  17049. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17050. (setq column (org-get-indentation (match-string 0))))
  17051. (t
  17052. (beginning-of-line 0)
  17053. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  17054. (not (looking-at "[ \t]*:END:"))
  17055. (not (looking-at org-drawer-regexp)))
  17056. (beginning-of-line 0))
  17057. (cond
  17058. ((looking-at "\\*+[ \t]+")
  17059. (if (not org-adapt-indentation)
  17060. (setq column 0)
  17061. (goto-char (match-end 0))
  17062. (setq column (current-column))))
  17063. ((looking-at org-drawer-regexp)
  17064. (goto-char (1- (match-beginning 1)))
  17065. (setq column (current-column)))
  17066. ((looking-at "\\([ \t]*\\):END:")
  17067. (goto-char (match-end 1))
  17068. (setq column (current-column)))
  17069. ((org-in-item-p)
  17070. (org-beginning-of-item)
  17071. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17072. (setq bpos (match-beginning 1) tpos (match-end 0)
  17073. bcol (progn (goto-char bpos) (current-column))
  17074. tcol (progn (goto-char tpos) (current-column))
  17075. bullet (match-string 1)
  17076. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  17077. (if (> tcol (+ bcol org-description-max-indent))
  17078. (setq tcol (+ bcol 5)))
  17079. (if (not itemp)
  17080. (setq column tcol)
  17081. (goto-char pos)
  17082. (beginning-of-line 1)
  17083. (if (looking-at "\\S-")
  17084. (progn
  17085. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  17086. (setq bullet (match-string 1)
  17087. btype (if (string-match "[0-9]" bullet) "n" bullet))
  17088. (setq column (if (equal btype bullet-type) bcol tcol)))
  17089. (setq column (org-get-indentation)))))
  17090. (t (setq column (org-get-indentation))))))
  17091. (goto-char pos)
  17092. (if (<= (current-column) (current-indentation))
  17093. (org-indent-line-to column)
  17094. (save-excursion (org-indent-line-to column)))
  17095. (setq column (current-column))
  17096. (beginning-of-line 1)
  17097. (if (looking-at
  17098. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17099. (replace-match (concat (match-string 1)
  17100. (format org-property-format
  17101. (match-string 2) (match-string 3)))
  17102. t t))
  17103. (org-move-to-column column)))
  17104. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17105. "Variable to store copy of `adaptive-fill-regexp'.
  17106. Since `adaptive-fill-regexp' is set to never match, we need to
  17107. store a backup of its value before entering `org-mode' so that
  17108. the functionality can be provided as a fall-back.")
  17109. (defun org-set-autofill-regexps ()
  17110. (interactive)
  17111. ;; In the paragraph separator we include headlines, because filling
  17112. ;; text in a line directly attached to a headline would otherwise
  17113. ;; fill the headline as well.
  17114. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17115. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17116. ;; The paragraph starter includes hand-formatted lists.
  17117. (org-set-local
  17118. 'paragraph-start
  17119. (concat
  17120. "\f" "\\|"
  17121. "[ ]*$" "\\|"
  17122. "\\*+ " "\\|"
  17123. "[ \t]*#" "\\|"
  17124. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17125. "[ \t]*[:|]" "\\|"
  17126. "\\$\\$" "\\|"
  17127. "\\\\\\(begin\\|end\\|[][]\\)"))
  17128. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17129. ;; But only if the user has not turned off tables or fixed-width regions
  17130. (org-set-local
  17131. 'auto-fill-inhibit-regexp
  17132. (concat "\\*+ \\|#\\+"
  17133. "\\|[ \t]*" org-keyword-time-regexp
  17134. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17135. (concat
  17136. "\\|[ \t]*["
  17137. (if org-enable-table-editor "|" "")
  17138. (if org-enable-fixed-width-editor ":" "")
  17139. "]"))))
  17140. ;; We use our own fill-paragraph function, to make sure that tables
  17141. ;; and fixed-width regions are not wrapped. That function will pass
  17142. ;; through to `fill-paragraph' when appropriate.
  17143. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17144. ;; Adaptive filling: To get full control, first make sure that
  17145. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17146. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17147. (org-set-local 'org-adaptive-fill-regexp-backup
  17148. adaptive-fill-regexp))
  17149. (org-set-local 'adaptive-fill-regexp "\000")
  17150. (org-set-local 'adaptive-fill-function
  17151. 'org-adaptive-fill-function)
  17152. (org-set-local
  17153. 'align-mode-rules-list
  17154. '((org-in-buffer-settings
  17155. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17156. (modes . '(org-mode))))))
  17157. (defun org-fill-paragraph (&optional justify)
  17158. "Re-align a table, pass through to fill-paragraph if no table."
  17159. (let ((table-p (org-at-table-p))
  17160. (table.el-p (org-at-table.el-p)))
  17161. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17162. (save-excursion (goto-char (point-at-bol))
  17163. (looking-at outline-regexp)))
  17164. t) ; skip headlines
  17165. (table.el-p t) ; skip table.el tables
  17166. (table-p (org-table-align) t) ; align org-mode tables
  17167. (t nil)))) ; call paragraph-fill
  17168. ;; For reference, this is the default value of adaptive-fill-regexp
  17169. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17170. (defun org-adaptive-fill-function ()
  17171. "Return a fill prefix for org-mode files.
  17172. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17173. work correctly."
  17174. (cond
  17175. ;; Comment line
  17176. ((looking-at "#[ \t]+")
  17177. (match-string-no-properties 0))
  17178. ;; Description list
  17179. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17180. (save-excursion
  17181. (if (> (match-end 1) (+ (match-beginning 1)
  17182. org-description-max-indent))
  17183. (goto-char (+ (match-beginning 1) 5))
  17184. (goto-char (match-end 0)))
  17185. (make-string (current-column) ?\ )))
  17186. ;; Ordered or unordered list
  17187. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17188. (save-excursion
  17189. (goto-char (match-end 0))
  17190. (make-string (current-column) ?\ )))
  17191. ;; Other text
  17192. ((looking-at org-adaptive-fill-regexp-backup)
  17193. (match-string-no-properties 0))))
  17194. ;;; Other stuff.
  17195. (defun org-toggle-fixed-width-section (arg)
  17196. "Toggle the fixed-width export.
  17197. If there is no active region, the QUOTE keyword at the current headline is
  17198. inserted or removed. When present, it causes the text between this headline
  17199. and the next to be exported as fixed-width text, and unmodified.
  17200. If there is an active region, this command adds or removes a colon as the
  17201. first character of this line. If the first character of a line is a colon,
  17202. this line is also exported in fixed-width font."
  17203. (interactive "P")
  17204. (let* ((cc 0)
  17205. (regionp (org-region-active-p))
  17206. (beg (if regionp (region-beginning) (point)))
  17207. (end (if regionp (region-end)))
  17208. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17209. (case-fold-search nil)
  17210. (re "[ \t]*\\(: \\)")
  17211. off)
  17212. (if regionp
  17213. (save-excursion
  17214. (goto-char beg)
  17215. (setq cc (current-column))
  17216. (beginning-of-line 1)
  17217. (setq off (looking-at re))
  17218. (while (> nlines 0)
  17219. (setq nlines (1- nlines))
  17220. (beginning-of-line 1)
  17221. (cond
  17222. (arg
  17223. (org-move-to-column cc t)
  17224. (insert ": \n")
  17225. (forward-line -1))
  17226. ((and off (looking-at re))
  17227. (replace-match "" t t nil 1))
  17228. ((not off) (org-move-to-column cc t) (insert ": ")))
  17229. (forward-line 1)))
  17230. (save-excursion
  17231. (org-back-to-heading)
  17232. (if (looking-at (concat outline-regexp
  17233. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17234. (replace-match "" t t nil 1)
  17235. (if (looking-at outline-regexp)
  17236. (progn
  17237. (goto-char (match-end 0))
  17238. (insert org-quote-string " "))))))))
  17239. (defun org-reftex-citation ()
  17240. "Use reftex-citation to insert a citation into the buffer.
  17241. This looks for a line like
  17242. #+BIBLIOGRAPHY: foo plain option:-d
  17243. and derives from it that foo.bib is the bibliography file relevant
  17244. for this document. It then installs the necessary environment for RefTeX
  17245. to work in this buffer and calls `reftex-citation' to insert a citation
  17246. into the buffer.
  17247. Export of such citations to both LaTeX and HTML is handled by the contributed
  17248. package org-exp-bibtex by Taru Karttunen."
  17249. (interactive)
  17250. (let ((reftex-docstruct-symbol 'rds)
  17251. (reftex-cite-format "\\cite{%l}")
  17252. rds bib)
  17253. (save-excursion
  17254. (save-restriction
  17255. (widen)
  17256. (let ((case-fold-search t)
  17257. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17258. (if (not (save-excursion
  17259. (or (re-search-forward re nil t)
  17260. (re-search-backward re nil t))))
  17261. (error "No bibliography defined in file")
  17262. (setq bib (concat (match-string 1) ".bib")
  17263. rds (list (list 'bib bib)))))))
  17264. (call-interactively 'reftex-citation)))
  17265. ;;;; Functions extending outline functionality
  17266. (defun org-beginning-of-line (&optional arg)
  17267. "Go to the beginning of the current line. If that is invisible, continue
  17268. to a visible line beginning. This makes the function of C-a more intuitive.
  17269. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17270. first attempt, and only move to after the tags when the cursor is already
  17271. beyond the end of the headline."
  17272. (interactive "P")
  17273. (let ((pos (point))
  17274. (special (if (consp org-special-ctrl-a/e)
  17275. (car org-special-ctrl-a/e)
  17276. org-special-ctrl-a/e))
  17277. refpos)
  17278. (if (org-bound-and-true-p line-move-visual)
  17279. (beginning-of-visual-line 1)
  17280. (beginning-of-line 1))
  17281. (if (and arg (fboundp 'move-beginning-of-line))
  17282. (call-interactively 'move-beginning-of-line)
  17283. (if (bobp)
  17284. nil
  17285. (backward-char 1)
  17286. (if (org-truely-invisible-p)
  17287. (while (and (not (bobp)) (org-truely-invisible-p))
  17288. (backward-char 1)
  17289. (beginning-of-line 1))
  17290. (forward-char 1))))
  17291. (when special
  17292. (cond
  17293. ((and (looking-at org-complex-heading-regexp)
  17294. (= (char-after (match-end 1)) ?\ ))
  17295. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17296. (point-at-eol)))
  17297. (goto-char
  17298. (if (eq special t)
  17299. (cond ((> pos refpos) refpos)
  17300. ((= pos (point)) refpos)
  17301. (t (point)))
  17302. (cond ((> pos (point)) (point))
  17303. ((not (eq last-command this-command)) (point))
  17304. (t refpos)))))
  17305. ((org-at-item-p)
  17306. (goto-char
  17307. (if (eq special t)
  17308. (cond ((> pos (match-end 4)) (match-end 4))
  17309. ((= pos (point)) (match-end 4))
  17310. (t (point)))
  17311. (cond ((> pos (point)) (point))
  17312. ((not (eq last-command this-command)) (point))
  17313. (t (match-end 4))))))))
  17314. (org-no-warnings
  17315. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17316. (defun org-end-of-line (&optional arg)
  17317. "Go to the end of the line.
  17318. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17319. first attempt, and only move to after the tags when the cursor is already
  17320. beyond the end of the headline."
  17321. (interactive "P")
  17322. (let ((special (if (consp org-special-ctrl-a/e)
  17323. (cdr org-special-ctrl-a/e)
  17324. org-special-ctrl-a/e)))
  17325. (if (or (not special)
  17326. (not (org-on-heading-p))
  17327. arg)
  17328. (call-interactively
  17329. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17330. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17331. (t 'end-of-line)))
  17332. (let ((pos (point)))
  17333. (beginning-of-line 1)
  17334. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  17335. (if (eq special t)
  17336. (if (or (< pos (match-beginning 1))
  17337. (= pos (match-end 0)))
  17338. (goto-char (match-beginning 1))
  17339. (goto-char (match-end 0)))
  17340. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17341. (goto-char (match-end 0))
  17342. (goto-char (match-beginning 1))))
  17343. (call-interactively (if (fboundp 'move-end-of-line)
  17344. 'move-end-of-line
  17345. 'end-of-line)))))
  17346. (org-no-warnings
  17347. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17348. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17349. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17350. (define-key org-mode-map [home] 'org-beginning-of-line)
  17351. (define-key org-mode-map [end] 'org-end-of-line)
  17352. (defun org-backward-sentence (&optional arg)
  17353. "Go to beginning of sentence, or beginning of table field.
  17354. This will call `backward-sentence' or `org-table-beginning-of-field',
  17355. depending on context."
  17356. (interactive "P")
  17357. (cond
  17358. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17359. (t (call-interactively 'backward-sentence))))
  17360. (defun org-forward-sentence (&optional arg)
  17361. "Go to end of sentence, or end of table field.
  17362. This will call `forward-sentence' or `org-table-end-of-field',
  17363. depending on context."
  17364. (interactive "P")
  17365. (cond
  17366. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17367. (t (call-interactively 'forward-sentence))))
  17368. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17369. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17370. (defun org-kill-line (&optional arg)
  17371. "Kill line, to tags or end of line."
  17372. (interactive "P")
  17373. (cond
  17374. ((or (not org-special-ctrl-k)
  17375. (bolp)
  17376. (not (org-on-heading-p)))
  17377. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17378. org-ctrl-k-protect-subtree)
  17379. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17380. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17381. (error "C-k aborted - would kill hidden subtree")))
  17382. (call-interactively 'kill-line))
  17383. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  17384. (kill-region (point) (match-beginning 1))
  17385. (org-set-tags nil t))
  17386. (t (kill-region (point) (point-at-eol)))))
  17387. (define-key org-mode-map "\C-k" 'org-kill-line)
  17388. (defun org-yank (&optional arg)
  17389. "Yank. If the kill is a subtree, treat it specially.
  17390. This command will look at the current kill and check if is a single
  17391. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17392. cursor is at the beginning of a line or after the stars of a currently
  17393. empty headline, then the yank is handled specially. How exactly depends
  17394. on the value of the following variables, both set by default.
  17395. org-yank-folded-subtrees
  17396. When set, the subtree(s) will be folded after insertion, but only
  17397. if doing so would now swallow text after the yanked text.
  17398. org-yank-adjusted-subtrees
  17399. When set, the subtree will be promoted or demoted in order to
  17400. fit into the local outline tree structure, which means that the level
  17401. will be adjusted so that it becomes the smaller one of the two
  17402. *visible* surrounding headings.
  17403. Any prefix to this command will cause `yank' to be called directly with
  17404. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17405. will just
  17406. plainly yank the text as it is.
  17407. \[1] The test checks if the first non-white line is a heading
  17408. and if there are no other headings with fewer stars."
  17409. (interactive "P")
  17410. (org-yank-generic 'yank arg))
  17411. (defun org-yank-generic (command arg)
  17412. "Perform some yank-like command.
  17413. This function implements the behavior described in the `org-yank'
  17414. documentation. However, it has been generalized to work for any
  17415. interactive command with similar behavior."
  17416. ;; pretend to be command COMMAND
  17417. (setq this-command command)
  17418. (if arg
  17419. (call-interactively command)
  17420. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17421. (and (org-kill-is-subtree-p)
  17422. (or (bolp)
  17423. (and (looking-at "[ \t]*$")
  17424. (string-match
  17425. "\\`\\*+\\'"
  17426. (buffer-substring (point-at-bol) (point)))))))
  17427. swallowp)
  17428. (cond
  17429. ((and subtreep org-yank-folded-subtrees)
  17430. (let ((beg (point))
  17431. end)
  17432. (if (and subtreep org-yank-adjusted-subtrees)
  17433. (org-paste-subtree nil nil 'for-yank)
  17434. (call-interactively command))
  17435. (setq end (point))
  17436. (goto-char beg)
  17437. (when (and (bolp) subtreep
  17438. (not (setq swallowp
  17439. (org-yank-folding-would-swallow-text beg end))))
  17440. (or (looking-at outline-regexp)
  17441. (re-search-forward (concat "^" outline-regexp) end t))
  17442. (while (and (< (point) end) (looking-at outline-regexp))
  17443. (hide-subtree)
  17444. (org-cycle-show-empty-lines 'folded)
  17445. (condition-case nil
  17446. (outline-forward-same-level 1)
  17447. (error (goto-char end)))))
  17448. (when swallowp
  17449. (message
  17450. "Inserted text not folded because that would swallow text"))
  17451. (goto-char end)
  17452. (skip-chars-forward " \t\n\r")
  17453. (beginning-of-line 1)
  17454. (push-mark beg 'nomsg)))
  17455. ((and subtreep org-yank-adjusted-subtrees)
  17456. (let ((beg (point-at-bol)))
  17457. (org-paste-subtree nil nil 'for-yank)
  17458. (push-mark beg 'nomsg)))
  17459. (t
  17460. (call-interactively command))))))
  17461. (defun org-yank-folding-would-swallow-text (beg end)
  17462. "Would hide-subtree at BEG swallow any text after END?"
  17463. (let (level)
  17464. (save-excursion
  17465. (goto-char beg)
  17466. (when (or (looking-at outline-regexp)
  17467. (re-search-forward (concat "^" outline-regexp) end t))
  17468. (setq level (org-outline-level)))
  17469. (goto-char end)
  17470. (skip-chars-forward " \t\r\n\v\f")
  17471. (if (or (eobp)
  17472. (and (bolp) (looking-at org-outline-regexp)
  17473. (<= (org-outline-level) level)))
  17474. nil ; Nothing would be swallowed
  17475. t)))) ; something would swallow
  17476. (define-key org-mode-map "\C-y" 'org-yank)
  17477. (defun org-invisible-p ()
  17478. "Check if point is at a character currently not visible."
  17479. ;; Early versions of noutline don't have `outline-invisible-p'.
  17480. (if (fboundp 'outline-invisible-p)
  17481. (outline-invisible-p)
  17482. (get-char-property (point) 'invisible)))
  17483. (defun org-truely-invisible-p ()
  17484. "Check if point is at a character currently not visible.
  17485. This version does not only check the character property, but also
  17486. `visible-mode'."
  17487. ;; Early versions of noutline don't have `outline-invisible-p'.
  17488. (if (org-bound-and-true-p visible-mode)
  17489. nil
  17490. (if (fboundp 'outline-invisible-p)
  17491. (outline-invisible-p)
  17492. (get-char-property (point) 'invisible))))
  17493. (defun org-invisible-p2 ()
  17494. "Check if point is at a character currently not visible."
  17495. (save-excursion
  17496. (if (and (eolp) (not (bobp))) (backward-char 1))
  17497. ;; Early versions of noutline don't have `outline-invisible-p'.
  17498. (if (fboundp 'outline-invisible-p)
  17499. (outline-invisible-p)
  17500. (get-char-property (point) 'invisible))))
  17501. (defun org-back-to-heading (&optional invisible-ok)
  17502. "Call `outline-back-to-heading', but provide a better error message."
  17503. (condition-case nil
  17504. (outline-back-to-heading invisible-ok)
  17505. (error (error "Before first headline at position %d in buffer %s"
  17506. (point) (current-buffer)))))
  17507. (defun org-beginning-of-defun ()
  17508. "Go to the beginning of the subtree, i.e. back to the heading."
  17509. (org-back-to-heading))
  17510. (defun org-end-of-defun ()
  17511. "Go to the end of the subtree."
  17512. (org-end-of-subtree nil t))
  17513. (defun org-before-first-heading-p ()
  17514. "Before first heading?"
  17515. (save-excursion
  17516. (null (re-search-backward "^\\*+ " nil t))))
  17517. (defun org-on-heading-p (&optional ignored)
  17518. (outline-on-heading-p t))
  17519. (defun org-at-heading-p (&optional ignored)
  17520. (outline-on-heading-p t))
  17521. (defun org-point-at-end-of-empty-headline ()
  17522. "If point is at the end of an empty headline, return t, else nil.
  17523. If the heading only contains a TODO keyword, it is still still considered
  17524. empty."
  17525. (and (looking-at "[ \t]*$")
  17526. (save-excursion
  17527. (beginning-of-line 1)
  17528. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17529. "\\)?[ \t]*$")))))
  17530. (defun org-at-heading-or-item-p ()
  17531. (or (org-on-heading-p) (org-at-item-p)))
  17532. (defun org-on-target-p ()
  17533. (or (org-in-regexp org-radio-target-regexp)
  17534. (org-in-regexp org-target-regexp)))
  17535. (defun org-up-heading-all (arg)
  17536. "Move to the heading line of which the present line is a subheading.
  17537. This function considers both visible and invisible heading lines.
  17538. With argument, move up ARG levels."
  17539. (if (fboundp 'outline-up-heading-all)
  17540. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17541. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17542. (defun org-up-heading-safe ()
  17543. "Move to the heading line of which the present line is a subheading.
  17544. This version will not throw an error. It will return the level of the
  17545. headline found, or nil if no higher level is found.
  17546. Also, this function will be a lot faster than `outline-up-heading',
  17547. because it relies on stars being the outline starters. This can really
  17548. make a significant difference in outlines with very many siblings."
  17549. (let (start-level re)
  17550. (org-back-to-heading t)
  17551. (setq start-level (funcall outline-level))
  17552. (if (equal start-level 1)
  17553. nil
  17554. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17555. (if (re-search-backward re nil t)
  17556. (funcall outline-level)))))
  17557. (defun org-first-sibling-p ()
  17558. "Is this heading the first child of its parents?"
  17559. (interactive)
  17560. (let ((re (concat "^" outline-regexp))
  17561. level l)
  17562. (unless (org-at-heading-p t)
  17563. (error "Not at a heading"))
  17564. (setq level (funcall outline-level))
  17565. (save-excursion
  17566. (if (not (re-search-backward re nil t))
  17567. t
  17568. (setq l (funcall outline-level))
  17569. (< l level)))))
  17570. (defun org-goto-sibling (&optional previous)
  17571. "Goto the next sibling, even if it is invisible.
  17572. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17573. when a sibling was found. When none is found, return nil and don't
  17574. move point."
  17575. (let ((fun (if previous 're-search-backward 're-search-forward))
  17576. (pos (point))
  17577. (re (concat "^" outline-regexp))
  17578. level l)
  17579. (when (condition-case nil (org-back-to-heading t) (error nil))
  17580. (setq level (funcall outline-level))
  17581. (catch 'exit
  17582. (or previous (forward-char 1))
  17583. (while (funcall fun re nil t)
  17584. (setq l (funcall outline-level))
  17585. (when (< l level) (goto-char pos) (throw 'exit nil))
  17586. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17587. (goto-char pos)
  17588. nil))))
  17589. (defun org-show-siblings ()
  17590. "Show all siblings of the current headline."
  17591. (save-excursion
  17592. (while (org-goto-sibling) (org-flag-heading nil)))
  17593. (save-excursion
  17594. (while (org-goto-sibling 'previous)
  17595. (org-flag-heading nil))))
  17596. (defun org-show-hidden-entry ()
  17597. "Show an entry where even the heading is hidden."
  17598. (save-excursion
  17599. (org-show-entry)))
  17600. (defun org-flag-heading (flag &optional entry)
  17601. "Flag the current heading. FLAG non-nil means make invisible.
  17602. When ENTRY is non-nil, show the entire entry."
  17603. (save-excursion
  17604. (org-back-to-heading t)
  17605. ;; Check if we should show the entire entry
  17606. (if entry
  17607. (progn
  17608. (org-show-entry)
  17609. (save-excursion
  17610. (and (outline-next-heading)
  17611. (org-flag-heading nil))))
  17612. (outline-flag-region (max (point-min) (1- (point)))
  17613. (save-excursion (outline-end-of-heading) (point))
  17614. flag))))
  17615. (defun org-get-next-sibling ()
  17616. "Move to next heading of the same level, and return point.
  17617. If there is no such heading, return nil.
  17618. This is like outline-next-sibling, but invisible headings are ok."
  17619. (let ((level (funcall outline-level)))
  17620. (outline-next-heading)
  17621. (while (and (not (eobp)) (> (funcall outline-level) level))
  17622. (outline-next-heading))
  17623. (if (or (eobp) (< (funcall outline-level) level))
  17624. nil
  17625. (point))))
  17626. (defun org-get-last-sibling ()
  17627. "Move to previous heading of the same level, and return point.
  17628. If there is no such heading, return nil."
  17629. (let ((opoint (point))
  17630. (level (funcall outline-level)))
  17631. (outline-previous-heading)
  17632. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17633. (while (and (> (funcall outline-level) level)
  17634. (not (bobp)))
  17635. (outline-previous-heading))
  17636. (if (< (funcall outline-level) level)
  17637. nil
  17638. (point)))))
  17639. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17640. ;; This contains an exact copy of the original function, but it uses
  17641. ;; `org-back-to-heading', to make it work also in invisible
  17642. ;; trees. And is uses an invisible-OK argument.
  17643. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17644. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17645. ;; with many children and grandchildren etc, this can be much faster
  17646. ;; than the outline version.
  17647. (org-back-to-heading invisible-OK)
  17648. (let ((first t)
  17649. (level (funcall outline-level)))
  17650. (if (and (org-mode-p) (< level 1000))
  17651. ;; A true heading (not a plain list item), in Org-mode
  17652. ;; This means we can easily find the end by looking
  17653. ;; only for the right number of stars. Using a regexp to do
  17654. ;; this is so much faster than using a Lisp loop.
  17655. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17656. (forward-char 1)
  17657. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17658. ;; something else, do it the slow way
  17659. (while (and (not (eobp))
  17660. (or first (> (funcall outline-level) level)))
  17661. (setq first nil)
  17662. (outline-next-heading)))
  17663. (unless to-heading
  17664. (if (memq (preceding-char) '(?\n ?\^M))
  17665. (progn
  17666. ;; Go to end of line before heading
  17667. (forward-char -1)
  17668. (if (memq (preceding-char) '(?\n ?\^M))
  17669. ;; leave blank line before heading
  17670. (forward-char -1))))))
  17671. (point))
  17672. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17673. "Use Org version in org-mode, for dramatic speed-up."
  17674. (if (eq major-mode 'org-mode)
  17675. (progn
  17676. (org-end-of-subtree nil t)
  17677. (unless (eobp) (backward-char 1)))
  17678. ad-do-it))
  17679. (defun org-forward-same-level (arg &optional invisible-ok)
  17680. "Move forward to the arg'th subheading at same level as this one.
  17681. Stop at the first and last subheadings of a superior heading."
  17682. (interactive "p")
  17683. (org-back-to-heading invisible-ok)
  17684. (org-on-heading-p)
  17685. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17686. (re (format "^\\*\\{1,%d\\} " level))
  17687. l)
  17688. (forward-char 1)
  17689. (while (> arg 0)
  17690. (while (and (re-search-forward re nil 'move)
  17691. (setq l (- (match-end 0) (match-beginning 0) 1))
  17692. (= l level)
  17693. (not invisible-ok)
  17694. (progn (backward-char 1) (org-invisible-p)))
  17695. (if (< l level) (setq arg 1)))
  17696. (setq arg (1- arg)))
  17697. (beginning-of-line 1)))
  17698. (defun org-backward-same-level (arg &optional invisible-ok)
  17699. "Move backward to the arg'th subheading at same level as this one.
  17700. Stop at the first and last subheadings of a superior heading."
  17701. (interactive "p")
  17702. (org-back-to-heading)
  17703. (org-on-heading-p)
  17704. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17705. (re (format "^\\*\\{1,%d\\} " level))
  17706. l)
  17707. (while (> arg 0)
  17708. (while (and (re-search-backward re nil 'move)
  17709. (setq l (- (match-end 0) (match-beginning 0) 1))
  17710. (= l level)
  17711. (not invisible-ok)
  17712. (org-invisible-p))
  17713. (if (< l level) (setq arg 1)))
  17714. (setq arg (1- arg)))))
  17715. (defun org-show-subtree ()
  17716. "Show everything after this heading at deeper levels."
  17717. (outline-flag-region
  17718. (point)
  17719. (save-excursion
  17720. (org-end-of-subtree t t))
  17721. nil))
  17722. (defun org-show-entry ()
  17723. "Show the body directly following this heading.
  17724. Show the heading too, if it is currently invisible."
  17725. (interactive)
  17726. (save-excursion
  17727. (condition-case nil
  17728. (progn
  17729. (org-back-to-heading t)
  17730. (outline-flag-region
  17731. (max (point-min) (1- (point)))
  17732. (save-excursion
  17733. (if (re-search-forward
  17734. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17735. (match-beginning 1)
  17736. (point-max)))
  17737. nil)
  17738. (org-cycle-hide-drawers 'children))
  17739. (error nil))))
  17740. (defun org-make-options-regexp (kwds &optional extra)
  17741. "Make a regular expression for keyword lines."
  17742. (concat
  17743. "^"
  17744. "#?[ \t]*\\+\\("
  17745. (mapconcat 'regexp-quote kwds "\\|")
  17746. (if extra (concat "\\|" extra))
  17747. "\\):[ \t]*"
  17748. "\\(.*\\)"))
  17749. ;; Make isearch reveal the necessary context
  17750. (defun org-isearch-end ()
  17751. "Reveal context after isearch exits."
  17752. (when isearch-success ; only if search was successful
  17753. (if (featurep 'xemacs)
  17754. ;; Under XEmacs, the hook is run in the correct place,
  17755. ;; we directly show the context.
  17756. (org-show-context 'isearch)
  17757. ;; In Emacs the hook runs *before* restoring the overlays.
  17758. ;; So we have to use a one-time post-command-hook to do this.
  17759. ;; (Emacs 22 has a special variable, see function `org-mode')
  17760. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17761. isearch-mode-end-hook-quit)
  17762. ;; Only when the isearch was not quitted.
  17763. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17764. 'append 'local)))))
  17765. (defun org-isearch-post-command ()
  17766. "Remove self from hook, and show context."
  17767. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17768. (org-show-context 'isearch))
  17769. ;;;; Integration with and fixes for other packages
  17770. ;;; Imenu support
  17771. (defvar org-imenu-markers nil
  17772. "All markers currently used by Imenu.")
  17773. (make-variable-buffer-local 'org-imenu-markers)
  17774. (defun org-imenu-new-marker (&optional pos)
  17775. "Return a new marker for use by Imenu, and remember the marker."
  17776. (let ((m (make-marker)))
  17777. (move-marker m (or pos (point)))
  17778. (push m org-imenu-markers)
  17779. m))
  17780. (defun org-imenu-get-tree ()
  17781. "Produce the index for Imenu."
  17782. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17783. (setq org-imenu-markers nil)
  17784. (let* ((n org-imenu-depth)
  17785. (re (concat "^" outline-regexp))
  17786. (subs (make-vector (1+ n) nil))
  17787. (last-level 0)
  17788. m level head)
  17789. (save-excursion
  17790. (save-restriction
  17791. (widen)
  17792. (goto-char (point-max))
  17793. (while (re-search-backward re nil t)
  17794. (setq level (org-reduced-level (funcall outline-level)))
  17795. (when (<= level n)
  17796. (looking-at org-complex-heading-regexp)
  17797. (setq head (org-link-display-format
  17798. (org-match-string-no-properties 4))
  17799. m (org-imenu-new-marker))
  17800. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17801. (if (>= level last-level)
  17802. (push (cons head m) (aref subs level))
  17803. (push (cons head (aref subs (1+ level))) (aref subs level))
  17804. (loop for i from (1+ level) to n do (aset subs i nil)))
  17805. (setq last-level level)))))
  17806. (aref subs 1)))
  17807. (eval-after-load "imenu"
  17808. '(progn
  17809. (add-hook 'imenu-after-jump-hook
  17810. (lambda ()
  17811. (if (eq major-mode 'org-mode)
  17812. (org-show-context 'org-goto))))))
  17813. (defun org-link-display-format (link)
  17814. "Replace a link with either the description, or the link target
  17815. if no description is present"
  17816. (save-match-data
  17817. (if (string-match org-bracket-link-analytic-regexp link)
  17818. (replace-match (if (match-end 5)
  17819. (match-string 5 link)
  17820. (concat (match-string 1 link)
  17821. (match-string 3 link)))
  17822. nil t link)
  17823. link)))
  17824. ;; Speedbar support
  17825. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17826. "Overlay marking the agenda restriction line in speedbar.")
  17827. (overlay-put org-speedbar-restriction-lock-overlay
  17828. 'face 'org-agenda-restriction-lock)
  17829. (overlay-put org-speedbar-restriction-lock-overlay
  17830. 'help-echo "Agendas are currently limited to this item.")
  17831. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17832. (defun org-speedbar-set-agenda-restriction ()
  17833. "Restrict future agenda commands to the location at point in speedbar.
  17834. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17835. (interactive)
  17836. (require 'org-agenda)
  17837. (let (p m tp np dir txt)
  17838. (cond
  17839. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17840. 'org-imenu t))
  17841. (setq m (get-text-property p 'org-imenu-marker))
  17842. (with-current-buffer (marker-buffer m)
  17843. (goto-char m)
  17844. (org-agenda-set-restriction-lock 'subtree)))
  17845. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17846. 'speedbar-function 'speedbar-find-file))
  17847. (setq tp (previous-single-property-change
  17848. (1+ p) 'speedbar-function)
  17849. np (next-single-property-change
  17850. tp 'speedbar-function)
  17851. dir (speedbar-line-directory)
  17852. txt (buffer-substring-no-properties (or tp (point-min))
  17853. (or np (point-max))))
  17854. (with-current-buffer (find-file-noselect
  17855. (let ((default-directory dir))
  17856. (expand-file-name txt)))
  17857. (unless (org-mode-p)
  17858. (error "Cannot restrict to non-Org-mode file"))
  17859. (org-agenda-set-restriction-lock 'file)))
  17860. (t (error "Don't know how to restrict Org-mode's agenda")))
  17861. (move-overlay org-speedbar-restriction-lock-overlay
  17862. (point-at-bol) (point-at-eol))
  17863. (setq current-prefix-arg nil)
  17864. (org-agenda-maybe-redo)))
  17865. (eval-after-load "speedbar"
  17866. '(progn
  17867. (speedbar-add-supported-extension ".org")
  17868. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17869. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17870. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17871. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17872. (add-hook 'speedbar-visiting-tag-hook
  17873. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17874. ;;; Fixes and Hacks for problems with other packages
  17875. ;; Make flyspell not check words in links, to not mess up our keymap
  17876. (defun org-mode-flyspell-verify ()
  17877. "Don't let flyspell put overlays at active buttons."
  17878. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17879. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17880. (defun org-remove-flyspell-overlays-in (beg end)
  17881. "Remove flyspell overlays in region."
  17882. (and (org-bound-and-true-p flyspell-mode)
  17883. (fboundp 'flyspell-delete-region-overlays)
  17884. (flyspell-delete-region-overlays beg end))
  17885. (add-text-properties beg end '(org-no-flyspell t)))
  17886. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17887. (eval-after-load "bookmark"
  17888. '(if (boundp 'bookmark-after-jump-hook)
  17889. ;; We can use the hook
  17890. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17891. ;; Hook not available, use advice
  17892. (defadvice bookmark-jump (after org-make-visible activate)
  17893. "Make the position visible."
  17894. (org-bookmark-jump-unhide))))
  17895. ;; Make sure saveplace shows the location if it was hidden
  17896. (eval-after-load "saveplace"
  17897. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17898. "Make the position visible."
  17899. (org-bookmark-jump-unhide)))
  17900. ;; Make sure ecb shows the location if it was hidden
  17901. (eval-after-load "ecb"
  17902. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17903. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17904. (if (eq major-mode 'org-mode)
  17905. (org-show-context))))
  17906. (defun org-bookmark-jump-unhide ()
  17907. "Unhide the current position, to show the bookmark location."
  17908. (and (org-mode-p)
  17909. (or (org-invisible-p)
  17910. (save-excursion (goto-char (max (point-min) (1- (point))))
  17911. (org-invisible-p)))
  17912. (org-show-context 'bookmark-jump)))
  17913. ;; Make session.el ignore our circular variable
  17914. (eval-after-load "session"
  17915. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17916. ;;;; Experimental code
  17917. (defun org-closed-in-range ()
  17918. "Sparse tree of items closed in a certain time range.
  17919. Still experimental, may disappear in the future."
  17920. (interactive)
  17921. ;; Get the time interval from the user.
  17922. (let* ((time1 (org-float-time
  17923. (org-read-date nil 'to-time nil "Starting date: ")))
  17924. (time2 (org-float-time
  17925. (org-read-date nil 'to-time nil "End date:")))
  17926. ;; callback function
  17927. (callback (lambda ()
  17928. (let ((time
  17929. (org-float-time
  17930. (apply 'encode-time
  17931. (org-parse-time-string
  17932. (match-string 1))))))
  17933. ;; check if time in interval
  17934. (and (>= time time1) (<= time time2))))))
  17935. ;; make tree, check each match with the callback
  17936. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17937. ;;;; Finish up
  17938. (provide 'org)
  17939. (run-hooks 'org-load-hook)
  17940. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17941. ;;; org.el ends here