org.el 732 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563
  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 "Javascript" js)
  147. (const :tag "Latex" latex)
  148. (const :tag "Ledger" ledger)
  149. (const :tag "Matlab" matlab)
  150. (const :tag "Mscgen" mscgen)
  151. (const :tag "Ocaml" ocaml)
  152. (const :tag "Octave" octave)
  153. (const :tag "Org" org)
  154. (const :tag "Perl" perl)
  155. (const :tag "PlantUML" plantuml)
  156. (const :tag "Python" python)
  157. (const :tag "Ruby" ruby)
  158. (const :tag "Sass" sass)
  159. (const :tag "Scheme" scheme)
  160. (const :tag "Screen" screen)
  161. (const :tag "Shell Script" sh)
  162. (const :tag "Sql" sql)
  163. (const :tag "Sqlite" sqlite))
  164. :value-type (boolean :tag "Activate" :value t)))
  165. ;;;; Customization variables
  166. (defcustom org-clone-delete-id nil
  167. "Remove ID property of clones of a subtree.
  168. When non-nil, clones of a subtree don't inherit the ID property.
  169. Otherwise they inherit the ID property with a new unique
  170. identifier."
  171. :type 'boolean
  172. :group 'org-id)
  173. ;;; Version
  174. (defconst org-version "7.01trans"
  175. "The version number of the file org.el.")
  176. (defun org-version (&optional here)
  177. "Show the org-mode version in the echo area.
  178. With prefix arg HERE, insert it at point."
  179. (interactive "P")
  180. (let* ((origin default-directory)
  181. (version org-version)
  182. (git-version)
  183. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  184. (when (and (file-exists-p (expand-file-name ".git" dir))
  185. (executable-find "git"))
  186. (unwind-protect
  187. (progn
  188. (cd dir)
  189. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  190. (with-current-buffer "*Shell Command Output*"
  191. (goto-char (point-min))
  192. (setq git-version (buffer-substring (point) (point-at-eol))))
  193. (subst-char-in-string ?- ?. git-version t)
  194. (when (string-match "\\S-"
  195. (shell-command-to-string
  196. "git diff-index --name-only HEAD --"))
  197. (setq git-version (concat git-version ".dirty")))
  198. (setq version (concat version " (" git-version ")"))))
  199. (cd origin)))
  200. (setq version (format "Org-mode version %s" version))
  201. (if here (insert version))
  202. (message version)))
  203. ;;; Compatibility constants
  204. ;;; The custom variables
  205. (defgroup org nil
  206. "Outline-based notes management and organizer."
  207. :tag "Org"
  208. :group 'outlines
  209. :group 'calendar)
  210. (defcustom org-mode-hook nil
  211. "Mode hook for Org-mode, run after the mode was turned on."
  212. :group 'org
  213. :type 'hook)
  214. (defcustom org-load-hook nil
  215. "Hook that is run after org.el has been loaded."
  216. :group 'org
  217. :type 'hook)
  218. (defvar org-modules) ; defined below
  219. (defvar org-modules-loaded nil
  220. "Have the modules been loaded already?")
  221. (defun org-load-modules-maybe (&optional force)
  222. "Load all extensions listed in `org-modules'."
  223. (when (or force (not org-modules-loaded))
  224. (mapc (lambda (ext)
  225. (condition-case nil (require ext)
  226. (error (message "Problems while trying to load feature `%s'" ext))))
  227. org-modules)
  228. (setq org-modules-loaded t)))
  229. (defun org-set-modules (var value)
  230. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  231. (set var value)
  232. (when (featurep 'org)
  233. (org-load-modules-maybe 'force)))
  234. (when (org-bound-and-true-p org-modules)
  235. (let ((a (member 'org-infojs org-modules)))
  236. (and a (setcar a 'org-jsinfo))))
  237. (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)
  238. "Modules that should always be loaded together with org.el.
  239. If a description starts with <C>, the file is not part of Emacs
  240. and loading it will require that you have downloaded and properly installed
  241. the org-mode distribution.
  242. You can also use this system to load external packages (i.e. neither Org
  243. core modules, nor modules from the CONTRIB directory). Just add symbols
  244. to the end of the list. If the package is called org-xyz.el, then you need
  245. to add the symbol `xyz', and the package must have a call to
  246. (provide 'org-xyz)"
  247. :group 'org
  248. :set 'org-set-modules
  249. :type
  250. '(set :greedy t
  251. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  252. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  253. (const :tag " crypt: Encryption of subtrees" org-crypt)
  254. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  255. (const :tag " docview: Links to doc-view buffers" org-docview)
  256. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  257. (const :tag " id: Global IDs for identifying entries" org-id)
  258. (const :tag " info: Links to Info nodes" org-info)
  259. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  260. (const :tag " habit: Track your consistency with habits" org-habit)
  261. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  262. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  263. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  264. (const :tag " mew Links to Mew folders/messages" org-mew)
  265. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  266. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  267. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  268. (const :tag " vm: Links to VM folders/messages" org-vm)
  269. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  270. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  271. (const :tag " mouse: Additional mouse support" org-mouse)
  272. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  273. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  274. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  275. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  276. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  277. (const :tag "C collector: Collect properties into tables" org-collector)
  278. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  279. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  280. (const :tag "C eval: Include command output as text" org-eval)
  281. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  282. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  283. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  284. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  285. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  286. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  287. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  288. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  289. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  290. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  291. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  292. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  293. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  294. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  295. (const :tag "C registry: A registry for Org-mode links" org-registry)
  296. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  297. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  298. (const :tag "C secretary: Team management with org-mode" org-secretary)
  299. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  300. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  301. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  302. (const :tag "C track: Keep up with Org-mode development" org-track)
  303. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  304. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  305. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  306. (defcustom org-support-shift-select nil
  307. "Non-nil means make shift-cursor commands select text when possible.
  308. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  309. selecting a region, or enlarge regions started in this way.
  310. In Org-mode, in special contexts, these same keys are used for other
  311. purposes, important enough to compete with shift selection. Org tries
  312. to balance these needs by supporting `shift-select-mode' outside these
  313. special contexts, under control of this variable.
  314. The default of this variable is nil, to avoid confusing behavior. Shifted
  315. cursor keys will then execute Org commands in the following contexts:
  316. - on a headline, changing TODO state (left/right) and priority (up/down)
  317. - on a time stamp, changing the time
  318. - in a plain list item, changing the bullet type
  319. - in a property definition line, switching between allowed values
  320. - in the BEGIN line of a clock table (changing the time block).
  321. Outside these contexts, the commands will throw an error.
  322. When this variable is t and the cursor is not in a special context,
  323. Org-mode will support shift-selection for making and enlarging regions.
  324. To make this more effective, the bullet cycling will no longer happen
  325. anywhere in an item line, but only if the cursor is exactly on the bullet.
  326. If you set this variable to the symbol `always', then the keys
  327. will not be special in headlines, property lines, and item lines, to make
  328. shift selection work there as well. If this is what you want, you can
  329. use the following alternative commands: `C-c C-t' and `C-c ,' to
  330. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  331. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  332. edited by hand or in column view.
  333. However, when the cursor is on a timestamp, shift-cursor commands
  334. will still edit the time stamp - this is just too good to give up.
  335. XEmacs user should have this variable set to nil, because shift-select-mode
  336. is Emacs 23 only."
  337. :group 'org
  338. :type '(choice
  339. (const :tag "Never" nil)
  340. (const :tag "When outside special context" t)
  341. (const :tag "Everywhere except timestamps" always)))
  342. (defgroup org-startup nil
  343. "Options concerning startup of Org-mode."
  344. :tag "Org Startup"
  345. :group 'org)
  346. (defcustom org-startup-folded t
  347. "Non-nil means entering Org-mode will switch to OVERVIEW.
  348. This can also be configured on a per-file basis by adding one of
  349. the following lines anywhere in the buffer:
  350. #+STARTUP: fold (or `overview', this is equivalent)
  351. #+STARTUP: nofold (or `showall', this is equivalent)
  352. #+STARTUP: content
  353. #+STARTUP: showeverything"
  354. :group 'org-startup
  355. :type '(choice
  356. (const :tag "nofold: show all" nil)
  357. (const :tag "fold: overview" t)
  358. (const :tag "content: all headlines" content)
  359. (const :tag "show everything, even drawers" showeverything)))
  360. (defcustom org-startup-truncated t
  361. "Non-nil means entering Org-mode will set `truncate-lines'.
  362. This is useful since some lines containing links can be very long and
  363. uninteresting. Also tables look terrible when wrapped."
  364. :group 'org-startup
  365. :type 'boolean)
  366. (defcustom org-startup-indented nil
  367. "Non-nil means turn on `org-indent-mode' on startup.
  368. This can also be configured on a per-file basis by adding one of
  369. the following lines anywhere in the buffer:
  370. #+STARTUP: indent
  371. #+STARTUP: noindent"
  372. :group 'org-structure
  373. :type '(choice
  374. (const :tag "Not" nil)
  375. (const :tag "Globally (slow on startup in large files)" t)))
  376. (defcustom org-use-sub-superscripts t
  377. "Non-nil means interpret \"_\" and \"^\" for export.
  378. When this option is turned on, you can use TeX-like syntax for sub- and
  379. superscripts. Several characters after \"_\" or \"^\" will be
  380. considered as a single item - so grouping with {} is normally not
  381. needed. For example, the following things will be parsed as single
  382. sub- or superscripts.
  383. 10^24 or 10^tau several digits will be considered 1 item.
  384. 10^-12 or 10^-tau a leading sign with digits or a word
  385. x^2-y^3 will be read as x^2 - y^3, because items are
  386. terminated by almost any nonword/nondigit char.
  387. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  388. Still, ambiguity is possible - so when in doubt use {} to enclose the
  389. sub/superscript. If you set this variable to the symbol `{}',
  390. the braces are *required* in order to trigger interpretations as
  391. sub/superscript. This can be helpful in documents that need \"_\"
  392. frequently in plain text.
  393. Not all export backends support this, but HTML does.
  394. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  395. :group 'org-startup
  396. :group 'org-export-translation
  397. :type '(choice
  398. (const :tag "Always interpret" t)
  399. (const :tag "Only with braces" {})
  400. (const :tag "Never interpret" nil)))
  401. (if (fboundp 'defvaralias)
  402. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  403. (defcustom org-startup-with-beamer-mode nil
  404. "Non-nil means turn on `org-beamer-mode' on startup.
  405. This can also be configured on a per-file basis by adding one of
  406. the following lines anywhere in the buffer:
  407. #+STARTUP: beamer"
  408. :group 'org-startup
  409. :type 'boolean)
  410. (defcustom org-startup-align-all-tables nil
  411. "Non-nil means align all tables when visiting a file.
  412. This is useful when the column width in tables is forced with <N> cookies
  413. in table fields. Such tables will look correct only after the first re-align.
  414. This can also be configured on a per-file basis by adding one of
  415. the following lines anywhere in the buffer:
  416. #+STARTUP: align
  417. #+STARTUP: noalign"
  418. :group 'org-startup
  419. :type 'boolean)
  420. (defcustom org-insert-mode-line-in-empty-file nil
  421. "Non-nil means insert the first line setting Org-mode in empty files.
  422. When the function `org-mode' is called interactively in an empty file, this
  423. normally means that the file name does not automatically trigger Org-mode.
  424. To ensure that the file will always be in Org-mode in the future, a
  425. line enforcing Org-mode will be inserted into the buffer, if this option
  426. has been set."
  427. :group 'org-startup
  428. :type 'boolean)
  429. (defcustom org-replace-disputed-keys nil
  430. "Non-nil means use alternative key bindings for some keys.
  431. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  432. These keys are also used by other packages like shift-selection-mode'
  433. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  434. If you want to use Org-mode together with one of these other modes,
  435. or more generally if you would like to move some Org-mode commands to
  436. other keys, set this variable and configure the keys with the variable
  437. `org-disputed-keys'.
  438. This option is only relevant at load-time of Org-mode, and must be set
  439. *before* org.el is loaded. Changing it requires a restart of Emacs to
  440. become effective."
  441. :group 'org-startup
  442. :type 'boolean)
  443. (defcustom org-use-extra-keys nil
  444. "Non-nil means use extra key sequence definitions for certain commands.
  445. This happens automatically if you run XEmacs or if `window-system'
  446. is nil. This variable lets you do the same manually. You must
  447. set it before loading org.
  448. Example: on Carbon Emacs 22 running graphically, with an external
  449. keyboard on a Powerbook, the default way of setting M-left might
  450. not work for either Alt or ESC. Setting this variable will make
  451. it work for ESC."
  452. :group 'org-startup
  453. :type 'boolean)
  454. (if (fboundp 'defvaralias)
  455. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  456. (defcustom org-disputed-keys
  457. '(([(shift up)] . [(meta p)])
  458. ([(shift down)] . [(meta n)])
  459. ([(shift left)] . [(meta -)])
  460. ([(shift right)] . [(meta +)])
  461. ([(control shift right)] . [(meta shift +)])
  462. ([(control shift left)] . [(meta shift -)]))
  463. "Keys for which Org-mode and other modes compete.
  464. This is an alist, cars are the default keys, second element specifies
  465. the alternative to use when `org-replace-disputed-keys' is t.
  466. Keys can be specified in any syntax supported by `define-key'.
  467. The value of this option takes effect only at Org-mode's startup,
  468. therefore you'll have to restart Emacs to apply it after changing."
  469. :group 'org-startup
  470. :type 'alist)
  471. (defun org-key (key)
  472. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  473. Or return the original if not disputed.
  474. Also apply the translations defined in `org-xemacs-key-equivalents'."
  475. (when org-replace-disputed-keys
  476. (let* ((nkey (key-description key))
  477. (x (org-find-if (lambda (x)
  478. (equal (key-description (car x)) nkey))
  479. org-disputed-keys)))
  480. (setq key (if x (cdr x) key))))
  481. (when (featurep 'xemacs)
  482. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  483. key)
  484. (defun org-find-if (predicate seq)
  485. (catch 'exit
  486. (while seq
  487. (if (funcall predicate (car seq))
  488. (throw 'exit (car seq))
  489. (pop seq)))))
  490. (defun org-defkey (keymap key def)
  491. "Define a key, possibly translated, as returned by `org-key'."
  492. (define-key keymap (org-key key) def))
  493. (defcustom org-ellipsis nil
  494. "The ellipsis to use in the Org-mode outline.
  495. When nil, just use the standard three dots. When a string, use that instead,
  496. When a face, use the standard 3 dots, but with the specified face.
  497. The change affects only Org-mode (which will then use its own display table).
  498. Changing this requires executing `M-x org-mode' in a buffer to become
  499. effective."
  500. :group 'org-startup
  501. :type '(choice (const :tag "Default" nil)
  502. (face :tag "Face" :value org-warning)
  503. (string :tag "String" :value "...#")))
  504. (defvar org-display-table nil
  505. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  506. (defgroup org-keywords nil
  507. "Keywords in Org-mode."
  508. :tag "Org Keywords"
  509. :group 'org)
  510. (defcustom org-deadline-string "DEADLINE:"
  511. "String to mark deadline entries.
  512. A deadline is this string, followed by a time stamp. Should be a word,
  513. terminated by a colon. You can insert a schedule keyword and
  514. a timestamp with \\[org-deadline].
  515. Changes become only effective after restarting Emacs."
  516. :group 'org-keywords
  517. :type 'string)
  518. (defcustom org-scheduled-string "SCHEDULED:"
  519. "String to mark scheduled TODO entries.
  520. A schedule is this string, followed by a time stamp. Should be a word,
  521. terminated by a colon. You can insert a schedule keyword and
  522. a timestamp with \\[org-schedule].
  523. Changes become only effective after restarting Emacs."
  524. :group 'org-keywords
  525. :type 'string)
  526. (defcustom org-closed-string "CLOSED:"
  527. "String used as the prefix for timestamps logging closing a TODO entry."
  528. :group 'org-keywords
  529. :type 'string)
  530. (defcustom org-clock-string "CLOCK:"
  531. "String used as prefix for timestamps clocking work hours on an item."
  532. :group 'org-keywords
  533. :type 'string)
  534. (defcustom org-comment-string "COMMENT"
  535. "Entries starting with this keyword will never be exported.
  536. An entry can be toggled between COMMENT and normal with
  537. \\[org-toggle-comment].
  538. Changes become only effective after restarting Emacs."
  539. :group 'org-keywords
  540. :type 'string)
  541. (defcustom org-quote-string "QUOTE"
  542. "Entries starting with this keyword will be exported in fixed-width font.
  543. Quoting applies only to the text in the entry following the headline, and does
  544. not extend beyond the next headline, even if that is lower level.
  545. An entry can be toggled between QUOTE and normal with
  546. \\[org-toggle-fixed-width-section]."
  547. :group 'org-keywords
  548. :type 'string)
  549. (defconst org-repeat-re
  550. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  551. "Regular expression for specifying repeated events.
  552. After a match, group 1 contains the repeat expression.")
  553. (defgroup org-structure nil
  554. "Options concerning the general structure of Org-mode files."
  555. :tag "Org Structure"
  556. :group 'org)
  557. (defgroup org-reveal-location nil
  558. "Options about how to make context of a location visible."
  559. :tag "Org Reveal Location"
  560. :group 'org-structure)
  561. (defconst org-context-choice
  562. '(choice
  563. (const :tag "Always" t)
  564. (const :tag "Never" nil)
  565. (repeat :greedy t :tag "Individual contexts"
  566. (cons
  567. (choice :tag "Context"
  568. (const agenda)
  569. (const org-goto)
  570. (const occur-tree)
  571. (const tags-tree)
  572. (const link-search)
  573. (const mark-goto)
  574. (const bookmark-jump)
  575. (const isearch)
  576. (const default))
  577. (boolean))))
  578. "Contexts for the reveal options.")
  579. (defcustom org-show-hierarchy-above '((default . t))
  580. "Non-nil means show full hierarchy when revealing a location.
  581. Org-mode often shows locations in an org-mode file which might have
  582. been invisible before. When this is set, the hierarchy of headings
  583. above the exposed location is shown.
  584. Turning this off for example for sparse trees makes them very compact.
  585. Instead of t, this can also be an alist specifying this option for different
  586. contexts. Valid contexts are
  587. agenda when exposing an entry from the agenda
  588. org-goto when using the command `org-goto' on key C-c C-j
  589. occur-tree when using the command `org-occur' on key C-c /
  590. tags-tree when constructing a sparse tree based on tags matches
  591. link-search when exposing search matches associated with a link
  592. mark-goto when exposing the jump goal of a mark
  593. bookmark-jump when exposing a bookmark location
  594. isearch when exiting from an incremental search
  595. default default for all contexts not set explicitly"
  596. :group 'org-reveal-location
  597. :type org-context-choice)
  598. (defcustom org-show-following-heading '((default . nil))
  599. "Non-nil means show following heading when revealing a location.
  600. Org-mode often shows locations in an org-mode file which might have
  601. been invisible before. When this is set, the heading following the
  602. match is shown.
  603. Turning this off for example for sparse trees makes them very compact,
  604. but makes it harder to edit the location of the match. In such a case,
  605. use the command \\[org-reveal] to show more context.
  606. Instead of t, this can also be an alist specifying this option for different
  607. contexts. See `org-show-hierarchy-above' for valid contexts."
  608. :group 'org-reveal-location
  609. :type org-context-choice)
  610. (defcustom org-show-siblings '((default . nil) (isearch t))
  611. "Non-nil means show all sibling heading when revealing a location.
  612. Org-mode often shows locations in an org-mode file which might have
  613. been invisible before. When this is set, the sibling of the current entry
  614. heading are all made visible. If `org-show-hierarchy-above' is t,
  615. the same happens on each level of the hierarchy above the current entry.
  616. By default this is on for the isearch context, off for all other contexts.
  617. Turning this off for example for sparse trees makes them very compact,
  618. but makes it harder to edit the location of the match. In such a case,
  619. use the command \\[org-reveal] to show more context.
  620. Instead of t, this can also be an alist specifying this option for different
  621. contexts. See `org-show-hierarchy-above' for valid contexts."
  622. :group 'org-reveal-location
  623. :type org-context-choice)
  624. (defcustom org-show-entry-below '((default . nil))
  625. "Non-nil means show the entry below a headline when revealing a location.
  626. Org-mode often shows locations in an org-mode file which might have
  627. been invisible before. When this is set, the text below the headline that is
  628. exposed is also shown.
  629. By default this is off for all contexts.
  630. Instead of t, this can also be an alist specifying this option for different
  631. contexts. See `org-show-hierarchy-above' for valid contexts."
  632. :group 'org-reveal-location
  633. :type org-context-choice)
  634. (defcustom org-indirect-buffer-display 'other-window
  635. "How should indirect tree buffers be displayed?
  636. This applies to indirect buffers created with the commands
  637. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  638. Valid values are:
  639. current-window Display in the current window
  640. other-window Just display in another window.
  641. dedicated-frame Create one new frame, and re-use it each time.
  642. new-frame Make a new frame each time. Note that in this case
  643. previously-made indirect buffers are kept, and you need to
  644. kill these buffers yourself."
  645. :group 'org-structure
  646. :group 'org-agenda-windows
  647. :type '(choice
  648. (const :tag "In current window" current-window)
  649. (const :tag "In current frame, other window" other-window)
  650. (const :tag "Each time a new frame" new-frame)
  651. (const :tag "One dedicated frame" dedicated-frame)))
  652. (defcustom org-use-speed-commands nil
  653. "Non-nil means activate single letter commands at beginning of a headline.
  654. This may also be a function to test for appropriate locations where speed
  655. commands should be active."
  656. :group 'org-structure
  657. :type '(choice
  658. (const :tag "Never" nil)
  659. (const :tag "At beginning of headline stars" t)
  660. (function)))
  661. (defcustom org-speed-commands-user nil
  662. "Alist of additional speed commands.
  663. This list will be checked before `org-speed-commands-default'
  664. when the variable `org-use-speed-commands' is non-nil
  665. and when the cursor is at the beginning of a headline.
  666. The car if each entry is a string with a single letter, which must
  667. be assigned to `self-insert-command' in the global map.
  668. The cdr is either a command to be called interactively, a function
  669. to be called, or a form to be evaluated.
  670. An entry that is just a list with a single string will be interpreted
  671. as a descriptive headline that will be added when listing the speed
  672. commands in the Help buffer using the `?' speed command."
  673. :group 'org-structure
  674. :type '(repeat :value ("k" . ignore)
  675. (choice :value ("k" . ignore)
  676. (list :tag "Descriptive Headline" (string :tag "Headline"))
  677. (cons :tag "Letter and Command"
  678. (string :tag "Command letter")
  679. (choice
  680. (function)
  681. (sexp))))))
  682. (defgroup org-cycle nil
  683. "Options concerning visibility cycling in Org-mode."
  684. :tag "Org Cycle"
  685. :group 'org-structure)
  686. (defcustom org-cycle-skip-children-state-if-no-children t
  687. "Non-nil means skip CHILDREN state in entries that don't have any."
  688. :group 'org-cycle
  689. :type 'boolean)
  690. (defcustom org-cycle-max-level nil
  691. "Maximum level which should still be subject to visibility cycling.
  692. Levels higher than this will, for cycling, be treated as text, not a headline.
  693. When `org-odd-levels-only' is set, a value of N in this variable actually
  694. means 2N-1 stars as the limiting headline.
  695. When nil, cycle all levels.
  696. Note that the limiting level of cycling is also influenced by
  697. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  698. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  699. than its value."
  700. :group 'org-cycle
  701. :type '(choice
  702. (const :tag "No limit" nil)
  703. (integer :tag "Maximum level")))
  704. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  705. "Names of drawers. Drawers are not opened by cycling on the headline above.
  706. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  707. this:
  708. :DRAWERNAME:
  709. .....
  710. :END:
  711. The drawer \"PROPERTIES\" is special for capturing properties through
  712. the property API.
  713. Drawers can be defined on the per-file basis with a line like:
  714. #+DRAWERS: HIDDEN STATE PROPERTIES"
  715. :group 'org-structure
  716. :group 'org-cycle
  717. :type '(repeat (string :tag "Drawer Name")))
  718. (defcustom org-hide-block-startup nil
  719. "Non-nil means entering Org-mode will fold all blocks.
  720. This can also be set in on a per-file basis with
  721. #+STARTUP: hideblocks
  722. #+STARTUP: showblocks"
  723. :group 'org-startup
  724. :group 'org-cycle
  725. :type 'boolean)
  726. (defcustom org-cycle-global-at-bob nil
  727. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  728. This makes it possible to do global cycling without having to use S-TAB or
  729. \\[universal-argument] TAB. For this special case to work, the first line \
  730. of the buffer
  731. must not be a headline - it may be empty or some other text. When used in
  732. this way, `org-cycle-hook' is disables temporarily, to make sure the
  733. cursor stays at the beginning of the buffer.
  734. When this option is nil, don't do anything special at the beginning
  735. of the buffer."
  736. :group 'org-cycle
  737. :type 'boolean)
  738. (defcustom org-cycle-level-after-item/entry-creation t
  739. "Non-nil means cycle entry level or item indentation in new empty entries.
  740. When the cursor is at the end of an empty headline, i.e with only stars
  741. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  742. and then all possible ancestor states, before returning to the original state.
  743. This makes data entry extremely fast: M-RET to create a new headline,
  744. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  745. When the cursor is at the end of an empty plain list item, one TAB will
  746. make it a subitem, two or more tabs will back up to make this an item
  747. higher up in the item hierarchy."
  748. :group 'org-cycle
  749. :type 'boolean)
  750. (defcustom org-cycle-emulate-tab t
  751. "Where should `org-cycle' emulate TAB.
  752. nil Never
  753. white Only in completely white lines
  754. whitestart Only at the beginning of lines, before the first non-white char
  755. t Everywhere except in headlines
  756. exc-hl-bol Everywhere except at the start of a headline
  757. If TAB is used in a place where it does not emulate TAB, the current subtree
  758. visibility is cycled."
  759. :group 'org-cycle
  760. :type '(choice (const :tag "Never" nil)
  761. (const :tag "Only in completely white lines" white)
  762. (const :tag "Before first char in a line" whitestart)
  763. (const :tag "Everywhere except in headlines" t)
  764. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  765. ))
  766. (defcustom org-cycle-separator-lines 2
  767. "Number of empty lines needed to keep an empty line between collapsed trees.
  768. If you leave an empty line between the end of a subtree and the following
  769. headline, this empty line is hidden when the subtree is folded.
  770. Org-mode will leave (exactly) one empty line visible if the number of
  771. empty lines is equal or larger to the number given in this variable.
  772. So the default 2 means at least 2 empty lines after the end of a subtree
  773. are needed to produce free space between a collapsed subtree and the
  774. following headline.
  775. If the number is negative, and the number of empty lines is at least -N,
  776. all empty lines are shown.
  777. Special case: when 0, never leave empty lines in collapsed view."
  778. :group 'org-cycle
  779. :type 'integer)
  780. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  781. (defcustom org-pre-cycle-hook nil
  782. "Hook that is run before visibility cycling is happening.
  783. The function(s) in this hook must accept a single argument which indicates
  784. the new state that will be set right after running this hook. The
  785. argument is a symbol. Before a global state change, it can have the values
  786. `overview', `content', or `all'. Before a local state change, it can have
  787. the values `folded', `children', or `subtree'."
  788. :group 'org-cycle
  789. :type 'hook)
  790. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  791. org-cycle-hide-drawers
  792. org-cycle-show-empty-lines
  793. org-optimize-window-after-visibility-change)
  794. "Hook that is run after `org-cycle' has changed the buffer visibility.
  795. The function(s) in this hook must accept a single argument which indicates
  796. the new state that was set by the most recent `org-cycle' command. The
  797. argument is a symbol. After a global state change, it can have the values
  798. `overview', `content', or `all'. After a local state change, it can have
  799. the values `folded', `children', or `subtree'."
  800. :group 'org-cycle
  801. :type 'hook)
  802. (defgroup org-edit-structure nil
  803. "Options concerning structure editing in Org-mode."
  804. :tag "Org Edit Structure"
  805. :group 'org-structure)
  806. (defcustom org-odd-levels-only nil
  807. "Non-nil means skip even levels and only use odd levels for the outline.
  808. This has the effect that two stars are being added/taken away in
  809. promotion/demotion commands. It also influences how levels are
  810. handled by the exporters.
  811. Changing it requires restart of `font-lock-mode' to become effective
  812. for fontification also in regions already fontified.
  813. You may also set this on a per-file basis by adding one of the following
  814. lines to the buffer:
  815. #+STARTUP: odd
  816. #+STARTUP: oddeven"
  817. :group 'org-edit-structure
  818. :group 'org-appearance
  819. :type 'boolean)
  820. (defcustom org-adapt-indentation t
  821. "Non-nil means adapt indentation to outline node level.
  822. When this variable is set, Org assumes that you write outlines by
  823. indenting text in each node to align with the headline (after the stars).
  824. The following issues are influenced by this variable:
  825. - When this is set and the *entire* text in an entry is indented, the
  826. indentation is increased by one space in a demotion command, and
  827. decreased by one in a promotion command. If any line in the entry
  828. body starts with text at column 0, indentation is not changed at all.
  829. - Property drawers and planning information is inserted indented when
  830. this variable s set. When nil, they will not be indented.
  831. - TAB indents a line relative to context. The lines below a headline
  832. will be indented when this variable is set.
  833. Note that this is all about true indentation, by adding and removing
  834. space characters. See also `org-indent.el' which does level-dependent
  835. indentation in a virtual way, i.e. at display time in Emacs."
  836. :group 'org-edit-structure
  837. :type 'boolean)
  838. (defcustom org-special-ctrl-a/e nil
  839. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  840. When t, `C-a' will bring back the cursor to the beginning of the
  841. headline text, i.e. after the stars and after a possible TODO keyword.
  842. In an item, this will be the position after the bullet.
  843. When the cursor is already at that position, another `C-a' will bring
  844. it to the beginning of the line.
  845. `C-e' will jump to the end of the headline, ignoring the presence of tags
  846. in the headline. A second `C-e' will then jump to the true end of the
  847. line, after any tags. This also means that, when this variable is
  848. non-nil, `C-e' also will never jump beyond the end of the heading of a
  849. folded section, i.e. not after the ellipses.
  850. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  851. going to the true line boundary first. Only a directly following, identical
  852. keypress will bring the cursor to the special positions.
  853. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  854. set separately."
  855. :group 'org-edit-structure
  856. :type '(choice
  857. (const :tag "off" nil)
  858. (const :tag "on: after stars/bullet and before tags first" t)
  859. (const :tag "reversed: true line boundary first" reversed)
  860. (cons :tag "Set C-a and C-e separately"
  861. (choice :tag "Special C-a"
  862. (const :tag "off" nil)
  863. (const :tag "on: after stars/bullet first" t)
  864. (const :tag "reversed: before stars/bullet first" reversed))
  865. (choice :tag "Special C-e"
  866. (const :tag "off" nil)
  867. (const :tag "on: before tags first" t)
  868. (const :tag "reversed: after tags first" reversed)))))
  869. (if (fboundp 'defvaralias)
  870. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  871. (defcustom org-special-ctrl-k nil
  872. "Non-nil means `C-k' will behave specially in headlines.
  873. When nil, `C-k' will call the default `kill-line' command.
  874. When t, the following will happen while the cursor is in the headline:
  875. - When the cursor is at the beginning of a headline, kill the entire
  876. line and possible the folded subtree below the line.
  877. - When in the middle of the headline text, kill the headline up to the tags.
  878. - When after the headline text, kill the tags."
  879. :group 'org-edit-structure
  880. :type 'boolean)
  881. (defcustom org-ctrl-k-protect-subtree nil
  882. "Non-nil means, do not delete a hidden subtree with C-k.
  883. When set to the symbol `error', simply throw an error when C-k is
  884. used to kill (part-of) a headline that has hidden text behind it.
  885. Any other non-nil value will result in a query to the user, if it is
  886. OK to kill that hidden subtree. When nil, kill without remorse."
  887. :group 'org-edit-structure
  888. :type '(choice
  889. (const :tag "Do not protect hidden subtrees" nil)
  890. (const :tag "Protect hidden subtrees with a security query" t)
  891. (const :tag "Never kill a hidden subtree with C-k" error)))
  892. (defcustom org-yank-folded-subtrees t
  893. "Non-nil means when yanking subtrees, fold them.
  894. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  895. it starts with a heading and all other headings in it are either children
  896. or siblings, then fold all the subtrees. However, do this only if no
  897. text after the yank would be swallowed into a folded tree by this action."
  898. :group 'org-edit-structure
  899. :type 'boolean)
  900. (defcustom org-yank-adjusted-subtrees nil
  901. "Non-nil means when yanking subtrees, adjust the level.
  902. With this setting, `org-paste-subtree' is used to insert the subtree, see
  903. this function for details."
  904. :group 'org-edit-structure
  905. :type 'boolean)
  906. (defcustom org-M-RET-may-split-line '((default . t))
  907. "Non-nil means M-RET will split the line at the cursor position.
  908. When nil, it will go to the end of the line before making a
  909. new line.
  910. You may also set this option in a different way for different
  911. contexts. Valid contexts are:
  912. headline when creating a new headline
  913. item when creating a new item
  914. table in a table field
  915. default the value to be used for all contexts not explicitly
  916. customized"
  917. :group 'org-structure
  918. :group 'org-table
  919. :type '(choice
  920. (const :tag "Always" t)
  921. (const :tag "Never" nil)
  922. (repeat :greedy t :tag "Individual contexts"
  923. (cons
  924. (choice :tag "Context"
  925. (const headline)
  926. (const item)
  927. (const table)
  928. (const default))
  929. (boolean)))))
  930. (defcustom org-insert-heading-respect-content nil
  931. "Non-nil means insert new headings after the current subtree.
  932. When nil, the new heading is created directly after the current line.
  933. The commands \\[org-insert-heading-respect-content] and
  934. \\[org-insert-todo-heading-respect-content] turn this variable on
  935. for the duration of the command."
  936. :group 'org-structure
  937. :type 'boolean)
  938. (defcustom org-blank-before-new-entry '((heading . auto)
  939. (plain-list-item . auto))
  940. "Should `org-insert-heading' leave a blank line before new heading/item?
  941. The value is an alist, with `heading' and `plain-list-item' as car,
  942. and a boolean flag as cdr. For plain lists, if the variable
  943. `org-empty-line-terminates-plain-lists' is set, the setting here
  944. is ignored and no empty line is inserted, to keep the list in tact."
  945. :group 'org-edit-structure
  946. :type '(list
  947. (cons (const heading)
  948. (choice (const :tag "Never" nil)
  949. (const :tag "Always" t)
  950. (const :tag "Auto" auto)))
  951. (cons (const plain-list-item)
  952. (choice (const :tag "Never" nil)
  953. (const :tag "Always" t)
  954. (const :tag "Auto" auto)))))
  955. (defcustom org-insert-heading-hook nil
  956. "Hook being run after inserting a new heading."
  957. :group 'org-edit-structure
  958. :type 'hook)
  959. (defcustom org-enable-fixed-width-editor t
  960. "Non-nil means lines starting with \":\" are treated as fixed-width.
  961. This currently only means they are never auto-wrapped.
  962. When nil, such lines will be treated like ordinary lines.
  963. See also the QUOTE keyword."
  964. :group 'org-edit-structure
  965. :type 'boolean)
  966. (defcustom org-goto-auto-isearch t
  967. "Non-nil means typing characters in `org-goto' starts incremental search."
  968. :group 'org-edit-structure
  969. :type 'boolean)
  970. (defgroup org-sparse-trees nil
  971. "Options concerning sparse trees in Org-mode."
  972. :tag "Org Sparse Trees"
  973. :group 'org-structure)
  974. (defcustom org-highlight-sparse-tree-matches t
  975. "Non-nil means highlight all matches that define a sparse tree.
  976. The highlights will automatically disappear the next time the buffer is
  977. changed by an edit command."
  978. :group 'org-sparse-trees
  979. :type 'boolean)
  980. (defcustom org-remove-highlights-with-change t
  981. "Non-nil means any change to the buffer will remove temporary highlights.
  982. Such highlights are created by `org-occur' and `org-clock-display'.
  983. When nil, `C-c C-c needs to be used to get rid of the highlights.
  984. The highlights created by `org-preview-latex-fragment' always need
  985. `C-c C-c' to be removed."
  986. :group 'org-sparse-trees
  987. :group 'org-time
  988. :type 'boolean)
  989. (defcustom org-occur-hook '(org-first-headline-recenter)
  990. "Hook that is run after `org-occur' has constructed a sparse tree.
  991. This can be used to recenter the window to show as much of the structure
  992. as possible."
  993. :group 'org-sparse-trees
  994. :type 'hook)
  995. (defgroup org-imenu-and-speedbar nil
  996. "Options concerning imenu and speedbar in Org-mode."
  997. :tag "Org Imenu and Speedbar"
  998. :group 'org-structure)
  999. (defcustom org-imenu-depth 2
  1000. "The maximum level for Imenu access to Org-mode headlines.
  1001. This also applied for speedbar access."
  1002. :group 'org-imenu-and-speedbar
  1003. :type 'integer)
  1004. (defgroup org-table nil
  1005. "Options concerning tables in Org-mode."
  1006. :tag "Org Table"
  1007. :group 'org)
  1008. (defcustom org-enable-table-editor 'optimized
  1009. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1010. When nil, such lines will be treated like ordinary lines.
  1011. When equal to the symbol `optimized', the table editor will be optimized to
  1012. do the following:
  1013. - Automatic overwrite mode in front of whitespace in table fields.
  1014. This makes the structure of the table stay in tact as long as the edited
  1015. field does not exceed the column width.
  1016. - Minimize the number of realigns. Normally, the table is aligned each time
  1017. TAB or RET are pressed to move to another field. With optimization this
  1018. happens only if changes to a field might have changed the column width.
  1019. Optimization requires replacing the functions `self-insert-command',
  1020. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1021. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1022. very good at guessing when a re-align will be necessary, but you can always
  1023. force one with \\[org-ctrl-c-ctrl-c].
  1024. If you would like to use the optimized version in Org-mode, but the
  1025. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1026. This variable can be used to turn on and off the table editor during a session,
  1027. but in order to toggle optimization, a restart is required.
  1028. See also the variable `org-table-auto-blank-field'."
  1029. :group 'org-table
  1030. :type '(choice
  1031. (const :tag "off" nil)
  1032. (const :tag "on" t)
  1033. (const :tag "on, optimized" optimized)))
  1034. (defcustom org-self-insert-cluster-for-undo t
  1035. "Non-nil means cluster self-insert commands for undo when possible.
  1036. If this is set, then, like in the Emacs command loop, 20 consecutive
  1037. characters will be undone together.
  1038. This is configurable, because there is some impact on typing performance."
  1039. :group 'org-table
  1040. :type 'boolean)
  1041. (defcustom org-table-tab-recognizes-table.el t
  1042. "Non-nil means TAB will automatically notice a table.el table.
  1043. When it sees such a table, it moves point into it and - if necessary -
  1044. calls `table-recognize-table'."
  1045. :group 'org-table-editing
  1046. :type 'boolean)
  1047. (defgroup org-link nil
  1048. "Options concerning links in Org-mode."
  1049. :tag "Org Link"
  1050. :group 'org)
  1051. (defvar org-link-abbrev-alist-local nil
  1052. "Buffer-local version of `org-link-abbrev-alist', which see.
  1053. The value of this is taken from the #+LINK lines.")
  1054. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1055. (defcustom org-link-abbrev-alist nil
  1056. "Alist of link abbreviations.
  1057. The car of each element is a string, to be replaced at the start of a link.
  1058. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1059. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1060. [[linkkey:tag][description]]
  1061. The 'linkkey' must be a word word, starting with a letter, followed
  1062. by letters, numbers, '-' or '_'.
  1063. If REPLACE is a string, the tag will simply be appended to create the link.
  1064. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1065. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1066. be inserted at that point (see the function `url-hexify-string').
  1067. REPLACE may also be a function that will be called with the tag as the
  1068. only argument to create the link, which should be returned as a string.
  1069. See the manual for examples."
  1070. :group 'org-link
  1071. :type '(repeat
  1072. (cons
  1073. (string :tag "Protocol")
  1074. (choice
  1075. (string :tag "Format")
  1076. (function)))))
  1077. (defcustom org-descriptive-links t
  1078. "Non-nil means hide link part and only show description of bracket links.
  1079. Bracket links are like [[link][description]]. This variable sets the initial
  1080. state in new org-mode buffers. The setting can then be toggled on a
  1081. per-buffer basis from the Org->Hyperlinks menu."
  1082. :group 'org-link
  1083. :type 'boolean)
  1084. (defcustom org-link-file-path-type 'adaptive
  1085. "How the path name in file links should be stored.
  1086. Valid values are:
  1087. relative Relative to the current directory, i.e. the directory of the file
  1088. into which the link is being inserted.
  1089. absolute Absolute path, if possible with ~ for home directory.
  1090. noabbrev Absolute path, no abbreviation of home directory.
  1091. adaptive Use relative path for files in the current directory and sub-
  1092. directories of it. For other files, use an absolute path."
  1093. :group 'org-link
  1094. :type '(choice
  1095. (const relative)
  1096. (const absolute)
  1097. (const noabbrev)
  1098. (const adaptive)))
  1099. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1100. "Types of links that should be activated in Org-mode files.
  1101. This is a list of symbols, each leading to the activation of a certain link
  1102. type. In principle, it does not hurt to turn on most link types - there may
  1103. be a small gain when turning off unused link types. The types are:
  1104. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1105. angular Links in angular brackets that may contain whitespace like
  1106. <bbdb:Carsten Dominik>.
  1107. plain Plain links in normal text, no whitespace, like http://google.com.
  1108. radio Text that is matched by a radio target, see manual for details.
  1109. tag Tag settings in a headline (link to tag search).
  1110. date Time stamps (link to calendar).
  1111. footnote Footnote labels.
  1112. Changing this variable requires a restart of Emacs to become effective."
  1113. :group 'org-link
  1114. :type '(set :greedy t
  1115. (const :tag "Double bracket links (new style)" bracket)
  1116. (const :tag "Angular bracket links (old style)" angular)
  1117. (const :tag "Plain text links" plain)
  1118. (const :tag "Radio target matches" radio)
  1119. (const :tag "Tags" tag)
  1120. (const :tag "Timestamps" date)
  1121. (const :tag "Footnotes" footnote)))
  1122. (defcustom org-make-link-description-function nil
  1123. "Function to use to generate link descriptions from links.
  1124. If nil the link location will be used. This function must take
  1125. two parameters; the first is the link and the second the
  1126. description `org-insert-link' has generated, and should return the
  1127. description to use."
  1128. :group 'org-link
  1129. :type 'function)
  1130. (defgroup org-link-store nil
  1131. "Options concerning storing links in Org-mode."
  1132. :tag "Org Store Link"
  1133. :group 'org-link)
  1134. (defcustom org-email-link-description-format "Email %c: %.30s"
  1135. "Format of the description part of a link to an email or usenet message.
  1136. The following %-escapes will be replaced by corresponding information:
  1137. %F full \"From\" field
  1138. %f name, taken from \"From\" field, address if no name
  1139. %T full \"To\" field
  1140. %t first name in \"To\" field, address if no name
  1141. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1142. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1143. %s subject
  1144. %m message-id.
  1145. You may use normal field width specification between the % and the letter.
  1146. This is for example useful to limit the length of the subject.
  1147. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1148. :group 'org-link-store
  1149. :type 'string)
  1150. (defcustom org-from-is-user-regexp
  1151. (let (r1 r2)
  1152. (when (and user-mail-address (not (string= user-mail-address "")))
  1153. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1154. (when (and user-full-name (not (string= user-full-name "")))
  1155. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1156. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1157. "Regexp matched against the \"From:\" header of an email or usenet message.
  1158. It should match if the message is from the user him/herself."
  1159. :group 'org-link-store
  1160. :type 'regexp)
  1161. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1162. "Non-nil means storing a link to an Org file will use entry IDs.
  1163. Note that before this variable is even considered, org-id must be loaded,
  1164. so please customize `org-modules' and turn it on.
  1165. The variable can have the following values:
  1166. t Create an ID if needed to make a link to the current entry.
  1167. create-if-interactive
  1168. If `org-store-link' is called directly (interactively, as a user
  1169. command), do create an ID to support the link. But when doing the
  1170. job for remember, only use the ID if it already exists. The
  1171. purpose of this setting is to avoid proliferation of unwanted
  1172. IDs, just because you happen to be in an Org file when you
  1173. call `org-remember' that automatically and preemptively
  1174. creates a link. If you do want to get an ID link in a remember
  1175. template to an entry not having an ID, create it first by
  1176. explicitly creating a link to it, using `C-c C-l' first.
  1177. create-if-interactive-and-no-custom-id
  1178. Like create-if-interactive, but do not create an ID if there is
  1179. a CUSTOM_ID property defined in the entry. This is the default.
  1180. use-existing
  1181. Use existing ID, do not create one.
  1182. nil Never use an ID to make a link, instead link using a text search for
  1183. the headline text."
  1184. :group 'org-link-store
  1185. :type '(choice
  1186. (const :tag "Create ID to make link" t)
  1187. (const :tag "Create if storing link interactively"
  1188. create-if-interactive)
  1189. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1190. create-if-interactive-and-no-custom-id)
  1191. (const :tag "Only use existing" use-existing)
  1192. (const :tag "Do not use ID to create link" nil)))
  1193. (defcustom org-context-in-file-links t
  1194. "Non-nil means file links from `org-store-link' contain context.
  1195. A search string will be added to the file name with :: as separator and
  1196. used to find the context when the link is activated by the command
  1197. `org-open-at-point'.
  1198. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1199. negates this setting for the duration of the command."
  1200. :group 'org-link-store
  1201. :type 'boolean)
  1202. (defcustom org-keep-stored-link-after-insertion nil
  1203. "Non-nil means keep link in list for entire session.
  1204. The command `org-store-link' adds a link pointing to the current
  1205. location to an internal list. These links accumulate during a session.
  1206. The command `org-insert-link' can be used to insert links into any
  1207. Org-mode file (offering completion for all stored links). When this
  1208. option is nil, every link which has been inserted once using \\[org-insert-link]
  1209. will be removed from the list, to make completing the unused links
  1210. more efficient."
  1211. :group 'org-link-store
  1212. :type 'boolean)
  1213. (defgroup org-link-follow nil
  1214. "Options concerning following links in Org-mode."
  1215. :tag "Org Follow Link"
  1216. :group 'org-link)
  1217. (defcustom org-link-translation-function nil
  1218. "Function to translate links with different syntax to Org syntax.
  1219. This can be used to translate links created for example by the Planner
  1220. or emacs-wiki packages to Org syntax.
  1221. The function must accept two parameters, a TYPE containing the link
  1222. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1223. which is everything after the link protocol. It should return a cons
  1224. with possibly modified values of type and path.
  1225. Org contains a function for this, so if you set this variable to
  1226. `org-translate-link-from-planner', you should be able follow many
  1227. links created by planner."
  1228. :group 'org-link-follow
  1229. :type 'function)
  1230. (defcustom org-follow-link-hook nil
  1231. "Hook that is run after a link has been followed."
  1232. :group 'org-link-follow
  1233. :type 'hook)
  1234. (defcustom org-tab-follows-link nil
  1235. "Non-nil means on links TAB will follow the link.
  1236. Needs to be set before org.el is loaded.
  1237. This really should not be used, it does not make sense, and the
  1238. implementation is bad."
  1239. :group 'org-link-follow
  1240. :type 'boolean)
  1241. (defcustom org-return-follows-link nil
  1242. "Non-nil means on links RET will follow the link."
  1243. :group 'org-link-follow
  1244. :type 'boolean)
  1245. (defcustom org-mouse-1-follows-link
  1246. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1247. "Non-nil means mouse-1 on a link will follow the link.
  1248. A longer mouse click will still set point. Does not work on XEmacs.
  1249. Needs to be set before org.el is loaded."
  1250. :group 'org-link-follow
  1251. :type 'boolean)
  1252. (defcustom org-mark-ring-length 4
  1253. "Number of different positions to be recorded in the ring.
  1254. Changing this requires a restart of Emacs to work correctly."
  1255. :group 'org-link-follow
  1256. :type 'integer)
  1257. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1258. "Non-nil means internal links in Org files must exactly match a headline.
  1259. When nil, the link search tries to match a phrase will all words
  1260. in the search text."
  1261. :group 'org-link-follow
  1262. :type '(choice
  1263. (const :tag "Use fuzy text search" nil)
  1264. (const :tag "Match only exact headline" t)
  1265. (const :tag "Match extact headline or query to create it"
  1266. query-to-create)))
  1267. (defcustom org-link-frame-setup
  1268. '((vm . vm-visit-folder-other-frame)
  1269. (gnus . org-gnus-no-new-news)
  1270. (file . find-file-other-window)
  1271. (wl . wl-other-frame))
  1272. "Setup the frame configuration for following links.
  1273. When following a link with Emacs, it may often be useful to display
  1274. this link in another window or frame. This variable can be used to
  1275. set this up for the different types of links.
  1276. For VM, use any of
  1277. `vm-visit-folder'
  1278. `vm-visit-folder-other-frame'
  1279. For Gnus, use any of
  1280. `gnus'
  1281. `gnus-other-frame'
  1282. `org-gnus-no-new-news'
  1283. For FILE, use any of
  1284. `find-file'
  1285. `find-file-other-window'
  1286. `find-file-other-frame'
  1287. For Wanderlust use any of
  1288. `wl'
  1289. `wl-other-frame'
  1290. For the calendar, use the variable `calendar-setup'.
  1291. For BBDB, it is currently only possible to display the matches in
  1292. another window."
  1293. :group 'org-link-follow
  1294. :type '(list
  1295. (cons (const vm)
  1296. (choice
  1297. (const vm-visit-folder)
  1298. (const vm-visit-folder-other-window)
  1299. (const vm-visit-folder-other-frame)))
  1300. (cons (const gnus)
  1301. (choice
  1302. (const gnus)
  1303. (const gnus-other-frame)
  1304. (const org-gnus-no-new-news)))
  1305. (cons (const file)
  1306. (choice
  1307. (const find-file)
  1308. (const find-file-other-window)
  1309. (const find-file-other-frame)))
  1310. (cons (const wl)
  1311. (choice
  1312. (const wl)
  1313. (const wl-other-frame)))))
  1314. (defcustom org-display-internal-link-with-indirect-buffer nil
  1315. "Non-nil means use indirect buffer to display infile links.
  1316. Activating internal links (from one location in a file to another location
  1317. in the same file) normally just jumps to the location. When the link is
  1318. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1319. is displayed in
  1320. another window. When this option is set, the other window actually displays
  1321. an indirect buffer clone of the current buffer, to avoid any visibility
  1322. changes to the current buffer."
  1323. :group 'org-link-follow
  1324. :type 'boolean)
  1325. (defcustom org-open-non-existing-files nil
  1326. "Non-nil means `org-open-file' will open non-existing files.
  1327. When nil, an error will be generated.
  1328. This variable applies only to external applications because they
  1329. might choke on non-existing files. If the link is to a file that
  1330. will be opened in Emacs, the variable is ignored."
  1331. :group 'org-link-follow
  1332. :type 'boolean)
  1333. (defcustom org-open-directory-means-index-dot-org nil
  1334. "Non-nil means a link to a directory really means to index.org.
  1335. When nil, following a directory link will run dired or open a finder/explorer
  1336. window on that directory."
  1337. :group 'org-link-follow
  1338. :type 'boolean)
  1339. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1340. "Function and arguments to call for following mailto links.
  1341. This is a list with the first element being a Lisp function, and the
  1342. remaining elements being arguments to the function. In string arguments,
  1343. %a will be replaced by the address, and %s will be replaced by the subject
  1344. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1345. :group 'org-link-follow
  1346. :type '(choice
  1347. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1348. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1349. (const :tag "message-mail" (message-mail "%a" "%s"))
  1350. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1351. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1352. "Non-nil means ask for confirmation before executing shell links.
  1353. Shell links can be dangerous: just think about a link
  1354. [[shell:rm -rf ~/*][Google Search]]
  1355. This link would show up in your Org-mode document as \"Google Search\",
  1356. but really it would remove your entire home directory.
  1357. Therefore we advise against setting this variable to nil.
  1358. Just change it to `y-or-n-p' if you want to confirm with a
  1359. single keystroke rather than having to type \"yes\"."
  1360. :group 'org-link-follow
  1361. :type '(choice
  1362. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1363. (const :tag "with y-or-n (faster)" y-or-n-p)
  1364. (const :tag "no confirmation (dangerous)" nil)))
  1365. (put 'org-confirm-shell-link-function
  1366. 'safe-local-variable
  1367. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1368. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1369. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1370. Elisp links can be dangerous: just think about a link
  1371. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1372. This link would show up in your Org-mode document as \"Google Search\",
  1373. but really it would remove your entire home directory.
  1374. Therefore we advise against setting this variable to nil.
  1375. Just change it to `y-or-n-p' if you want to confirm with a
  1376. single keystroke rather than having to type \"yes\"."
  1377. :group 'org-link-follow
  1378. :type '(choice
  1379. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1380. (const :tag "with y-or-n (faster)" y-or-n-p)
  1381. (const :tag "no confirmation (dangerous)" nil)))
  1382. (put 'org-confirm-shell-link-function
  1383. 'safe-local-variable
  1384. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1385. (defconst org-file-apps-defaults-gnu
  1386. '((remote . emacs)
  1387. (system . mailcap)
  1388. (t . mailcap))
  1389. "Default file applications on a UNIX or GNU/Linux system.
  1390. See `org-file-apps'.")
  1391. (defconst org-file-apps-defaults-macosx
  1392. '((remote . emacs)
  1393. (t . "open %s")
  1394. (system . "open %s")
  1395. ("ps.gz" . "gv %s")
  1396. ("eps.gz" . "gv %s")
  1397. ("dvi" . "xdvi %s")
  1398. ("fig" . "xfig %s"))
  1399. "Default file applications on a MacOS X system.
  1400. The system \"open\" is known as a default, but we use X11 applications
  1401. for some files for which the OS does not have a good default.
  1402. See `org-file-apps'.")
  1403. (defconst org-file-apps-defaults-windowsnt
  1404. (list
  1405. '(remote . emacs)
  1406. (cons t
  1407. (list (if (featurep 'xemacs)
  1408. 'mswindows-shell-execute
  1409. 'w32-shell-execute)
  1410. "open" 'file))
  1411. (cons 'system
  1412. (list (if (featurep 'xemacs)
  1413. 'mswindows-shell-execute
  1414. 'w32-shell-execute)
  1415. "open" 'file)))
  1416. "Default file applications on a Windows NT system.
  1417. The system \"open\" is used for most files.
  1418. See `org-file-apps'.")
  1419. (defcustom org-file-apps
  1420. '(
  1421. (auto-mode . emacs)
  1422. ("\\.mm\\'" . default)
  1423. ("\\.x?html?\\'" . default)
  1424. ("\\.pdf\\'" . default)
  1425. )
  1426. "External applications for opening `file:path' items in a document.
  1427. Org-mode uses system defaults for different file types, but
  1428. you can use this variable to set the application for a given file
  1429. extension. The entries in this list are cons cells where the car identifies
  1430. files and the cdr the corresponding command. Possible values for the
  1431. file identifier are
  1432. \"string\" A string as a file identifier can be interpreted in different
  1433. ways, depending on its contents:
  1434. - Alphanumeric characters only:
  1435. Match links with this file extension.
  1436. Example: (\"pdf\" . \"evince %s\")
  1437. to open PDFs with evince.
  1438. - Regular expression: Match links where the
  1439. filename matches the regexp. If you want to
  1440. use groups here, use shy groups.
  1441. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1442. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1443. to open *.html and *.xhtml with firefox.
  1444. - Regular expression which contains (non-shy) groups:
  1445. Match links where the whole link, including \"::\", and
  1446. anything after that, matches the regexp.
  1447. In a custom command string, %1, %2, etc. are replaced with
  1448. the parts of the link that were matched by the groups.
  1449. For backwards compatibility, if a command string is given
  1450. that does not use any of the group matches, this case is
  1451. handled identically to the second one (i.e. match against
  1452. file name only).
  1453. In a custom lisp form, you can access the group matches with
  1454. (match-string n link).
  1455. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1456. to open [[file:document.pdf::5]] with evince at page 5.
  1457. `directory' Matches a directory
  1458. `remote' Matches a remote file, accessible through tramp or efs.
  1459. Remote files most likely should be visited through Emacs
  1460. because external applications cannot handle such paths.
  1461. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1462. so all files Emacs knows how to handle. Using this with
  1463. command `emacs' will open most files in Emacs. Beware that this
  1464. will also open html files inside Emacs, unless you add
  1465. (\"html\" . default) to the list as well.
  1466. t Default for files not matched by any of the other options.
  1467. `system' The system command to open files, like `open' on Windows
  1468. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1469. that will be selected if you call `C-c C-o' with a double
  1470. \\[universal-argument] \\[universal-argument] prefix.
  1471. Possible values for the command are:
  1472. `emacs' The file will be visited by the current Emacs process.
  1473. `default' Use the default application for this file type, which is the
  1474. association for t in the list, most likely in the system-specific
  1475. part.
  1476. This can be used to overrule an unwanted setting in the
  1477. system-specific variable.
  1478. `system' Use the system command for opening files, like \"open\".
  1479. This command is specified by the entry whose car is `system'.
  1480. Most likely, the system-specific version of this variable
  1481. does define this command, but you can overrule/replace it
  1482. here.
  1483. string A command to be executed by a shell; %s will be replaced
  1484. by the path to the file.
  1485. sexp A Lisp form which will be evaluated. The file path will
  1486. be available in the Lisp variable `file'.
  1487. For more examples, see the system specific constants
  1488. `org-file-apps-defaults-macosx'
  1489. `org-file-apps-defaults-windowsnt'
  1490. `org-file-apps-defaults-gnu'."
  1491. :group 'org-link-follow
  1492. :type '(repeat
  1493. (cons (choice :value ""
  1494. (string :tag "Extension")
  1495. (const :tag "System command to open files" system)
  1496. (const :tag "Default for unrecognized files" t)
  1497. (const :tag "Remote file" remote)
  1498. (const :tag "Links to a directory" directory)
  1499. (const :tag "Any files that have Emacs modes"
  1500. auto-mode))
  1501. (choice :value ""
  1502. (const :tag "Visit with Emacs" emacs)
  1503. (const :tag "Use default" default)
  1504. (const :tag "Use the system command" system)
  1505. (string :tag "Command")
  1506. (sexp :tag "Lisp form")))))
  1507. (defgroup org-refile nil
  1508. "Options concerning refiling entries in Org-mode."
  1509. :tag "Org Refile"
  1510. :group 'org)
  1511. (defcustom org-directory "~/org"
  1512. "Directory with org files.
  1513. This is just a default location to look for Org files. There is no need
  1514. at all to put your files into this directory. It is only used in the
  1515. following situations:
  1516. 1. When a remember template specifies a target file that is not an
  1517. absolute path. The path will then be interpreted relative to
  1518. `org-directory'
  1519. 2. When a remember note is filed away in an interactive way (when exiting the
  1520. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1521. with `org-directory' as the default path."
  1522. :group 'org-refile
  1523. :group 'org-remember
  1524. :type 'directory)
  1525. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1526. "Default target for storing notes.
  1527. Used as a fall back file for org-remember.el and org-capture.el, for
  1528. templates that do not specify a target file."
  1529. :group 'org-refile
  1530. :group 'org-remember
  1531. :type '(choice
  1532. (const :tag "Default from remember-data-file" nil)
  1533. file))
  1534. (defcustom org-goto-interface 'outline
  1535. "The default interface to be used for `org-goto'.
  1536. Allowed values are:
  1537. outline The interface shows an outline of the relevant file
  1538. and the correct heading is found by moving through
  1539. the outline or by searching with incremental search.
  1540. outline-path-completion Headlines in the current buffer are offered via
  1541. completion. This is the interface also used by
  1542. the refile command."
  1543. :group 'org-refile
  1544. :type '(choice
  1545. (const :tag "Outline" outline)
  1546. (const :tag "Outline-path-completion" outline-path-completion)))
  1547. (defcustom org-goto-max-level 5
  1548. "Maximum target level when running `org-goto' with refile interface."
  1549. :group 'org-refile
  1550. :type 'integer)
  1551. (defcustom org-reverse-note-order nil
  1552. "Non-nil means store new notes at the beginning of a file or entry.
  1553. When nil, new notes will be filed to the end of a file or entry.
  1554. This can also be a list with cons cells of regular expressions that
  1555. are matched against file names, and values."
  1556. :group 'org-remember
  1557. :group 'org-refile
  1558. :type '(choice
  1559. (const :tag "Reverse always" t)
  1560. (const :tag "Reverse never" nil)
  1561. (repeat :tag "By file name regexp"
  1562. (cons regexp boolean))))
  1563. (defcustom org-log-refile nil
  1564. "Information to record when a task is refiled.
  1565. Possible values are:
  1566. nil Don't add anything
  1567. time Add a time stamp to the task
  1568. note Prompt for a note and add it with template `org-log-note-headings'
  1569. This option can also be set with on a per-file-basis with
  1570. #+STARTUP: nologrefile
  1571. #+STARTUP: logrefile
  1572. #+STARTUP: lognoterefile
  1573. You can have local logging settings for a subtree by setting the LOGGING
  1574. property to one or more of these keywords.
  1575. When bulk-refiling from the agenda, the value `note' is forbidden and
  1576. will temporarily be changed to `time'."
  1577. :group 'org-refile
  1578. :group 'org-progress
  1579. :type '(choice
  1580. (const :tag "No logging" nil)
  1581. (const :tag "Record timestamp" time)
  1582. (const :tag "Record timestamp with note." note)))
  1583. (defcustom org-refile-targets nil
  1584. "Targets for refiling entries with \\[org-refile].
  1585. This is list of cons cells. Each cell contains:
  1586. - a specification of the files to be considered, either a list of files,
  1587. or a symbol whose function or variable value will be used to retrieve
  1588. a file name or a list of file names. If you use `org-agenda-files' for
  1589. that, all agenda files will be scanned for targets. Nil means consider
  1590. headings in the current buffer.
  1591. - A specification of how to find candidate refile targets. This may be
  1592. any of:
  1593. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1594. This tag has to be present in all target headlines, inheritance will
  1595. not be considered.
  1596. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1597. todo keyword.
  1598. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1599. headlines that are refiling targets.
  1600. - a cons cell (:level . N). Any headline of level N is considered 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. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1604. Note that, when `org-odd-levels-only' is set, level corresponds to
  1605. order in hierarchy, not to the number of stars.
  1606. You can set the variable `org-refile-target-verify-function' to a function
  1607. to verify each headline found by the simple criteria above.
  1608. When this variable is nil, all top-level headlines in the current buffer
  1609. are used, equivalent to the value `((nil . (:level . 1))'."
  1610. :group 'org-refile
  1611. :type '(repeat
  1612. (cons
  1613. (choice :value org-agenda-files
  1614. (const :tag "All agenda files" org-agenda-files)
  1615. (const :tag "Current buffer" nil)
  1616. (function) (variable) (file))
  1617. (choice :tag "Identify target headline by"
  1618. (cons :tag "Specific tag" (const :value :tag) (string))
  1619. (cons :tag "TODO keyword" (const :value :todo) (string))
  1620. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1621. (cons :tag "Level number" (const :value :level) (integer))
  1622. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1623. (defcustom org-refile-target-verify-function nil
  1624. "Function to verify if the headline at point should be a refile target.
  1625. The function will be called without arguments, with point at the
  1626. beginning of the headline. It should return t and leave point
  1627. where it is if the headline is a valid target for refiling.
  1628. If the target should not be selected, the function must return nil.
  1629. In addition to this, it may move point to a place from where the search
  1630. should be continued. For example, the function may decide that the entire
  1631. subtree of the current entry should be excluded and move point to the end
  1632. of the subtree."
  1633. :group 'org-refile
  1634. :type 'function)
  1635. (defcustom org-refile-use-cache nil
  1636. "Non-nil means cache refile targets to speed up the process.
  1637. The cache for a particular file will be updated automatically when
  1638. the buffer has been killed, or when any of the marker used for flagging
  1639. refile targets no longer points at a live buffer.
  1640. If you have added new entries to a buffer that might themselves be targets,
  1641. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1642. find that easier, `C-u C-u C-u C-c C-w'."
  1643. :group 'org-refile
  1644. :type 'boolean)
  1645. (defcustom org-refile-use-outline-path nil
  1646. "Non-nil means provide refile targets as paths.
  1647. So a level 3 headline will be available as level1/level2/level3.
  1648. When the value is `file', also include the file name (without directory)
  1649. into the path. In this case, you can also stop the completion after
  1650. the file name, to get entries inserted as top level in the file.
  1651. When `full-file-path', include the full file path."
  1652. :group 'org-refile
  1653. :type '(choice
  1654. (const :tag "Not" nil)
  1655. (const :tag "Yes" t)
  1656. (const :tag "Start with file name" file)
  1657. (const :tag "Start with full file path" full-file-path)))
  1658. (defcustom org-outline-path-complete-in-steps t
  1659. "Non-nil means complete the outline path in hierarchical steps.
  1660. When Org-mode uses the refile interface to select an outline path
  1661. \(see variable `org-refile-use-outline-path'), the completion of
  1662. the path can be done is a single go, or if can be done in steps down
  1663. the headline hierarchy. Going in steps is probably the best if you
  1664. do not use a special completion package like `ido' or `icicles'.
  1665. However, when using these packages, going in one step can be very
  1666. fast, while still showing the whole path to the entry."
  1667. :group 'org-refile
  1668. :type 'boolean)
  1669. (defcustom org-refile-allow-creating-parent-nodes nil
  1670. "Non-nil means allow to create new nodes as refile targets.
  1671. New nodes are then created by adding \"/new node name\" to the completion
  1672. of an existing node. When the value of this variable is `confirm',
  1673. new node creation must be confirmed by the user (recommended)
  1674. When nil, the completion must match an existing entry.
  1675. Note that, if the new heading is not seen by the criteria
  1676. listed in `org-refile-targets', multiple instances of the same
  1677. heading would be created by trying again to file under the new
  1678. heading."
  1679. :group 'org-refile
  1680. :type '(choice
  1681. (const :tag "Never" nil)
  1682. (const :tag "Always" t)
  1683. (const :tag "Prompt for confirmation" confirm)))
  1684. (defgroup org-todo nil
  1685. "Options concerning TODO items in Org-mode."
  1686. :tag "Org TODO"
  1687. :group 'org)
  1688. (defgroup org-progress nil
  1689. "Options concerning Progress logging in Org-mode."
  1690. :tag "Org Progress"
  1691. :group 'org-time)
  1692. (defvar org-todo-interpretation-widgets
  1693. '(
  1694. (:tag "Sequence (cycling hits every state)" sequence)
  1695. (:tag "Type (cycling directly to DONE)" type))
  1696. "The available interpretation symbols for customizing `org-todo-keywords'.
  1697. Interested libraries should add to this list.")
  1698. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1699. "List of TODO entry keyword sequences and their interpretation.
  1700. \\<org-mode-map>This is a list of sequences.
  1701. Each sequence starts with a symbol, either `sequence' or `type',
  1702. indicating if the keywords should be interpreted as a sequence of
  1703. action steps, or as different types of TODO items. The first
  1704. keywords are states requiring action - these states will select a headline
  1705. for inclusion into the global TODO list Org-mode produces. If one of
  1706. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1707. signify that no further action is necessary. If \"|\" is not found,
  1708. the last keyword is treated as the only DONE state of the sequence.
  1709. The command \\[org-todo] cycles an entry through these states, and one
  1710. additional state where no keyword is present. For details about this
  1711. cycling, see the manual.
  1712. TODO keywords and interpretation can also be set on a per-file basis with
  1713. the special #+SEQ_TODO and #+TYP_TODO lines.
  1714. Each keyword can optionally specify a character for fast state selection
  1715. \(in combination with the variable `org-use-fast-todo-selection')
  1716. and specifiers for state change logging, using the same syntax
  1717. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1718. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1719. indicates to record a time stamp each time this state is selected.
  1720. Each keyword may also specify if a timestamp or a note should be
  1721. recorded when entering or leaving the state, by adding additional
  1722. characters in the parenthesis after the keyword. This looks like this:
  1723. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1724. record only the time of the state change. With X and Y being either
  1725. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1726. Y when leaving the state if and only if the *target* state does not
  1727. define X. You may omit any of the fast-selection key or X or /Y,
  1728. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1729. For backward compatibility, this variable may also be just a list
  1730. of keywords - in this case the interpretation (sequence or type) will be
  1731. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1732. :group 'org-todo
  1733. :group 'org-keywords
  1734. :type '(choice
  1735. (repeat :tag "Old syntax, just keywords"
  1736. (string :tag "Keyword"))
  1737. (repeat :tag "New syntax"
  1738. (cons
  1739. (choice
  1740. :tag "Interpretation"
  1741. ;;Quick and dirty way to see
  1742. ;;`org-todo-interpretations'. This takes the
  1743. ;;place of item arguments
  1744. :convert-widget
  1745. (lambda (widget)
  1746. (widget-put widget
  1747. :args (mapcar
  1748. #'(lambda (x)
  1749. (widget-convert
  1750. (cons 'const x)))
  1751. org-todo-interpretation-widgets))
  1752. widget))
  1753. (repeat
  1754. (string :tag "Keyword"))))))
  1755. (defvar org-todo-keywords-1 nil
  1756. "All TODO and DONE keywords active in a buffer.")
  1757. (make-variable-buffer-local 'org-todo-keywords-1)
  1758. (defvar org-todo-keywords-for-agenda nil)
  1759. (defvar org-done-keywords-for-agenda nil)
  1760. (defvar org-drawers-for-agenda nil)
  1761. (defvar org-todo-keyword-alist-for-agenda nil)
  1762. (defvar org-tag-alist-for-agenda nil)
  1763. (defvar org-agenda-contributing-files nil)
  1764. (defvar org-not-done-keywords nil)
  1765. (make-variable-buffer-local 'org-not-done-keywords)
  1766. (defvar org-done-keywords nil)
  1767. (make-variable-buffer-local 'org-done-keywords)
  1768. (defvar org-todo-heads nil)
  1769. (make-variable-buffer-local 'org-todo-heads)
  1770. (defvar org-todo-sets nil)
  1771. (make-variable-buffer-local 'org-todo-sets)
  1772. (defvar org-todo-log-states nil)
  1773. (make-variable-buffer-local 'org-todo-log-states)
  1774. (defvar org-todo-kwd-alist nil)
  1775. (make-variable-buffer-local 'org-todo-kwd-alist)
  1776. (defvar org-todo-key-alist nil)
  1777. (make-variable-buffer-local 'org-todo-key-alist)
  1778. (defvar org-todo-key-trigger nil)
  1779. (make-variable-buffer-local 'org-todo-key-trigger)
  1780. (defcustom org-todo-interpretation 'sequence
  1781. "Controls how TODO keywords are interpreted.
  1782. This variable is in principle obsolete and is only used for
  1783. backward compatibility, if the interpretation of todo keywords is
  1784. not given already in `org-todo-keywords'. See that variable for
  1785. more information."
  1786. :group 'org-todo
  1787. :group 'org-keywords
  1788. :type '(choice (const sequence)
  1789. (const type)))
  1790. (defcustom org-use-fast-todo-selection t
  1791. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1792. This variable describes if and under what circumstances the cycling
  1793. mechanism for TODO keywords will be replaced by a single-key, direct
  1794. selection scheme.
  1795. When nil, fast selection is never used.
  1796. When the symbol `prefix', it will be used when `org-todo' is called with
  1797. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1798. in an agenda buffer.
  1799. When t, fast selection is used by default. In this case, the prefix
  1800. argument forces cycling instead.
  1801. In all cases, the special interface is only used if access keys have actually
  1802. been assigned by the user, i.e. if keywords in the configuration are followed
  1803. by a letter in parenthesis, like TODO(t)."
  1804. :group 'org-todo
  1805. :type '(choice
  1806. (const :tag "Never" nil)
  1807. (const :tag "By default" t)
  1808. (const :tag "Only with C-u C-c C-t" prefix)))
  1809. (defcustom org-provide-todo-statistics t
  1810. "Non-nil means update todo statistics after insert and toggle.
  1811. ALL-HEADLINES means update todo statistics by including headlines
  1812. with no TODO keyword as well, counting them as not done.
  1813. A list of TODO keywords means the same, but skip keywords that are
  1814. not in this list.
  1815. When this is set, todo statistics is updated in the parent of the
  1816. current entry each time a todo state is changed."
  1817. :group 'org-todo
  1818. :type '(choice
  1819. (const :tag "Yes, only for TODO entries" t)
  1820. (const :tag "Yes, including all entries" 'all-headlines)
  1821. (repeat :tag "Yes, for TODOs in this list"
  1822. (string :tag "TODO keyword"))
  1823. (other :tag "No TODO statistics" nil)))
  1824. (defcustom org-hierarchical-todo-statistics t
  1825. "Non-nil means TODO statistics covers just direct children.
  1826. When nil, all entries in the subtree are considered.
  1827. This has only an effect if `org-provide-todo-statistics' is set.
  1828. To set this to nil for only a single subtree, use a COOKIE_DATA
  1829. property and include the word \"recursive\" into the value."
  1830. :group 'org-todo
  1831. :type 'boolean)
  1832. (defcustom org-after-todo-state-change-hook nil
  1833. "Hook which is run after the state of a TODO item was changed.
  1834. The new state (a string with a TODO keyword, or nil) is available in the
  1835. Lisp variable `state'."
  1836. :group 'org-todo
  1837. :type 'hook)
  1838. (defvar org-blocker-hook nil
  1839. "Hook for functions that are allowed to block a state change.
  1840. Each function gets as its single argument a property list, see
  1841. `org-trigger-hook' for more information about this list.
  1842. If any of the functions in this hook returns nil, the state change
  1843. is blocked.")
  1844. (defvar org-trigger-hook nil
  1845. "Hook for functions that are triggered by a state change.
  1846. Each function gets as its single argument a property list with at least
  1847. the following elements:
  1848. (:type type-of-change :position pos-at-entry-start
  1849. :from old-state :to new-state)
  1850. Depending on the type, more properties may be present.
  1851. This mechanism is currently implemented for:
  1852. TODO state changes
  1853. ------------------
  1854. :type todo-state-change
  1855. :from previous state (keyword as a string), or nil, or a symbol
  1856. 'todo' or 'done', to indicate the general type of state.
  1857. :to new state, like in :from")
  1858. (defcustom org-enforce-todo-dependencies nil
  1859. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1860. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1861. be blocked if any prior sibling is not yet done.
  1862. Finally, if the parent is blocked because of ordered siblings of its own,
  1863. the child will also be blocked.
  1864. This variable needs to be set before org.el is loaded, and you need to
  1865. restart Emacs after a change to make the change effective. The only way
  1866. to change is while Emacs is running is through the customize interface."
  1867. :set (lambda (var val)
  1868. (set var val)
  1869. (if val
  1870. (add-hook 'org-blocker-hook
  1871. 'org-block-todo-from-children-or-siblings-or-parent)
  1872. (remove-hook 'org-blocker-hook
  1873. 'org-block-todo-from-children-or-siblings-or-parent)))
  1874. :group 'org-todo
  1875. :type 'boolean)
  1876. (defcustom org-enforce-todo-checkbox-dependencies nil
  1877. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1878. When this is nil, checkboxes have no influence on switching TODO states.
  1879. When non-nil, you first need to check off all check boxes before the TODO
  1880. entry can be switched to DONE.
  1881. This variable needs to be set before org.el is loaded, and you need to
  1882. restart Emacs after a change to make the change effective. The only way
  1883. to change is while Emacs is running is through the customize interface."
  1884. :set (lambda (var val)
  1885. (set var val)
  1886. (if val
  1887. (add-hook 'org-blocker-hook
  1888. 'org-block-todo-from-checkboxes)
  1889. (remove-hook 'org-blocker-hook
  1890. 'org-block-todo-from-checkboxes)))
  1891. :group 'org-todo
  1892. :type 'boolean)
  1893. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1894. "Non-nil means inserting a TODO heading is treated as state change.
  1895. So when the command \\[org-insert-todo-heading] is used, state change
  1896. logging will apply if appropriate. When nil, the new TODO item will
  1897. be inserted directly, and no logging will take place."
  1898. :group 'org-todo
  1899. :type 'boolean)
  1900. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1901. "Non-nil means switching TODO states with S-cursor counts as state change.
  1902. This is the default behavior. However, setting this to nil allows a
  1903. convenient way to select a TODO state and bypass any logging associated
  1904. with that."
  1905. :group 'org-todo
  1906. :type 'boolean)
  1907. (defcustom org-todo-state-tags-triggers nil
  1908. "Tag changes that should be triggered by TODO state changes.
  1909. This is a list. Each entry is
  1910. (state-change (tag . flag) .......)
  1911. State-change can be a string with a state, and empty string to indicate the
  1912. state that has no TODO keyword, or it can be one of the symbols `todo'
  1913. or `done', meaning any not-done or done state, respectively."
  1914. :group 'org-todo
  1915. :group 'org-tags
  1916. :type '(repeat
  1917. (cons (choice :tag "When changing to"
  1918. (const :tag "Not-done state" todo)
  1919. (const :tag "Done state" done)
  1920. (string :tag "State"))
  1921. (repeat
  1922. (cons :tag "Tag action"
  1923. (string :tag "Tag")
  1924. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1925. (defcustom org-log-done nil
  1926. "Information to record when a task moves to the DONE state.
  1927. Possible values are:
  1928. nil Don't add anything, just change the keyword
  1929. time Add a time stamp to the task
  1930. note Prompt for a note and add it with template `org-log-note-headings'
  1931. This option can also be set with on a per-file-basis with
  1932. #+STARTUP: nologdone
  1933. #+STARTUP: logdone
  1934. #+STARTUP: lognotedone
  1935. You can have local logging settings for a subtree by setting the LOGGING
  1936. property to one or more of these keywords."
  1937. :group 'org-todo
  1938. :group 'org-progress
  1939. :type '(choice
  1940. (const :tag "No logging" nil)
  1941. (const :tag "Record CLOSED timestamp" time)
  1942. (const :tag "Record CLOSED timestamp with note." note)))
  1943. ;; Normalize old uses of org-log-done.
  1944. (cond
  1945. ((eq org-log-done t) (setq org-log-done 'time))
  1946. ((and (listp org-log-done) (memq 'done org-log-done))
  1947. (setq org-log-done 'note)))
  1948. (defcustom org-log-reschedule nil
  1949. "Information to record when the scheduling date of a tasks is modified.
  1950. Possible values are:
  1951. nil Don't add anything, just change the date
  1952. time Add a time stamp to the task
  1953. note Prompt for a note and add it with template `org-log-note-headings'
  1954. This option can also be set with on a per-file-basis with
  1955. #+STARTUP: nologreschedule
  1956. #+STARTUP: logreschedule
  1957. #+STARTUP: lognotereschedule"
  1958. :group 'org-todo
  1959. :group 'org-progress
  1960. :type '(choice
  1961. (const :tag "No logging" nil)
  1962. (const :tag "Record timestamp" time)
  1963. (const :tag "Record timestamp with note." note)))
  1964. (defcustom org-log-redeadline nil
  1965. "Information to record when the deadline date of a tasks is modified.
  1966. Possible values are:
  1967. nil Don't add anything, just change the date
  1968. time Add a time stamp to the task
  1969. note Prompt for a note and add it with template `org-log-note-headings'
  1970. This option can also be set with on a per-file-basis with
  1971. #+STARTUP: nologredeadline
  1972. #+STARTUP: logredeadline
  1973. #+STARTUP: lognoteredeadline
  1974. You can have local logging settings for a subtree by setting the LOGGING
  1975. property to one or more of these keywords."
  1976. :group 'org-todo
  1977. :group 'org-progress
  1978. :type '(choice
  1979. (const :tag "No logging" nil)
  1980. (const :tag "Record timestamp" time)
  1981. (const :tag "Record timestamp with note." note)))
  1982. (defcustom org-log-note-clock-out nil
  1983. "Non-nil means record a note when clocking out of an item.
  1984. This can also be configured on a per-file basis by adding one of
  1985. the following lines anywhere in the buffer:
  1986. #+STARTUP: lognoteclock-out
  1987. #+STARTUP: nolognoteclock-out"
  1988. :group 'org-todo
  1989. :group 'org-progress
  1990. :type 'boolean)
  1991. (defcustom org-log-done-with-time t
  1992. "Non-nil means the CLOSED time stamp will contain date and time.
  1993. When nil, only the date will be recorded."
  1994. :group 'org-progress
  1995. :type 'boolean)
  1996. (defcustom org-log-note-headings
  1997. '((done . "CLOSING NOTE %t")
  1998. (state . "State %-12s from %-12S %t")
  1999. (note . "Note taken on %t")
  2000. (reschedule . "Rescheduled from %S on %t")
  2001. (delschedule . "Not scheduled, was %S on %t")
  2002. (redeadline . "New deadline from %S on %t")
  2003. (deldeadline . "Removed deadline, was %S on %t")
  2004. (refile . "Refiled on %t")
  2005. (clock-out . ""))
  2006. "Headings for notes added to entries.
  2007. The value is an alist, with the car being a symbol indicating the note
  2008. context, and the cdr is the heading to be used. The heading may also be the
  2009. empty string.
  2010. %t in the heading will be replaced by a time stamp.
  2011. %T will be an active time stamp instead the default inactive one
  2012. %s will be replaced by the new TODO state, in double quotes.
  2013. %S will be replaced by the old TODO state, in double quotes.
  2014. %u will be replaced by the user name.
  2015. %U will be replaced by the full user name.
  2016. In fact, it is not a good idea to change the `state' entry, because
  2017. agenda log mode depends on the format of these entries."
  2018. :group 'org-todo
  2019. :group 'org-progress
  2020. :type '(list :greedy t
  2021. (cons (const :tag "Heading when closing an item" done) string)
  2022. (cons (const :tag
  2023. "Heading when changing todo state (todo sequence only)"
  2024. state) string)
  2025. (cons (const :tag "Heading when just taking a note" note) string)
  2026. (cons (const :tag "Heading when clocking out" clock-out) string)
  2027. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2028. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2029. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2030. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2031. (cons (const :tag "Heading when refiling" refile) string)))
  2032. (unless (assq 'note org-log-note-headings)
  2033. (push '(note . "%t") org-log-note-headings))
  2034. (defcustom org-log-into-drawer nil
  2035. "Non-nil means insert state change notes and time stamps into a drawer.
  2036. When nil, state changes notes will be inserted after the headline and
  2037. any scheduling and clock lines, but not inside a drawer.
  2038. The value of this variable should be the name of the drawer to use.
  2039. LOGBOOK is proposed at the default drawer for this purpose, you can
  2040. also set this to a string to define the drawer of your choice.
  2041. A value of t is also allowed, representing \"LOGBOOK\".
  2042. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2043. will be ignored.
  2044. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2045. a subtree."
  2046. :group 'org-todo
  2047. :group 'org-progress
  2048. :type '(choice
  2049. (const :tag "Not into a drawer" nil)
  2050. (const :tag "LOGBOOK" t)
  2051. (string :tag "Other")))
  2052. (if (fboundp 'defvaralias)
  2053. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2054. (defun org-log-into-drawer ()
  2055. "Return the value of `org-log-into-drawer', but let properties overrule.
  2056. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2057. used instead of the default value."
  2058. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2059. (cond
  2060. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2061. ((equal p "t") "LOGBOOK")
  2062. (t p))))
  2063. (defcustom org-log-state-notes-insert-after-drawers nil
  2064. "Non-nil means insert state change notes after any drawers in entry.
  2065. Only the drawers that *immediately* follow the headline and the
  2066. deadline/scheduled line are skipped.
  2067. When nil, insert notes right after the heading and perhaps the line
  2068. with deadline/scheduling if present.
  2069. This variable will have no effect if `org-log-into-drawer' is
  2070. set."
  2071. :group 'org-todo
  2072. :group 'org-progress
  2073. :type 'boolean)
  2074. (defcustom org-log-states-order-reversed t
  2075. "Non-nil means the latest state note will be directly after heading.
  2076. When nil, the state change notes will be ordered according to time."
  2077. :group 'org-todo
  2078. :group 'org-progress
  2079. :type 'boolean)
  2080. (defcustom org-todo-repeat-to-state nil
  2081. "The TODO state to which a repeater should return the repeating task.
  2082. By default this is the first task in a TODO sequence, or the previous state
  2083. in a TODO_TYP set. But you can specify another task here.
  2084. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2085. :group 'org-todo
  2086. :type '(choice (const :tag "Head of sequence" nil)
  2087. (string :tag "Specific state")))
  2088. (defcustom org-log-repeat 'time
  2089. "Non-nil means record moving through the DONE state when triggering repeat.
  2090. An auto-repeating task is immediately switched back to TODO when
  2091. marked DONE. If you are not logging state changes (by adding \"@\"
  2092. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2093. record a closing note, there will be no record of the task moving
  2094. through DONE. This variable forces taking a note anyway.
  2095. nil Don't force a record
  2096. time Record a time stamp
  2097. note Record a note
  2098. This option can also be set with on a per-file-basis with
  2099. #+STARTUP: logrepeat
  2100. #+STARTUP: lognoterepeat
  2101. #+STARTUP: nologrepeat
  2102. You can have local logging settings for a subtree by setting the LOGGING
  2103. property to one or more of these keywords."
  2104. :group 'org-todo
  2105. :group 'org-progress
  2106. :type '(choice
  2107. (const :tag "Don't force a record" nil)
  2108. (const :tag "Force recording the DONE state" time)
  2109. (const :tag "Force recording a note with the DONE state" note)))
  2110. (defgroup org-priorities nil
  2111. "Priorities in Org-mode."
  2112. :tag "Org Priorities"
  2113. :group 'org-todo)
  2114. (defcustom org-enable-priority-commands t
  2115. "Non-nil means priority commands are active.
  2116. When nil, these commands will be disabled, so that you never accidentally
  2117. set a priority."
  2118. :group 'org-priorities
  2119. :type 'boolean)
  2120. (defcustom org-highest-priority ?A
  2121. "The highest priority of TODO items. A character like ?A, ?B etc.
  2122. Must have a smaller ASCII number than `org-lowest-priority'."
  2123. :group 'org-priorities
  2124. :type 'character)
  2125. (defcustom org-lowest-priority ?C
  2126. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2127. Must have a larger ASCII number than `org-highest-priority'."
  2128. :group 'org-priorities
  2129. :type 'character)
  2130. (defcustom org-default-priority ?B
  2131. "The default priority of TODO items.
  2132. This is the priority an item get if no explicit priority is given."
  2133. :group 'org-priorities
  2134. :type 'character)
  2135. (defcustom org-priority-start-cycle-with-default t
  2136. "Non-nil means start with default priority when starting to cycle.
  2137. When this is nil, the first step in the cycle will be (depending on the
  2138. command used) one higher or lower that the default priority."
  2139. :group 'org-priorities
  2140. :type 'boolean)
  2141. (defgroup org-time nil
  2142. "Options concerning time stamps and deadlines in Org-mode."
  2143. :tag "Org Time"
  2144. :group 'org)
  2145. (defcustom org-insert-labeled-timestamps-at-point nil
  2146. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2147. When nil, these labeled time stamps are forces into the second line of an
  2148. entry, just after the headline. When scheduling from the global TODO list,
  2149. the time stamp will always be forced into the second line."
  2150. :group 'org-time
  2151. :type 'boolean)
  2152. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2153. "Formats for `format-time-string' which are used for time stamps.
  2154. It is not recommended to change this constant.")
  2155. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2156. "Number of minutes to round time stamps to.
  2157. These are two values, the first applies when first creating a time stamp.
  2158. The second applies when changing it with the commands `S-up' and `S-down'.
  2159. When changing the time stamp, this means that it will change in steps
  2160. of N minutes, as given by the second value.
  2161. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2162. numbers should be factors of 60, so for example 5, 10, 15.
  2163. When this is larger than 1, you can still force an exact time stamp by using
  2164. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2165. and by using a prefix arg to `S-up/down' to specify the exact number
  2166. of minutes to shift."
  2167. :group 'org-time
  2168. :get '(lambda (var) ; Make sure both elements are there
  2169. (if (integerp (default-value var))
  2170. (list (default-value var) 5)
  2171. (default-value var)))
  2172. :type '(list
  2173. (integer :tag "when inserting times")
  2174. (integer :tag "when modifying times")))
  2175. ;; Normalize old customizations of this variable.
  2176. (when (integerp org-time-stamp-rounding-minutes)
  2177. (setq org-time-stamp-rounding-minutes
  2178. (list org-time-stamp-rounding-minutes
  2179. org-time-stamp-rounding-minutes)))
  2180. (defcustom org-display-custom-times nil
  2181. "Non-nil means overlay custom formats over all time stamps.
  2182. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2183. To turn this on on a per-file basis, insert anywhere in the file:
  2184. #+STARTUP: customtime"
  2185. :group 'org-time
  2186. :set 'set-default
  2187. :type 'sexp)
  2188. (make-variable-buffer-local 'org-display-custom-times)
  2189. (defcustom org-time-stamp-custom-formats
  2190. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2191. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2192. These are overlayed over the default ISO format if the variable
  2193. `org-display-custom-times' is set. Time like %H:%M should be at the
  2194. end of the second format. The custom formats are also honored by export
  2195. commands, if custom time display is turned on at the time of export."
  2196. :group 'org-time
  2197. :type 'sexp)
  2198. (defun org-time-stamp-format (&optional long inactive)
  2199. "Get the right format for a time string."
  2200. (let ((f (if long (cdr org-time-stamp-formats)
  2201. (car org-time-stamp-formats))))
  2202. (if inactive
  2203. (concat "[" (substring f 1 -1) "]")
  2204. f)))
  2205. (defcustom org-time-clocksum-format "%d:%02d"
  2206. "The format string used when creating CLOCKSUM lines.
  2207. This is also used when org-mode generates a time duration."
  2208. :group 'org-time
  2209. :type 'string)
  2210. (defcustom org-time-clocksum-use-fractional nil
  2211. "If non-nil, \\[org-clock-display] uses fractional times.
  2212. org-mode generates a time duration."
  2213. :group 'org-time
  2214. :type 'boolean)
  2215. (defcustom org-time-clocksum-fractional-format "%.2f"
  2216. "The format string used when creating CLOCKSUM lines, or when
  2217. org-mode generates a time duration."
  2218. :group 'org-time
  2219. :type 'string)
  2220. (defcustom org-deadline-warning-days 14
  2221. "No. of days before expiration during which a deadline becomes active.
  2222. This variable governs the display in sparse trees and in the agenda.
  2223. When 0 or negative, it means use this number (the absolute value of it)
  2224. even if a deadline has a different individual lead time specified.
  2225. Custom commands can set this variable in the options section."
  2226. :group 'org-time
  2227. :group 'org-agenda-daily/weekly
  2228. :type 'integer)
  2229. (defcustom org-read-date-prefer-future t
  2230. "Non-nil means assume future for incomplete date input from user.
  2231. This affects the following situations:
  2232. 1. The user gives a month but not a year.
  2233. For example, if it is April and you enter \"feb 2\", this will be read
  2234. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2235. 2. The user gives a day, but no month.
  2236. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2237. read this as the third of *next* month. However, if you enter \"17\",
  2238. it will be considered as *this* month.
  2239. If you set this variable to the symbol `time', then also the following
  2240. will work:
  2241. 3. If the user gives a time, but no day. If the time is before now,
  2242. to will be interpreted as tomorrow.
  2243. Currently none of this works for ISO week specifications.
  2244. When this option is nil, the current day, month and year will always be
  2245. used as defaults."
  2246. :group 'org-time
  2247. :type '(choice
  2248. (const :tag "Never" nil)
  2249. (const :tag "Check month and day" t)
  2250. (const :tag "Check month, day, and time" time)))
  2251. (defcustom org-read-date-display-live t
  2252. "Non-nil means display current interpretation of date prompt live.
  2253. This display will be in an overlay, in the minibuffer."
  2254. :group 'org-time
  2255. :type 'boolean)
  2256. (defcustom org-read-date-popup-calendar t
  2257. "Non-nil means pop up a calendar when prompting for a date.
  2258. In the calendar, the date can be selected with mouse-1. However, the
  2259. minibuffer will also be active, and you can simply enter the date as well.
  2260. When nil, only the minibuffer will be available."
  2261. :group 'org-time
  2262. :type 'boolean)
  2263. (if (fboundp 'defvaralias)
  2264. (defvaralias 'org-popup-calendar-for-date-prompt
  2265. 'org-read-date-popup-calendar))
  2266. (defcustom org-read-date-minibuffer-setup-hook nil
  2267. "Hook to be used to set up keys for the date/time interface.
  2268. Add key definitions to `minibuffer-local-map', which will be a temporary
  2269. copy."
  2270. :group 'org-time
  2271. :type 'hook)
  2272. (defcustom org-extend-today-until 0
  2273. "The hour when your day really ends. Must be an integer.
  2274. This has influence for the following applications:
  2275. - When switching the agenda to \"today\". It it is still earlier than
  2276. the time given here, the day recognized as TODAY is actually yesterday.
  2277. - When a date is read from the user and it is still before the time given
  2278. here, the current date and time will be assumed to be yesterday, 23:59.
  2279. Also, timestamps inserted in remember templates follow this rule.
  2280. IMPORTANT: This is a feature whose implementation is and likely will
  2281. remain incomplete. Really, it is only here because past midnight seems to
  2282. be the favorite working time of John Wiegley :-)"
  2283. :group 'org-time
  2284. :type 'integer)
  2285. (defcustom org-edit-timestamp-down-means-later nil
  2286. "Non-nil means S-down will increase the time in a time stamp.
  2287. When nil, S-up will increase."
  2288. :group 'org-time
  2289. :type 'boolean)
  2290. (defcustom org-calendar-follow-timestamp-change t
  2291. "Non-nil means make the calendar window follow timestamp changes.
  2292. When a timestamp is modified and the calendar window is visible, it will be
  2293. moved to the new date."
  2294. :group 'org-time
  2295. :type 'boolean)
  2296. (defgroup org-tags nil
  2297. "Options concerning tags in Org-mode."
  2298. :tag "Org Tags"
  2299. :group 'org)
  2300. (defcustom org-tag-alist nil
  2301. "List of tags allowed in Org-mode files.
  2302. When this list is nil, Org-mode will base TAG input on what is already in the
  2303. buffer.
  2304. The value of this variable is an alist, the car of each entry must be a
  2305. keyword as a string, the cdr may be a character that is used to select
  2306. that tag through the fast-tag-selection interface.
  2307. See the manual for details."
  2308. :group 'org-tags
  2309. :type '(repeat
  2310. (choice
  2311. (cons (string :tag "Tag name")
  2312. (character :tag "Access char"))
  2313. (list :tag "Start radio group"
  2314. (const :startgroup)
  2315. (option (string :tag "Group description")))
  2316. (list :tag "End radio group"
  2317. (const :endgroup)
  2318. (option (string :tag "Group description")))
  2319. (const :tag "New line" (:newline)))))
  2320. (defcustom org-tag-persistent-alist nil
  2321. "List of tags that will always appear in all Org-mode files.
  2322. This is in addition to any in buffer settings or customizations
  2323. of `org-tag-alist'.
  2324. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2325. The value of this variable is an alist, the car of each entry must be a
  2326. keyword as a string, the cdr may be a character that is used to select
  2327. that tag through the fast-tag-selection interface.
  2328. See the manual for details.
  2329. To disable these tags on a per-file basis, insert anywhere in the file:
  2330. #+STARTUP: noptag"
  2331. :group 'org-tags
  2332. :type '(repeat
  2333. (choice
  2334. (cons (string :tag "Tag name")
  2335. (character :tag "Access char"))
  2336. (const :tag "Start radio group" (:startgroup))
  2337. (const :tag "End radio group" (:endgroup))
  2338. (const :tag "New line" (:newline)))))
  2339. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2340. "If non-nil, always offer completion for all tags of all agenda files.
  2341. Instead of customizing this variable directly, you might want to
  2342. set it locally for remember buffers, because there no list of
  2343. tags in that file can be created dynamically (there are none).
  2344. (add-hook 'org-remember-mode-hook
  2345. (lambda ()
  2346. (set (make-local-variable
  2347. 'org-complete-tags-always-offer-all-agenda-tags)
  2348. t)))"
  2349. :group 'org-tags
  2350. :type 'boolean)
  2351. (defvar org-file-tags nil
  2352. "List of tags that can be inherited by all entries in the file.
  2353. The tags will be inherited if the variable `org-use-tag-inheritance'
  2354. says they should be.
  2355. This variable is populated from #+FILETAGS lines.")
  2356. (defcustom org-use-fast-tag-selection 'auto
  2357. "Non-nil means use fast tag selection scheme.
  2358. This is a special interface to select and deselect tags with single keys.
  2359. When nil, fast selection is never used.
  2360. When the symbol `auto', fast selection is used if and only if selection
  2361. characters for tags have been configured, either through the variable
  2362. `org-tag-alist' or through a #+TAGS line in the buffer.
  2363. When t, fast selection is always used and selection keys are assigned
  2364. automatically if necessary."
  2365. :group 'org-tags
  2366. :type '(choice
  2367. (const :tag "Always" t)
  2368. (const :tag "Never" nil)
  2369. (const :tag "When selection characters are configured" 'auto)))
  2370. (defcustom org-fast-tag-selection-single-key nil
  2371. "Non-nil means fast tag selection exits after first change.
  2372. When nil, you have to press RET to exit it.
  2373. During fast tag selection, you can toggle this flag with `C-c'.
  2374. This variable can also have the value `expert'. In this case, the window
  2375. displaying the tags menu is not even shown, until you press C-c again."
  2376. :group 'org-tags
  2377. :type '(choice
  2378. (const :tag "No" nil)
  2379. (const :tag "Yes" t)
  2380. (const :tag "Expert" expert)))
  2381. (defvar org-fast-tag-selection-include-todo nil
  2382. "Non-nil means fast tags selection interface will also offer TODO states.
  2383. This is an undocumented feature, you should not rely on it.")
  2384. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2385. "The column to which tags should be indented in a headline.
  2386. If this number is positive, it specifies the column. If it is negative,
  2387. it means that the tags should be flushright to that column. For example,
  2388. -80 works well for a normal 80 character screen."
  2389. :group 'org-tags
  2390. :type 'integer)
  2391. (defcustom org-auto-align-tags t
  2392. "Non-nil means realign tags after pro/demotion of TODO state change.
  2393. These operations change the length of a headline and therefore shift
  2394. the tags around. With this options turned on, after each such operation
  2395. the tags are again aligned to `org-tags-column'."
  2396. :group 'org-tags
  2397. :type 'boolean)
  2398. (defcustom org-use-tag-inheritance t
  2399. "Non-nil means tags in levels apply also for sublevels.
  2400. When nil, only the tags directly given in a specific line apply there.
  2401. This may also be a list of tags that should be inherited, or a regexp that
  2402. matches tags that should be inherited. Additional control is possible
  2403. with the variable `org-tags-exclude-from-inheritance' which gives an
  2404. explicit list of tags to be excluded from inheritance., even if the value of
  2405. `org-use-tag-inheritance' would select it for inheritance.
  2406. If this option is t, a match early-on in a tree can lead to a large
  2407. number of matches in the subtree when constructing the agenda or creating
  2408. a sparse tree. If you only want to see the first match in a tree during
  2409. a search, check out the variable `org-tags-match-list-sublevels'."
  2410. :group 'org-tags
  2411. :type '(choice
  2412. (const :tag "Not" nil)
  2413. (const :tag "Always" t)
  2414. (repeat :tag "Specific tags" (string :tag "Tag"))
  2415. (regexp :tag "Tags matched by regexp")))
  2416. (defcustom org-tags-exclude-from-inheritance nil
  2417. "List of tags that should never be inherited.
  2418. This is a way to exclude a few tags from inheritance. For way to do
  2419. the opposite, to actively allow inheritance for selected tags,
  2420. see the variable `org-use-tag-inheritance'."
  2421. :group 'org-tags
  2422. :type '(repeat (string :tag "Tag")))
  2423. (defun org-tag-inherit-p (tag)
  2424. "Check if TAG is one that should be inherited."
  2425. (cond
  2426. ((member tag org-tags-exclude-from-inheritance) nil)
  2427. ((eq org-use-tag-inheritance t) t)
  2428. ((not org-use-tag-inheritance) nil)
  2429. ((stringp org-use-tag-inheritance)
  2430. (string-match org-use-tag-inheritance tag))
  2431. ((listp org-use-tag-inheritance)
  2432. (member tag org-use-tag-inheritance))
  2433. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2434. (defcustom org-tags-match-list-sublevels t
  2435. "Non-nil means list also sublevels of headlines matching a search.
  2436. This variable applies to tags/property searches, and also to stuck
  2437. projects because this search is based on a tags match as well.
  2438. When set to the symbol `indented', sublevels are indented with
  2439. leading dots.
  2440. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2441. the sublevels of a headline matching a tag search often also match
  2442. the same search. Listing all of them can create very long lists.
  2443. Setting this variable to nil causes subtrees of a match to be skipped.
  2444. This variable is semi-obsolete and probably should always be true. It
  2445. is better to limit inheritance to certain tags using the variables
  2446. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2447. :group 'org-tags
  2448. :type '(choice
  2449. (const :tag "No, don't list them" nil)
  2450. (const :tag "Yes, do list them" t)
  2451. (const :tag "List them, indented with leading dots" indented)))
  2452. (defcustom org-tags-sort-function nil
  2453. "When set, tags are sorted using this function as a comparator."
  2454. :group 'org-tags
  2455. :type '(choice
  2456. (const :tag "No sorting" nil)
  2457. (const :tag "Alphabetical" string<)
  2458. (const :tag "Reverse alphabetical" string>)
  2459. (function :tag "Custom function" nil)))
  2460. (defvar org-tags-history nil
  2461. "History of minibuffer reads for tags.")
  2462. (defvar org-last-tags-completion-table nil
  2463. "The last used completion table for tags.")
  2464. (defvar org-after-tags-change-hook nil
  2465. "Hook that is run after the tags in a line have changed.")
  2466. (defgroup org-properties nil
  2467. "Options concerning properties in Org-mode."
  2468. :tag "Org Properties"
  2469. :group 'org)
  2470. (defcustom org-property-format "%-10s %s"
  2471. "How property key/value pairs should be formatted by `indent-line'.
  2472. When `indent-line' hits a property definition, it will format the line
  2473. according to this format, mainly to make sure that the values are
  2474. lined-up with respect to each other."
  2475. :group 'org-properties
  2476. :type 'string)
  2477. (defcustom org-use-property-inheritance nil
  2478. "Non-nil means properties apply also for sublevels.
  2479. This setting is chiefly used during property searches. Turning it on can
  2480. cause significant overhead when doing a search, which is why it is not
  2481. on by default.
  2482. When nil, only the properties directly given in the current entry count.
  2483. When t, every property is inherited. The value may also be a list of
  2484. properties that should have inheritance, or a regular expression matching
  2485. properties that should be inherited.
  2486. However, note that some special properties use inheritance under special
  2487. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2488. and the properties ending in \"_ALL\" when they are used as descriptor
  2489. for valid values of a property.
  2490. Note for programmers:
  2491. When querying an entry with `org-entry-get', you can control if inheritance
  2492. should be used. By default, `org-entry-get' looks only at the local
  2493. properties. You can request inheritance by setting the inherit argument
  2494. to t (to force inheritance) or to `selective' (to respect the setting
  2495. in this variable)."
  2496. :group 'org-properties
  2497. :type '(choice
  2498. (const :tag "Not" nil)
  2499. (const :tag "Always" t)
  2500. (repeat :tag "Specific properties" (string :tag "Property"))
  2501. (regexp :tag "Properties matched by regexp")))
  2502. (defun org-property-inherit-p (property)
  2503. "Check if PROPERTY is one that should be inherited."
  2504. (cond
  2505. ((eq org-use-property-inheritance t) t)
  2506. ((not org-use-property-inheritance) nil)
  2507. ((stringp org-use-property-inheritance)
  2508. (string-match org-use-property-inheritance property))
  2509. ((listp org-use-property-inheritance)
  2510. (member property org-use-property-inheritance))
  2511. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2512. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2513. "The default column format, if no other format has been defined.
  2514. This variable can be set on the per-file basis by inserting a line
  2515. #+COLUMNS: %25ITEM ....."
  2516. :group 'org-properties
  2517. :type 'string)
  2518. (defcustom org-columns-ellipses ".."
  2519. "The ellipses to be used when a field in column view is truncated.
  2520. When this is the empty string, as many characters as possible are shown,
  2521. but then there will be no visual indication that the field has been truncated.
  2522. When this is a string of length N, the last N characters of a truncated
  2523. field are replaced by this string. If the column is narrower than the
  2524. ellipses string, only part of the ellipses string will be shown."
  2525. :group 'org-properties
  2526. :type 'string)
  2527. (defcustom org-columns-modify-value-for-display-function nil
  2528. "Function that modifies values for display in column view.
  2529. For example, it can be used to cut out a certain part from a time stamp.
  2530. The function must take 2 arguments:
  2531. column-title The title of the column (*not* the property name)
  2532. value The value that should be modified.
  2533. The function should return the value that should be displayed,
  2534. or nil if the normal value should be used."
  2535. :group 'org-properties
  2536. :type 'function)
  2537. (defcustom org-effort-property "Effort"
  2538. "The property that is being used to keep track of effort estimates.
  2539. Effort estimates given in this property need to have the format H:MM."
  2540. :group 'org-properties
  2541. :group 'org-progress
  2542. :type '(string :tag "Property"))
  2543. (defconst org-global-properties-fixed
  2544. '(("VISIBILITY_ALL" . "folded children content all")
  2545. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2546. "List of property/value pairs that can be inherited by any entry.
  2547. These are fixed values, for the preset properties. The user variable
  2548. that can be used to add to this list is `org-global-properties'.
  2549. The entries in this list are cons cells where the car is a property
  2550. name and cdr is a string with the value. If the value represents
  2551. multiple items like an \"_ALL\" property, separate the items by
  2552. spaces.")
  2553. (defcustom org-global-properties nil
  2554. "List of property/value pairs that can be inherited by any entry.
  2555. This list will be combined with the constant `org-global-properties-fixed'.
  2556. The entries in this list are cons cells where the car is a property
  2557. name and cdr is a string with the value.
  2558. You can set buffer-local values for the same purpose in the variable
  2559. `org-file-properties' this by adding lines like
  2560. #+PROPERTY: NAME VALUE"
  2561. :group 'org-properties
  2562. :type '(repeat
  2563. (cons (string :tag "Property")
  2564. (string :tag "Value"))))
  2565. (defvar org-file-properties nil
  2566. "List of property/value pairs that can be inherited by any entry.
  2567. Valid for the current buffer.
  2568. This variable is populated from #+PROPERTY lines.")
  2569. (make-variable-buffer-local 'org-file-properties)
  2570. (defgroup org-agenda nil
  2571. "Options concerning agenda views in Org-mode."
  2572. :tag "Org Agenda"
  2573. :group 'org)
  2574. (defvar org-category nil
  2575. "Variable used by org files to set a category for agenda display.
  2576. Such files should use a file variable to set it, for example
  2577. # -*- mode: org; org-category: \"ELisp\"
  2578. or contain a special line
  2579. #+CATEGORY: ELisp
  2580. If the file does not specify a category, then file's base name
  2581. is used instead.")
  2582. (make-variable-buffer-local 'org-category)
  2583. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2584. (defcustom org-agenda-files nil
  2585. "The files to be used for agenda display.
  2586. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2587. \\[org-remove-file]. You can also use customize to edit the list.
  2588. If an entry is a directory, all files in that directory that are matched by
  2589. `org-agenda-file-regexp' will be part of the file list.
  2590. If the value of the variable is not a list but a single file name, then
  2591. the list of agenda files is actually stored and maintained in that file, one
  2592. agenda file per line. In this file paths can be given relative to
  2593. `org-directory'. Tilde expansion and environment variable substitution
  2594. are also made."
  2595. :group 'org-agenda
  2596. :type '(choice
  2597. (repeat :tag "List of files and directories" file)
  2598. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2599. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2600. "Regular expression to match files for `org-agenda-files'.
  2601. If any element in the list in that variable contains a directory instead
  2602. of a normal file, all files in that directory that are matched by this
  2603. regular expression will be included."
  2604. :group 'org-agenda
  2605. :type 'regexp)
  2606. (defcustom org-agenda-text-search-extra-files nil
  2607. "List of extra files to be searched by text search commands.
  2608. These files will be search in addition to the agenda files by the
  2609. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2610. Note that these files will only be searched for text search commands,
  2611. not for the other agenda views like todo lists, tag searches or the weekly
  2612. agenda. This variable is intended to list notes and possibly archive files
  2613. that should also be searched by these two commands.
  2614. In fact, if the first element in the list is the symbol `agenda-archives',
  2615. than all archive files of all agenda files will be added to the search
  2616. scope."
  2617. :group 'org-agenda
  2618. :type '(set :greedy t
  2619. (const :tag "Agenda Archives" agenda-archives)
  2620. (repeat :inline t (file))))
  2621. (if (fboundp 'defvaralias)
  2622. (defvaralias 'org-agenda-multi-occur-extra-files
  2623. 'org-agenda-text-search-extra-files))
  2624. (defcustom org-agenda-skip-unavailable-files nil
  2625. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2626. A nil value means to remove them, after a query, from the list."
  2627. :group 'org-agenda
  2628. :type 'boolean)
  2629. (defcustom org-calendar-to-agenda-key [?c]
  2630. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2631. The command `org-calendar-goto-agenda' will be bound to this key. The
  2632. default is the character `c' because then `c' can be used to switch back and
  2633. forth between agenda and calendar."
  2634. :group 'org-agenda
  2635. :type 'sexp)
  2636. (defcustom org-calendar-agenda-action-key [?k]
  2637. "The key to be installed in `calendar-mode-map' for agenda-action.
  2638. The command `org-agenda-action' will be bound to this key. The
  2639. default is the character `k' because we use the same key in the agenda."
  2640. :group 'org-agenda
  2641. :type 'sexp)
  2642. (defcustom org-calendar-insert-diary-entry-key [?i]
  2643. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2644. This option is irrelevant until `org-agenda-diary-file' has been configured
  2645. to point to an Org-mode file. When that is the case, the command
  2646. `org-agenda-diary-entry' will be bound to the key given here, by default
  2647. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2648. if you want to continue doing this, you need to change this to a different
  2649. key."
  2650. :group 'org-agenda
  2651. :type 'sexp)
  2652. (defcustom org-agenda-diary-file 'diary-file
  2653. "File to which to add new entries with the `i' key in agenda and calendar.
  2654. When this is the symbol `diary-file', the functionality in the Emacs
  2655. calendar will be used to add entries to the `diary-file'. But when this
  2656. points to a file, `org-agenda-diary-entry' will be used instead."
  2657. :group 'org-agenda
  2658. :type '(choice
  2659. (const :tag "The standard Emacs diary file" diary-file)
  2660. (file :tag "Special Org file diary entries")))
  2661. (eval-after-load "calendar"
  2662. '(progn
  2663. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2664. 'org-calendar-goto-agenda)
  2665. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2666. 'org-agenda-action)
  2667. (add-hook 'calendar-mode-hook
  2668. (lambda ()
  2669. (unless (eq org-agenda-diary-file 'diary-file)
  2670. (define-key calendar-mode-map
  2671. org-calendar-insert-diary-entry-key
  2672. 'org-agenda-diary-entry))))))
  2673. (defgroup org-latex nil
  2674. "Options for embedding LaTeX code into Org-mode."
  2675. :tag "Org LaTeX"
  2676. :group 'org)
  2677. (defcustom org-format-latex-options
  2678. '(:foreground default :background default :scale 1.0
  2679. :html-foreground "Black" :html-background "Transparent"
  2680. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2681. "Options for creating images from LaTeX fragments.
  2682. This is a property list with the following properties:
  2683. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2684. `default' means use the foreground of the default face.
  2685. :background the background color, or \"Transparent\".
  2686. `default' means use the background of the default face.
  2687. :scale a scaling factor for the size of the images, to get more pixels
  2688. :html-foreground, :html-background, :html-scale
  2689. the same numbers for HTML export.
  2690. :matchers a list indicating which matchers should be used to
  2691. find LaTeX fragments. Valid members of this list are:
  2692. \"begin\" find environments
  2693. \"$1\" find single characters surrounded by $.$
  2694. \"$\" find math expressions surrounded by $...$
  2695. \"$$\" find math expressions surrounded by $$....$$
  2696. \"\\(\" find math expressions surrounded by \\(...\\)
  2697. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2698. :group 'org-latex
  2699. :type 'plist)
  2700. (defcustom org-format-latex-signal-error t
  2701. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2702. When nil, just push out a message."
  2703. :group 'org-latex
  2704. :type 'boolean)
  2705. (defcustom org-format-latex-header "\\documentclass{article}
  2706. \\usepackage[usenames]{color}
  2707. \\usepackage{amsmath}
  2708. \\usepackage[mathscr]{eucal}
  2709. \\pagestyle{empty} % do not remove
  2710. \[PACKAGES]
  2711. \[DEFAULT-PACKAGES]
  2712. % The settings below are copied from fullpage.sty
  2713. \\setlength{\\textwidth}{\\paperwidth}
  2714. \\addtolength{\\textwidth}{-3cm}
  2715. \\setlength{\\oddsidemargin}{1.5cm}
  2716. \\addtolength{\\oddsidemargin}{-2.54cm}
  2717. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2718. \\setlength{\\textheight}{\\paperheight}
  2719. \\addtolength{\\textheight}{-\\headheight}
  2720. \\addtolength{\\textheight}{-\\headsep}
  2721. \\addtolength{\\textheight}{-\\footskip}
  2722. \\addtolength{\\textheight}{-3cm}
  2723. \\setlength{\\topmargin}{1.5cm}
  2724. \\addtolength{\\topmargin}{-2.54cm}"
  2725. "The document header used for processing LaTeX fragments.
  2726. It is imperative that this header make sure that no page number
  2727. appears on the page. The package defined in the variables
  2728. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2729. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2730. will be appended."
  2731. :group 'org-latex
  2732. :type 'string)
  2733. (defvar org-format-latex-header-extra nil)
  2734. (defun org-set-packages-alist (var val)
  2735. "Set the packages alist and make sure it has 3 elements per entry."
  2736. (set var (mapcar (lambda (x)
  2737. (if (and (consp x) (= (length x) 2))
  2738. (list (car x) (nth 1 x) t)
  2739. x))
  2740. val)))
  2741. (defun org-get-packages-alist (var)
  2742. "Get the packages alist and make sure it has 3 elements per entry."
  2743. (mapcar (lambda (x)
  2744. (if (and (consp x) (= (length x) 2))
  2745. (list (car x) (nth 1 x) t)
  2746. x))
  2747. (default-value var)))
  2748. ;; The following variables are defined here because is it also used
  2749. ;; when formatting latex fragments. Originally it was part of the
  2750. ;; LaTeX exporter, which is why the name includes "export".
  2751. (defcustom org-export-latex-default-packages-alist
  2752. '(("AUTO" "inputenc" t)
  2753. ("T1" "fontenc" t)
  2754. ("" "fixltx2e" nil)
  2755. ("" "graphicx" t)
  2756. ("" "longtable" nil)
  2757. ("" "float" nil)
  2758. ("" "wrapfig" nil)
  2759. ("" "soul" t)
  2760. ("" "textcomp" t)
  2761. ("" "marvosym" t)
  2762. ("" "wasysym" t)
  2763. ("" "latexsym" t)
  2764. ("" "amssymb" t)
  2765. ("" "hyperref" nil)
  2766. "\\tolerance=1000"
  2767. )
  2768. "Alist of default packages to be inserted in the header.
  2769. Change this only if one of the packages here causes an incompatibility
  2770. with another package you are using.
  2771. The packages in this list are needed by one part or another of Org-mode
  2772. to function properly.
  2773. - inputenc, fontenc: for basic font and character selection
  2774. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2775. for interpreting the entities in `org-entities'. You can skip some of these
  2776. packages if you don't use any of the symbols in it.
  2777. - graphicx: for including images
  2778. - float, wrapfig: for figure placement
  2779. - longtable: for long tables
  2780. - hyperref: for cross references
  2781. Therefore you should not modify this variable unless you know what you
  2782. are doing. The one reason to change it anyway is that you might be loading
  2783. some other package that conflicts with one of the default packages.
  2784. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2785. If SNIPPET-FLAG is t, the package also needs to be included when
  2786. compiling LaTeX snippets into images for inclusion into HTML."
  2787. :group 'org-export-latex
  2788. :set 'org-set-packages-alist
  2789. :get 'org-get-packages-alist
  2790. :type '(repeat
  2791. (choice
  2792. (list :tag "options/package pair"
  2793. (string :tag "options")
  2794. (string :tag "package")
  2795. (boolean :tag "Snippet"))
  2796. (string :tag "A line of LaTeX"))))
  2797. (defcustom org-export-latex-packages-alist nil
  2798. "Alist of packages to be inserted in every LaTeX header.
  2799. These will be inserted after `org-export-latex-default-packages-alist'.
  2800. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2801. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2802. turning LaTeX snippets into images for inclusion into HTML.
  2803. Make sure that you only list packages here which:
  2804. - you want in every file
  2805. - do not conflict with the default packages in
  2806. `org-export-latex-default-packages-alist'
  2807. - do not conflict with the setup in `org-format-latex-header'."
  2808. :group 'org-export-latex
  2809. :set 'org-set-packages-alist
  2810. :get 'org-get-packages-alist
  2811. :type '(repeat
  2812. (choice
  2813. (list :tag "options/package pair"
  2814. (string :tag "options")
  2815. (string :tag "package")
  2816. (boolean :tag "Snippet"))
  2817. (string :tag "A line of LaTeX"))))
  2818. (defgroup org-appearance nil
  2819. "Settings for Org-mode appearance."
  2820. :tag "Org Appearance"
  2821. :group 'org)
  2822. (defcustom org-level-color-stars-only nil
  2823. "Non-nil means fontify only the stars in each headline.
  2824. When nil, the entire headline is fontified.
  2825. Changing it requires restart of `font-lock-mode' to become effective
  2826. also in regions already fontified."
  2827. :group 'org-appearance
  2828. :type 'boolean)
  2829. (defcustom org-hide-leading-stars nil
  2830. "Non-nil means hide the first N-1 stars in a headline.
  2831. This works by using the face `org-hide' for these stars. This
  2832. face is white for a light background, and black for a dark
  2833. background. You may have to customize the face `org-hide' to
  2834. make this work.
  2835. Changing it requires restart of `font-lock-mode' to become effective
  2836. also in regions already fontified.
  2837. You may also set this on a per-file basis by adding one of the following
  2838. lines to the buffer:
  2839. #+STARTUP: hidestars
  2840. #+STARTUP: showstars"
  2841. :group 'org-appearance
  2842. :type 'boolean)
  2843. (defcustom org-hidden-keywords nil
  2844. "List of keywords that should be hidden when typed in the org buffer.
  2845. For example, add #+TITLE to this list in order to make the
  2846. document title appear in the buffer without the initial #+TITLE:
  2847. keyword."
  2848. :group 'org-appearance
  2849. :type '(set (const :tag "#+AUTHOR" author)
  2850. (const :tag "#+DATE" date)
  2851. (const :tag "#+EMAIL" email)
  2852. (const :tag "#+TITLE" title)))
  2853. (defcustom org-fontify-done-headline nil
  2854. "Non-nil means change the face of a headline if it is marked DONE.
  2855. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2856. When this is non-nil, the headline after the keyword is set to the
  2857. `org-headline-done' as an additional indication."
  2858. :group 'org-appearance
  2859. :type 'boolean)
  2860. (defcustom org-fontify-emphasized-text t
  2861. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2862. Changing this variable requires a restart of Emacs to take effect."
  2863. :group 'org-appearance
  2864. :type 'boolean)
  2865. (defcustom org-fontify-whole-heading-line nil
  2866. "Non-nil means fontify the whole line for headings.
  2867. This is useful when setting a background color for the
  2868. org-level-* faces."
  2869. :group 'org-appearance
  2870. :type 'boolean)
  2871. (defcustom org-highlight-latex-fragments-and-specials nil
  2872. "Non-nil means fontify what is treated specially by the exporters."
  2873. :group 'org-appearance
  2874. :type 'boolean)
  2875. (defcustom org-hide-emphasis-markers nil
  2876. "Non-nil mean font-lock should hide the emphasis marker characters."
  2877. :group 'org-appearance
  2878. :type 'boolean)
  2879. (defcustom org-pretty-entities nil
  2880. "Non-nil means show entities as UTF8 characters.
  2881. When nil, the \\name form remains in the buffer."
  2882. :group 'org-appearance
  2883. :type 'boolean)
  2884. (defcustom org-pretty-entities-include-sub-superscripts t
  2885. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2886. :group 'org-appearance
  2887. :type 'boolean)
  2888. (defvar org-emph-re nil
  2889. "Regular expression for matching emphasis.
  2890. After a match, the match groups contain these elements:
  2891. 0 The match of the full regular expression, including the characters
  2892. before and after the proper match
  2893. 1 The character before the proper match, or empty at beginning of line
  2894. 2 The proper match, including the leading and trailing markers
  2895. 3 The leading marker like * or /, indicating the type of highlighting
  2896. 4 The text between the emphasis markers, not including the markers
  2897. 5 The character after the match, empty at the end of a line")
  2898. (defvar org-verbatim-re nil
  2899. "Regular expression for matching verbatim text.")
  2900. (defvar org-emphasis-regexp-components) ; defined just below
  2901. (defvar org-emphasis-alist) ; defined just below
  2902. (defun org-set-emph-re (var val)
  2903. "Set variable and compute the emphasis regular expression."
  2904. (set var val)
  2905. (when (and (boundp 'org-emphasis-alist)
  2906. (boundp 'org-emphasis-regexp-components)
  2907. org-emphasis-alist org-emphasis-regexp-components)
  2908. (let* ((e org-emphasis-regexp-components)
  2909. (pre (car e))
  2910. (post (nth 1 e))
  2911. (border (nth 2 e))
  2912. (body (nth 3 e))
  2913. (nl (nth 4 e))
  2914. (body1 (concat body "*?"))
  2915. (markers (mapconcat 'car org-emphasis-alist ""))
  2916. (vmarkers (mapconcat
  2917. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2918. org-emphasis-alist "")))
  2919. ;; make sure special characters appear at the right position in the class
  2920. (if (string-match "\\^" markers)
  2921. (setq markers (concat (replace-match "" t t markers) "^")))
  2922. (if (string-match "-" markers)
  2923. (setq markers (concat (replace-match "" t t markers) "-")))
  2924. (if (string-match "\\^" vmarkers)
  2925. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2926. (if (string-match "-" vmarkers)
  2927. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2928. (if (> nl 0)
  2929. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2930. (int-to-string nl) "\\}")))
  2931. ;; Make the regexp
  2932. (setq org-emph-re
  2933. (concat "\\([" pre "]\\|^\\)"
  2934. "\\("
  2935. "\\([" markers "]\\)"
  2936. "\\("
  2937. "[^" border "]\\|"
  2938. "[^" border "]"
  2939. body1
  2940. "[^" border "]"
  2941. "\\)"
  2942. "\\3\\)"
  2943. "\\([" post "]\\|$\\)"))
  2944. (setq org-verbatim-re
  2945. (concat "\\([" pre "]\\|^\\)"
  2946. "\\("
  2947. "\\([" vmarkers "]\\)"
  2948. "\\("
  2949. "[^" border "]\\|"
  2950. "[^" border "]"
  2951. body1
  2952. "[^" border "]"
  2953. "\\)"
  2954. "\\3\\)"
  2955. "\\([" post "]\\|$\\)")))))
  2956. (defcustom org-emphasis-regexp-components
  2957. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2958. "Components used to build the regular expression for emphasis.
  2959. This is a list with 6 entries. Terminology: In an emphasis string
  2960. like \" *strong word* \", we call the initial space PREMATCH, the final
  2961. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2962. and \"trong wor\" is the body. The different components in this variable
  2963. specify what is allowed/forbidden in each part:
  2964. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2965. post Chars allowed as postmatch. End of line will be allowed too.
  2966. border The chars *forbidden* as border characters.
  2967. body-regexp A regexp like \".\" to match a body character. Don't use
  2968. non-shy groups here, and don't allow newline here.
  2969. newline The maximum number of newlines allowed in an emphasis exp.
  2970. Use customize to modify this, or restart Emacs after changing it."
  2971. :group 'org-appearance
  2972. :set 'org-set-emph-re
  2973. :type '(list
  2974. (sexp :tag "Allowed chars in pre ")
  2975. (sexp :tag "Allowed chars in post ")
  2976. (sexp :tag "Forbidden chars in border ")
  2977. (sexp :tag "Regexp for body ")
  2978. (integer :tag "number of newlines allowed")
  2979. (option (boolean :tag "Please ignore this button"))))
  2980. (defcustom org-emphasis-alist
  2981. `(("*" bold "<b>" "</b>")
  2982. ("/" italic "<i>" "</i>")
  2983. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2984. ("=" org-code "<code>" "</code>" verbatim)
  2985. ("~" org-verbatim "<code>" "</code>" verbatim)
  2986. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2987. "<del>" "</del>")
  2988. )
  2989. "Special syntax for emphasized text.
  2990. Text starting and ending with a special character will be emphasized, for
  2991. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2992. characters, the face to be used by font-lock for highlighting in Org-mode
  2993. Emacs buffers, and the HTML tags to be used for this.
  2994. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2995. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  2996. Use customize to modify this, or restart Emacs after changing it."
  2997. :group 'org-appearance
  2998. :set 'org-set-emph-re
  2999. :type '(repeat
  3000. (list
  3001. (string :tag "Marker character")
  3002. (choice
  3003. (face :tag "Font-lock-face")
  3004. (plist :tag "Face property list"))
  3005. (string :tag "HTML start tag")
  3006. (string :tag "HTML end tag")
  3007. (option (const verbatim)))))
  3008. (defvar org-protecting-blocks
  3009. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3010. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3011. This is needed for font-lock setup.")
  3012. ;;; Miscellaneous options
  3013. (defgroup org-completion nil
  3014. "Completion in Org-mode."
  3015. :tag "Org Completion"
  3016. :group 'org)
  3017. (defcustom org-completion-use-ido nil
  3018. "Non-nil means use ido completion wherever possible.
  3019. Note that `ido-mode' must be active for this variable to be relevant.
  3020. If you decide to turn this variable on, you might well want to turn off
  3021. `org-outline-path-complete-in-steps'.
  3022. See also `org-completion-use-iswitchb'."
  3023. :group 'org-completion
  3024. :type 'boolean)
  3025. (defcustom org-completion-use-iswitchb nil
  3026. "Non-nil means use iswitchb completion wherever possible.
  3027. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3028. If you decide to turn this variable on, you might well want to turn off
  3029. `org-outline-path-complete-in-steps'.
  3030. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3031. :group 'org-completion
  3032. :type 'boolean)
  3033. (defcustom org-completion-fallback-command 'hippie-expand
  3034. "The expansion command called by \\[org-complete] in normal context.
  3035. Normal means no org-mode-specific context."
  3036. :group 'org-completion
  3037. :type 'function)
  3038. ;;; Functions and variables from their packages
  3039. ;; Declared here to avoid compiler warnings
  3040. ;; XEmacs only
  3041. (defvar outline-mode-menu-heading)
  3042. (defvar outline-mode-menu-show)
  3043. (defvar outline-mode-menu-hide)
  3044. (defvar zmacs-regions) ; XEmacs regions
  3045. ;; Emacs only
  3046. (defvar mark-active)
  3047. ;; Various packages
  3048. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3049. (declare-function calendar-forward-day "cal-move" (arg))
  3050. (declare-function calendar-goto-date "cal-move" (date))
  3051. (declare-function calendar-goto-today "cal-move" ())
  3052. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3053. (defvar calc-embedded-close-formula)
  3054. (defvar calc-embedded-open-formula)
  3055. (declare-function cdlatex-tab "ext:cdlatex" ())
  3056. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3057. (defvar font-lock-unfontify-region-function)
  3058. (declare-function iswitchb-read-buffer "iswitchb"
  3059. (prompt &optional default require-match start matches-set))
  3060. (defvar iswitchb-temp-buflist)
  3061. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3062. (defvar org-agenda-tags-todo-honor-ignore-options)
  3063. (declare-function org-agenda-skip "org-agenda" ())
  3064. (declare-function
  3065. org-format-agenda-item "org-agenda"
  3066. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3067. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3068. (declare-function org-agenda-change-all-lines "org-agenda"
  3069. (newhead hdmarker &optional fixface just-this))
  3070. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3071. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3072. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3073. (beg end))
  3074. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3075. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3076. "org-agenda" (&optional end))
  3077. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3078. (declare-function org-indent-mode "org-indent" (&optional arg))
  3079. (declare-function parse-time-string "parse-time" (string))
  3080. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3081. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3082. (defvar remember-data-file)
  3083. (defvar texmathp-why)
  3084. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3085. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3086. (defvar w3m-current-url)
  3087. (defvar w3m-current-title)
  3088. (defvar org-latex-regexps)
  3089. ;;; Autoload and prepare some org modules
  3090. ;; Some table stuff that needs to be defined here, because it is used
  3091. ;; by the functions setting up org-mode or checking for table context.
  3092. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3093. "Detect an org-type or table-type table.")
  3094. (defconst org-table-line-regexp "^[ \t]*|"
  3095. "Detect an org-type table line.")
  3096. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3097. "Detect an org-type table line.")
  3098. (defconst org-table-hline-regexp "^[ \t]*|-"
  3099. "Detect an org-type table hline.")
  3100. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3101. "Detect a table-type table hline.")
  3102. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3103. "Detect the first line outside a table when searching from within it.
  3104. This works for both table types.")
  3105. ;; Autoload the functions in org-table.el that are needed by functions here.
  3106. (eval-and-compile
  3107. (org-autoload "org-table"
  3108. '(org-table-align org-table-begin org-table-blank-field
  3109. org-table-convert org-table-convert-region org-table-copy-down
  3110. org-table-copy-region org-table-create
  3111. org-table-create-or-convert-from-region
  3112. org-table-create-with-table.el org-table-current-dline
  3113. org-table-cut-region org-table-delete-column org-table-edit-field
  3114. org-table-edit-formulas org-table-end org-table-eval-formula
  3115. org-table-export org-table-field-info
  3116. org-table-get-stored-formulas org-table-goto-column
  3117. org-table-hline-and-move org-table-import org-table-insert-column
  3118. org-table-insert-hline org-table-insert-row org-table-iterate
  3119. org-table-justify-field-maybe org-table-kill-row
  3120. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3121. org-table-move-column org-table-move-column-left
  3122. org-table-move-column-right org-table-move-row
  3123. org-table-move-row-down org-table-move-row-up
  3124. org-table-next-field org-table-next-row org-table-paste-rectangle
  3125. org-table-previous-field org-table-recalculate
  3126. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3127. org-table-toggle-coordinate-overlays
  3128. org-table-toggle-formula-debugger org-table-wrap-region
  3129. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3130. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3131. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3132. (defun org-at-table-p (&optional table-type)
  3133. "Return t if the cursor is inside an org-type table.
  3134. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3135. (if org-enable-table-editor
  3136. (save-excursion
  3137. (beginning-of-line 1)
  3138. (looking-at (if table-type org-table-any-line-regexp
  3139. org-table-line-regexp)))
  3140. nil))
  3141. (defsubst org-table-p () (org-at-table-p))
  3142. (defun org-at-table.el-p ()
  3143. "Return t if and only if we are at a table.el table."
  3144. (and (org-at-table-p 'any)
  3145. (save-excursion
  3146. (goto-char (org-table-begin 'any))
  3147. (looking-at org-table1-hline-regexp))))
  3148. (defun org-table-recognize-table.el ()
  3149. "If there is a table.el table nearby, recognize it and move into it."
  3150. (if org-table-tab-recognizes-table.el
  3151. (if (org-at-table.el-p)
  3152. (progn
  3153. (beginning-of-line 1)
  3154. (if (looking-at org-table-dataline-regexp)
  3155. nil
  3156. (if (looking-at org-table1-hline-regexp)
  3157. (progn
  3158. (beginning-of-line 2)
  3159. (if (looking-at org-table-any-border-regexp)
  3160. (beginning-of-line -1)))))
  3161. (if (re-search-forward "|" (org-table-end t) t)
  3162. (progn
  3163. (require 'table)
  3164. (if (table--at-cell-p (point))
  3165. t
  3166. (message "recognizing table.el table...")
  3167. (table-recognize-table)
  3168. (message "recognizing table.el table...done")))
  3169. (error "This should not happen"))
  3170. t)
  3171. nil)
  3172. nil))
  3173. (defun org-at-table-hline-p ()
  3174. "Return t if the cursor is inside a hline in a table."
  3175. (if org-enable-table-editor
  3176. (save-excursion
  3177. (beginning-of-line 1)
  3178. (looking-at org-table-hline-regexp))
  3179. nil))
  3180. (defvar org-table-clean-did-remove-column nil)
  3181. (defun org-table-map-tables (function &optional quietly)
  3182. "Apply FUNCTION to the start of all tables in the buffer."
  3183. (save-excursion
  3184. (save-restriction
  3185. (widen)
  3186. (goto-char (point-min))
  3187. (while (re-search-forward org-table-any-line-regexp nil t)
  3188. (unless quietly
  3189. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3190. (beginning-of-line 1)
  3191. (when (looking-at org-table-line-regexp)
  3192. (save-excursion (funcall function))
  3193. (or (looking-at org-table-line-regexp)
  3194. (forward-char 1)))
  3195. (re-search-forward org-table-any-border-regexp nil 1))))
  3196. (unless quietly (message "Mapping tables: done")))
  3197. ;; Declare and autoload functions from org-exp.el & Co
  3198. (declare-function org-default-export-plist "org-exp")
  3199. (declare-function org-infile-export-plist "org-exp")
  3200. (declare-function org-get-current-options "org-exp")
  3201. (eval-and-compile
  3202. (org-autoload "org-exp"
  3203. '(org-export org-export-visible
  3204. org-insert-export-options-template
  3205. org-table-clean-before-export))
  3206. (org-autoload "org-ascii"
  3207. '(org-export-as-ascii org-export-ascii-preprocess
  3208. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3209. org-export-region-as-ascii))
  3210. (org-autoload "org-latex"
  3211. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3212. org-replace-region-by-latex org-export-region-as-latex
  3213. org-export-as-latex org-export-as-pdf
  3214. org-export-as-pdf-and-open))
  3215. (org-autoload "org-html"
  3216. '(org-export-as-html-and-open
  3217. org-export-as-html-batch org-export-as-html-to-buffer
  3218. org-replace-region-by-html org-export-region-as-html
  3219. org-export-as-html))
  3220. (org-autoload "org-docbook"
  3221. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3222. org-replace-region-by-docbook org-export-region-as-docbook
  3223. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3224. org-export-as-docbook))
  3225. (org-autoload "org-icalendar"
  3226. '(org-export-icalendar-this-file
  3227. org-export-icalendar-all-agenda-files
  3228. org-export-icalendar-combine-agenda-files))
  3229. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3230. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3231. ;; Declare and autoload functions from org-agenda.el
  3232. (eval-and-compile
  3233. (org-autoload "org-agenda"
  3234. '(org-agenda org-agenda-list org-search-view
  3235. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3236. org-diary org-agenda-to-appt
  3237. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3238. ;; Autoload org-remember
  3239. (eval-and-compile
  3240. (org-autoload "org-remember"
  3241. '(org-remember-insinuate org-remember-annotation
  3242. org-remember-apply-template org-remember org-remember-handler)))
  3243. (eval-and-compile
  3244. (org-autoload "org-capture"
  3245. '(org-capture org-capture-insert-template-here
  3246. org-capture-import-remember-templates)))
  3247. ;; Autoload org-clock.el
  3248. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3249. (beg end))
  3250. (declare-function org-clock-update-mode-line "org-clock" ())
  3251. (declare-function org-resolve-clocks "org-clock"
  3252. (&optional also-non-dangling-p prompt last-valid))
  3253. (defvar org-clock-start-time)
  3254. (defvar org-clock-marker (make-marker)
  3255. "Marker recording the last clock-in.")
  3256. (defvar org-clock-hd-marker (make-marker)
  3257. "Marker recording the last clock-in, but the headline position.")
  3258. (defvar org-clock-heading ""
  3259. "The heading of the current clock entry.")
  3260. (defun org-clock-is-active ()
  3261. "Return non-nil if clock is currently running.
  3262. The return value is actually the clock marker."
  3263. (marker-buffer org-clock-marker))
  3264. (eval-and-compile
  3265. (org-autoload
  3266. "org-clock"
  3267. '(org-clock-in org-clock-out org-clock-cancel
  3268. org-clock-goto org-clock-sum org-clock-display
  3269. org-clock-remove-overlays org-clock-report
  3270. org-clocktable-shift org-dblock-write:clocktable
  3271. org-get-clocktable org-resolve-clocks)))
  3272. (defun org-clock-update-time-maybe ()
  3273. "If this is a CLOCK line, update it and return t.
  3274. Otherwise, return nil."
  3275. (interactive)
  3276. (save-excursion
  3277. (beginning-of-line 1)
  3278. (skip-chars-forward " \t")
  3279. (when (looking-at org-clock-string)
  3280. (let ((re (concat "[ \t]*" org-clock-string
  3281. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3282. "\\([ \t]*=>.*\\)?\\)?"))
  3283. ts te h m s neg)
  3284. (cond
  3285. ((not (looking-at re))
  3286. nil)
  3287. ((not (match-end 2))
  3288. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3289. (> org-clock-marker (point))
  3290. (<= org-clock-marker (point-at-eol)))
  3291. ;; The clock is running here
  3292. (setq org-clock-start-time
  3293. (apply 'encode-time
  3294. (org-parse-time-string (match-string 1))))
  3295. (org-clock-update-mode-line)))
  3296. (t
  3297. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3298. (end-of-line 1)
  3299. (setq ts (match-string 1)
  3300. te (match-string 3))
  3301. (setq s (- (org-float-time
  3302. (apply 'encode-time (org-parse-time-string te)))
  3303. (org-float-time
  3304. (apply 'encode-time (org-parse-time-string ts))))
  3305. neg (< s 0)
  3306. s (abs s)
  3307. h (floor (/ s 3600))
  3308. s (- s (* 3600 h))
  3309. m (floor (/ s 60))
  3310. s (- s (* 60 s)))
  3311. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3312. t))))))
  3313. (defun org-check-running-clock ()
  3314. "Check if the current buffer contains the running clock.
  3315. If yes, offer to stop it and to save the buffer with the changes."
  3316. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3317. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3318. (buffer-name))))
  3319. (org-clock-out)
  3320. (when (y-or-n-p "Save changed buffer?")
  3321. (save-buffer))))
  3322. (defun org-clocktable-try-shift (dir n)
  3323. "Check if this line starts a clock table, if yes, shift the time block."
  3324. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3325. (org-clocktable-shift dir n)))
  3326. ;; Autoload org-timer.el
  3327. (eval-and-compile
  3328. (org-autoload
  3329. "org-timer"
  3330. '(org-timer-start org-timer org-timer-item
  3331. org-timer-change-times-in-region
  3332. org-timer-set-timer
  3333. org-timer-reset-timers
  3334. org-timer-show-remaining-time)))
  3335. ;; Autoload org-feed.el
  3336. (eval-and-compile
  3337. (org-autoload
  3338. "org-feed"
  3339. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3340. ;; Autoload org-indent.el
  3341. ;; Define the variable already here, to make sure we have it.
  3342. (defvar org-indent-mode nil
  3343. "Non-nil if Org-Indent mode is enabled.
  3344. Use the command `org-indent-mode' to change this variable.")
  3345. (eval-and-compile
  3346. (org-autoload
  3347. "org-indent"
  3348. '(org-indent-mode)))
  3349. ;; Autoload org-mobile.el
  3350. (eval-and-compile
  3351. (org-autoload
  3352. "org-mobile"
  3353. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3354. ;; Autoload archiving code
  3355. ;; The stuff that is needed for cycling and tags has to be defined here.
  3356. (defgroup org-archive nil
  3357. "Options concerning archiving in Org-mode."
  3358. :tag "Org Archive"
  3359. :group 'org-structure)
  3360. (defcustom org-archive-location "%s_archive::"
  3361. "The location where subtrees should be archived.
  3362. The value of this variable is a string, consisting of two parts,
  3363. separated by a double-colon. The first part is a filename and
  3364. the second part is a headline.
  3365. When the filename is omitted, archiving happens in the same file.
  3366. %s in the filename will be replaced by the current file
  3367. name (without the directory part). Archiving to a different file
  3368. is useful to keep archived entries from contributing to the
  3369. Org-mode Agenda.
  3370. The archived entries will be filed as subtrees of the specified
  3371. headline. When the headline is omitted, the subtrees are simply
  3372. filed away at the end of the file, as top-level entries. Also in
  3373. the heading you can use %s to represent the file name, this can be
  3374. useful when using the same archive for a number of different files.
  3375. Here are a few examples:
  3376. \"%s_archive::\"
  3377. If the current file is Projects.org, archive in file
  3378. Projects.org_archive, as top-level trees. This is the default.
  3379. \"::* Archived Tasks\"
  3380. Archive in the current file, under the top-level headline
  3381. \"* Archived Tasks\".
  3382. \"~/org/archive.org::\"
  3383. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3384. \"~/org/archive.org::From %s\"
  3385. Archive in file ~/org/archive.org (absolute path), under headlines
  3386. \"From FILENAME\" where file name is the current file name.
  3387. \"basement::** Finished Tasks\"
  3388. Archive in file ./basement (relative path), as level 3 trees
  3389. below the level 2 heading \"** Finished Tasks\".
  3390. You may set this option on a per-file basis by adding to the buffer a
  3391. line like
  3392. #+ARCHIVE: basement::** Finished Tasks
  3393. You may also define it locally for a subtree by setting an ARCHIVE property
  3394. in the entry. If such a property is found in an entry, or anywhere up
  3395. the hierarchy, it will be used."
  3396. :group 'org-archive
  3397. :type 'string)
  3398. (defcustom org-archive-tag "ARCHIVE"
  3399. "The tag that marks a subtree as archived.
  3400. An archived subtree does not open during visibility cycling, and does
  3401. not contribute to the agenda listings.
  3402. After changing this, font-lock must be restarted in the relevant buffers to
  3403. get the proper fontification."
  3404. :group 'org-archive
  3405. :group 'org-keywords
  3406. :type 'string)
  3407. (defcustom org-agenda-skip-archived-trees t
  3408. "Non-nil means the agenda will skip any items located in archived trees.
  3409. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3410. variable is no longer recommended, you should leave it at the value t.
  3411. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3412. :group 'org-archive
  3413. :group 'org-agenda-skip
  3414. :type 'boolean)
  3415. (defcustom org-columns-skip-archived-trees t
  3416. "Non-nil means ignore archived trees when creating column view."
  3417. :group 'org-archive
  3418. :group 'org-properties
  3419. :type 'boolean)
  3420. (defcustom org-cycle-open-archived-trees nil
  3421. "Non-nil means `org-cycle' will open archived trees.
  3422. An archived tree is a tree marked with the tag ARCHIVE.
  3423. When nil, archived trees will stay folded. You can still open them with
  3424. normal outline commands like `show-all', but not with the cycling commands."
  3425. :group 'org-archive
  3426. :group 'org-cycle
  3427. :type 'boolean)
  3428. (defcustom org-sparse-tree-open-archived-trees nil
  3429. "Non-nil means sparse tree construction shows matches in archived trees.
  3430. When nil, matches in these trees are highlighted, but the trees are kept in
  3431. collapsed state."
  3432. :group 'org-archive
  3433. :group 'org-sparse-trees
  3434. :type 'boolean)
  3435. (defun org-cycle-hide-archived-subtrees (state)
  3436. "Re-hide all archived subtrees after a visibility state change."
  3437. (when (and (not org-cycle-open-archived-trees)
  3438. (not (memq state '(overview folded))))
  3439. (save-excursion
  3440. (let* ((globalp (memq state '(contents all)))
  3441. (beg (if globalp (point-min) (point)))
  3442. (end (if globalp (point-max) (org-end-of-subtree t))))
  3443. (org-hide-archived-subtrees beg end)
  3444. (goto-char beg)
  3445. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3446. (message "%s" (substitute-command-keys
  3447. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3448. (defun org-force-cycle-archived ()
  3449. "Cycle subtree even if it is archived."
  3450. (interactive)
  3451. (setq this-command 'org-cycle)
  3452. (let ((org-cycle-open-archived-trees t))
  3453. (call-interactively 'org-cycle)))
  3454. (defun org-hide-archived-subtrees (beg end)
  3455. "Re-hide all archived subtrees after a visibility state change."
  3456. (save-excursion
  3457. (let* ((re (concat ":" org-archive-tag ":")))
  3458. (goto-char beg)
  3459. (while (re-search-forward re end t)
  3460. (when (org-on-heading-p)
  3461. (org-flag-subtree t)
  3462. (org-end-of-subtree t))))))
  3463. (defun org-flag-subtree (flag)
  3464. (save-excursion
  3465. (org-back-to-heading t)
  3466. (outline-end-of-heading)
  3467. (outline-flag-region (point)
  3468. (progn (org-end-of-subtree t) (point))
  3469. flag)))
  3470. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3471. (eval-and-compile
  3472. (org-autoload "org-archive"
  3473. '(org-add-archive-files org-archive-subtree
  3474. org-archive-to-archive-sibling org-toggle-archive-tag
  3475. org-archive-subtree-default
  3476. org-archive-subtree-default-with-confirmation)))
  3477. ;; Autoload Column View Code
  3478. (declare-function org-columns-number-to-string "org-colview")
  3479. (declare-function org-columns-get-format-and-top-level "org-colview")
  3480. (declare-function org-columns-compute "org-colview")
  3481. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3482. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3483. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3484. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3485. ;; Autoload ID code
  3486. (declare-function org-id-store-link "org-id")
  3487. (declare-function org-id-locations-load "org-id")
  3488. (declare-function org-id-locations-save "org-id")
  3489. (defvar org-id-track-globally)
  3490. (org-autoload "org-id"
  3491. '(org-id-get-create org-id-new org-id-copy org-id-get
  3492. org-id-get-with-outline-path-completion
  3493. org-id-get-with-outline-drilling org-id-store-link
  3494. org-id-goto org-id-find org-id-store-link))
  3495. ;; Autoload Plotting Code
  3496. (org-autoload "org-plot"
  3497. '(org-plot/gnuplot))
  3498. ;;; Variables for pre-computed regular expressions, all buffer local
  3499. (defvar org-drawer-regexp nil
  3500. "Matches first line of a hidden block.")
  3501. (make-variable-buffer-local 'org-drawer-regexp)
  3502. (defvar org-todo-regexp nil
  3503. "Matches any of the TODO state keywords.")
  3504. (make-variable-buffer-local 'org-todo-regexp)
  3505. (defvar org-not-done-regexp nil
  3506. "Matches any of the TODO state keywords except the last one.")
  3507. (make-variable-buffer-local 'org-not-done-regexp)
  3508. (defvar org-not-done-heading-regexp nil
  3509. "Matches a TODO headline that is not done.")
  3510. (make-variable-buffer-local 'org-not-done-regexp)
  3511. (defvar org-todo-line-regexp nil
  3512. "Matches a headline and puts TODO state into group 2 if present.")
  3513. (make-variable-buffer-local 'org-todo-line-regexp)
  3514. (defvar org-complex-heading-regexp nil
  3515. "Matches a headline and puts everything into groups:
  3516. group 1: the stars
  3517. group 2: The todo keyword, maybe
  3518. group 3: Priority cookie
  3519. group 4: True headline
  3520. group 5: Tags")
  3521. (make-variable-buffer-local 'org-complex-heading-regexp)
  3522. (defvar org-complex-heading-regexp-format nil
  3523. "Printf format to make regexp to match an exact headline.
  3524. This regexp will match the headline of any node which hase the exact
  3525. headline text that is put into the format, but may have any TODO state,
  3526. priority and tags.")
  3527. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3528. (defvar org-todo-line-tags-regexp nil
  3529. "Matches a headline and puts TODO state into group 2 if present.
  3530. Also put tags into group 4 if tags are present.")
  3531. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3532. (defvar org-nl-done-regexp nil
  3533. "Matches newline followed by a headline with the DONE keyword.")
  3534. (make-variable-buffer-local 'org-nl-done-regexp)
  3535. (defvar org-looking-at-done-regexp nil
  3536. "Matches the DONE keyword a point.")
  3537. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3538. (defvar org-ds-keyword-length 12
  3539. "Maximum length of the Deadline and SCHEDULED keywords.")
  3540. (make-variable-buffer-local 'org-ds-keyword-length)
  3541. (defvar org-deadline-regexp nil
  3542. "Matches the DEADLINE keyword.")
  3543. (make-variable-buffer-local 'org-deadline-regexp)
  3544. (defvar org-deadline-time-regexp nil
  3545. "Matches the DEADLINE keyword together with a time stamp.")
  3546. (make-variable-buffer-local 'org-deadline-time-regexp)
  3547. (defvar org-deadline-line-regexp nil
  3548. "Matches the DEADLINE keyword and the rest of the line.")
  3549. (make-variable-buffer-local 'org-deadline-line-regexp)
  3550. (defvar org-scheduled-regexp nil
  3551. "Matches the SCHEDULED keyword.")
  3552. (make-variable-buffer-local 'org-scheduled-regexp)
  3553. (defvar org-scheduled-time-regexp nil
  3554. "Matches the SCHEDULED keyword together with a time stamp.")
  3555. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3556. (defvar org-closed-time-regexp nil
  3557. "Matches the CLOSED keyword together with a time stamp.")
  3558. (make-variable-buffer-local 'org-closed-time-regexp)
  3559. (defvar org-keyword-time-regexp nil
  3560. "Matches any of the 4 keywords, together with the time stamp.")
  3561. (make-variable-buffer-local 'org-keyword-time-regexp)
  3562. (defvar org-keyword-time-not-clock-regexp nil
  3563. "Matches any of the 3 keywords, together with the time stamp.")
  3564. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3565. (defvar org-maybe-keyword-time-regexp nil
  3566. "Matches a timestamp, possibly preceded by a keyword.")
  3567. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3568. (defvar org-planning-or-clock-line-re nil
  3569. "Matches a line with planning or clock info.")
  3570. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3571. (defvar org-all-time-keywords nil
  3572. "List of time keywords.")
  3573. (make-variable-buffer-local 'org-all-time-keywords)
  3574. (defconst org-plain-time-of-day-regexp
  3575. (concat
  3576. "\\(\\<[012]?[0-9]"
  3577. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3578. "\\(--?"
  3579. "\\(\\<[012]?[0-9]"
  3580. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3581. "\\)?")
  3582. "Regular expression to match a plain time or time range.
  3583. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3584. groups carry important information:
  3585. 0 the full match
  3586. 1 the first time, range or not
  3587. 8 the second time, if it is a range.")
  3588. (defconst org-plain-time-extension-regexp
  3589. (concat
  3590. "\\(\\<[012]?[0-9]"
  3591. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3592. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3593. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3594. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3595. groups carry important information:
  3596. 0 the full match
  3597. 7 hours of duration
  3598. 9 minutes of duration")
  3599. (defconst org-stamp-time-of-day-regexp
  3600. (concat
  3601. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3602. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3603. "\\(--?"
  3604. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3605. "Regular expression to match a timestamp time or time range.
  3606. After a match, the following groups carry important information:
  3607. 0 the full match
  3608. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3609. 2 the first time, range or not
  3610. 4 the second time, if it is a range.")
  3611. (defconst org-startup-options
  3612. '(("fold" org-startup-folded t)
  3613. ("overview" org-startup-folded t)
  3614. ("nofold" org-startup-folded nil)
  3615. ("showall" org-startup-folded nil)
  3616. ("showeverything" org-startup-folded showeverything)
  3617. ("content" org-startup-folded content)
  3618. ("indent" org-startup-indented t)
  3619. ("noindent" org-startup-indented nil)
  3620. ("hidestars" org-hide-leading-stars t)
  3621. ("showstars" org-hide-leading-stars nil)
  3622. ("odd" org-odd-levels-only t)
  3623. ("oddeven" org-odd-levels-only nil)
  3624. ("align" org-startup-align-all-tables t)
  3625. ("noalign" org-startup-align-all-tables nil)
  3626. ("customtime" org-display-custom-times t)
  3627. ("logdone" org-log-done time)
  3628. ("lognotedone" org-log-done note)
  3629. ("nologdone" org-log-done nil)
  3630. ("lognoteclock-out" org-log-note-clock-out t)
  3631. ("nolognoteclock-out" org-log-note-clock-out nil)
  3632. ("logrepeat" org-log-repeat state)
  3633. ("lognoterepeat" org-log-repeat note)
  3634. ("nologrepeat" org-log-repeat nil)
  3635. ("logreschedule" org-log-reschedule time)
  3636. ("lognotereschedule" org-log-reschedule note)
  3637. ("nologreschedule" org-log-reschedule nil)
  3638. ("logredeadline" org-log-redeadline time)
  3639. ("lognoteredeadline" org-log-redeadline note)
  3640. ("nologredeadline" org-log-redeadline nil)
  3641. ("logrefile" org-log-refile time)
  3642. ("lognoterefile" org-log-refile note)
  3643. ("nologrefile" org-log-refile nil)
  3644. ("fninline" org-footnote-define-inline t)
  3645. ("nofninline" org-footnote-define-inline nil)
  3646. ("fnlocal" org-footnote-section nil)
  3647. ("fnauto" org-footnote-auto-label t)
  3648. ("fnprompt" org-footnote-auto-label nil)
  3649. ("fnconfirm" org-footnote-auto-label confirm)
  3650. ("fnplain" org-footnote-auto-label plain)
  3651. ("fnadjust" org-footnote-auto-adjust t)
  3652. ("nofnadjust" org-footnote-auto-adjust nil)
  3653. ("constcgs" constants-unit-system cgs)
  3654. ("constSI" constants-unit-system SI)
  3655. ("noptag" org-tag-persistent-alist nil)
  3656. ("hideblocks" org-hide-block-startup t)
  3657. ("nohideblocks" org-hide-block-startup nil)
  3658. ("beamer" org-startup-with-beamer-mode t)
  3659. ("entitiespretty" org-pretty-entities t)
  3660. ("entitiesplain" org-pretty-entities nil))
  3661. "Variable associated with STARTUP options for org-mode.
  3662. Each element is a list of three items: The startup options as written
  3663. in the #+STARTUP line, the corresponding variable, and the value to
  3664. set this variable to if the option is found. An optional forth element PUSH
  3665. means to push this value onto the list in the variable.")
  3666. (defun org-set-regexps-and-options ()
  3667. "Precompute regular expressions for current buffer."
  3668. (when (org-mode-p)
  3669. (org-set-local 'org-todo-kwd-alist nil)
  3670. (org-set-local 'org-todo-key-alist nil)
  3671. (org-set-local 'org-todo-key-trigger nil)
  3672. (org-set-local 'org-todo-keywords-1 nil)
  3673. (org-set-local 'org-done-keywords nil)
  3674. (org-set-local 'org-todo-heads nil)
  3675. (org-set-local 'org-todo-sets nil)
  3676. (org-set-local 'org-todo-log-states nil)
  3677. (org-set-local 'org-file-properties nil)
  3678. (org-set-local 'org-file-tags nil)
  3679. (let ((re (org-make-options-regexp
  3680. '("CATEGORY" "TODO" "COLUMNS"
  3681. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3682. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3683. "OPTIONS")
  3684. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3685. (splitre "[ \t]+")
  3686. (scripts org-use-sub-superscripts)
  3687. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3688. tail sep kws1 prio props ftags drawers beamer-p
  3689. ext-setup-or-nil setup-contents (start 0))
  3690. (save-excursion
  3691. (save-restriction
  3692. (widen)
  3693. (goto-char (point-min))
  3694. (while (or (and ext-setup-or-nil
  3695. (string-match re ext-setup-or-nil start)
  3696. (setq start (match-end 0)))
  3697. (and (setq ext-setup-or-nil nil start 0)
  3698. (re-search-forward re nil t)))
  3699. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3700. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3701. (if (stringp value) (setq value (org-trim value)))
  3702. (cond
  3703. ((equal key "CATEGORY")
  3704. (setq cat value))
  3705. ((member key '("SEQ_TODO" "TODO"))
  3706. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3707. ((equal key "TYP_TODO")
  3708. (push (cons 'type (org-split-string value splitre)) kwds))
  3709. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3710. ;; general TODO-like setup
  3711. (push (cons (intern (downcase (match-string 1 key)))
  3712. (org-split-string value splitre)) kwds))
  3713. ((equal key "TAGS")
  3714. (setq tags (append tags (if tags '("\\n") nil)
  3715. (org-split-string value splitre))))
  3716. ((equal key "COLUMNS")
  3717. (org-set-local 'org-columns-default-format value))
  3718. ((equal key "LINK")
  3719. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3720. (push (cons (match-string 1 value)
  3721. (org-trim (match-string 2 value)))
  3722. links)))
  3723. ((equal key "PRIORITIES")
  3724. (setq prio (org-split-string value " +")))
  3725. ((equal key "PROPERTY")
  3726. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3727. (push (cons (match-string 1 value) (match-string 2 value))
  3728. props)))
  3729. ((equal key "FILETAGS")
  3730. (when (string-match "\\S-" value)
  3731. (setq ftags
  3732. (append
  3733. ftags
  3734. (apply 'append
  3735. (mapcar (lambda (x) (org-split-string x ":"))
  3736. (org-split-string value)))))))
  3737. ((equal key "DRAWERS")
  3738. (setq drawers (org-split-string value splitre)))
  3739. ((equal key "CONSTANTS")
  3740. (setq const (append const (org-split-string value splitre))))
  3741. ((equal key "STARTUP")
  3742. (let ((opts (org-split-string value splitre))
  3743. l var val)
  3744. (while (setq l (pop opts))
  3745. (when (setq l (assoc l org-startup-options))
  3746. (setq var (nth 1 l) val (nth 2 l))
  3747. (if (not (nth 3 l))
  3748. (set (make-local-variable var) val)
  3749. (if (not (listp (symbol-value var)))
  3750. (set (make-local-variable var) nil))
  3751. (set (make-local-variable var) (symbol-value var))
  3752. (add-to-list var val))))))
  3753. ((equal key "ARCHIVE")
  3754. (setq arch value)
  3755. (remove-text-properties 0 (length arch)
  3756. '(face t fontified t) arch))
  3757. ((equal key "LATEX_CLASS")
  3758. (setq beamer-p (equal value "beamer")))
  3759. ((equal key "OPTIONS")
  3760. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3761. (setq scripts (read (match-string 2 value)))))
  3762. ((equal key "SETUPFILE")
  3763. (setq setup-contents (org-file-contents
  3764. (expand-file-name
  3765. (org-remove-double-quotes value))
  3766. 'noerror))
  3767. (if (not ext-setup-or-nil)
  3768. (setq ext-setup-or-nil setup-contents start 0)
  3769. (setq ext-setup-or-nil
  3770. (concat (substring ext-setup-or-nil 0 start)
  3771. "\n" setup-contents "\n"
  3772. (substring ext-setup-or-nil start)))))
  3773. ))))
  3774. (org-set-local 'org-use-sub-superscripts scripts)
  3775. (when cat
  3776. (org-set-local 'org-category (intern cat))
  3777. (push (cons "CATEGORY" cat) props))
  3778. (when prio
  3779. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3780. (setq prio (mapcar 'string-to-char prio))
  3781. (org-set-local 'org-highest-priority (nth 0 prio))
  3782. (org-set-local 'org-lowest-priority (nth 1 prio))
  3783. (org-set-local 'org-default-priority (nth 2 prio)))
  3784. (and props (org-set-local 'org-file-properties (nreverse props)))
  3785. (and ftags (org-set-local 'org-file-tags
  3786. (mapcar 'org-add-prop-inherited ftags)))
  3787. (and drawers (org-set-local 'org-drawers drawers))
  3788. (and arch (org-set-local 'org-archive-location arch))
  3789. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3790. ;; Process the TODO keywords
  3791. (unless kwds
  3792. ;; Use the global values as if they had been given locally.
  3793. (setq kwds (default-value 'org-todo-keywords))
  3794. (if (stringp (car kwds))
  3795. (setq kwds (list (cons org-todo-interpretation
  3796. (default-value 'org-todo-keywords)))))
  3797. (setq kwds (reverse kwds)))
  3798. (setq kwds (nreverse kwds))
  3799. (let (inter kws kw)
  3800. (while (setq kws (pop kwds))
  3801. (let ((kws (or
  3802. (run-hook-with-args-until-success
  3803. 'org-todo-setup-filter-hook kws)
  3804. kws)))
  3805. (setq inter (pop kws) sep (member "|" kws)
  3806. kws0 (delete "|" (copy-sequence kws))
  3807. kwsa nil
  3808. kws1 (mapcar
  3809. (lambda (x)
  3810. ;; 1 2
  3811. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3812. (progn
  3813. (setq kw (match-string 1 x)
  3814. key (and (match-end 2) (match-string 2 x))
  3815. log (org-extract-log-state-settings x))
  3816. (push (cons kw (and key (string-to-char key))) kwsa)
  3817. (and log (push log org-todo-log-states))
  3818. kw)
  3819. (error "Invalid TODO keyword %s" x)))
  3820. kws0)
  3821. kwsa (if kwsa (append '((:startgroup))
  3822. (nreverse kwsa)
  3823. '((:endgroup))))
  3824. hw (car kws1)
  3825. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3826. tail (list inter hw (car dws) (org-last dws))))
  3827. (add-to-list 'org-todo-heads hw 'append)
  3828. (push kws1 org-todo-sets)
  3829. (setq org-done-keywords (append org-done-keywords dws nil))
  3830. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3831. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3832. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3833. (setq org-todo-sets (nreverse org-todo-sets)
  3834. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3835. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3836. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3837. ;; Process the constants
  3838. (when const
  3839. (let (e cst)
  3840. (while (setq e (pop const))
  3841. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3842. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3843. (setq org-table-formula-constants-local cst)))
  3844. ;; Process the tags.
  3845. (when tags
  3846. (let (e tgs)
  3847. (while (setq e (pop tags))
  3848. (cond
  3849. ((equal e "{") (push '(:startgroup) tgs))
  3850. ((equal e "}") (push '(:endgroup) tgs))
  3851. ((equal e "\\n") (push '(:newline) tgs))
  3852. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3853. (push (cons (match-string 1 e)
  3854. (string-to-char (match-string 2 e)))
  3855. tgs))
  3856. (t (push (list e) tgs))))
  3857. (org-set-local 'org-tag-alist nil)
  3858. (while (setq e (pop tgs))
  3859. (or (and (stringp (car e))
  3860. (assoc (car e) org-tag-alist))
  3861. (push e org-tag-alist)))))
  3862. ;; Compute the regular expressions and other local variables
  3863. (if (not org-done-keywords)
  3864. (setq org-done-keywords (and org-todo-keywords-1
  3865. (list (org-last org-todo-keywords-1)))))
  3866. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3867. (length org-scheduled-string)
  3868. (length org-clock-string)
  3869. (length org-closed-string)))
  3870. org-drawer-regexp
  3871. (concat "^[ \t]*:\\("
  3872. (mapconcat 'regexp-quote org-drawers "\\|")
  3873. "\\):[ \t]*$")
  3874. org-not-done-keywords
  3875. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3876. org-todo-regexp
  3877. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3878. "\\|") "\\)\\>")
  3879. org-not-done-regexp
  3880. (concat "\\<\\("
  3881. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3882. "\\)\\>")
  3883. org-not-done-heading-regexp
  3884. (concat "^\\(\\*+\\)[ \t]+\\("
  3885. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3886. "\\)\\>")
  3887. org-todo-line-regexp
  3888. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3889. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3890. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3891. org-complex-heading-regexp
  3892. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3893. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3894. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3895. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3896. org-complex-heading-regexp-format
  3897. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3898. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3899. "\\)\\>\\)?"
  3900. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3901. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3902. "[ \t]*\\(%s\\)"
  3903. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3904. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3905. org-nl-done-regexp
  3906. (concat "\n\\*+[ \t]+"
  3907. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3908. "\\)" "\\>")
  3909. org-todo-line-tags-regexp
  3910. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3911. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3912. (org-re
  3913. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3914. org-looking-at-done-regexp
  3915. (concat "^" "\\(?:"
  3916. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3917. "\\>")
  3918. org-deadline-regexp (concat "\\<" org-deadline-string)
  3919. org-deadline-time-regexp
  3920. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3921. org-deadline-line-regexp
  3922. (concat "\\<\\(" org-deadline-string "\\).*")
  3923. org-scheduled-regexp
  3924. (concat "\\<" org-scheduled-string)
  3925. org-scheduled-time-regexp
  3926. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3927. org-closed-time-regexp
  3928. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3929. org-keyword-time-regexp
  3930. (concat "\\<\\(" org-scheduled-string
  3931. "\\|" org-deadline-string
  3932. "\\|" org-closed-string
  3933. "\\|" org-clock-string "\\)"
  3934. " *[[<]\\([^]>]+\\)[]>]")
  3935. org-keyword-time-not-clock-regexp
  3936. (concat "\\<\\(" org-scheduled-string
  3937. "\\|" org-deadline-string
  3938. "\\|" org-closed-string
  3939. "\\)"
  3940. " *[[<]\\([^]>]+\\)[]>]")
  3941. org-maybe-keyword-time-regexp
  3942. (concat "\\(\\<\\(" org-scheduled-string
  3943. "\\|" org-deadline-string
  3944. "\\|" org-closed-string
  3945. "\\|" org-clock-string "\\)\\)?"
  3946. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3947. org-planning-or-clock-line-re
  3948. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3949. "\\|" org-deadline-string
  3950. "\\|" org-closed-string "\\|" org-clock-string
  3951. "\\)\\>\\)")
  3952. org-all-time-keywords
  3953. (mapcar (lambda (w) (substring w 0 -1))
  3954. (list org-scheduled-string org-deadline-string
  3955. org-clock-string org-closed-string))
  3956. )
  3957. (org-compute-latex-and-specials-regexp)
  3958. (org-set-font-lock-defaults))))
  3959. (defun org-file-contents (file &optional noerror)
  3960. "Return the contents of FILE, as a string."
  3961. (if (or (not file)
  3962. (not (file-readable-p file)))
  3963. (if noerror
  3964. (progn
  3965. (message "Cannot read file \"%s\"" file)
  3966. (ding) (sit-for 2)
  3967. "")
  3968. (error "Cannot read file \"%s\"" file))
  3969. (with-temp-buffer
  3970. (insert-file-contents file)
  3971. (buffer-string))))
  3972. (defun org-extract-log-state-settings (x)
  3973. "Extract the log state setting from a TODO keyword string.
  3974. This will extract info from a string like \"WAIT(w@/!)\"."
  3975. (let (kw key log1 log2)
  3976. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3977. (setq kw (match-string 1 x)
  3978. key (and (match-end 2) (match-string 2 x))
  3979. log1 (and (match-end 3) (match-string 3 x))
  3980. log2 (and (match-end 4) (match-string 4 x)))
  3981. (and (or log1 log2)
  3982. (list kw
  3983. (and log1 (if (equal log1 "!") 'time 'note))
  3984. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3985. (defun org-remove-keyword-keys (list)
  3986. "Remove a pair of parenthesis at the end of each string in LIST."
  3987. (mapcar (lambda (x)
  3988. (if (string-match "(.*)$" x)
  3989. (substring x 0 (match-beginning 0))
  3990. x))
  3991. list))
  3992. (defun org-assign-fast-keys (alist)
  3993. "Assign fast keys to a keyword-key alist.
  3994. Respect keys that are already there."
  3995. (let (new e (alt ?0))
  3996. (while (setq e (pop alist))
  3997. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3998. (cdr e)) ;; Key already assigned.
  3999. (push e new)
  4000. (let ((clist (string-to-list (downcase (car e))))
  4001. (used (append new alist)))
  4002. (when (= (car clist) ?@)
  4003. (pop clist))
  4004. (while (and clist (rassoc (car clist) used))
  4005. (pop clist))
  4006. (unless clist
  4007. (while (rassoc alt used)
  4008. (incf alt)))
  4009. (push (cons (car e) (or (car clist) alt)) new))))
  4010. (nreverse new)))
  4011. ;;; Some variables used in various places
  4012. (defvar org-window-configuration nil
  4013. "Used in various places to store a window configuration.")
  4014. (defvar org-selected-window nil
  4015. "Used in various places to store a window configuration.")
  4016. (defvar org-finish-function nil
  4017. "Function to be called when `C-c C-c' is used.
  4018. This is for getting out of special buffers like remember.")
  4019. ;; FIXME: Occasionally check by commenting these, to make sure
  4020. ;; no other functions uses these, forgetting to let-bind them.
  4021. (defvar entry)
  4022. (defvar last-state)
  4023. (defvar date)
  4024. ;; Defined somewhere in this file, but used before definition.
  4025. (defvar org-entities) ;; defined in org-entities.el
  4026. (defvar org-struct-menu)
  4027. (defvar org-org-menu)
  4028. (defvar org-tbl-menu)
  4029. ;;;; Define the Org-mode
  4030. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4031. (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"))
  4032. ;; We use a before-change function to check if a table might need
  4033. ;; an update.
  4034. (defvar org-table-may-need-update t
  4035. "Indicates that a table might need an update.
  4036. This variable is set by `org-before-change-function'.
  4037. `org-table-align' sets it back to nil.")
  4038. (defun org-before-change-function (beg end)
  4039. "Every change indicates that a table might need an update."
  4040. (setq org-table-may-need-update t))
  4041. (defvar org-mode-map)
  4042. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4043. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4044. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4045. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4046. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4047. (defvar org-table-buffer-is-an nil)
  4048. (defconst org-outline-regexp "\\*+ ")
  4049. ;;;###autoload
  4050. (define-derived-mode org-mode outline-mode "Org"
  4051. "Outline-based notes management and organizer, alias
  4052. \"Carsten's outline-mode for keeping track of everything.\"
  4053. Org-mode develops organizational tasks around a NOTES file which
  4054. contains information about projects as plain text. Org-mode is
  4055. implemented on top of outline-mode, which is ideal to keep the content
  4056. of large files well structured. It supports ToDo items, deadlines and
  4057. time stamps, which magically appear in the diary listing of the Emacs
  4058. calendar. Tables are easily created with a built-in table editor.
  4059. Plain text URL-like links connect to websites, emails (VM), Usenet
  4060. messages (Gnus), BBDB entries, and any files related to the project.
  4061. For printing and sharing of notes, an Org-mode file (or a part of it)
  4062. can be exported as a structured ASCII or HTML file.
  4063. The following commands are available:
  4064. \\{org-mode-map}"
  4065. ;; Get rid of Outline menus, they are not needed
  4066. ;; Need to do this here because define-derived-mode sets up
  4067. ;; the keymap so late. Still, it is a waste to call this each time
  4068. ;; we switch another buffer into org-mode.
  4069. (if (featurep 'xemacs)
  4070. (when (boundp 'outline-mode-menu-heading)
  4071. ;; Assume this is Greg's port, it uses easymenu
  4072. (easy-menu-remove outline-mode-menu-heading)
  4073. (easy-menu-remove outline-mode-menu-show)
  4074. (easy-menu-remove outline-mode-menu-hide))
  4075. (define-key org-mode-map [menu-bar headings] 'undefined)
  4076. (define-key org-mode-map [menu-bar hide] 'undefined)
  4077. (define-key org-mode-map [menu-bar show] 'undefined))
  4078. (org-load-modules-maybe)
  4079. (easy-menu-add org-org-menu)
  4080. (easy-menu-add org-tbl-menu)
  4081. (org-install-agenda-files-menu)
  4082. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4083. (add-to-invisibility-spec '(org-cwidth))
  4084. (add-to-invisibility-spec '(org-hide-block . t))
  4085. (when (featurep 'xemacs)
  4086. (org-set-local 'line-move-ignore-invisible t))
  4087. (org-set-local 'outline-regexp org-outline-regexp)
  4088. (org-set-local 'outline-level 'org-outline-level)
  4089. (when (and org-ellipsis
  4090. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4091. (fboundp 'make-glyph-code))
  4092. (unless org-display-table
  4093. (setq org-display-table (make-display-table)))
  4094. (set-display-table-slot
  4095. org-display-table 4
  4096. (vconcat (mapcar
  4097. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4098. org-ellipsis)))
  4099. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4100. (setq buffer-display-table org-display-table))
  4101. (org-set-regexps-and-options)
  4102. (when (and org-tag-faces (not org-tags-special-faces-re))
  4103. ;; tag faces set outside customize.... force initialization.
  4104. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4105. ;; Calc embedded
  4106. (org-set-local 'calc-embedded-open-mode "# ")
  4107. (modify-syntax-entry ?@ "w")
  4108. (if org-startup-truncated (setq truncate-lines t))
  4109. (org-set-local 'font-lock-unfontify-region-function
  4110. 'org-unfontify-region)
  4111. ;; Activate before-change-function
  4112. (org-set-local 'org-table-may-need-update t)
  4113. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4114. 'local)
  4115. ;; Check for running clock before killing a buffer
  4116. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4117. ;; Paragraphs and auto-filling
  4118. (org-set-autofill-regexps)
  4119. (setq indent-line-function 'org-indent-line-function)
  4120. (org-update-radio-target-regexp)
  4121. ;; Beginning/end of defun
  4122. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4123. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4124. ;; Make sure dependence stuff works reliably, even for users who set it
  4125. ;; too late :-(
  4126. (if org-enforce-todo-dependencies
  4127. (add-hook 'org-blocker-hook
  4128. 'org-block-todo-from-children-or-siblings-or-parent)
  4129. (remove-hook 'org-blocker-hook
  4130. 'org-block-todo-from-children-or-siblings-or-parent))
  4131. (if org-enforce-todo-checkbox-dependencies
  4132. (add-hook 'org-blocker-hook
  4133. 'org-block-todo-from-checkboxes)
  4134. (remove-hook 'org-blocker-hook
  4135. 'org-block-todo-from-checkboxes))
  4136. ;; Comment characters
  4137. (org-set-local 'comment-start "#")
  4138. (org-set-local 'comment-padding " ")
  4139. ;; Align options lines
  4140. (org-set-local
  4141. 'align-mode-rules-list
  4142. '((org-in-buffer-settings
  4143. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4144. (modes . '(org-mode)))))
  4145. ;; Imenu
  4146. (org-set-local 'imenu-create-index-function
  4147. 'org-imenu-get-tree)
  4148. ;; Make isearch reveal context
  4149. (if (or (featurep 'xemacs)
  4150. (not (boundp 'outline-isearch-open-invisible-function)))
  4151. ;; Emacs 21 and XEmacs make use of the hook
  4152. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4153. ;; Emacs 22 deals with this through a special variable
  4154. (org-set-local 'outline-isearch-open-invisible-function
  4155. (lambda (&rest ignore) (org-show-context 'isearch))))
  4156. ;; Turn on org-beamer-mode?
  4157. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4158. ;; If empty file that did not turn on org-mode automatically, make it to.
  4159. (if (and org-insert-mode-line-in-empty-file
  4160. (interactive-p)
  4161. (= (point-min) (point-max)))
  4162. (insert "# -*- mode: org -*-\n\n"))
  4163. (unless org-inhibit-startup
  4164. (when org-startup-align-all-tables
  4165. (let ((bmp (buffer-modified-p)))
  4166. (org-table-map-tables 'org-table-align 'quietly)
  4167. (set-buffer-modified-p bmp)))
  4168. (when org-startup-indented
  4169. (require 'org-indent)
  4170. (org-indent-mode 1))
  4171. (unless org-inhibit-startup-visibility-stuff
  4172. (org-set-startup-visibility))))
  4173. (when (fboundp 'abbrev-table-put)
  4174. (abbrev-table-put org-mode-abbrev-table
  4175. :parents (list text-mode-abbrev-table)))
  4176. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4177. (defun org-current-time ()
  4178. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4179. (if (> (car org-time-stamp-rounding-minutes) 1)
  4180. (let ((r (car org-time-stamp-rounding-minutes))
  4181. (time (decode-time)))
  4182. (apply 'encode-time
  4183. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4184. (nthcdr 2 time))))
  4185. (current-time)))
  4186. ;;;; Font-Lock stuff, including the activators
  4187. (defvar org-mouse-map (make-sparse-keymap))
  4188. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4189. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4190. (when org-mouse-1-follows-link
  4191. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4192. (when org-tab-follows-link
  4193. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4194. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4195. (require 'font-lock)
  4196. (defconst org-non-link-chars "]\t\n\r<>")
  4197. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4198. "shell" "elisp" "doi"))
  4199. (defvar org-link-types-re nil
  4200. "Matches a link that has a url-like prefix like \"http:\"")
  4201. (defvar org-link-re-with-space nil
  4202. "Matches a link with spaces, optional angular brackets around it.")
  4203. (defvar org-link-re-with-space2 nil
  4204. "Matches a link with spaces, optional angular brackets around it.")
  4205. (defvar org-link-re-with-space3 nil
  4206. "Matches a link with spaces, only for internal part in bracket links.")
  4207. (defvar org-angle-link-re nil
  4208. "Matches link with angular brackets, spaces are allowed.")
  4209. (defvar org-plain-link-re nil
  4210. "Matches plain link, without spaces.")
  4211. (defvar org-bracket-link-regexp nil
  4212. "Matches a link in double brackets.")
  4213. (defvar org-bracket-link-analytic-regexp nil
  4214. "Regular expression used to analyze links.
  4215. Here is what the match groups contain after a match:
  4216. 1: http:
  4217. 2: http
  4218. 3: path
  4219. 4: [desc]
  4220. 5: desc")
  4221. (defvar org-bracket-link-analytic-regexp++ nil
  4222. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4223. (defvar org-any-link-re nil
  4224. "Regular expression matching any link.")
  4225. (defcustom org-match-sexp-depth 3
  4226. "Number of stacked braces for sub/superscript matching.
  4227. This has to be set before loading org.el to be effective."
  4228. :group 'org-export-translation ; ??????????????????????????/
  4229. :type 'integer)
  4230. (defun org-create-multibrace-regexp (left right n)
  4231. "Create a regular expression which will match a balanced sexp.
  4232. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4233. as single character strings.
  4234. The regexp returned will match the entire expression including the
  4235. delimiters. It will also define a single group which contains the
  4236. match except for the outermost delimiters. The maximum depth of
  4237. stacked delimiters is N. Escaping delimiters is not possible."
  4238. (let* ((nothing (concat "[^" left right "]*?"))
  4239. (or "\\|")
  4240. (re nothing)
  4241. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4242. (while (> n 1)
  4243. (setq n (1- n)
  4244. re (concat re or next)
  4245. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4246. (concat left "\\(" re "\\)" right)))
  4247. (defvar org-match-substring-regexp
  4248. (concat
  4249. "\\([^\\]\\)\\([_^]\\)\\("
  4250. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4251. "\\|"
  4252. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4253. "\\|"
  4254. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4255. "The regular expression matching a sub- or superscript.")
  4256. (defvar org-match-substring-with-braces-regexp
  4257. (concat
  4258. "\\([^\\]\\)\\([_^]\\)\\("
  4259. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4260. "\\)")
  4261. "The regular expression matching a sub- or superscript, forcing braces.")
  4262. (defun org-make-link-regexps ()
  4263. "Update the link regular expressions.
  4264. This should be called after the variable `org-link-types' has changed."
  4265. (setq org-link-types-re
  4266. (concat
  4267. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4268. org-link-re-with-space
  4269. (concat
  4270. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4271. "\\([^" org-non-link-chars " ]"
  4272. "[^" org-non-link-chars "]*"
  4273. "[^" org-non-link-chars " ]\\)>?")
  4274. org-link-re-with-space2
  4275. (concat
  4276. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4277. "\\([^" org-non-link-chars " ]"
  4278. "[^\t\n\r]*"
  4279. "[^" org-non-link-chars " ]\\)>?")
  4280. org-link-re-with-space3
  4281. (concat
  4282. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4283. "\\([^" org-non-link-chars " ]"
  4284. "[^\t\n\r]*\\)")
  4285. org-angle-link-re
  4286. (concat
  4287. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4288. "\\([^" org-non-link-chars " ]"
  4289. "[^" org-non-link-chars "]*"
  4290. "\\)>")
  4291. org-plain-link-re
  4292. (concat
  4293. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4294. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4295. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4296. org-bracket-link-regexp
  4297. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4298. org-bracket-link-analytic-regexp
  4299. (concat
  4300. "\\[\\["
  4301. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4302. "\\([^]]+\\)"
  4303. "\\]"
  4304. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4305. "\\]")
  4306. org-bracket-link-analytic-regexp++
  4307. (concat
  4308. "\\[\\["
  4309. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4310. "\\([^]]+\\)"
  4311. "\\]"
  4312. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4313. "\\]")
  4314. org-any-link-re
  4315. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4316. org-angle-link-re "\\)\\|\\("
  4317. org-plain-link-re "\\)")))
  4318. (org-make-link-regexps)
  4319. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4320. "Regular expression for fast time stamp matching.")
  4321. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4322. "Regular expression for fast time stamp matching.")
  4323. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4324. "Regular expression matching time strings for analysis.
  4325. This one does not require the space after the date, so it can be used
  4326. on a string that terminates immediately after the date.")
  4327. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4328. "Regular expression matching time strings for analysis.")
  4329. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4330. "Regular expression matching time stamps, with groups.")
  4331. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4332. "Regular expression matching time stamps (also [..]), with groups.")
  4333. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4334. "Regular expression matching a time stamp range.")
  4335. (defconst org-tr-regexp-both
  4336. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4337. "Regular expression matching a time stamp range.")
  4338. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4339. org-ts-regexp "\\)?")
  4340. "Regular expression matching a time stamp or time stamp range.")
  4341. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4342. org-ts-regexp-both "\\)?")
  4343. "Regular expression matching a time stamp or time stamp range.
  4344. The time stamps may be either active or inactive.")
  4345. (defvar org-emph-face nil)
  4346. (defun org-do-emphasis-faces (limit)
  4347. "Run through the buffer and add overlays to links."
  4348. (let (rtn a)
  4349. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4350. (if (not (= (char-after (match-beginning 3))
  4351. (char-after (match-beginning 4))))
  4352. (progn
  4353. (setq rtn t)
  4354. (setq a (assoc (match-string 3) org-emphasis-alist))
  4355. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4356. 'face
  4357. (nth 1 a))
  4358. (and (nth 4 a)
  4359. (org-remove-flyspell-overlays-in
  4360. (match-beginning 0) (match-end 0)))
  4361. (add-text-properties (match-beginning 2) (match-end 2)
  4362. '(font-lock-multiline t org-emphasis t))
  4363. (when org-hide-emphasis-markers
  4364. (add-text-properties (match-end 4) (match-beginning 5)
  4365. '(invisible org-link))
  4366. (add-text-properties (match-beginning 3) (match-end 3)
  4367. '(invisible org-link)))))
  4368. (backward-char 1))
  4369. rtn))
  4370. (defun org-emphasize (&optional char)
  4371. "Insert or change an emphasis, i.e. a font like bold or italic.
  4372. If there is an active region, change that region to a new emphasis.
  4373. If there is no region, just insert the marker characters and position
  4374. the cursor between them.
  4375. CHAR should be either the marker character, or the first character of the
  4376. HTML tag associated with that emphasis. If CHAR is a space, the means
  4377. to remove the emphasis of the selected region.
  4378. If char is not given (for example in an interactive call) it
  4379. will be prompted for."
  4380. (interactive)
  4381. (let ((eal org-emphasis-alist) e det
  4382. (erc org-emphasis-regexp-components)
  4383. (prompt "")
  4384. (string "") beg end move tag c s)
  4385. (if (org-region-active-p)
  4386. (setq beg (region-beginning) end (region-end)
  4387. string (buffer-substring beg end))
  4388. (setq move t))
  4389. (while (setq e (pop eal))
  4390. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4391. c (aref tag 0))
  4392. (push (cons c (string-to-char (car e))) det)
  4393. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4394. (substring tag 1)))))
  4395. (setq det (nreverse det))
  4396. (unless char
  4397. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4398. (setq char (read-char-exclusive)))
  4399. (setq char (or (cdr (assoc char det)) char))
  4400. (if (equal char ?\ )
  4401. (setq s "" move nil)
  4402. (unless (assoc (char-to-string char) org-emphasis-alist)
  4403. (error "No such emphasis marker: \"%c\"" char))
  4404. (setq s (char-to-string char)))
  4405. (while (and (> (length string) 1)
  4406. (equal (substring string 0 1) (substring string -1))
  4407. (assoc (substring string 0 1) org-emphasis-alist))
  4408. (setq string (substring string 1 -1)))
  4409. (setq string (concat s string s))
  4410. (if beg (delete-region beg end))
  4411. (unless (or (bolp)
  4412. (string-match (concat "[" (nth 0 erc) "\n]")
  4413. (char-to-string (char-before (point)))))
  4414. (insert " "))
  4415. (unless (or (eobp)
  4416. (string-match (concat "[" (nth 1 erc) "\n]")
  4417. (char-to-string (char-after (point)))))
  4418. (insert " ") (backward-char 1))
  4419. (insert string)
  4420. (and move (backward-char 1))))
  4421. (defconst org-nonsticky-props
  4422. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4423. (defsubst org-rear-nonsticky-at (pos)
  4424. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4425. (defun org-activate-plain-links (limit)
  4426. "Run through the buffer and add overlays to links."
  4427. (catch 'exit
  4428. (let (f)
  4429. (if (re-search-forward org-plain-link-re limit t)
  4430. (progn
  4431. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4432. (setq f (get-text-property (match-beginning 0) 'face))
  4433. (if (or (eq f 'org-tag)
  4434. (and (listp f) (memq 'org-tag f)))
  4435. nil
  4436. (add-text-properties (match-beginning 0) (match-end 0)
  4437. (list 'mouse-face 'highlight
  4438. 'face 'org-link
  4439. 'keymap org-mouse-map))
  4440. (org-rear-nonsticky-at (match-end 0)))
  4441. t)))))
  4442. (defun org-activate-code (limit)
  4443. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4444. (progn
  4445. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4446. (remove-text-properties (match-beginning 0) (match-end 0)
  4447. '(display t invisible t intangible t))
  4448. t)))
  4449. (defvar org-src-fontify-natively t
  4450. "When non-nil, fontify code in code blocks.")
  4451. (defun org-fontify-meta-lines-and-blocks (limit)
  4452. "Fontify #+ lines and blocks, in the correct ways."
  4453. (let ((case-fold-search t))
  4454. (if (re-search-forward
  4455. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4456. limit t)
  4457. (let ((beg (match-beginning 0))
  4458. (block-start (match-end 0))
  4459. (block-end nil)
  4460. (lang (match-string 7))
  4461. (beg1 (line-beginning-position 2))
  4462. (dc1 (downcase (match-string 2)))
  4463. (dc3 (downcase (match-string 3)))
  4464. end end1 quoting block-type)
  4465. (cond
  4466. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4467. ;; a single line of backend-specific content
  4468. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4469. (remove-text-properties (match-beginning 0) (match-end 0)
  4470. '(display t invisible t intangible t))
  4471. (add-text-properties (match-beginning 1) (match-end 3)
  4472. '(font-lock-fontified t face org-meta-line))
  4473. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4474. '(font-lock-fontified t face org-block))
  4475. ; for backend-specific code
  4476. t)
  4477. ((and (match-end 4) (equal dc3 "begin"))
  4478. ;; Truly a block
  4479. (setq block-type (downcase (match-string 5))
  4480. quoting (member block-type org-protecting-blocks))
  4481. (when (re-search-forward
  4482. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4483. nil t) ;; on purpose, we look further than LIMIT
  4484. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4485. (setq block-end (match-beginning 0))
  4486. (when quoting
  4487. (remove-text-properties beg end
  4488. '(display t invisible t intangible t)))
  4489. (add-text-properties
  4490. beg end
  4491. '(font-lock-fontified t font-lock-multiline t))
  4492. (add-text-properties beg beg1 '(face org-meta-line))
  4493. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4494. ; for end_src
  4495. (cond
  4496. ((and lang org-src-fontify-natively)
  4497. (org-src-font-lock-fontify-block lang block-start block-end))
  4498. (quoting
  4499. (add-text-properties beg1 (+ end1 1) '(face
  4500. org-block)))
  4501. ; end of source block
  4502. ((not org-fontify-quote-and-verse-blocks))
  4503. ((string= block-type "quote")
  4504. (add-text-properties beg1 end1 '(face org-quote)))
  4505. ((string= block-type "verse")
  4506. (add-text-properties beg1 end1 '(face org-verse))))
  4507. t))
  4508. ((member dc1 '("title:" "author:" "email:" "date:"))
  4509. (add-text-properties
  4510. beg (match-end 3)
  4511. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4512. '(font-lock-fontified t invisible t)
  4513. '(font-lock-fontified t face org-document-info-keyword)))
  4514. (add-text-properties
  4515. (match-beginning 6) (match-end 6)
  4516. (if (string-equal dc1 "title:")
  4517. '(font-lock-fontified t face org-document-title)
  4518. '(font-lock-fontified t face org-document-info))))
  4519. ((not (member (char-after beg) '(?\ ?\t)))
  4520. ;; just any other in-buffer setting, but not indented
  4521. (add-text-properties
  4522. beg (match-end 0)
  4523. '(font-lock-fontified t face org-meta-line))
  4524. t)
  4525. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4526. "orgtbl:" "tblfm:" "tblname:" "result:"
  4527. "results:" "source:" "srcname:" "call:"))
  4528. (and (match-end 4) (equal dc3 "attr")))
  4529. (add-text-properties
  4530. beg (match-end 0)
  4531. '(font-lock-fontified t face org-meta-line))
  4532. t)
  4533. ((member dc3 '(" " ""))
  4534. (add-text-properties
  4535. beg (match-end 0)
  4536. '(font-lock-fontified t face font-lock-comment-face)))
  4537. (t nil))))))
  4538. (defun org-activate-angle-links (limit)
  4539. "Run through the buffer and add overlays to links."
  4540. (if (re-search-forward org-angle-link-re limit t)
  4541. (progn
  4542. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4543. (add-text-properties (match-beginning 0) (match-end 0)
  4544. (list 'mouse-face 'highlight
  4545. 'keymap org-mouse-map))
  4546. (org-rear-nonsticky-at (match-end 0))
  4547. t)))
  4548. (defun org-activate-footnote-links (limit)
  4549. "Run through the buffer and add overlays to links."
  4550. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4551. limit t)
  4552. (progn
  4553. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4554. (add-text-properties (match-beginning 2) (match-end 2)
  4555. (list 'mouse-face 'highlight
  4556. 'keymap org-mouse-map
  4557. 'help-echo
  4558. (if (= (point-at-bol) (match-beginning 2))
  4559. "Footnote definition"
  4560. "Footnote reference")
  4561. ))
  4562. (org-rear-nonsticky-at (match-end 2))
  4563. t)))
  4564. (defun org-activate-bracket-links (limit)
  4565. "Run through the buffer and add overlays to bracketed links."
  4566. (if (re-search-forward org-bracket-link-regexp limit t)
  4567. (let* ((help (concat "LINK: "
  4568. (org-match-string-no-properties 1)))
  4569. ;; FIXME: above we should remove the escapes.
  4570. ;; but that requires another match, protecting match data,
  4571. ;; a lot of overhead for font-lock.
  4572. (ip (org-maybe-intangible
  4573. (list 'invisible 'org-link
  4574. 'keymap org-mouse-map 'mouse-face 'highlight
  4575. 'font-lock-multiline t 'help-echo help)))
  4576. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4577. 'font-lock-multiline t 'help-echo help)))
  4578. ;; We need to remove the invisible property here. Table narrowing
  4579. ;; may have made some of this invisible.
  4580. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4581. (remove-text-properties (match-beginning 0) (match-end 0)
  4582. '(invisible nil))
  4583. (if (match-end 3)
  4584. (progn
  4585. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4586. (org-rear-nonsticky-at (match-beginning 3))
  4587. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4588. (org-rear-nonsticky-at (match-end 3))
  4589. (add-text-properties (match-end 3) (match-end 0) ip)
  4590. (org-rear-nonsticky-at (match-end 0)))
  4591. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4592. (org-rear-nonsticky-at (match-beginning 1))
  4593. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4594. (org-rear-nonsticky-at (match-end 1))
  4595. (add-text-properties (match-end 1) (match-end 0) ip)
  4596. (org-rear-nonsticky-at (match-end 0)))
  4597. t)))
  4598. (defun org-activate-dates (limit)
  4599. "Run through the buffer and add overlays to dates."
  4600. (if (re-search-forward org-tsr-regexp-both limit t)
  4601. (progn
  4602. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4603. (add-text-properties (match-beginning 0) (match-end 0)
  4604. (list 'mouse-face 'highlight
  4605. 'keymap org-mouse-map))
  4606. (org-rear-nonsticky-at (match-end 0))
  4607. (when org-display-custom-times
  4608. (if (match-end 3)
  4609. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4610. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4611. t)))
  4612. (defvar org-target-link-regexp nil
  4613. "Regular expression matching radio targets in plain text.")
  4614. (make-variable-buffer-local 'org-target-link-regexp)
  4615. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4616. "Regular expression matching a link target.")
  4617. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4618. "Regular expression matching a radio target.")
  4619. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4620. "Regular expression matching any target.")
  4621. (defun org-activate-target-links (limit)
  4622. "Run through the buffer and add overlays to target matches."
  4623. (when org-target-link-regexp
  4624. (let ((case-fold-search t))
  4625. (if (re-search-forward org-target-link-regexp limit t)
  4626. (progn
  4627. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4628. (add-text-properties (match-beginning 0) (match-end 0)
  4629. (list 'mouse-face 'highlight
  4630. 'keymap org-mouse-map
  4631. 'help-echo "Radio target link"
  4632. 'org-linked-text t))
  4633. (org-rear-nonsticky-at (match-end 0))
  4634. t)))))
  4635. (defun org-update-radio-target-regexp ()
  4636. "Find all radio targets in this file and update the regular expression."
  4637. (interactive)
  4638. (when (memq 'radio org-activate-links)
  4639. (setq org-target-link-regexp
  4640. (org-make-target-link-regexp (org-all-targets 'radio)))
  4641. (org-restart-font-lock)))
  4642. (defun org-hide-wide-columns (limit)
  4643. (let (s e)
  4644. (setq s (text-property-any (point) (or limit (point-max))
  4645. 'org-cwidth t))
  4646. (when s
  4647. (setq e (next-single-property-change s 'org-cwidth))
  4648. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4649. (goto-char e)
  4650. t)))
  4651. (defvar org-latex-and-specials-regexp nil
  4652. "Regular expression for highlighting export special stuff.")
  4653. (defvar org-match-substring-regexp)
  4654. (defvar org-match-substring-with-braces-regexp)
  4655. ;; This should be with the exporter code, but we also use if for font-locking
  4656. (defconst org-export-html-special-string-regexps
  4657. '(("\\\\-" . "&shy;")
  4658. ("---\\([^-]\\)" . "&mdash;\\1")
  4659. ("--\\([^-]\\)" . "&ndash;\\1")
  4660. ("\\.\\.\\." . "&hellip;"))
  4661. "Regular expressions for special string conversion.")
  4662. (defun org-compute-latex-and-specials-regexp ()
  4663. "Compute regular expression for stuff treated specially by exporters."
  4664. (if (not org-highlight-latex-fragments-and-specials)
  4665. (org-set-local 'org-latex-and-specials-regexp nil)
  4666. (require 'org-exp)
  4667. (let*
  4668. ((matchers (plist-get org-format-latex-options :matchers))
  4669. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4670. org-latex-regexps)))
  4671. (org-export-allow-BIND nil)
  4672. (options (org-combine-plists (org-default-export-plist)
  4673. (org-infile-export-plist)))
  4674. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4675. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4676. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4677. (org-export-html-expand (plist-get options :expand-quoted-html))
  4678. (org-export-with-special-strings (plist-get options :special-strings))
  4679. (re-sub
  4680. (cond
  4681. ((equal org-export-with-sub-superscripts '{})
  4682. (list org-match-substring-with-braces-regexp))
  4683. (org-export-with-sub-superscripts
  4684. (list org-match-substring-regexp))
  4685. (t nil)))
  4686. (re-latex
  4687. (if org-export-with-LaTeX-fragments
  4688. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4689. (re-macros
  4690. (if org-export-with-TeX-macros
  4691. (list (concat "\\\\"
  4692. (regexp-opt
  4693. (append
  4694. (delq nil
  4695. (mapcar 'car-safe
  4696. (append org-entities-user
  4697. org-entities)))
  4698. (if (boundp 'org-latex-entities)
  4699. (mapcar (lambda (x)
  4700. (or (car-safe x) x))
  4701. org-latex-entities)
  4702. nil))
  4703. 'words))) ; FIXME
  4704. ))
  4705. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4706. (re-special (if org-export-with-special-strings
  4707. (mapcar (lambda (x) (car x))
  4708. org-export-html-special-string-regexps)))
  4709. (re-rest
  4710. (delq nil
  4711. (list
  4712. (if org-export-html-expand "@<[^>\n]+>")
  4713. ))))
  4714. (org-set-local
  4715. 'org-latex-and-specials-regexp
  4716. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4717. re-rest) "\\|")))))
  4718. (defun org-do-latex-and-special-faces (limit)
  4719. "Run through the buffer and add overlays to links."
  4720. (when org-latex-and-specials-regexp
  4721. (let (rtn d)
  4722. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4723. limit t))
  4724. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4725. 'face))
  4726. '(org-code org-verbatim underline)))
  4727. (progn
  4728. (setq rtn t
  4729. d (cond ((member (char-after (1+ (match-beginning 0)))
  4730. '(?_ ?^)) 1)
  4731. (t 0)))
  4732. (font-lock-prepend-text-property
  4733. (+ d (match-beginning 0)) (match-end 0)
  4734. 'face 'org-latex-and-export-specials)
  4735. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4736. '(font-lock-multiline t)))))
  4737. rtn)))
  4738. (defun org-restart-font-lock ()
  4739. "Restart `font-lock-mode', to force refontification."
  4740. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4741. (font-lock-mode -1)
  4742. (font-lock-mode 1)))
  4743. (defun org-all-targets (&optional radio)
  4744. "Return a list of all targets in this file.
  4745. With optional argument RADIO, only find radio targets."
  4746. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4747. rtn)
  4748. (save-excursion
  4749. (goto-char (point-min))
  4750. (while (re-search-forward re nil t)
  4751. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4752. rtn)))
  4753. (defun org-make-target-link-regexp (targets)
  4754. "Make regular expression matching all strings in TARGETS.
  4755. The regular expression finds the targets also if there is a line break
  4756. between words."
  4757. (and targets
  4758. (concat
  4759. "\\<\\("
  4760. (mapconcat
  4761. (lambda (x)
  4762. (while (string-match " +" x)
  4763. (setq x (replace-match "\\s-+" t t x)))
  4764. x)
  4765. targets
  4766. "\\|")
  4767. "\\)\\>")))
  4768. (defun org-activate-tags (limit)
  4769. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4770. (progn
  4771. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4772. (add-text-properties (match-beginning 1) (match-end 1)
  4773. (list 'mouse-face 'highlight
  4774. 'keymap org-mouse-map))
  4775. (org-rear-nonsticky-at (match-end 1))
  4776. t)))
  4777. (defun org-outline-level ()
  4778. "Compute the outline level of the heading at point.
  4779. This function assumes that the cursor is at the beginning of a line matched
  4780. by `outline-regexp'. Otherwise it returns garbage.
  4781. If this is called at a normal headline, the level is the number of stars.
  4782. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4783. For plain list items, if they are matched by `outline-regexp', this returns
  4784. 1000 plus the line indentation."
  4785. (save-excursion
  4786. (looking-at outline-regexp)
  4787. (if (match-beginning 1)
  4788. (+ (org-get-string-indentation (match-string 1)) 1000)
  4789. (1- (- (match-end 0) (match-beginning 0))))))
  4790. (defvar org-font-lock-keywords nil)
  4791. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4792. "Regular expression matching a property line.")
  4793. (defvar org-font-lock-hook nil
  4794. "Functions to be called for special font lock stuff.")
  4795. (defvar org-font-lock-set-keywords-hook nil
  4796. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4797. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4798. it is installed to be used by font lock. This can be useful if something
  4799. needs to be inserted at a specific position in the font-lock sequence.")
  4800. (defun org-font-lock-hook (limit)
  4801. (run-hook-with-args 'org-font-lock-hook limit))
  4802. (defun org-set-font-lock-defaults ()
  4803. (let* ((em org-fontify-emphasized-text)
  4804. (lk org-activate-links)
  4805. (org-font-lock-extra-keywords
  4806. (list
  4807. ;; Call the hook
  4808. '(org-font-lock-hook)
  4809. ;; Headlines
  4810. `(,(if org-fontify-whole-heading-line
  4811. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4812. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4813. (1 (org-get-level-face 1))
  4814. (2 (org-get-level-face 2))
  4815. (3 (org-get-level-face 3)))
  4816. ;; Table lines
  4817. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4818. (1 'org-table t))
  4819. ;; Table internals
  4820. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4821. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4822. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4823. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4824. ;; Drawers
  4825. (list org-drawer-regexp '(0 'org-special-keyword t))
  4826. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4827. ;; Properties
  4828. (list org-property-re
  4829. '(1 'org-special-keyword t)
  4830. '(3 'org-property-value t))
  4831. ;; Links
  4832. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4833. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4834. (if (memq 'plain lk) '(org-activate-plain-links))
  4835. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4836. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4837. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4838. (if (memq 'footnote lk) '(org-activate-footnote-links
  4839. (2 'org-footnote t)))
  4840. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4841. '(org-hide-wide-columns (0 nil append))
  4842. ;; TODO lines
  4843. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4844. '(1 (org-get-todo-face 1) t))
  4845. ;; DONE
  4846. (if org-fontify-done-headline
  4847. (list (concat "^[*]+ +\\<\\("
  4848. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4849. "\\)\\(.*\\)")
  4850. '(2 'org-headline-done t))
  4851. nil)
  4852. ;; Priorities
  4853. '(org-font-lock-add-priority-faces)
  4854. ;; Tags
  4855. '(org-font-lock-add-tag-faces)
  4856. ;; Special keywords
  4857. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4858. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4859. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4860. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4861. ;; Emphasis
  4862. (if em
  4863. (if (featurep 'xemacs)
  4864. '(org-do-emphasis-faces (0 nil append))
  4865. '(org-do-emphasis-faces)))
  4866. ;; Checkboxes
  4867. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4868. 1 'org-checkbox prepend)
  4869. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4870. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4871. (0 (org-get-checkbox-statistics-face) t)))
  4872. ;; Description list items
  4873. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(.*? ::\\)"
  4874. 2 'bold prepend)
  4875. ;; ARCHIVEd headings
  4876. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4877. '(1 'org-archived prepend))
  4878. ;; Specials
  4879. '(org-do-latex-and-special-faces)
  4880. '(org-fontify-entities)
  4881. '(org-raise-scripts)
  4882. ;; Code
  4883. '(org-activate-code (1 'org-code t))
  4884. ;; COMMENT
  4885. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4886. "\\|" org-quote-string "\\)\\>")
  4887. '(1 'org-special-keyword t))
  4888. '("^#.*" (0 'font-lock-comment-face t))
  4889. ;; Blocks and meta lines
  4890. '(org-fontify-meta-lines-and-blocks)
  4891. )))
  4892. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4893. (run-hooks 'org-font-lock-set-keywords-hook)
  4894. ;; Now set the full font-lock-keywords
  4895. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4896. (org-set-local 'font-lock-defaults
  4897. '(org-font-lock-keywords t nil nil backward-paragraph))
  4898. (kill-local-variable 'font-lock-keywords) nil))
  4899. (defun org-toggle-pretty-entities ()
  4900. "Toggle the composition display of entities as UTF8 characters."
  4901. (interactive)
  4902. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4903. (org-restart-font-lock)
  4904. (if org-pretty-entities
  4905. (message "Entities are displayed as UTF8 characers")
  4906. (save-restriction
  4907. (widen)
  4908. (decompose-region (point-min) (point-max))
  4909. (message "Entities are displayed plain"))))
  4910. (defun org-fontify-entities (limit)
  4911. "Find an entity to fontify."
  4912. (let (ee)
  4913. (when org-pretty-entities
  4914. (catch 'match
  4915. (while (re-search-forward
  4916. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4917. limit t)
  4918. (if (and (not (org-in-indented-comment-line))
  4919. (setq ee (org-entity-get (match-string 1)))
  4920. (= (length (nth 6 ee)) 1))
  4921. (progn
  4922. (add-text-properties
  4923. (match-beginning 0) (match-end 1)
  4924. (list 'font-lock-fontified t))
  4925. (compose-region (match-beginning 0) (match-end 1)
  4926. (nth 6 ee) nil)
  4927. (backward-char 1)
  4928. (throw 'match t))))
  4929. nil))))
  4930. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4931. "Fontify string S like in Org-mode."
  4932. (with-temp-buffer
  4933. (insert s)
  4934. (let ((org-odd-levels-only odd-levels))
  4935. (org-mode)
  4936. (font-lock-fontify-buffer)
  4937. (buffer-string))))
  4938. (defvar org-m nil)
  4939. (defvar org-l nil)
  4940. (defvar org-f nil)
  4941. (defun org-get-level-face (n)
  4942. "Get the right face for match N in font-lock matching of headlines."
  4943. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4944. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4945. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4946. (cond
  4947. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4948. ((eq n 2) org-f)
  4949. (t (if org-level-color-stars-only nil org-f))))
  4950. (defun org-get-todo-face (kwd)
  4951. "Get the right face for a TODO keyword KWD.
  4952. If KWD is a number, get the corresponding match group."
  4953. (if (numberp kwd) (setq kwd (match-string kwd)))
  4954. (or (org-face-from-face-or-color
  4955. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4956. (and (member kwd org-done-keywords) 'org-done)
  4957. 'org-todo))
  4958. (defun org-face-from-face-or-color (context inherit face-or-color)
  4959. "Create a face list that inherits INHERIT, but sets the foreground color.
  4960. When FACE-OR-COLOR is not a string, just return it."
  4961. (if (stringp face-or-color)
  4962. (list :inherit inherit
  4963. (cdr (assoc context org-faces-easy-properties))
  4964. face-or-color)
  4965. face-or-color))
  4966. (defun org-font-lock-add-tag-faces (limit)
  4967. "Add the special tag faces."
  4968. (when (and org-tag-faces org-tags-special-faces-re)
  4969. (while (re-search-forward org-tags-special-faces-re limit t)
  4970. (add-text-properties (match-beginning 1) (match-end 1)
  4971. (list 'face (org-get-tag-face 1)
  4972. 'font-lock-fontified t))
  4973. (backward-char 1))))
  4974. (defun org-font-lock-add-priority-faces (limit)
  4975. "Add the special priority faces."
  4976. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4977. (add-text-properties
  4978. (match-beginning 0) (match-end 0)
  4979. (list 'face (or (org-face-from-face-or-color
  4980. 'priority 'org-special-keyword
  4981. (cdr (assoc (char-after (match-beginning 1))
  4982. org-priority-faces)))
  4983. 'org-special-keyword)
  4984. 'font-lock-fontified t))))
  4985. (defun org-get-tag-face (kwd)
  4986. "Get the right face for a TODO keyword KWD.
  4987. If KWD is a number, get the corresponding match group."
  4988. (if (numberp kwd) (setq kwd (match-string kwd)))
  4989. (or (org-face-from-face-or-color
  4990. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4991. 'org-tag))
  4992. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4993. "Remove fontification and activation overlays from links."
  4994. (font-lock-default-unfontify-region beg end)
  4995. (let* ((buffer-undo-list t)
  4996. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4997. (inhibit-modification-hooks t)
  4998. deactivate-mark buffer-file-name buffer-file-truename)
  4999. (decompose-region beg end)
  5000. (remove-text-properties
  5001. beg end
  5002. (if org-indent-mode
  5003. ;; also remove line-prefix and wrap-prefix properties
  5004. '(mouse-face t keymap t org-linked-text t
  5005. invisible t intangible t
  5006. line-prefix t wrap-prefix t
  5007. org-no-flyspell t org-emphasis t)
  5008. '(mouse-face t keymap t org-linked-text t
  5009. invisible t intangible t
  5010. org-no-flyspell t org-emphasis t)))
  5011. (org-remove-font-lock-display-properties beg end)))
  5012. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5013. ((raise 0.3) (height 0.7))
  5014. ((raise -0.5))
  5015. ((raise 0.5)))
  5016. "Display properties for showing superscripts and subscripts.")
  5017. (defun org-remove-font-lock-display-properties (beg end)
  5018. "Remove specific display properties that have been added by font lock.
  5019. The will remove the raise properties that are used to show superscripts
  5020. and subscripts."
  5021. (let (next prop)
  5022. (while (< beg end)
  5023. (setq next (next-single-property-change beg 'display nil end)
  5024. prop (get-text-property beg 'display))
  5025. (if (member prop org-script-display)
  5026. (put-text-property beg next 'display nil))
  5027. (setq beg next))))
  5028. (defun org-raise-scripts (limit)
  5029. "Add raise properties to sub/superscripts."
  5030. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5031. (if (re-search-forward
  5032. (if (eq org-use-sub-superscripts t)
  5033. org-match-substring-regexp
  5034. org-match-substring-with-braces-regexp)
  5035. limit t)
  5036. (let* ((pos (point)) table-p comment-p
  5037. (mpos (match-beginning 3))
  5038. (emph-p (get-text-property mpos 'org-emphasis))
  5039. (link-p (get-text-property mpos 'mouse-face))
  5040. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5041. (goto-char (point-at-bol))
  5042. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5043. comment-p (org-looking-at-p "[ \t]*#"))
  5044. (goto-char pos)
  5045. ;; FIXME: Should we go back one character here, for a_b^c
  5046. ;; (goto-char (1- pos)) ;????????????????????
  5047. (if (or comment-p emph-p link-p keyw-p)
  5048. t
  5049. (put-text-property (match-beginning 3) (match-end 0)
  5050. 'display
  5051. (if (equal (char-after (match-beginning 2)) ?^)
  5052. (nth (if table-p 3 1) org-script-display)
  5053. (nth (if table-p 2 0) org-script-display)))
  5054. (add-text-properties (match-beginning 2) (match-end 2)
  5055. (list 'invisible t
  5056. 'org-dwidth t 'org-dwidth-n 1))
  5057. (if (and (eq (char-after (match-beginning 3)) ?{)
  5058. (eq (char-before (match-end 3)) ?}))
  5059. (progn
  5060. (add-text-properties
  5061. (match-beginning 3) (1+ (match-beginning 3))
  5062. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5063. (add-text-properties
  5064. (1- (match-end 3)) (match-end 3)
  5065. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5066. t)))))
  5067. ;;;; Visibility cycling, including org-goto and indirect buffer
  5068. ;;; Cycling
  5069. (defvar org-cycle-global-status nil)
  5070. (make-variable-buffer-local 'org-cycle-global-status)
  5071. (defvar org-cycle-subtree-status nil)
  5072. (make-variable-buffer-local 'org-cycle-subtree-status)
  5073. ;;;###autoload
  5074. (defvar org-inlinetask-min-level)
  5075. (defun org-cycle (&optional arg)
  5076. "TAB-action and visibility cycling for Org-mode.
  5077. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5078. is outline visibility cycling, but it also invokes other actions
  5079. in special contexts.
  5080. - When this function is called with a prefix argument, rotate the entire
  5081. buffer through 3 states (global cycling)
  5082. 1. OVERVIEW: Show only top-level headlines.
  5083. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5084. 3. SHOW ALL: Show everything.
  5085. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5086. determined by the variable `org-startup-folded', and by any VISIBILITY
  5087. properties in the buffer.
  5088. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5089. including any drawers.
  5090. - When inside a table, re-align the table and move to the next field.
  5091. - When point is at the beginning of a headline, rotate the subtree started
  5092. by this line through 3 different states (local cycling)
  5093. 1. FOLDED: Only the main headline is shown.
  5094. 2. CHILDREN: The main headline and the direct children are shown.
  5095. From this state, you can move to one of the children
  5096. and zoom in further.
  5097. 3. SUBTREE: Show the entire subtree, including body text.
  5098. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5099. - When point is at the beginning of an empty headline and the variable
  5100. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5101. of the headline by demoting and promoting it to likely levels. This
  5102. speeds up creation document structure by pressing TAB once or several
  5103. times right after creating a new headline.
  5104. - When there is a numeric prefix, go up to a heading with level ARG, do
  5105. a `show-subtree' and return to the previous cursor position. If ARG
  5106. is negative, go up that many levels.
  5107. - When point is not at the beginning of a headline, execute the global
  5108. binding for TAB, which is re-indenting the line. See the option
  5109. `org-cycle-emulate-tab' for details.
  5110. - Special case: if point is at the beginning of the buffer and there is
  5111. no headline in line 1, this function will act as if called with prefix arg.
  5112. But only if also the variable `org-cycle-global-at-bob' is t."
  5113. (interactive "P")
  5114. (org-load-modules-maybe)
  5115. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5116. (and org-cycle-level-after-item/entry-creation
  5117. (or (org-cycle-level)
  5118. (org-cycle-item-indentation))))
  5119. (let* ((limit-level
  5120. (or org-cycle-max-level
  5121. (and (boundp 'org-inlinetask-min-level)
  5122. org-inlinetask-min-level
  5123. (1- org-inlinetask-min-level))))
  5124. (nstars (and limit-level
  5125. (if org-odd-levels-only
  5126. (and limit-level (1- (* limit-level 2)))
  5127. limit-level)))
  5128. (outline-regexp
  5129. (cond
  5130. ((not (org-mode-p)) outline-regexp)
  5131. ((or (eq org-cycle-include-plain-lists 'integrate)
  5132. (and org-cycle-include-plain-lists (org-at-item-p)))
  5133. (concat "\\(?:\\*"
  5134. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5135. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5136. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5137. (bob-special (and org-cycle-global-at-bob (bobp)
  5138. (not (looking-at outline-regexp))))
  5139. (org-cycle-hook
  5140. (if bob-special
  5141. (delq 'org-optimize-window-after-visibility-change
  5142. (copy-sequence org-cycle-hook))
  5143. org-cycle-hook))
  5144. (pos (point)))
  5145. (if (or bob-special (equal arg '(4)))
  5146. ;; special case: use global cycling
  5147. (setq arg t))
  5148. (cond
  5149. ((equal arg '(16))
  5150. (org-set-startup-visibility)
  5151. (message "Startup visibility, plus VISIBILITY properties"))
  5152. ((equal arg '(64))
  5153. (show-all)
  5154. (message "Entire buffer visible, including drawers"))
  5155. ((org-at-table-p 'any)
  5156. ;; Enter the table or move to the next field in the table
  5157. (if (org-at-table.el-p)
  5158. (message "Use C-c ' to edit table.el tables")
  5159. (if arg (org-table-edit-field t)
  5160. (org-table-justify-field-maybe)
  5161. (call-interactively 'org-table-next-field))))
  5162. ((run-hook-with-args-until-success
  5163. 'org-tab-after-check-for-table-hook))
  5164. ((eq arg t) ;; Global cycling
  5165. (org-cycle-internal-global))
  5166. ((and org-drawers org-drawer-regexp
  5167. (save-excursion
  5168. (beginning-of-line 1)
  5169. (looking-at org-drawer-regexp)))
  5170. ;; Toggle block visibility
  5171. (org-flag-drawer
  5172. (not (get-char-property (match-end 0) 'invisible))))
  5173. ((integerp arg)
  5174. ;; Show-subtree, ARG levels up from here.
  5175. (save-excursion
  5176. (org-back-to-heading)
  5177. (outline-up-heading (if (< arg 0) (- arg)
  5178. (- (funcall outline-level) arg)))
  5179. (org-show-subtree)))
  5180. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5181. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5182. (org-cycle-internal-local))
  5183. ;; TAB emulation and template completion
  5184. (buffer-read-only (org-back-to-heading))
  5185. ((run-hook-with-args-until-success
  5186. 'org-tab-after-check-for-cycling-hook))
  5187. ((org-try-structure-completion))
  5188. ((org-try-cdlatex-tab))
  5189. ((run-hook-with-args-until-success
  5190. 'org-tab-before-tab-emulation-hook))
  5191. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5192. (or (not (bolp))
  5193. (not (looking-at outline-regexp))))
  5194. (call-interactively (global-key-binding "\t")))
  5195. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5196. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5197. (or (and (eq org-cycle-emulate-tab 'white)
  5198. (= (match-end 0) (point-at-eol)))
  5199. (and (eq org-cycle-emulate-tab 'whitestart)
  5200. (>= (match-end 0) pos))))
  5201. t
  5202. (eq org-cycle-emulate-tab t))
  5203. (call-interactively (global-key-binding "\t")))
  5204. (t (save-excursion
  5205. (org-back-to-heading)
  5206. (org-cycle)))))))
  5207. (defun org-cycle-internal-global ()
  5208. "Do the global cycling action."
  5209. (cond
  5210. ((and (eq last-command this-command)
  5211. (eq org-cycle-global-status 'overview))
  5212. ;; We just created the overview - now do table of contents
  5213. ;; This can be slow in very large buffers, so indicate action
  5214. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5215. (message "CONTENTS...")
  5216. (org-content)
  5217. (message "CONTENTS...done")
  5218. (setq org-cycle-global-status 'contents)
  5219. (run-hook-with-args 'org-cycle-hook 'contents))
  5220. ((and (eq last-command this-command)
  5221. (eq org-cycle-global-status 'contents))
  5222. ;; We just showed the table of contents - now show everything
  5223. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5224. (show-all)
  5225. (message "SHOW ALL")
  5226. (setq org-cycle-global-status 'all)
  5227. (run-hook-with-args 'org-cycle-hook 'all))
  5228. (t
  5229. ;; Default action: go to overview
  5230. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5231. (org-overview)
  5232. (message "OVERVIEW")
  5233. (setq org-cycle-global-status 'overview)
  5234. (run-hook-with-args 'org-cycle-hook 'overview))))
  5235. (defun org-cycle-internal-local ()
  5236. "Do the local cycling action."
  5237. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5238. ;; First, some boundaries
  5239. (save-excursion
  5240. (org-back-to-heading)
  5241. (setq level (funcall outline-level))
  5242. (save-excursion
  5243. (beginning-of-line 2)
  5244. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5245. ; XEmacs does not have `next-single-char-property-change'
  5246. ; I'm not sure about Emacs 21.
  5247. (while (and (not (eobp)) ;; this is like `next-line'
  5248. (get-char-property (1- (point)) 'invisible))
  5249. (beginning-of-line 2))
  5250. (while (and (not (eobp)) ;; this is like `next-line'
  5251. (get-char-property (1- (point)) 'invisible))
  5252. (goto-char (next-single-char-property-change (point) 'invisible))
  5253. (and (eolp) (beginning-of-line 2))))
  5254. (setq eol (point)))
  5255. (outline-end-of-heading) (setq eoh (point))
  5256. (save-excursion
  5257. (outline-next-heading)
  5258. (setq has-children (and (org-at-heading-p t)
  5259. (> (funcall outline-level) level))))
  5260. ;; if we're in a list, org-end-of-subtree is in fact org-end-of-item.
  5261. (if (org-at-item-p)
  5262. (setq eos (if (and (org-end-of-item) (bolp))
  5263. (1- (point))
  5264. (point)))
  5265. (org-end-of-subtree t)
  5266. (unless (eobp)
  5267. (skip-chars-forward " \t\n"))
  5268. (setq eos (if (eobp) (point) (1- (point))))))
  5269. ;; Find out what to do next and set `this-command'
  5270. (cond
  5271. ((= eos eoh)
  5272. ;; Nothing is hidden behind this heading
  5273. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5274. (message "EMPTY ENTRY")
  5275. (setq org-cycle-subtree-status nil)
  5276. (save-excursion
  5277. (goto-char eos)
  5278. (outline-next-heading)
  5279. (if (org-invisible-p) (org-flag-heading nil))))
  5280. ((and (or (>= eol eos)
  5281. (not (string-match "\\S-" (buffer-substring eol eos))))
  5282. (or has-children
  5283. (not (setq children-skipped
  5284. org-cycle-skip-children-state-if-no-children))))
  5285. ;; Entire subtree is hidden in one line: children view
  5286. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5287. (org-show-entry)
  5288. (show-children)
  5289. (message "CHILDREN")
  5290. (save-excursion
  5291. (goto-char eos)
  5292. (outline-next-heading)
  5293. (if (org-invisible-p) (org-flag-heading nil)))
  5294. (setq org-cycle-subtree-status 'children)
  5295. (run-hook-with-args 'org-cycle-hook 'children))
  5296. ((or children-skipped
  5297. (and (eq last-command this-command)
  5298. (eq org-cycle-subtree-status 'children)))
  5299. ;; We just showed the children, or no children are there,
  5300. ;; now show everything.
  5301. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5302. (outline-flag-region eoh eos nil)
  5303. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5304. (setq org-cycle-subtree-status 'subtree)
  5305. (run-hook-with-args 'org-cycle-hook 'subtree))
  5306. (t
  5307. ;; Default action: hide the subtree.
  5308. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5309. (outline-flag-region eoh eos t)
  5310. (message "FOLDED")
  5311. (setq org-cycle-subtree-status 'folded)
  5312. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5313. ;;;###autoload
  5314. (defun org-global-cycle (&optional arg)
  5315. "Cycle the global visibility. For details see `org-cycle'.
  5316. With \\[universal-argument] prefix arg, switch to startup visibility.
  5317. With a numeric prefix, show all headlines up to that level."
  5318. (interactive "P")
  5319. (let ((org-cycle-include-plain-lists
  5320. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5321. (cond
  5322. ((integerp arg)
  5323. (show-all)
  5324. (hide-sublevels arg)
  5325. (setq org-cycle-global-status 'contents))
  5326. ((equal arg '(4))
  5327. (org-set-startup-visibility)
  5328. (message "Startup visibility, plus VISIBILITY properties."))
  5329. (t
  5330. (org-cycle '(4))))))
  5331. (defun org-set-startup-visibility ()
  5332. "Set the visibility required by startup options and properties."
  5333. (cond
  5334. ((eq org-startup-folded t)
  5335. (org-cycle '(4)))
  5336. ((eq org-startup-folded 'content)
  5337. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5338. (org-cycle '(4)) (org-cycle '(4)))))
  5339. (unless (eq org-startup-folded 'showeverything)
  5340. (if org-hide-block-startup (org-hide-block-all))
  5341. (org-set-visibility-according-to-property 'no-cleanup)
  5342. (org-cycle-hide-archived-subtrees 'all)
  5343. (org-cycle-hide-drawers 'all)
  5344. (org-cycle-show-empty-lines t)))
  5345. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5346. "Switch subtree visibilities according to :VISIBILITY: property."
  5347. (interactive)
  5348. (let (org-show-entry-below state)
  5349. (save-excursion
  5350. (goto-char (point-max))
  5351. (while (re-search-backward
  5352. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5353. nil t)
  5354. (setq state (match-string 1))
  5355. (save-excursion
  5356. (org-back-to-heading t)
  5357. (hide-subtree)
  5358. (org-reveal)
  5359. (cond
  5360. ((equal state '("fold" "folded"))
  5361. (hide-subtree))
  5362. ((equal state "children")
  5363. (org-show-hidden-entry)
  5364. (show-children))
  5365. ((equal state "content")
  5366. (save-excursion
  5367. (save-restriction
  5368. (org-narrow-to-subtree)
  5369. (org-content))))
  5370. ((member state '("all" "showall"))
  5371. (show-subtree)))))
  5372. (unless no-cleanup
  5373. (org-cycle-hide-archived-subtrees 'all)
  5374. (org-cycle-hide-drawers 'all)
  5375. (org-cycle-show-empty-lines 'all)))))
  5376. (defun org-overview ()
  5377. "Switch to overview mode, showing only top-level headlines.
  5378. Really, this shows all headlines with level equal or greater than the level
  5379. of the first headline in the buffer. This is important, because if the
  5380. first headline is not level one, then (hide-sublevels 1) gives confusing
  5381. results."
  5382. (interactive)
  5383. (let ((level (save-excursion
  5384. (goto-char (point-min))
  5385. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5386. (progn
  5387. (goto-char (match-beginning 0))
  5388. (funcall outline-level))))))
  5389. (and level (hide-sublevels level))))
  5390. (defun org-content (&optional arg)
  5391. "Show all headlines in the buffer, like a table of contents.
  5392. With numerical argument N, show content up to level N."
  5393. (interactive "P")
  5394. (save-excursion
  5395. ;; Visit all headings and show their offspring
  5396. (and (integerp arg) (org-overview))
  5397. (goto-char (point-max))
  5398. (catch 'exit
  5399. (while (and (progn (condition-case nil
  5400. (outline-previous-visible-heading 1)
  5401. (error (goto-char (point-min))))
  5402. t)
  5403. (looking-at outline-regexp))
  5404. (if (integerp arg)
  5405. (show-children (1- arg))
  5406. (show-branches))
  5407. (if (bobp) (throw 'exit nil))))))
  5408. (defun org-optimize-window-after-visibility-change (state)
  5409. "Adjust the window after a change in outline visibility.
  5410. This function is the default value of the hook `org-cycle-hook'."
  5411. (when (get-buffer-window (current-buffer))
  5412. (cond
  5413. ((eq state 'content) nil)
  5414. ((eq state 'all) nil)
  5415. ((eq state 'folded) nil)
  5416. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5417. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5418. (defun org-remove-empty-overlays-at (pos)
  5419. "Remove outline overlays that do not contain non-white stuff."
  5420. (mapc
  5421. (lambda (o)
  5422. (and (eq 'outline (overlay-get o 'invisible))
  5423. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5424. (overlay-end o))))
  5425. (delete-overlay o)))
  5426. (overlays-at pos)))
  5427. (defun org-clean-visibility-after-subtree-move ()
  5428. "Fix visibility issues after moving a subtree."
  5429. ;; First, find a reasonable region to look at:
  5430. ;; Start two siblings above, end three below
  5431. (let* ((beg (save-excursion
  5432. (and (org-get-last-sibling)
  5433. (org-get-last-sibling))
  5434. (point)))
  5435. (end (save-excursion
  5436. (and (org-get-next-sibling)
  5437. (org-get-next-sibling)
  5438. (org-get-next-sibling))
  5439. (if (org-at-heading-p)
  5440. (point-at-eol)
  5441. (point))))
  5442. (level (looking-at "\\*+"))
  5443. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5444. (save-excursion
  5445. (save-restriction
  5446. (narrow-to-region beg end)
  5447. (when re
  5448. ;; Properly fold already folded siblings
  5449. (goto-char (point-min))
  5450. (while (re-search-forward re nil t)
  5451. (if (and (not (org-invisible-p))
  5452. (save-excursion
  5453. (goto-char (point-at-eol)) (org-invisible-p)))
  5454. (hide-entry))))
  5455. (org-cycle-show-empty-lines 'overview)
  5456. (org-cycle-hide-drawers 'overview)))))
  5457. (defun org-cycle-show-empty-lines (state)
  5458. "Show empty lines above all visible headlines.
  5459. The region to be covered depends on STATE when called through
  5460. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5461. entire buffer covered. Note that an empty line is only shown if there
  5462. are at least `org-cycle-separator-lines' empty lines before the headline."
  5463. (when (not (= org-cycle-separator-lines 0))
  5464. (save-excursion
  5465. (let* ((n (abs org-cycle-separator-lines))
  5466. (re (cond
  5467. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5468. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5469. (t (let ((ns (number-to-string (- n 2))))
  5470. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5471. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5472. beg end b e)
  5473. (cond
  5474. ((memq state '(overview contents t))
  5475. (setq beg (point-min) end (point-max)))
  5476. ((memq state '(children folded))
  5477. (setq beg (point) end (progn (org-end-of-subtree t t)
  5478. (beginning-of-line 2)
  5479. (point)))))
  5480. (when beg
  5481. (goto-char beg)
  5482. (while (re-search-forward re end t)
  5483. (unless (get-char-property (match-end 1) 'invisible)
  5484. (setq e (match-end 1))
  5485. (if (< org-cycle-separator-lines 0)
  5486. (setq b (save-excursion
  5487. (goto-char (match-beginning 0))
  5488. (org-back-over-empty-lines)
  5489. (if (save-excursion
  5490. (goto-char (max (point-min) (1- (point))))
  5491. (org-on-heading-p))
  5492. (1- (point))
  5493. (point))))
  5494. (setq b (match-beginning 1)))
  5495. (outline-flag-region b e nil)))))))
  5496. ;; Never hide empty lines at the end of the file.
  5497. (save-excursion
  5498. (goto-char (point-max))
  5499. (outline-previous-heading)
  5500. (outline-end-of-heading)
  5501. (if (and (looking-at "[ \t\n]+")
  5502. (= (match-end 0) (point-max)))
  5503. (outline-flag-region (point) (match-end 0) nil))))
  5504. (defun org-show-empty-lines-in-parent ()
  5505. "Move to the parent and re-show empty lines before visible headlines."
  5506. (save-excursion
  5507. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5508. (org-cycle-show-empty-lines context))))
  5509. (defun org-files-list ()
  5510. "Return `org-agenda-files' list, plus all open org-mode files.
  5511. This is useful for operations that need to scan all of a user's
  5512. open and agenda-wise Org files."
  5513. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5514. (dolist (buf (buffer-list))
  5515. (with-current-buffer buf
  5516. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5517. (let ((file (expand-file-name (buffer-file-name))))
  5518. (unless (member file files)
  5519. (push file files))))))
  5520. files))
  5521. (defsubst org-entry-beginning-position ()
  5522. "Return the beginning position of the current entry."
  5523. (save-excursion (outline-back-to-heading t) (point)))
  5524. (defsubst org-entry-end-position ()
  5525. "Return the end position of the current entry."
  5526. (save-excursion (outline-next-heading) (point)))
  5527. (defun org-cycle-hide-drawers (state)
  5528. "Re-hide all drawers after a visibility state change."
  5529. (when (and (org-mode-p)
  5530. (not (memq state '(overview folded contents))))
  5531. (save-excursion
  5532. (let* ((globalp (memq state '(contents all)))
  5533. (beg (if globalp (point-min) (point)))
  5534. (end (if globalp (point-max)
  5535. (if (eq state 'children)
  5536. (save-excursion (outline-next-heading) (point))
  5537. (org-end-of-subtree t)))))
  5538. (goto-char beg)
  5539. (while (re-search-forward org-drawer-regexp end t)
  5540. (org-flag-drawer t))))))
  5541. (defun org-flag-drawer (flag)
  5542. (save-excursion
  5543. (beginning-of-line 1)
  5544. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5545. (let ((b (match-end 0))
  5546. (outline-regexp org-outline-regexp))
  5547. (if (re-search-forward
  5548. "^[ \t]*:END:"
  5549. (save-excursion (outline-next-heading) (point)) t)
  5550. (outline-flag-region b (point-at-eol) flag)
  5551. (error ":END: line missing at position %s" b))))))
  5552. (defun org-subtree-end-visible-p ()
  5553. "Is the end of the current subtree visible?"
  5554. (pos-visible-in-window-p
  5555. (save-excursion (org-end-of-subtree t) (point))))
  5556. (defun org-first-headline-recenter (&optional N)
  5557. "Move cursor to the first headline and recenter the headline.
  5558. Optional argument N means put the headline into the Nth line of the window."
  5559. (goto-char (point-min))
  5560. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5561. (beginning-of-line)
  5562. (recenter (prefix-numeric-value N))))
  5563. ;;; Saving and restoring visibility
  5564. (defun org-outline-overlay-data (&optional use-markers)
  5565. "Return a list of the locations of all outline overlays.
  5566. The are overlays with the `invisible' property value `outline'.
  5567. The return values is a list of cons cells, with start and stop
  5568. positions for each overlay.
  5569. If USE-MARKERS is set, return the positions as markers."
  5570. (let (beg end)
  5571. (save-excursion
  5572. (save-restriction
  5573. (widen)
  5574. (delq nil
  5575. (mapcar (lambda (o)
  5576. (when (eq (overlay-get o 'invisible) 'outline)
  5577. (setq beg (overlay-start o)
  5578. end (overlay-end o))
  5579. (and beg end (> end beg)
  5580. (if use-markers
  5581. (cons (move-marker (make-marker) beg)
  5582. (move-marker (make-marker) end))
  5583. (cons beg end)))))
  5584. (overlays-in (point-min) (point-max))))))))
  5585. (defun org-set-outline-overlay-data (data)
  5586. "Create visibility overlays for all positions in DATA.
  5587. DATA should have been made by `org-outline-overlay-data'."
  5588. (let (o)
  5589. (save-excursion
  5590. (save-restriction
  5591. (widen)
  5592. (show-all)
  5593. (mapc (lambda (c)
  5594. (setq o (make-overlay (car c) (cdr c)))
  5595. (overlay-put o 'invisible 'outline))
  5596. data)))))
  5597. ;;; Folding of blocks
  5598. (defconst org-block-regexp
  5599. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5600. "Regular expression for hiding blocks.")
  5601. (defvar org-hide-block-overlays nil
  5602. "Overlays hiding blocks.")
  5603. (make-variable-buffer-local 'org-hide-block-overlays)
  5604. (defun org-block-map (function &optional start end)
  5605. "Call FUNCTION at the head of all source blocks in the current buffer.
  5606. Optional arguments START and END can be used to limit the range."
  5607. (let ((start (or start (point-min)))
  5608. (end (or end (point-max))))
  5609. (save-excursion
  5610. (goto-char start)
  5611. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5612. (save-excursion
  5613. (save-match-data
  5614. (goto-char (match-beginning 0))
  5615. (funcall function)))))))
  5616. (defun org-hide-block-toggle-all ()
  5617. "Toggle the visibility of all blocks in the current buffer."
  5618. (org-block-map #'org-hide-block-toggle))
  5619. (defun org-hide-block-all ()
  5620. "Fold all blocks in the current buffer."
  5621. (interactive)
  5622. (org-show-block-all)
  5623. (org-block-map #'org-hide-block-toggle-maybe))
  5624. (defun org-show-block-all ()
  5625. "Unfold all blocks in the current buffer."
  5626. (interactive)
  5627. (mapc 'delete-overlay org-hide-block-overlays)
  5628. (setq org-hide-block-overlays nil))
  5629. (defun org-hide-block-toggle-maybe ()
  5630. "Toggle visibility of block at point."
  5631. (interactive)
  5632. (let ((case-fold-search t))
  5633. (if (save-excursion
  5634. (beginning-of-line 1)
  5635. (looking-at org-block-regexp))
  5636. (progn (org-hide-block-toggle)
  5637. t) ;; to signal that we took action
  5638. nil))) ;; to signal that we did not
  5639. (defun org-hide-block-toggle (&optional force)
  5640. "Toggle the visibility of the current block."
  5641. (interactive)
  5642. (save-excursion
  5643. (beginning-of-line)
  5644. (if (re-search-forward org-block-regexp nil t)
  5645. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5646. (end (match-end 0)) ;; end of entire body
  5647. ov)
  5648. (if (memq t (mapcar (lambda (overlay)
  5649. (eq (overlay-get overlay 'invisible)
  5650. 'org-hide-block))
  5651. (overlays-at start)))
  5652. (if (or (not force) (eq force 'off))
  5653. (mapc (lambda (ov)
  5654. (when (member ov org-hide-block-overlays)
  5655. (setq org-hide-block-overlays
  5656. (delq ov org-hide-block-overlays)))
  5657. (when (eq (overlay-get ov 'invisible)
  5658. 'org-hide-block)
  5659. (delete-overlay ov)))
  5660. (overlays-at start)))
  5661. (setq ov (make-overlay start end))
  5662. (overlay-put ov 'invisible 'org-hide-block)
  5663. ;; make the block accessible to isearch
  5664. (overlay-put
  5665. ov 'isearch-open-invisible
  5666. (lambda (ov)
  5667. (when (member ov org-hide-block-overlays)
  5668. (setq org-hide-block-overlays
  5669. (delq ov org-hide-block-overlays)))
  5670. (when (eq (overlay-get ov 'invisible)
  5671. 'org-hide-block)
  5672. (delete-overlay ov))))
  5673. (push ov org-hide-block-overlays)))
  5674. (error "Not looking at a source block"))))
  5675. ;; org-tab-after-check-for-cycling-hook
  5676. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5677. ;; Remove overlays when changing major mode
  5678. (add-hook 'org-mode-hook
  5679. (lambda () (org-add-hook 'change-major-mode-hook
  5680. 'org-show-block-all 'append 'local)))
  5681. ;;; Org-goto
  5682. (defvar org-goto-window-configuration nil)
  5683. (defvar org-goto-marker nil)
  5684. (defvar org-goto-map
  5685. (let ((map (make-sparse-keymap)))
  5686. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5687. (while (setq cmd (pop cmds))
  5688. (substitute-key-definition cmd cmd map global-map)))
  5689. (suppress-keymap map)
  5690. (org-defkey map "\C-m" 'org-goto-ret)
  5691. (org-defkey map [(return)] 'org-goto-ret)
  5692. (org-defkey map [(left)] 'org-goto-left)
  5693. (org-defkey map [(right)] 'org-goto-right)
  5694. (org-defkey map [(control ?g)] 'org-goto-quit)
  5695. (org-defkey map "\C-i" 'org-cycle)
  5696. (org-defkey map [(tab)] 'org-cycle)
  5697. (org-defkey map [(down)] 'outline-next-visible-heading)
  5698. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5699. (if org-goto-auto-isearch
  5700. (if (fboundp 'define-key-after)
  5701. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5702. nil)
  5703. (org-defkey map "q" 'org-goto-quit)
  5704. (org-defkey map "n" 'outline-next-visible-heading)
  5705. (org-defkey map "p" 'outline-previous-visible-heading)
  5706. (org-defkey map "f" 'outline-forward-same-level)
  5707. (org-defkey map "b" 'outline-backward-same-level)
  5708. (org-defkey map "u" 'outline-up-heading))
  5709. (org-defkey map "/" 'org-occur)
  5710. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5711. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5712. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5713. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5714. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5715. map))
  5716. (defconst org-goto-help
  5717. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5718. RET=jump to location [Q]uit and return to previous location
  5719. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5720. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5721. ;; FIXME: Docstring does not mention both interfaces
  5722. (defun org-goto (&optional alternative-interface)
  5723. "Look up a different location in the current file, keeping current visibility.
  5724. When you want look-up or go to a different location in a document, the
  5725. fastest way is often to fold the entire buffer and then dive into the tree.
  5726. This method has the disadvantage, that the previous location will be folded,
  5727. which may not be what you want.
  5728. This command works around this by showing a copy of the current buffer
  5729. in an indirect buffer, in overview mode. You can dive into the tree in
  5730. that copy, use org-occur and incremental search to find a location.
  5731. When pressing RET or `Q', the command returns to the original buffer in
  5732. which the visibility is still unchanged. After RET is will also jump to
  5733. the location selected in the indirect buffer and expose the
  5734. the headline hierarchy above."
  5735. (interactive "P")
  5736. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5737. (org-refile-use-outline-path t)
  5738. (org-refile-target-verify-function nil)
  5739. (interface
  5740. (if (not alternative-interface)
  5741. org-goto-interface
  5742. (if (eq org-goto-interface 'outline)
  5743. 'outline-path-completion
  5744. 'outline)))
  5745. (org-goto-start-pos (point))
  5746. (selected-point
  5747. (if (eq interface 'outline)
  5748. (car (org-get-location (current-buffer) org-goto-help))
  5749. (let ((pa (org-refile-get-location "Goto: ")))
  5750. (org-refile-check-position pa)
  5751. (nth 3 pa)))))
  5752. (if selected-point
  5753. (progn
  5754. (org-mark-ring-push org-goto-start-pos)
  5755. (goto-char selected-point)
  5756. (if (or (org-invisible-p) (org-invisible-p2))
  5757. (org-show-context 'org-goto)))
  5758. (message "Quit"))))
  5759. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5760. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5761. (defvar org-goto-local-auto-isearch-map) ; defined below
  5762. (defun org-get-location (buf help)
  5763. "Let the user select a location in the Org-mode buffer BUF.
  5764. This function uses a recursive edit. It returns the selected position
  5765. or nil."
  5766. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5767. (isearch-hide-immediately nil)
  5768. (isearch-search-fun-function
  5769. (lambda () 'org-goto-local-search-headings))
  5770. (org-goto-selected-point org-goto-exit-command)
  5771. (pop-up-frames nil)
  5772. (special-display-buffer-names nil)
  5773. (special-display-regexps nil)
  5774. (special-display-function nil))
  5775. (save-excursion
  5776. (save-window-excursion
  5777. (delete-other-windows)
  5778. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5779. (switch-to-buffer
  5780. (condition-case nil
  5781. (make-indirect-buffer (current-buffer) "*org-goto*")
  5782. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5783. (with-output-to-temp-buffer "*Help*"
  5784. (princ help))
  5785. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5786. (setq buffer-read-only nil)
  5787. (let ((org-startup-truncated t)
  5788. (org-startup-folded nil)
  5789. (org-startup-align-all-tables nil))
  5790. (org-mode)
  5791. (org-overview))
  5792. (setq buffer-read-only t)
  5793. (if (and (boundp 'org-goto-start-pos)
  5794. (integer-or-marker-p org-goto-start-pos))
  5795. (let ((org-show-hierarchy-above t)
  5796. (org-show-siblings t)
  5797. (org-show-following-heading t))
  5798. (goto-char org-goto-start-pos)
  5799. (and (org-invisible-p) (org-show-context)))
  5800. (goto-char (point-min)))
  5801. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5802. (message "Select location and press RET")
  5803. (use-local-map org-goto-map)
  5804. (recursive-edit)
  5805. ))
  5806. (kill-buffer "*org-goto*")
  5807. (cons org-goto-selected-point org-goto-exit-command)))
  5808. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5809. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5810. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5811. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5812. (defun org-goto-local-search-headings (string bound noerror)
  5813. "Search and make sure that any matches are in headlines."
  5814. (catch 'return
  5815. (while (if isearch-forward
  5816. (search-forward string bound noerror)
  5817. (search-backward string bound noerror))
  5818. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5819. (and (member :headline context)
  5820. (not (member :tags context))))
  5821. (throw 'return (point))))))
  5822. (defun org-goto-local-auto-isearch ()
  5823. "Start isearch."
  5824. (interactive)
  5825. (goto-char (point-min))
  5826. (let ((keys (this-command-keys)))
  5827. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5828. (isearch-mode t)
  5829. (isearch-process-search-char (string-to-char keys)))))
  5830. (defun org-goto-ret (&optional arg)
  5831. "Finish `org-goto' by going to the new location."
  5832. (interactive "P")
  5833. (setq org-goto-selected-point (point)
  5834. org-goto-exit-command 'return)
  5835. (throw 'exit nil))
  5836. (defun org-goto-left ()
  5837. "Finish `org-goto' by going to the new location."
  5838. (interactive)
  5839. (if (org-on-heading-p)
  5840. (progn
  5841. (beginning-of-line 1)
  5842. (setq org-goto-selected-point (point)
  5843. org-goto-exit-command 'left)
  5844. (throw 'exit nil))
  5845. (error "Not on a heading")))
  5846. (defun org-goto-right ()
  5847. "Finish `org-goto' by going to the new location."
  5848. (interactive)
  5849. (if (org-on-heading-p)
  5850. (progn
  5851. (setq org-goto-selected-point (point)
  5852. org-goto-exit-command 'right)
  5853. (throw 'exit nil))
  5854. (error "Not on a heading")))
  5855. (defun org-goto-quit ()
  5856. "Finish `org-goto' without cursor motion."
  5857. (interactive)
  5858. (setq org-goto-selected-point nil)
  5859. (setq org-goto-exit-command 'quit)
  5860. (throw 'exit nil))
  5861. ;;; Indirect buffer display of subtrees
  5862. (defvar org-indirect-dedicated-frame nil
  5863. "This is the frame being used for indirect tree display.")
  5864. (defvar org-last-indirect-buffer nil)
  5865. (defun org-tree-to-indirect-buffer (&optional arg)
  5866. "Create indirect buffer and narrow it to current subtree.
  5867. With numerical prefix ARG, go up to this level and then take that tree.
  5868. If ARG is negative, go up that many levels.
  5869. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5870. indirect buffer previously made with this command, to avoid proliferation of
  5871. indirect buffers. However, when you call the command with a \
  5872. \\[universal-argument] prefix, or
  5873. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5874. is kept so that you can work with several indirect buffers at the same time.
  5875. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5876. \\[universal-argument] prefix also
  5877. requests that a new frame be made for the new buffer, so that the dedicated
  5878. frame is not changed."
  5879. (interactive "P")
  5880. (let ((cbuf (current-buffer))
  5881. (cwin (selected-window))
  5882. (pos (point))
  5883. beg end level heading ibuf)
  5884. (save-excursion
  5885. (org-back-to-heading t)
  5886. (when (numberp arg)
  5887. (setq level (org-outline-level))
  5888. (if (< arg 0) (setq arg (+ level arg)))
  5889. (while (> (setq level (org-outline-level)) arg)
  5890. (outline-up-heading 1 t)))
  5891. (setq beg (point)
  5892. heading (org-get-heading))
  5893. (org-end-of-subtree t t)
  5894. (if (org-on-heading-p) (backward-char 1))
  5895. (setq end (point)))
  5896. (if (and (buffer-live-p org-last-indirect-buffer)
  5897. (not (eq org-indirect-buffer-display 'new-frame))
  5898. (not arg))
  5899. (kill-buffer org-last-indirect-buffer))
  5900. (setq ibuf (org-get-indirect-buffer cbuf)
  5901. org-last-indirect-buffer ibuf)
  5902. (cond
  5903. ((or (eq org-indirect-buffer-display 'new-frame)
  5904. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5905. (select-frame (make-frame))
  5906. (delete-other-windows)
  5907. (switch-to-buffer ibuf)
  5908. (org-set-frame-title heading))
  5909. ((eq org-indirect-buffer-display 'dedicated-frame)
  5910. (raise-frame
  5911. (select-frame (or (and org-indirect-dedicated-frame
  5912. (frame-live-p org-indirect-dedicated-frame)
  5913. org-indirect-dedicated-frame)
  5914. (setq org-indirect-dedicated-frame (make-frame)))))
  5915. (delete-other-windows)
  5916. (switch-to-buffer ibuf)
  5917. (org-set-frame-title (concat "Indirect: " heading)))
  5918. ((eq org-indirect-buffer-display 'current-window)
  5919. (switch-to-buffer ibuf))
  5920. ((eq org-indirect-buffer-display 'other-window)
  5921. (pop-to-buffer ibuf))
  5922. (t (error "Invalid value")))
  5923. (if (featurep 'xemacs)
  5924. (save-excursion (org-mode) (turn-on-font-lock)))
  5925. (narrow-to-region beg end)
  5926. (show-all)
  5927. (goto-char pos)
  5928. (and (window-live-p cwin) (select-window cwin))))
  5929. (defun org-get-indirect-buffer (&optional buffer)
  5930. (setq buffer (or buffer (current-buffer)))
  5931. (let ((n 1) (base (buffer-name buffer)) bname)
  5932. (while (buffer-live-p
  5933. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5934. (setq n (1+ n)))
  5935. (condition-case nil
  5936. (make-indirect-buffer buffer bname 'clone)
  5937. (error (make-indirect-buffer buffer bname)))))
  5938. (defun org-set-frame-title (title)
  5939. "Set the title of the current frame to the string TITLE."
  5940. ;; FIXME: how to name a single frame in XEmacs???
  5941. (unless (featurep 'xemacs)
  5942. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5943. ;;;; Structure editing
  5944. ;;; Inserting headlines
  5945. (defun org-previous-line-empty-p ()
  5946. (save-excursion
  5947. (and (not (bobp))
  5948. (or (beginning-of-line 0) t)
  5949. (save-match-data
  5950. (looking-at "[ \t]*$")))))
  5951. (defun org-insert-heading (&optional force-heading invisible-ok)
  5952. "Insert a new heading or item with same depth at point.
  5953. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5954. If point is at the beginning of a headline, insert a sibling before the
  5955. current headline. If point is not at the beginning, do not split the line,
  5956. but create the new headline after the current line.
  5957. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5958. This is important for non-interactive uses of the command."
  5959. (interactive "P")
  5960. (if (or (= (buffer-size) 0)
  5961. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5962. (org-on-heading-p))))
  5963. (not (org-in-item-p))))
  5964. (insert "\n* ")
  5965. (when (or force-heading (not (org-insert-item)))
  5966. (let* ((empty-line-p nil)
  5967. (head (save-excursion
  5968. (condition-case nil
  5969. (progn
  5970. (org-back-to-heading invisible-ok)
  5971. (setq empty-line-p (org-previous-line-empty-p))
  5972. (match-string 0))
  5973. (error "*"))))
  5974. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5975. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5976. pos hide-previous previous-pos)
  5977. (cond
  5978. ((and (org-on-heading-p) (bolp)
  5979. (or (bobp)
  5980. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5981. ;; insert before the current line
  5982. (open-line (if blank 2 1)))
  5983. ((and (bolp)
  5984. (not org-insert-heading-respect-content)
  5985. (or (bobp)
  5986. (save-excursion
  5987. (backward-char 1) (not (org-invisible-p)))))
  5988. ;; insert right here
  5989. nil)
  5990. (t
  5991. ;; somewhere in the line
  5992. (save-excursion
  5993. (setq previous-pos (point-at-bol))
  5994. (end-of-line)
  5995. (setq hide-previous (org-invisible-p)))
  5996. (and org-insert-heading-respect-content (org-show-subtree))
  5997. (let ((split
  5998. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5999. (save-excursion
  6000. (let ((p (point)))
  6001. (goto-char (point-at-bol))
  6002. (and (looking-at org-complex-heading-regexp)
  6003. (> p (match-beginning 4)))))))
  6004. tags pos)
  6005. (cond
  6006. (org-insert-heading-respect-content
  6007. (org-end-of-subtree nil t)
  6008. (or (bolp) (newline))
  6009. (or (org-previous-line-empty-p)
  6010. (and blank (newline)))
  6011. (open-line 1))
  6012. ((org-on-heading-p)
  6013. (when hide-previous
  6014. (show-children)
  6015. (org-show-entry))
  6016. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6017. (setq tags (and (match-end 2) (match-string 2)))
  6018. (and (match-end 1)
  6019. (delete-region (match-beginning 1) (match-end 1)))
  6020. (setq pos (point-at-bol))
  6021. (or split (end-of-line 1))
  6022. (delete-horizontal-space)
  6023. (if (string-match "\\`\\*+\\'"
  6024. (buffer-substring (point-at-bol) (point)))
  6025. (insert " "))
  6026. (newline (if blank 2 1))
  6027. (when tags
  6028. (save-excursion
  6029. (goto-char pos)
  6030. (end-of-line 1)
  6031. (insert " " tags)
  6032. (org-set-tags nil 'align))))
  6033. (t
  6034. (or split (end-of-line 1))
  6035. (newline (if blank 2 1)))))))
  6036. (insert head) (just-one-space)
  6037. (setq pos (point))
  6038. (end-of-line 1)
  6039. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6040. (when (and org-insert-heading-respect-content hide-previous)
  6041. (save-excursion
  6042. (goto-char previous-pos)
  6043. (hide-subtree)))
  6044. (run-hooks 'org-insert-heading-hook)))))
  6045. (defun org-get-heading (&optional no-tags)
  6046. "Return the heading of the current entry, without the stars."
  6047. (save-excursion
  6048. (org-back-to-heading t)
  6049. (if (looking-at
  6050. (if no-tags
  6051. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6052. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6053. (match-string 1) "")))
  6054. (defun org-heading-components ()
  6055. "Return the components of the current heading.
  6056. This is a list with the following elements:
  6057. - the level as an integer
  6058. - the reduced level, different if `org-odd-levels-only' is set.
  6059. - the TODO keyword, or nil
  6060. - the priority character, like ?A, or nil if no priority is given
  6061. - the headline text itself, or the tags string if no headline text
  6062. - the tags string, or nil."
  6063. (save-excursion
  6064. (org-back-to-heading t)
  6065. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6066. (list (length (match-string 1))
  6067. (org-reduced-level (length (match-string 1)))
  6068. (org-match-string-no-properties 2)
  6069. (and (match-end 3) (aref (match-string 3) 2))
  6070. (org-match-string-no-properties 4)
  6071. (org-match-string-no-properties 5)))))
  6072. (defun org-get-entry ()
  6073. "Get the entry text, after heading, entire subtree."
  6074. (save-excursion
  6075. (org-back-to-heading t)
  6076. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6077. (defun org-insert-heading-after-current ()
  6078. "Insert a new heading with same level as current, after current subtree."
  6079. (interactive)
  6080. (org-back-to-heading)
  6081. (org-insert-heading)
  6082. (org-move-subtree-down)
  6083. (end-of-line 1))
  6084. (defun org-insert-heading-respect-content ()
  6085. (interactive)
  6086. (let ((org-insert-heading-respect-content t))
  6087. (org-insert-heading t)))
  6088. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6089. (interactive "P")
  6090. (let ((org-insert-heading-respect-content t))
  6091. (org-insert-todo-heading force-state t)))
  6092. (defun org-insert-todo-heading (arg &optional force-heading)
  6093. "Insert a new heading with the same level and TODO state as current heading.
  6094. If the heading has no TODO state, or if the state is DONE, use the first
  6095. state (TODO by default). Also with prefix arg, force first state."
  6096. (interactive "P")
  6097. (when (or force-heading (not (org-insert-item 'checkbox)))
  6098. (org-insert-heading force-heading)
  6099. (save-excursion
  6100. (org-back-to-heading)
  6101. (outline-previous-heading)
  6102. (looking-at org-todo-line-regexp))
  6103. (let*
  6104. ((new-mark-x
  6105. (if (or arg
  6106. (not (match-beginning 2))
  6107. (member (match-string 2) org-done-keywords))
  6108. (car org-todo-keywords-1)
  6109. (match-string 2)))
  6110. (new-mark
  6111. (or
  6112. (run-hook-with-args-until-success
  6113. 'org-todo-get-default-hook new-mark-x nil)
  6114. new-mark-x)))
  6115. (beginning-of-line 1)
  6116. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6117. (if org-treat-insert-todo-heading-as-state-change
  6118. (org-todo new-mark)
  6119. (insert new-mark " "))))
  6120. (when org-provide-todo-statistics
  6121. (org-update-parent-todo-statistics))))
  6122. (defun org-insert-subheading (arg)
  6123. "Insert a new subheading and demote it.
  6124. Works for outline headings and for plain lists alike."
  6125. (interactive "P")
  6126. (org-insert-heading arg)
  6127. (cond
  6128. ((org-on-heading-p) (org-do-demote))
  6129. ((org-at-item-p) (org-indent-item))))
  6130. (defun org-insert-todo-subheading (arg)
  6131. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6132. Works for outline headings and for plain lists alike."
  6133. (interactive "P")
  6134. (org-insert-todo-heading arg)
  6135. (cond
  6136. ((org-on-heading-p) (org-do-demote))
  6137. ((org-at-item-p) (org-indent-item))))
  6138. ;;; Promotion and Demotion
  6139. (defvar org-after-demote-entry-hook nil
  6140. "Hook run after an entry has been demoted.
  6141. The cursor will be at the beginning of the entry.
  6142. When a subtree is being demoted, the hook will be called for each node.")
  6143. (defvar org-after-promote-entry-hook nil
  6144. "Hook run after an entry has been promoted.
  6145. The cursor will be at the beginning of the entry.
  6146. When a subtree is being promoted, the hook will be called for each node.")
  6147. (defun org-promote-subtree ()
  6148. "Promote the entire subtree.
  6149. See also `org-promote'."
  6150. (interactive)
  6151. (save-excursion
  6152. (org-map-tree 'org-promote))
  6153. (org-fix-position-after-promote))
  6154. (defun org-demote-subtree ()
  6155. "Demote the entire subtree. See `org-demote'.
  6156. See also `org-promote'."
  6157. (interactive)
  6158. (save-excursion
  6159. (org-map-tree 'org-demote))
  6160. (org-fix-position-after-promote))
  6161. (defun org-do-promote ()
  6162. "Promote the current heading higher up the tree.
  6163. If the region is active in `transient-mark-mode', promote all headings
  6164. in the region."
  6165. (interactive)
  6166. (save-excursion
  6167. (if (org-region-active-p)
  6168. (org-map-region 'org-promote (region-beginning) (region-end))
  6169. (org-promote)))
  6170. (org-fix-position-after-promote))
  6171. (defun org-do-demote ()
  6172. "Demote the current heading lower down the tree.
  6173. If the region is active in `transient-mark-mode', demote all headings
  6174. in the region."
  6175. (interactive)
  6176. (save-excursion
  6177. (if (org-region-active-p)
  6178. (org-map-region 'org-demote (region-beginning) (region-end))
  6179. (org-demote)))
  6180. (org-fix-position-after-promote))
  6181. (defun org-fix-position-after-promote ()
  6182. "Make sure that after pro/demotion cursor position is right."
  6183. (let ((pos (point)))
  6184. (when (save-excursion
  6185. (beginning-of-line 1)
  6186. (looking-at org-todo-line-regexp)
  6187. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6188. (cond ((eobp) (insert " "))
  6189. ((eolp) (insert " "))
  6190. ((equal (char-after) ?\ ) (forward-char 1))))))
  6191. (defun org-current-level ()
  6192. "Return the level of the current entry, or nil if before the first headline.
  6193. The level is the number of stars at the beginning of the headline."
  6194. (save-excursion
  6195. (condition-case nil
  6196. (progn
  6197. (org-back-to-heading t)
  6198. (funcall outline-level))
  6199. (error nil))))
  6200. (defun org-get-previous-line-level ()
  6201. "Return the outline depth of the last headline before the current line.
  6202. Returns 0 for the first headline in the buffer, and nil if before the
  6203. first headline."
  6204. (let ((current-level (org-current-level))
  6205. (prev-level (when (> (line-number-at-pos) 1)
  6206. (save-excursion
  6207. (beginning-of-line 0)
  6208. (org-current-level)))))
  6209. (cond ((null current-level) nil) ; Before first headline
  6210. ((null prev-level) 0) ; At first headline
  6211. (prev-level))))
  6212. (defun org-reduced-level (l)
  6213. "Compute the effective level of a heading.
  6214. This takes into account the setting of `org-odd-levels-only'."
  6215. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6216. (defun org-level-increment ()
  6217. "Return the number of stars that will be added or removed at a
  6218. time to headlines when structure editing, based on the value of
  6219. `org-odd-levels-only'."
  6220. (if org-odd-levels-only 2 1))
  6221. (defun org-get-valid-level (level &optional change)
  6222. "Rectify a level change under the influence of `org-odd-levels-only'
  6223. LEVEL is a current level, CHANGE is by how much the level should be
  6224. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6225. even level numbers will become the next higher odd number."
  6226. (if org-odd-levels-only
  6227. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6228. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6229. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6230. (max 1 (+ level (or change 0)))))
  6231. (if (boundp 'define-obsolete-function-alias)
  6232. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6233. (define-obsolete-function-alias 'org-get-legal-level
  6234. 'org-get-valid-level)
  6235. (define-obsolete-function-alias 'org-get-legal-level
  6236. 'org-get-valid-level "23.1")))
  6237. (defun org-promote ()
  6238. "Promote the current heading higher up the tree.
  6239. If the region is active in `transient-mark-mode', promote all headings
  6240. in the region."
  6241. (org-back-to-heading t)
  6242. (let* ((level (save-match-data (funcall outline-level)))
  6243. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6244. (diff (abs (- level (length up-head) -1))))
  6245. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6246. (replace-match up-head nil t)
  6247. ;; Fixup tag positioning
  6248. (and org-auto-align-tags (org-set-tags nil t))
  6249. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6250. (run-hooks 'org-after-promote-entry-hook)))
  6251. (defun org-demote ()
  6252. "Demote the current heading lower down the tree.
  6253. If the region is active in `transient-mark-mode', demote all headings
  6254. in the region."
  6255. (org-back-to-heading t)
  6256. (let* ((level (save-match-data (funcall outline-level)))
  6257. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6258. (diff (abs (- level (length down-head) -1))))
  6259. (replace-match down-head nil t)
  6260. ;; Fixup tag positioning
  6261. (and org-auto-align-tags (org-set-tags nil t))
  6262. (if org-adapt-indentation (org-fixup-indentation diff))
  6263. (run-hooks 'org-after-demote-entry-hook)))
  6264. (defun org-cycle-level ()
  6265. "Cycle the level of an empty headline through possible states.
  6266. This goes first to child, then to parent, level, then up the hierarchy.
  6267. After top level, it switches back to sibling level."
  6268. (interactive)
  6269. (let ((org-adapt-indentation nil))
  6270. (when (org-point-at-end-of-empty-headline)
  6271. (setq this-command 'org-cycle-level) ; Only needed for caching
  6272. (let ((cur-level (org-current-level))
  6273. (prev-level (org-get-previous-line-level)))
  6274. (cond
  6275. ;; If first headline in file, promote to top-level.
  6276. ((= prev-level 0)
  6277. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6278. do (org-do-promote)))
  6279. ;; If same level as prev, demote one.
  6280. ((= prev-level cur-level)
  6281. (org-do-demote))
  6282. ;; If parent is top-level, promote to top level if not already.
  6283. ((= prev-level 1)
  6284. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6285. do (org-do-promote)))
  6286. ;; If top-level, return to prev-level.
  6287. ((= cur-level 1)
  6288. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6289. do (org-do-demote)))
  6290. ;; If less than prev-level, promote one.
  6291. ((< cur-level prev-level)
  6292. (org-do-promote))
  6293. ;; If deeper than prev-level, promote until higher than
  6294. ;; prev-level.
  6295. ((> cur-level prev-level)
  6296. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6297. do (org-do-promote))))
  6298. t))))
  6299. (defun org-map-tree (fun)
  6300. "Call FUN for every heading underneath the current one."
  6301. (org-back-to-heading)
  6302. (let ((level (funcall outline-level)))
  6303. (save-excursion
  6304. (funcall fun)
  6305. (while (and (progn
  6306. (outline-next-heading)
  6307. (> (funcall outline-level) level))
  6308. (not (eobp)))
  6309. (funcall fun)))))
  6310. (defun org-map-region (fun beg end)
  6311. "Call FUN for every heading between BEG and END."
  6312. (let ((org-ignore-region t))
  6313. (save-excursion
  6314. (setq end (copy-marker end))
  6315. (goto-char beg)
  6316. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6317. (< (point) end))
  6318. (funcall fun))
  6319. (while (and (progn
  6320. (outline-next-heading)
  6321. (< (point) end))
  6322. (not (eobp)))
  6323. (funcall fun)))))
  6324. (defun org-fixup-indentation (diff)
  6325. "Change the indentation in the current entry by DIFF.
  6326. However, if any line in the current entry has no indentation, or if it
  6327. would end up with no indentation after the change, nothing at all is done."
  6328. (save-excursion
  6329. (let ((end (save-excursion (outline-next-heading)
  6330. (point-marker)))
  6331. (prohibit (if (> diff 0)
  6332. "^\\S-"
  6333. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6334. col)
  6335. (unless (save-excursion (end-of-line 1)
  6336. (re-search-forward prohibit end t))
  6337. (while (and (< (point) end)
  6338. (re-search-forward "^[ \t]+" end t))
  6339. (goto-char (match-end 0))
  6340. (setq col (current-column))
  6341. (if (< diff 0) (replace-match ""))
  6342. (org-indent-to-column (+ diff col))))
  6343. (move-marker end nil))))
  6344. (defun org-convert-to-odd-levels ()
  6345. "Convert an org-mode file with all levels allowed to one with odd levels.
  6346. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6347. level 5 etc."
  6348. (interactive)
  6349. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6350. (let ((outline-regexp org-outline-regexp)
  6351. (outline-level 'org-outline-level)
  6352. (org-odd-levels-only nil) n)
  6353. (save-excursion
  6354. (goto-char (point-min))
  6355. (while (re-search-forward "^\\*\\*+ " nil t)
  6356. (setq n (- (length (match-string 0)) 2))
  6357. (while (>= (setq n (1- n)) 0)
  6358. (org-demote))
  6359. (end-of-line 1))))))
  6360. (defun org-convert-to-oddeven-levels ()
  6361. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6362. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6363. file contains a section with an even level, conversion would
  6364. destroy the structure of the file. An error is signaled in this
  6365. case."
  6366. (interactive)
  6367. (goto-char (point-min))
  6368. ;; First check if there are no even levels
  6369. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6370. (org-show-context t)
  6371. (error "Not all levels are odd in this file. Conversion not possible"))
  6372. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6373. (let ((outline-regexp org-outline-regexp)
  6374. (outline-level 'org-outline-level)
  6375. (org-odd-levels-only nil) n)
  6376. (save-excursion
  6377. (goto-char (point-min))
  6378. (while (re-search-forward "^\\*\\*+ " nil t)
  6379. (setq n (/ (1- (length (match-string 0))) 2))
  6380. (while (>= (setq n (1- n)) 0)
  6381. (org-promote))
  6382. (end-of-line 1))))))
  6383. (defun org-tr-level (n)
  6384. "Make N odd if required."
  6385. (if org-odd-levels-only (1+ (/ n 2)) n))
  6386. ;;; Vertical tree motion, cutting and pasting of subtrees
  6387. (defun org-move-subtree-up (&optional arg)
  6388. "Move the current subtree up past ARG headlines of the same level."
  6389. (interactive "p")
  6390. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6391. (defun org-move-subtree-down (&optional arg)
  6392. "Move the current subtree down past ARG headlines of the same level."
  6393. (interactive "p")
  6394. (setq arg (prefix-numeric-value arg))
  6395. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6396. 'org-get-last-sibling))
  6397. (ins-point (make-marker))
  6398. (cnt (abs arg))
  6399. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6400. ;; Select the tree
  6401. (org-back-to-heading)
  6402. (setq beg0 (point))
  6403. (save-excursion
  6404. (setq ne-beg (org-back-over-empty-lines))
  6405. (setq beg (point)))
  6406. (save-match-data
  6407. (save-excursion (outline-end-of-heading)
  6408. (setq folded (org-invisible-p)))
  6409. (outline-end-of-subtree))
  6410. (outline-next-heading)
  6411. (setq ne-end (org-back-over-empty-lines))
  6412. (setq end (point))
  6413. (goto-char beg0)
  6414. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6415. ;; include less whitespace
  6416. (save-excursion
  6417. (goto-char beg)
  6418. (forward-line (- ne-beg ne-end))
  6419. (setq beg (point))))
  6420. ;; Find insertion point, with error handling
  6421. (while (> cnt 0)
  6422. (or (and (funcall movfunc) (looking-at outline-regexp))
  6423. (progn (goto-char beg0)
  6424. (error "Cannot move past superior level or buffer limit")))
  6425. (setq cnt (1- cnt)))
  6426. (if (> arg 0)
  6427. ;; Moving forward - still need to move over subtree
  6428. (progn (org-end-of-subtree t t)
  6429. (save-excursion
  6430. (org-back-over-empty-lines)
  6431. (or (bolp) (newline)))))
  6432. (setq ne-ins (org-back-over-empty-lines))
  6433. (move-marker ins-point (point))
  6434. (setq txt (buffer-substring beg end))
  6435. (org-save-markers-in-region beg end)
  6436. (delete-region beg end)
  6437. (org-remove-empty-overlays-at beg)
  6438. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6439. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6440. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6441. (let ((bbb (point)))
  6442. (insert-before-markers txt)
  6443. (org-reinstall-markers-in-region bbb)
  6444. (move-marker ins-point bbb))
  6445. (or (bolp) (insert "\n"))
  6446. (setq ins-end (point))
  6447. (goto-char ins-point)
  6448. (org-skip-whitespace)
  6449. (when (and (< arg 0)
  6450. (org-first-sibling-p)
  6451. (> ne-ins ne-beg))
  6452. ;; Move whitespace back to beginning
  6453. (save-excursion
  6454. (goto-char ins-end)
  6455. (let ((kill-whole-line t))
  6456. (kill-line (- ne-ins ne-beg)) (point)))
  6457. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6458. (move-marker ins-point nil)
  6459. (if folded
  6460. (hide-subtree)
  6461. (org-show-entry)
  6462. (show-children)
  6463. (org-cycle-hide-drawers 'children))
  6464. (org-clean-visibility-after-subtree-move)))
  6465. (defvar org-subtree-clip ""
  6466. "Clipboard for cut and paste of subtrees.
  6467. This is actually only a copy of the kill, because we use the normal kill
  6468. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6469. (defvar org-subtree-clip-folded nil
  6470. "Was the last copied subtree folded?
  6471. This is used to fold the tree back after pasting.")
  6472. (defun org-cut-subtree (&optional n)
  6473. "Cut the current subtree into the clipboard.
  6474. With prefix arg N, cut this many sequential subtrees.
  6475. This is a short-hand for marking the subtree and then cutting it."
  6476. (interactive "p")
  6477. (org-copy-subtree n 'cut))
  6478. (defun org-copy-subtree (&optional n cut force-store-markers)
  6479. "Cut the current subtree into the clipboard.
  6480. With prefix arg N, cut this many sequential subtrees.
  6481. This is a short-hand for marking the subtree and then copying it.
  6482. If CUT is non-nil, actually cut the subtree.
  6483. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6484. of some markers in the region, even if CUT is non-nil. This is
  6485. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6486. (interactive "p")
  6487. (let (beg end folded (beg0 (point)))
  6488. (if (interactive-p)
  6489. (org-back-to-heading nil) ; take what looks like a subtree
  6490. (org-back-to-heading t)) ; take what is really there
  6491. (org-back-over-empty-lines)
  6492. (setq beg (point))
  6493. (skip-chars-forward " \t\r\n")
  6494. (save-match-data
  6495. (save-excursion (outline-end-of-heading)
  6496. (setq folded (org-invisible-p)))
  6497. (condition-case nil
  6498. (org-forward-same-level (1- n) t)
  6499. (error nil))
  6500. (org-end-of-subtree t t))
  6501. (org-back-over-empty-lines)
  6502. (setq end (point))
  6503. (goto-char beg0)
  6504. (when (> end beg)
  6505. (setq org-subtree-clip-folded folded)
  6506. (when (or cut force-store-markers)
  6507. (org-save-markers-in-region beg end))
  6508. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6509. (setq org-subtree-clip (current-kill 0))
  6510. (message "%s: Subtree(s) with %d characters"
  6511. (if cut "Cut" "Copied")
  6512. (length org-subtree-clip)))))
  6513. (defun org-paste-subtree (&optional level tree for-yank)
  6514. "Paste the clipboard as a subtree, with modification of headline level.
  6515. The entire subtree is promoted or demoted in order to match a new headline
  6516. level.
  6517. If the cursor is at the beginning of a headline, the same level as
  6518. that headline is used to paste the tree
  6519. If not, the new level is derived from the *visible* headings
  6520. before and after the insertion point, and taken to be the inferior headline
  6521. level of the two. So if the previous visible heading is level 3 and the
  6522. next is level 4 (or vice versa), level 4 will be used for insertion.
  6523. This makes sure that the subtree remains an independent subtree and does
  6524. not swallow low level entries.
  6525. You can also force a different level, either by using a numeric prefix
  6526. argument, or by inserting the heading marker by hand. For example, if the
  6527. cursor is after \"*****\", then the tree will be shifted to level 5.
  6528. If optional TREE is given, use this text instead of the kill ring.
  6529. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6530. move back over whitespace before inserting, and move point to the end of
  6531. the inserted text when done."
  6532. (interactive "P")
  6533. (setq tree (or tree (and kill-ring (current-kill 0))))
  6534. (unless (org-kill-is-subtree-p tree)
  6535. (error "%s"
  6536. (substitute-command-keys
  6537. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6538. (let* ((visp (not (org-invisible-p)))
  6539. (txt tree)
  6540. (^re (concat "^\\(" outline-regexp "\\)"))
  6541. (re (concat "\\(" outline-regexp "\\)"))
  6542. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6543. (old-level (if (string-match ^re txt)
  6544. (- (match-end 0) (match-beginning 0) 1)
  6545. -1))
  6546. (force-level (cond (level (prefix-numeric-value level))
  6547. ((and (looking-at "[ \t]*$")
  6548. (string-match
  6549. ^re_ (buffer-substring
  6550. (point-at-bol) (point))))
  6551. (- (match-end 1) (match-beginning 1)))
  6552. ((and (bolp)
  6553. (looking-at org-outline-regexp))
  6554. (- (match-end 0) (point) 1))
  6555. (t nil)))
  6556. (previous-level (save-excursion
  6557. (condition-case nil
  6558. (progn
  6559. (outline-previous-visible-heading 1)
  6560. (if (looking-at re)
  6561. (- (match-end 0) (match-beginning 0) 1)
  6562. 1))
  6563. (error 1))))
  6564. (next-level (save-excursion
  6565. (condition-case nil
  6566. (progn
  6567. (or (looking-at outline-regexp)
  6568. (outline-next-visible-heading 1))
  6569. (if (looking-at re)
  6570. (- (match-end 0) (match-beginning 0) 1)
  6571. 1))
  6572. (error 1))))
  6573. (new-level (or force-level (max previous-level next-level)))
  6574. (shift (if (or (= old-level -1)
  6575. (= new-level -1)
  6576. (= old-level new-level))
  6577. 0
  6578. (- new-level old-level)))
  6579. (delta (if (> shift 0) -1 1))
  6580. (func (if (> shift 0) 'org-demote 'org-promote))
  6581. (org-odd-levels-only nil)
  6582. beg end newend)
  6583. ;; Remove the forced level indicator
  6584. (if force-level
  6585. (delete-region (point-at-bol) (point)))
  6586. ;; Paste
  6587. (beginning-of-line 1)
  6588. (unless for-yank (org-back-over-empty-lines))
  6589. (setq beg (point))
  6590. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6591. (insert-before-markers txt)
  6592. (unless (string-match "\n\\'" txt) (insert "\n"))
  6593. (setq newend (point))
  6594. (org-reinstall-markers-in-region beg)
  6595. (setq end (point))
  6596. (goto-char beg)
  6597. (skip-chars-forward " \t\n\r")
  6598. (setq beg (point))
  6599. (if (and (org-invisible-p) visp)
  6600. (save-excursion (outline-show-heading)))
  6601. ;; Shift if necessary
  6602. (unless (= shift 0)
  6603. (save-restriction
  6604. (narrow-to-region beg end)
  6605. (while (not (= shift 0))
  6606. (org-map-region func (point-min) (point-max))
  6607. (setq shift (+ delta shift)))
  6608. (goto-char (point-min))
  6609. (setq newend (point-max))))
  6610. (when (or (interactive-p) for-yank)
  6611. (message "Clipboard pasted as level %d subtree" new-level))
  6612. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6613. kill-ring
  6614. (eq org-subtree-clip (current-kill 0))
  6615. org-subtree-clip-folded)
  6616. ;; The tree was folded before it was killed/copied
  6617. (hide-subtree))
  6618. (and for-yank (goto-char newend))))
  6619. (defun org-kill-is-subtree-p (&optional txt)
  6620. "Check if the current kill is an outline subtree, or a set of trees.
  6621. Returns nil if kill does not start with a headline, or if the first
  6622. headline level is not the largest headline level in the tree.
  6623. So this will actually accept several entries of equal levels as well,
  6624. which is OK for `org-paste-subtree'.
  6625. If optional TXT is given, check this string instead of the current kill."
  6626. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6627. (start-level (and kill
  6628. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6629. org-outline-regexp "\\)")
  6630. kill)
  6631. (- (match-end 2) (match-beginning 2) 1)))
  6632. (re (concat "^" org-outline-regexp))
  6633. (start (1+ (or (match-beginning 2) -1))))
  6634. (if (not start-level)
  6635. (progn
  6636. nil) ;; does not even start with a heading
  6637. (catch 'exit
  6638. (while (setq start (string-match re kill (1+ start)))
  6639. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6640. (throw 'exit nil)))
  6641. t))))
  6642. (defvar org-markers-to-move nil
  6643. "Markers that should be moved with a cut-and-paste operation.
  6644. Those markers are stored together with their positions relative to
  6645. the start of the region.")
  6646. (defun org-save-markers-in-region (beg end)
  6647. "Check markers in region.
  6648. If these markers are between BEG and END, record their position relative
  6649. to BEG, so that after moving the block of text, we can put the markers back
  6650. into place.
  6651. This function gets called just before an entry or tree gets cut from the
  6652. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6653. called immediately, to move the markers with the entries."
  6654. (setq org-markers-to-move nil)
  6655. (when (featurep 'org-clock)
  6656. (org-clock-save-markers-for-cut-and-paste beg end))
  6657. (when (featurep 'org-agenda)
  6658. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6659. (defun org-check-and-save-marker (marker beg end)
  6660. "Check if MARKER is between BEG and END.
  6661. If yes, remember the marker and the distance to BEG."
  6662. (when (and (marker-buffer marker)
  6663. (equal (marker-buffer marker) (current-buffer)))
  6664. (if (and (>= marker beg) (< marker end))
  6665. (push (cons marker (- marker beg)) org-markers-to-move))))
  6666. (defun org-reinstall-markers-in-region (beg)
  6667. "Move all remembered markers to their position relative to BEG."
  6668. (mapc (lambda (x)
  6669. (move-marker (car x) (+ beg (cdr x))))
  6670. org-markers-to-move)
  6671. (setq org-markers-to-move nil))
  6672. (defun org-narrow-to-subtree ()
  6673. "Narrow buffer to the current subtree."
  6674. (interactive)
  6675. (save-excursion
  6676. (save-match-data
  6677. (narrow-to-region
  6678. (progn (org-back-to-heading t) (point))
  6679. (progn (org-end-of-subtree t t)
  6680. (if (org-on-heading-p) (backward-char 1))
  6681. (point))))))
  6682. (eval-when-compile
  6683. (defvar org-property-drawer-re))
  6684. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6685. "Clone the task (subtree) at point N times.
  6686. The clones will be inserted as siblings.
  6687. In interactive use, the user will be prompted for the number of
  6688. clones to be produced, and for a time SHIFT, which may be a
  6689. repeater as used in time stamps, for example `+3d'.
  6690. When a valid repeater is given and the entry contains any time
  6691. stamps, the clones will become a sequence in time, with time
  6692. stamps in the subtree shifted for each clone produced. If SHIFT
  6693. is nil or the empty string, time stamps will be left alone. The
  6694. ID property of the original subtree is removed.
  6695. If the original subtree did contain time stamps with a repeater,
  6696. the following will happen:
  6697. - the repeater will be removed in each clone
  6698. - an additional clone will be produced, with the current, unshifted
  6699. date(s) in the entry.
  6700. - the original entry will be placed *after* all the clones, with
  6701. repeater intact.
  6702. - the start days in the repeater in the original entry will be shifted
  6703. to past the last clone.
  6704. I this way you can spell out a number of instances of a repeating task,
  6705. and still retain the repeater to cover future instances of the task."
  6706. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6707. (let (beg end template task idprop
  6708. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6709. (if (not (and (integerp n) (> n 0)))
  6710. (error "Invalid number of replications %s" n))
  6711. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6712. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6713. shift)))
  6714. (error "Invalid shift specification %s" shift))
  6715. (when doshift
  6716. (setq shift-n (string-to-number (match-string 1 shift))
  6717. shift-what (cdr (assoc (match-string 2 shift)
  6718. '(("d" . day) ("w" . week)
  6719. ("m" . month) ("y" . year))))))
  6720. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6721. (setq nmin 1 nmax n)
  6722. (org-back-to-heading t)
  6723. (setq beg (point))
  6724. (setq idprop (org-entry-get nil "ID"))
  6725. (org-end-of-subtree t t)
  6726. (or (bolp) (insert "\n"))
  6727. (setq end (point))
  6728. (setq template (buffer-substring beg end))
  6729. (when (and doshift
  6730. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6731. (delete-region beg end)
  6732. (setq end beg)
  6733. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6734. (goto-char end)
  6735. (loop for n from nmin to nmax do
  6736. ;; prepare clone
  6737. (with-temp-buffer
  6738. (insert template)
  6739. (org-mode)
  6740. (goto-char (point-min))
  6741. (and idprop (if org-clone-delete-id
  6742. (org-entry-delete nil "ID")
  6743. (org-id-get-create t)))
  6744. (while (re-search-forward org-property-drawer-re nil t)
  6745. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6746. (goto-char (point-min))
  6747. (when doshift
  6748. (while (re-search-forward org-ts-regexp-both nil t)
  6749. (org-timestamp-change (* n shift-n) shift-what))
  6750. (unless (= n n-no-remove)
  6751. (goto-char (point-min))
  6752. (while (re-search-forward org-ts-regexp nil t)
  6753. (save-excursion
  6754. (goto-char (match-beginning 0))
  6755. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6756. (delete-region (match-beginning 1) (match-end 1)))))))
  6757. (setq task (buffer-string)))
  6758. (insert task))
  6759. (goto-char beg)))
  6760. ;;; Outline Sorting
  6761. (defun org-sort (with-case)
  6762. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6763. Optional argument WITH-CASE means sort case-sensitively.
  6764. With a double prefix argument, also remove duplicate entries."
  6765. (interactive "P")
  6766. (cond
  6767. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6768. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6769. (t
  6770. (org-call-with-arg 'org-sort-entries with-case))))
  6771. (defun org-sort-remove-invisible (s)
  6772. (remove-text-properties 0 (length s) org-rm-props s)
  6773. (while (string-match org-bracket-link-regexp s)
  6774. (setq s (replace-match (if (match-end 2)
  6775. (match-string 3 s)
  6776. (match-string 1 s)) t t s)))
  6777. s)
  6778. (defvar org-priority-regexp) ; defined later in the file
  6779. (defvar org-after-sorting-entries-or-items-hook nil
  6780. "Hook that is run after a bunch of entries or items have been sorted.
  6781. When children are sorted, the cursor is in the parent line when this
  6782. hook gets called. When a region or a plain list is sorted, the cursor
  6783. will be in the first entry of the sorted region/list.")
  6784. (defun org-sort-entries
  6785. (&optional with-case sorting-type getkey-func compare-func property)
  6786. "Sort entries on a certain level of an outline tree.
  6787. If there is an active region, the entries in the region are sorted.
  6788. Else, if the cursor is before the first entry, sort the top-level items.
  6789. Else, the children of the entry at point are sorted.
  6790. Sorting can be alphabetically, numerically, by date/time as given by
  6791. a time stamp, by a property or by priority.
  6792. The command prompts for the sorting type unless it has been given to the
  6793. function through the SORTING-TYPE argument, which needs to be a character,
  6794. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6795. precise meaning of each character:
  6796. n Numerically, by converting the beginning of the entry/item to a number.
  6797. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6798. t By date/time, either the first active time stamp in the entry, or, if
  6799. none exist, by the first inactive one.
  6800. s By the scheduled date/time.
  6801. d By deadline date/time.
  6802. c By creation time, which is assumed to be the first inactive time stamp
  6803. at the beginning of a line.
  6804. p By priority according to the cookie.
  6805. r By the value of a property.
  6806. Capital letters will reverse the sort order.
  6807. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6808. called with point at the beginning of the record. It must return either
  6809. a string or a number that should serve as the sorting key for that record.
  6810. Comparing entries ignores case by default. However, with an optional argument
  6811. WITH-CASE, the sorting considers case as well."
  6812. (interactive "P")
  6813. (let ((case-func (if with-case 'identity 'downcase))
  6814. start beg end stars re re2
  6815. txt what tmp)
  6816. ;; Find beginning and end of region to sort
  6817. (cond
  6818. ((org-region-active-p)
  6819. ;; we will sort the region
  6820. (setq end (region-end)
  6821. what "region")
  6822. (goto-char (region-beginning))
  6823. (if (not (org-on-heading-p)) (outline-next-heading))
  6824. (setq start (point)))
  6825. ((or (org-on-heading-p)
  6826. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6827. ;; we will sort the children of the current headline
  6828. (org-back-to-heading)
  6829. (setq start (point)
  6830. end (progn (org-end-of-subtree t t)
  6831. (or (bolp) (insert "\n"))
  6832. (org-back-over-empty-lines)
  6833. (point))
  6834. what "children")
  6835. (goto-char start)
  6836. (show-subtree)
  6837. (outline-next-heading))
  6838. (t
  6839. ;; we will sort the top-level entries in this file
  6840. (goto-char (point-min))
  6841. (or (org-on-heading-p) (outline-next-heading))
  6842. (setq start (point))
  6843. (goto-char (point-max))
  6844. (beginning-of-line 1)
  6845. (when (looking-at ".*?\\S-")
  6846. ;; File ends in a non-white line
  6847. (end-of-line 1)
  6848. (insert "\n"))
  6849. (setq end (point-max))
  6850. (setq what "top-level")
  6851. (goto-char start)
  6852. (show-all)))
  6853. (setq beg (point))
  6854. (if (>= beg end) (error "Nothing to sort"))
  6855. (looking-at "\\(\\*+\\)")
  6856. (setq stars (match-string 1)
  6857. re (concat "^" (regexp-quote stars) " +")
  6858. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6859. txt (buffer-substring beg end))
  6860. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6861. (if (and (not (equal stars "*")) (string-match re2 txt))
  6862. (error "Region to sort contains a level above the first entry"))
  6863. (unless sorting-type
  6864. (message
  6865. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6866. [t]ime [s]cheduled [d]eadline [c]reated
  6867. A/N/T/S/D/C/P/O/F means reversed:"
  6868. what)
  6869. (setq sorting-type (read-char-exclusive))
  6870. (and (= (downcase sorting-type) ?f)
  6871. (setq getkey-func
  6872. (org-icompleting-read "Sort using function: "
  6873. obarray 'fboundp t nil nil))
  6874. (setq getkey-func (intern getkey-func)))
  6875. (and (= (downcase sorting-type) ?r)
  6876. (setq property
  6877. (org-icompleting-read "Property: "
  6878. (mapcar 'list (org-buffer-property-keys t))
  6879. nil t))))
  6880. (message "Sorting entries...")
  6881. (save-restriction
  6882. (narrow-to-region start end)
  6883. (let ((dcst (downcase sorting-type))
  6884. (case-fold-search nil)
  6885. (now (current-time)))
  6886. (sort-subr
  6887. (/= dcst sorting-type)
  6888. ;; This function moves to the beginning character of the "record" to
  6889. ;; be sorted.
  6890. (lambda nil
  6891. (if (re-search-forward re nil t)
  6892. (goto-char (match-beginning 0))
  6893. (goto-char (point-max))))
  6894. ;; This function moves to the last character of the "record" being
  6895. ;; sorted.
  6896. (lambda nil
  6897. (save-match-data
  6898. (condition-case nil
  6899. (outline-forward-same-level 1)
  6900. (error
  6901. (goto-char (point-max))))))
  6902. ;; This function returns the value that gets sorted against.
  6903. (lambda nil
  6904. (cond
  6905. ((= dcst ?n)
  6906. (if (looking-at org-complex-heading-regexp)
  6907. (string-to-number (match-string 4))
  6908. nil))
  6909. ((= dcst ?a)
  6910. (if (looking-at org-complex-heading-regexp)
  6911. (funcall case-func (match-string 4))
  6912. nil))
  6913. ((= dcst ?t)
  6914. (let ((end (save-excursion (outline-next-heading) (point))))
  6915. (if (or (re-search-forward org-ts-regexp end t)
  6916. (re-search-forward org-ts-regexp-both end t))
  6917. (org-time-string-to-seconds (match-string 0))
  6918. (org-float-time now))))
  6919. ((= dcst ?c)
  6920. (let ((end (save-excursion (outline-next-heading) (point))))
  6921. (if (re-search-forward
  6922. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6923. end t)
  6924. (org-time-string-to-seconds (match-string 0))
  6925. (org-float-time now))))
  6926. ((= dcst ?s)
  6927. (let ((end (save-excursion (outline-next-heading) (point))))
  6928. (if (re-search-forward org-scheduled-time-regexp end t)
  6929. (org-time-string-to-seconds (match-string 1))
  6930. (org-float-time now))))
  6931. ((= dcst ?d)
  6932. (let ((end (save-excursion (outline-next-heading) (point))))
  6933. (if (re-search-forward org-deadline-time-regexp end t)
  6934. (org-time-string-to-seconds (match-string 1))
  6935. (org-float-time now))))
  6936. ((= dcst ?p)
  6937. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6938. (string-to-char (match-string 2))
  6939. org-default-priority))
  6940. ((= dcst ?r)
  6941. (or (org-entry-get nil property) ""))
  6942. ((= dcst ?o)
  6943. (if (looking-at org-complex-heading-regexp)
  6944. (- 9999 (length (member (match-string 2)
  6945. org-todo-keywords-1)))))
  6946. ((= dcst ?f)
  6947. (if getkey-func
  6948. (progn
  6949. (setq tmp (funcall getkey-func))
  6950. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6951. tmp)
  6952. (error "Invalid key function `%s'" getkey-func)))
  6953. (t (error "Invalid sorting type `%c'" sorting-type))))
  6954. nil
  6955. (cond
  6956. ((= dcst ?a) 'string<)
  6957. ((= dcst ?f) compare-func)
  6958. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6959. (t nil)))))
  6960. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6961. (message "Sorting entries...done")))
  6962. (defun org-do-sort (table what &optional with-case sorting-type)
  6963. "Sort TABLE of WHAT according to SORTING-TYPE.
  6964. The user will be prompted for the SORTING-TYPE if the call to this
  6965. function does not specify it. WHAT is only for the prompt, to indicate
  6966. what is being sorted. The sorting key will be extracted from
  6967. the car of the elements of the table.
  6968. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6969. (unless sorting-type
  6970. (message
  6971. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6972. what)
  6973. (setq sorting-type (read-char-exclusive)))
  6974. (let ((dcst (downcase sorting-type))
  6975. extractfun comparefun)
  6976. ;; Define the appropriate functions
  6977. (cond
  6978. ((= dcst ?n)
  6979. (setq extractfun 'string-to-number
  6980. comparefun (if (= dcst sorting-type) '< '>)))
  6981. ((= dcst ?a)
  6982. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6983. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6984. comparefun (if (= dcst sorting-type)
  6985. 'string<
  6986. (lambda (a b) (and (not (string< a b))
  6987. (not (string= a b)))))))
  6988. ((= dcst ?t)
  6989. (setq extractfun
  6990. (lambda (x)
  6991. (if (or (string-match org-ts-regexp x)
  6992. (string-match org-ts-regexp-both x))
  6993. (org-float-time
  6994. (org-time-string-to-time (match-string 0 x)))
  6995. 0))
  6996. comparefun (if (= dcst sorting-type) '< '>)))
  6997. (t (error "Invalid sorting type `%c'" sorting-type)))
  6998. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6999. table)
  7000. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7001. ;;; The orgstruct minor mode
  7002. ;; Define a minor mode which can be used in other modes in order to
  7003. ;; integrate the org-mode structure editing commands.
  7004. ;; This is really a hack, because the org-mode structure commands use
  7005. ;; keys which normally belong to the major mode. Here is how it
  7006. ;; works: The minor mode defines all the keys necessary to operate the
  7007. ;; structure commands, but wraps the commands into a function which
  7008. ;; tests if the cursor is currently at a headline or a plain list
  7009. ;; item. If that is the case, the structure command is used,
  7010. ;; temporarily setting many Org-mode variables like regular
  7011. ;; expressions for filling etc. However, when any of those keys is
  7012. ;; used at a different location, function uses `key-binding' to look
  7013. ;; up if the key has an associated command in another currently active
  7014. ;; keymap (minor modes, major mode, global), and executes that
  7015. ;; command. There might be problems if any of the keys is otherwise
  7016. ;; used as a prefix key.
  7017. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7018. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7019. ;; addresses this by checking explicitly for both bindings.
  7020. (defvar orgstruct-mode-map (make-sparse-keymap)
  7021. "Keymap for the minor `orgstruct-mode'.")
  7022. (defvar org-local-vars nil
  7023. "List of local variables, for use by `orgstruct-mode'.")
  7024. ;;;###autoload
  7025. (define-minor-mode orgstruct-mode
  7026. "Toggle the minor mode `orgstruct-mode'.
  7027. This mode is for using Org-mode structure commands in other
  7028. modes. The following keys behave as if Org-mode were active, if
  7029. the cursor is on a headline, or on a plain list item (both as
  7030. defined by Org-mode).
  7031. M-up Move entry/item up
  7032. M-down Move entry/item down
  7033. M-left Promote
  7034. M-right Demote
  7035. M-S-up Move entry/item up
  7036. M-S-down Move entry/item down
  7037. M-S-left Promote subtree
  7038. M-S-right Demote subtree
  7039. M-q Fill paragraph and items like in Org-mode
  7040. C-c ^ Sort entries
  7041. C-c - Cycle list bullet
  7042. TAB Cycle item visibility
  7043. M-RET Insert new heading/item
  7044. S-M-RET Insert new TODO heading / Checkbox item
  7045. C-c C-c Set tags / toggle checkbox"
  7046. nil " OrgStruct" nil
  7047. (org-load-modules-maybe)
  7048. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7049. ;;;###autoload
  7050. (defun turn-on-orgstruct ()
  7051. "Unconditionally turn on `orgstruct-mode'."
  7052. (orgstruct-mode 1))
  7053. (defun orgstruct++-mode (&optional arg)
  7054. "Toggle `orgstruct-mode', the enhanced version of it.
  7055. In addition to setting orgstruct-mode, this also exports all indentation
  7056. and autofilling variables from org-mode into the buffer. It will also
  7057. recognize item context in multiline items.
  7058. Note that turning off orgstruct-mode will *not* remove the
  7059. indentation/paragraph settings. This can only be done by refreshing the
  7060. major mode, for example with \\[normal-mode]."
  7061. (interactive "P")
  7062. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7063. (if (< arg 1)
  7064. (orgstruct-mode -1)
  7065. (orgstruct-mode 1)
  7066. (let (var val)
  7067. (mapc
  7068. (lambda (x)
  7069. (when (string-match
  7070. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7071. (symbol-name (car x)))
  7072. (setq var (car x) val (nth 1 x))
  7073. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7074. org-local-vars)
  7075. (org-set-local 'orgstruct-is-++ t))))
  7076. (defvar orgstruct-is-++ nil
  7077. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7078. (make-variable-buffer-local 'orgstruct-is-++)
  7079. ;;;###autoload
  7080. (defun turn-on-orgstruct++ ()
  7081. "Unconditionally turn on `orgstruct++-mode'."
  7082. (orgstruct++-mode 1))
  7083. (defun orgstruct-error ()
  7084. "Error when there is no default binding for a structure key."
  7085. (interactive)
  7086. (error "This key has no function outside structure elements"))
  7087. (defun orgstruct-setup ()
  7088. "Setup orgstruct keymaps."
  7089. (let ((nfunc 0)
  7090. (bindings
  7091. (list
  7092. '([(meta up)] org-metaup)
  7093. '([(meta down)] org-metadown)
  7094. '([(meta left)] org-metaleft)
  7095. '([(meta right)] org-metaright)
  7096. '([(meta shift up)] org-shiftmetaup)
  7097. '([(meta shift down)] org-shiftmetadown)
  7098. '([(meta shift left)] org-shiftmetaleft)
  7099. '([(meta shift right)] org-shiftmetaright)
  7100. '([?\e (up)] org-metaup)
  7101. '([?\e (down)] org-metadown)
  7102. '([?\e (left)] org-metaleft)
  7103. '([?\e (right)] org-metaright)
  7104. '([?\e (shift up)] org-shiftmetaup)
  7105. '([?\e (shift down)] org-shiftmetadown)
  7106. '([?\e (shift left)] org-shiftmetaleft)
  7107. '([?\e (shift right)] org-shiftmetaright)
  7108. '([(shift up)] org-shiftup)
  7109. '([(shift down)] org-shiftdown)
  7110. '([(shift left)] org-shiftleft)
  7111. '([(shift right)] org-shiftright)
  7112. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7113. '("\M-q" fill-paragraph)
  7114. '("\C-c^" org-sort)
  7115. '("\C-c-" org-cycle-list-bullet)))
  7116. elt key fun cmd)
  7117. (while (setq elt (pop bindings))
  7118. (setq nfunc (1+ nfunc))
  7119. (setq key (org-key (car elt))
  7120. fun (nth 1 elt)
  7121. cmd (orgstruct-make-binding fun nfunc key))
  7122. (org-defkey orgstruct-mode-map key cmd))
  7123. ;; Special treatment needed for TAB and RET
  7124. (org-defkey orgstruct-mode-map [(tab)]
  7125. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7126. (org-defkey orgstruct-mode-map "\C-i"
  7127. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7128. (org-defkey orgstruct-mode-map "\M-\C-m"
  7129. (orgstruct-make-binding 'org-insert-heading 105
  7130. "\M-\C-m" [(meta return)]))
  7131. (org-defkey orgstruct-mode-map [(meta return)]
  7132. (orgstruct-make-binding 'org-insert-heading 106
  7133. [(meta return)] "\M-\C-m"))
  7134. (org-defkey orgstruct-mode-map [(shift meta return)]
  7135. (orgstruct-make-binding 'org-insert-todo-heading 107
  7136. [(meta return)] "\M-\C-m"))
  7137. (org-defkey orgstruct-mode-map "\e\C-m"
  7138. (orgstruct-make-binding 'org-insert-heading 108
  7139. "\e\C-m" [?\e (return)]))
  7140. (org-defkey orgstruct-mode-map [?\e (return)]
  7141. (orgstruct-make-binding 'org-insert-heading 109
  7142. [?\e (return)] "\e\C-m"))
  7143. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7144. (orgstruct-make-binding 'org-insert-todo-heading 110
  7145. [?\e (return)] "\e\C-m"))
  7146. (unless org-local-vars
  7147. (setq org-local-vars (org-get-local-variables)))
  7148. t))
  7149. (defun orgstruct-make-binding (fun n &rest keys)
  7150. "Create a function for binding in the structure minor mode.
  7151. FUN is the command to call inside a table. N is used to create a unique
  7152. command name. KEYS are keys that should be checked in for a command
  7153. to execute outside of tables."
  7154. (eval
  7155. (list 'defun
  7156. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7157. '(arg)
  7158. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7159. "Outside of structure, run the binding of `"
  7160. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7161. "'.")
  7162. '(interactive "p")
  7163. (list 'if
  7164. `(org-context-p 'headline 'item
  7165. (and orgstruct-is-++
  7166. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7167. 'item-body))
  7168. (list 'org-run-like-in-org-mode (list 'quote fun))
  7169. (list 'let '(orgstruct-mode)
  7170. (list 'call-interactively
  7171. (append '(or)
  7172. (mapcar (lambda (k)
  7173. (list 'key-binding k))
  7174. keys)
  7175. '('orgstruct-error))))))))
  7176. (defun org-context-p (&rest contexts)
  7177. "Check if local context is any of CONTEXTS.
  7178. Possible values in the list of contexts are `table', `headline', and `item'."
  7179. (let ((pos (point)))
  7180. (goto-char (point-at-bol))
  7181. (prog1 (or (and (memq 'table contexts)
  7182. (looking-at "[ \t]*|"))
  7183. (and (memq 'headline contexts)
  7184. ;;????????? (looking-at "\\*+"))
  7185. (looking-at outline-regexp))
  7186. (and (memq 'item contexts)
  7187. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7188. (and (memq 'item-body contexts)
  7189. (org-in-item-p)))
  7190. (goto-char pos))))
  7191. (defun org-get-local-variables ()
  7192. "Return a list of all local variables in an org-mode buffer."
  7193. (let (varlist)
  7194. (with-current-buffer (get-buffer-create "*Org tmp*")
  7195. (erase-buffer)
  7196. (org-mode)
  7197. (setq varlist (buffer-local-variables)))
  7198. (kill-buffer "*Org tmp*")
  7199. (delq nil
  7200. (mapcar
  7201. (lambda (x)
  7202. (setq x
  7203. (if (symbolp x)
  7204. (list x)
  7205. (list (car x) (list 'quote (cdr x)))))
  7206. (if (string-match
  7207. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7208. (symbol-name (car x)))
  7209. x nil))
  7210. varlist))))
  7211. ;;;###autoload
  7212. (defun org-run-like-in-org-mode (cmd)
  7213. "Run a command, pretending that the current buffer is in Org-mode.
  7214. This will temporarily bind local variables that are typically bound in
  7215. Org-mode to the values they have in Org-mode, and then interactively
  7216. call CMD."
  7217. (org-load-modules-maybe)
  7218. (unless org-local-vars
  7219. (setq org-local-vars (org-get-local-variables)))
  7220. (eval (list 'let org-local-vars
  7221. (list 'call-interactively (list 'quote cmd)))))
  7222. ;;;; Archiving
  7223. (defun org-get-category (&optional pos)
  7224. "Get the category applying to position POS."
  7225. (get-text-property (or pos (point)) 'org-category))
  7226. (defun org-refresh-category-properties ()
  7227. "Refresh category text properties in the buffer."
  7228. (let ((def-cat (cond
  7229. ((null org-category)
  7230. (if buffer-file-name
  7231. (file-name-sans-extension
  7232. (file-name-nondirectory buffer-file-name))
  7233. "???"))
  7234. ((symbolp org-category) (symbol-name org-category))
  7235. (t org-category)))
  7236. beg end cat pos optionp)
  7237. (org-unmodified
  7238. (save-excursion
  7239. (save-restriction
  7240. (widen)
  7241. (goto-char (point-min))
  7242. (put-text-property (point) (point-max) 'org-category def-cat)
  7243. (while (re-search-forward
  7244. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7245. (setq pos (match-end 0)
  7246. optionp (equal (char-after (match-beginning 0)) ?#)
  7247. cat (org-trim (match-string 2)))
  7248. (if optionp
  7249. (setq beg (point-at-bol) end (point-max))
  7250. (org-back-to-heading t)
  7251. (setq beg (point) end (org-end-of-subtree t t)))
  7252. (put-text-property beg end 'org-category cat)
  7253. (goto-char pos)))))))
  7254. ;;;; Link Stuff
  7255. ;;; Link abbreviations
  7256. (defun org-link-expand-abbrev (link)
  7257. "Apply replacements as defined in `org-link-abbrev-alist."
  7258. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7259. (let* ((key (match-string 1 link))
  7260. (as (or (assoc key org-link-abbrev-alist-local)
  7261. (assoc key org-link-abbrev-alist)))
  7262. (tag (and (match-end 2) (match-string 3 link)))
  7263. rpl)
  7264. (if (not as)
  7265. link
  7266. (setq rpl (cdr as))
  7267. (cond
  7268. ((symbolp rpl) (funcall rpl tag))
  7269. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7270. ((string-match "%h" rpl)
  7271. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7272. (t (concat rpl tag)))))
  7273. link))
  7274. ;;; Storing and inserting links
  7275. (defvar org-insert-link-history nil
  7276. "Minibuffer history for links inserted with `org-insert-link'.")
  7277. (defvar org-stored-links nil
  7278. "Contains the links stored with `org-store-link'.")
  7279. (defvar org-store-link-plist nil
  7280. "Plist with info about the most recently link created with `org-store-link'.")
  7281. (defvar org-link-protocols nil
  7282. "Link protocols added to Org-mode using `org-add-link-type'.")
  7283. (defvar org-store-link-functions nil
  7284. "List of functions that are called to create and store a link.
  7285. Each function will be called in turn until one returns a non-nil
  7286. value. Each function should check if it is responsible for creating
  7287. this link (for example by looking at the major mode).
  7288. If not, it must exit and return nil.
  7289. If yes, it should return a non-nil value after a calling
  7290. `org-store-link-props' with a list of properties and values.
  7291. Special properties are:
  7292. :type The link prefix, like \"http\". This must be given.
  7293. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7294. This is obligatory as well.
  7295. :description Optional default description for the second pair
  7296. of brackets in an Org-mode link. The user can still change
  7297. this when inserting this link into an Org-mode buffer.
  7298. In addition to these, any additional properties can be specified
  7299. and then used in remember templates.")
  7300. (defun org-add-link-type (type &optional follow export)
  7301. "Add TYPE to the list of `org-link-types'.
  7302. Re-compute all regular expressions depending on `org-link-types'
  7303. FOLLOW and EXPORT are two functions.
  7304. FOLLOW should take the link path as the single argument and do whatever
  7305. is necessary to follow the link, for example find a file or display
  7306. a mail message.
  7307. EXPORT should format the link path for export to one of the export formats.
  7308. It should be a function accepting three arguments:
  7309. path the path of the link, the text after the prefix (like \"http:\")
  7310. desc the description of the link, if any, nil if there was no description
  7311. format the export format, a symbol like `html' or `latex'.
  7312. The function may use the FORMAT information to return different values
  7313. depending on the format. The return value will be put literally into
  7314. the exported file.
  7315. Org-mode has a built-in default for exporting links. If you are happy with
  7316. this default, there is no need to define an export function for the link
  7317. type. For a simple example of an export function, see `org-bbdb.el'."
  7318. (add-to-list 'org-link-types type t)
  7319. (org-make-link-regexps)
  7320. (if (assoc type org-link-protocols)
  7321. (setcdr (assoc type org-link-protocols) (list follow export))
  7322. (push (list type follow export) org-link-protocols)))
  7323. (defvar org-agenda-buffer-name)
  7324. ;;;###autoload
  7325. (defun org-store-link (arg)
  7326. "\\<org-mode-map>Store an org-link to the current location.
  7327. This link is added to `org-stored-links' and can later be inserted
  7328. into an org-buffer with \\[org-insert-link].
  7329. For some link types, a prefix arg is interpreted:
  7330. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7331. For file links, arg negates `org-context-in-file-links'."
  7332. (interactive "P")
  7333. (org-load-modules-maybe)
  7334. (setq org-store-link-plist nil) ; reset
  7335. (let ((outline-regexp (org-get-limited-outline-regexp))
  7336. link cpltxt desc description search txt custom-id agenda-link)
  7337. (cond
  7338. ((run-hook-with-args-until-success 'org-store-link-functions)
  7339. (setq link (plist-get org-store-link-plist :link)
  7340. desc (or (plist-get org-store-link-plist :description) link)))
  7341. ((equal (buffer-name) "*Org Edit Src Example*")
  7342. (let (label gc)
  7343. (while (or (not label)
  7344. (save-excursion
  7345. (save-restriction
  7346. (widen)
  7347. (goto-char (point-min))
  7348. (re-search-forward
  7349. (regexp-quote (format org-coderef-label-format label))
  7350. nil t))))
  7351. (when label (message "Label exists already") (sit-for 2))
  7352. (setq label (read-string "Code line label: " label)))
  7353. (end-of-line 1)
  7354. (setq link (format org-coderef-label-format label))
  7355. (setq gc (- 79 (length link)))
  7356. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7357. (insert link)
  7358. (setq link (concat "(" label ")") desc nil)))
  7359. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7360. ;; We are in the agenda, link to referenced location
  7361. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7362. (get-text-property (point) 'org-marker))))
  7363. (when m
  7364. (org-with-point-at m
  7365. (setq agenda-link
  7366. (if (interactive-p)
  7367. (call-interactively 'org-store-link)
  7368. (org-store-link nil)))))))
  7369. ((eq major-mode 'calendar-mode)
  7370. (let ((cd (calendar-cursor-to-date)))
  7371. (setq link
  7372. (format-time-string
  7373. (car org-time-stamp-formats)
  7374. (apply 'encode-time
  7375. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7376. nil nil nil))))
  7377. (org-store-link-props :type "calendar" :date cd)))
  7378. ((eq major-mode 'w3-mode)
  7379. (setq cpltxt (if (and (buffer-name)
  7380. (not (string-match "Untitled" (buffer-name))))
  7381. (buffer-name)
  7382. (url-view-url t))
  7383. link (org-make-link (url-view-url t)))
  7384. (org-store-link-props :type "w3" :url (url-view-url t)))
  7385. ((eq major-mode 'w3m-mode)
  7386. (setq cpltxt (or w3m-current-title w3m-current-url)
  7387. link (org-make-link w3m-current-url))
  7388. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7389. ((setq search (run-hook-with-args-until-success
  7390. 'org-create-file-search-functions))
  7391. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7392. "::" search))
  7393. (setq cpltxt (or description link)))
  7394. ((eq major-mode 'image-mode)
  7395. (setq cpltxt (concat "file:"
  7396. (abbreviate-file-name buffer-file-name))
  7397. link (org-make-link cpltxt))
  7398. (org-store-link-props :type "image" :file buffer-file-name))
  7399. ((eq major-mode 'dired-mode)
  7400. ;; link to the file in the current line
  7401. (let ((file (dired-get-filename nil t)))
  7402. (setq file (if file
  7403. (abbreviate-file-name
  7404. (expand-file-name (dired-get-filename nil t)))
  7405. ;; otherwise, no file so use current directory.
  7406. default-directory))
  7407. (setq cpltxt (concat "file:" file)
  7408. link (org-make-link cpltxt))))
  7409. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7410. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7411. (cond
  7412. ((org-in-regexp "<<\\(.*?\\)>>")
  7413. (setq cpltxt
  7414. (concat "file:"
  7415. (abbreviate-file-name
  7416. (buffer-file-name (buffer-base-buffer)))
  7417. "::" (match-string 1))
  7418. link (org-make-link cpltxt)))
  7419. ((and (featurep 'org-id)
  7420. (or (eq org-link-to-org-use-id t)
  7421. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7422. (interactive-p))
  7423. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7424. (interactive-p)
  7425. (not custom-id))
  7426. (and org-link-to-org-use-id
  7427. (condition-case nil
  7428. (org-entry-get nil "ID")
  7429. (error nil)))))
  7430. ;; We can make a link using the ID.
  7431. (setq link (condition-case nil
  7432. (prog1 (org-id-store-link)
  7433. (setq desc (plist-get org-store-link-plist
  7434. :description)))
  7435. (error
  7436. ;; probably before first headline, link to file only
  7437. (concat "file:"
  7438. (abbreviate-file-name
  7439. (buffer-file-name (buffer-base-buffer))))))))
  7440. (t
  7441. ;; Just link to current headline
  7442. (setq cpltxt (concat "file:"
  7443. (abbreviate-file-name
  7444. (buffer-file-name (buffer-base-buffer)))))
  7445. ;; Add a context search string
  7446. (when (org-xor org-context-in-file-links arg)
  7447. (setq txt (cond
  7448. ((org-on-heading-p) nil)
  7449. ((org-region-active-p)
  7450. (buffer-substring (region-beginning) (region-end)))
  7451. (t nil)))
  7452. (when (or (null txt) (string-match "\\S-" txt))
  7453. (setq cpltxt
  7454. (concat cpltxt "::"
  7455. (condition-case nil
  7456. (org-make-org-heading-search-string txt)
  7457. (error "")))
  7458. desc (or (nth 4 (ignore-errors
  7459. (org-heading-components))) "NONE"))))
  7460. (if (string-match "::\\'" cpltxt)
  7461. (setq cpltxt (substring cpltxt 0 -2)))
  7462. (setq link (org-make-link cpltxt)))))
  7463. ((buffer-file-name (buffer-base-buffer))
  7464. ;; Just link to this file here.
  7465. (setq cpltxt (concat "file:"
  7466. (abbreviate-file-name
  7467. (buffer-file-name (buffer-base-buffer)))))
  7468. ;; Add a context string
  7469. (when (org-xor org-context-in-file-links arg)
  7470. (setq txt (if (org-region-active-p)
  7471. (buffer-substring (region-beginning) (region-end))
  7472. (buffer-substring (point-at-bol) (point-at-eol))))
  7473. ;; Only use search option if there is some text.
  7474. (when (string-match "\\S-" txt)
  7475. (setq cpltxt
  7476. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7477. desc "NONE")))
  7478. (setq link (org-make-link cpltxt)))
  7479. ((interactive-p)
  7480. (error "Cannot link to a buffer which is not visiting a file"))
  7481. (t (setq link nil)))
  7482. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7483. (setq link (or link cpltxt)
  7484. desc (or desc cpltxt))
  7485. (if (equal desc "NONE") (setq desc nil))
  7486. (if (and (or (interactive-p) executing-kbd-macro) link)
  7487. (progn
  7488. (setq org-stored-links
  7489. (cons (list link desc) org-stored-links))
  7490. (message "Stored: %s" (or desc link))
  7491. (when custom-id
  7492. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7493. "::#" custom-id))
  7494. (setq org-stored-links
  7495. (cons (list link desc) org-stored-links))))
  7496. (or agenda-link (and link (org-make-link-string link desc))))))
  7497. (defun org-store-link-props (&rest plist)
  7498. "Store link properties, extract names and addresses."
  7499. (let (x adr)
  7500. (when (setq x (plist-get plist :from))
  7501. (setq adr (mail-extract-address-components x))
  7502. (setq plist (plist-put plist :fromname (car adr)))
  7503. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7504. (when (setq x (plist-get plist :to))
  7505. (setq adr (mail-extract-address-components x))
  7506. (setq plist (plist-put plist :toname (car adr)))
  7507. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7508. (let ((from (plist-get plist :from))
  7509. (to (plist-get plist :to)))
  7510. (when (and from to org-from-is-user-regexp)
  7511. (setq plist
  7512. (plist-put plist :fromto
  7513. (if (string-match org-from-is-user-regexp from)
  7514. (concat "to %t")
  7515. (concat "from %f"))))))
  7516. (setq org-store-link-plist plist))
  7517. (defun org-add-link-props (&rest plist)
  7518. "Add these properties to the link property list."
  7519. (let (key value)
  7520. (while plist
  7521. (setq key (pop plist) value (pop plist))
  7522. (setq org-store-link-plist
  7523. (plist-put org-store-link-plist key value)))))
  7524. (defun org-email-link-description (&optional fmt)
  7525. "Return the description part of an email link.
  7526. This takes information from `org-store-link-plist' and formats it
  7527. according to FMT (default from `org-email-link-description-format')."
  7528. (setq fmt (or fmt org-email-link-description-format))
  7529. (let* ((p org-store-link-plist)
  7530. (to (plist-get p :toaddress))
  7531. (from (plist-get p :fromaddress))
  7532. (table
  7533. (list
  7534. (cons "%c" (plist-get p :fromto))
  7535. (cons "%F" (plist-get p :from))
  7536. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7537. (cons "%T" (plist-get p :to))
  7538. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7539. (cons "%s" (plist-get p :subject))
  7540. (cons "%m" (plist-get p :message-id)))))
  7541. (when (string-match "%c" fmt)
  7542. ;; Check if the user wrote this message
  7543. (if (and org-from-is-user-regexp from to
  7544. (save-match-data (string-match org-from-is-user-regexp from)))
  7545. (setq fmt (replace-match "to %t" t t fmt))
  7546. (setq fmt (replace-match "from %f" t t fmt))))
  7547. (org-replace-escapes fmt table)))
  7548. (defun org-make-org-heading-search-string (&optional string heading)
  7549. "Make search string for STRING or current headline."
  7550. (interactive)
  7551. (let ((s (or string (org-get-heading))))
  7552. (unless (and string (not heading))
  7553. ;; We are using a headline, clean up garbage in there.
  7554. (if (string-match org-todo-regexp s)
  7555. (setq s (replace-match "" t t s)))
  7556. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7557. (setq s (replace-match "" t t s)))
  7558. (setq s (org-trim s))
  7559. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7560. org-comment-string "\\)") s)
  7561. (setq s (replace-match "" t t s)))
  7562. (while (string-match org-ts-regexp s)
  7563. (setq s (replace-match "" t t s))))
  7564. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7565. (setq s (replace-match " " t t s)))
  7566. (or string (setq s (concat "*" s))) ; Add * for headlines
  7567. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7568. (defun org-make-link (&rest strings)
  7569. "Concatenate STRINGS."
  7570. (apply 'concat strings))
  7571. (defun org-make-link-string (link &optional description)
  7572. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7573. (unless (string-match "\\S-" link)
  7574. (error "Empty link"))
  7575. (when (and description
  7576. (stringp description)
  7577. (not (string-match "\\S-" description)))
  7578. (setq description nil))
  7579. (when (stringp description)
  7580. ;; Remove brackets from the description, they are fatal.
  7581. (while (string-match "\\[" description)
  7582. (setq description (replace-match "{" t t description)))
  7583. (while (string-match "\\]" description)
  7584. (setq description (replace-match "}" t t description))))
  7585. (when (equal (org-link-escape link) description)
  7586. ;; No description needed, it is identical
  7587. (setq description nil))
  7588. (when (and (not description)
  7589. (not (equal link (org-link-escape link))))
  7590. (setq description (org-extract-attributes link)))
  7591. (concat "[[" (org-link-escape link) "]"
  7592. (if description (concat "[" description "]") "")
  7593. "]"))
  7594. (defconst org-link-escape-chars
  7595. '((?\ . "%20")
  7596. (?\[ . "%5B")
  7597. (?\] . "%5D")
  7598. (?\340 . "%E0") ; `a
  7599. (?\342 . "%E2") ; ^a
  7600. (?\347 . "%E7") ; ,c
  7601. (?\350 . "%E8") ; `e
  7602. (?\351 . "%E9") ; 'e
  7603. (?\352 . "%EA") ; ^e
  7604. (?\356 . "%EE") ; ^i
  7605. (?\364 . "%F4") ; ^o
  7606. (?\371 . "%F9") ; `u
  7607. (?\373 . "%FB") ; ^u
  7608. (?\; . "%3B")
  7609. ;; (?? . "%3F")
  7610. (?= . "%3D")
  7611. (?+ . "%2B")
  7612. )
  7613. "Association list of escapes for some characters problematic in links.
  7614. This is the list that is used for internal purposes.")
  7615. (defvar org-url-encoding-use-url-hexify nil)
  7616. (defconst org-link-escape-chars-browser
  7617. '((?\ . "%20")) ; 32 for the SPC char
  7618. "Association list of escapes for some characters problematic in links.
  7619. This is the list that is used before handing over to the browser.")
  7620. (defun org-link-escape (text &optional table)
  7621. "Escape characters in TEXT that are problematic for links."
  7622. (if (and org-url-encoding-use-url-hexify (not table))
  7623. (url-hexify-string text)
  7624. (setq table (or table org-link-escape-chars))
  7625. (when text
  7626. (let ((re (mapconcat (lambda (x) (regexp-quote
  7627. (char-to-string (car x))))
  7628. table "\\|")))
  7629. (while (string-match re text)
  7630. (setq text
  7631. (replace-match
  7632. (cdr (assoc (string-to-char (match-string 0 text))
  7633. table))
  7634. t t text)))
  7635. text))))
  7636. (defun org-link-unescape (text &optional table)
  7637. "Reverse the action of `org-link-escape'."
  7638. (if (and org-url-encoding-use-url-hexify (not table))
  7639. (url-unhex-string text)
  7640. (setq table (or table org-link-escape-chars))
  7641. (when text
  7642. (let ((case-fold-search t)
  7643. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7644. table "\\|")))
  7645. (while (string-match re text)
  7646. (setq text
  7647. (replace-match
  7648. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7649. table)))
  7650. t t text)))
  7651. text))))
  7652. (defun org-xor (a b)
  7653. "Exclusive or."
  7654. (if a (not b) b))
  7655. (defun org-fixup-message-id-for-http (s)
  7656. "Replace special characters in a message id, so it can be used in an http query."
  7657. (when (string-match "%" s)
  7658. (setq s (mapconcat (lambda (c)
  7659. (if (eq c ?%)
  7660. "%25"
  7661. (char-to-string c)))
  7662. s "")))
  7663. (while (string-match "<" s)
  7664. (setq s (replace-match "%3C" t t s)))
  7665. (while (string-match ">" s)
  7666. (setq s (replace-match "%3E" t t s)))
  7667. (while (string-match "@" s)
  7668. (setq s (replace-match "%40" t t s)))
  7669. s)
  7670. ;;;###autoload
  7671. (defun org-insert-link-global ()
  7672. "Insert a link like Org-mode does.
  7673. This command can be called in any mode to insert a link in Org-mode syntax."
  7674. (interactive)
  7675. (org-load-modules-maybe)
  7676. (org-run-like-in-org-mode 'org-insert-link))
  7677. (defun org-insert-link (&optional complete-file link-location)
  7678. "Insert a link. At the prompt, enter the link.
  7679. Completion can be used to insert any of the link protocol prefixes like
  7680. http or ftp in use.
  7681. The history can be used to select a link previously stored with
  7682. `org-store-link'. When the empty string is entered (i.e. if you just
  7683. press RET at the prompt), the link defaults to the most recently
  7684. stored link. As SPC triggers completion in the minibuffer, you need to
  7685. use M-SPC or C-q SPC to force the insertion of a space character.
  7686. You will also be prompted for a description, and if one is given, it will
  7687. be displayed in the buffer instead of the link.
  7688. If there is already a link at point, this command will allow you to edit link
  7689. and description parts.
  7690. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7691. be selected using completion. The path to the file will be relative to the
  7692. current directory if the file is in the current directory or a subdirectory.
  7693. Otherwise, the link will be the absolute path as completed in the minibuffer
  7694. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7695. option `org-link-file-path-type'.
  7696. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7697. the current directory or below.
  7698. With three \\[universal-argument] prefixes, negate the meaning of
  7699. `org-keep-stored-link-after-insertion'.
  7700. If `org-make-link-description-function' is non-nil, this function will be
  7701. called with the link target, and the result will be the default
  7702. link description.
  7703. If the LINK-LOCATION parameter is non-nil, this value will be
  7704. used as the link location instead of reading one interactively."
  7705. (interactive "P")
  7706. (let* ((wcf (current-window-configuration))
  7707. (region (if (org-region-active-p)
  7708. (buffer-substring (region-beginning) (region-end))))
  7709. (remove (and region (list (region-beginning) (region-end))))
  7710. (desc region)
  7711. tmphist ; byte-compile incorrectly complains about this
  7712. (link link-location)
  7713. entry file all-prefixes)
  7714. (cond
  7715. (link-location) ; specified by arg, just use it.
  7716. ((org-in-regexp org-bracket-link-regexp 1)
  7717. ;; We do have a link at point, and we are going to edit it.
  7718. (setq remove (list (match-beginning 0) (match-end 0)))
  7719. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7720. (setq link (read-string "Link: "
  7721. (org-link-unescape
  7722. (org-match-string-no-properties 1)))))
  7723. ((or (org-in-regexp org-angle-link-re)
  7724. (org-in-regexp org-plain-link-re))
  7725. ;; Convert to bracket link
  7726. (setq remove (list (match-beginning 0) (match-end 0))
  7727. link (read-string "Link: "
  7728. (org-remove-angle-brackets (match-string 0)))))
  7729. ((member complete-file '((4) (16)))
  7730. ;; Completing read for file names.
  7731. (setq link (org-file-complete-link complete-file)))
  7732. (t
  7733. ;; Read link, with completion for stored links.
  7734. (with-output-to-temp-buffer "*Org Links*"
  7735. (princ "Insert a link.
  7736. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7737. (when org-stored-links
  7738. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7739. (princ (mapconcat
  7740. (lambda (x)
  7741. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7742. (reverse org-stored-links) "\n"))))
  7743. (let ((cw (selected-window)))
  7744. (select-window (get-buffer-window "*Org Links*" 'visible))
  7745. (setq truncate-lines t)
  7746. (unless (pos-visible-in-window-p (point-max))
  7747. (org-fit-window-to-buffer))
  7748. (and (window-live-p cw) (select-window cw)))
  7749. ;; Fake a link history, containing the stored links.
  7750. (setq tmphist (append (mapcar 'car org-stored-links)
  7751. org-insert-link-history))
  7752. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7753. (mapcar 'car org-link-abbrev-alist)
  7754. org-link-types))
  7755. (unwind-protect
  7756. (progn
  7757. (setq link
  7758. (let ((org-completion-use-ido nil)
  7759. (org-completion-use-iswitchb nil))
  7760. (org-completing-read
  7761. "Link: "
  7762. (append
  7763. (mapcar (lambda (x) (list (concat x ":")))
  7764. all-prefixes)
  7765. (mapcar 'car org-stored-links))
  7766. nil nil nil
  7767. 'tmphist
  7768. (car (car org-stored-links)))))
  7769. (if (not (string-match "\\S-" link))
  7770. (error "No link selected"))
  7771. (if (or (member link all-prefixes)
  7772. (and (equal ":" (substring link -1))
  7773. (member (substring link 0 -1) all-prefixes)
  7774. (setq link (substring link 0 -1))))
  7775. (setq link (org-link-try-special-completion link))))
  7776. (set-window-configuration wcf)
  7777. (kill-buffer "*Org Links*"))
  7778. (setq entry (assoc link org-stored-links))
  7779. (or entry (push link org-insert-link-history))
  7780. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7781. (not org-keep-stored-link-after-insertion))
  7782. (setq org-stored-links (delq (assoc link org-stored-links)
  7783. org-stored-links)))
  7784. (setq desc (or desc (nth 1 entry)))))
  7785. (if (string-match org-plain-link-re link)
  7786. ;; URL-like link, normalize the use of angular brackets.
  7787. (setq link (org-make-link (org-remove-angle-brackets link))))
  7788. ;; Check if we are linking to the current file with a search option
  7789. ;; If yes, simplify the link by using only the search option.
  7790. (when (and buffer-file-name
  7791. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7792. (let* ((path (match-string 1 link))
  7793. (case-fold-search nil)
  7794. (search (match-string 2 link)))
  7795. (save-match-data
  7796. (if (equal (file-truename buffer-file-name) (file-truename path))
  7797. ;; We are linking to this same file, with a search option
  7798. (setq link search)))))
  7799. ;; Check if we can/should use a relative path. If yes, simplify the link
  7800. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7801. (let* ((type (match-string 1 link))
  7802. (path (match-string 2 link))
  7803. (origpath path)
  7804. (case-fold-search nil))
  7805. (cond
  7806. ((or (eq org-link-file-path-type 'absolute)
  7807. (equal complete-file '(16)))
  7808. (setq path (abbreviate-file-name (expand-file-name path))))
  7809. ((eq org-link-file-path-type 'noabbrev)
  7810. (setq path (expand-file-name path)))
  7811. ((eq org-link-file-path-type 'relative)
  7812. (setq path (file-relative-name path)))
  7813. (t
  7814. (save-match-data
  7815. (if (string-match (concat "^" (regexp-quote
  7816. (expand-file-name
  7817. (file-name-as-directory
  7818. default-directory))))
  7819. (expand-file-name path))
  7820. ;; We are linking a file with relative path name.
  7821. (setq path (substring (expand-file-name path)
  7822. (match-end 0)))
  7823. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7824. (setq link (concat type path))
  7825. (if (equal desc origpath)
  7826. (setq desc path))))
  7827. (if org-make-link-description-function
  7828. (setq desc (funcall org-make-link-description-function link desc)))
  7829. (setq desc (read-string "Description: " desc))
  7830. (unless (string-match "\\S-" desc) (setq desc nil))
  7831. (if remove (apply 'delete-region remove))
  7832. (insert (org-make-link-string link desc))))
  7833. (defun org-link-try-special-completion (type)
  7834. "If there is completion support for link type TYPE, offer it."
  7835. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7836. (if (functionp fun)
  7837. (funcall fun)
  7838. (read-string "Link (no completion support): " (concat type ":")))))
  7839. (defun org-file-complete-link (&optional arg)
  7840. "Create a file link using completion."
  7841. (let (file link)
  7842. (setq file (read-file-name "File: "))
  7843. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7844. (pwd1 (file-name-as-directory (abbreviate-file-name
  7845. (expand-file-name ".")))))
  7846. (cond
  7847. ((equal arg '(16))
  7848. (setq link (org-make-link
  7849. "file:"
  7850. (abbreviate-file-name (expand-file-name file)))))
  7851. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7852. (setq link (org-make-link "file:" (match-string 1 file))))
  7853. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7854. (expand-file-name file))
  7855. (setq link (org-make-link
  7856. "file:" (match-string 1 (expand-file-name file)))))
  7857. (t (setq link (org-make-link "file:" file)))))
  7858. link))
  7859. (defun org-completing-read (&rest args)
  7860. "Completing-read with SPACE being a normal character."
  7861. (let ((minibuffer-local-completion-map
  7862. (copy-keymap minibuffer-local-completion-map)))
  7863. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7864. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7865. (apply 'org-icompleting-read args)))
  7866. (defun org-completing-read-no-i (&rest args)
  7867. (let (org-completion-use-ido org-completion-use-iswitchb)
  7868. (apply 'org-completing-read args)))
  7869. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7870. "Use iswitch as a completing-read replacement to choose from choices.
  7871. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7872. from."
  7873. (let* ((iswitchb-use-virtual-buffers nil)
  7874. (iswitchb-make-buflist-hook
  7875. (lambda ()
  7876. (setq iswitchb-temp-buflist choices))))
  7877. (iswitchb-read-buffer prompt)))
  7878. (defun org-icompleting-read (&rest args)
  7879. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7880. (org-without-partial-completion
  7881. (if (and org-completion-use-ido
  7882. (fboundp 'ido-completing-read)
  7883. (boundp 'ido-mode) ido-mode
  7884. (listp (second args)))
  7885. (let ((ido-enter-matching-directory nil))
  7886. (apply 'ido-completing-read (concat (car args))
  7887. (if (consp (car (nth 1 args)))
  7888. (mapcar (lambda (x) (car x)) (nth 1 args))
  7889. (nth 1 args))
  7890. (cddr args)))
  7891. (if (and org-completion-use-iswitchb
  7892. (boundp 'iswitchb-mode) iswitchb-mode
  7893. (listp (second args)))
  7894. (apply 'org-iswitchb-completing-read (concat (car args))
  7895. (if (consp (car (nth 1 args)))
  7896. (mapcar (lambda (x) (car x)) (nth 1 args))
  7897. (nth 1 args))
  7898. (cddr args))
  7899. (apply 'completing-read args)))))
  7900. (defun org-extract-attributes (s)
  7901. "Extract the attributes cookie from a string and set as text property."
  7902. (let (a attr (start 0) key value)
  7903. (save-match-data
  7904. (when (string-match "{{\\([^}]+\\)}}$" s)
  7905. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7906. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7907. (setq key (match-string 1 a) value (match-string 2 a)
  7908. start (match-end 0)
  7909. attr (plist-put attr (intern key) value))))
  7910. (org-add-props s nil 'org-attr attr))
  7911. s))
  7912. (defun org-extract-attributes-from-string (tag)
  7913. (let (key value attr)
  7914. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7915. (setq key (match-string 1 tag) value (match-string 2 tag)
  7916. tag (replace-match "" t t tag)
  7917. attr (plist-put attr (intern key) value)))
  7918. (cons tag attr)))
  7919. (defun org-attributes-to-string (plist)
  7920. "Format a property list into an HTML attribute list."
  7921. (let ((s "") key value)
  7922. (while plist
  7923. (setq key (pop plist) value (pop plist))
  7924. (and value
  7925. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7926. s))
  7927. ;;; Opening/following a link
  7928. (defvar org-link-search-failed nil)
  7929. (defvar org-open-link-functions nil
  7930. "Hook for functions finding a plain text link.
  7931. These functions must take a single argument, the link content.
  7932. They will be called for links that look like [[link text][description]]
  7933. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7934. like a filename (e.g. \"./blue.png\").
  7935. These functions will be called *before* Org attempts to resolve the
  7936. link by doing text searches in the current buffer - so if you want a
  7937. link \"[[target]]\" to still find \"<<target>>\", your function should
  7938. handle this as a special case.
  7939. When the function does handle the link, it must return a non-nil value.
  7940. If it decides that it is not responsible for this link, it must return
  7941. nil to indicate that that Org-mode can continue with other options
  7942. like exact and fuzzy text search.")
  7943. (defun org-next-link ()
  7944. "Move forward to the next link.
  7945. If the link is in hidden text, expose it."
  7946. (interactive)
  7947. (when (and org-link-search-failed (eq this-command last-command))
  7948. (goto-char (point-min))
  7949. (message "Link search wrapped back to beginning of buffer"))
  7950. (setq org-link-search-failed nil)
  7951. (let* ((pos (point))
  7952. (ct (org-context))
  7953. (a (assoc :link ct)))
  7954. (if a (goto-char (nth 2 a)))
  7955. (if (re-search-forward org-any-link-re nil t)
  7956. (progn
  7957. (goto-char (match-beginning 0))
  7958. (if (org-invisible-p) (org-show-context)))
  7959. (goto-char pos)
  7960. (setq org-link-search-failed t)
  7961. (error "No further link found"))))
  7962. (defun org-previous-link ()
  7963. "Move backward to the previous link.
  7964. If the link is in hidden text, expose it."
  7965. (interactive)
  7966. (when (and org-link-search-failed (eq this-command last-command))
  7967. (goto-char (point-max))
  7968. (message "Link search wrapped back to end of buffer"))
  7969. (setq org-link-search-failed nil)
  7970. (let* ((pos (point))
  7971. (ct (org-context))
  7972. (a (assoc :link ct)))
  7973. (if a (goto-char (nth 1 a)))
  7974. (if (re-search-backward org-any-link-re nil t)
  7975. (progn
  7976. (goto-char (match-beginning 0))
  7977. (if (org-invisible-p) (org-show-context)))
  7978. (goto-char pos)
  7979. (setq org-link-search-failed t)
  7980. (error "No further link found"))))
  7981. (defun org-translate-link (s)
  7982. "Translate a link string if a translation function has been defined."
  7983. (if (and org-link-translation-function
  7984. (fboundp org-link-translation-function)
  7985. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7986. (progn
  7987. (setq s (funcall org-link-translation-function
  7988. (match-string 1) (match-string 2)))
  7989. (concat (car s) ":" (cdr s)))
  7990. s))
  7991. (defun org-translate-link-from-planner (type path)
  7992. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7993. This is still an experimental function, your mileage may vary."
  7994. (cond
  7995. ((member type '("http" "https" "news" "ftp"))
  7996. ;; standard Internet links are the same.
  7997. nil)
  7998. ((and (equal type "irc") (string-match "^//" path))
  7999. ;; Planner has two / at the beginning of an irc link, we have 1.
  8000. ;; We should have zero, actually....
  8001. (setq path (substring path 1)))
  8002. ((and (equal type "lisp") (string-match "^/" path))
  8003. ;; Planner has a slash, we do not.
  8004. (setq type "elisp" path (substring path 1)))
  8005. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8006. ;; A typical message link. Planner has the id after the final slash,
  8007. ;; we separate it with a hash mark
  8008. (setq path (concat (match-string 1 path) "#"
  8009. (org-remove-angle-brackets (match-string 2 path)))))
  8010. )
  8011. (cons type path))
  8012. (defun org-find-file-at-mouse (ev)
  8013. "Open file link or URL at mouse."
  8014. (interactive "e")
  8015. (mouse-set-point ev)
  8016. (org-open-at-point 'in-emacs))
  8017. (defun org-open-at-mouse (ev)
  8018. "Open file link or URL at mouse."
  8019. (interactive "e")
  8020. (mouse-set-point ev)
  8021. (if (eq major-mode 'org-agenda-mode)
  8022. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8023. (org-open-at-point))
  8024. (defvar org-window-config-before-follow-link nil
  8025. "The window configuration before following a link.
  8026. This is saved in case the need arises to restore it.")
  8027. (defvar org-open-link-marker (make-marker)
  8028. "Marker pointing to the location where `org-open-at-point; was called.")
  8029. ;;;###autoload
  8030. (defun org-open-at-point-global ()
  8031. "Follow a link like Org-mode does.
  8032. This command can be called in any mode to follow a link that has
  8033. Org-mode syntax."
  8034. (interactive)
  8035. (org-run-like-in-org-mode 'org-open-at-point))
  8036. ;;;###autoload
  8037. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8038. "Open a link in the string S, as if it was in Org-mode."
  8039. (interactive "sLink: \nP")
  8040. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8041. (with-temp-buffer
  8042. (let ((org-inhibit-startup t))
  8043. (org-mode)
  8044. (insert s)
  8045. (goto-char (point-min))
  8046. (when reference-buffer
  8047. (setq org-link-abbrev-alist-local
  8048. (with-current-buffer reference-buffer
  8049. org-link-abbrev-alist-local)))
  8050. (org-open-at-point arg reference-buffer)))))
  8051. (defvar org-open-at-point-functions nil
  8052. "Hook that is run when following a link at point.
  8053. Functions in this hook must return t if they identify and follow
  8054. a link at point. If they don't find anything interesting at point,
  8055. they must return nil.")
  8056. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8057. "Open link at or after point.
  8058. If there is no link at point, this function will search forward up to
  8059. the end of the current line.
  8060. Normally, files will be opened by an appropriate application. If the
  8061. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8062. With a double prefix argument, try to open outside of Emacs, in the
  8063. application the system uses for this file type."
  8064. (interactive "P")
  8065. ;; if in a code block, then open the block's results
  8066. (unless (call-interactively #'org-babel-open-src-block-result)
  8067. (org-load-modules-maybe)
  8068. (move-marker org-open-link-marker (point))
  8069. (setq org-window-config-before-follow-link (current-window-configuration))
  8070. (org-remove-occur-highlights nil nil t)
  8071. (cond
  8072. ((and (org-on-heading-p)
  8073. (not (org-in-regexp
  8074. (concat org-plain-link-re "\\|"
  8075. org-bracket-link-regexp "\\|"
  8076. org-angle-link-re "\\|"
  8077. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8078. (not (get-text-property (point) 'org-linked-text)))
  8079. (or (org-offer-links-in-entry in-emacs)
  8080. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8081. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8082. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8083. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8084. (org-footnote-action))
  8085. (t
  8086. (let (type path link line search (pos (point)))
  8087. (catch 'match
  8088. (save-excursion
  8089. (skip-chars-forward "^]\n\r")
  8090. (when (org-in-regexp org-bracket-link-regexp 1)
  8091. (setq link (org-extract-attributes
  8092. (org-link-unescape (org-match-string-no-properties 1))))
  8093. (while (string-match " *\n *" link)
  8094. (setq link (replace-match " " t t link)))
  8095. (setq link (org-link-expand-abbrev link))
  8096. (cond
  8097. ((or (file-name-absolute-p link)
  8098. (string-match "^\\.\\.?/" link))
  8099. (setq type "file" path link))
  8100. ((string-match org-link-re-with-space3 link)
  8101. (setq type (match-string 1 link) path (match-string 2 link)))
  8102. (t (setq type "thisfile" path link)))
  8103. (throw 'match t)))
  8104. (when (get-text-property (point) 'org-linked-text)
  8105. (setq type "thisfile"
  8106. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8107. (1+ (point)) (point))
  8108. path (buffer-substring
  8109. (previous-single-property-change pos 'org-linked-text)
  8110. (next-single-property-change pos 'org-linked-text)))
  8111. (throw 'match t))
  8112. (save-excursion
  8113. (when (or (org-in-regexp org-angle-link-re)
  8114. (org-in-regexp org-plain-link-re))
  8115. (setq type (match-string 1) path (match-string 2))
  8116. (throw 'match t)))
  8117. (save-excursion
  8118. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8119. (setq type "tags"
  8120. path (match-string 1))
  8121. (while (string-match ":" path)
  8122. (setq path (replace-match "+" t t path)))
  8123. (throw 'match t)))
  8124. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8125. (setq type "tree-match"
  8126. path (match-string 1))
  8127. (throw 'match t)))
  8128. (unless path
  8129. (error "No link found"))
  8130. ;; switch back to reference buffer
  8131. ;; needed when if called in a temporary buffer through
  8132. ;; org-open-link-from-string
  8133. (with-current-buffer (or reference-buffer (current-buffer))
  8134. ;; Remove any trailing spaces in path
  8135. (if (string-match " +\\'" path)
  8136. (setq path (replace-match "" t t path)))
  8137. (if (and org-link-translation-function
  8138. (fboundp org-link-translation-function))
  8139. ;; Check if we need to translate the link
  8140. (let ((tmp (funcall org-link-translation-function type path)))
  8141. (setq type (car tmp) path (cdr tmp))))
  8142. (cond
  8143. ((assoc type org-link-protocols)
  8144. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8145. ((equal type "mailto")
  8146. (let ((cmd (car org-link-mailto-program))
  8147. (args (cdr org-link-mailto-program)) args1
  8148. (address path) (subject "") a)
  8149. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8150. (setq address (match-string 1 path)
  8151. subject (org-link-escape (match-string 2 path))))
  8152. (while args
  8153. (cond
  8154. ((not (stringp (car args))) (push (pop args) args1))
  8155. (t (setq a (pop args))
  8156. (if (string-match "%a" a)
  8157. (setq a (replace-match address t t a)))
  8158. (if (string-match "%s" a)
  8159. (setq a (replace-match subject t t a)))
  8160. (push a args1))))
  8161. (apply cmd (nreverse args1))))
  8162. ((member type '("http" "https" "ftp" "news"))
  8163. (browse-url (concat type ":" (org-link-escape
  8164. path org-link-escape-chars-browser))))
  8165. ((string= type "doi")
  8166. (browse-url (concat "http://dx.doi.org/"
  8167. (org-link-escape
  8168. path org-link-escape-chars-browser))))
  8169. ((member type '("message"))
  8170. (browse-url (concat type ":" path)))
  8171. ((string= type "tags")
  8172. (org-tags-view in-emacs path))
  8173. ((string= type "tree-match")
  8174. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8175. ((string= type "file")
  8176. (if (string-match "::\\([0-9]+\\)\\'" path)
  8177. (setq line (string-to-number (match-string 1 path))
  8178. path (substring path 0 (match-beginning 0)))
  8179. (if (string-match "::\\(.+\\)\\'" path)
  8180. (setq search (match-string 1 path)
  8181. path (substring path 0 (match-beginning 0)))))
  8182. (if (string-match "[*?{]" (file-name-nondirectory path))
  8183. (dired path)
  8184. (org-open-file path in-emacs line search)))
  8185. ((string= type "news")
  8186. (require 'org-gnus)
  8187. (org-gnus-follow-link path))
  8188. ((string= type "shell")
  8189. (let ((cmd path))
  8190. (if (or (not org-confirm-shell-link-function)
  8191. (funcall org-confirm-shell-link-function
  8192. (format "Execute \"%s\" in shell? "
  8193. (org-add-props cmd nil
  8194. 'face 'org-warning))))
  8195. (progn
  8196. (message "Executing %s" cmd)
  8197. (shell-command cmd))
  8198. (error "Abort"))))
  8199. ((string= type "elisp")
  8200. (let ((cmd path))
  8201. (if (or (not org-confirm-elisp-link-function)
  8202. (funcall org-confirm-elisp-link-function
  8203. (format "Execute \"%s\" as elisp? "
  8204. (org-add-props cmd nil
  8205. 'face 'org-warning))))
  8206. (message "%s => %s" cmd
  8207. (if (equal (string-to-char cmd) ?\()
  8208. (eval (read cmd))
  8209. (call-interactively (read cmd))))
  8210. (error "Abort"))))
  8211. ((and (string= type "thisfile")
  8212. (run-hook-with-args-until-success
  8213. 'org-open-link-functions path)))
  8214. ((string= type "thisfile")
  8215. (if in-emacs
  8216. (switch-to-buffer-other-window
  8217. (org-get-buffer-for-internal-link (current-buffer)))
  8218. (org-mark-ring-push))
  8219. (let ((cmd `(org-link-search
  8220. ,path
  8221. ,(cond ((equal in-emacs '(4)) 'occur)
  8222. ((equal in-emacs '(16)) 'org-occur)
  8223. (t nil))
  8224. ,pos)))
  8225. (condition-case nil (eval cmd)
  8226. (error (progn (widen) (eval cmd))))))
  8227. (t
  8228. (browse-url-at-point)))))))
  8229. (move-marker org-open-link-marker nil)
  8230. (run-hook-with-args 'org-follow-link-hook)))
  8231. (defun org-offer-links-in-entry (&optional nth zero)
  8232. "Offer links in the current entry and follow the selected link.
  8233. If there is only one link, follow it immediately as well.
  8234. If NTH is an integer, immediately pick the NTH link found.
  8235. If ZERO is a string, check also this string for a link, and if
  8236. there is one, offer it as link number zero."
  8237. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8238. "\\(" org-angle-link-re "\\)\\|"
  8239. "\\(" org-plain-link-re "\\)"))
  8240. (cnt ?0)
  8241. (in-emacs (if (integerp nth) nil nth))
  8242. have-zero end links link c)
  8243. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8244. (push (match-string 0 zero) links)
  8245. (setq cnt (1- cnt) have-zero t))
  8246. (save-excursion
  8247. (org-back-to-heading t)
  8248. (setq end (save-excursion (outline-next-heading) (point)))
  8249. (while (re-search-forward re end t)
  8250. (push (match-string 0) links))
  8251. (setq links (org-uniquify (reverse links))))
  8252. (cond
  8253. ((null links)
  8254. (message "No links"))
  8255. ((equal (length links) 1)
  8256. (setq link (list (car links))))
  8257. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8258. (setq link (nth (if have-zero nth (1- nth)) links)))
  8259. (t ; we have to select a link
  8260. (save-excursion
  8261. (save-window-excursion
  8262. (delete-other-windows)
  8263. (with-output-to-temp-buffer "*Select Link*"
  8264. (mapc (lambda (l)
  8265. (if (not (string-match org-bracket-link-regexp l))
  8266. (princ (format "[%c] %s\n" (incf cnt)
  8267. (org-remove-angle-brackets l)))
  8268. (if (match-end 3)
  8269. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8270. (match-string 3 l) (match-string 1 l)))
  8271. (princ (format "[%c] %s\n" (incf cnt)
  8272. (match-string 1 l))))))
  8273. links))
  8274. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8275. (message "Select link to open, RET to open all:")
  8276. (setq c (read-char-exclusive))
  8277. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8278. (when (equal c ?q) (error "Abort"))
  8279. (if (equal c ?\C-m)
  8280. (setq link links)
  8281. (setq nth (- c ?0))
  8282. (if have-zero (setq nth (1+ nth)))
  8283. (unless (and (integerp nth) (>= (length links) nth))
  8284. (error "Invalid link selection"))
  8285. (setq link (list (nth (1- nth) links))))))
  8286. (if link
  8287. (let ((buf (current-buffer)))
  8288. (dolist (l link)
  8289. (org-open-link-from-string l in-emacs buf))
  8290. t)
  8291. nil)))
  8292. ;; Add special file links that specify the way of opening
  8293. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8294. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8295. (defun org-open-file-with-system (path)
  8296. "Open file at PATH using the system way of opening it."
  8297. (org-open-file path 'system))
  8298. (defun org-open-file-with-emacs (path)
  8299. "Open file at PATH in Emacs."
  8300. (org-open-file path 'emacs))
  8301. (defun org-remove-file-link-modifiers ()
  8302. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8303. (goto-char (point-min))
  8304. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8305. (org-if-unprotected
  8306. (replace-match "file:" t t))))
  8307. (eval-after-load "org-exp"
  8308. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8309. 'org-remove-file-link-modifiers))
  8310. ;;;; Time estimates
  8311. (defun org-get-effort (&optional pom)
  8312. "Get the effort estimate for the current entry."
  8313. (org-entry-get pom org-effort-property))
  8314. ;;; File search
  8315. (defvar org-create-file-search-functions nil
  8316. "List of functions to construct the right search string for a file link.
  8317. These functions are called in turn with point at the location to
  8318. which the link should point.
  8319. A function in the hook should first test if it would like to
  8320. handle this file type, for example by checking the `major-mode'
  8321. or the file extension. If it decides not to handle this file, it
  8322. should just return nil to give other functions a chance. If it
  8323. does handle the file, it must return the search string to be used
  8324. when following the link. The search string will be part of the
  8325. file link, given after a double colon, and `org-open-at-point'
  8326. will automatically search for it. If special measures must be
  8327. taken to make the search successful, another function should be
  8328. added to the companion hook `org-execute-file-search-functions',
  8329. which see.
  8330. A function in this hook may also use `setq' to set the variable
  8331. `description' to provide a suggestion for the descriptive text to
  8332. be used for this link when it gets inserted into an Org-mode
  8333. buffer with \\[org-insert-link].")
  8334. (defvar org-execute-file-search-functions nil
  8335. "List of functions to execute a file search triggered by a link.
  8336. Functions added to this hook must accept a single argument, the
  8337. search string that was part of the file link, the part after the
  8338. double colon. The function must first check if it would like to
  8339. handle this search, for example by checking the `major-mode' or
  8340. the file extension. If it decides not to handle this search, it
  8341. should just return nil to give other functions a chance. If it
  8342. does handle the search, it must return a non-nil value to keep
  8343. other functions from trying.
  8344. Each function can access the current prefix argument through the
  8345. variable `current-prefix-argument'. Note that a single prefix is
  8346. used to force opening a link in Emacs, so it may be good to only
  8347. use a numeric or double prefix to guide the search function.
  8348. In case this is needed, a function in this hook can also restore
  8349. the window configuration before `org-open-at-point' was called using:
  8350. (set-window-configuration org-window-config-before-follow-link)")
  8351. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8352. (defun org-link-search (s &optional type avoid-pos)
  8353. "Search for a link search option.
  8354. If S is surrounded by forward slashes, it is interpreted as a
  8355. regular expression. In org-mode files, this will create an `org-occur'
  8356. sparse tree. In ordinary files, `occur' will be used to list matches.
  8357. If the current buffer is in `dired-mode', grep will be used to search
  8358. in all files. If AVOID-POS is given, ignore matches near that position."
  8359. (let ((case-fold-search t)
  8360. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8361. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8362. (append '(("") (" ") ("\t") ("\n"))
  8363. org-emphasis-alist)
  8364. "\\|") "\\)"))
  8365. (pos (point))
  8366. (pre nil) (post nil)
  8367. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8368. (cond
  8369. ;; First check if there are any special search functions
  8370. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8371. ;; Now try the builtin stuff
  8372. ((and (equal (string-to-char s0) ?#)
  8373. (> (length s0) 1)
  8374. (save-excursion
  8375. (goto-char (point-min))
  8376. (and
  8377. (re-search-forward
  8378. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8379. (setq type 'dedicated
  8380. pos (match-beginning 0))))
  8381. ;; There is an exact target for this
  8382. (goto-char pos)
  8383. (org-back-to-heading t)))
  8384. ((save-excursion
  8385. (goto-char (point-min))
  8386. (and
  8387. (re-search-forward
  8388. (concat "<<" (regexp-quote s0) ">>") nil t)
  8389. (setq type 'dedicated
  8390. pos (match-beginning 0))))
  8391. ;; There is an exact target for this
  8392. (goto-char pos))
  8393. ((and (string-match "^(\\(.*\\))$" s0)
  8394. (save-excursion
  8395. (goto-char (point-min))
  8396. (and
  8397. (re-search-forward
  8398. (concat "[^[]" (regexp-quote
  8399. (format org-coderef-label-format
  8400. (match-string 1 s0))))
  8401. nil t)
  8402. (setq type 'dedicated
  8403. pos (1+ (match-beginning 0))))))
  8404. ;; There is a coderef target for this
  8405. (goto-char pos))
  8406. ((string-match "^/\\(.*\\)/$" s)
  8407. ;; A regular expression
  8408. (cond
  8409. ((org-mode-p)
  8410. (org-occur (match-string 1 s)))
  8411. ;;((eq major-mode 'dired-mode)
  8412. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8413. (t (org-do-occur (match-string 1 s)))))
  8414. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8415. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8416. (goto-char (point-min))
  8417. (cond
  8418. ((let (case-fold-search)
  8419. (re-search-forward (format org-complex-heading-regexp-format
  8420. (regexp-quote s))
  8421. nil t))
  8422. ;; OK, found a match
  8423. (goto-char (match-beginning 0)))
  8424. ((and (not org-link-search-inhibit-query)
  8425. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8426. (y-or-n-p "No match - create this as a new heading? "))
  8427. (goto-char (point-max))
  8428. (or (bolp) (newline))
  8429. (insert "* " s "\n")
  8430. (beginning-of-line 0))
  8431. (t
  8432. (goto-char pos)
  8433. (error "No match"))))
  8434. (t
  8435. ;; A normal search string
  8436. (when (equal (string-to-char s) ?*)
  8437. ;; Anchor on headlines, post may include tags.
  8438. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8439. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8440. s (substring s 1)))
  8441. (remove-text-properties
  8442. 0 (length s)
  8443. '(face nil mouse-face nil keymap nil fontified nil) s)
  8444. ;; Make a series of regular expressions to find a match
  8445. (setq words (org-split-string s "[ \n\r\t]+")
  8446. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8447. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8448. "\\)" markers)
  8449. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8450. re2a (concat "[ \t\r\n]" re2a_)
  8451. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8452. re4 (concat "[^a-zA-Z_]" re4_)
  8453. re1 (concat pre re2 post)
  8454. re3 (concat pre (if pre re4_ re4) post)
  8455. re5 (concat pre ".*" re4)
  8456. re2 (concat pre re2)
  8457. re2a (concat pre (if pre re2a_ re2a))
  8458. re4 (concat pre (if pre re4_ re4))
  8459. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8460. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8461. re5 "\\)"
  8462. ))
  8463. (cond
  8464. ((eq type 'org-occur) (org-occur reall))
  8465. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8466. (t (goto-char (point-min))
  8467. (setq type 'fuzzy)
  8468. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8469. (org-search-not-self 1 re1 nil t)
  8470. (org-search-not-self 1 re2 nil t)
  8471. (org-search-not-self 1 re2a nil t)
  8472. (org-search-not-self 1 re3 nil t)
  8473. (org-search-not-self 1 re4 nil t)
  8474. (org-search-not-self 1 re5 nil t)
  8475. )
  8476. (goto-char (match-beginning 1))
  8477. (goto-char pos)
  8478. (error "No match"))))))
  8479. (and (org-mode-p) (org-show-context 'link-search))
  8480. type))
  8481. (defun org-search-not-self (group &rest args)
  8482. "Execute `re-search-forward', but only accept matches that do not
  8483. enclose the position of `org-open-link-marker'."
  8484. (let ((m org-open-link-marker))
  8485. (catch 'exit
  8486. (while (apply 're-search-forward args)
  8487. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8488. (goto-char (match-end group))
  8489. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8490. (> (match-beginning 0) (marker-position m))
  8491. (< (match-end 0) (marker-position m)))
  8492. (save-match-data
  8493. (or (not (org-in-regexp
  8494. org-bracket-link-analytic-regexp 1))
  8495. (not (match-end 4)) ; no description
  8496. (and (<= (match-beginning 4) (point))
  8497. (>= (match-end 4) (point))))))
  8498. (throw 'exit (point))))))))
  8499. (defun org-get-buffer-for-internal-link (buffer)
  8500. "Return a buffer to be used for displaying the link target of internal links."
  8501. (cond
  8502. ((not org-display-internal-link-with-indirect-buffer)
  8503. buffer)
  8504. ((string-match "(Clone)$" (buffer-name buffer))
  8505. (message "Buffer is already a clone, not making another one")
  8506. ;; we also do not modify visibility in this case
  8507. buffer)
  8508. (t ; make a new indirect buffer for displaying the link
  8509. (let* ((bn (buffer-name buffer))
  8510. (ibn (concat bn "(Clone)"))
  8511. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8512. (with-current-buffer ib (org-overview))
  8513. ib))))
  8514. (defun org-do-occur (regexp &optional cleanup)
  8515. "Call the Emacs command `occur'.
  8516. If CLEANUP is non-nil, remove the printout of the regular expression
  8517. in the *Occur* buffer. This is useful if the regex is long and not useful
  8518. to read."
  8519. (occur regexp)
  8520. (when cleanup
  8521. (let ((cwin (selected-window)) win beg end)
  8522. (when (setq win (get-buffer-window "*Occur*"))
  8523. (select-window win))
  8524. (goto-char (point-min))
  8525. (when (re-search-forward "match[a-z]+" nil t)
  8526. (setq beg (match-end 0))
  8527. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8528. (setq end (1- (match-beginning 0)))))
  8529. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8530. (goto-char (point-min))
  8531. (select-window cwin))))
  8532. ;;; The mark ring for links jumps
  8533. (defvar org-mark-ring nil
  8534. "Mark ring for positions before jumps in Org-mode.")
  8535. (defvar org-mark-ring-last-goto nil
  8536. "Last position in the mark ring used to go back.")
  8537. ;; Fill and close the ring
  8538. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8539. (loop for i from 1 to org-mark-ring-length do
  8540. (push (make-marker) org-mark-ring))
  8541. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8542. org-mark-ring)
  8543. (defun org-mark-ring-push (&optional pos buffer)
  8544. "Put the current position or POS into the mark ring and rotate it."
  8545. (interactive)
  8546. (setq pos (or pos (point)))
  8547. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8548. (move-marker (car org-mark-ring)
  8549. (or pos (point))
  8550. (or buffer (current-buffer)))
  8551. (message "%s"
  8552. (substitute-command-keys
  8553. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8554. (defun org-mark-ring-goto (&optional n)
  8555. "Jump to the previous position in the mark ring.
  8556. With prefix arg N, jump back that many stored positions. When
  8557. called several times in succession, walk through the entire ring.
  8558. Org-mode commands jumping to a different position in the current file,
  8559. or to another Org-mode file, automatically push the old position
  8560. onto the ring."
  8561. (interactive "p")
  8562. (let (p m)
  8563. (if (eq last-command this-command)
  8564. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8565. (setq p org-mark-ring))
  8566. (setq org-mark-ring-last-goto p)
  8567. (setq m (car p))
  8568. (switch-to-buffer (marker-buffer m))
  8569. (goto-char m)
  8570. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8571. (defun org-remove-angle-brackets (s)
  8572. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8573. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8574. s)
  8575. (defun org-add-angle-brackets (s)
  8576. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8577. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8578. s)
  8579. (defun org-remove-double-quotes (s)
  8580. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8581. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8582. s)
  8583. ;;; Following specific links
  8584. (defun org-follow-timestamp-link ()
  8585. (cond
  8586. ((org-at-date-range-p t)
  8587. (let ((org-agenda-start-on-weekday)
  8588. (t1 (match-string 1))
  8589. (t2 (match-string 2)))
  8590. (setq t1 (time-to-days (org-time-string-to-time t1))
  8591. t2 (time-to-days (org-time-string-to-time t2)))
  8592. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8593. ((org-at-timestamp-p t)
  8594. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8595. (substring (match-string 1) 0 10)))
  8596. 1))
  8597. (t (error "This should not happen"))))
  8598. ;;; Following file links
  8599. (defvar org-wait nil)
  8600. (defun org-open-file (path &optional in-emacs line search)
  8601. "Open the file at PATH.
  8602. First, this expands any special file name abbreviations. Then the
  8603. configuration variable `org-file-apps' is checked if it contains an
  8604. entry for this file type, and if yes, the corresponding command is launched.
  8605. If no application is found, Emacs simply visits the file.
  8606. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8607. With a double \\[universal-argument] \\[universal-argument] \
  8608. prefix arg, Org tries to avoid opening in Emacs
  8609. and to use an external application to visit the file.
  8610. Optional LINE specifies a line to go to, optional SEARCH a string
  8611. to search for. If LINE or SEARCH is given, the file will be
  8612. opened in Emacs, unless an entry from org-file-apps that makes
  8613. use of groups in a regexp matches.
  8614. If the file does not exist, an error is thrown."
  8615. (let* ((file (if (equal path "")
  8616. buffer-file-name
  8617. (substitute-in-file-name (expand-file-name path))))
  8618. (file-apps (append org-file-apps (org-default-apps)))
  8619. (apps (org-remove-if
  8620. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8621. (apps-dlink (org-remove-if-not
  8622. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8623. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8624. (dirp (if remp nil (file-directory-p file)))
  8625. (file (if (and dirp org-open-directory-means-index-dot-org)
  8626. (concat (file-name-as-directory file) "index.org")
  8627. file))
  8628. (a-m-a-p (assq 'auto-mode apps))
  8629. (dfile (downcase file))
  8630. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8631. (link (cond ((and (eq line nil)
  8632. (eq search nil))
  8633. file)
  8634. (line
  8635. (concat file "::" (number-to-string line)))
  8636. (search
  8637. (concat file "::" search))))
  8638. (dlink (downcase link))
  8639. (old-buffer (current-buffer))
  8640. (old-pos (point))
  8641. (old-mode major-mode)
  8642. ext cmd link-match-data)
  8643. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8644. (setq ext (match-string 1 dfile))
  8645. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8646. (setq ext (match-string 1 dfile))))
  8647. (cond
  8648. ((member in-emacs '((16) system))
  8649. (setq cmd (cdr (assoc 'system apps))))
  8650. (in-emacs (setq cmd 'emacs))
  8651. (t
  8652. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8653. (and dirp (cdr (assoc 'directory apps)))
  8654. ; first, try matching against apps-dlink
  8655. ; if we get a match here, store the match data for later
  8656. (let ((match (assoc-default dlink apps-dlink
  8657. 'string-match)))
  8658. (if match
  8659. (progn (setq link-match-data (match-data))
  8660. match)
  8661. (progn (setq in-emacs (or in-emacs line search))
  8662. nil))) ; if we have no match in apps-dlink,
  8663. ; always open the file in emacs if line or search
  8664. ; is given (for backwards compatibility)
  8665. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8666. 'string-match)
  8667. (cdr (assoc ext apps))
  8668. (cdr (assoc t apps))))))
  8669. (when (eq cmd 'system)
  8670. (setq cmd (cdr (assoc 'system apps))))
  8671. (when (eq cmd 'default)
  8672. (setq cmd (cdr (assoc t apps))))
  8673. (when (eq cmd 'mailcap)
  8674. (require 'mailcap)
  8675. (mailcap-parse-mailcaps)
  8676. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8677. (command (mailcap-mime-info mime-type)))
  8678. (if (stringp command)
  8679. (setq cmd command)
  8680. (setq cmd 'emacs))))
  8681. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8682. (not (file-exists-p file))
  8683. (not org-open-non-existing-files))
  8684. (error "No such file: %s" file))
  8685. (cond
  8686. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8687. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8688. (while (string-match "['\"]%s['\"]" cmd)
  8689. (setq cmd (replace-match "%s" t t cmd)))
  8690. (while (string-match "%s" cmd)
  8691. (setq cmd (replace-match
  8692. (save-match-data
  8693. (shell-quote-argument
  8694. (convert-standard-filename file)))
  8695. t t cmd)))
  8696. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8697. (save-match-data
  8698. (let ((match-index 1)
  8699. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8700. (set-match-data link-match-data)
  8701. (while (<= match-index number-of-groups)
  8702. (let ((regex (concat "%" (number-to-string match-index)))
  8703. (replace-with (match-string match-index dlink)))
  8704. (while (string-match regex cmd)
  8705. (setq cmd (replace-match replace-with t t cmd))))
  8706. (setq match-index (+ match-index 1)))))
  8707. (save-window-excursion
  8708. (start-process-shell-command cmd nil cmd)
  8709. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8710. ))
  8711. ((or (stringp cmd)
  8712. (eq cmd 'emacs))
  8713. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8714. (widen)
  8715. (if line (org-goto-line line)
  8716. (if search (org-link-search search))))
  8717. ((consp cmd)
  8718. (let ((file (convert-standard-filename file)))
  8719. (save-match-data
  8720. (set-match-data link-match-data)
  8721. (eval cmd))))
  8722. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8723. (and (org-mode-p) (eq old-mode 'org-mode)
  8724. (or (not (equal old-buffer (current-buffer)))
  8725. (not (equal old-pos (point))))
  8726. (org-mark-ring-push old-pos old-buffer))))
  8727. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8728. "This function returns non-nil if `entry' uses a regular
  8729. expression which should be matched against the whole link by
  8730. org-open-file.
  8731. It assumes that is the case when the entry uses a regular
  8732. expression which has at least one grouping construct and the
  8733. action is either a lisp form or a command string containing
  8734. '%1', i.e. using at least one subexpression match as a
  8735. parameter."
  8736. (let ((selector (car entry))
  8737. (action (cdr entry)))
  8738. (if (stringp selector)
  8739. (and (> (regexp-opt-depth selector) 0)
  8740. (or (and (stringp action)
  8741. (string-match "%[0-9]" action))
  8742. (consp action)))
  8743. nil)))
  8744. (defun org-default-apps ()
  8745. "Return the default applications for this operating system."
  8746. (cond
  8747. ((eq system-type 'darwin)
  8748. org-file-apps-defaults-macosx)
  8749. ((eq system-type 'windows-nt)
  8750. org-file-apps-defaults-windowsnt)
  8751. (t org-file-apps-defaults-gnu)))
  8752. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8753. "Convert extensions to regular expressions in the cars of LIST.
  8754. Also, weed out any non-string entries, because the return value is used
  8755. only for regexp matching.
  8756. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8757. point to the symbol `emacs', indicating that the file should
  8758. be opened in Emacs."
  8759. (append
  8760. (delq nil
  8761. (mapcar (lambda (x)
  8762. (if (not (stringp (car x)))
  8763. nil
  8764. (if (string-match "\\W" (car x))
  8765. x
  8766. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8767. list))
  8768. (if add-auto-mode
  8769. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8770. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8771. (defun org-file-remote-p (file)
  8772. "Test whether FILE specifies a location on a remote system.
  8773. Return non-nil if the location is indeed remote.
  8774. For example, the filename \"/user@host:/foo\" specifies a location
  8775. on the system \"/user@host:\"."
  8776. (cond ((fboundp 'file-remote-p)
  8777. (file-remote-p file))
  8778. ((fboundp 'tramp-handle-file-remote-p)
  8779. (tramp-handle-file-remote-p file))
  8780. ((and (boundp 'ange-ftp-name-format)
  8781. (string-match (car ange-ftp-name-format) file))
  8782. t)
  8783. (t nil)))
  8784. ;;;; Refiling
  8785. (defun org-get-org-file ()
  8786. "Read a filename, with default directory `org-directory'."
  8787. (let ((default (or org-default-notes-file remember-data-file)))
  8788. (read-file-name (format "File name [%s]: " default)
  8789. (file-name-as-directory org-directory)
  8790. default)))
  8791. (defun org-notes-order-reversed-p ()
  8792. "Check if the current file should receive notes in reversed order."
  8793. (cond
  8794. ((not org-reverse-note-order) nil)
  8795. ((eq t org-reverse-note-order) t)
  8796. ((not (listp org-reverse-note-order)) nil)
  8797. (t (catch 'exit
  8798. (let ((all org-reverse-note-order)
  8799. entry)
  8800. (while (setq entry (pop all))
  8801. (if (string-match (car entry) buffer-file-name)
  8802. (throw 'exit (cdr entry))))
  8803. nil)))))
  8804. (defvar org-refile-target-table nil
  8805. "The list of refile targets, created by `org-refile'.")
  8806. (defvar org-agenda-new-buffers nil
  8807. "Buffers created to visit agenda files.")
  8808. (defvar org-refile-cache nil
  8809. "Cache for refile targets.")
  8810. (defvar org-refile-markers nil
  8811. "All the markers used for caching refile locations.")
  8812. (defun org-refile-marker (pos)
  8813. "Get a new refile marker, but only if caching is in use."
  8814. (if (not org-refile-use-cache)
  8815. pos
  8816. (let ((m (make-marker)))
  8817. (move-marker m pos)
  8818. (push m org-refile-markers)
  8819. m)))
  8820. (defun org-refile-cache-clear ()
  8821. "Clear the refile cache and disable all the markers."
  8822. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8823. (setq org-refile-markers nil)
  8824. (setq org-refile-cache nil)
  8825. (message "Refile cache has been cleared"))
  8826. (defun org-refile-cache-check-set (set)
  8827. "Check if all the markers in the cache still have live buffers."
  8828. (let (marker)
  8829. (catch 'exit
  8830. (while (and set (setq marker (nth 3 (pop set))))
  8831. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8832. (when (and marker (null (marker-buffer marker)))
  8833. (message "not found") (sit-for 3)
  8834. (throw 'exit nil)))
  8835. t)))
  8836. (defun org-refile-cache-put (set &rest identifiers)
  8837. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8838. (let* ((key (sha1 (prin1-to-string identifiers)))
  8839. (entry (assoc key org-refile-cache)))
  8840. (if entry
  8841. (setcdr entry set)
  8842. (push (cons key set) org-refile-cache))))
  8843. (defun org-refile-cache-get (&rest identifiers)
  8844. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8845. (cond
  8846. ((not org-refile-cache) nil)
  8847. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8848. (t
  8849. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8850. org-refile-cache))))
  8851. (and set (org-refile-cache-check-set set) set)))))
  8852. (defun org-get-refile-targets (&optional default-buffer)
  8853. "Produce a table with refile targets."
  8854. (let ((case-fold-search nil)
  8855. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8856. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8857. targets tgs txt re files f desc descre fast-path-p level pos0)
  8858. (message "Getting targets...")
  8859. (with-current-buffer (or default-buffer (current-buffer))
  8860. (while (setq entry (pop entries))
  8861. (setq files (car entry) desc (cdr entry))
  8862. (setq fast-path-p nil)
  8863. (cond
  8864. ((null files) (setq files (list (current-buffer))))
  8865. ((eq files 'org-agenda-files)
  8866. (setq files (org-agenda-files 'unrestricted)))
  8867. ((and (symbolp files) (fboundp files))
  8868. (setq files (funcall files)))
  8869. ((and (symbolp files) (boundp files))
  8870. (setq files (symbol-value files))))
  8871. (if (stringp files) (setq files (list files)))
  8872. (cond
  8873. ((eq (car desc) :tag)
  8874. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8875. ((eq (car desc) :todo)
  8876. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8877. ((eq (car desc) :regexp)
  8878. (setq descre (cdr desc)))
  8879. ((eq (car desc) :level)
  8880. (setq descre (concat "^\\*\\{" (number-to-string
  8881. (if org-odd-levels-only
  8882. (1- (* 2 (cdr desc)))
  8883. (cdr desc)))
  8884. "\\}[ \t]")))
  8885. ((eq (car desc) :maxlevel)
  8886. (setq fast-path-p t)
  8887. (setq descre (concat "^\\*\\{1," (number-to-string
  8888. (if org-odd-levels-only
  8889. (1- (* 2 (cdr desc)))
  8890. (cdr desc)))
  8891. "\\}[ \t]")))
  8892. (t (error "Bad refiling target description %s" desc)))
  8893. (while (setq f (pop files))
  8894. (with-current-buffer
  8895. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8896. (or
  8897. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8898. (progn
  8899. (if (bufferp f) (setq f (buffer-file-name
  8900. (buffer-base-buffer f))))
  8901. (setq f (and f (expand-file-name f)))
  8902. (if (eq org-refile-use-outline-path 'file)
  8903. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8904. (save-excursion
  8905. (save-restriction
  8906. (widen)
  8907. (goto-char (point-min))
  8908. (while (re-search-forward descre nil t)
  8909. (goto-char (setq pos0 (point-at-bol)))
  8910. (catch 'next
  8911. (when org-refile-target-verify-function
  8912. (save-match-data
  8913. (or (funcall org-refile-target-verify-function)
  8914. (throw 'next t))))
  8915. (when (looking-at org-complex-heading-regexp)
  8916. (setq level (org-reduced-level
  8917. (- (match-end 1) (match-beginning 1)))
  8918. txt (org-link-display-format (match-string 4))
  8919. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  8920. re (format org-complex-heading-regexp-format
  8921. (regexp-quote (match-string 4))))
  8922. (when org-refile-use-outline-path
  8923. (setq txt (mapconcat
  8924. 'org-protect-slash
  8925. (append
  8926. (if (eq org-refile-use-outline-path
  8927. 'file)
  8928. (list (file-name-nondirectory
  8929. (buffer-file-name
  8930. (buffer-base-buffer))))
  8931. (if (eq org-refile-use-outline-path
  8932. 'full-file-path)
  8933. (list (buffer-file-name
  8934. (buffer-base-buffer)))))
  8935. (org-get-outline-path fast-path-p
  8936. level txt)
  8937. (list txt))
  8938. "/")))
  8939. (push (list txt f re (org-refile-marker (point)))
  8940. tgs)))
  8941. (when (= (point) pos0)
  8942. ;; verification function has not moved point
  8943. (goto-char (point-at-eol))))))))
  8944. (when org-refile-use-cache
  8945. (org-refile-cache-put tgs (buffer-file-name) descre))
  8946. (setq targets (append tgs targets))
  8947. ))))
  8948. (message "Getting targets...done")
  8949. (nreverse targets)))
  8950. (defun org-protect-slash (s)
  8951. (while (string-match "/" s)
  8952. (setq s (replace-match "\\" t t s)))
  8953. s)
  8954. (defvar org-olpa (make-vector 20 nil))
  8955. (defun org-get-outline-path (&optional fastp level heading)
  8956. "Return the outline path to the current entry, as a list.
  8957. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  8958. routine which makes outline path derivations for an entire file,
  8959. avoiding backtracing. Refile target collection makes use of that."
  8960. (if fastp
  8961. (progn
  8962. (if (> level 19)
  8963. (error "Outline path failure, more than 19 levels"))
  8964. (loop for i from level upto 19 do
  8965. (aset org-olpa i nil))
  8966. (prog1
  8967. (delq nil (append org-olpa nil))
  8968. (aset org-olpa level heading)))
  8969. (let (rtn case-fold-search)
  8970. (save-excursion
  8971. (save-restriction
  8972. (widen)
  8973. (while (org-up-heading-safe)
  8974. (when (looking-at org-complex-heading-regexp)
  8975. (push (org-match-string-no-properties 4) rtn)))
  8976. rtn)))))
  8977. (defun org-format-outline-path (path &optional width prefix)
  8978. "Format the outline path PATH for display.
  8979. Width is the maximum number of characters that is available.
  8980. Prefix is a prefix to be included in the returned string,
  8981. such as the file name."
  8982. (setq width (or width 79))
  8983. (if prefix (setq width (- width (length prefix))))
  8984. (if (not path)
  8985. (or prefix "")
  8986. (let* ((nsteps (length path))
  8987. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8988. (maxwidth (if (<= total-width width)
  8989. 10000 ;; everything fits
  8990. ;; we need to shorten the level headings
  8991. (/ (- width nsteps) nsteps)))
  8992. (org-odd-levels-only nil)
  8993. (n 0)
  8994. (total (1+ (length prefix))))
  8995. (setq maxwidth (max maxwidth 10))
  8996. (concat prefix
  8997. (mapconcat
  8998. (lambda (h)
  8999. (setq n (1+ n))
  9000. (if (and (= n nsteps) (< maxwidth 10000))
  9001. (setq maxwidth (- total-width total)))
  9002. (if (< (length h) maxwidth)
  9003. (progn (setq total (+ total (length h) 1)) h)
  9004. (setq h (substring h 0 (- maxwidth 2))
  9005. total (+ total maxwidth 1))
  9006. (if (string-match "[ \t]+\\'" h)
  9007. (setq h (substring h 0 (match-beginning 0))))
  9008. (setq h (concat h "..")))
  9009. (org-add-props h nil 'face
  9010. (nth (% (1- n) org-n-level-faces)
  9011. org-level-faces))
  9012. h)
  9013. path "/")))))
  9014. (defun org-display-outline-path (&optional file current)
  9015. "Display the current outline path in the echo area."
  9016. (interactive "P")
  9017. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9018. (case-fold-search nil)
  9019. (path (and (org-mode-p) (org-get-outline-path))))
  9020. (if current (setq path (append path
  9021. (save-excursion
  9022. (org-back-to-heading t)
  9023. (if (looking-at org-complex-heading-regexp)
  9024. (list (match-string 4)))))))
  9025. (message "%s"
  9026. (org-format-outline-path
  9027. path
  9028. (1- (frame-width))
  9029. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9030. (defvar org-refile-history nil
  9031. "History for refiling operations.")
  9032. (defvar org-after-refile-insert-hook nil
  9033. "Hook run after `org-refile' has inserted its stuff at the new location.
  9034. Note that this is still *before* the stuff will be removed from
  9035. the *old* location.")
  9036. (defvar org-capture-last-stored-marker)
  9037. (defun org-refile (&optional goto default-buffer rfloc)
  9038. "Move the entry at point to another heading.
  9039. The list of target headings is compiled using the information in
  9040. `org-refile-targets', which see. This list is created before each use
  9041. and will therefore always be up-to-date.
  9042. At the target location, the entry is filed as a subitem of the target heading.
  9043. Depending on `org-reverse-note-order', the new subitem will either be the
  9044. first or the last subitem.
  9045. If there is an active region, all entries in that region will be moved.
  9046. However, the region must fulfill the requirement that the first heading
  9047. is the first one sets the top-level of the moved text - at most siblings
  9048. below it are allowed.
  9049. With prefix arg GOTO, the command will only visit the target location,
  9050. not actually move anything.
  9051. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9052. go to the location where the last refiling
  9053. operation has put the subtree.
  9054. With a prefix argument of `2', refile to the running clock.
  9055. RFLOC can be a refile location obtained in a different way.
  9056. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9057. If you are using target caching (see `org-refile-use-cache'),
  9058. You have to clear the target cache in order to find new targets.
  9059. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9060. (interactive "P")
  9061. (if (member goto '(0 (64)))
  9062. (org-refile-cache-clear)
  9063. (let* ((cbuf (current-buffer))
  9064. (regionp (org-region-active-p))
  9065. (region-start (and regionp (region-beginning)))
  9066. (region-end (and regionp (region-end)))
  9067. (region-length (and regionp (- region-end region-start)))
  9068. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9069. pos it nbuf file re level reversed)
  9070. (setq last-command nil)
  9071. (when regionp
  9072. (goto-char region-start)
  9073. (or (bolp) (goto-char (point-at-bol)))
  9074. (setq region-start (point))
  9075. (unless (org-kill-is-subtree-p
  9076. (buffer-substring region-start region-end))
  9077. (error "The region is not a (sequence of) subtree(s)")))
  9078. (if (equal goto '(16))
  9079. (org-refile-goto-last-stored)
  9080. (when (or
  9081. (and (equal goto 2)
  9082. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9083. (prog1
  9084. (setq it (list (or org-clock-heading "running clock")
  9085. (buffer-file-name
  9086. (marker-buffer org-clock-hd-marker))
  9087. ""
  9088. (marker-position org-clock-hd-marker)))
  9089. (setq goto nil)))
  9090. (setq it (or rfloc
  9091. (save-excursion
  9092. (org-refile-get-location
  9093. (if goto "Goto: " "Refile to: ") default-buffer
  9094. org-refile-allow-creating-parent-nodes)))))
  9095. (setq file (nth 1 it)
  9096. re (nth 2 it)
  9097. pos (nth 3 it))
  9098. (if (and (not goto)
  9099. pos
  9100. (equal (buffer-file-name) file)
  9101. (if regionp
  9102. (and (>= pos region-start)
  9103. (<= pos region-end))
  9104. (and (>= pos (point))
  9105. (< pos (save-excursion
  9106. (org-end-of-subtree t t))))))
  9107. (error "Cannot refile to position inside the tree or region"))
  9108. (setq nbuf (or (find-buffer-visiting file)
  9109. (find-file-noselect file)))
  9110. (if goto
  9111. (progn
  9112. (switch-to-buffer nbuf)
  9113. (goto-char pos)
  9114. (org-show-context 'org-goto))
  9115. (if regionp
  9116. (progn
  9117. (org-kill-new (buffer-substring region-start region-end))
  9118. (org-save-markers-in-region region-start region-end))
  9119. (org-copy-subtree 1 nil t))
  9120. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9121. (find-file-noselect file)))
  9122. (setq reversed (org-notes-order-reversed-p))
  9123. (save-excursion
  9124. (save-restriction
  9125. (widen)
  9126. (if pos
  9127. (progn
  9128. (goto-char pos)
  9129. (looking-at outline-regexp)
  9130. (setq level (org-get-valid-level (funcall outline-level) 1))
  9131. (goto-char
  9132. (if reversed
  9133. (or (outline-next-heading) (point-max))
  9134. (or (save-excursion (org-get-next-sibling))
  9135. (org-end-of-subtree t t)
  9136. (point-max)))))
  9137. (setq level 1)
  9138. (if (not reversed)
  9139. (goto-char (point-max))
  9140. (goto-char (point-min))
  9141. (or (outline-next-heading) (goto-char (point-max)))))
  9142. (if (not (bolp)) (newline))
  9143. (org-paste-subtree level)
  9144. (when org-log-refile
  9145. (org-add-log-setup 'refile nil nil 'findpos
  9146. org-log-refile)
  9147. (unless (eq org-log-refile 'note)
  9148. (save-excursion (org-add-log-note))))
  9149. (and org-auto-align-tags (org-set-tags nil t))
  9150. (bookmark-set "org-refile-last-stored")
  9151. ;; If we are refiling for capture, make sure that the
  9152. ;; last-capture pointers point here
  9153. (when (org-bound-and-true-p org-refile-for-capture)
  9154. (bookmark-set "org-capture-last-stored-marker")
  9155. (move-marker org-capture-last-stored-marker (point)))
  9156. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9157. (run-hooks 'org-after-refile-insert-hook))))
  9158. (if regionp
  9159. (delete-region (point) (+ (point) region-length))
  9160. (org-cut-subtree))
  9161. (when (featurep 'org-inlinetask)
  9162. (org-inlinetask-remove-END-maybe))
  9163. (setq org-markers-to-move nil)
  9164. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9165. (defun org-refile-goto-last-stored ()
  9166. "Go to the location where the last refile was stored."
  9167. (interactive)
  9168. (bookmark-jump "org-refile-last-stored")
  9169. (message "This is the location of the last refile"))
  9170. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9171. "Prompt the user for a refile location, using PROMPT."
  9172. (let ((org-refile-targets org-refile-targets)
  9173. (org-refile-use-outline-path org-refile-use-outline-path))
  9174. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9175. (unless org-refile-target-table
  9176. (error "No refile targets"))
  9177. (let* ((cbuf (current-buffer))
  9178. (partial-completion-mode nil)
  9179. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9180. (cfunc (if (and org-refile-use-outline-path
  9181. org-outline-path-complete-in-steps)
  9182. 'org-olpath-completing-read
  9183. 'org-icompleting-read))
  9184. (extra (if org-refile-use-outline-path "/" ""))
  9185. (filename (and cfn (expand-file-name cfn)))
  9186. (tbl (mapcar
  9187. (lambda (x)
  9188. (if (and (not (member org-refile-use-outline-path
  9189. '(file full-file-path)))
  9190. (not (equal filename (nth 1 x))))
  9191. (cons (concat (car x) extra " ("
  9192. (file-name-nondirectory (nth 1 x)) ")")
  9193. (cdr x))
  9194. (cons (concat (car x) extra) (cdr x))))
  9195. org-refile-target-table))
  9196. (completion-ignore-case t)
  9197. pa answ parent-target child parent old-hist)
  9198. (setq old-hist org-refile-history)
  9199. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9200. nil 'org-refile-history))
  9201. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9202. (org-refile-check-position pa)
  9203. (if pa
  9204. (progn
  9205. (when (or (not org-refile-history)
  9206. (not (eq old-hist org-refile-history))
  9207. (not (equal (car pa) (car org-refile-history))))
  9208. (setq org-refile-history
  9209. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9210. org-refile-history
  9211. (cdr org-refile-history))))
  9212. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9213. (pop org-refile-history)))
  9214. pa)
  9215. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9216. (progn
  9217. (setq parent (match-string 1 answ)
  9218. child (match-string 2 answ))
  9219. (setq parent-target (or (assoc parent tbl)
  9220. (assoc (concat parent "/") tbl)))
  9221. (when (and parent-target
  9222. (or (eq new-nodes t)
  9223. (and (eq new-nodes 'confirm)
  9224. (y-or-n-p (format "Create new node \"%s\"? "
  9225. child)))))
  9226. (org-refile-new-child parent-target child)))
  9227. (error "Invalid target location")))))
  9228. (defun org-refile-check-position (refile-pointer)
  9229. "Check if the refile pointer matches the readline to which it points."
  9230. (let* ((file (nth 1 refile-pointer))
  9231. (re (nth 2 refile-pointer))
  9232. (pos (nth 3 refile-pointer))
  9233. buffer)
  9234. (when (org-string-nw-p re)
  9235. (setq buffer (if (markerp pos)
  9236. (marker-buffer pos)
  9237. (or (find-buffer-visiting file)
  9238. (find-file-noselect file))))
  9239. (with-current-buffer buffer
  9240. (save-excursion
  9241. (save-restriction
  9242. (widen)
  9243. (goto-char pos)
  9244. (beginning-of-line 1)
  9245. (unless (org-looking-at-p re)
  9246. (error "Invalid refile position, please rebuild the cache"))))))))
  9247. (defun org-refile-new-child (parent-target child)
  9248. "Use refile target PARENT-TARGET to add new CHILD below it."
  9249. (unless parent-target
  9250. (error "Cannot find parent for new node"))
  9251. (let ((file (nth 1 parent-target))
  9252. (pos (nth 3 parent-target))
  9253. level)
  9254. (with-current-buffer (or (find-buffer-visiting file)
  9255. (find-file-noselect file))
  9256. (save-excursion
  9257. (save-restriction
  9258. (widen)
  9259. (if pos
  9260. (goto-char pos)
  9261. (goto-char (point-max))
  9262. (if (not (bolp)) (newline)))
  9263. (when (looking-at outline-regexp)
  9264. (setq level (funcall outline-level))
  9265. (org-end-of-subtree t t))
  9266. (org-back-over-empty-lines)
  9267. (insert "\n" (make-string
  9268. (if pos (org-get-valid-level level 1) 1) ?*)
  9269. " " child "\n")
  9270. (beginning-of-line 0)
  9271. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9272. (defun org-olpath-completing-read (prompt collection &rest args)
  9273. "Read an outline path like a file name."
  9274. (let ((thetable collection)
  9275. (org-completion-use-ido nil) ; does not work with ido.
  9276. (org-completion-use-iswitchb nil)) ; or iswitchb
  9277. (apply
  9278. 'org-icompleting-read prompt
  9279. (lambda (string predicate &optional flag)
  9280. (let (rtn r f (l (length string)))
  9281. (cond
  9282. ((eq flag nil)
  9283. ;; try completion
  9284. (try-completion string thetable))
  9285. ((eq flag t)
  9286. ;; all-completions
  9287. (setq rtn (all-completions string thetable predicate))
  9288. (mapcar
  9289. (lambda (x)
  9290. (setq r (substring x l))
  9291. (if (string-match " ([^)]*)$" x)
  9292. (setq f (match-string 0 x))
  9293. (setq f ""))
  9294. (if (string-match "/" r)
  9295. (concat string (substring r 0 (match-end 0)) f)
  9296. x))
  9297. rtn))
  9298. ((eq flag 'lambda)
  9299. ;; exact match?
  9300. (assoc string thetable)))
  9301. ))
  9302. args)))
  9303. ;;;; Dynamic blocks
  9304. (defun org-find-dblock (name)
  9305. "Find the first dynamic block with name NAME in the buffer.
  9306. If not found, stay at current position and return nil."
  9307. (let (pos)
  9308. (save-excursion
  9309. (goto-char (point-min))
  9310. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9311. nil t)
  9312. (match-beginning 0))))
  9313. (if pos (goto-char pos))
  9314. pos))
  9315. (defconst org-dblock-start-re
  9316. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9317. "Matches the start line of a dynamic block, with parameters.")
  9318. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9319. "Matches the end of a dynamic block.")
  9320. (defun org-create-dblock (plist)
  9321. "Create a dynamic block section, with parameters taken from PLIST.
  9322. PLIST must contain a :name entry which is used as name of the block."
  9323. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9324. (end-of-line 1)
  9325. (newline))
  9326. (let ((col (current-column))
  9327. (name (plist-get plist :name)))
  9328. (insert "#+BEGIN: " name)
  9329. (while plist
  9330. (if (eq (car plist) :name)
  9331. (setq plist (cddr plist))
  9332. (insert " " (prin1-to-string (pop plist)))))
  9333. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9334. (beginning-of-line -2)))
  9335. (defun org-prepare-dblock ()
  9336. "Prepare dynamic block for refresh.
  9337. This empties the block, puts the cursor at the insert position and returns
  9338. the property list including an extra property :name with the block name."
  9339. (unless (looking-at org-dblock-start-re)
  9340. (error "Not at a dynamic block"))
  9341. (let* ((begdel (1+ (match-end 0)))
  9342. (name (org-no-properties (match-string 1)))
  9343. (params (append (list :name name)
  9344. (read (concat "(" (match-string 3) ")")))))
  9345. (save-excursion
  9346. (beginning-of-line 1)
  9347. (skip-chars-forward " \t")
  9348. (setq params (plist-put params :indentation-column (current-column))))
  9349. (unless (re-search-forward org-dblock-end-re nil t)
  9350. (error "Dynamic block not terminated"))
  9351. (setq params
  9352. (append params
  9353. (list :content (buffer-substring
  9354. begdel (match-beginning 0)))))
  9355. (delete-region begdel (match-beginning 0))
  9356. (goto-char begdel)
  9357. (open-line 1)
  9358. params))
  9359. (defun org-map-dblocks (&optional command)
  9360. "Apply COMMAND to all dynamic blocks in the current buffer.
  9361. If COMMAND is not given, use `org-update-dblock'."
  9362. (let ((cmd (or command 'org-update-dblock)))
  9363. (save-excursion
  9364. (goto-char (point-min))
  9365. (while (re-search-forward org-dblock-start-re nil t)
  9366. (goto-char (match-beginning 0))
  9367. (save-excursion
  9368. (condition-case nil
  9369. (funcall cmd)
  9370. (error (message "Error during update of dynamic block"))))
  9371. (unless (re-search-forward org-dblock-end-re nil t)
  9372. (error "Dynamic block not terminated"))))))
  9373. (defun org-dblock-update (&optional arg)
  9374. "User command for updating dynamic blocks.
  9375. Update the dynamic block at point. With prefix ARG, update all dynamic
  9376. blocks in the buffer."
  9377. (interactive "P")
  9378. (if arg
  9379. (org-update-all-dblocks)
  9380. (or (looking-at org-dblock-start-re)
  9381. (org-beginning-of-dblock))
  9382. (org-update-dblock)))
  9383. (defun org-update-dblock ()
  9384. "Update the dynamic block at point.
  9385. This means to empty the block, parse for parameters and then call
  9386. the correct writing function."
  9387. (save-window-excursion
  9388. (let* ((pos (point))
  9389. (line (org-current-line))
  9390. (params (org-prepare-dblock))
  9391. (name (plist-get params :name))
  9392. (indent (plist-get params :indentation-column))
  9393. (cmd (intern (concat "org-dblock-write:" name))))
  9394. (message "Updating dynamic block `%s' at line %d..." name line)
  9395. (funcall cmd params)
  9396. (message "Updating dynamic block `%s' at line %d...done" name line)
  9397. (goto-char pos)
  9398. (when (and indent (> indent 0))
  9399. (setq indent (make-string indent ?\ ))
  9400. (save-excursion
  9401. (org-beginning-of-dblock)
  9402. (forward-line 1)
  9403. (while (not (looking-at org-dblock-end-re))
  9404. (insert indent)
  9405. (beginning-of-line 2))
  9406. (when (looking-at org-dblock-end-re)
  9407. (and (looking-at "[ \t]+")
  9408. (replace-match ""))
  9409. (insert indent)))))))
  9410. (defun org-beginning-of-dblock ()
  9411. "Find the beginning of the dynamic block at point.
  9412. Error if there is no such block at point."
  9413. (let ((pos (point))
  9414. beg)
  9415. (end-of-line 1)
  9416. (if (and (re-search-backward org-dblock-start-re nil t)
  9417. (setq beg (match-beginning 0))
  9418. (re-search-forward org-dblock-end-re nil t)
  9419. (> (match-end 0) pos))
  9420. (goto-char beg)
  9421. (goto-char pos)
  9422. (error "Not in a dynamic block"))))
  9423. (defun org-update-all-dblocks ()
  9424. "Update all dynamic blocks in the buffer.
  9425. This function can be used in a hook."
  9426. (when (org-mode-p)
  9427. (org-map-dblocks 'org-update-dblock)))
  9428. ;;;; Completion
  9429. (defconst org-additional-option-like-keywords
  9430. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9431. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9432. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9433. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9434. "BEGIN:" "END:"
  9435. "ORGTBL" "TBLFM:" "TBLNAME:"
  9436. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9437. "BEGIN_QUOTE" "END_QUOTE"
  9438. "BEGIN_VERSE" "END_VERSE"
  9439. "BEGIN_CENTER" "END_CENTER"
  9440. "BEGIN_SRC" "END_SRC"
  9441. "CATEGORY" "COLUMNS"
  9442. "CAPTION" "LABEL"
  9443. "SETUPFILE"
  9444. "BIND"
  9445. "MACRO"))
  9446. (defcustom org-structure-template-alist
  9447. '(
  9448. ("s" "#+begin_src ?\n\n#+end_src"
  9449. "<src lang=\"?\">\n\n</src>")
  9450. ("e" "#+begin_example\n?\n#+end_example"
  9451. "<example>\n?\n</example>")
  9452. ("q" "#+begin_quote\n?\n#+end_quote"
  9453. "<quote>\n?\n</quote>")
  9454. ("v" "#+begin_verse\n?\n#+end_verse"
  9455. "<verse>\n?\n/verse>")
  9456. ("c" "#+begin_center\n?\n#+end_center"
  9457. "<center>\n?\n/center>")
  9458. ("l" "#+begin_latex\n?\n#+end_latex"
  9459. "<literal style=\"latex\">\n?\n</literal>")
  9460. ("L" "#+latex: "
  9461. "<literal style=\"latex\">?</literal>")
  9462. ("h" "#+begin_html\n?\n#+end_html"
  9463. "<literal style=\"html\">\n?\n</literal>")
  9464. ("H" "#+html: "
  9465. "<literal style=\"html\">?</literal>")
  9466. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9467. ("A" "#+ascii: ")
  9468. ("i" "#+include %file ?"
  9469. "<include file=%file markup=\"?\">")
  9470. )
  9471. "Structure completion elements.
  9472. This is a list of abbreviation keys and values. The value gets inserted
  9473. if you type `<' followed by the key and then press the completion key,
  9474. usually `M-TAB'. %file will be replaced by a file name after prompting
  9475. for the file using completion.
  9476. There are two templates for each key, the first uses the original Org syntax,
  9477. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9478. the default when the /org-mtags.el/ module has been loaded. See also the
  9479. variable `org-mtags-prefer-muse-templates'.
  9480. This is an experimental feature, it is undecided if it is going to stay in."
  9481. :group 'org-completion
  9482. :type '(repeat
  9483. (string :tag "Key")
  9484. (string :tag "Template")
  9485. (string :tag "Muse Template")))
  9486. (defun org-try-structure-completion ()
  9487. "Try to complete a structure template before point.
  9488. This looks for strings like \"<e\" on an otherwise empty line and
  9489. expands them."
  9490. (let ((l (buffer-substring (point-at-bol) (point)))
  9491. a)
  9492. (when (and (looking-at "[ \t]*$")
  9493. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9494. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9495. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9496. (match-beginning 1)) a)
  9497. t)))
  9498. (defun org-complete-expand-structure-template (start cell)
  9499. "Expand a structure template."
  9500. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9501. (rpl (nth (if musep 2 1) cell))
  9502. (ind ""))
  9503. (delete-region start (point))
  9504. (when (string-match "\\`#\\+" rpl)
  9505. (cond
  9506. ((bolp))
  9507. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9508. (setq ind (buffer-substring (point-at-bol) (point))))
  9509. (t (newline))))
  9510. (setq start (point))
  9511. (if (string-match "%file" rpl)
  9512. (setq rpl (replace-match
  9513. (concat
  9514. "\""
  9515. (save-match-data
  9516. (abbreviate-file-name (read-file-name "Include file: ")))
  9517. "\"")
  9518. t t rpl)))
  9519. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9520. (concat "\n" ind)))
  9521. (insert rpl)
  9522. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9523. (defun org-complete (&optional arg)
  9524. "Perform completion on word at point.
  9525. At the beginning of a headline, this completes TODO keywords as given in
  9526. `org-todo-keywords'.
  9527. If the current word is preceded by a backslash, completes the TeX symbols
  9528. that are supported for HTML support.
  9529. If the current word is preceded by \"#+\", completes special words for
  9530. setting file options.
  9531. In the line after \"#+STARTUP:, complete valid keywords.\"
  9532. At all other locations, this simply calls the value of
  9533. `org-completion-fallback-command'."
  9534. (interactive "P")
  9535. (org-without-partial-completion
  9536. (catch 'exit
  9537. (let* ((a nil)
  9538. (end (point))
  9539. (beg1 (save-excursion
  9540. (skip-chars-backward (org-re "[:alnum:]_@#%"))
  9541. (point)))
  9542. (beg (save-excursion
  9543. (skip-chars-backward "a-zA-Z0-9_:$")
  9544. (point)))
  9545. (confirm (lambda (x) (stringp (car x))))
  9546. (searchhead (equal (char-before beg) ?*))
  9547. (struct
  9548. (when (and (member (char-before beg1) '(?. ?<))
  9549. (setq a (assoc (buffer-substring beg1 (point))
  9550. org-structure-template-alist)))
  9551. (org-complete-expand-structure-template (1- beg1) a)
  9552. (throw 'exit t)))
  9553. (tag (and (equal (char-before beg1) ?:)
  9554. (equal (char-after (point-at-bol)) ?*)))
  9555. (prop (and (equal (char-before beg1) ?:)
  9556. (not (equal (char-after (point-at-bol)) ?*))))
  9557. (texp (equal (char-before beg) ?\\))
  9558. (link (equal (char-before beg) ?\[))
  9559. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9560. beg)
  9561. "#+"))
  9562. (startup (string-match "^#\\+STARTUP:.*"
  9563. (buffer-substring (point-at-bol) (point))))
  9564. (completion-ignore-case opt)
  9565. (type nil)
  9566. (tbl nil)
  9567. (table (cond
  9568. (opt
  9569. (setq type :opt)
  9570. (require 'org-exp)
  9571. (append
  9572. (delq nil
  9573. (mapcar
  9574. (lambda (x)
  9575. (if (string-match
  9576. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9577. (cons (match-string 2 x)
  9578. (match-string 1 x))))
  9579. (org-split-string (org-get-current-options) "\n")))
  9580. (mapcar 'list org-additional-option-like-keywords)))
  9581. (startup
  9582. (setq type :startup)
  9583. org-startup-options)
  9584. (link (append org-link-abbrev-alist-local
  9585. org-link-abbrev-alist))
  9586. (texp
  9587. (setq type :tex)
  9588. (append org-entities-user org-entities))
  9589. ((string-match "\\`\\*+[ \t]+\\'"
  9590. (buffer-substring (point-at-bol) beg))
  9591. (setq type :todo)
  9592. (mapcar 'list org-todo-keywords-1))
  9593. (searchhead
  9594. (setq type :searchhead)
  9595. (save-excursion
  9596. (goto-char (point-min))
  9597. (while (re-search-forward org-todo-line-regexp nil t)
  9598. (push (list
  9599. (org-make-org-heading-search-string
  9600. (match-string 3) t))
  9601. tbl)))
  9602. tbl)
  9603. (tag (setq type :tag beg beg1)
  9604. (or org-tag-alist (org-get-buffer-tags)))
  9605. (prop (setq type :prop beg beg1)
  9606. (mapcar 'list (org-buffer-property-keys nil t t)))
  9607. (t (progn
  9608. (call-interactively org-completion-fallback-command)
  9609. (throw 'exit nil)))))
  9610. (pattern (buffer-substring-no-properties beg end))
  9611. (completion (try-completion pattern table confirm)))
  9612. (cond ((eq completion t)
  9613. (if (not (assoc (upcase pattern) table))
  9614. (message "Already complete")
  9615. (if (and (equal type :opt)
  9616. (not (member (car (assoc (upcase pattern) table))
  9617. org-additional-option-like-keywords)))
  9618. (insert (substring (cdr (assoc (upcase pattern) table))
  9619. (length pattern)))
  9620. (if (memq type '(:tag :prop)) (insert ":")))))
  9621. ((null completion)
  9622. (message "Can't find completion for \"%s\"" pattern)
  9623. (ding))
  9624. ((not (string= pattern completion))
  9625. (delete-region beg end)
  9626. (if (string-match " +$" completion)
  9627. (setq completion (replace-match "" t t completion)))
  9628. (insert completion)
  9629. (if (get-buffer-window "*Completions*")
  9630. (delete-window (get-buffer-window "*Completions*")))
  9631. (if (assoc completion table)
  9632. (if (eq type :todo) (insert " ")
  9633. (if (memq type '(:tag :prop)) (insert ":"))))
  9634. (if (and (equal type :opt) (assoc completion table))
  9635. (message "%s" (substitute-command-keys
  9636. "Press \\[org-complete] again to insert example settings"))))
  9637. (t
  9638. (message "Making completion list...")
  9639. (let ((list (sort (all-completions pattern table confirm)
  9640. 'string<)))
  9641. (with-output-to-temp-buffer "*Completions*"
  9642. (condition-case nil
  9643. ;; Protection needed for XEmacs and emacs 21
  9644. (display-completion-list list pattern)
  9645. (error (display-completion-list list)))))
  9646. (message "Making completion list...%s" "done")))))))
  9647. ;;;; TODO, DEADLINE, Comments
  9648. (defun org-toggle-comment ()
  9649. "Change the COMMENT state of an entry."
  9650. (interactive)
  9651. (save-excursion
  9652. (org-back-to-heading)
  9653. (let (case-fold-search)
  9654. (if (looking-at (concat outline-regexp
  9655. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9656. (replace-match "" t t nil 1)
  9657. (if (looking-at outline-regexp)
  9658. (progn
  9659. (goto-char (match-end 0))
  9660. (insert org-comment-string " ")))))))
  9661. (defvar org-last-todo-state-is-todo nil
  9662. "This is non-nil when the last TODO state change led to a TODO state.
  9663. If the last change removed the TODO tag or switched to DONE, then
  9664. this is nil.")
  9665. (defvar org-setting-tags nil) ; dynamically skipped
  9666. (defun org-parse-local-options (string var)
  9667. "Parse STRING for startup setting relevant for variable VAR."
  9668. (let ((rtn (symbol-value var))
  9669. e opts)
  9670. (save-match-data
  9671. (if (or (not string) (not (string-match "\\S-" string)))
  9672. rtn
  9673. (setq opts (delq nil (mapcar (lambda (x)
  9674. (setq e (assoc x org-startup-options))
  9675. (if (eq (nth 1 e) var) e nil))
  9676. (org-split-string string "[ \t]+"))))
  9677. (if (not opts)
  9678. rtn
  9679. (setq rtn nil)
  9680. (while (setq e (pop opts))
  9681. (if (not (nth 3 e))
  9682. (setq rtn (nth 2 e))
  9683. (if (not (listp rtn)) (setq rtn nil))
  9684. (push (nth 2 e) rtn)))
  9685. rtn)))))
  9686. (defvar org-todo-setup-filter-hook nil
  9687. "Hook for functions that pre-filter todo specs.
  9688. Each function takes a todo spec and returns either nil or the spec
  9689. transformed into canonical form." )
  9690. (defvar org-todo-get-default-hook nil
  9691. "Hook for functions that get a default item for todo.
  9692. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9693. nil or a string to be used for the todo mark." )
  9694. (defvar org-agenda-headline-snapshot-before-repeat)
  9695. (defun org-todo (&optional arg)
  9696. "Change the TODO state of an item.
  9697. The state of an item is given by a keyword at the start of the heading,
  9698. like
  9699. *** TODO Write paper
  9700. *** DONE Call mom
  9701. The different keywords are specified in the variable `org-todo-keywords'.
  9702. By default the available states are \"TODO\" and \"DONE\".
  9703. So for this example: when the item starts with TODO, it is changed to DONE.
  9704. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9705. DONE are present, add TODO at the beginning of the heading.
  9706. With \\[universal-argument] prefix arg, use completion to determine the new \
  9707. state.
  9708. With numeric prefix arg, switch to that state.
  9709. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9710. keywords (nextset).
  9711. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9712. For calling through lisp, arg is also interpreted in the following way:
  9713. 'none -> empty state
  9714. \"\"(empty string) -> switch to empty state
  9715. 'done -> switch to DONE
  9716. 'nextset -> switch to the next set of keywords
  9717. 'previousset -> switch to the previous set of keywords
  9718. \"WAITING\" -> switch to the specified keyword, but only if it
  9719. really is a member of `org-todo-keywords'."
  9720. (interactive "P")
  9721. (if (equal arg '(16)) (setq arg 'nextset))
  9722. (let ((org-blocker-hook org-blocker-hook)
  9723. (case-fold-search nil))
  9724. (when (equal arg '(64))
  9725. (setq arg nil org-blocker-hook nil))
  9726. (when (and org-blocker-hook
  9727. (or org-inhibit-blocking
  9728. (org-entry-get nil "NOBLOCKING")))
  9729. (setq org-blocker-hook nil))
  9730. (save-excursion
  9731. (catch 'exit
  9732. (org-back-to-heading t)
  9733. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9734. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9735. (looking-at " *"))
  9736. (let* ((match-data (match-data))
  9737. (startpos (point-at-bol))
  9738. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9739. (org-log-done org-log-done)
  9740. (org-log-repeat org-log-repeat)
  9741. (org-todo-log-states org-todo-log-states)
  9742. (this (match-string 1))
  9743. (hl-pos (match-beginning 0))
  9744. (head (org-get-todo-sequence-head this))
  9745. (ass (assoc head org-todo-kwd-alist))
  9746. (interpret (nth 1 ass))
  9747. (done-word (nth 3 ass))
  9748. (final-done-word (nth 4 ass))
  9749. (last-state (or this ""))
  9750. (completion-ignore-case t)
  9751. (member (member this org-todo-keywords-1))
  9752. (tail (cdr member))
  9753. (state (cond
  9754. ((and org-todo-key-trigger
  9755. (or (and (equal arg '(4))
  9756. (eq org-use-fast-todo-selection 'prefix))
  9757. (and (not arg) org-use-fast-todo-selection
  9758. (not (eq org-use-fast-todo-selection
  9759. 'prefix)))))
  9760. ;; Use fast selection
  9761. (org-fast-todo-selection))
  9762. ((and (equal arg '(4))
  9763. (or (not org-use-fast-todo-selection)
  9764. (not org-todo-key-trigger)))
  9765. ;; Read a state with completion
  9766. (org-icompleting-read
  9767. "State: " (mapcar (lambda(x) (list x))
  9768. org-todo-keywords-1)
  9769. nil t))
  9770. ((eq arg 'right)
  9771. (if this
  9772. (if tail (car tail) nil)
  9773. (car org-todo-keywords-1)))
  9774. ((eq arg 'left)
  9775. (if (equal member org-todo-keywords-1)
  9776. nil
  9777. (if this
  9778. (nth (- (length org-todo-keywords-1)
  9779. (length tail) 2)
  9780. org-todo-keywords-1)
  9781. (org-last org-todo-keywords-1))))
  9782. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9783. (setq arg nil))) ; hack to fall back to cycling
  9784. (arg
  9785. ;; user or caller requests a specific state
  9786. (cond
  9787. ((equal arg "") nil)
  9788. ((eq arg 'none) nil)
  9789. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9790. ((eq arg 'nextset)
  9791. (or (car (cdr (member head org-todo-heads)))
  9792. (car org-todo-heads)))
  9793. ((eq arg 'previousset)
  9794. (let ((org-todo-heads (reverse org-todo-heads)))
  9795. (or (car (cdr (member head org-todo-heads)))
  9796. (car org-todo-heads))))
  9797. ((car (member arg org-todo-keywords-1)))
  9798. ((stringp arg)
  9799. (error "State `%s' not valid in this file" arg))
  9800. ((nth (1- (prefix-numeric-value arg))
  9801. org-todo-keywords-1))))
  9802. ((null member) (or head (car org-todo-keywords-1)))
  9803. ((equal this final-done-word) nil) ;; -> make empty
  9804. ((null tail) nil) ;; -> first entry
  9805. ((memq interpret '(type priority))
  9806. (if (eq this-command last-command)
  9807. (car tail)
  9808. (if (> (length tail) 0)
  9809. (or done-word (car org-done-keywords))
  9810. nil)))
  9811. (t
  9812. (car tail))))
  9813. (state (or
  9814. (run-hook-with-args-until-success
  9815. 'org-todo-get-default-hook state last-state)
  9816. state))
  9817. (next (if state (concat " " state " ") " "))
  9818. (change-plist (list :type 'todo-state-change :from this :to state
  9819. :position startpos))
  9820. dolog now-done-p)
  9821. (when org-blocker-hook
  9822. (setq org-last-todo-state-is-todo
  9823. (not (member this org-done-keywords)))
  9824. (unless (save-excursion
  9825. (save-match-data
  9826. (run-hook-with-args-until-failure
  9827. 'org-blocker-hook change-plist)))
  9828. (if (interactive-p)
  9829. (error "TODO state change from %s to %s blocked" this state)
  9830. ;; fail silently
  9831. (message "TODO state change from %s to %s blocked" this state)
  9832. (throw 'exit nil))))
  9833. (store-match-data match-data)
  9834. (replace-match next t t)
  9835. (unless (pos-visible-in-window-p hl-pos)
  9836. (message "TODO state changed to %s" (org-trim next)))
  9837. (unless head
  9838. (setq head (org-get-todo-sequence-head state)
  9839. ass (assoc head org-todo-kwd-alist)
  9840. interpret (nth 1 ass)
  9841. done-word (nth 3 ass)
  9842. final-done-word (nth 4 ass)))
  9843. (when (memq arg '(nextset previousset))
  9844. (message "Keyword-Set %d/%d: %s"
  9845. (- (length org-todo-sets) -1
  9846. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9847. (length org-todo-sets)
  9848. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9849. (setq org-last-todo-state-is-todo
  9850. (not (member state org-done-keywords)))
  9851. (setq now-done-p (and (member state org-done-keywords)
  9852. (not (member this org-done-keywords))))
  9853. (and logging (org-local-logging logging))
  9854. (when (and (or org-todo-log-states org-log-done)
  9855. (not (eq org-inhibit-logging t))
  9856. (not (memq arg '(nextset previousset))))
  9857. ;; we need to look at recording a time and note
  9858. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9859. (nth 2 (assoc this org-todo-log-states))))
  9860. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9861. (setq dolog 'time))
  9862. (when (and state
  9863. (member state org-not-done-keywords)
  9864. (not (member this org-not-done-keywords)))
  9865. ;; This is now a todo state and was not one before
  9866. ;; If there was a CLOSED time stamp, get rid of it.
  9867. (org-add-planning-info nil nil 'closed))
  9868. (when (and now-done-p org-log-done)
  9869. ;; It is now done, and it was not done before
  9870. (org-add-planning-info 'closed (org-current-time))
  9871. (if (and (not dolog) (eq 'note org-log-done))
  9872. (org-add-log-setup 'done state this 'findpos 'note)))
  9873. (when (and state dolog)
  9874. ;; This is a non-nil state, and we need to log it
  9875. (org-add-log-setup 'state state this 'findpos dolog)))
  9876. ;; Fixup tag positioning
  9877. (org-todo-trigger-tag-changes state)
  9878. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9879. (when org-provide-todo-statistics
  9880. (org-update-parent-todo-statistics))
  9881. (run-hooks 'org-after-todo-state-change-hook)
  9882. (if (and arg (not (member state org-done-keywords)))
  9883. (setq head (org-get-todo-sequence-head state)))
  9884. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9885. ;; Do we need to trigger a repeat?
  9886. (when now-done-p
  9887. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9888. ;; This is for the agenda, take a snapshot of the headline.
  9889. (save-match-data
  9890. (setq org-agenda-headline-snapshot-before-repeat
  9891. (org-get-heading))))
  9892. (org-auto-repeat-maybe state))
  9893. ;; Fixup cursor location if close to the keyword
  9894. (if (and (outline-on-heading-p)
  9895. (not (bolp))
  9896. (save-excursion (beginning-of-line 1)
  9897. (looking-at org-todo-line-regexp))
  9898. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9899. (progn
  9900. (goto-char (or (match-end 2) (match-end 1)))
  9901. (and (looking-at " ") (just-one-space))))
  9902. (when org-trigger-hook
  9903. (save-excursion
  9904. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9905. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9906. "Block turning an entry into a TODO, using the hierarchy.
  9907. This checks whether the current task should be blocked from state
  9908. changes. Such blocking occurs when:
  9909. 1. The task has children which are not all in a completed state.
  9910. 2. A task has a parent with the property :ORDERED:, and there
  9911. are siblings prior to the current task with incomplete
  9912. status.
  9913. 3. The parent of the task is blocked because it has siblings that should
  9914. be done first, or is child of a block grandparent TODO entry."
  9915. (if (not org-enforce-todo-dependencies)
  9916. t ; if locally turned off don't block
  9917. (catch 'dont-block
  9918. ;; If this is not a todo state change, or if this entry is already DONE,
  9919. ;; do not block
  9920. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9921. (member (plist-get change-plist :from)
  9922. (cons 'done org-done-keywords))
  9923. (member (plist-get change-plist :to)
  9924. (cons 'todo org-not-done-keywords))
  9925. (not (plist-get change-plist :to)))
  9926. (throw 'dont-block t))
  9927. ;; If this task has children, and any are undone, it's blocked
  9928. (save-excursion
  9929. (org-back-to-heading t)
  9930. (let ((this-level (funcall outline-level)))
  9931. (outline-next-heading)
  9932. (let ((child-level (funcall outline-level)))
  9933. (while (and (not (eobp))
  9934. (> child-level this-level))
  9935. ;; this todo has children, check whether they are all
  9936. ;; completed
  9937. (if (and (not (org-entry-is-done-p))
  9938. (org-entry-is-todo-p))
  9939. (throw 'dont-block nil))
  9940. (outline-next-heading)
  9941. (setq child-level (funcall outline-level))))))
  9942. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9943. ;; any previous siblings are undone, it's blocked
  9944. (save-excursion
  9945. (org-back-to-heading t)
  9946. (let* ((pos (point))
  9947. (parent-pos (and (org-up-heading-safe) (point))))
  9948. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9949. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9950. (forward-line 1)
  9951. (re-search-forward org-not-done-heading-regexp pos t))
  9952. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9953. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9954. (while t
  9955. (goto-char parent-pos)
  9956. (if (not (looking-at org-not-done-heading-regexp))
  9957. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9958. (setq pos (point))
  9959. (setq parent-pos (and (org-up-heading-safe) (point)))
  9960. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9961. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9962. (forward-line 1)
  9963. (re-search-forward org-not-done-heading-regexp pos t))
  9964. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9965. (defcustom org-track-ordered-property-with-tag nil
  9966. "Should the ORDERED property also be shown as a tag?
  9967. The ORDERED property decides if an entry should require subtasks to be
  9968. completed in sequence. Since a property is not very visible, setting
  9969. this option means that toggling the ORDERED property with the command
  9970. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9971. not relevant for the behavior, but it makes things more visible.
  9972. Note that toggling the tag with tags commands will not change the property
  9973. and therefore not influence behavior!
  9974. This can be t, meaning the tag ORDERED should be used, It can also be a
  9975. string to select a different tag for this task."
  9976. :group 'org-todo
  9977. :type '(choice
  9978. (const :tag "No tracking" nil)
  9979. (const :tag "Track with ORDERED tag" t)
  9980. (string :tag "Use other tag")))
  9981. (defun org-toggle-ordered-property ()
  9982. "Toggle the ORDERED property of the current entry.
  9983. For better visibility, you can track the value of this property with a tag.
  9984. See variable `org-track-ordered-property-with-tag'."
  9985. (interactive)
  9986. (let* ((t1 org-track-ordered-property-with-tag)
  9987. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9988. (save-excursion
  9989. (org-back-to-heading)
  9990. (if (org-entry-get nil "ORDERED")
  9991. (progn
  9992. (org-delete-property "ORDERED")
  9993. (and tag (org-toggle-tag tag 'off))
  9994. (message "Subtasks can be completed in arbitrary order"))
  9995. (org-entry-put nil "ORDERED" "t")
  9996. (and tag (org-toggle-tag tag 'on))
  9997. (message "Subtasks must be completed in sequence")))))
  9998. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9999. (defun org-block-todo-from-checkboxes (change-plist)
  10000. "Block turning an entry into a TODO, using checkboxes.
  10001. This checks whether the current task should be blocked from state
  10002. changes because there are unchecked boxes in this entry."
  10003. (if (not org-enforce-todo-checkbox-dependencies)
  10004. t ; if locally turned off don't block
  10005. (catch 'dont-block
  10006. ;; If this is not a todo state change, or if this entry is already DONE,
  10007. ;; do not block
  10008. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10009. (member (plist-get change-plist :from)
  10010. (cons 'done org-done-keywords))
  10011. (member (plist-get change-plist :to)
  10012. (cons 'todo org-not-done-keywords))
  10013. (not (plist-get change-plist :to)))
  10014. (throw 'dont-block t))
  10015. ;; If this task has checkboxes that are not checked, it's blocked
  10016. (save-excursion
  10017. (org-back-to-heading t)
  10018. (let ((beg (point)) end)
  10019. (outline-next-heading)
  10020. (setq end (point))
  10021. (goto-char beg)
  10022. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10023. end t)
  10024. (progn
  10025. (if (boundp 'org-blocked-by-checkboxes)
  10026. (setq org-blocked-by-checkboxes t))
  10027. (throw 'dont-block nil)))))
  10028. t))) ; do not block
  10029. (defun org-entry-blocked-p ()
  10030. "Is the current entry blocked?"
  10031. (if (org-entry-get nil "NOBLOCKING")
  10032. nil ;; Never block this entry
  10033. (not
  10034. (run-hook-with-args-until-failure
  10035. 'org-blocker-hook
  10036. (list :type 'todo-state-change
  10037. :position (point)
  10038. :from 'todo
  10039. :to 'done)))))
  10040. (defun org-update-statistics-cookies (all)
  10041. "Update the statistics cookie, either from TODO or from checkboxes.
  10042. This should be called with the cursor in a line with a statistics cookie."
  10043. (interactive "P")
  10044. (if all
  10045. (progn
  10046. (org-update-checkbox-count 'all)
  10047. (org-map-entries 'org-update-parent-todo-statistics))
  10048. (if (not (org-on-heading-p))
  10049. (org-update-checkbox-count)
  10050. (let ((pos (move-marker (make-marker) (point)))
  10051. end l1 l2)
  10052. (ignore-errors (org-back-to-heading t))
  10053. (if (not (org-on-heading-p))
  10054. (org-update-checkbox-count)
  10055. (setq l1 (org-outline-level))
  10056. (setq end (save-excursion
  10057. (outline-next-heading)
  10058. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10059. (point)))
  10060. (if (and (save-excursion
  10061. (re-search-forward
  10062. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10063. (not (save-excursion (re-search-forward
  10064. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10065. (org-update-checkbox-count)
  10066. (if (and l2 (> l2 l1))
  10067. (progn
  10068. (goto-char end)
  10069. (org-update-parent-todo-statistics))
  10070. (goto-char pos)
  10071. (beginning-of-line 1)
  10072. (while (re-search-forward
  10073. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10074. (point-at-eol) t)
  10075. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10076. (goto-char pos)
  10077. (move-marker pos nil)))))
  10078. (defvar org-entry-property-inherited-from) ;; defined below
  10079. (defun org-update-parent-todo-statistics ()
  10080. "Update any statistics cookie in the parent of the current headline.
  10081. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10082. the entire subtree and this will travel up the hierarchy and update
  10083. statistics everywhere."
  10084. (interactive)
  10085. (let* ((lim 0) prop
  10086. (recursive (or (not org-hierarchical-todo-statistics)
  10087. (string-match
  10088. "\\<recursive\\>"
  10089. (or (setq prop (org-entry-get
  10090. nil "COOKIE_DATA" 'inherit)) ""))))
  10091. (lim (or (and prop (marker-position
  10092. org-entry-property-inherited-from))
  10093. lim))
  10094. (first t)
  10095. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10096. level ltoggle l1 new ndel
  10097. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10098. (catch 'exit
  10099. (save-excursion
  10100. (beginning-of-line 1)
  10101. (if (org-at-heading-p)
  10102. (setq ltoggle (funcall outline-level))
  10103. (error "This should not happen"))
  10104. (while (and (setq level (org-up-heading-safe))
  10105. (or recursive first)
  10106. (>= (point) lim))
  10107. (setq first nil cookie-present nil)
  10108. (unless (and level
  10109. (not (string-match
  10110. "\\<checkbox\\>"
  10111. (downcase
  10112. (or (org-entry-get
  10113. nil "COOKIE_DATA")
  10114. "")))))
  10115. (throw 'exit nil))
  10116. (while (re-search-forward box-re (point-at-eol) t)
  10117. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10118. (setq is-percent (match-end 2))
  10119. (save-match-data
  10120. (unless (outline-next-heading) (throw 'exit nil))
  10121. (while (and (looking-at org-complex-heading-regexp)
  10122. (> (setq l1 (length (match-string 1))) level))
  10123. (setq kwd (and (or recursive (= l1 ltoggle))
  10124. (match-string 2)))
  10125. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10126. (and (listp org-provide-todo-statistics)
  10127. (or (member kwd org-provide-todo-statistics)
  10128. (member kwd org-done-keywords))))
  10129. (setq cnt-all (1+ cnt-all))
  10130. (if (eq org-provide-todo-statistics t)
  10131. (and kwd (setq cnt-all (1+ cnt-all)))))
  10132. (and (member kwd org-done-keywords)
  10133. (setq cnt-done (1+ cnt-done)))
  10134. (outline-next-heading)))
  10135. (setq new
  10136. (if is-percent
  10137. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10138. (format "[%d/%d]" cnt-done cnt-all))
  10139. ndel (- (match-end 0) (match-beginning 0)))
  10140. (goto-char (match-beginning 0))
  10141. (insert new)
  10142. (delete-region (point) (+ (point) ndel)))
  10143. (when cookie-present
  10144. (run-hook-with-args 'org-after-todo-statistics-hook
  10145. cnt-done (- cnt-all cnt-done))))))
  10146. (run-hooks 'org-todo-statistics-hook)))
  10147. (defvar org-after-todo-statistics-hook nil
  10148. "Hook that is called after a TODO statistics cookie has been updated.
  10149. Each function is called with two arguments: the number of not-done entries
  10150. and the number of done entries.
  10151. For example, the following function, when added to this hook, will switch
  10152. an entry to DONE when all children are done, and back to TODO when new
  10153. entries are set to a TODO status. Note that this hook is only called
  10154. when there is a statistics cookie in the headline!
  10155. (defun org-summary-todo (n-done n-not-done)
  10156. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10157. (let (org-log-done org-log-states) ; turn off logging
  10158. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10159. ")
  10160. (defvar org-todo-statistics-hook nil
  10161. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10162. This hook runs even if there is no statistics cookie present, in which case
  10163. `org-after-todo-statistics-hook' would not run.")
  10164. (defun org-todo-trigger-tag-changes (state)
  10165. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10166. (let ((l org-todo-state-tags-triggers)
  10167. changes)
  10168. (when (or (not state) (equal state ""))
  10169. (setq changes (append changes (cdr (assoc "" l)))))
  10170. (when (and (stringp state) (> (length state) 0))
  10171. (setq changes (append changes (cdr (assoc state l)))))
  10172. (when (member state org-not-done-keywords)
  10173. (setq changes (append changes (cdr (assoc 'todo l)))))
  10174. (when (member state org-done-keywords)
  10175. (setq changes (append changes (cdr (assoc 'done l)))))
  10176. (dolist (c changes)
  10177. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10178. (defun org-local-logging (value)
  10179. "Get logging settings from a property VALUE."
  10180. (let* (words w a)
  10181. ;; directly set the variables, they are already local.
  10182. (setq org-log-done nil
  10183. org-log-repeat nil
  10184. org-todo-log-states nil)
  10185. (setq words (org-split-string value))
  10186. (while (setq w (pop words))
  10187. (cond
  10188. ((setq a (assoc w org-startup-options))
  10189. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10190. (set (nth 1 a) (nth 2 a))))
  10191. ((setq a (org-extract-log-state-settings w))
  10192. (and (member (car a) org-todo-keywords-1)
  10193. (push a org-todo-log-states)))))))
  10194. (defun org-get-todo-sequence-head (kwd)
  10195. "Return the head of the TODO sequence to which KWD belongs.
  10196. If KWD is not set, check if there is a text property remembering the
  10197. right sequence."
  10198. (let (p)
  10199. (cond
  10200. ((not kwd)
  10201. (or (get-text-property (point-at-bol) 'org-todo-head)
  10202. (progn
  10203. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10204. nil (point-at-eol)))
  10205. (get-text-property p 'org-todo-head))))
  10206. ((not (member kwd org-todo-keywords-1))
  10207. (car org-todo-keywords-1))
  10208. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10209. (defun org-fast-todo-selection ()
  10210. "Fast TODO keyword selection with single keys.
  10211. Returns the new TODO keyword, or nil if no state change should occur."
  10212. (let* ((fulltable org-todo-key-alist)
  10213. (done-keywords org-done-keywords) ;; needed for the faces.
  10214. (maxlen (apply 'max (mapcar
  10215. (lambda (x)
  10216. (if (stringp (car x)) (string-width (car x)) 0))
  10217. fulltable)))
  10218. (expert nil)
  10219. (fwidth (+ maxlen 3 1 3))
  10220. (ncol (/ (- (window-width) 4) fwidth))
  10221. tg cnt e c tbl
  10222. groups ingroup)
  10223. (save-excursion
  10224. (save-window-excursion
  10225. (if expert
  10226. (set-buffer (get-buffer-create " *Org todo*"))
  10227. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10228. (erase-buffer)
  10229. (org-set-local 'org-done-keywords done-keywords)
  10230. (setq tbl fulltable cnt 0)
  10231. (while (setq e (pop tbl))
  10232. (cond
  10233. ((equal e '(:startgroup))
  10234. (push '() groups) (setq ingroup t)
  10235. (when (not (= cnt 0))
  10236. (setq cnt 0)
  10237. (insert "\n"))
  10238. (insert "{ "))
  10239. ((equal e '(:endgroup))
  10240. (setq ingroup nil cnt 0)
  10241. (insert "}\n"))
  10242. ((equal e '(:newline))
  10243. (when (not (= cnt 0))
  10244. (setq cnt 0)
  10245. (insert "\n")
  10246. (setq e (car tbl))
  10247. (while (equal (car tbl) '(:newline))
  10248. (insert "\n")
  10249. (setq tbl (cdr tbl)))))
  10250. (t
  10251. (setq tg (car e) c (cdr e))
  10252. (if ingroup (push tg (car groups)))
  10253. (setq tg (org-add-props tg nil 'face
  10254. (org-get-todo-face tg)))
  10255. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10256. (insert "[" c "] " tg (make-string
  10257. (- fwidth 4 (length tg)) ?\ ))
  10258. (when (= (setq cnt (1+ cnt)) ncol)
  10259. (insert "\n")
  10260. (if ingroup (insert " "))
  10261. (setq cnt 0)))))
  10262. (insert "\n")
  10263. (goto-char (point-min))
  10264. (if (not expert) (org-fit-window-to-buffer))
  10265. (message "[a-z..]:Set [SPC]:clear")
  10266. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10267. (cond
  10268. ((or (= c ?\C-g)
  10269. (and (= c ?q) (not (rassoc c fulltable))))
  10270. (setq quit-flag t))
  10271. ((= c ?\ ) nil)
  10272. ((setq e (rassoc c fulltable) tg (car e))
  10273. tg)
  10274. (t (setq quit-flag t)))))))
  10275. (defun org-entry-is-todo-p ()
  10276. (member (org-get-todo-state) org-not-done-keywords))
  10277. (defun org-entry-is-done-p ()
  10278. (member (org-get-todo-state) org-done-keywords))
  10279. (defun org-get-todo-state ()
  10280. (save-excursion
  10281. (org-back-to-heading t)
  10282. (and (looking-at org-todo-line-regexp)
  10283. (match-end 2)
  10284. (match-string 2))))
  10285. (defun org-at-date-range-p (&optional inactive-ok)
  10286. "Is the cursor inside a date range?"
  10287. (interactive)
  10288. (save-excursion
  10289. (catch 'exit
  10290. (let ((pos (point)))
  10291. (skip-chars-backward "^[<\r\n")
  10292. (skip-chars-backward "<[")
  10293. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10294. (>= (match-end 0) pos)
  10295. (throw 'exit t))
  10296. (skip-chars-backward "^<[\r\n")
  10297. (skip-chars-backward "<[")
  10298. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10299. (>= (match-end 0) pos)
  10300. (throw 'exit t)))
  10301. nil)))
  10302. (defun org-get-repeat (&optional tagline)
  10303. "Check if there is a deadline/schedule with repeater in this entry."
  10304. (save-match-data
  10305. (save-excursion
  10306. (org-back-to-heading t)
  10307. (and (re-search-forward (if tagline
  10308. (concat tagline "\\s-*" org-repeat-re)
  10309. org-repeat-re)
  10310. (org-entry-end-position) t)
  10311. (match-string-no-properties 1)))))
  10312. (defvar org-last-changed-timestamp)
  10313. (defvar org-last-inserted-timestamp)
  10314. (defvar org-log-post-message)
  10315. (defvar org-log-note-purpose)
  10316. (defvar org-log-note-how)
  10317. (defvar org-log-note-extra)
  10318. (defun org-auto-repeat-maybe (done-word)
  10319. "Check if the current headline contains a repeated deadline/schedule.
  10320. If yes, set TODO state back to what it was and change the base date
  10321. of repeating deadline/scheduled time stamps to new date.
  10322. This function is run automatically after each state change to a DONE state."
  10323. ;; last-state is dynamically scoped into this function
  10324. (let* ((repeat (org-get-repeat))
  10325. (aa (assoc last-state org-todo-kwd-alist))
  10326. (interpret (nth 1 aa))
  10327. (head (nth 2 aa))
  10328. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10329. (msg "Entry repeats: ")
  10330. (org-log-done nil)
  10331. (org-todo-log-states nil)
  10332. (nshiftmax 10) (nshift 0)
  10333. re type n what ts time to-state)
  10334. (when repeat
  10335. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10336. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10337. org-todo-repeat-to-state))
  10338. (unless (and to-state (member to-state org-todo-keywords-1))
  10339. (setq to-state (if (eq interpret 'type) last-state head)))
  10340. (org-todo to-state)
  10341. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10342. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10343. (org-time-stamp-format t t))))
  10344. (when org-log-repeat
  10345. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10346. (memq 'org-add-log-note post-command-hook))
  10347. ;; OK, we are already setup for some record
  10348. (if (eq org-log-repeat 'note)
  10349. ;; make sure we take a note, not only a time stamp
  10350. (setq org-log-note-how 'note))
  10351. ;; Set up for taking a record
  10352. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10353. last-state
  10354. 'findpos org-log-repeat)))
  10355. (org-back-to-heading t)
  10356. (org-add-planning-info nil nil 'closed)
  10357. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10358. org-deadline-time-regexp "\\)\\|\\("
  10359. org-ts-regexp "\\)"))
  10360. (while (re-search-forward
  10361. re (save-excursion (outline-next-heading) (point)) t)
  10362. (setq type (if (match-end 1) org-scheduled-string
  10363. (if (match-end 3) org-deadline-string "Plain:"))
  10364. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10365. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10366. (setq n (string-to-number (match-string 2 ts))
  10367. what (match-string 3 ts))
  10368. (if (equal what "w") (setq n (* n 7) what "d"))
  10369. ;; Preparation, see if we need to modify the start date for the change
  10370. (when (match-end 1)
  10371. (setq time (save-match-data (org-time-string-to-time ts)))
  10372. (cond
  10373. ((equal (match-string 1 ts) ".")
  10374. ;; Shift starting date to today
  10375. (org-timestamp-change
  10376. (- (time-to-days (current-time)) (time-to-days time))
  10377. 'day))
  10378. ((equal (match-string 1 ts) "+")
  10379. (while (or (= nshift 0)
  10380. (<= (time-to-days time) (time-to-days (current-time))))
  10381. (when (= (incf nshift) nshiftmax)
  10382. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10383. (error "Abort")))
  10384. (org-timestamp-change n (cdr (assoc what whata)))
  10385. (org-at-timestamp-p t)
  10386. (setq ts (match-string 1))
  10387. (setq time (save-match-data (org-time-string-to-time ts))))
  10388. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10389. ;; rematch, so that we have everything in place for the real shift
  10390. (org-at-timestamp-p t)
  10391. (setq ts (match-string 1))
  10392. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10393. (org-timestamp-change n (cdr (assoc what whata)))
  10394. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10395. (setq org-log-post-message msg)
  10396. (message "%s" msg))))
  10397. (defun org-show-todo-tree (arg)
  10398. "Make a compact tree which shows all headlines marked with TODO.
  10399. The tree will show the lines where the regexp matches, and all higher
  10400. headlines above the match.
  10401. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10402. With a numeric prefix N, construct a sparse tree for the Nth element
  10403. of `org-todo-keywords-1'."
  10404. (interactive "P")
  10405. (let ((case-fold-search nil)
  10406. (kwd-re
  10407. (cond ((null arg) org-not-done-regexp)
  10408. ((equal arg '(4))
  10409. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10410. (mapcar 'list org-todo-keywords-1))))
  10411. (concat "\\("
  10412. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10413. "\\)\\>")))
  10414. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10415. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10416. org-todo-keywords-1)))
  10417. (t (error "Invalid prefix argument: %s" arg)))))
  10418. (message "%d TODO entries found"
  10419. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10420. (defun org-deadline (&optional remove time)
  10421. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10422. With argument REMOVE, remove any deadline from the item.
  10423. When TIME is set, it should be an internal time specification, and the
  10424. scheduling will use the corresponding date."
  10425. (interactive "P")
  10426. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10427. (repeater (and old-date
  10428. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10429. (match-string 1 old-date))))
  10430. (if remove
  10431. (progn
  10432. (when (and old-date org-log-redeadline)
  10433. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10434. org-log-redeadline))
  10435. (org-remove-timestamp-with-keyword org-deadline-string)
  10436. (message "Item no longer has a deadline."))
  10437. (org-add-planning-info 'deadline time 'closed)
  10438. (when (and old-date org-log-redeadline
  10439. (not (equal old-date
  10440. (substring org-last-inserted-timestamp 1 -1))))
  10441. (org-add-log-setup 'redeadline nil old-date 'findpos
  10442. org-log-redeadline))
  10443. (when repeater
  10444. (save-excursion
  10445. (org-back-to-heading t)
  10446. (when (re-search-forward (concat org-deadline-string " "
  10447. org-last-inserted-timestamp)
  10448. (save-excursion
  10449. (outline-next-heading) (point)) t)
  10450. (goto-char (1- (match-end 0)))
  10451. (insert " " repeater)
  10452. (setq org-last-inserted-timestamp
  10453. (concat (substring org-last-inserted-timestamp 0 -1)
  10454. " " repeater
  10455. (substring org-last-inserted-timestamp -1))))))
  10456. (message "Deadline on %s" org-last-inserted-timestamp))))
  10457. (defun org-schedule (&optional remove time)
  10458. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10459. With argument REMOVE, remove any scheduling date from the item.
  10460. When TIME is set, it should be an internal time specification, and the
  10461. scheduling will use the corresponding date."
  10462. (interactive "P")
  10463. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10464. (repeater (and old-date
  10465. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10466. (match-string 1 old-date))))
  10467. (if remove
  10468. (progn
  10469. (when (and old-date org-log-reschedule)
  10470. (org-add-log-setup 'delschedule nil old-date 'findpos
  10471. org-log-reschedule))
  10472. (org-remove-timestamp-with-keyword org-scheduled-string)
  10473. (message "Item is no longer scheduled."))
  10474. (org-add-planning-info 'scheduled time 'closed)
  10475. (when (and old-date org-log-reschedule
  10476. (not (equal old-date
  10477. (substring org-last-inserted-timestamp 1 -1))))
  10478. (org-add-log-setup 'reschedule nil old-date 'findpos
  10479. org-log-reschedule))
  10480. (when repeater
  10481. (save-excursion
  10482. (org-back-to-heading t)
  10483. (when (re-search-forward (concat org-scheduled-string " "
  10484. org-last-inserted-timestamp)
  10485. (save-excursion
  10486. (outline-next-heading) (point)) t)
  10487. (goto-char (1- (match-end 0)))
  10488. (insert " " repeater)
  10489. (setq org-last-inserted-timestamp
  10490. (concat (substring org-last-inserted-timestamp 0 -1)
  10491. " " repeater
  10492. (substring org-last-inserted-timestamp -1))))))
  10493. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10494. (defun org-get-scheduled-time (pom &optional inherit)
  10495. "Get the scheduled time as a time tuple, of a format suitable
  10496. for calling org-schedule with, or if there is no scheduling,
  10497. returns nil."
  10498. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10499. (when time
  10500. (apply 'encode-time (org-parse-time-string time)))))
  10501. (defun org-get-deadline-time (pom &optional inherit)
  10502. "Get the deadline as a time tuple, of a format suitable for
  10503. calling org-deadline with, or if there is no scheduling, returns
  10504. nil."
  10505. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10506. (when time
  10507. (apply 'encode-time (org-parse-time-string time)))))
  10508. (defun org-remove-timestamp-with-keyword (keyword)
  10509. "Remove all time stamps with KEYWORD in the current entry."
  10510. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10511. beg)
  10512. (save-excursion
  10513. (org-back-to-heading t)
  10514. (setq beg (point))
  10515. (outline-next-heading)
  10516. (while (re-search-backward re beg t)
  10517. (replace-match "")
  10518. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10519. (equal (char-before) ?\ ))
  10520. (backward-delete-char 1)
  10521. (if (string-match "^[ \t]*$" (buffer-substring
  10522. (point-at-bol) (point-at-eol)))
  10523. (delete-region (point-at-bol)
  10524. (min (point-max) (1+ (point-at-eol))))))))))
  10525. (defun org-add-planning-info (what &optional time &rest remove)
  10526. "Insert new timestamp with keyword in the line directly after the headline.
  10527. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10528. If non is given, the user is prompted for a date.
  10529. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10530. be removed."
  10531. (interactive)
  10532. (let (org-time-was-given org-end-time-was-given ts
  10533. end default-time default-input)
  10534. (catch 'exit
  10535. (when (and (not time) (memq what '(scheduled deadline)))
  10536. ;; Try to get a default date/time from existing timestamp
  10537. (save-excursion
  10538. (org-back-to-heading t)
  10539. (setq end (save-excursion (outline-next-heading) (point)))
  10540. (when (re-search-forward (if (eq what 'scheduled)
  10541. org-scheduled-time-regexp
  10542. org-deadline-time-regexp)
  10543. end t)
  10544. (setq ts (match-string 1)
  10545. default-time
  10546. (apply 'encode-time (org-parse-time-string ts))
  10547. default-input (and ts (org-get-compact-tod ts))))))
  10548. (when what
  10549. ;; If necessary, get the time from the user
  10550. (setq time (or time (org-read-date nil 'to-time nil nil
  10551. default-time default-input))))
  10552. (when (and org-insert-labeled-timestamps-at-point
  10553. (member what '(scheduled deadline)))
  10554. (insert
  10555. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10556. (org-insert-time-stamp time org-time-was-given
  10557. nil nil nil (list org-end-time-was-given))
  10558. (setq what nil))
  10559. (save-excursion
  10560. (save-restriction
  10561. (let (col list elt ts buffer-invisibility-spec)
  10562. (org-back-to-heading t)
  10563. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10564. (goto-char (match-end 1))
  10565. (setq col (current-column))
  10566. (goto-char (match-end 0))
  10567. (if (eobp) (insert "\n") (forward-char 1))
  10568. (when (and (not what)
  10569. (not (looking-at
  10570. (concat "[ \t]*"
  10571. org-keyword-time-not-clock-regexp))))
  10572. ;; Nothing to add, nothing to remove...... :-)
  10573. (throw 'exit nil))
  10574. (if (and (not (looking-at outline-regexp))
  10575. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10576. "[^\r\n]*"))
  10577. (not (equal (match-string 1) org-clock-string)))
  10578. (narrow-to-region (match-beginning 0) (match-end 0))
  10579. (insert-before-markers "\n")
  10580. (backward-char 1)
  10581. (narrow-to-region (point) (point))
  10582. (and org-adapt-indentation (org-indent-to-column col)))
  10583. ;; Check if we have to remove something.
  10584. (setq list (cons what remove))
  10585. (while list
  10586. (setq elt (pop list))
  10587. (goto-char (point-min))
  10588. (when (or (and (eq elt 'scheduled)
  10589. (re-search-forward org-scheduled-time-regexp nil t))
  10590. (and (eq elt 'deadline)
  10591. (re-search-forward org-deadline-time-regexp nil t))
  10592. (and (eq elt 'closed)
  10593. (re-search-forward org-closed-time-regexp nil t)))
  10594. (replace-match "")
  10595. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10596. (skip-chars-backward " ")
  10597. (if (looking-at " +") (replace-match ""))))
  10598. (goto-char (point-max))
  10599. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10600. (when what
  10601. (insert
  10602. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10603. (cond ((eq what 'scheduled) org-scheduled-string)
  10604. ((eq what 'deadline) org-deadline-string)
  10605. ((eq what 'closed) org-closed-string))
  10606. " ")
  10607. (setq ts (org-insert-time-stamp
  10608. time
  10609. (or org-time-was-given
  10610. (and (eq what 'closed) org-log-done-with-time))
  10611. (eq what 'closed)
  10612. nil nil (list org-end-time-was-given)))
  10613. (end-of-line 1))
  10614. (goto-char (point-min))
  10615. (widen)
  10616. (if (and (looking-at "[ \t]*\n")
  10617. (equal (char-before) ?\n))
  10618. (delete-region (1- (point)) (point-at-eol)))
  10619. ts))))))
  10620. (defvar org-log-note-marker (make-marker))
  10621. (defvar org-log-note-purpose nil)
  10622. (defvar org-log-note-state nil)
  10623. (defvar org-log-note-previous-state nil)
  10624. (defvar org-log-note-how nil)
  10625. (defvar org-log-note-extra nil)
  10626. (defvar org-log-note-window-configuration nil)
  10627. (defvar org-log-note-return-to (make-marker))
  10628. (defvar org-log-post-message nil
  10629. "Message to be displayed after a log note has been stored.
  10630. The auto-repeater uses this.")
  10631. (defun org-add-note ()
  10632. "Add a note to the current entry.
  10633. This is done in the same way as adding a state change note."
  10634. (interactive)
  10635. (org-add-log-setup 'note nil nil 'findpos nil))
  10636. (defvar org-property-end-re)
  10637. (defun org-add-log-setup (&optional purpose state prev-state
  10638. findpos how extra)
  10639. "Set up the post command hook to take a note.
  10640. If this is about to TODO state change, the new state is expected in STATE.
  10641. When FINDPOS is non-nil, find the correct position for the note in
  10642. the current entry. If not, assume that it can be inserted at point.
  10643. HOW is an indicator what kind of note should be created.
  10644. EXTRA is additional text that will be inserted into the notes buffer."
  10645. (let* ((org-log-into-drawer (org-log-into-drawer))
  10646. (drawer (cond ((stringp org-log-into-drawer)
  10647. org-log-into-drawer)
  10648. (org-log-into-drawer "LOGBOOK")
  10649. (t nil))))
  10650. (save-restriction
  10651. (save-excursion
  10652. (when findpos
  10653. (org-back-to-heading t)
  10654. (narrow-to-region (point) (save-excursion
  10655. (outline-next-heading) (point)))
  10656. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10657. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10658. "[^\r\n]*\\)?"))
  10659. (goto-char (match-end 0))
  10660. (cond
  10661. (drawer
  10662. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10663. nil t)
  10664. (progn
  10665. (goto-char (match-end 0))
  10666. (or org-log-states-order-reversed
  10667. (and (re-search-forward org-property-end-re nil t)
  10668. (goto-char (1- (match-beginning 0))))))
  10669. (insert "\n:" drawer ":\n:END:")
  10670. (beginning-of-line 0)
  10671. (org-indent-line-function)
  10672. (beginning-of-line 2)
  10673. (org-indent-line-function)
  10674. (end-of-line 0)))
  10675. ((and org-log-state-notes-insert-after-drawers
  10676. (save-excursion
  10677. (forward-line) (looking-at org-drawer-regexp)))
  10678. (forward-line)
  10679. (while (looking-at org-drawer-regexp)
  10680. (goto-char (match-end 0))
  10681. (re-search-forward org-property-end-re (point-max) t)
  10682. (forward-line))
  10683. (forward-line -1)))
  10684. (unless org-log-states-order-reversed
  10685. (and (= (char-after) ?\n) (forward-char 1))
  10686. (org-skip-over-state-notes)
  10687. (skip-chars-backward " \t\n\r")))
  10688. (move-marker org-log-note-marker (point))
  10689. (setq org-log-note-purpose purpose
  10690. org-log-note-state state
  10691. org-log-note-previous-state prev-state
  10692. org-log-note-how how
  10693. org-log-note-extra extra)
  10694. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10695. (defun org-skip-over-state-notes ()
  10696. "Skip past the list of State notes in an entry."
  10697. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10698. (while (looking-at "[ \t]*- State")
  10699. (condition-case nil
  10700. (org-next-item)
  10701. (error (org-end-of-item)))))
  10702. (defun org-add-log-note (&optional purpose)
  10703. "Pop up a window for taking a note, and add this note later at point."
  10704. (remove-hook 'post-command-hook 'org-add-log-note)
  10705. (setq org-log-note-window-configuration (current-window-configuration))
  10706. (delete-other-windows)
  10707. (move-marker org-log-note-return-to (point))
  10708. (switch-to-buffer (marker-buffer org-log-note-marker))
  10709. (goto-char org-log-note-marker)
  10710. (org-switch-to-buffer-other-window "*Org Note*")
  10711. (erase-buffer)
  10712. (if (memq org-log-note-how '(time state))
  10713. (let (current-prefix-arg) (org-store-log-note))
  10714. (let ((org-inhibit-startup t)) (org-mode))
  10715. (insert (format "# Insert note for %s.
  10716. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10717. (cond
  10718. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10719. ((eq org-log-note-purpose 'done) "closed todo item")
  10720. ((eq org-log-note-purpose 'state)
  10721. (format "state change from \"%s\" to \"%s\""
  10722. (or org-log-note-previous-state "")
  10723. (or org-log-note-state "")))
  10724. ((eq org-log-note-purpose 'reschedule)
  10725. "rescheduling")
  10726. ((eq org-log-note-purpose 'delschedule)
  10727. "no longer scheduled")
  10728. ((eq org-log-note-purpose 'redeadline)
  10729. "changing deadline")
  10730. ((eq org-log-note-purpose 'deldeadline)
  10731. "removing deadline")
  10732. ((eq org-log-note-purpose 'refile)
  10733. "refiling")
  10734. ((eq org-log-note-purpose 'note)
  10735. "this entry")
  10736. (t (error "This should not happen")))))
  10737. (if org-log-note-extra (insert org-log-note-extra))
  10738. (org-set-local 'org-finish-function 'org-store-log-note)))
  10739. (defvar org-note-abort nil) ; dynamically scoped
  10740. (defun org-store-log-note ()
  10741. "Finish taking a log note, and insert it to where it belongs."
  10742. (let ((txt (buffer-string))
  10743. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10744. lines ind)
  10745. (kill-buffer (current-buffer))
  10746. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10747. (setq txt (replace-match "" t t txt)))
  10748. (if (string-match "\\s-+\\'" txt)
  10749. (setq txt (replace-match "" t t txt)))
  10750. (setq lines (org-split-string txt "\n"))
  10751. (when (and note (string-match "\\S-" note))
  10752. (setq note
  10753. (org-replace-escapes
  10754. note
  10755. (list (cons "%u" (user-login-name))
  10756. (cons "%U" user-full-name)
  10757. (cons "%t" (format-time-string
  10758. (org-time-stamp-format 'long 'inactive)
  10759. (current-time)))
  10760. (cons "%T" (format-time-string
  10761. (org-time-stamp-format 'long nil)
  10762. (current-time)))
  10763. (cons "%s" (if org-log-note-state
  10764. (concat "\"" org-log-note-state "\"")
  10765. ""))
  10766. (cons "%S" (if org-log-note-previous-state
  10767. (concat "\"" org-log-note-previous-state "\"")
  10768. "\"\"")))))
  10769. (if lines (setq note (concat note " \\\\")))
  10770. (push note lines))
  10771. (when (or current-prefix-arg org-note-abort)
  10772. (when org-log-into-drawer
  10773. (org-remove-empty-drawer-at
  10774. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10775. org-log-note-marker))
  10776. (setq lines nil))
  10777. (when lines
  10778. (with-current-buffer (marker-buffer org-log-note-marker)
  10779. (save-excursion
  10780. (goto-char org-log-note-marker)
  10781. (move-marker org-log-note-marker nil)
  10782. (end-of-line 1)
  10783. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10784. (org-indent-line-function)
  10785. (insert "- " (pop lines))
  10786. (beginning-of-line 1)
  10787. (looking-at "[ \t]*")
  10788. (setq ind (concat (match-string 0) " "))
  10789. (end-of-line 1)
  10790. (while lines (insert "\n" ind (pop lines)))
  10791. (message "Note stored")
  10792. (org-back-to-heading t)
  10793. (org-cycle-hide-drawers 'children)))))
  10794. (set-window-configuration org-log-note-window-configuration)
  10795. (with-current-buffer (marker-buffer org-log-note-return-to)
  10796. (goto-char org-log-note-return-to))
  10797. (move-marker org-log-note-return-to nil)
  10798. (and org-log-post-message (message "%s" org-log-post-message)))
  10799. (defun org-remove-empty-drawer-at (drawer pos)
  10800. "Remove an empty drawer DRAWER at position POS.
  10801. POS may also be a marker."
  10802. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10803. (save-excursion
  10804. (save-restriction
  10805. (widen)
  10806. (goto-char pos)
  10807. (if (org-in-regexp
  10808. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10809. (replace-match ""))))))
  10810. (defun org-sparse-tree (&optional arg)
  10811. "Create a sparse tree, prompt for the details.
  10812. This command can create sparse trees. You first need to select the type
  10813. of match used to create the tree:
  10814. t Show all TODO entries.
  10815. T Show entries with a specific TODO keyword.
  10816. m Show entries selected by a tags/property match.
  10817. p Enter a property name and its value (both with completion on existing
  10818. names/values) and show entries with that property.
  10819. / Show entries matching a regular expression (`r' can be used as well)
  10820. d Show deadlines due within `org-deadline-warning-days'.
  10821. b Show deadlines and scheduled items before a date.
  10822. a Show deadlines and scheduled items after a date."
  10823. (interactive "P")
  10824. (let (ans kwd value)
  10825. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10826. (setq ans (read-char-exclusive))
  10827. (cond
  10828. ((equal ans ?d)
  10829. (call-interactively 'org-check-deadlines))
  10830. ((equal ans ?b)
  10831. (call-interactively 'org-check-before-date))
  10832. ((equal ans ?a)
  10833. (call-interactively 'org-check-after-date))
  10834. ((equal ans ?t)
  10835. (org-show-todo-tree nil))
  10836. ((equal ans ?T)
  10837. (org-show-todo-tree '(4)))
  10838. ((member ans '(?T ?m))
  10839. (call-interactively 'org-match-sparse-tree))
  10840. ((member ans '(?p ?P))
  10841. (setq kwd (org-icompleting-read "Property: "
  10842. (mapcar 'list (org-buffer-property-keys))))
  10843. (setq value (org-icompleting-read "Value: "
  10844. (mapcar 'list (org-property-values kwd))))
  10845. (unless (string-match "\\`{.*}\\'" value)
  10846. (setq value (concat "\"" value "\"")))
  10847. (org-match-sparse-tree arg (concat kwd "=" value)))
  10848. ((member ans '(?r ?R ?/))
  10849. (call-interactively 'org-occur))
  10850. (t (error "No such sparse tree command \"%c\"" ans)))))
  10851. (defvar org-occur-highlights nil
  10852. "List of overlays used for occur matches.")
  10853. (make-variable-buffer-local 'org-occur-highlights)
  10854. (defvar org-occur-parameters nil
  10855. "Parameters of the active org-occur calls.
  10856. This is a list, each call to org-occur pushes as cons cell,
  10857. containing the regular expression and the callback, onto the list.
  10858. The list can contain several entries if `org-occur' has been called
  10859. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10860. will only contain one set of parameters. When the highlights are
  10861. removed (for example with `C-c C-c', or with the next edit (depending
  10862. on `org-remove-highlights-with-change'), this variable is emptied
  10863. as well.")
  10864. (make-variable-buffer-local 'org-occur-parameters)
  10865. (defun org-occur (regexp &optional keep-previous callback)
  10866. "Make a compact tree which shows all matches of REGEXP.
  10867. The tree will show the lines where the regexp matches, and all higher
  10868. headlines above the match. It will also show the heading after the match,
  10869. to make sure editing the matching entry is easy.
  10870. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10871. call to `org-occur' will be kept, to allow stacking of calls to this
  10872. command.
  10873. If CALLBACK is non-nil, it is a function which is called to confirm
  10874. that the match should indeed be shown."
  10875. (interactive "sRegexp: \nP")
  10876. (when (equal regexp "")
  10877. (error "Regexp cannot be empty"))
  10878. (unless keep-previous
  10879. (org-remove-occur-highlights nil nil t))
  10880. (push (cons regexp callback) org-occur-parameters)
  10881. (let ((cnt 0))
  10882. (save-excursion
  10883. (goto-char (point-min))
  10884. (if (or (not keep-previous) ; do not want to keep
  10885. (not org-occur-highlights)) ; no previous matches
  10886. ;; hide everything
  10887. (org-overview))
  10888. (while (re-search-forward regexp nil t)
  10889. (when (or (not callback)
  10890. (save-match-data (funcall callback)))
  10891. (setq cnt (1+ cnt))
  10892. (when org-highlight-sparse-tree-matches
  10893. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10894. (org-show-context 'occur-tree))))
  10895. (when org-remove-highlights-with-change
  10896. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10897. nil 'local))
  10898. (unless org-sparse-tree-open-archived-trees
  10899. (org-hide-archived-subtrees (point-min) (point-max)))
  10900. (run-hooks 'org-occur-hook)
  10901. (if (interactive-p)
  10902. (message "%d match(es) for regexp %s" cnt regexp))
  10903. cnt))
  10904. (defun org-show-context (&optional key)
  10905. "Make sure point and context are visible.
  10906. How much context is shown depends upon the variables
  10907. `org-show-hierarchy-above', `org-show-following-heading'. and
  10908. `org-show-siblings'."
  10909. (let ((heading-p (org-on-heading-p t))
  10910. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10911. (following-p (org-get-alist-option org-show-following-heading key))
  10912. (entry-p (org-get-alist-option org-show-entry-below key))
  10913. (siblings-p (org-get-alist-option org-show-siblings key)))
  10914. (catch 'exit
  10915. ;; Show heading or entry text
  10916. (if (and heading-p (not entry-p))
  10917. (org-flag-heading nil) ; only show the heading
  10918. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10919. (org-show-hidden-entry))) ; show entire entry
  10920. (when following-p
  10921. ;; Show next sibling, or heading below text
  10922. (save-excursion
  10923. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10924. (org-flag-heading nil))))
  10925. (when siblings-p (org-show-siblings))
  10926. (when hierarchy-p
  10927. ;; show all higher headings, possibly with siblings
  10928. (save-excursion
  10929. (while (and (condition-case nil
  10930. (progn (org-up-heading-all 1) t)
  10931. (error nil))
  10932. (not (bobp)))
  10933. (org-flag-heading nil)
  10934. (when siblings-p (org-show-siblings))))))))
  10935. (defvar org-reveal-start-hook nil
  10936. "Hook run before revealing a location.")
  10937. (defun org-reveal (&optional siblings)
  10938. "Show current entry, hierarchy above it, and the following headline.
  10939. This can be used to show a consistent set of context around locations
  10940. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10941. not t for the search context.
  10942. With optional argument SIBLINGS, on each level of the hierarchy all
  10943. siblings are shown. This repairs the tree structure to what it would
  10944. look like when opened with hierarchical calls to `org-cycle'.
  10945. With double optional argument \\[universal-argument] \\[universal-argument], \
  10946. go to the parent and show the
  10947. entire tree."
  10948. (interactive "P")
  10949. (run-hooks 'org-reveal-start-hook)
  10950. (let ((org-show-hierarchy-above t)
  10951. (org-show-following-heading t)
  10952. (org-show-siblings (if siblings t org-show-siblings)))
  10953. (org-show-context nil))
  10954. (when (equal siblings '(16))
  10955. (save-excursion
  10956. (when (org-up-heading-safe)
  10957. (org-show-subtree)
  10958. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10959. (defun org-highlight-new-match (beg end)
  10960. "Highlight from BEG to END and mark the highlight is an occur headline."
  10961. (let ((ov (make-overlay beg end)))
  10962. (overlay-put ov 'face 'secondary-selection)
  10963. (push ov org-occur-highlights)))
  10964. (defun org-remove-occur-highlights (&optional beg end noremove)
  10965. "Remove the occur highlights from the buffer.
  10966. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10967. from the `before-change-functions' in the current buffer."
  10968. (interactive)
  10969. (unless org-inhibit-highlight-removal
  10970. (mapc 'delete-overlay org-occur-highlights)
  10971. (setq org-occur-highlights nil)
  10972. (setq org-occur-parameters nil)
  10973. (unless noremove
  10974. (remove-hook 'before-change-functions
  10975. 'org-remove-occur-highlights 'local))))
  10976. ;;;; Priorities
  10977. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10978. "Regular expression matching the priority indicator.")
  10979. (defvar org-remove-priority-next-time nil)
  10980. (defun org-priority-up ()
  10981. "Increase the priority of the current item."
  10982. (interactive)
  10983. (org-priority 'up))
  10984. (defun org-priority-down ()
  10985. "Decrease the priority of the current item."
  10986. (interactive)
  10987. (org-priority 'down))
  10988. (defun org-priority (&optional action)
  10989. "Change the priority of an item by ARG.
  10990. ACTION can be `set', `up', `down', or a character."
  10991. (interactive)
  10992. (unless org-enable-priority-commands
  10993. (error "Priority commands are disabled"))
  10994. (setq action (or action 'set))
  10995. (let (current new news have remove)
  10996. (save-excursion
  10997. (org-back-to-heading t)
  10998. (if (looking-at org-priority-regexp)
  10999. (setq current (string-to-char (match-string 2))
  11000. have t)
  11001. (setq current org-default-priority))
  11002. (cond
  11003. ((eq action 'remove)
  11004. (setq remove t new ?\ ))
  11005. ((or (eq action 'set)
  11006. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11007. (if (not (eq action 'set))
  11008. (setq new action)
  11009. (message "Priority %c-%c, SPC to remove: "
  11010. org-highest-priority org-lowest-priority)
  11011. (setq new (read-char-exclusive)))
  11012. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11013. (= (upcase org-lowest-priority) org-lowest-priority))
  11014. (setq new (upcase new)))
  11015. (cond ((equal new ?\ ) (setq remove t))
  11016. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11017. (error "Priority must be between `%c' and `%c'"
  11018. org-highest-priority org-lowest-priority))))
  11019. ((eq action 'up)
  11020. (if (and (not have) (eq last-command this-command))
  11021. (setq new org-lowest-priority)
  11022. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11023. org-default-priority (1- current)))))
  11024. ((eq action 'down)
  11025. (if (and (not have) (eq last-command this-command))
  11026. (setq new org-highest-priority)
  11027. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11028. org-default-priority (1+ current)))))
  11029. (t (error "Invalid action")))
  11030. (if (or (< (upcase new) org-highest-priority)
  11031. (> (upcase new) org-lowest-priority))
  11032. (setq remove t))
  11033. (setq news (format "%c" new))
  11034. (if have
  11035. (if remove
  11036. (replace-match "" t t nil 1)
  11037. (replace-match news t t nil 2))
  11038. (if remove
  11039. (error "No priority cookie found in line")
  11040. (let ((case-fold-search nil))
  11041. (looking-at org-todo-line-regexp))
  11042. (if (match-end 2)
  11043. (progn
  11044. (goto-char (match-end 2))
  11045. (insert " [#" news "]"))
  11046. (goto-char (match-beginning 3))
  11047. (insert "[#" news "] "))))
  11048. (org-preserve-lc (org-set-tags nil 'align)))
  11049. (if remove
  11050. (message "Priority removed")
  11051. (message "Priority of current item set to %s" news))))
  11052. (defun org-get-priority (s)
  11053. "Find priority cookie and return priority."
  11054. (save-match-data
  11055. (if (not (string-match org-priority-regexp s))
  11056. (* 1000 (- org-lowest-priority org-default-priority))
  11057. (* 1000 (- org-lowest-priority
  11058. (string-to-char (match-string 2 s)))))))
  11059. ;;;; Tags
  11060. (defvar org-agenda-archives-mode)
  11061. (defvar org-map-continue-from nil
  11062. "Position from where mapping should continue.
  11063. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11064. (defvar org-scanner-tags nil
  11065. "The current tag list while the tags scanner is running.")
  11066. (defvar org-trust-scanner-tags nil
  11067. "Should `org-get-tags-at' use the tags fro the scanner.
  11068. This is for internal dynamical scoping only.
  11069. When this is non-nil, the function `org-get-tags-at' will return the value
  11070. of `org-scanner-tags' instead of building the list by itself. This
  11071. can lead to large speed-ups when the tags scanner is used in a file with
  11072. many entries, and when the list of tags is retrieved, for example to
  11073. obtain a list of properties. Building the tags list for each entry in such
  11074. a file becomes an N^2 operation - but with this variable set, it scales
  11075. as N.")
  11076. (defun org-scan-tags (action matcher &optional todo-only)
  11077. "Scan headline tags with inheritance and produce output ACTION.
  11078. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11079. or `agenda' to produce an entry list for an agenda view. It can also be
  11080. a Lisp form or a function that should be called at each matched headline, in
  11081. this case the return value is a list of all return values from these calls.
  11082. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11083. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11084. only lines with a TODO keyword are included in the output."
  11085. (require 'org-agenda)
  11086. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11087. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11088. (org-re
  11089. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11090. (props (list 'face 'default
  11091. 'done-face 'org-agenda-done
  11092. 'undone-face 'default
  11093. 'mouse-face 'highlight
  11094. 'org-not-done-regexp org-not-done-regexp
  11095. 'org-todo-regexp org-todo-regexp
  11096. 'help-echo
  11097. (format "mouse-2 or RET jump to org file %s"
  11098. (abbreviate-file-name
  11099. (or (buffer-file-name (buffer-base-buffer))
  11100. (buffer-name (buffer-base-buffer)))))))
  11101. (case-fold-search nil)
  11102. (org-map-continue-from nil)
  11103. lspos tags tags-list
  11104. (tags-alist (list (cons 0 org-file-tags)))
  11105. (llast 0) rtn rtn1 level category i txt
  11106. todo marker entry priority)
  11107. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11108. (setq action (list 'lambda nil action)))
  11109. (save-excursion
  11110. (goto-char (point-min))
  11111. (when (eq action 'sparse-tree)
  11112. (org-overview)
  11113. (org-remove-occur-highlights))
  11114. (while (re-search-forward re nil t)
  11115. (catch :skip
  11116. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11117. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11118. (goto-char (setq lspos (match-beginning 0)))
  11119. (setq level (org-reduced-level (funcall outline-level))
  11120. category (org-get-category))
  11121. (setq i llast llast level)
  11122. ;; remove tag lists from same and sublevels
  11123. (while (>= i level)
  11124. (when (setq entry (assoc i tags-alist))
  11125. (setq tags-alist (delete entry tags-alist)))
  11126. (setq i (1- i)))
  11127. ;; add the next tags
  11128. (when tags
  11129. (setq tags (org-split-string tags ":")
  11130. tags-alist
  11131. (cons (cons level tags) tags-alist)))
  11132. ;; compile tags for current headline
  11133. (setq tags-list
  11134. (if org-use-tag-inheritance
  11135. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11136. tags)
  11137. org-scanner-tags tags-list)
  11138. (when org-use-tag-inheritance
  11139. (setcdr (car tags-alist)
  11140. (mapcar (lambda (x)
  11141. (setq x (copy-sequence x))
  11142. (org-add-prop-inherited x))
  11143. (cdar tags-alist))))
  11144. (when (and tags org-use-tag-inheritance
  11145. (or (not (eq t org-use-tag-inheritance))
  11146. org-tags-exclude-from-inheritance))
  11147. ;; selective inheritance, remove uninherited ones
  11148. (setcdr (car tags-alist)
  11149. (org-remove-uniherited-tags (cdar tags-alist))))
  11150. (when (and (or (not todo-only)
  11151. (and (member todo org-not-done-keywords)
  11152. (or (not org-agenda-tags-todo-honor-ignore-options)
  11153. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11154. (let ((case-fold-search t)) (eval matcher))
  11155. (or
  11156. (not (member org-archive-tag tags-list))
  11157. ;; we have an archive tag, should we use this anyway?
  11158. (or (not org-agenda-skip-archived-trees)
  11159. (and (eq action 'agenda) org-agenda-archives-mode))))
  11160. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11161. ;; select this headline
  11162. (cond
  11163. ((eq action 'sparse-tree)
  11164. (and org-highlight-sparse-tree-matches
  11165. (org-get-heading) (match-end 0)
  11166. (org-highlight-new-match
  11167. (match-beginning 0) (match-beginning 1)))
  11168. (org-show-context 'tags-tree))
  11169. ((eq action 'agenda)
  11170. (setq txt (org-format-agenda-item
  11171. ""
  11172. (concat
  11173. (if (eq org-tags-match-list-sublevels 'indented)
  11174. (make-string (1- level) ?.) "")
  11175. (org-get-heading))
  11176. category
  11177. tags-list
  11178. )
  11179. priority (org-get-priority txt))
  11180. (goto-char lspos)
  11181. (setq marker (org-agenda-new-marker))
  11182. (org-add-props txt props
  11183. 'org-marker marker 'org-hd-marker marker 'org-category category
  11184. 'todo-state todo
  11185. 'priority priority 'type "tagsmatch")
  11186. (push txt rtn))
  11187. ((functionp action)
  11188. (setq org-map-continue-from nil)
  11189. (save-excursion
  11190. (setq rtn1 (funcall action))
  11191. (push rtn1 rtn)))
  11192. (t (error "Invalid action")))
  11193. ;; if we are to skip sublevels, jump to end of subtree
  11194. (unless org-tags-match-list-sublevels
  11195. (org-end-of-subtree t)
  11196. (backward-char 1))))
  11197. ;; Get the correct position from where to continue
  11198. (if org-map-continue-from
  11199. (goto-char org-map-continue-from)
  11200. (and (= (point) lspos) (end-of-line 1)))))
  11201. (when (and (eq action 'sparse-tree)
  11202. (not org-sparse-tree-open-archived-trees))
  11203. (org-hide-archived-subtrees (point-min) (point-max)))
  11204. (nreverse rtn)))
  11205. (defun org-remove-uniherited-tags (tags)
  11206. "Remove all tags that are not inherited from the list TAGS."
  11207. (cond
  11208. ((eq org-use-tag-inheritance t)
  11209. (if org-tags-exclude-from-inheritance
  11210. (org-delete-all org-tags-exclude-from-inheritance tags)
  11211. tags))
  11212. ((not org-use-tag-inheritance) nil)
  11213. ((stringp org-use-tag-inheritance)
  11214. (delq nil (mapcar
  11215. (lambda (x)
  11216. (if (and (string-match org-use-tag-inheritance x)
  11217. (not (member x org-tags-exclude-from-inheritance)))
  11218. x nil))
  11219. tags)))
  11220. ((listp org-use-tag-inheritance)
  11221. (delq nil (mapcar
  11222. (lambda (x)
  11223. (if (member x org-use-tag-inheritance) x nil))
  11224. tags)))))
  11225. (defvar todo-only) ;; dynamically scoped
  11226. (defun org-match-sparse-tree (&optional todo-only match)
  11227. "Create a sparse tree according to tags string MATCH.
  11228. MATCH can contain positive and negative selection of tags, like
  11229. \"+WORK+URGENT-WITHBOSS\".
  11230. If optional argument TODO-ONLY is non-nil, only select lines that are
  11231. also TODO lines."
  11232. (interactive "P")
  11233. (org-prepare-agenda-buffers (list (current-buffer)))
  11234. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11235. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11236. (defvar org-cached-props nil)
  11237. (defun org-cached-entry-get (pom property)
  11238. (if (or (eq t org-use-property-inheritance)
  11239. (and (stringp org-use-property-inheritance)
  11240. (string-match org-use-property-inheritance property))
  11241. (and (listp org-use-property-inheritance)
  11242. (member property org-use-property-inheritance)))
  11243. ;; Caching is not possible, check it directly
  11244. (org-entry-get pom property 'inherit)
  11245. ;; Get all properties, so that we can do complicated checks easily
  11246. (cdr (assoc property (or org-cached-props
  11247. (setq org-cached-props
  11248. (org-entry-properties pom)))))))
  11249. (defun org-global-tags-completion-table (&optional files)
  11250. "Return the list of all tags in all agenda buffer/files."
  11251. (save-excursion
  11252. (org-uniquify
  11253. (delq nil
  11254. (apply 'append
  11255. (mapcar
  11256. (lambda (file)
  11257. (set-buffer (find-file-noselect file))
  11258. (append (org-get-buffer-tags)
  11259. (mapcar (lambda (x) (if (stringp (car-safe x))
  11260. (list (car-safe x)) nil))
  11261. org-tag-alist)))
  11262. (if (and files (car files))
  11263. files
  11264. (org-agenda-files))))))))
  11265. (defun org-make-tags-matcher (match)
  11266. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11267. ;; todo-only is scoped dynamically into this function, and the function
  11268. ;; may change it if the matcher asks for it.
  11269. (unless match
  11270. ;; Get a new match request, with completion
  11271. (let ((org-last-tags-completion-table
  11272. (org-global-tags-completion-table)))
  11273. (setq match (org-completing-read-no-i
  11274. "Match: " 'org-tags-completion-function nil nil nil
  11275. 'org-tags-history))))
  11276. ;; Parse the string and create a lisp form
  11277. (let ((match0 match)
  11278. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11279. minus tag mm
  11280. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11281. orterms term orlist re-p str-p level-p level-op time-p
  11282. prop-p pn pv po cat-p gv rest)
  11283. (if (string-match "/+" match)
  11284. ;; match contains also a todo-matching request
  11285. (progn
  11286. (setq tagsmatch (substring match 0 (match-beginning 0))
  11287. todomatch (substring match (match-end 0)))
  11288. (if (string-match "^!" todomatch)
  11289. (setq todo-only t todomatch (substring todomatch 1)))
  11290. (if (string-match "^\\s-*$" todomatch)
  11291. (setq todomatch nil)))
  11292. ;; only matching tags
  11293. (setq tagsmatch match todomatch nil))
  11294. ;; Make the tags matcher
  11295. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11296. (setq tagsmatcher t)
  11297. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11298. (while (setq term (pop orterms))
  11299. (while (and (equal (substring term -1) "\\") orterms)
  11300. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11301. (while (string-match re term)
  11302. (setq rest (substring term (match-end 0))
  11303. minus (and (match-end 1)
  11304. (equal (match-string 1 term) "-"))
  11305. tag (save-match-data (replace-regexp-in-string
  11306. "\\\\-" "-"
  11307. (match-string 2 term)))
  11308. re-p (equal (string-to-char tag) ?{)
  11309. level-p (match-end 4)
  11310. prop-p (match-end 5)
  11311. mm (cond
  11312. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11313. (level-p
  11314. (setq level-op (org-op-to-function (match-string 3 term)))
  11315. `(,level-op level ,(string-to-number
  11316. (match-string 4 term))))
  11317. (prop-p
  11318. (setq pn (match-string 5 term)
  11319. po (match-string 6 term)
  11320. pv (match-string 7 term)
  11321. cat-p (equal pn "CATEGORY")
  11322. re-p (equal (string-to-char pv) ?{)
  11323. str-p (equal (string-to-char pv) ?\")
  11324. time-p (save-match-data
  11325. (string-match "^\"[[<].*[]>]\"$" pv))
  11326. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11327. (if time-p (setq pv (org-matcher-time pv)))
  11328. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11329. (cond
  11330. ((equal pn "CATEGORY")
  11331. (setq gv '(get-text-property (point) 'org-category)))
  11332. ((equal pn "TODO")
  11333. (setq gv 'todo))
  11334. (t
  11335. (setq gv `(org-cached-entry-get nil ,pn))))
  11336. (if re-p
  11337. (if (eq po 'org<>)
  11338. `(not (string-match ,pv (or ,gv "")))
  11339. `(string-match ,pv (or ,gv "")))
  11340. (if str-p
  11341. `(,po (or ,gv "") ,pv)
  11342. `(,po (string-to-number (or ,gv ""))
  11343. ,(string-to-number pv) ))))
  11344. (t `(member ,tag tags-list)))
  11345. mm (if minus (list 'not mm) mm)
  11346. term rest)
  11347. (push mm tagsmatcher))
  11348. (push (if (> (length tagsmatcher) 1)
  11349. (cons 'and tagsmatcher)
  11350. (car tagsmatcher))
  11351. orlist)
  11352. (setq tagsmatcher nil))
  11353. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11354. (setq tagsmatcher
  11355. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11356. ;; Make the todo matcher
  11357. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11358. (setq todomatcher t)
  11359. (setq orterms (org-split-string todomatch "|") orlist nil)
  11360. (while (setq term (pop orterms))
  11361. (while (string-match re term)
  11362. (setq minus (and (match-end 1)
  11363. (equal (match-string 1 term) "-"))
  11364. kwd (match-string 2 term)
  11365. re-p (equal (string-to-char kwd) ?{)
  11366. term (substring term (match-end 0))
  11367. mm (if re-p
  11368. `(string-match ,(substring kwd 1 -1) todo)
  11369. (list 'equal 'todo kwd))
  11370. mm (if minus (list 'not mm) mm))
  11371. (push mm todomatcher))
  11372. (push (if (> (length todomatcher) 1)
  11373. (cons 'and todomatcher)
  11374. (car todomatcher))
  11375. orlist)
  11376. (setq todomatcher nil))
  11377. (setq todomatcher (if (> (length orlist) 1)
  11378. (cons 'or orlist) (car orlist))))
  11379. ;; Return the string and lisp forms of the matcher
  11380. (setq matcher (if todomatcher
  11381. (list 'and tagsmatcher todomatcher)
  11382. tagsmatcher))
  11383. (cons match0 matcher)))
  11384. (defun org-op-to-function (op &optional stringp)
  11385. "Turn an operator into the appropriate function."
  11386. (setq op
  11387. (cond
  11388. ((equal op "<" ) '(< string< org-time<))
  11389. ((equal op ">" ) '(> org-string> org-time>))
  11390. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11391. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11392. ((member op '("=" "==")) '(= string= org-time=))
  11393. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11394. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11395. (defun org<> (a b) (not (= a b)))
  11396. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11397. (defun org-string>= (a b) (not (string< a b)))
  11398. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11399. (defun org-string<> (a b) (not (string= a b)))
  11400. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11401. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11402. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11403. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11404. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11405. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11406. (defun org-2ft (s)
  11407. "Convert S to a floating point time.
  11408. If S is already a number, just return it. If it is a string, parse
  11409. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11410. (cond
  11411. ((numberp s) s)
  11412. ((stringp s)
  11413. (condition-case nil
  11414. (float-time (apply 'encode-time (org-parse-time-string s)))
  11415. (error 0.)))
  11416. (t 0.)))
  11417. (defun org-time-today ()
  11418. "Time in seconds today at 0:00.
  11419. Returns the float number of seconds since the beginning of the
  11420. epoch to the beginning of today (00:00)."
  11421. (float-time (apply 'encode-time
  11422. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11423. (defun org-matcher-time (s)
  11424. "Interpret a time comparison value."
  11425. (save-match-data
  11426. (cond
  11427. ((string= s "<now>") (float-time))
  11428. ((string= s "<today>") (org-time-today))
  11429. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11430. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11431. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11432. (+ (org-time-today)
  11433. (* (string-to-number (match-string 1 s))
  11434. (cdr (assoc (match-string 2 s)
  11435. '(("d" . 86400.0) ("w" . 604800.0)
  11436. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11437. (t (org-2ft s)))))
  11438. (defun org-match-any-p (re list)
  11439. "Does re match any element of list?"
  11440. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11441. (delq nil list))
  11442. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11443. (defvar org-tags-overlay (make-overlay 1 1))
  11444. (org-detach-overlay org-tags-overlay)
  11445. (defun org-get-local-tags-at (&optional pos)
  11446. "Get a list of tags defined in the current headline."
  11447. (org-get-tags-at pos 'local))
  11448. (defun org-get-local-tags ()
  11449. "Get a list of tags defined in the current headline."
  11450. (org-get-tags-at nil 'local))
  11451. (defun org-get-tags-at (&optional pos local)
  11452. "Get a list of all headline tags applicable at POS.
  11453. POS defaults to point. If tags are inherited, the list contains
  11454. the targets in the same sequence as the headlines appear, i.e.
  11455. the tags of the current headline come last.
  11456. When LOCAL is non-nil, only return tags from the current headline,
  11457. ignore inherited ones."
  11458. (interactive)
  11459. (if (and org-trust-scanner-tags
  11460. (or (not pos) (equal pos (point)))
  11461. (not local))
  11462. org-scanner-tags
  11463. (let (tags ltags lastpos parent)
  11464. (save-excursion
  11465. (save-restriction
  11466. (widen)
  11467. (goto-char (or pos (point)))
  11468. (save-match-data
  11469. (catch 'done
  11470. (condition-case nil
  11471. (progn
  11472. (org-back-to-heading t)
  11473. (while (not (equal lastpos (point)))
  11474. (setq lastpos (point))
  11475. (when (looking-at
  11476. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11477. (setq ltags (org-split-string
  11478. (org-match-string-no-properties 1) ":"))
  11479. (when parent
  11480. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11481. (setq tags (append
  11482. (if parent
  11483. (org-remove-uniherited-tags ltags)
  11484. ltags)
  11485. tags)))
  11486. (or org-use-tag-inheritance (throw 'done t))
  11487. (if local (throw 'done t))
  11488. (or (org-up-heading-safe) (error nil))
  11489. (setq parent t)))
  11490. (error nil)))))
  11491. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11492. (defun org-add-prop-inherited (s)
  11493. (add-text-properties 0 (length s) '(inherited t) s)
  11494. s)
  11495. (defun org-toggle-tag (tag &optional onoff)
  11496. "Toggle the tag TAG for the current line.
  11497. If ONOFF is `on' or `off', don't toggle but set to this state."
  11498. (let (res current)
  11499. (save-excursion
  11500. (org-back-to-heading t)
  11501. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11502. (point-at-eol) t)
  11503. (progn
  11504. (setq current (match-string 1))
  11505. (replace-match ""))
  11506. (setq current ""))
  11507. (setq current (nreverse (org-split-string current ":")))
  11508. (cond
  11509. ((eq onoff 'on)
  11510. (setq res t)
  11511. (or (member tag current) (push tag current)))
  11512. ((eq onoff 'off)
  11513. (or (not (member tag current)) (setq current (delete tag current))))
  11514. (t (if (member tag current)
  11515. (setq current (delete tag current))
  11516. (setq res t)
  11517. (push tag current))))
  11518. (end-of-line 1)
  11519. (if current
  11520. (progn
  11521. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11522. (org-set-tags nil t))
  11523. (delete-horizontal-space))
  11524. (run-hooks 'org-after-tags-change-hook))
  11525. res))
  11526. (defun org-align-tags-here (to-col)
  11527. ;; Assumes that this is a headline
  11528. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11529. (beginning-of-line 1)
  11530. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11531. (< pos (match-beginning 2)))
  11532. (progn
  11533. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11534. (goto-char (match-beginning 1))
  11535. (insert " ")
  11536. (delete-region (point) (1+ (match-beginning 2)))
  11537. (setq ncol (max (1+ (current-column))
  11538. (1+ col)
  11539. (if (> to-col 0)
  11540. to-col
  11541. (- (abs to-col) tags-l))))
  11542. (setq p (point))
  11543. (insert (make-string (- ncol (current-column)) ?\ ))
  11544. (setq ncol (current-column))
  11545. (when indent-tabs-mode (tabify p (point-at-eol)))
  11546. (org-move-to-column (min ncol col) t))
  11547. (goto-char pos))))
  11548. (defun org-set-tags-command (&optional arg just-align)
  11549. "Call the set-tags command for the current entry."
  11550. (interactive "P")
  11551. (if (org-on-heading-p)
  11552. (org-set-tags arg just-align)
  11553. (save-excursion
  11554. (org-back-to-heading t)
  11555. (org-set-tags arg just-align))))
  11556. (defun org-set-tags-to (data)
  11557. "Set the tags of the current entry to DATA, replacing the current tags.
  11558. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11559. If DATA is nil or the empty string, any tags will be removed."
  11560. (interactive "sTags: ")
  11561. (setq data
  11562. (cond
  11563. ((eq data nil) "")
  11564. ((equal data "") "")
  11565. ((stringp data)
  11566. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11567. ":"))
  11568. ((listp data)
  11569. (concat ":" (mapconcat 'identity data ":") ":"))
  11570. (t nil)))
  11571. (when data
  11572. (save-excursion
  11573. (org-back-to-heading t)
  11574. (when (looking-at org-complex-heading-regexp)
  11575. (if (match-end 5)
  11576. (progn
  11577. (goto-char (match-beginning 5))
  11578. (insert data)
  11579. (delete-region (point) (point-at-eol))
  11580. (org-set-tags nil 'align))
  11581. (goto-char (point-at-eol))
  11582. (insert " " data)
  11583. (org-set-tags nil 'align)))
  11584. (beginning-of-line 1)
  11585. (if (looking-at ".*?\\([ \t]+\\)$")
  11586. (delete-region (match-beginning 1) (match-end 1))))))
  11587. (defun org-align-all-tags ()
  11588. "Align the tags i all headings."
  11589. (interactive)
  11590. (save-excursion
  11591. (or (ignore-errors (org-back-to-heading t))
  11592. (outline-next-heading))
  11593. (if (org-on-heading-p)
  11594. (org-set-tags t)
  11595. (message "No headings"))))
  11596. (defvar org-indent-indentation-per-level)
  11597. (defun org-set-tags (&optional arg just-align)
  11598. "Set the tags for the current headline.
  11599. With prefix ARG, realign all tags in headings in the current buffer."
  11600. (interactive "P")
  11601. (let* ((re (concat "^" outline-regexp))
  11602. (current (org-get-tags-string))
  11603. (col (current-column))
  11604. (org-setting-tags t)
  11605. table current-tags inherited-tags ; computed below when needed
  11606. tags p0 c0 c1 rpl di tc level)
  11607. (if arg
  11608. (save-excursion
  11609. (goto-char (point-min))
  11610. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11611. (while (re-search-forward re nil t)
  11612. (org-set-tags nil t)
  11613. (end-of-line 1)))
  11614. (message "All tags realigned to column %d" org-tags-column))
  11615. (if just-align
  11616. (setq tags current)
  11617. ;; Get a new set of tags from the user
  11618. (save-excursion
  11619. (setq table (append org-tag-persistent-alist
  11620. (or org-tag-alist (org-get-buffer-tags))
  11621. (and org-complete-tags-always-offer-all-agenda-tags
  11622. (org-global-tags-completion-table (org-agenda-files))))
  11623. org-last-tags-completion-table table
  11624. current-tags (org-split-string current ":")
  11625. inherited-tags (nreverse
  11626. (nthcdr (length current-tags)
  11627. (nreverse (org-get-tags-at))))
  11628. tags
  11629. (if (or (eq t org-use-fast-tag-selection)
  11630. (and org-use-fast-tag-selection
  11631. (delq nil (mapcar 'cdr table))))
  11632. (org-fast-tag-selection
  11633. current-tags inherited-tags table
  11634. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11635. (let ((org-add-colon-after-tag-completion t))
  11636. (org-trim
  11637. (org-without-partial-completion
  11638. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11639. nil nil current 'org-tags-history)))))))
  11640. (while (string-match "[-+&]+" tags)
  11641. ;; No boolean logic, just a list
  11642. (setq tags (replace-match ":" t t tags))))
  11643. (if org-tags-sort-function
  11644. (setq tags (mapconcat 'identity
  11645. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  11646. org-tags-sort-function) ":")))
  11647. (if (string-match "\\`[\t ]*\\'" tags)
  11648. (setq tags "")
  11649. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11650. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11651. ;; Insert new tags at the correct column
  11652. (beginning-of-line 1)
  11653. (setq level (or (and (looking-at org-outline-regexp)
  11654. (- (match-end 0) (point) 1))
  11655. 1))
  11656. (cond
  11657. ((and (equal current "") (equal tags "")))
  11658. ((re-search-forward
  11659. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11660. (point-at-eol) t)
  11661. (if (equal tags "")
  11662. (setq rpl "")
  11663. (goto-char (match-beginning 0))
  11664. (setq c0 (current-column)
  11665. ;; compute offset for the case of org-indent-mode active
  11666. di (if org-indent-mode
  11667. (* (1- org-indent-indentation-per-level) (1- level))
  11668. 0)
  11669. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11670. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11671. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11672. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11673. (replace-match rpl t t)
  11674. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11675. tags)
  11676. (t (error "Tags alignment failed")))
  11677. (org-move-to-column col)
  11678. (unless just-align
  11679. (run-hooks 'org-after-tags-change-hook)))))
  11680. (defun org-change-tag-in-region (beg end tag off)
  11681. "Add or remove TAG for each entry in the region.
  11682. This works in the agenda, and also in an org-mode buffer."
  11683. (interactive
  11684. (list (region-beginning) (region-end)
  11685. (let ((org-last-tags-completion-table
  11686. (if (org-mode-p)
  11687. (org-get-buffer-tags)
  11688. (org-global-tags-completion-table))))
  11689. (org-icompleting-read
  11690. "Tag: " 'org-tags-completion-function nil nil nil
  11691. 'org-tags-history))
  11692. (progn
  11693. (message "[s]et or [r]emove? ")
  11694. (equal (read-char-exclusive) ?r))))
  11695. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11696. (let ((agendap (equal major-mode 'org-agenda-mode))
  11697. l1 l2 m buf pos newhead (cnt 0))
  11698. (goto-char end)
  11699. (setq l2 (1- (org-current-line)))
  11700. (goto-char beg)
  11701. (setq l1 (org-current-line))
  11702. (loop for l from l1 to l2 do
  11703. (org-goto-line l)
  11704. (setq m (get-text-property (point) 'org-hd-marker))
  11705. (when (or (and (org-mode-p) (org-on-heading-p))
  11706. (and agendap m))
  11707. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11708. pos (if agendap m (point)))
  11709. (with-current-buffer buf
  11710. (save-excursion
  11711. (save-restriction
  11712. (goto-char pos)
  11713. (setq cnt (1+ cnt))
  11714. (org-toggle-tag tag (if off 'off 'on))
  11715. (setq newhead (org-get-heading)))))
  11716. (and agendap (org-agenda-change-all-lines newhead m))))
  11717. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11718. (defun org-tags-completion-function (string predicate &optional flag)
  11719. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11720. (confirm (lambda (x) (stringp (car x)))))
  11721. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11722. (setq s1 (match-string 1 string)
  11723. s2 (match-string 2 string))
  11724. (setq s1 "" s2 string))
  11725. (cond
  11726. ((eq flag nil)
  11727. ;; try completion
  11728. (setq rtn (try-completion s2 ctable confirm))
  11729. (if (stringp rtn)
  11730. (setq rtn
  11731. (concat s1 s2 (substring rtn (length s2))
  11732. (if (and org-add-colon-after-tag-completion
  11733. (assoc rtn ctable))
  11734. ":" ""))))
  11735. rtn)
  11736. ((eq flag t)
  11737. ;; all-completions
  11738. (all-completions s2 ctable confirm)
  11739. )
  11740. ((eq flag 'lambda)
  11741. ;; exact match?
  11742. (assoc s2 ctable)))
  11743. ))
  11744. (defun org-fast-tag-insert (kwd tags face &optional end)
  11745. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11746. (insert (format "%-12s" (concat kwd ":"))
  11747. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11748. (or end "")))
  11749. (defun org-fast-tag-show-exit (flag)
  11750. (save-excursion
  11751. (org-goto-line 3)
  11752. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11753. (replace-match ""))
  11754. (when flag
  11755. (end-of-line 1)
  11756. (org-move-to-column (- (window-width) 19) t)
  11757. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11758. (defun org-set-current-tags-overlay (current prefix)
  11759. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11760. (if (featurep 'xemacs)
  11761. (org-overlay-display org-tags-overlay (concat prefix s)
  11762. 'secondary-selection)
  11763. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11764. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11765. (defvar org-last-tag-selection-key nil)
  11766. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11767. "Fast tag selection with single keys.
  11768. CURRENT is the current list of tags in the headline, INHERITED is the
  11769. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11770. possibly with grouping information. TODO-TABLE is a similar table with
  11771. TODO keywords, should these have keys assigned to them.
  11772. If the keys are nil, a-z are automatically assigned.
  11773. Returns the new tags string, or nil to not change the current settings."
  11774. (let* ((fulltable (append table todo-table))
  11775. (maxlen (apply 'max (mapcar
  11776. (lambda (x)
  11777. (if (stringp (car x)) (string-width (car x)) 0))
  11778. fulltable)))
  11779. (buf (current-buffer))
  11780. (expert (eq org-fast-tag-selection-single-key 'expert))
  11781. (buffer-tags nil)
  11782. (fwidth (+ maxlen 3 1 3))
  11783. (ncol (/ (- (window-width) 4) fwidth))
  11784. (i-face 'org-done)
  11785. (c-face 'org-todo)
  11786. tg cnt e c char c1 c2 ntable tbl rtn
  11787. ov-start ov-end ov-prefix
  11788. (exit-after-next org-fast-tag-selection-single-key)
  11789. (done-keywords org-done-keywords)
  11790. groups ingroup)
  11791. (save-excursion
  11792. (beginning-of-line 1)
  11793. (if (looking-at
  11794. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11795. (setq ov-start (match-beginning 1)
  11796. ov-end (match-end 1)
  11797. ov-prefix "")
  11798. (setq ov-start (1- (point-at-eol))
  11799. ov-end (1+ ov-start))
  11800. (skip-chars-forward "^\n\r")
  11801. (setq ov-prefix
  11802. (concat
  11803. (buffer-substring (1- (point)) (point))
  11804. (if (> (current-column) org-tags-column)
  11805. " "
  11806. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11807. (move-overlay org-tags-overlay ov-start ov-end)
  11808. (save-window-excursion
  11809. (if expert
  11810. (set-buffer (get-buffer-create " *Org tags*"))
  11811. (delete-other-windows)
  11812. (split-window-vertically)
  11813. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11814. (erase-buffer)
  11815. (org-set-local 'org-done-keywords done-keywords)
  11816. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11817. (org-fast-tag-insert "Current" current c-face "\n\n")
  11818. (org-fast-tag-show-exit exit-after-next)
  11819. (org-set-current-tags-overlay current ov-prefix)
  11820. (setq tbl fulltable char ?a cnt 0)
  11821. (while (setq e (pop tbl))
  11822. (cond
  11823. ((equal (car e) :startgroup)
  11824. (push '() groups) (setq ingroup t)
  11825. (when (not (= cnt 0))
  11826. (setq cnt 0)
  11827. (insert "\n"))
  11828. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11829. ((equal (car e) :endgroup)
  11830. (setq ingroup nil cnt 0)
  11831. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11832. ((equal e '(:newline))
  11833. (when (not (= cnt 0))
  11834. (setq cnt 0)
  11835. (insert "\n")
  11836. (setq e (car tbl))
  11837. (while (equal (car tbl) '(:newline))
  11838. (insert "\n")
  11839. (setq tbl (cdr tbl)))))
  11840. (t
  11841. (setq tg (copy-sequence (car e)) c2 nil)
  11842. (if (cdr e)
  11843. (setq c (cdr e))
  11844. ;; automatically assign a character.
  11845. (setq c1 (string-to-char
  11846. (downcase (substring
  11847. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11848. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11849. (while (or (rassoc char ntable) (rassoc char table))
  11850. (setq char (1+ char)))
  11851. (setq c2 c1))
  11852. (setq c (or c2 char)))
  11853. (if ingroup (push tg (car groups)))
  11854. (setq tg (org-add-props tg nil 'face
  11855. (cond
  11856. ((not (assoc tg table))
  11857. (org-get-todo-face tg))
  11858. ((member tg current) c-face)
  11859. ((member tg inherited) i-face)
  11860. (t nil))))
  11861. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11862. (insert "[" c "] " tg (make-string
  11863. (- fwidth 4 (length tg)) ?\ ))
  11864. (push (cons tg c) ntable)
  11865. (when (= (setq cnt (1+ cnt)) ncol)
  11866. (insert "\n")
  11867. (if ingroup (insert " "))
  11868. (setq cnt 0)))))
  11869. (setq ntable (nreverse ntable))
  11870. (insert "\n")
  11871. (goto-char (point-min))
  11872. (if (not expert) (org-fit-window-to-buffer))
  11873. (setq rtn
  11874. (catch 'exit
  11875. (while t
  11876. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11877. (if (not groups) "no " "")
  11878. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11879. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11880. (setq org-last-tag-selection-key c)
  11881. (cond
  11882. ((= c ?\r) (throw 'exit t))
  11883. ((= c ?!)
  11884. (setq groups (not groups))
  11885. (goto-char (point-min))
  11886. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11887. ((= c ?\C-c)
  11888. (if (not expert)
  11889. (org-fast-tag-show-exit
  11890. (setq exit-after-next (not exit-after-next)))
  11891. (setq expert nil)
  11892. (delete-other-windows)
  11893. (split-window-vertically)
  11894. (org-switch-to-buffer-other-window " *Org tags*")
  11895. (org-fit-window-to-buffer)))
  11896. ((or (= c ?\C-g)
  11897. (and (= c ?q) (not (rassoc c ntable))))
  11898. (org-detach-overlay org-tags-overlay)
  11899. (setq quit-flag t))
  11900. ((= c ?\ )
  11901. (setq current nil)
  11902. (if exit-after-next (setq exit-after-next 'now)))
  11903. ((= c ?\t)
  11904. (condition-case nil
  11905. (setq tg (org-icompleting-read
  11906. "Tag: "
  11907. (or buffer-tags
  11908. (with-current-buffer buf
  11909. (org-get-buffer-tags)))))
  11910. (quit (setq tg "")))
  11911. (when (string-match "\\S-" tg)
  11912. (add-to-list 'buffer-tags (list tg))
  11913. (if (member tg current)
  11914. (setq current (delete tg current))
  11915. (push tg current)))
  11916. (if exit-after-next (setq exit-after-next 'now)))
  11917. ((setq e (rassoc c todo-table) tg (car e))
  11918. (with-current-buffer buf
  11919. (save-excursion (org-todo tg)))
  11920. (if exit-after-next (setq exit-after-next 'now)))
  11921. ((setq e (rassoc c ntable) tg (car e))
  11922. (if (member tg current)
  11923. (setq current (delete tg current))
  11924. (loop for g in groups do
  11925. (if (member tg g)
  11926. (mapc (lambda (x)
  11927. (setq current (delete x current)))
  11928. g)))
  11929. (push tg current))
  11930. (if exit-after-next (setq exit-after-next 'now))))
  11931. ;; Create a sorted list
  11932. (setq current
  11933. (sort current
  11934. (lambda (a b)
  11935. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11936. (if (eq exit-after-next 'now) (throw 'exit t))
  11937. (goto-char (point-min))
  11938. (beginning-of-line 2)
  11939. (delete-region (point) (point-at-eol))
  11940. (org-fast-tag-insert "Current" current c-face)
  11941. (org-set-current-tags-overlay current ov-prefix)
  11942. (while (re-search-forward
  11943. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  11944. (setq tg (match-string 1))
  11945. (add-text-properties
  11946. (match-beginning 1) (match-end 1)
  11947. (list 'face
  11948. (cond
  11949. ((member tg current) c-face)
  11950. ((member tg inherited) i-face)
  11951. (t (get-text-property (match-beginning 1) 'face))))))
  11952. (goto-char (point-min)))))
  11953. (org-detach-overlay org-tags-overlay)
  11954. (if rtn
  11955. (mapconcat 'identity current ":")
  11956. nil))))
  11957. (defun org-get-tags-string ()
  11958. "Get the TAGS string in the current headline."
  11959. (unless (org-on-heading-p t)
  11960. (error "Not on a heading"))
  11961. (save-excursion
  11962. (beginning-of-line 1)
  11963. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11964. (org-match-string-no-properties 1)
  11965. "")))
  11966. (defun org-get-tags ()
  11967. "Get the list of tags specified in the current headline."
  11968. (org-split-string (org-get-tags-string) ":"))
  11969. (defun org-get-buffer-tags ()
  11970. "Get a table of all tags used in the buffer, for completion."
  11971. (let (tags)
  11972. (save-excursion
  11973. (goto-char (point-min))
  11974. (while (re-search-forward
  11975. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  11976. (when (equal (char-after (point-at-bol 0)) ?*)
  11977. (mapc (lambda (x) (add-to-list 'tags x))
  11978. (org-split-string (org-match-string-no-properties 1) ":")))))
  11979. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11980. (mapcar 'list tags)))
  11981. ;;;; The mapping API
  11982. ;;;###autoload
  11983. (defun org-map-entries (func &optional match scope &rest skip)
  11984. "Call FUNC at each headline selected by MATCH in SCOPE.
  11985. FUNC is a function or a lisp form. The function will be called without
  11986. arguments, with the cursor positioned at the beginning of the headline.
  11987. The return values of all calls to the function will be collected and
  11988. returned as a list.
  11989. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11990. does not need to preserve point. After evaluation, the cursor will be
  11991. moved to the end of the line (presumably of the headline of the
  11992. processed entry) and search continues from there. Under some
  11993. circumstances, this may not produce the wanted results. For example,
  11994. if you have removed (e.g. archived) the current (sub)tree it could
  11995. mean that the next entry will be skipped entirely. In such cases, you
  11996. can specify the position from where search should continue by making
  11997. FUNC set the variable `org-map-continue-from' to the desired buffer
  11998. position.
  11999. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12000. Only headlines that are matched by this query will be considered during
  12001. the iteration. When MATCH is nil or t, all headlines will be
  12002. visited by the iteration.
  12003. SCOPE determines the scope of this command. It can be any of:
  12004. nil The current buffer, respecting the restriction if any
  12005. tree The subtree started with the entry at point
  12006. file The current buffer, without restriction
  12007. file-with-archives
  12008. The current buffer, and any archives associated with it
  12009. agenda All agenda files
  12010. agenda-with-archives
  12011. All agenda files with any archive files associated with them
  12012. \(file1 file2 ...)
  12013. If this is a list, all files in the list will be scanned
  12014. The remaining args are treated as settings for the skipping facilities of
  12015. the scanner. The following items can be given here:
  12016. archive skip trees with the archive tag.
  12017. comment skip trees with the COMMENT keyword
  12018. function or Emacs Lisp form:
  12019. will be used as value for `org-agenda-skip-function', so whenever
  12020. the function returns t, FUNC will not be called for that
  12021. entry and search will continue from the point where the
  12022. function leaves it.
  12023. If your function needs to retrieve the tags including inherited tags
  12024. at the *current* entry, you can use the value of the variable
  12025. `org-scanner-tags' which will be much faster than getting the value
  12026. with `org-get-tags-at'. If your function gets properties with
  12027. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12028. to t around the call to `org-entry-properties' to get the same speedup.
  12029. Note that if your function moves around to retrieve tags and properties at
  12030. a *different* entry, you cannot use these techniques."
  12031. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12032. (org-agenda-skip-archived-trees (memq 'archive skip))
  12033. (org-agenda-skip-comment-trees (memq 'comment skip))
  12034. (org-agenda-skip-function
  12035. (car (org-delete-all '(comment archive) skip)))
  12036. (org-tags-match-list-sublevels t)
  12037. matcher file res
  12038. org-todo-keywords-for-agenda
  12039. org-done-keywords-for-agenda
  12040. org-todo-keyword-alist-for-agenda
  12041. org-drawers-for-agenda
  12042. org-tag-alist-for-agenda)
  12043. (cond
  12044. ((eq match t) (setq matcher t))
  12045. ((eq match nil) (setq matcher t))
  12046. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12047. (save-excursion
  12048. (save-restriction
  12049. (when (eq scope 'tree)
  12050. (org-back-to-heading t)
  12051. (org-narrow-to-subtree)
  12052. (setq scope nil))
  12053. (if (not scope)
  12054. (progn
  12055. (org-prepare-agenda-buffers
  12056. (list (buffer-file-name (current-buffer))))
  12057. (setq res (org-scan-tags func matcher)))
  12058. ;; Get the right scope
  12059. (cond
  12060. ((and scope (listp scope) (symbolp (car scope)))
  12061. (setq scope (eval scope)))
  12062. ((eq scope 'agenda)
  12063. (setq scope (org-agenda-files t)))
  12064. ((eq scope 'agenda-with-archives)
  12065. (setq scope (org-agenda-files t))
  12066. (setq scope (org-add-archive-files scope)))
  12067. ((eq scope 'file)
  12068. (setq scope (list (buffer-file-name))))
  12069. ((eq scope 'file-with-archives)
  12070. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12071. (org-prepare-agenda-buffers scope)
  12072. (while (setq file (pop scope))
  12073. (with-current-buffer (org-find-base-buffer-visiting file)
  12074. (save-excursion
  12075. (save-restriction
  12076. (widen)
  12077. (goto-char (point-min))
  12078. (setq res (append res (org-scan-tags func matcher))))))))))
  12079. res))
  12080. ;;;; Properties
  12081. ;;; Setting and retrieving properties
  12082. (defconst org-special-properties
  12083. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12084. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12085. "The special properties valid in Org-mode.
  12086. These are properties that are not defined in the property drawer,
  12087. but in some other way.")
  12088. (defconst org-default-properties
  12089. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12090. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12091. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12092. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12093. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12094. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12095. "Some properties that are used by Org-mode for various purposes.
  12096. Being in this list makes sure that they are offered for completion.")
  12097. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12098. "Regular expression matching the first line of a property drawer.")
  12099. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12100. "Regular expression matching the last line of a property drawer.")
  12101. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12102. "Regular expression matching the first line of a property drawer.")
  12103. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12104. "Regular expression matching the first line of a property drawer.")
  12105. (defconst org-property-drawer-re
  12106. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12107. org-property-end-re "\\)\n?")
  12108. "Matches an entire property drawer.")
  12109. (defconst org-clock-drawer-re
  12110. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12111. org-property-end-re "\\)\n?")
  12112. "Matches an entire clock drawer.")
  12113. (defun org-property-action ()
  12114. "Do an action on properties."
  12115. (interactive)
  12116. (let (c)
  12117. (org-at-property-p)
  12118. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12119. (setq c (read-char-exclusive))
  12120. (cond
  12121. ((equal c ?s)
  12122. (call-interactively 'org-set-property))
  12123. ((equal c ?d)
  12124. (call-interactively 'org-delete-property))
  12125. ((equal c ?D)
  12126. (call-interactively 'org-delete-property-globally))
  12127. ((equal c ?c)
  12128. (call-interactively 'org-compute-property-at-point))
  12129. (t (error "No such property action %c" c)))))
  12130. (defun org-set-effort (&optional value)
  12131. "Set the effort property of the current entry.
  12132. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12133. allowed value."
  12134. (interactive "P")
  12135. (if (equal value 0) (setq value 10))
  12136. (let* ((completion-ignore-case t)
  12137. (prop org-effort-property)
  12138. (cur (org-entry-get nil prop))
  12139. (allowed (org-property-get-allowed-values nil prop 'table))
  12140. (existing (mapcar 'list (org-property-values prop)))
  12141. rpl
  12142. (val (cond
  12143. ((stringp value) value)
  12144. ((and allowed (integerp value))
  12145. (or (car (nth (1- value) allowed))
  12146. (car (org-last allowed))))
  12147. (allowed
  12148. (message "Select 1-9,0, [RET%s]: %s"
  12149. (if cur (concat "=" cur) "")
  12150. (mapconcat 'car allowed " "))
  12151. (setq rpl (read-char-exclusive))
  12152. (if (equal rpl ?\r)
  12153. cur
  12154. (setq rpl (- rpl ?0))
  12155. (if (equal rpl 0) (setq rpl 10))
  12156. (if (and (> rpl 0) (<= rpl (length allowed)))
  12157. (car (nth (1- rpl) allowed))
  12158. (org-completing-read "Effort: " allowed nil))))
  12159. (t
  12160. (let (org-completion-use-ido org-completion-use-iswitchb)
  12161. (org-completing-read
  12162. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12163. (concat "[" cur "]") "")
  12164. ": ")
  12165. existing nil nil "" nil cur))))))
  12166. (unless (equal (org-entry-get nil prop) val)
  12167. (org-entry-put nil prop val))
  12168. (message "%s is now %s" prop val)))
  12169. (defun org-at-property-p ()
  12170. "Is cursor inside a property drawer?"
  12171. (save-excursion
  12172. (beginning-of-line 1)
  12173. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12174. (save-match-data ;; Used by calling procedures
  12175. (let ((p (point))
  12176. (range (unless (org-before-first-heading-p)
  12177. (org-get-property-block))))
  12178. (and range (<= (car range) p) (< p (cdr range))))))))
  12179. (defun org-get-property-block (&optional beg end force)
  12180. "Return the (beg . end) range of the body of the property drawer.
  12181. BEG and END can be beginning and end of subtree, if not given
  12182. they will be found.
  12183. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12184. (catch 'exit
  12185. (save-excursion
  12186. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12187. (end (or end (progn (outline-next-heading) (point)))))
  12188. (goto-char beg)
  12189. (if (re-search-forward org-property-start-re end t)
  12190. (setq beg (1+ (match-end 0)))
  12191. (if force
  12192. (save-excursion
  12193. (org-insert-property-drawer)
  12194. (setq end (progn (outline-next-heading) (point))))
  12195. (throw 'exit nil))
  12196. (goto-char beg)
  12197. (if (re-search-forward org-property-start-re end t)
  12198. (setq beg (1+ (match-end 0)))))
  12199. (if (re-search-forward org-property-end-re end t)
  12200. (setq end (match-beginning 0))
  12201. (or force (throw 'exit nil))
  12202. (goto-char beg)
  12203. (setq end beg)
  12204. (org-indent-line-function)
  12205. (insert ":END:\n"))
  12206. (cons beg end)))))
  12207. (defun org-entry-properties (&optional pom which specific)
  12208. "Get all properties of the entry at point-or-marker POM.
  12209. This includes the TODO keyword, the tags, time strings for deadline,
  12210. scheduled, and clocking, and any additional properties defined in the
  12211. entry. The return value is an alist, keys may occur multiple times
  12212. if the property key was used several times.
  12213. POM may also be nil, in which case the current entry is used.
  12214. If WHICH is nil or `all', get all properties. If WHICH is
  12215. `special' or `standard', only get that subclass. If WHICH
  12216. is a string only get exactly this property. Specific can be a string, the
  12217. specific property we are interested in. Specifying it can speed
  12218. things up because then unnecessary parsing is avoided."
  12219. (setq which (or which 'all))
  12220. (org-with-point-at pom
  12221. (let ((clockstr (substring org-clock-string 0 -1))
  12222. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12223. (case-fold-search nil)
  12224. beg end range props sum-props key key1 value string clocksum)
  12225. (save-excursion
  12226. (when (condition-case nil
  12227. (and (org-mode-p) (org-back-to-heading t))
  12228. (error nil))
  12229. (setq beg (point))
  12230. (setq sum-props (get-text-property (point) 'org-summaries))
  12231. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12232. (outline-next-heading)
  12233. (setq end (point))
  12234. (when (memq which '(all special))
  12235. ;; Get the special properties, like TODO and tags
  12236. (goto-char beg)
  12237. (when (and (or (not specific) (string= specific "TODO"))
  12238. (looking-at org-todo-line-regexp) (match-end 2))
  12239. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12240. (when (and (or (not specific) (string= specific "PRIORITY"))
  12241. (looking-at org-priority-regexp))
  12242. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12243. (when (and (or (not specific) (string= specific "TAGS"))
  12244. (setq value (org-get-tags-string))
  12245. (string-match "\\S-" value))
  12246. (push (cons "TAGS" value) props))
  12247. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12248. (setq value (org-get-tags-at)))
  12249. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12250. ":"))
  12251. props))
  12252. (when (or (not specific) (string= specific "BLOCKED"))
  12253. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12254. (when (or (not specific)
  12255. (member specific
  12256. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12257. "TIMESTAMP" "TIMESTAMP_IA")))
  12258. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12259. (setq key (if (match-end 1)
  12260. (substring (org-match-string-no-properties 1)
  12261. 0 -1))
  12262. string (if (equal key clockstr)
  12263. (org-no-properties
  12264. (org-trim
  12265. (buffer-substring
  12266. (match-beginning 3) (goto-char
  12267. (point-at-eol)))))
  12268. (substring (org-match-string-no-properties 3)
  12269. 1 -1)))
  12270. ;; Get the correct property name from the key. This is
  12271. ;; necessary if the user has configured time keywords.
  12272. (setq key1 (concat key ":"))
  12273. (cond
  12274. ((not key)
  12275. (setq key
  12276. (if (= (char-after (match-beginning 3)) ?\[)
  12277. "TIMESTAMP_IA" "TIMESTAMP")))
  12278. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12279. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12280. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12281. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12282. (when (or (equal key "CLOCK") (not (assoc key props)))
  12283. (push (cons key string) props))))
  12284. )
  12285. (when (memq which '(all standard))
  12286. ;; Get the standard properties, like :PROP: ...
  12287. (setq range (org-get-property-block beg end))
  12288. (when range
  12289. (goto-char (car range))
  12290. (while (re-search-forward
  12291. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12292. (cdr range) t)
  12293. (setq key (org-match-string-no-properties 1)
  12294. value (org-trim (or (org-match-string-no-properties 2) "")))
  12295. (unless (member key excluded)
  12296. (push (cons key (or value "")) props)))))
  12297. (if clocksum
  12298. (push (cons "CLOCKSUM"
  12299. (org-columns-number-to-string (/ (float clocksum) 60.)
  12300. 'add_times))
  12301. props))
  12302. (unless (assoc "CATEGORY" props)
  12303. (setq value (or (org-get-category)
  12304. (progn (org-refresh-category-properties)
  12305. (org-get-category))))
  12306. (push (cons "CATEGORY" value) props))
  12307. (append sum-props (nreverse props)))))))
  12308. (defun org-entry-get (pom property &optional inherit literal-nil)
  12309. "Get value of PROPERTY for entry at point-or-marker POM.
  12310. If INHERIT is non-nil and the entry does not have the property,
  12311. then also check higher levels of the hierarchy.
  12312. If INHERIT is the symbol `selective', use inheritance only if the setting
  12313. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12314. If the property is present but empty, the return value is the empty string.
  12315. If the property is not present at all, nil is returned.
  12316. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12317. do not interpret it as the list atom nil. This is used for inheritance
  12318. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12319. (org-with-point-at pom
  12320. (if (and inherit (if (eq inherit 'selective)
  12321. (org-property-inherit-p property)
  12322. t))
  12323. (org-entry-get-with-inheritance property literal-nil)
  12324. (if (member property org-special-properties)
  12325. ;; We need a special property. Use `org-entry-properties' to
  12326. ;; retrieve it, but specify the wanted property
  12327. (cdr (assoc property (org-entry-properties nil 'special property)))
  12328. (let ((range (org-get-property-block)))
  12329. (if (and range
  12330. (goto-char (car range))
  12331. (re-search-forward
  12332. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12333. (cdr range) t))
  12334. ;; Found the property, return it.
  12335. (if (match-end 1)
  12336. (if literal-nil
  12337. (org-match-string-no-properties 1)
  12338. (org-not-nil (org-match-string-no-properties 1)))
  12339. "")))))))
  12340. (defun org-property-or-variable-value (var &optional inherit)
  12341. "Check if there is a property fixing the value of VAR.
  12342. If yes, return this value. If not, return the current value of the variable."
  12343. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12344. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12345. (read prop)
  12346. (symbol-value var))))
  12347. (defun org-entry-delete (pom property)
  12348. "Delete the property PROPERTY from entry at point-or-marker POM."
  12349. (org-with-point-at pom
  12350. (if (member property org-special-properties)
  12351. nil ; cannot delete these properties.
  12352. (let ((range (org-get-property-block)))
  12353. (if (and range
  12354. (goto-char (car range))
  12355. (re-search-forward
  12356. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12357. (cdr range) t))
  12358. (progn
  12359. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12360. t)
  12361. nil)))))
  12362. ;; Multi-values properties are properties that contain multiple values
  12363. ;; These values are assumed to be single words, separated by whitespace.
  12364. (defun org-entry-add-to-multivalued-property (pom property value)
  12365. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12366. (let* ((old (org-entry-get pom property))
  12367. (values (and old (org-split-string old "[ \t]"))))
  12368. (setq value (org-entry-protect-space value))
  12369. (unless (member value values)
  12370. (setq values (cons value values))
  12371. (org-entry-put pom property
  12372. (mapconcat 'identity values " ")))))
  12373. (defun org-entry-remove-from-multivalued-property (pom property value)
  12374. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12375. (let* ((old (org-entry-get pom property))
  12376. (values (and old (org-split-string old "[ \t]"))))
  12377. (setq value (org-entry-protect-space value))
  12378. (when (member value values)
  12379. (setq values (delete value values))
  12380. (org-entry-put pom property
  12381. (mapconcat 'identity values " ")))))
  12382. (defun org-entry-member-in-multivalued-property (pom property value)
  12383. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12384. (let* ((old (org-entry-get pom property))
  12385. (values (and old (org-split-string old "[ \t]"))))
  12386. (setq value (org-entry-protect-space value))
  12387. (member value values)))
  12388. (defun org-entry-get-multivalued-property (pom property)
  12389. "Return a list of values in a multivalued property."
  12390. (let* ((value (org-entry-get pom property))
  12391. (values (and value (org-split-string value "[ \t]"))))
  12392. (mapcar 'org-entry-restore-space values)))
  12393. (defun org-entry-put-multivalued-property (pom property &rest values)
  12394. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12395. VALUES should be a list of strings. Spaces will be protected."
  12396. (org-entry-put pom property
  12397. (mapconcat 'org-entry-protect-space values " "))
  12398. (let* ((value (org-entry-get pom property))
  12399. (values (and value (org-split-string value "[ \t]"))))
  12400. (mapcar 'org-entry-restore-space values)))
  12401. (defun org-entry-protect-space (s)
  12402. "Protect spaces and newline in string S."
  12403. (while (string-match " " s)
  12404. (setq s (replace-match "%20" t t s)))
  12405. (while (string-match "\n" s)
  12406. (setq s (replace-match "%0A" t t s)))
  12407. s)
  12408. (defun org-entry-restore-space (s)
  12409. "Restore spaces and newline in string S."
  12410. (while (string-match "%20" s)
  12411. (setq s (replace-match " " t t s)))
  12412. (while (string-match "%0A" s)
  12413. (setq s (replace-match "\n" t t s)))
  12414. s)
  12415. (defvar org-entry-property-inherited-from (make-marker)
  12416. "Marker pointing to the entry from where a property was inherited.
  12417. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12418. location of the entry where the inheritance search matched. If there was
  12419. no match, the marker will point nowhere.
  12420. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12421. is set.")
  12422. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12423. "Get entry property, and search higher levels if not present.
  12424. The search will stop at the first ancestor which has the property defined.
  12425. If the value found is \"nil\", return nil to show that the property
  12426. should be considered as undefined (this is the meaning of nil here).
  12427. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12428. (move-marker org-entry-property-inherited-from nil)
  12429. (let (tmp)
  12430. (save-excursion
  12431. (save-restriction
  12432. (widen)
  12433. (catch 'ex
  12434. (while t
  12435. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12436. (org-back-to-heading t)
  12437. (move-marker org-entry-property-inherited-from (point))
  12438. (throw 'ex tmp))
  12439. (or (org-up-heading-safe) (throw 'ex nil)))))
  12440. (setq tmp (or tmp
  12441. (cdr (assoc property org-file-properties))
  12442. (cdr (assoc property org-global-properties))
  12443. (cdr (assoc property org-global-properties-fixed))))
  12444. (if literal-nil tmp (org-not-nil tmp)))))
  12445. (defvar org-property-changed-functions nil
  12446. "Hook called when the value of a property has changed.
  12447. Each hook function should accept two arguments, the name of the property
  12448. and the new value.")
  12449. (defun org-entry-put (pom property value)
  12450. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12451. (org-with-point-at pom
  12452. (org-back-to-heading t)
  12453. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12454. range)
  12455. (cond
  12456. ((equal property "TODO")
  12457. (when (and (stringp value) (string-match "\\S-" value)
  12458. (not (member value org-todo-keywords-1)))
  12459. (error "\"%s\" is not a valid TODO state" value))
  12460. (if (or (not value)
  12461. (not (string-match "\\S-" value)))
  12462. (setq value 'none))
  12463. (org-todo value)
  12464. (org-set-tags nil 'align))
  12465. ((equal property "PRIORITY")
  12466. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12467. (string-to-char value) ?\ ))
  12468. (org-set-tags nil 'align))
  12469. ((equal property "SCHEDULED")
  12470. (if (re-search-forward org-scheduled-time-regexp end t)
  12471. (cond
  12472. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12473. ((eq value 'later) (org-timestamp-change 1 'day))
  12474. (t (call-interactively 'org-schedule)))
  12475. (call-interactively 'org-schedule)))
  12476. ((equal property "DEADLINE")
  12477. (if (re-search-forward org-deadline-time-regexp end t)
  12478. (cond
  12479. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12480. ((eq value 'later) (org-timestamp-change 1 'day))
  12481. (t (call-interactively 'org-deadline)))
  12482. (call-interactively 'org-deadline)))
  12483. ((member property org-special-properties)
  12484. (error "The %s property can not yet be set with `org-entry-put'"
  12485. property))
  12486. (t ; a non-special property
  12487. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12488. (setq range (org-get-property-block beg end 'force))
  12489. (goto-char (car range))
  12490. (if (re-search-forward
  12491. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12492. (progn
  12493. (delete-region (match-beginning 1) (match-end 1))
  12494. (goto-char (match-beginning 1)))
  12495. (goto-char (cdr range))
  12496. (insert "\n")
  12497. (backward-char 1)
  12498. (org-indent-line-function)
  12499. (insert ":" property ":"))
  12500. (and value (insert " " value))
  12501. (org-indent-line-function)))))
  12502. (run-hook-with-args 'org-property-changed-functions property value)))
  12503. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12504. "Get all property keys in the current buffer.
  12505. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12506. like tags and TODO state.
  12507. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12508. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12509. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12510. formats in the current buffer."
  12511. (let (rtn range cfmt s p)
  12512. (save-excursion
  12513. (save-restriction
  12514. (widen)
  12515. (goto-char (point-min))
  12516. (while (re-search-forward org-property-start-re nil t)
  12517. (setq range (org-get-property-block))
  12518. (goto-char (car range))
  12519. (while (re-search-forward
  12520. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12521. (cdr range) t)
  12522. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12523. (outline-next-heading))))
  12524. (when include-specials
  12525. (setq rtn (append org-special-properties rtn)))
  12526. (when include-defaults
  12527. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12528. (add-to-list 'rtn org-effort-property))
  12529. (when include-columns
  12530. (save-excursion
  12531. (save-restriction
  12532. (widen)
  12533. (goto-char (point-min))
  12534. (while (re-search-forward
  12535. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12536. nil t)
  12537. (setq cfmt (match-string 2) s 0)
  12538. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12539. cfmt s)
  12540. (setq s (match-end 0)
  12541. p (match-string 1 cfmt))
  12542. (unless (or (equal p "ITEM")
  12543. (member p org-special-properties))
  12544. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12545. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12546. (defun org-property-values (key)
  12547. "Return a list of all values of property KEY."
  12548. (save-excursion
  12549. (save-restriction
  12550. (widen)
  12551. (goto-char (point-min))
  12552. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12553. values)
  12554. (while (re-search-forward re nil t)
  12555. (add-to-list 'values (org-trim (match-string 1))))
  12556. (delete "" values)))))
  12557. (defun org-insert-property-drawer ()
  12558. "Insert a property drawer into the current entry."
  12559. (interactive)
  12560. (org-back-to-heading t)
  12561. (looking-at outline-regexp)
  12562. (let ((indent (if org-adapt-indentation
  12563. (- (match-end 0)(match-beginning 0))
  12564. 0))
  12565. (beg (point))
  12566. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12567. end hiddenp)
  12568. (outline-next-heading)
  12569. (setq end (point))
  12570. (goto-char beg)
  12571. (while (re-search-forward re end t))
  12572. (setq hiddenp (org-invisible-p))
  12573. (end-of-line 1)
  12574. (and (equal (char-after) ?\n) (forward-char 1))
  12575. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12576. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12577. ;; just skip this line
  12578. (beginning-of-line 2)
  12579. ;; Drawer start, find the end
  12580. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12581. (beginning-of-line 1)))
  12582. (org-skip-over-state-notes)
  12583. (skip-chars-backward " \t\n\r")
  12584. (if (eq (char-before) ?*) (forward-char 1))
  12585. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12586. (beginning-of-line 0)
  12587. (org-indent-to-column indent)
  12588. (beginning-of-line 2)
  12589. (org-indent-to-column indent)
  12590. (beginning-of-line 0)
  12591. (if hiddenp
  12592. (save-excursion
  12593. (org-back-to-heading t)
  12594. (hide-entry))
  12595. (org-flag-drawer t))))
  12596. (defun org-set-property (property value)
  12597. "In the current entry, set PROPERTY to VALUE.
  12598. When called interactively, this will prompt for a property name, offering
  12599. completion on existing and default properties. And then it will prompt
  12600. for a value, offering completion either on allowed values (via an inherited
  12601. xxx_ALL property) or on existing values in other instances of this property
  12602. in the current file."
  12603. (interactive
  12604. (let* ((completion-ignore-case t)
  12605. (keys (org-buffer-property-keys nil t t))
  12606. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12607. (prop (if (member prop0 keys)
  12608. prop0
  12609. (or (cdr (assoc (downcase prop0)
  12610. (mapcar (lambda (x) (cons (downcase x) x))
  12611. keys)))
  12612. prop0)))
  12613. (cur (org-entry-get nil prop))
  12614. (prompt (concat prop " value"
  12615. (if (and cur (string-match "\\S-" cur))
  12616. (concat " [" cur "]") "") ": "))
  12617. (allowed (org-property-get-allowed-values nil prop 'table))
  12618. (existing (mapcar 'list (org-property-values prop)))
  12619. (val (if allowed
  12620. (org-completing-read prompt allowed nil
  12621. (not (get-text-property 0 'org-unrestricted
  12622. (caar allowed))))
  12623. (let (org-completion-use-ido org-completion-use-iswitchb)
  12624. (org-completing-read prompt existing nil nil "" nil cur)))))
  12625. (list prop (if (equal val "") cur val))))
  12626. (unless (equal (org-entry-get nil property) value)
  12627. (org-entry-put nil property value)))
  12628. (defun org-delete-property (property)
  12629. "In the current entry, delete PROPERTY."
  12630. (interactive
  12631. (let* ((completion-ignore-case t)
  12632. (prop (org-icompleting-read "Property: "
  12633. (org-entry-properties nil 'standard))))
  12634. (list prop)))
  12635. (message "Property %s %s" property
  12636. (if (org-entry-delete nil property)
  12637. "deleted"
  12638. "was not present in the entry")))
  12639. (defun org-delete-property-globally (property)
  12640. "Remove PROPERTY globally, from all entries."
  12641. (interactive
  12642. (let* ((completion-ignore-case t)
  12643. (prop (org-icompleting-read
  12644. "Globally remove property: "
  12645. (mapcar 'list (org-buffer-property-keys)))))
  12646. (list prop)))
  12647. (save-excursion
  12648. (save-restriction
  12649. (widen)
  12650. (goto-char (point-min))
  12651. (let ((cnt 0))
  12652. (while (re-search-forward
  12653. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12654. nil t)
  12655. (setq cnt (1+ cnt))
  12656. (replace-match ""))
  12657. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12658. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12659. (defun org-compute-property-at-point ()
  12660. "Compute the property at point.
  12661. This looks for an enclosing column format, extracts the operator and
  12662. then applies it to the property in the column format's scope."
  12663. (interactive)
  12664. (unless (org-at-property-p)
  12665. (error "Not at a property"))
  12666. (let ((prop (org-match-string-no-properties 2)))
  12667. (org-columns-get-format-and-top-level)
  12668. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12669. (error "No operator defined for property %s" prop))
  12670. (org-columns-compute prop)))
  12671. (defvar org-property-allowed-value-functions nil
  12672. "Hook for functions supplying allowed values for a specific property.
  12673. The functions must take a single argument, the name of the property, and
  12674. return a flat list of allowed values. If \":ETC\" is one of
  12675. the values, this means that these values are intended as defaults for
  12676. completion, but that other values should be allowed too.
  12677. The functions must return nil if they are not responsible for this
  12678. property.")
  12679. (defun org-property-get-allowed-values (pom property &optional table)
  12680. "Get allowed values for the property PROPERTY.
  12681. When TABLE is non-nil, return an alist that can directly be used for
  12682. completion."
  12683. (let (vals)
  12684. (cond
  12685. ((equal property "TODO")
  12686. (setq vals (org-with-point-at pom
  12687. (append org-todo-keywords-1 '("")))))
  12688. ((equal property "PRIORITY")
  12689. (let ((n org-lowest-priority))
  12690. (while (>= n org-highest-priority)
  12691. (push (char-to-string n) vals)
  12692. (setq n (1- n)))))
  12693. ((member property org-special-properties))
  12694. ((setq vals (run-hook-with-args-until-success
  12695. 'org-property-allowed-value-functions property)))
  12696. (t
  12697. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12698. (when (and vals (string-match "\\S-" vals))
  12699. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12700. (setq vals (mapcar (lambda (x)
  12701. (cond ((stringp x) x)
  12702. ((numberp x) (number-to-string x))
  12703. ((symbolp x) (symbol-name x))
  12704. (t "???")))
  12705. vals)))))
  12706. (when (member ":ETC" vals)
  12707. (setq vals (remove ":ETC" vals))
  12708. (org-add-props (car vals) '(org-unrestricted t)))
  12709. (if table (mapcar 'list vals) vals)))
  12710. (defun org-property-previous-allowed-value (&optional previous)
  12711. "Switch to the next allowed value for this property."
  12712. (interactive)
  12713. (org-property-next-allowed-value t))
  12714. (defun org-property-next-allowed-value (&optional previous)
  12715. "Switch to the next allowed value for this property."
  12716. (interactive)
  12717. (unless (org-at-property-p)
  12718. (error "Not at a property"))
  12719. (let* ((key (match-string 2))
  12720. (value (match-string 3))
  12721. (allowed (or (org-property-get-allowed-values (point) key)
  12722. (and (member value '("[ ]" "[-]" "[X]"))
  12723. '("[ ]" "[X]"))))
  12724. nval)
  12725. (unless allowed
  12726. (error "Allowed values for this property have not been defined"))
  12727. (if previous (setq allowed (reverse allowed)))
  12728. (if (member value allowed)
  12729. (setq nval (car (cdr (member value allowed)))))
  12730. (setq nval (or nval (car allowed)))
  12731. (if (equal nval value)
  12732. (error "Only one allowed value for this property"))
  12733. (org-at-property-p)
  12734. (replace-match (concat " :" key ": " nval) t t)
  12735. (org-indent-line-function)
  12736. (beginning-of-line 1)
  12737. (skip-chars-forward " \t")
  12738. (run-hook-with-args 'org-property-changed-functions key nval)))
  12739. (defun org-find-olp (path &optional this-buffer)
  12740. "Return a marker pointing to the entry at outline path OLP.
  12741. If anything goes wrong, throw an error.
  12742. You can wrap this call to catch the error like this:
  12743. (condition-case msg
  12744. (org-mobile-locate-entry (match-string 4))
  12745. (error (nth 1 msg)))
  12746. The return value will then be either a string with the error message,
  12747. or a marker if everything is OK.
  12748. If THIS-BUFFER is set, the outline path does not contain a file,
  12749. only headings."
  12750. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12751. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12752. (level 1)
  12753. (lmin 1)
  12754. (lmax 1)
  12755. limit re end found pos heading cnt)
  12756. (unless buffer (error "File not found :%s" file))
  12757. (with-current-buffer buffer
  12758. (save-excursion
  12759. (save-restriction
  12760. (widen)
  12761. (setq limit (point-max))
  12762. (goto-char (point-min))
  12763. (while (setq heading (pop path))
  12764. (setq re (format org-complex-heading-regexp-format
  12765. (regexp-quote heading)))
  12766. (setq cnt 0 pos (point))
  12767. (while (re-search-forward re end t)
  12768. (setq level (- (match-end 1) (match-beginning 1)))
  12769. (if (and (>= level lmin) (<= level lmax))
  12770. (setq found (match-beginning 0) cnt (1+ cnt))))
  12771. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12772. lmax heading))
  12773. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12774. lmax heading))
  12775. (goto-char found)
  12776. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12777. (setq end (save-excursion (org-end-of-subtree t t))))
  12778. (when (org-on-heading-p)
  12779. (move-marker (make-marker) (point))))))))
  12780. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  12781. "Find node HEADING in BUFFER.
  12782. Return a marker to the heading if it was found, or nil if not.
  12783. If POS-ONLY is set, return just the position instead of a marker.
  12784. The heading text must match exact, but it may have a TODO keyword,
  12785. a priority cookie and tags in the standard locations."
  12786. (with-current-buffer (or buffer (current-buffer))
  12787. (save-excursion
  12788. (save-restriction
  12789. (widen)
  12790. (goto-char (point-min))
  12791. (let (case-fold-search)
  12792. (if (re-search-forward
  12793. (format org-complex-heading-regexp-format
  12794. (regexp-quote heading)) nil t)
  12795. (if pos-only
  12796. (match-beginning 0)
  12797. (move-marker (make-marker) (match-beginning 0)))))))))
  12798. (defun org-find-exact-heading-in-directory (heading &optional dir)
  12799. "Find Org node headline HEADING in all .org files in directory DIR.
  12800. When the target headline is found, return a marker to this location."
  12801. (let ((files (directory-files (or dir default-directory)
  12802. nil "\\`[^.#].*\\.org\\'"))
  12803. file visiting m buffer)
  12804. (catch 'found
  12805. (while (setq file (pop files))
  12806. (message "trying %s" file)
  12807. (setq visiting (org-find-base-buffer-visiting file))
  12808. (setq buffer (or visiting (find-file-noselect file)))
  12809. (setq m (org-find-exact-headline-in-buffer
  12810. heading buffer))
  12811. (when (and (not m) (not visiting)) (kill-buffer buffer))
  12812. (and m (throw 'found m))))))
  12813. (defun org-find-entry-with-id (ident)
  12814. "Locate the entry that contains the ID property with exact value IDENT.
  12815. IDENT can be a string, a symbol or a number, this function will search for
  12816. the string representation of it.
  12817. Return the position where this entry starts, or nil if there is no such entry."
  12818. (interactive "sID: ")
  12819. (let ((id (cond
  12820. ((stringp ident) ident)
  12821. ((symbol-name ident) (symbol-name ident))
  12822. ((numberp ident) (number-to-string ident))
  12823. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12824. (case-fold-search nil))
  12825. (save-excursion
  12826. (save-restriction
  12827. (widen)
  12828. (goto-char (point-min))
  12829. (when (re-search-forward
  12830. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12831. nil t)
  12832. (org-back-to-heading t)
  12833. (point))))))
  12834. ;;;; Timestamps
  12835. (defvar org-last-changed-timestamp nil)
  12836. (defvar org-last-inserted-timestamp nil
  12837. "The last time stamp inserted with `org-insert-time-stamp'.")
  12838. (defvar org-time-was-given) ; dynamically scoped parameter
  12839. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12840. (defvar org-ts-what) ; dynamically scoped parameter
  12841. (defun org-time-stamp (arg &optional inactive)
  12842. "Prompt for a date/time and insert a time stamp.
  12843. If the user specifies a time like HH:MM, or if this command is called
  12844. with a prefix argument, the time stamp will contain date and time.
  12845. Otherwise, only the date will be included. All parts of a date not
  12846. specified by the user will be filled in from the current date/time.
  12847. So if you press just return without typing anything, the time stamp
  12848. will represent the current date/time. If there is already a timestamp
  12849. at the cursor, it will be modified."
  12850. (interactive "P")
  12851. (let* ((ts nil)
  12852. (default-time
  12853. ;; Default time is either today, or, when entering a range,
  12854. ;; the range start.
  12855. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12856. (save-excursion
  12857. (re-search-backward
  12858. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12859. (- (point) 20) t)))
  12860. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12861. (current-time)))
  12862. (default-input (and ts (org-get-compact-tod ts)))
  12863. org-time-was-given org-end-time-was-given time)
  12864. (cond
  12865. ((and (org-at-timestamp-p t)
  12866. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12867. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12868. (insert "--")
  12869. (setq time (let ((this-command this-command))
  12870. (org-read-date arg 'totime nil nil
  12871. default-time default-input)))
  12872. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12873. ((org-at-timestamp-p t)
  12874. (setq time (let ((this-command this-command))
  12875. (org-read-date arg 'totime nil nil default-time default-input)))
  12876. (when (org-at-timestamp-p t) ; just to get the match data
  12877. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12878. (replace-match "")
  12879. (setq org-last-changed-timestamp
  12880. (org-insert-time-stamp
  12881. time (or org-time-was-given arg)
  12882. inactive nil nil (list org-end-time-was-given))))
  12883. (message "Timestamp updated"))
  12884. (t
  12885. (setq time (let ((this-command this-command))
  12886. (org-read-date arg 'totime nil nil default-time default-input)))
  12887. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12888. nil nil (list org-end-time-was-given))))))
  12889. ;; FIXME: can we use this for something else, like computing time differences?
  12890. (defun org-get-compact-tod (s)
  12891. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12892. (let* ((t1 (match-string 1 s))
  12893. (h1 (string-to-number (match-string 2 s)))
  12894. (m1 (string-to-number (match-string 3 s)))
  12895. (t2 (and (match-end 4) (match-string 5 s)))
  12896. (h2 (and t2 (string-to-number (match-string 6 s))))
  12897. (m2 (and t2 (string-to-number (match-string 7 s))))
  12898. dh dm)
  12899. (if (not t2)
  12900. t1
  12901. (setq dh (- h2 h1) dm (- m2 m1))
  12902. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12903. (concat t1 "+" (number-to-string dh)
  12904. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12905. (defun org-time-stamp-inactive (&optional arg)
  12906. "Insert an inactive time stamp.
  12907. An inactive time stamp is enclosed in square brackets instead of angle
  12908. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12909. does not link to the calendar and cannot be changed with the S-cursor keys.
  12910. So these are more for recording a certain time/date."
  12911. (interactive "P")
  12912. (org-time-stamp arg 'inactive))
  12913. (defvar org-date-ovl (make-overlay 1 1))
  12914. (overlay-put org-date-ovl 'face 'org-warning)
  12915. (org-detach-overlay org-date-ovl)
  12916. (defvar org-ans1) ; dynamically scoped parameter
  12917. (defvar org-ans2) ; dynamically scoped parameter
  12918. (defvar org-plain-time-of-day-regexp) ; defined below
  12919. (defvar org-overriding-default-time nil) ; dynamically scoped
  12920. (defvar org-read-date-overlay nil)
  12921. (defvar org-dcst nil) ; dynamically scoped
  12922. (defvar org-read-date-history nil)
  12923. (defvar org-read-date-final-answer nil)
  12924. (defun org-read-date (&optional with-time to-time from-string prompt
  12925. default-time default-input)
  12926. "Read a date, possibly a time, and make things smooth for the user.
  12927. The prompt will suggest to enter an ISO date, but you can also enter anything
  12928. which will at least partially be understood by `parse-time-string'.
  12929. Unrecognized parts of the date will default to the current day, month, year,
  12930. hour and minute. If this command is called to replace a timestamp at point,
  12931. of to enter the second timestamp of a range, the default time is taken
  12932. from the existing stamp. Furthermore, the command prefers the future,
  12933. so if you are giving a date where the year is not given, and the day-month
  12934. combination is already past in the current year, it will assume you
  12935. mean next year. For details, see the manual. A few examples:
  12936. 3-2-5 --> 2003-02-05
  12937. feb 15 --> currentyear-02-15
  12938. 2/15 --> currentyear-02-15
  12939. sep 12 9 --> 2009-09-12
  12940. 12:45 --> today 12:45
  12941. 22 sept 0:34 --> currentyear-09-22 0:34
  12942. 12 --> currentyear-currentmonth-12
  12943. Fri --> nearest Friday (today or later)
  12944. etc.
  12945. Furthermore you can specify a relative date by giving, as the *first* thing
  12946. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12947. change in days weeks, months, years.
  12948. With a single plus or minus, the date is relative to today. With a double
  12949. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12950. +4d --> four days from today
  12951. +4 --> same as above
  12952. +2w --> two weeks from today
  12953. ++5 --> five days from default date
  12954. The function understands only English month and weekday abbreviations,
  12955. but this can be configured with the variables `parse-time-months' and
  12956. `parse-time-weekdays'.
  12957. While prompting, a calendar is popped up - you can also select the
  12958. date with the mouse (button 1). The calendar shows a period of three
  12959. months. To scroll it to other months, use the keys `>' and `<'.
  12960. If you don't like the calendar, turn it off with
  12961. \(setq org-read-date-popup-calendar nil)
  12962. With optional argument TO-TIME, the date will immediately be converted
  12963. to an internal time.
  12964. With an optional argument WITH-TIME, the prompt will suggest to also
  12965. insert a time. Note that when WITH-TIME is not set, you can still
  12966. enter a time, and this function will inform the calling routine about
  12967. this change. The calling routine may then choose to change the format
  12968. used to insert the time stamp into the buffer to include the time.
  12969. With optional argument FROM-STRING, read from this string instead from
  12970. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12971. the time/date that is used for everything that is not specified by the
  12972. user."
  12973. (require 'parse-time)
  12974. (let* ((org-time-stamp-rounding-minutes
  12975. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12976. (org-dcst org-display-custom-times)
  12977. (ct (org-current-time))
  12978. (def (or org-overriding-default-time default-time ct))
  12979. (defdecode (decode-time def))
  12980. (dummy (progn
  12981. (when (< (nth 2 defdecode) org-extend-today-until)
  12982. (setcar (nthcdr 2 defdecode) -1)
  12983. (setcar (nthcdr 1 defdecode) 59)
  12984. (setq def (apply 'encode-time defdecode)
  12985. defdecode (decode-time def)))))
  12986. (calendar-frame-setup nil)
  12987. (calendar-setup nil)
  12988. (calendar-move-hook nil)
  12989. (calendar-view-diary-initially-flag nil)
  12990. (calendar-view-holidays-initially-flag nil)
  12991. (timestr (format-time-string
  12992. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12993. (prompt (concat (if prompt (concat prompt " ") "")
  12994. (format "Date+time [%s]: " timestr)))
  12995. ans (org-ans0 "") org-ans1 org-ans2 final)
  12996. (cond
  12997. (from-string (setq ans from-string))
  12998. (org-read-date-popup-calendar
  12999. (save-excursion
  13000. (save-window-excursion
  13001. (calendar)
  13002. (calendar-forward-day (- (time-to-days def)
  13003. (calendar-absolute-from-gregorian
  13004. (calendar-current-date))))
  13005. (org-eval-in-calendar nil t)
  13006. (let* ((old-map (current-local-map))
  13007. (map (copy-keymap calendar-mode-map))
  13008. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13009. (org-defkey map (kbd "RET") 'org-calendar-select)
  13010. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13011. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13012. (org-defkey minibuffer-local-map [(meta shift left)]
  13013. (lambda () (interactive)
  13014. (org-eval-in-calendar '(calendar-backward-month 1))))
  13015. (org-defkey minibuffer-local-map [(meta shift right)]
  13016. (lambda () (interactive)
  13017. (org-eval-in-calendar '(calendar-forward-month 1))))
  13018. (org-defkey minibuffer-local-map [(meta shift up)]
  13019. (lambda () (interactive)
  13020. (org-eval-in-calendar '(calendar-backward-year 1))))
  13021. (org-defkey minibuffer-local-map [(meta shift down)]
  13022. (lambda () (interactive)
  13023. (org-eval-in-calendar '(calendar-forward-year 1))))
  13024. (org-defkey minibuffer-local-map [?\e (shift left)]
  13025. (lambda () (interactive)
  13026. (org-eval-in-calendar '(calendar-backward-month 1))))
  13027. (org-defkey minibuffer-local-map [?\e (shift right)]
  13028. (lambda () (interactive)
  13029. (org-eval-in-calendar '(calendar-forward-month 1))))
  13030. (org-defkey minibuffer-local-map [?\e (shift up)]
  13031. (lambda () (interactive)
  13032. (org-eval-in-calendar '(calendar-backward-year 1))))
  13033. (org-defkey minibuffer-local-map [?\e (shift down)]
  13034. (lambda () (interactive)
  13035. (org-eval-in-calendar '(calendar-forward-year 1))))
  13036. (org-defkey minibuffer-local-map [(shift up)]
  13037. (lambda () (interactive)
  13038. (org-eval-in-calendar '(calendar-backward-week 1))))
  13039. (org-defkey minibuffer-local-map [(shift down)]
  13040. (lambda () (interactive)
  13041. (org-eval-in-calendar '(calendar-forward-week 1))))
  13042. (org-defkey minibuffer-local-map [(shift left)]
  13043. (lambda () (interactive)
  13044. (org-eval-in-calendar '(calendar-backward-day 1))))
  13045. (org-defkey minibuffer-local-map [(shift right)]
  13046. (lambda () (interactive)
  13047. (org-eval-in-calendar '(calendar-forward-day 1))))
  13048. (org-defkey minibuffer-local-map ">"
  13049. (lambda () (interactive)
  13050. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13051. (org-defkey minibuffer-local-map "<"
  13052. (lambda () (interactive)
  13053. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13054. (org-defkey minibuffer-local-map "\C-v"
  13055. (lambda () (interactive)
  13056. (org-eval-in-calendar
  13057. '(calendar-scroll-left-three-months 1))))
  13058. (org-defkey minibuffer-local-map "\M-v"
  13059. (lambda () (interactive)
  13060. (org-eval-in-calendar
  13061. '(calendar-scroll-right-three-months 1))))
  13062. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13063. (unwind-protect
  13064. (progn
  13065. (use-local-map map)
  13066. (add-hook 'post-command-hook 'org-read-date-display)
  13067. (setq org-ans0 (read-string prompt default-input
  13068. 'org-read-date-history nil))
  13069. ;; org-ans0: from prompt
  13070. ;; org-ans1: from mouse click
  13071. ;; org-ans2: from calendar motion
  13072. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13073. (remove-hook 'post-command-hook 'org-read-date-display)
  13074. (use-local-map old-map)
  13075. (when org-read-date-overlay
  13076. (delete-overlay org-read-date-overlay)
  13077. (setq org-read-date-overlay nil)))))))
  13078. (t ; Naked prompt only
  13079. (unwind-protect
  13080. (setq ans (read-string prompt default-input
  13081. 'org-read-date-history timestr))
  13082. (when org-read-date-overlay
  13083. (delete-overlay org-read-date-overlay)
  13084. (setq org-read-date-overlay nil)))))
  13085. (setq final (org-read-date-analyze ans def defdecode))
  13086. ;; One round trip to get rid of 34th of August and stuff like that....
  13087. (setq final (decode-time (apply 'encode-time final)))
  13088. (setq org-read-date-final-answer ans)
  13089. (if to-time
  13090. (apply 'encode-time final)
  13091. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13092. (format "%04d-%02d-%02d %02d:%02d"
  13093. (nth 5 final) (nth 4 final) (nth 3 final)
  13094. (nth 2 final) (nth 1 final))
  13095. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13096. (defvar def)
  13097. (defvar defdecode)
  13098. (defvar with-time)
  13099. (defvar org-read-date-analyze-futurep nil)
  13100. (defun org-read-date-display ()
  13101. "Display the current date prompt interpretation in the minibuffer."
  13102. (when org-read-date-display-live
  13103. (when org-read-date-overlay
  13104. (delete-overlay org-read-date-overlay))
  13105. (let ((p (point)))
  13106. (end-of-line 1)
  13107. (while (not (equal (buffer-substring
  13108. (max (point-min) (- (point) 4)) (point))
  13109. " "))
  13110. (insert " "))
  13111. (goto-char p))
  13112. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13113. " " (or org-ans1 org-ans2)))
  13114. (org-end-time-was-given nil)
  13115. (f (org-read-date-analyze ans def defdecode))
  13116. (fmts (if org-dcst
  13117. org-time-stamp-custom-formats
  13118. org-time-stamp-formats))
  13119. (fmt (if (or with-time
  13120. (and (boundp 'org-time-was-given) org-time-was-given))
  13121. (cdr fmts)
  13122. (car fmts)))
  13123. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13124. (when (and org-end-time-was-given
  13125. (string-match org-plain-time-of-day-regexp txt))
  13126. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13127. org-end-time-was-given
  13128. (substring txt (match-end 0)))))
  13129. (when org-read-date-analyze-futurep
  13130. (setq txt (concat txt " (=>F)")))
  13131. (setq org-read-date-overlay
  13132. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13133. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13134. (defun org-read-date-analyze (ans def defdecode)
  13135. "Analyze the combined answer of the date prompt."
  13136. ;; FIXME: cleanup and comment
  13137. (let ((nowdecode (decode-time (current-time)))
  13138. delta deltan deltaw deltadef year month day
  13139. hour minute second wday pm h2 m2 tl wday1
  13140. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13141. (setq org-read-date-analyze-futurep nil)
  13142. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13143. (setq ans "+0"))
  13144. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13145. (setq ans (replace-match "" t t ans)
  13146. deltan (car delta)
  13147. deltaw (nth 1 delta)
  13148. deltadef (nth 2 delta)))
  13149. ;; Check if there is an iso week date in there
  13150. ;; If yes, store the info and postpone interpreting it until the rest
  13151. ;; of the parsing is done
  13152. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13153. (setq iso-year (if (match-end 1)
  13154. (org-small-year-to-year
  13155. (string-to-number (match-string 1 ans))))
  13156. iso-weekday (if (match-end 3)
  13157. (string-to-number (match-string 3 ans)))
  13158. iso-week (string-to-number (match-string 2 ans)))
  13159. (setq ans (replace-match "" t t ans)))
  13160. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13161. (when (string-match
  13162. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13163. (setq year (if (match-end 2)
  13164. (string-to-number (match-string 2 ans))
  13165. (progn (setq kill-year t)
  13166. (string-to-number (format-time-string "%Y"))))
  13167. month (string-to-number (match-string 3 ans))
  13168. day (string-to-number (match-string 4 ans)))
  13169. (if (< year 100) (setq year (+ 2000 year)))
  13170. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13171. t nil ans)))
  13172. ;; Help matching american dates, like 5/30 or 5/30/7
  13173. (when (string-match
  13174. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13175. (setq year (if (match-end 4)
  13176. (string-to-number (match-string 4 ans))
  13177. (progn (setq kill-year t)
  13178. (string-to-number (format-time-string "%Y"))))
  13179. month (string-to-number (match-string 1 ans))
  13180. day (string-to-number (match-string 2 ans)))
  13181. (if (< year 100) (setq year (+ 2000 year)))
  13182. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13183. t nil ans)))
  13184. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13185. ;; If there is a time with am/pm, and *no* time without it, we convert
  13186. ;; so that matching will be successful.
  13187. (loop for i from 1 to 2 do ; twice, for end time as well
  13188. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13189. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13190. (setq hour (string-to-number (match-string 1 ans))
  13191. minute (if (match-end 3)
  13192. (string-to-number (match-string 3 ans))
  13193. 0)
  13194. pm (equal ?p
  13195. (string-to-char (downcase (match-string 4 ans)))))
  13196. (if (and (= hour 12) (not pm))
  13197. (setq hour 0)
  13198. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13199. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13200. t t ans))))
  13201. ;; Check if a time range is given as a duration
  13202. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13203. (setq hour (string-to-number (match-string 1 ans))
  13204. h2 (+ hour (string-to-number (match-string 3 ans)))
  13205. minute (string-to-number (match-string 2 ans))
  13206. m2 (+ minute (if (match-end 5) (string-to-number
  13207. (match-string 5 ans))0)))
  13208. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13209. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13210. t t ans)))
  13211. ;; Check if there is a time range
  13212. (when (boundp 'org-end-time-was-given)
  13213. (setq org-time-was-given nil)
  13214. (when (and (string-match org-plain-time-of-day-regexp ans)
  13215. (match-end 8))
  13216. (setq org-end-time-was-given (match-string 8 ans))
  13217. (setq ans (concat (substring ans 0 (match-beginning 7))
  13218. (substring ans (match-end 7))))))
  13219. (setq tl (parse-time-string ans)
  13220. day (or (nth 3 tl) (nth 3 defdecode))
  13221. month (or (nth 4 tl)
  13222. (if (and org-read-date-prefer-future
  13223. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13224. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13225. (nth 4 defdecode)))
  13226. year (or (and (not kill-year) (nth 5 tl))
  13227. (if (and org-read-date-prefer-future
  13228. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13229. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13230. (nth 5 defdecode)))
  13231. hour (or (nth 2 tl) (nth 2 defdecode))
  13232. minute (or (nth 1 tl) (nth 1 defdecode))
  13233. second (or (nth 0 tl) 0)
  13234. wday (nth 6 tl))
  13235. (when (and (eq org-read-date-prefer-future 'time)
  13236. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13237. (equal day (nth 3 nowdecode))
  13238. (equal month (nth 4 nowdecode))
  13239. (equal year (nth 5 nowdecode))
  13240. (nth 2 tl)
  13241. (or (< (nth 2 tl) (nth 2 nowdecode))
  13242. (and (= (nth 2 tl) (nth 2 nowdecode))
  13243. (nth 1 tl)
  13244. (< (nth 1 tl) (nth 1 nowdecode)))))
  13245. (setq day (1+ day)
  13246. futurep t))
  13247. ;; Special date definitions below
  13248. (cond
  13249. (iso-week
  13250. ;; There was an iso week
  13251. (require 'cal-iso)
  13252. (setq futurep nil)
  13253. (setq year (or iso-year year)
  13254. day (or iso-weekday wday 1)
  13255. wday nil ; to make sure that the trigger below does not match
  13256. iso-date (calendar-gregorian-from-absolute
  13257. (calendar-absolute-from-iso
  13258. (list iso-week day year))))
  13259. ; FIXME: Should we also push ISO weeks into the future?
  13260. ; (when (and org-read-date-prefer-future
  13261. ; (not iso-year)
  13262. ; (< (calendar-absolute-from-gregorian iso-date)
  13263. ; (time-to-days (current-time))))
  13264. ; (setq year (1+ year)
  13265. ; iso-date (calendar-gregorian-from-absolute
  13266. ; (calendar-absolute-from-iso
  13267. ; (list iso-week day year)))))
  13268. (setq month (car iso-date)
  13269. year (nth 2 iso-date)
  13270. day (nth 1 iso-date)))
  13271. (deltan
  13272. (setq futurep nil)
  13273. (unless deltadef
  13274. (let ((now (decode-time (current-time))))
  13275. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13276. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13277. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13278. ((equal deltaw "m") (setq month (+ month deltan)))
  13279. ((equal deltaw "y") (setq year (+ year deltan)))))
  13280. ((and wday (not (nth 3 tl)))
  13281. (setq futurep nil)
  13282. ;; Weekday was given, but no day, so pick that day in the week
  13283. ;; on or after the derived date.
  13284. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13285. (unless (equal wday wday1)
  13286. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13287. (if (and (boundp 'org-time-was-given)
  13288. (nth 2 tl))
  13289. (setq org-time-was-given t))
  13290. (if (< year 100) (setq year (+ 2000 year)))
  13291. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13292. (setq org-read-date-analyze-futurep futurep)
  13293. (list second minute hour day month year)))
  13294. (defvar parse-time-weekdays)
  13295. (defun org-read-date-get-relative (s today default)
  13296. "Check string S for special relative date string.
  13297. TODAY and DEFAULT are internal times, for today and for a default.
  13298. Return shift list (N what def-flag)
  13299. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13300. N is the number of WHATs to shift.
  13301. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13302. the DEFAULT date rather than TODAY."
  13303. (when (and
  13304. (string-match
  13305. (concat
  13306. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13307. "\\([0-9]+\\)?"
  13308. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13309. "\\([ \t]\\|$\\)") s)
  13310. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13311. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13312. (string-to-char (substring (match-string 1 s) -1))
  13313. ?+))
  13314. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13315. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13316. (what (if (match-end 3) (match-string 3 s) "d"))
  13317. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13318. (date (if rel default today))
  13319. (wday (nth 6 (decode-time date)))
  13320. delta)
  13321. (if wday1
  13322. (progn
  13323. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13324. (if (= dir ?-) (setq delta (- delta 7)))
  13325. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13326. (list delta "d" rel))
  13327. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13328. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13329. "Turn a user-specified date into the internal representation.
  13330. The internal representation needed by the calendar is (month day year).
  13331. This is a wrapper to handle the brain-dead convention in calendar that
  13332. user function argument order change dependent on argument order."
  13333. (if (boundp 'calendar-date-style)
  13334. (cond
  13335. ((eq calendar-date-style 'american)
  13336. (list arg1 arg2 arg3))
  13337. ((eq calendar-date-style 'european)
  13338. (list arg2 arg1 arg3))
  13339. ((eq calendar-date-style 'iso)
  13340. (list arg2 arg3 arg1)))
  13341. (if (org-bound-and-true-p european-calendar-style)
  13342. (list arg2 arg1 arg3)
  13343. (list arg1 arg2 arg3))))
  13344. (defun org-eval-in-calendar (form &optional keepdate)
  13345. "Eval FORM in the calendar window and return to current window.
  13346. Also, store the cursor date in variable org-ans2."
  13347. (let ((sf (selected-frame))
  13348. (sw (selected-window)))
  13349. (select-window (get-buffer-window "*Calendar*" t))
  13350. (eval form)
  13351. (when (and (not keepdate) (calendar-cursor-to-date))
  13352. (let* ((date (calendar-cursor-to-date))
  13353. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13354. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13355. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13356. (select-window sw)
  13357. (org-select-frame-set-input-focus sf)))
  13358. (defun org-calendar-select ()
  13359. "Return to `org-read-date' with the date currently selected.
  13360. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13361. (interactive)
  13362. (when (calendar-cursor-to-date)
  13363. (let* ((date (calendar-cursor-to-date))
  13364. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13365. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13366. (if (active-minibuffer-window) (exit-minibuffer))))
  13367. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13368. "Insert a date stamp for the date given by the internal TIME.
  13369. WITH-HM means use the stamp format that includes the time of the day.
  13370. INACTIVE means use square brackets instead of angular ones, so that the
  13371. stamp will not contribute to the agenda.
  13372. PRE and POST are optional strings to be inserted before and after the
  13373. stamp.
  13374. The command returns the inserted time stamp."
  13375. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13376. stamp)
  13377. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13378. (insert-before-markers (or pre ""))
  13379. (when (listp extra)
  13380. (setq extra (car extra))
  13381. (if (and (stringp extra)
  13382. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13383. (setq extra (format "-%02d:%02d"
  13384. (string-to-number (match-string 1 extra))
  13385. (string-to-number (match-string 2 extra))))
  13386. (setq extra nil)))
  13387. (when extra
  13388. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13389. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13390. (insert-before-markers (or post ""))
  13391. (setq org-last-inserted-timestamp stamp)))
  13392. (defun org-toggle-time-stamp-overlays ()
  13393. "Toggle the use of custom time stamp formats."
  13394. (interactive)
  13395. (setq org-display-custom-times (not org-display-custom-times))
  13396. (unless org-display-custom-times
  13397. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13398. (while (setq p (next-single-property-change p 'display))
  13399. (if (and (get-text-property p 'display)
  13400. (eq (get-text-property p 'face) 'org-date))
  13401. (remove-text-properties
  13402. p (setq p (next-single-property-change p 'display))
  13403. '(display t))))
  13404. (set-buffer-modified-p bmp)))
  13405. (if (featurep 'xemacs)
  13406. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13407. (org-restart-font-lock)
  13408. (setq org-table-may-need-update t)
  13409. (if org-display-custom-times
  13410. (message "Time stamps are overlayed with custom format")
  13411. (message "Time stamp overlays removed")))
  13412. (defun org-display-custom-time (beg end)
  13413. "Overlay modified time stamp format over timestamp between BEG and END."
  13414. (let* ((ts (buffer-substring beg end))
  13415. t1 w1 with-hm tf time str w2 (off 0))
  13416. (save-match-data
  13417. (setq t1 (org-parse-time-string ts t))
  13418. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13419. (setq off (- (match-end 0) (match-beginning 0)))))
  13420. (setq end (- end off))
  13421. (setq w1 (- end beg)
  13422. with-hm (and (nth 1 t1) (nth 2 t1))
  13423. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13424. time (org-fix-decoded-time t1)
  13425. str (org-add-props
  13426. (format-time-string
  13427. (substring tf 1 -1) (apply 'encode-time time))
  13428. nil 'mouse-face 'highlight)
  13429. w2 (length str))
  13430. (if (not (= w2 w1))
  13431. (add-text-properties (1+ beg) (+ 2 beg)
  13432. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13433. (if (featurep 'xemacs)
  13434. (progn
  13435. (put-text-property beg end 'invisible t)
  13436. (put-text-property beg end 'end-glyph (make-glyph str)))
  13437. (put-text-property beg end 'display str))))
  13438. (defun org-translate-time (string)
  13439. "Translate all timestamps in STRING to custom format.
  13440. But do this only if the variable `org-display-custom-times' is set."
  13441. (when org-display-custom-times
  13442. (save-match-data
  13443. (let* ((start 0)
  13444. (re org-ts-regexp-both)
  13445. t1 with-hm inactive tf time str beg end)
  13446. (while (setq start (string-match re string start))
  13447. (setq beg (match-beginning 0)
  13448. end (match-end 0)
  13449. t1 (save-match-data
  13450. (org-parse-time-string (substring string beg end) t))
  13451. with-hm (and (nth 1 t1) (nth 2 t1))
  13452. inactive (equal (substring string beg (1+ beg)) "[")
  13453. tf (funcall (if with-hm 'cdr 'car)
  13454. org-time-stamp-custom-formats)
  13455. time (org-fix-decoded-time t1)
  13456. str (format-time-string
  13457. (concat
  13458. (if inactive "[" "<") (substring tf 1 -1)
  13459. (if inactive "]" ">"))
  13460. (apply 'encode-time time))
  13461. string (replace-match str t t string)
  13462. start (+ start (length str)))))))
  13463. string)
  13464. (defun org-fix-decoded-time (time)
  13465. "Set 0 instead of nil for the first 6 elements of time.
  13466. Don't touch the rest."
  13467. (let ((n 0))
  13468. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13469. (defun org-days-to-time (timestamp-string)
  13470. "Difference between TIMESTAMP-STRING and now in days."
  13471. (- (time-to-days (org-time-string-to-time timestamp-string))
  13472. (time-to-days (current-time))))
  13473. (defun org-deadline-close (timestamp-string &optional ndays)
  13474. "Is the time in TIMESTAMP-STRING close to the current date?"
  13475. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13476. (and (< (org-days-to-time timestamp-string) ndays)
  13477. (not (org-entry-is-done-p))))
  13478. (defun org-get-wdays (ts)
  13479. "Get the deadline lead time appropriate for timestring TS."
  13480. (cond
  13481. ((<= org-deadline-warning-days 0)
  13482. ;; 0 or negative, enforce this value no matter what
  13483. (- org-deadline-warning-days))
  13484. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13485. ;; lead time is specified.
  13486. (floor (* (string-to-number (match-string 1 ts))
  13487. (cdr (assoc (match-string 2 ts)
  13488. '(("d" . 1) ("w" . 7)
  13489. ("m" . 30.4) ("y" . 365.25)))))))
  13490. ;; go for the default.
  13491. (t org-deadline-warning-days)))
  13492. (defun org-calendar-select-mouse (ev)
  13493. "Return to `org-read-date' with the date currently selected.
  13494. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13495. (interactive "e")
  13496. (mouse-set-point ev)
  13497. (when (calendar-cursor-to-date)
  13498. (let* ((date (calendar-cursor-to-date))
  13499. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13500. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13501. (if (active-minibuffer-window) (exit-minibuffer))))
  13502. (defun org-check-deadlines (ndays)
  13503. "Check if there are any deadlines due or past due.
  13504. A deadline is considered due if it happens within `org-deadline-warning-days'
  13505. days from today's date. If the deadline appears in an entry marked DONE,
  13506. it is not shown. The prefix arg NDAYS can be used to test that many
  13507. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13508. (interactive "P")
  13509. (let* ((org-warn-days
  13510. (cond
  13511. ((equal ndays '(4)) 100000)
  13512. (ndays (prefix-numeric-value ndays))
  13513. (t (abs org-deadline-warning-days))))
  13514. (case-fold-search nil)
  13515. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13516. (callback
  13517. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13518. (message "%d deadlines past-due or due within %d days"
  13519. (org-occur regexp nil callback)
  13520. org-warn-days)))
  13521. (defun org-check-before-date (date)
  13522. "Check if there are deadlines or scheduled entries before DATE."
  13523. (interactive (list (org-read-date)))
  13524. (let ((case-fold-search nil)
  13525. (regexp (concat "\\<\\(" org-deadline-string
  13526. "\\|" org-scheduled-string
  13527. "\\) *<\\([^>]+\\)>"))
  13528. (callback
  13529. (lambda () (time-less-p
  13530. (org-time-string-to-time (match-string 2))
  13531. (org-time-string-to-time date)))))
  13532. (message "%d entries before %s"
  13533. (org-occur regexp nil callback) date)))
  13534. (defun org-check-after-date (date)
  13535. "Check if there are deadlines or scheduled entries after DATE."
  13536. (interactive (list (org-read-date)))
  13537. (let ((case-fold-search nil)
  13538. (regexp (concat "\\<\\(" org-deadline-string
  13539. "\\|" org-scheduled-string
  13540. "\\) *<\\([^>]+\\)>"))
  13541. (callback
  13542. (lambda () (not
  13543. (time-less-p
  13544. (org-time-string-to-time (match-string 2))
  13545. (org-time-string-to-time date))))))
  13546. (message "%d entries after %s"
  13547. (org-occur regexp nil callback) date)))
  13548. (defun org-evaluate-time-range (&optional to-buffer)
  13549. "Evaluate a time range by computing the difference between start and end.
  13550. Normally the result is just printed in the echo area, but with prefix arg
  13551. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13552. If the time range is actually in a table, the result is inserted into the
  13553. next column.
  13554. For time difference computation, a year is assumed to be exactly 365
  13555. days in order to avoid rounding problems."
  13556. (interactive "P")
  13557. (or
  13558. (org-clock-update-time-maybe)
  13559. (save-excursion
  13560. (unless (org-at-date-range-p t)
  13561. (goto-char (point-at-bol))
  13562. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13563. (if (not (org-at-date-range-p t))
  13564. (error "Not at a time-stamp range, and none found in current line")))
  13565. (let* ((ts1 (match-string 1))
  13566. (ts2 (match-string 2))
  13567. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13568. (match-end (match-end 0))
  13569. (time1 (org-time-string-to-time ts1))
  13570. (time2 (org-time-string-to-time ts2))
  13571. (t1 (org-float-time time1))
  13572. (t2 (org-float-time time2))
  13573. (diff (abs (- t2 t1)))
  13574. (negative (< (- t2 t1) 0))
  13575. ;; (ys (floor (* 365 24 60 60)))
  13576. (ds (* 24 60 60))
  13577. (hs (* 60 60))
  13578. (fy "%dy %dd %02d:%02d")
  13579. (fy1 "%dy %dd")
  13580. (fd "%dd %02d:%02d")
  13581. (fd1 "%dd")
  13582. (fh "%02d:%02d")
  13583. y d h m align)
  13584. (if havetime
  13585. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13586. y 0
  13587. d (floor (/ diff ds)) diff (mod diff ds)
  13588. h (floor (/ diff hs)) diff (mod diff hs)
  13589. m (floor (/ diff 60)))
  13590. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13591. y 0
  13592. d (floor (+ (/ diff ds) 0.5))
  13593. h 0 m 0))
  13594. (if (not to-buffer)
  13595. (message "%s" (org-make-tdiff-string y d h m))
  13596. (if (org-at-table-p)
  13597. (progn
  13598. (goto-char match-end)
  13599. (setq align t)
  13600. (and (looking-at " *|") (goto-char (match-end 0))))
  13601. (goto-char match-end))
  13602. (if (looking-at
  13603. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13604. (replace-match ""))
  13605. (if negative (insert " -"))
  13606. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13607. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13608. (insert " " (format fh h m))))
  13609. (if align (org-table-align))
  13610. (message "Time difference inserted")))))
  13611. (defun org-make-tdiff-string (y d h m)
  13612. (let ((fmt "")
  13613. (l nil))
  13614. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13615. l (push y l)))
  13616. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13617. l (push d l)))
  13618. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13619. l (push h l)))
  13620. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13621. l (push m l)))
  13622. (apply 'format fmt (nreverse l))))
  13623. (defun org-time-string-to-time (s)
  13624. (apply 'encode-time (org-parse-time-string s)))
  13625. (defun org-time-string-to-seconds (s)
  13626. (org-float-time (org-time-string-to-time s)))
  13627. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13628. "Convert a time stamp to an absolute day number.
  13629. If there is a specifier for a cyclic time stamp, get the closest date to
  13630. DAYNR.
  13631. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13632. the variable date is bound by the calendar when this is called."
  13633. (cond
  13634. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13635. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13636. daynr
  13637. (+ daynr 1000)))
  13638. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13639. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13640. (time-to-days (current-time))) (match-string 0 s)
  13641. prefer show-all))
  13642. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13643. (defun org-days-to-iso-week (days)
  13644. "Return the iso week number."
  13645. (require 'cal-iso)
  13646. (car (calendar-iso-from-absolute days)))
  13647. (defun org-small-year-to-year (year)
  13648. "Convert 2-digit years into 4-digit years.
  13649. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13650. The year 2000 cannot be abbreviated. Any year larger than 99
  13651. is returned unchanged."
  13652. (if (< year 38)
  13653. (setq year (+ 2000 year))
  13654. (if (< year 100)
  13655. (setq year (+ 1900 year))))
  13656. year)
  13657. (defun org-time-from-absolute (d)
  13658. "Return the time corresponding to date D.
  13659. D may be an absolute day number, or a calendar-type list (month day year)."
  13660. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13661. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13662. (defun org-calendar-holiday ()
  13663. "List of holidays, for Diary display in Org-mode."
  13664. (require 'holidays)
  13665. (let ((hl (funcall
  13666. (if (fboundp 'calendar-check-holidays)
  13667. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13668. (if hl (mapconcat 'identity hl "; "))))
  13669. (defun org-diary-sexp-entry (sexp entry date)
  13670. "Process a SEXP diary ENTRY for DATE."
  13671. (require 'diary-lib)
  13672. (let ((result (if calendar-debug-sexp
  13673. (let ((stack-trace-on-error t))
  13674. (eval (car (read-from-string sexp))))
  13675. (condition-case nil
  13676. (eval (car (read-from-string sexp)))
  13677. (error
  13678. (beep)
  13679. (message "Bad sexp at line %d in %s: %s"
  13680. (org-current-line)
  13681. (buffer-file-name) sexp)
  13682. (sleep-for 2))))))
  13683. (cond ((stringp result) result)
  13684. ((and (consp result)
  13685. (stringp (cdr result))) (cdr result))
  13686. (result entry)
  13687. (t nil))))
  13688. (defun org-diary-to-ical-string (frombuf)
  13689. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13690. This uses the icalendar.el library."
  13691. (let* ((tmpdir (if (featurep 'xemacs)
  13692. (temp-directory)
  13693. temporary-file-directory))
  13694. (tmpfile (make-temp-name
  13695. (expand-file-name "orgics" tmpdir)))
  13696. buf rtn b e)
  13697. (with-current-buffer frombuf
  13698. (icalendar-export-region (point-min) (point-max) tmpfile)
  13699. (setq buf (find-buffer-visiting tmpfile))
  13700. (set-buffer buf)
  13701. (goto-char (point-min))
  13702. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13703. (setq b (match-beginning 0)))
  13704. (goto-char (point-max))
  13705. (if (re-search-backward "^END:VEVENT" nil t)
  13706. (setq e (match-end 0)))
  13707. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13708. (kill-buffer buf)
  13709. (delete-file tmpfile)
  13710. rtn))
  13711. (defun org-closest-date (start current change prefer show-all)
  13712. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13713. When PREFER is `past' return a date that is either CURRENT or past.
  13714. When PREFER is `future', return a date that is either CURRENT or future.
  13715. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13716. ;; Make the proper lists from the dates
  13717. (catch 'exit
  13718. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13719. dn dw sday cday n1 n2 n0
  13720. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13721. (setq start (org-date-to-gregorian start)
  13722. current (org-date-to-gregorian
  13723. (if show-all
  13724. current
  13725. (time-to-days (current-time))))
  13726. sday (calendar-absolute-from-gregorian start)
  13727. cday (calendar-absolute-from-gregorian current))
  13728. (if (<= cday sday) (throw 'exit sday))
  13729. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13730. (setq dn (string-to-number (match-string 1 change))
  13731. dw (cdr (assoc (match-string 2 change) a1)))
  13732. (error "Invalid change specifier: %s" change))
  13733. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13734. (cond
  13735. ((eq dw 'day)
  13736. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13737. n2 (+ n1 dn)))
  13738. ((eq dw 'year)
  13739. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13740. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13741. (setq date1 (list m d y1)
  13742. n1 (calendar-absolute-from-gregorian date1)
  13743. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13744. n2 (calendar-absolute-from-gregorian date2)))
  13745. ((eq dw 'month)
  13746. ;; approx number of month between the two dates
  13747. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13748. ;; How often does dn fit in there?
  13749. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13750. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13751. m (+ m nm)
  13752. ny (floor (/ m 12))
  13753. y (+ y ny)
  13754. m (- m (* ny 12)))
  13755. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13756. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13757. (setq m2 (+ m dn) y2 y)
  13758. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13759. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13760. (while (<= n2 cday)
  13761. (setq n1 n2 m m2 y y2)
  13762. (setq m2 (+ m dn) y2 y)
  13763. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13764. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13765. ;; Make sure n1 is the earlier date
  13766. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13767. (if show-all
  13768. (cond
  13769. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13770. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13771. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13772. (cond
  13773. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13774. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13775. (t (if (= cday n1) n1 n2)))))))
  13776. (defun org-date-to-gregorian (date)
  13777. "Turn any specification of DATE into a Gregorian date for the calendar."
  13778. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13779. ((and (listp date) (= (length date) 3)) date)
  13780. ((stringp date)
  13781. (setq date (org-parse-time-string date))
  13782. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13783. ((listp date)
  13784. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13785. (defun org-parse-time-string (s &optional nodefault)
  13786. "Parse the standard Org-mode time string.
  13787. This should be a lot faster than the normal `parse-time-string'.
  13788. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13789. hour and minute fields will be nil if not given."
  13790. (if (string-match org-ts-regexp0 s)
  13791. (list 0
  13792. (if (or (match-beginning 8) (not nodefault))
  13793. (string-to-number (or (match-string 8 s) "0")))
  13794. (if (or (match-beginning 7) (not nodefault))
  13795. (string-to-number (or (match-string 7 s) "0")))
  13796. (string-to-number (match-string 4 s))
  13797. (string-to-number (match-string 3 s))
  13798. (string-to-number (match-string 2 s))
  13799. nil nil nil)
  13800. (error "Not a standard Org-mode time string: %s" s)))
  13801. (defun org-timestamp-up (&optional arg)
  13802. "Increase the date item at the cursor by one.
  13803. If the cursor is on the year, change the year. If it is on the month or
  13804. the day, change that.
  13805. With prefix ARG, change by that many units."
  13806. (interactive "p")
  13807. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13808. (defun org-timestamp-down (&optional arg)
  13809. "Decrease the date item at the cursor by one.
  13810. If the cursor is on the year, change the year. If it is on the month or
  13811. the day, change that.
  13812. With prefix ARG, change by that many units."
  13813. (interactive "p")
  13814. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13815. (defun org-timestamp-up-day (&optional arg)
  13816. "Increase the date in the time stamp by one day.
  13817. With prefix ARG, change that many days."
  13818. (interactive "p")
  13819. (if (and (not (org-at-timestamp-p t))
  13820. (org-on-heading-p))
  13821. (org-todo 'up)
  13822. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13823. (defun org-timestamp-down-day (&optional arg)
  13824. "Decrease the date in the time stamp by one day.
  13825. With prefix ARG, change that many days."
  13826. (interactive "p")
  13827. (if (and (not (org-at-timestamp-p t))
  13828. (org-on-heading-p))
  13829. (org-todo 'down)
  13830. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13831. (defun org-at-timestamp-p (&optional inactive-ok)
  13832. "Determine if the cursor is in or at a timestamp."
  13833. (interactive)
  13834. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13835. (pos (point))
  13836. (ans (or (looking-at tsr)
  13837. (save-excursion
  13838. (skip-chars-backward "^[<\n\r\t")
  13839. (if (> (point) (point-min)) (backward-char 1))
  13840. (and (looking-at tsr)
  13841. (> (- (match-end 0) pos) -1))))))
  13842. (and ans
  13843. (boundp 'org-ts-what)
  13844. (setq org-ts-what
  13845. (cond
  13846. ((= pos (match-beginning 0)) 'bracket)
  13847. ((= pos (1- (match-end 0))) 'bracket)
  13848. ((org-pos-in-match-range pos 2) 'year)
  13849. ((org-pos-in-match-range pos 3) 'month)
  13850. ((org-pos-in-match-range pos 7) 'hour)
  13851. ((org-pos-in-match-range pos 8) 'minute)
  13852. ((or (org-pos-in-match-range pos 4)
  13853. (org-pos-in-match-range pos 5)) 'day)
  13854. ((and (> pos (or (match-end 8) (match-end 5)))
  13855. (< pos (match-end 0)))
  13856. (- pos (or (match-end 8) (match-end 5))))
  13857. (t 'day))))
  13858. ans))
  13859. (defun org-toggle-timestamp-type ()
  13860. "Toggle the type (<active> or [inactive]) of a time stamp."
  13861. (interactive)
  13862. (when (org-at-timestamp-p t)
  13863. (let ((beg (match-beginning 0)) (end (match-end 0))
  13864. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13865. (save-excursion
  13866. (goto-char beg)
  13867. (while (re-search-forward "[][<>]" end t)
  13868. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13869. t t)))
  13870. (message "Timestamp is now %sactive"
  13871. (if (equal (char-after beg) ?<) "" "in")))))
  13872. (defun org-timestamp-change (n &optional what updown)
  13873. "Change the date in the time stamp at point.
  13874. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13875. `year', `minute', `second'. If WHAT is not given, the cursor position
  13876. in the timestamp determines what will be changed."
  13877. (let ((pos (point))
  13878. with-hm inactive
  13879. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13880. org-ts-what
  13881. extra rem
  13882. ts time time0)
  13883. (if (not (org-at-timestamp-p t))
  13884. (error "Not at a timestamp"))
  13885. (if (and (not what) (eq org-ts-what 'bracket))
  13886. (org-toggle-timestamp-type)
  13887. (if (and (not what) (not (eq org-ts-what 'day))
  13888. org-display-custom-times
  13889. (get-text-property (point) 'display)
  13890. (not (get-text-property (1- (point)) 'display)))
  13891. (setq org-ts-what 'day))
  13892. (setq org-ts-what (or what org-ts-what)
  13893. inactive (= (char-after (match-beginning 0)) ?\[)
  13894. ts (match-string 0))
  13895. (replace-match "")
  13896. (if (string-match
  13897. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13898. ts)
  13899. (setq extra (match-string 1 ts)))
  13900. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13901. (setq with-hm t))
  13902. (setq time0 (org-parse-time-string ts))
  13903. (when (and updown
  13904. (eq org-ts-what 'minute)
  13905. (not current-prefix-arg))
  13906. ;; This looks like s-up and s-down. Change by one rounding step.
  13907. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13908. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13909. (setcar (cdr time0) (+ (nth 1 time0)
  13910. (if (> n 0) (- rem) (- dm rem))))))
  13911. (setq time
  13912. (encode-time (or (car time0) 0)
  13913. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13914. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13915. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13916. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13917. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13918. (nthcdr 6 time0)))
  13919. (when (and (member org-ts-what '(hour minute))
  13920. extra
  13921. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13922. (setq extra (org-modify-ts-extra
  13923. extra
  13924. (if (eq org-ts-what 'hour) 2 5)
  13925. n dm)))
  13926. (when (integerp org-ts-what)
  13927. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13928. (if (eq what 'calendar)
  13929. (let ((cal-date (org-get-date-from-calendar)))
  13930. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13931. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13932. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13933. (setcar time0 (or (car time0) 0))
  13934. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13935. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13936. (setq time (apply 'encode-time time0))))
  13937. (setq org-last-changed-timestamp
  13938. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13939. (org-clock-update-time-maybe)
  13940. (goto-char pos)
  13941. ;; Try to recenter the calendar window, if any
  13942. (if (and org-calendar-follow-timestamp-change
  13943. (get-buffer-window "*Calendar*" t)
  13944. (memq org-ts-what '(day month year)))
  13945. (org-recenter-calendar (time-to-days time))))))
  13946. (defun org-modify-ts-extra (s pos n dm)
  13947. "Change the different parts of the lead-time and repeat fields in timestamp."
  13948. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13949. ng h m new rem)
  13950. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13951. (cond
  13952. ((or (org-pos-in-match-range pos 2)
  13953. (org-pos-in-match-range pos 3))
  13954. (setq m (string-to-number (match-string 3 s))
  13955. h (string-to-number (match-string 2 s)))
  13956. (if (org-pos-in-match-range pos 2)
  13957. (setq h (+ h n))
  13958. (setq n (* dm (org-no-warnings (signum n))))
  13959. (when (not (= 0 (setq rem (% m dm))))
  13960. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13961. (setq m (+ m n)))
  13962. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13963. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13964. (setq h (min 24 (max 0 h)))
  13965. (setq ng 1 new (format "-%02d:%02d" h m)))
  13966. ((org-pos-in-match-range pos 6)
  13967. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13968. ((org-pos-in-match-range pos 5)
  13969. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13970. ((org-pos-in-match-range pos 9)
  13971. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13972. ((org-pos-in-match-range pos 8)
  13973. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13974. (when ng
  13975. (setq s (concat
  13976. (substring s 0 (match-beginning ng))
  13977. new
  13978. (substring s (match-end ng))))))
  13979. s))
  13980. (defun org-recenter-calendar (date)
  13981. "If the calendar is visible, recenter it to DATE."
  13982. (let* ((win (selected-window))
  13983. (cwin (get-buffer-window "*Calendar*" t))
  13984. (calendar-move-hook nil))
  13985. (when cwin
  13986. (select-window cwin)
  13987. (calendar-goto-date (if (listp date) date
  13988. (calendar-gregorian-from-absolute date)))
  13989. (select-window win))))
  13990. (defun org-goto-calendar (&optional arg)
  13991. "Go to the Emacs calendar at the current date.
  13992. If there is a time stamp in the current line, go to that date.
  13993. A prefix ARG can be used to force the current date."
  13994. (interactive "P")
  13995. (let ((tsr org-ts-regexp) diff
  13996. (calendar-move-hook nil)
  13997. (calendar-view-holidays-initially-flag nil)
  13998. (calendar-view-diary-initially-flag nil))
  13999. (if (or (org-at-timestamp-p)
  14000. (save-excursion
  14001. (beginning-of-line 1)
  14002. (looking-at (concat ".*" tsr))))
  14003. (let ((d1 (time-to-days (current-time)))
  14004. (d2 (time-to-days
  14005. (org-time-string-to-time (match-string 1)))))
  14006. (setq diff (- d2 d1))))
  14007. (calendar)
  14008. (calendar-goto-today)
  14009. (if (and diff (not arg)) (calendar-forward-day diff))))
  14010. (defun org-get-date-from-calendar ()
  14011. "Return a list (month day year) of date at point in calendar."
  14012. (with-current-buffer "*Calendar*"
  14013. (save-match-data
  14014. (calendar-cursor-to-date))))
  14015. (defun org-date-from-calendar ()
  14016. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14017. If there is already a time stamp at the cursor position, update it."
  14018. (interactive)
  14019. (if (org-at-timestamp-p t)
  14020. (org-timestamp-change 0 'calendar)
  14021. (let ((cal-date (org-get-date-from-calendar)))
  14022. (org-insert-time-stamp
  14023. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14024. (defun org-minutes-to-hh:mm-string (m)
  14025. "Compute H:MM from a number of minutes."
  14026. (let ((h (/ m 60)))
  14027. (setq m (- m (* 60 h)))
  14028. (format org-time-clocksum-format h m)))
  14029. (defun org-hh:mm-string-to-minutes (s)
  14030. "Convert a string H:MM to a number of minutes.
  14031. If the string is just a number, interpret it as minutes.
  14032. In fact, the first hh:mm or number in the string will be taken,
  14033. there can be extra stuff in the string.
  14034. If no number is found, the return value is 0."
  14035. (cond
  14036. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14037. (+ (* (string-to-number (match-string 1 s)) 60)
  14038. (string-to-number (match-string 2 s))))
  14039. ((string-match "\\([0-9]+\\)" s)
  14040. (string-to-number (match-string 1 s)))
  14041. (t 0)))
  14042. ;;;; Files
  14043. (defun org-save-all-org-buffers ()
  14044. "Save all Org-mode buffers without user confirmation."
  14045. (interactive)
  14046. (message "Saving all Org-mode buffers...")
  14047. (save-some-buffers t 'org-mode-p)
  14048. (when (featurep 'org-id) (org-id-locations-save))
  14049. (message "Saving all Org-mode buffers... done"))
  14050. (defun org-revert-all-org-buffers ()
  14051. "Revert all Org-mode buffers.
  14052. Prompt for confirmation when there are unsaved changes.
  14053. Be sure you know what you are doing before letting this function
  14054. overwrite your changes.
  14055. This function is useful in a setup where one tracks org files
  14056. with a version control system, to revert on one machine after pulling
  14057. changes from another. I believe the procedure must be like this:
  14058. 1. M-x org-save-all-org-buffers
  14059. 2. Pull changes from the other machine, resolve conflicts
  14060. 3. M-x org-revert-all-org-buffers"
  14061. (interactive)
  14062. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14063. (error "Abort"))
  14064. (save-excursion
  14065. (save-window-excursion
  14066. (mapc
  14067. (lambda (b)
  14068. (when (and (with-current-buffer b (org-mode-p))
  14069. (with-current-buffer b buffer-file-name))
  14070. (switch-to-buffer b)
  14071. (revert-buffer t 'no-confirm)))
  14072. (buffer-list))
  14073. (when (and (featurep 'org-id) org-id-track-globally)
  14074. (org-id-locations-load)))))
  14075. ;;;; Agenda files
  14076. ;;;###autoload
  14077. (defun org-switchb (&optional arg)
  14078. "Switch between Org buffers.
  14079. With a prefix argument, restrict available to files.
  14080. With two prefix arguments, restrict available buffers to agenda files.
  14081. Defaults to `iswitchb' for buffer name completion.
  14082. Set `org-completion-use-ido' to make it use ido instead."
  14083. (interactive "P")
  14084. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14085. ((equal arg '(16)) (org-buffer-list 'agenda))
  14086. (t (org-buffer-list))))
  14087. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14088. (org-completion-use-ido org-completion-use-ido))
  14089. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14090. (setq org-completion-use-iswitchb t))
  14091. (switch-to-buffer
  14092. (org-icompleting-read "Org buffer: "
  14093. (mapcar 'list (mapcar 'buffer-name blist))
  14094. nil t))))
  14095. ;;; Define some older names previously used for this functionality
  14096. ;;;###autoload
  14097. (defalias 'org-ido-switchb 'org-switchb)
  14098. ;;;###autoload
  14099. (defalias 'org-iswitchb 'org-switchb)
  14100. (defun org-buffer-list (&optional predicate exclude-tmp)
  14101. "Return a list of Org buffers.
  14102. PREDICATE can be `export', `files' or `agenda'.
  14103. export restrict the list to Export buffers.
  14104. files restrict the list to buffers visiting Org files.
  14105. agenda restrict the list to buffers visiting agenda files.
  14106. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14107. (let* ((bfn nil)
  14108. (agenda-files (and (eq predicate 'agenda)
  14109. (mapcar 'file-truename (org-agenda-files t))))
  14110. (filter
  14111. (cond
  14112. ((eq predicate 'files)
  14113. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14114. ((eq predicate 'export)
  14115. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14116. ((eq predicate 'agenda)
  14117. (lambda (b)
  14118. (with-current-buffer b
  14119. (and (eq major-mode 'org-mode)
  14120. (setq bfn (buffer-file-name b))
  14121. (member (file-truename bfn) agenda-files)))))
  14122. (t (lambda (b) (with-current-buffer b
  14123. (or (eq major-mode 'org-mode)
  14124. (string-match "\*Org .*Export"
  14125. (buffer-name b)))))))))
  14126. (delq nil
  14127. (mapcar
  14128. (lambda(b)
  14129. (if (and (funcall filter b)
  14130. (or (not exclude-tmp)
  14131. (not (string-match "tmp" (buffer-name b)))))
  14132. b
  14133. nil))
  14134. (buffer-list)))))
  14135. (defun org-agenda-files (&optional unrestricted archives)
  14136. "Get the list of agenda files.
  14137. Optional UNRESTRICTED means return the full list even if a restriction
  14138. is currently in place.
  14139. When ARCHIVES is t, include all archive files that are really being
  14140. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14141. `org-agenda-archives-mode' is t."
  14142. (let ((files
  14143. (cond
  14144. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14145. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14146. ((listp org-agenda-files) org-agenda-files)
  14147. (t (error "Invalid value of `org-agenda-files'")))))
  14148. (setq files (apply 'append
  14149. (mapcar (lambda (f)
  14150. (if (file-directory-p f)
  14151. (directory-files
  14152. f t org-agenda-file-regexp)
  14153. (list f)))
  14154. files)))
  14155. (when org-agenda-skip-unavailable-files
  14156. (setq files (delq nil
  14157. (mapcar (function
  14158. (lambda (file)
  14159. (and (file-readable-p file) file)))
  14160. files))))
  14161. (when (or (eq archives t)
  14162. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14163. (setq files (org-add-archive-files files)))
  14164. files))
  14165. (defun org-agenda-file-p (&optional file)
  14166. "Return non-nil, if FILE is an agenda file.
  14167. If FILE is omitted, use the file associated with the current
  14168. buffer."
  14169. (member (or file (buffer-file-name))
  14170. (org-agenda-files t)))
  14171. (defun org-edit-agenda-file-list ()
  14172. "Edit the list of agenda files.
  14173. Depending on setup, this either uses customize to edit the variable
  14174. `org-agenda-files', or it visits the file that is holding the list. In the
  14175. latter case, the buffer is set up in a way that saving it automatically kills
  14176. the buffer and restores the previous window configuration."
  14177. (interactive)
  14178. (if (stringp org-agenda-files)
  14179. (let ((cw (current-window-configuration)))
  14180. (find-file org-agenda-files)
  14181. (org-set-local 'org-window-configuration cw)
  14182. (org-add-hook 'after-save-hook
  14183. (lambda ()
  14184. (set-window-configuration
  14185. (prog1 org-window-configuration
  14186. (kill-buffer (current-buffer))))
  14187. (org-install-agenda-files-menu)
  14188. (message "New agenda file list installed"))
  14189. nil 'local)
  14190. (message "%s" (substitute-command-keys
  14191. "Edit list and finish with \\[save-buffer]")))
  14192. (customize-variable 'org-agenda-files)))
  14193. (defun org-store-new-agenda-file-list (list)
  14194. "Set new value for the agenda file list and save it correctly."
  14195. (if (stringp org-agenda-files)
  14196. (let ((fe (org-read-agenda-file-list t)) b u)
  14197. (while (setq b (find-buffer-visiting org-agenda-files))
  14198. (kill-buffer b))
  14199. (with-temp-file org-agenda-files
  14200. (insert
  14201. (mapconcat
  14202. (lambda (f) ;; Keep un-expanded entries.
  14203. (if (setq u (assoc f fe))
  14204. (cdr u)
  14205. f))
  14206. list "\n")
  14207. "\n")))
  14208. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14209. (org-insert-mode-line-in-empty-file nil))
  14210. (setq org-agenda-files list)
  14211. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14212. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14213. "Read the list of agenda files from a file.
  14214. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14215. filenames, used by `org-store-new-agenda-file-list' to write back
  14216. un-expanded file names."
  14217. (when (file-directory-p org-agenda-files)
  14218. (error "`org-agenda-files' cannot be a single directory"))
  14219. (when (stringp org-agenda-files)
  14220. (with-temp-buffer
  14221. (insert-file-contents org-agenda-files)
  14222. (mapcar
  14223. (lambda (f)
  14224. (let ((e (expand-file-name (substitute-in-file-name f)
  14225. org-directory)))
  14226. (if pair-with-expansion
  14227. (cons e f)
  14228. e)))
  14229. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14230. ;;;###autoload
  14231. (defun org-cycle-agenda-files ()
  14232. "Cycle through the files in `org-agenda-files'.
  14233. If the current buffer visits an agenda file, find the next one in the list.
  14234. If the current buffer does not, find the first agenda file."
  14235. (interactive)
  14236. (let* ((fs (org-agenda-files t))
  14237. (files (append fs (list (car fs))))
  14238. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14239. file)
  14240. (unless files (error "No agenda files"))
  14241. (catch 'exit
  14242. (while (setq file (pop files))
  14243. (if (equal (file-truename file) tcf)
  14244. (when (car files)
  14245. (find-file (car files))
  14246. (throw 'exit t))))
  14247. (find-file (car fs)))
  14248. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14249. (defun org-agenda-file-to-front (&optional to-end)
  14250. "Move/add the current file to the top of the agenda file list.
  14251. If the file is not present in the list, it is added to the front. If it is
  14252. present, it is moved there. With optional argument TO-END, add/move to the
  14253. end of the list."
  14254. (interactive "P")
  14255. (let ((org-agenda-skip-unavailable-files nil)
  14256. (file-alist (mapcar (lambda (x)
  14257. (cons (file-truename x) x))
  14258. (org-agenda-files t)))
  14259. (ctf (file-truename buffer-file-name))
  14260. x had)
  14261. (setq x (assoc ctf file-alist) had x)
  14262. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14263. (if to-end
  14264. (setq file-alist (append (delq x file-alist) (list x)))
  14265. (setq file-alist (cons x (delq x file-alist))))
  14266. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14267. (org-install-agenda-files-menu)
  14268. (message "File %s to %s of agenda file list"
  14269. (if had "moved" "added") (if to-end "end" "front"))))
  14270. (defun org-remove-file (&optional file)
  14271. "Remove current file from the list of files in variable `org-agenda-files'.
  14272. These are the files which are being checked for agenda entries.
  14273. Optional argument FILE means use this file instead of the current."
  14274. (interactive)
  14275. (let* ((org-agenda-skip-unavailable-files nil)
  14276. (file (or file buffer-file-name))
  14277. (true-file (file-truename file))
  14278. (afile (abbreviate-file-name file))
  14279. (files (delq nil (mapcar
  14280. (lambda (x)
  14281. (if (equal true-file
  14282. (file-truename x))
  14283. nil x))
  14284. (org-agenda-files t)))))
  14285. (if (not (= (length files) (length (org-agenda-files t))))
  14286. (progn
  14287. (org-store-new-agenda-file-list files)
  14288. (org-install-agenda-files-menu)
  14289. (message "Removed file: %s" afile))
  14290. (message "File was not in list: %s (not removed)" afile))))
  14291. (defun org-file-menu-entry (file)
  14292. (vector file (list 'find-file file) t))
  14293. (defun org-check-agenda-file (file)
  14294. "Make sure FILE exists. If not, ask user what to do."
  14295. (when (not (file-exists-p file))
  14296. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14297. (abbreviate-file-name file))
  14298. (let ((r (downcase (read-char-exclusive))))
  14299. (cond
  14300. ((equal r ?r)
  14301. (org-remove-file file)
  14302. (throw 'nextfile t))
  14303. (t (error "Abort"))))))
  14304. (defun org-get-agenda-file-buffer (file)
  14305. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14306. it to the list of buffers which might be released later."
  14307. (let ((buf (org-find-base-buffer-visiting file)))
  14308. (if buf
  14309. buf ; just return it
  14310. ;; Make a new buffer and remember it
  14311. (setq buf (find-file-noselect file))
  14312. (if buf (push buf org-agenda-new-buffers))
  14313. buf)))
  14314. (defun org-release-buffers (blist)
  14315. "Release all buffers in list, asking the user for confirmation when needed.
  14316. When a buffer is unmodified, it is just killed. When modified, it is saved
  14317. \(if the user agrees) and then killed."
  14318. (let (buf file)
  14319. (while (setq buf (pop blist))
  14320. (setq file (buffer-file-name buf))
  14321. (when (and (buffer-modified-p buf)
  14322. file
  14323. (y-or-n-p (format "Save file %s? " file)))
  14324. (with-current-buffer buf (save-buffer)))
  14325. (kill-buffer buf))))
  14326. (defun org-prepare-agenda-buffers (files)
  14327. "Create buffers for all agenda files, protect archived trees and comments."
  14328. (interactive)
  14329. (let ((pa '(:org-archived t))
  14330. (pc '(:org-comment t))
  14331. (pall '(:org-archived t :org-comment t))
  14332. (inhibit-read-only t)
  14333. (rea (concat ":" org-archive-tag ":"))
  14334. bmp file re)
  14335. (save-excursion
  14336. (save-restriction
  14337. (while (setq file (pop files))
  14338. (catch 'nextfile
  14339. (if (bufferp file)
  14340. (set-buffer file)
  14341. (org-check-agenda-file file)
  14342. (set-buffer (org-get-agenda-file-buffer file)))
  14343. (widen)
  14344. (setq bmp (buffer-modified-p))
  14345. (org-refresh-category-properties)
  14346. (setq org-todo-keywords-for-agenda
  14347. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14348. (setq org-done-keywords-for-agenda
  14349. (append org-done-keywords-for-agenda org-done-keywords))
  14350. (setq org-todo-keyword-alist-for-agenda
  14351. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14352. (setq org-drawers-for-agenda
  14353. (append org-drawers-for-agenda org-drawers))
  14354. (setq org-tag-alist-for-agenda
  14355. (append org-tag-alist-for-agenda org-tag-alist))
  14356. (save-excursion
  14357. (remove-text-properties (point-min) (point-max) pall)
  14358. (when org-agenda-skip-archived-trees
  14359. (goto-char (point-min))
  14360. (while (re-search-forward rea nil t)
  14361. (if (org-on-heading-p t)
  14362. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14363. (goto-char (point-min))
  14364. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14365. (while (re-search-forward re nil t)
  14366. (add-text-properties
  14367. (match-beginning 0) (org-end-of-subtree t) pc)))
  14368. (set-buffer-modified-p bmp)))))
  14369. (setq org-todo-keywords-for-agenda
  14370. (org-uniquify org-todo-keywords-for-agenda))
  14371. (setq org-todo-keyword-alist-for-agenda
  14372. (org-uniquify org-todo-keyword-alist-for-agenda)
  14373. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14374. ;;;; Embedded LaTeX
  14375. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14376. "Keymap for the minor `org-cdlatex-mode'.")
  14377. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14378. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14379. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14380. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14381. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14382. (defvar org-cdlatex-texmathp-advice-is-done nil
  14383. "Flag remembering if we have applied the advice to texmathp already.")
  14384. (define-minor-mode org-cdlatex-mode
  14385. "Toggle the minor `org-cdlatex-mode'.
  14386. This mode supports entering LaTeX environment and math in LaTeX fragments
  14387. in Org-mode.
  14388. \\{org-cdlatex-mode-map}"
  14389. nil " OCDL" nil
  14390. (when org-cdlatex-mode (require 'cdlatex))
  14391. (unless org-cdlatex-texmathp-advice-is-done
  14392. (setq org-cdlatex-texmathp-advice-is-done t)
  14393. (defadvice texmathp (around org-math-always-on activate)
  14394. "Always return t in org-mode buffers.
  14395. This is because we want to insert math symbols without dollars even outside
  14396. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14397. an embedded LaTeX fragment, let texmathp do its job.
  14398. \\[org-cdlatex-mode-map]"
  14399. (interactive)
  14400. (let (p)
  14401. (cond
  14402. ((not (org-mode-p)) ad-do-it)
  14403. ((eq this-command 'cdlatex-math-symbol)
  14404. (setq ad-return-value t
  14405. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14406. (t
  14407. (let ((p (org-inside-LaTeX-fragment-p)))
  14408. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14409. (setq ad-return-value t
  14410. texmathp-why '("Org-mode embedded math" . 0))
  14411. (if p ad-do-it)))))))))
  14412. (defun turn-on-org-cdlatex ()
  14413. "Unconditionally turn on `org-cdlatex-mode'."
  14414. (org-cdlatex-mode 1))
  14415. (defun org-inside-LaTeX-fragment-p ()
  14416. "Test if point is inside a LaTeX fragment.
  14417. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14418. sequence appearing also before point.
  14419. Even though the matchers for math are configurable, this function assumes
  14420. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14421. delimiters are skipped when they have been removed by customization.
  14422. The return value is nil, or a cons cell with the delimiter and
  14423. and the position of this delimiter.
  14424. This function does a reasonably good job, but can locally be fooled by
  14425. for example currency specifications. For example it will assume being in
  14426. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14427. fragments that are properly closed, but during editing, we have to live
  14428. with the uncertainty caused by missing closing delimiters. This function
  14429. looks only before point, not after."
  14430. (catch 'exit
  14431. (let ((pos (point))
  14432. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14433. (lim (progn
  14434. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14435. (point)))
  14436. dd-on str (start 0) m re)
  14437. (goto-char pos)
  14438. (when dodollar
  14439. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14440. re (nth 1 (assoc "$" org-latex-regexps)))
  14441. (while (string-match re str start)
  14442. (cond
  14443. ((= (match-end 0) (length str))
  14444. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14445. ((= (match-end 0) (- (length str) 5))
  14446. (throw 'exit nil))
  14447. (t (setq start (match-end 0))))))
  14448. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14449. (goto-char pos)
  14450. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14451. (and (match-beginning 2) (throw 'exit nil))
  14452. ;; count $$
  14453. (while (re-search-backward "\\$\\$" lim t)
  14454. (setq dd-on (not dd-on)))
  14455. (goto-char pos)
  14456. (if dd-on (cons "$$" m))))))
  14457. (defun org-inside-latex-macro-p ()
  14458. "Is point inside a LaTeX macro or its arguments?"
  14459. (save-match-data
  14460. (org-in-regexp
  14461. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14462. (defun org-try-cdlatex-tab ()
  14463. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14464. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14465. - inside a LaTeX fragment, or
  14466. - after the first word in a line, where an abbreviation expansion could
  14467. insert a LaTeX environment."
  14468. (when org-cdlatex-mode
  14469. (cond
  14470. ((save-excursion
  14471. (skip-chars-backward "a-zA-Z0-9*")
  14472. (skip-chars-backward " \t")
  14473. (bolp))
  14474. (cdlatex-tab) t)
  14475. ((org-inside-LaTeX-fragment-p)
  14476. (cdlatex-tab) t)
  14477. (t nil))))
  14478. (defun org-cdlatex-underscore-caret (&optional arg)
  14479. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14480. Revert to the normal definition outside of these fragments."
  14481. (interactive "P")
  14482. (if (org-inside-LaTeX-fragment-p)
  14483. (call-interactively 'cdlatex-sub-superscript)
  14484. (let (org-cdlatex-mode)
  14485. (call-interactively (key-binding (vector last-input-event))))))
  14486. (defun org-cdlatex-math-modify (&optional arg)
  14487. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14488. Revert to the normal definition outside of these fragments."
  14489. (interactive "P")
  14490. (if (org-inside-LaTeX-fragment-p)
  14491. (call-interactively 'cdlatex-math-modify)
  14492. (let (org-cdlatex-mode)
  14493. (call-interactively (key-binding (vector last-input-event))))))
  14494. (defvar org-latex-fragment-image-overlays nil
  14495. "List of overlays carrying the images of latex fragments.")
  14496. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14497. (defun org-remove-latex-fragment-image-overlays ()
  14498. "Remove all overlays with LaTeX fragment images in current buffer."
  14499. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14500. (setq org-latex-fragment-image-overlays nil))
  14501. (defun org-preview-latex-fragment (&optional subtree)
  14502. "Preview the LaTeX fragment at point, or all locally or globally.
  14503. If the cursor is in a LaTeX fragment, create the image and overlay
  14504. it over the source code. If there is no fragment at point, display
  14505. all fragments in the current text, from one headline to the next. With
  14506. prefix SUBTREE, display all fragments in the current subtree. With a
  14507. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14508. the cursor is before the first headline,
  14509. display all fragments in the buffer.
  14510. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14511. (interactive "P")
  14512. (org-remove-latex-fragment-image-overlays)
  14513. (save-excursion
  14514. (save-restriction
  14515. (let (beg end at msg)
  14516. (cond
  14517. ((or (equal subtree '(16))
  14518. (not (save-excursion
  14519. (re-search-backward (concat "^" outline-regexp) nil t))))
  14520. (setq beg (point-min) end (point-max)
  14521. msg "Creating images for buffer...%s"))
  14522. ((equal subtree '(4))
  14523. (org-back-to-heading)
  14524. (setq beg (point) end (org-end-of-subtree t)
  14525. msg "Creating images for subtree...%s"))
  14526. (t
  14527. (if (setq at (org-inside-LaTeX-fragment-p))
  14528. (goto-char (max (point-min) (- (cdr at) 2)))
  14529. (org-back-to-heading))
  14530. (setq beg (point) end (progn (outline-next-heading) (point))
  14531. msg (if at "Creating image...%s"
  14532. "Creating images for entry...%s"))))
  14533. (message msg "")
  14534. (narrow-to-region beg end)
  14535. (goto-char beg)
  14536. (org-format-latex
  14537. (concat "ltxpng/" (file-name-sans-extension
  14538. (file-name-nondirectory
  14539. buffer-file-name)))
  14540. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14541. (message msg "done. Use `C-c C-c' to remove images.")))))
  14542. (defvar org-latex-regexps
  14543. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14544. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14545. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14546. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14547. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14548. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14549. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14550. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14551. "Regular expressions for matching embedded LaTeX.")
  14552. (defvar org-export-have-math nil) ;; dynamic scoping
  14553. (defun org-format-latex (prefix &optional dir overlays msg at
  14554. forbuffer processing-type)
  14555. "Replace LaTeX fragments with links to an image, and produce images.
  14556. Some of the options can be changed using the variable
  14557. `org-format-latex-options'."
  14558. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14559. (let* ((prefixnodir (file-name-nondirectory prefix))
  14560. (absprefix (expand-file-name prefix dir))
  14561. (todir (file-name-directory absprefix))
  14562. (opt org-format-latex-options)
  14563. (matchers (plist-get opt :matchers))
  14564. (re-list org-latex-regexps)
  14565. (org-format-latex-header-extra
  14566. (plist-get (org-infile-export-plist) :latex-header-extra))
  14567. (cnt 0) txt hash link beg end re e checkdir
  14568. executables-checked string
  14569. m n block linkfile movefile ov)
  14570. ;; Check the different regular expressions
  14571. (while (setq e (pop re-list))
  14572. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14573. block (if (nth 3 e) "\n\n" ""))
  14574. (when (member m matchers)
  14575. (goto-char (point-min))
  14576. (while (re-search-forward re nil t)
  14577. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14578. (not (get-text-property (match-beginning n)
  14579. 'org-protected))
  14580. (or (not overlays)
  14581. (not (eq (get-char-property (match-beginning n)
  14582. 'org-overlay-type)
  14583. 'org-latex-overlay))))
  14584. (setq org-export-have-math t)
  14585. (cond
  14586. ((eq processing-type 'verbatim)
  14587. ;; Leave the text verbatim, just protect it
  14588. (add-text-properties (match-beginning n) (match-end n)
  14589. '(org-protected t)))
  14590. ((eq processing-type 'mathjax)
  14591. ;; Prepare for MathJax processing
  14592. (setq string (match-string n))
  14593. (if (equal m "$")
  14594. (save-excursion
  14595. (delete-region (match-beginning n) (match-end n))
  14596. (goto-char (match-beginning n))
  14597. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14598. "\\)")
  14599. '(org-protected t))))
  14600. (add-text-properties (match-beginning n) (match-end n)
  14601. '(org-protected t))))
  14602. ((or (eq processing-type 'dvipng) t)
  14603. ;; Process to an image
  14604. (setq txt (match-string n)
  14605. beg (match-beginning n) end (match-end n)
  14606. cnt (1+ cnt))
  14607. (let (print-length print-level) ; make sure full list is printed
  14608. (setq hash (sha1 (prin1-to-string
  14609. (list org-format-latex-header
  14610. org-format-latex-header-extra
  14611. org-export-latex-default-packages-alist
  14612. org-export-latex-packages-alist
  14613. org-format-latex-options
  14614. forbuffer txt)))
  14615. linkfile (format "%s_%s.png" prefix hash)
  14616. movefile (format "%s_%s.png" absprefix hash)))
  14617. (setq link (concat block "[[file:" linkfile "]]" block))
  14618. (if msg (message msg cnt))
  14619. (goto-char beg)
  14620. (unless checkdir ; make sure the directory exists
  14621. (setq checkdir t)
  14622. (or (file-directory-p todir) (make-directory todir t)))
  14623. (unless executables-checked
  14624. (org-check-external-command
  14625. "latex" "needed to convert LaTeX fragments to images")
  14626. (org-check-external-command
  14627. "dvipng" "needed to convert LaTeX fragments to images")
  14628. (setq executables-checked t))
  14629. (unless (file-exists-p movefile)
  14630. (org-create-formula-image
  14631. txt movefile opt forbuffer))
  14632. (if overlays
  14633. (progn
  14634. (mapc (lambda (o)
  14635. (if (eq (overlay-get o 'org-overlay-type)
  14636. 'org-latex-overlay)
  14637. (delete-overlay o)))
  14638. (overlays-in beg end))
  14639. (setq ov (make-overlay beg end))
  14640. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14641. (if (featurep 'xemacs)
  14642. (progn
  14643. (overlay-put ov 'invisible t)
  14644. (overlay-put
  14645. ov 'end-glyph
  14646. (make-glyph (vector 'png :file movefile))))
  14647. (overlay-put
  14648. ov 'display
  14649. (list 'image :type 'png :file movefile :ascent 'center)))
  14650. (push ov org-latex-fragment-image-overlays)
  14651. (goto-char end))
  14652. (delete-region beg end)
  14653. (insert (org-add-props link
  14654. (list 'org-latex-src
  14655. (replace-regexp-in-string
  14656. "\"" "" txt)))))))))))))
  14657. ;; This function borrows from Ganesh Swami's latex2png.el
  14658. (defun org-create-formula-image (string tofile options buffer)
  14659. "This calls dvipng."
  14660. (require 'org-latex)
  14661. (let* ((tmpdir (if (featurep 'xemacs)
  14662. (temp-directory)
  14663. temporary-file-directory))
  14664. (texfilebase (make-temp-name
  14665. (expand-file-name "orgtex" tmpdir)))
  14666. (texfile (concat texfilebase ".tex"))
  14667. (dvifile (concat texfilebase ".dvi"))
  14668. (pngfile (concat texfilebase ".png"))
  14669. (fnh (if (featurep 'xemacs)
  14670. (font-height (get-face-font 'default))
  14671. (face-attribute 'default :height nil)))
  14672. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14673. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14674. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14675. "Black"))
  14676. (bg (or (plist-get options (if buffer :background :html-background))
  14677. "Transparent")))
  14678. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14679. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14680. (with-temp-file texfile
  14681. (insert (org-splice-latex-header
  14682. org-format-latex-header
  14683. org-export-latex-default-packages-alist
  14684. org-export-latex-packages-alist t
  14685. org-format-latex-header-extra))
  14686. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14687. (require 'org-latex)
  14688. (org-export-latex-fix-inputenc))
  14689. (let ((dir default-directory))
  14690. (condition-case nil
  14691. (progn
  14692. (cd tmpdir)
  14693. (call-process "latex" nil nil nil texfile))
  14694. (error nil))
  14695. (cd dir))
  14696. (if (not (file-exists-p dvifile))
  14697. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14698. (condition-case nil
  14699. (call-process "dvipng" nil nil nil
  14700. "-fg" fg "-bg" bg
  14701. "-D" dpi
  14702. ;;"-x" scale "-y" scale
  14703. "-T" "tight"
  14704. "-o" pngfile
  14705. dvifile)
  14706. (error nil))
  14707. (if (not (file-exists-p pngfile))
  14708. (if org-format-latex-signal-error
  14709. (error "Failed to create png file from %s" texfile)
  14710. (message "Failed to create png file from %s" texfile)
  14711. nil)
  14712. ;; Use the requested file name and clean up
  14713. (copy-file pngfile tofile 'replace)
  14714. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14715. (delete-file (concat texfilebase e)))
  14716. pngfile))))
  14717. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14718. "Fill a LaTeX header template TPL.
  14719. In the template, the following place holders will be recognized:
  14720. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14721. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14722. [PACKAGES] \\usepackage statements for PKG
  14723. [NO-PACKAGES] do not include PKG
  14724. [EXTRA] the string EXTRA
  14725. [NO-EXTRA] do not include EXTRA
  14726. For backward compatibility, if both the positive and the negative place
  14727. holder is missing, the positive one (without the \"NO-\") will be
  14728. assumed to be present at the end of the template.
  14729. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14730. EXTRA is a string.
  14731. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14732. (let (rpl (end ""))
  14733. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14734. (setq rpl (if (or (match-end 1) (not def-pkg))
  14735. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14736. tpl (replace-match rpl t t tpl))
  14737. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14738. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14739. (setq rpl (if (or (match-end 1) (not pkg))
  14740. "" (org-latex-packages-to-string pkg snippets-p t))
  14741. tpl (replace-match rpl t t tpl))
  14742. (if pkg (setq end
  14743. (concat end "\n"
  14744. (org-latex-packages-to-string pkg snippets-p)))))
  14745. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14746. (setq rpl (if (or (match-end 1) (not extra))
  14747. "" (concat extra "\n"))
  14748. tpl (replace-match rpl t t tpl))
  14749. (if (and extra (string-match "\\S-" extra))
  14750. (setq end (concat end "\n" extra))))
  14751. (if (string-match "\\S-" end)
  14752. (concat tpl "\n" end)
  14753. tpl)))
  14754. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14755. "Turn an alist of packages into a string with the \\usepackage macros."
  14756. (setq pkg (mapconcat (lambda(p)
  14757. (cond
  14758. ((stringp p) p)
  14759. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14760. (format "%% Package %s omitted" (cadr p)))
  14761. ((equal "" (car p))
  14762. (format "\\usepackage{%s}" (cadr p)))
  14763. (t
  14764. (format "\\usepackage[%s]{%s}"
  14765. (car p) (cadr p)))))
  14766. pkg
  14767. "\n"))
  14768. (if newline (concat pkg "\n") pkg))
  14769. (defun org-dvipng-color (attr)
  14770. "Return an rgb color specification for dvipng."
  14771. (apply 'format "rgb %s %s %s"
  14772. (mapcar 'org-normalize-color
  14773. (color-values (face-attribute 'default attr nil)))))
  14774. (defun org-normalize-color (value)
  14775. "Return string to be used as color value for an RGB component."
  14776. (format "%g" (/ value 65535.0)))
  14777. ;; Image display
  14778. (defvar org-inline-image-overlays nil)
  14779. (make-variable-buffer-local 'org-inline-image-overlays)
  14780. (defun org-toggle-inline-images (&optional include-linked)
  14781. "Toggle the display of inline images.
  14782. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14783. (interactive "P")
  14784. (if org-inline-image-overlays
  14785. (progn
  14786. (org-remove-inline-images)
  14787. (message "Inline image display turned off"))
  14788. (org-display-inline-images include-linked)
  14789. (if org-inline-image-overlays
  14790. (message "%d images displayed inline"
  14791. (length org-inline-image-overlays))
  14792. (message "No images to display inline"))))
  14793. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14794. "Display inline images.
  14795. Normally only links without a description part are inlined, because this
  14796. is how it will work for export. When INCLUDE-LINKED is set, also links
  14797. with a description part will be inlined. This can be nice for a quick
  14798. look at those images, but it does not reflect what exported files will look
  14799. like.
  14800. When REFRESH is set, refresh existing images between BEG and END.
  14801. This will create new image displays only if necessary.
  14802. BEG and END default to the buffer boundaries."
  14803. (interactive "P")
  14804. (unless refresh
  14805. (org-remove-inline-images)
  14806. (clear-image-cache))
  14807. (save-excursion
  14808. (save-restriction
  14809. (widen)
  14810. (setq beg (or beg (point-min)) end (or end (point-max)))
  14811. (goto-char (point-min))
  14812. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
  14813. (substring (org-image-file-name-regexp) 0 -2)
  14814. "\\)\\]" (if include-linked "" "\\]")))
  14815. old file ov img)
  14816. (while (re-search-forward re end t)
  14817. (setq old (get-char-property-and-overlay (match-beginning 1)
  14818. 'org-image-overlay))
  14819. (setq file (expand-file-name
  14820. (concat (or (match-string 3) "") (match-string 4))))
  14821. (when (file-exists-p file)
  14822. (if (and (car-safe old) refresh)
  14823. (image-refresh (overlay-get (cdr old) 'display))
  14824. (setq img (create-image file))
  14825. (when img
  14826. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14827. (overlay-put ov 'display img)
  14828. (overlay-put ov 'face 'default)
  14829. (overlay-put ov 'org-image-overlay t)
  14830. (overlay-put ov 'modification-hooks
  14831. (list 'org-display-inline-modification-hook))
  14832. (push ov org-inline-image-overlays)))))))))
  14833. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14834. "Remove inline-display overlay if a corresponding region is modified."
  14835. (let ((inhibit-modification-hooks t))
  14836. (when (and ov after)
  14837. (delete ov org-inline-image-overlays)
  14838. (delete-overlay ov))))
  14839. (defun org-remove-inline-images ()
  14840. "Remove inline display of images."
  14841. (interactive)
  14842. (mapc 'delete-overlay org-inline-image-overlays)
  14843. (setq org-inline-image-overlays nil))
  14844. ;;;; Key bindings
  14845. ;; Make `C-c C-x' a prefix key
  14846. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14847. ;; TAB key with modifiers
  14848. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14849. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14850. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14851. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14852. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14853. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14854. ;; The following line is necessary under Suse GNU/Linux
  14855. (unless (featurep 'xemacs)
  14856. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14857. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14858. (define-key org-mode-map [backtab] 'org-shifttab)
  14859. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14860. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14861. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14862. ;; Cursor keys with modifiers
  14863. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14864. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14865. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14866. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14867. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14868. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14869. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14870. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14871. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14872. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14873. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14874. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14875. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14876. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14877. ;; Babel keys
  14878. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14879. (mapc (lambda (pair)
  14880. (define-key org-babel-map (car pair) (cdr pair)))
  14881. org-babel-key-bindings)
  14882. ;;; Extra keys for tty access.
  14883. ;; We only set them when really needed because otherwise the
  14884. ;; menus don't show the simple keys
  14885. (when (or org-use-extra-keys
  14886. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14887. (not window-system))
  14888. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14889. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14890. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14891. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14892. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14893. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14894. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14895. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14896. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14897. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14898. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14899. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14900. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14901. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14902. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14903. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14904. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14905. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14906. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14907. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14908. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14909. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14910. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14911. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14912. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14913. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14914. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14915. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14916. ;; All the other keys
  14917. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14918. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14919. (if (boundp 'narrow-map)
  14920. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14921. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14922. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14923. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14924. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14925. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14926. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14927. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14928. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14929. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14930. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14931. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14932. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14933. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14934. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14935. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14936. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14937. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14938. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14939. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14940. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14941. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14942. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14943. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14944. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14945. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14946. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14947. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14948. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14949. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14950. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14951. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14952. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14953. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14954. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14955. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14956. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14957. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14958. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14959. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14960. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14961. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14962. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14963. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14964. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14965. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14966. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14967. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14968. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14969. (org-defkey org-mode-map "\C-m" 'org-return)
  14970. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14971. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14972. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14973. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14974. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14975. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14976. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14977. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14978. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14979. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14980. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14981. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14982. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14983. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14984. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14985. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14986. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14987. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14988. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14989. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14990. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14991. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14992. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14993. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14994. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14995. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14996. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14997. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14998. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14999. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15000. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15001. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15002. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15003. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15004. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15005. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15006. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15007. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15008. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15009. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15010. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15011. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15012. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15013. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15014. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15015. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15016. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15017. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15018. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15019. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15020. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15021. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15022. (when (featurep 'xemacs)
  15023. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15024. (defconst org-speed-commands-default
  15025. '(
  15026. ("Outline Navigation")
  15027. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15028. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15029. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15030. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15031. ("u" . (org-speed-move-safe 'outline-up-heading))
  15032. ("j" . org-goto)
  15033. ("g" . (org-refile t))
  15034. ("Outline Visibility")
  15035. ("c" . org-cycle)
  15036. ("C" . org-shifttab)
  15037. (" " . org-display-outline-path)
  15038. ("Outline Structure Editing")
  15039. ("U" . org-shiftmetaup)
  15040. ("D" . org-shiftmetadown)
  15041. ("r" . org-metaright)
  15042. ("l" . org-metaleft)
  15043. ("R" . org-shiftmetaright)
  15044. ("L" . org-shiftmetaleft)
  15045. ("i" . (progn (forward-char 1) (call-interactively
  15046. 'org-insert-heading-respect-content)))
  15047. ("^" . org-sort)
  15048. ("w" . org-refile)
  15049. ("a" . org-archive-subtree-default-with-confirmation)
  15050. ("." . outline-mark-subtree)
  15051. ("Clock Commands")
  15052. ("I" . org-clock-in)
  15053. ("O" . org-clock-out)
  15054. ("Meta Data Editing")
  15055. ("t" . org-todo)
  15056. ("0" . (org-priority ?\ ))
  15057. ("1" . (org-priority ?A))
  15058. ("2" . (org-priority ?B))
  15059. ("3" . (org-priority ?C))
  15060. (";" . org-set-tags-command)
  15061. ("e" . org-set-effort)
  15062. ("Agenda Views etc")
  15063. ("v" . org-agenda)
  15064. ("/" . org-sparse-tree)
  15065. ("Misc")
  15066. ("o" . org-open-at-point)
  15067. ("?" . org-speed-command-help)
  15068. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15069. (">" . (org-agenda-remove-restriction-lock))
  15070. )
  15071. "The default speed commands.")
  15072. (defun org-print-speed-command (e)
  15073. (if (> (length (car e)) 1)
  15074. (progn
  15075. (princ "\n")
  15076. (princ (car e))
  15077. (princ "\n")
  15078. (princ (make-string (length (car e)) ?-))
  15079. (princ "\n"))
  15080. (princ (car e))
  15081. (princ " ")
  15082. (if (symbolp (cdr e))
  15083. (princ (symbol-name (cdr e)))
  15084. (prin1 (cdr e)))
  15085. (princ "\n")))
  15086. (defun org-speed-command-help ()
  15087. "Show the available speed commands."
  15088. (interactive)
  15089. (if (not org-use-speed-commands)
  15090. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15091. (with-output-to-temp-buffer "*Help*"
  15092. (princ "User-defined Speed commands\n===========================\n")
  15093. (mapc 'org-print-speed-command org-speed-commands-user)
  15094. (princ "\n")
  15095. (princ "Built-in Speed commands\n=======================\n")
  15096. (mapc 'org-print-speed-command org-speed-commands-default))
  15097. (with-current-buffer "*Help*"
  15098. (setq truncate-lines t))))
  15099. (defun org-speed-move-safe (cmd)
  15100. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15101. If not, return to the original position and throw an error."
  15102. (interactive)
  15103. (let ((pos (point)))
  15104. (call-interactively cmd)
  15105. (unless (and (bolp) (org-on-heading-p))
  15106. (goto-char pos)
  15107. (error "Boundary reached while executing %s" cmd))))
  15108. (defvar org-self-insert-command-undo-counter 0)
  15109. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15110. (defvar org-speed-command nil)
  15111. (defun org-self-insert-command (N)
  15112. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15113. If the cursor is in a table looking at whitespace, the whitespace is
  15114. overwritten, and the table is not marked as requiring realignment."
  15115. (interactive "p")
  15116. (cond
  15117. ((and org-use-speed-commands
  15118. (or (and (bolp) (looking-at outline-regexp))
  15119. (and (functionp org-use-speed-commands)
  15120. (funcall org-use-speed-commands)))
  15121. (setq
  15122. org-speed-command
  15123. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  15124. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  15125. (cond
  15126. ((commandp org-speed-command)
  15127. (setq this-command org-speed-command)
  15128. (call-interactively org-speed-command))
  15129. ((functionp org-speed-command)
  15130. (funcall org-speed-command))
  15131. ((and org-speed-command (listp org-speed-command))
  15132. (eval org-speed-command))
  15133. (t (let (org-use-speed-commands)
  15134. (call-interactively 'org-self-insert-command)))))
  15135. ((and
  15136. (org-table-p)
  15137. (progn
  15138. ;; check if we blank the field, and if that triggers align
  15139. (and (featurep 'org-table) org-table-auto-blank-field
  15140. (member last-command
  15141. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15142. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15143. ;; got extra space, this field does not determine column width
  15144. (let (org-table-may-need-update) (org-table-blank-field))
  15145. ;; no extra space, this field may determine column width
  15146. (org-table-blank-field)))
  15147. t)
  15148. (eq N 1)
  15149. (looking-at "[^|\n]* |"))
  15150. (let (org-table-may-need-update)
  15151. (goto-char (1- (match-end 0)))
  15152. (delete-backward-char 1)
  15153. (goto-char (match-beginning 0))
  15154. (self-insert-command N)))
  15155. (t
  15156. (setq org-table-may-need-update t)
  15157. (self-insert-command N)
  15158. (org-fix-tags-on-the-fly)
  15159. (if org-self-insert-cluster-for-undo
  15160. (if (not (eq last-command 'org-self-insert-command))
  15161. (setq org-self-insert-command-undo-counter 1)
  15162. (if (>= org-self-insert-command-undo-counter 20)
  15163. (setq org-self-insert-command-undo-counter 1)
  15164. (and (> org-self-insert-command-undo-counter 0)
  15165. buffer-undo-list
  15166. (not (cadr buffer-undo-list)) ; remove nil entry
  15167. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15168. (setq org-self-insert-command-undo-counter
  15169. (1+ org-self-insert-command-undo-counter))))))))
  15170. (defun org-fix-tags-on-the-fly ()
  15171. (when (and (equal (char-after (point-at-bol)) ?*)
  15172. (org-on-heading-p))
  15173. (org-align-tags-here org-tags-column)))
  15174. (defun org-delete-backward-char (N)
  15175. "Like `delete-backward-char', insert whitespace at field end in tables.
  15176. When deleting backwards, in tables this function will insert whitespace in
  15177. front of the next \"|\" separator, to keep the table aligned. The table will
  15178. still be marked for re-alignment if the field did fill the entire column,
  15179. because, in this case the deletion might narrow the column."
  15180. (interactive "p")
  15181. (if (and (org-table-p)
  15182. (eq N 1)
  15183. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15184. (looking-at ".*?|"))
  15185. (let ((pos (point))
  15186. (noalign (looking-at "[^|\n\r]* |"))
  15187. (c org-table-may-need-update))
  15188. (backward-delete-char N)
  15189. (skip-chars-forward "^|")
  15190. (insert " ")
  15191. (goto-char (1- pos))
  15192. ;; noalign: if there were two spaces at the end, this field
  15193. ;; does not determine the width of the column.
  15194. (if noalign (setq org-table-may-need-update c)))
  15195. (backward-delete-char N)
  15196. (org-fix-tags-on-the-fly)))
  15197. (defun org-delete-char (N)
  15198. "Like `delete-char', but insert whitespace at field end in tables.
  15199. When deleting characters, in tables this function will insert whitespace in
  15200. front of the next \"|\" separator, to keep the table aligned. The table will
  15201. still be marked for re-alignment if the field did fill the entire column,
  15202. because, in this case the deletion might narrow the column."
  15203. (interactive "p")
  15204. (if (and (org-table-p)
  15205. (not (bolp))
  15206. (not (= (char-after) ?|))
  15207. (eq N 1))
  15208. (if (looking-at ".*?|")
  15209. (let ((pos (point))
  15210. (noalign (looking-at "[^|\n\r]* |"))
  15211. (c org-table-may-need-update))
  15212. (replace-match (concat
  15213. (substring (match-string 0) 1 -1)
  15214. " |"))
  15215. (goto-char pos)
  15216. ;; noalign: if there were two spaces at the end, this field
  15217. ;; does not determine the width of the column.
  15218. (if noalign (setq org-table-may-need-update c)))
  15219. (delete-char N))
  15220. (delete-char N)
  15221. (org-fix-tags-on-the-fly)))
  15222. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15223. (put 'org-self-insert-command 'delete-selection t)
  15224. (put 'orgtbl-self-insert-command 'delete-selection t)
  15225. (put 'org-delete-char 'delete-selection 'supersede)
  15226. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15227. (put 'org-yank 'delete-selection 'yank)
  15228. ;; Make `flyspell-mode' delay after some commands
  15229. (put 'org-self-insert-command 'flyspell-delayed t)
  15230. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15231. (put 'org-delete-char 'flyspell-delayed t)
  15232. (put 'org-delete-backward-char 'flyspell-delayed t)
  15233. ;; Make pabbrev-mode expand after org-mode commands
  15234. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15235. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15236. ;; How to do this: Measure non-white length of current string
  15237. ;; If equal to column width, we should realign.
  15238. (defun org-remap (map &rest commands)
  15239. "In MAP, remap the functions given in COMMANDS.
  15240. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15241. (let (new old)
  15242. (while commands
  15243. (setq old (pop commands) new (pop commands))
  15244. (if (fboundp 'command-remapping)
  15245. (org-defkey map (vector 'remap old) new)
  15246. (substitute-key-definition old new map global-map)))))
  15247. (when (eq org-enable-table-editor 'optimized)
  15248. ;; If the user wants maximum table support, we need to hijack
  15249. ;; some standard editing functions
  15250. (org-remap org-mode-map
  15251. 'self-insert-command 'org-self-insert-command
  15252. 'delete-char 'org-delete-char
  15253. 'delete-backward-char 'org-delete-backward-char)
  15254. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15255. (defvar org-ctrl-c-ctrl-c-hook nil
  15256. "Hook for functions attaching themselves to `C-c C-c'.
  15257. This can be used to add additional functionality to the C-c C-c key which
  15258. executes context-dependent commands.
  15259. Each function will be called with no arguments. The function must check
  15260. if the context is appropriate for it to act. If yes, it should do its
  15261. thing and then return a non-nil value. If the context is wrong,
  15262. just do nothing and return nil.")
  15263. (defvar org-tab-first-hook nil
  15264. "Hook for functions to attach themselves to TAB.
  15265. See `org-ctrl-c-ctrl-c-hook' for more information.
  15266. This hook runs as the first action when TAB is pressed, even before
  15267. `org-cycle' messes around with the `outline-regexp' to cater for
  15268. inline tasks and plain list item folding.
  15269. If any function in this hook returns t, any other actions that
  15270. would have been caused by TAB (such as table field motion or visibility
  15271. cycling) will not occur.")
  15272. (defvar org-tab-after-check-for-table-hook nil
  15273. "Hook for functions to attach themselves to TAB.
  15274. See `org-ctrl-c-ctrl-c-hook' for more information.
  15275. This hook runs after it has been established that the cursor is not in a
  15276. table, but before checking if the cursor is in a headline or if global cycling
  15277. should be done.
  15278. If any function in this hook returns t, not other actions like visibility
  15279. cycling will be done.")
  15280. (defvar org-tab-after-check-for-cycling-hook nil
  15281. "Hook for functions to attach themselves to TAB.
  15282. See `org-ctrl-c-ctrl-c-hook' for more information.
  15283. This hook runs after it has been established that not table field motion and
  15284. not visibility should be done because of current context. This is probably
  15285. the place where a package like yasnippets can hook in.")
  15286. (defvar org-tab-before-tab-emulation-hook nil
  15287. "Hook for functions to attach themselves to TAB.
  15288. See `org-ctrl-c-ctrl-c-hook' for more information.
  15289. This hook runs after every other options for TAB have been exhausted, but
  15290. before indentation and \t insertion takes place.")
  15291. (defvar org-metaleft-hook nil
  15292. "Hook for functions attaching themselves to `M-left'.
  15293. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15294. (defvar org-metaright-hook nil
  15295. "Hook for functions attaching themselves to `M-right'.
  15296. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15297. (defvar org-metaup-hook nil
  15298. "Hook for functions attaching themselves to `M-up'.
  15299. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15300. (defvar org-metadown-hook nil
  15301. "Hook for functions attaching themselves to `M-down'.
  15302. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15303. (defvar org-shiftmetaleft-hook nil
  15304. "Hook for functions attaching themselves to `M-S-left'.
  15305. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15306. (defvar org-shiftmetaright-hook nil
  15307. "Hook for functions attaching themselves to `M-S-right'.
  15308. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15309. (defvar org-shiftmetaup-hook nil
  15310. "Hook for functions attaching themselves to `M-S-up'.
  15311. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15312. (defvar org-shiftmetadown-hook nil
  15313. "Hook for functions attaching themselves to `M-S-down'.
  15314. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15315. (defvar org-metareturn-hook nil
  15316. "Hook for functions attaching themselves to `M-RET'.
  15317. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15318. (defvar org-shiftup-hook nil
  15319. "Hook for functions attaching themselves to `S-up'.
  15320. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15321. (defvar org-shiftup-final-hook nil
  15322. "Hook for functions attaching themselves to `S-up'.
  15323. This one runs after all other options except shift-select have been excluded.
  15324. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15325. (defvar org-shiftdown-hook nil
  15326. "Hook for functions attaching themselves to `S-down'.
  15327. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15328. (defvar org-shiftdown-final-hook nil
  15329. "Hook for functions attaching themselves to `S-down'.
  15330. This one runs after all other options except shift-select have been excluded.
  15331. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15332. (defvar org-shiftleft-hook nil
  15333. "Hook for functions attaching themselves to `S-left'.
  15334. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15335. (defvar org-shiftleft-final-hook nil
  15336. "Hook for functions attaching themselves to `S-left'.
  15337. This one runs after all other options except shift-select have been excluded.
  15338. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15339. (defvar org-shiftright-hook nil
  15340. "Hook for functions attaching themselves to `S-right'.
  15341. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15342. (defvar org-shiftright-final-hook nil
  15343. "Hook for functions attaching themselves to `S-right'.
  15344. This one runs after all other options except shift-select have been excluded.
  15345. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15346. (defun org-modifier-cursor-error ()
  15347. "Throw an error, a modified cursor command was applied in wrong context."
  15348. (error "This command is active in special context like tables, headlines or items"))
  15349. (defun org-shiftselect-error ()
  15350. "Throw an error because Shift-Cursor command was applied in wrong context."
  15351. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15352. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15353. (error "This command works only in special context like headlines or timestamps")))
  15354. (defun org-call-for-shift-select (cmd)
  15355. (let ((this-command-keys-shift-translated t))
  15356. (call-interactively cmd)))
  15357. (defun org-shifttab (&optional arg)
  15358. "Global visibility cycling or move to previous table field.
  15359. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15360. on context.
  15361. See the individual commands for more information."
  15362. (interactive "P")
  15363. (cond
  15364. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15365. ((integerp arg)
  15366. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15367. (message "Content view to level: %d" arg)
  15368. (org-content (prefix-numeric-value arg2))
  15369. (setq org-cycle-global-status 'overview)))
  15370. (t (call-interactively 'org-global-cycle))))
  15371. (defun org-shiftmetaleft ()
  15372. "Promote subtree or delete table column.
  15373. Calls `org-promote-subtree', `org-outdent-item',
  15374. or `org-table-delete-column', depending on context.
  15375. See the individual commands for more information."
  15376. (interactive)
  15377. (cond
  15378. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15379. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15380. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15381. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15382. (t (org-modifier-cursor-error))))
  15383. (defun org-shiftmetaright ()
  15384. "Demote subtree or insert table column.
  15385. Calls `org-demote-subtree', `org-indent-item',
  15386. or `org-table-insert-column', depending on context.
  15387. See the individual commands for more information."
  15388. (interactive)
  15389. (cond
  15390. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15391. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15392. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15393. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15394. (t (org-modifier-cursor-error))))
  15395. (defun org-shiftmetaup (&optional arg)
  15396. "Move subtree up or kill table row.
  15397. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15398. `org-move-item-up' depending on context. See the individual commands
  15399. for more information."
  15400. (interactive "P")
  15401. (cond
  15402. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15403. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15404. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15405. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15406. (t (org-modifier-cursor-error))))
  15407. (defun org-shiftmetadown (&optional arg)
  15408. "Move subtree down or insert table row.
  15409. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15410. `org-move-item-down', depending on context. See the individual
  15411. commands for more information."
  15412. (interactive "P")
  15413. (cond
  15414. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15415. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15416. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15417. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15418. (t (org-modifier-cursor-error))))
  15419. (defsubst org-hidden-tree-error ()
  15420. (error
  15421. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15422. (defun org-metaleft (&optional arg)
  15423. "Promote heading or move table column to left.
  15424. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15425. With no specific context, calls the Emacs default `backward-word'.
  15426. See the individual commands for more information."
  15427. (interactive "P")
  15428. (cond
  15429. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15430. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15431. ((or (org-on-heading-p)
  15432. (and (org-region-active-p)
  15433. (save-excursion
  15434. (goto-char (region-beginning))
  15435. (org-on-heading-p))))
  15436. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15437. (call-interactively 'org-do-promote))
  15438. ((or (org-at-item-p)
  15439. (and (org-region-active-p)
  15440. (save-excursion
  15441. (goto-char (region-beginning))
  15442. (org-at-item-p))))
  15443. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15444. (call-interactively 'org-outdent-item))
  15445. (t (call-interactively 'backward-word))))
  15446. (defun org-metaright (&optional arg)
  15447. "Demote subtree or move table column to right.
  15448. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15449. With no specific context, calls the Emacs default `forward-word'.
  15450. See the individual commands for more information."
  15451. (interactive "P")
  15452. (cond
  15453. ((run-hook-with-args-until-success 'org-metaright-hook))
  15454. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15455. ((or (org-on-heading-p)
  15456. (and (org-region-active-p)
  15457. (save-excursion
  15458. (goto-char (region-beginning))
  15459. (org-on-heading-p))))
  15460. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15461. (call-interactively 'org-do-demote))
  15462. ((or (org-at-item-p)
  15463. (and (org-region-active-p)
  15464. (save-excursion
  15465. (goto-char (region-beginning))
  15466. (org-at-item-p))))
  15467. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15468. (call-interactively 'org-indent-item))
  15469. (t (call-interactively 'forward-word))))
  15470. (defun org-check-for-hidden (what)
  15471. "Check if there are hidden headlines/items in the current visual line.
  15472. WHAT can be either `headlines' or `items'. If the current line is
  15473. an outline or item heading and it has a folded subtree below it,
  15474. this function returns t, nil otherwise."
  15475. (let ((re (cond
  15476. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15477. ((eq what 'items) (concat "^" (org-item-re t)))
  15478. (t (error "This should not happen"))))
  15479. beg end)
  15480. (save-excursion
  15481. (catch 'exit
  15482. (unless (org-region-active-p)
  15483. (setq beg (point-at-bol))
  15484. (beginning-of-line 2)
  15485. (while (and (not (eobp)) ;; this is like `next-line'
  15486. (get-char-property (1- (point)) 'invisible))
  15487. (beginning-of-line 2))
  15488. (setq end (point))
  15489. (goto-char beg)
  15490. (goto-char (point-at-eol))
  15491. (setq end (max end (point)))
  15492. (while (re-search-forward re end t)
  15493. (if (get-char-property (match-beginning 0) 'invisible)
  15494. (throw 'exit t))))
  15495. nil))))
  15496. (defun org-metaup (&optional arg)
  15497. "Move subtree up or move table row up.
  15498. Calls `org-move-subtree-up' or `org-table-move-row' or
  15499. `org-move-item-up', depending on context. See the individual commands
  15500. for more information."
  15501. (interactive "P")
  15502. (cond
  15503. ((run-hook-with-args-until-success 'org-metaup-hook))
  15504. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15505. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15506. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15507. (t (transpose-lines 1) (beginning-of-line -1))))
  15508. (defun org-metadown (&optional arg)
  15509. "Move subtree down or move table row down.
  15510. Calls `org-move-subtree-down' or `org-table-move-row' or
  15511. `org-move-item-down', depending on context. See the individual
  15512. commands for more information."
  15513. (interactive "P")
  15514. (cond
  15515. ((run-hook-with-args-until-success 'org-metadown-hook))
  15516. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15517. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15518. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15519. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15520. (defun org-shiftup (&optional arg)
  15521. "Increase item in timestamp or increase priority of current headline.
  15522. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15523. depending on context. See the individual commands for more information."
  15524. (interactive "P")
  15525. (cond
  15526. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15527. ((and org-support-shift-select (org-region-active-p))
  15528. (org-call-for-shift-select 'previous-line))
  15529. ((org-at-timestamp-p t)
  15530. (call-interactively (if org-edit-timestamp-down-means-later
  15531. 'org-timestamp-down 'org-timestamp-up)))
  15532. ((and (not (eq org-support-shift-select 'always))
  15533. org-enable-priority-commands
  15534. (org-on-heading-p))
  15535. (call-interactively 'org-priority-up))
  15536. ((and (not org-support-shift-select) (org-at-item-p))
  15537. (call-interactively 'org-previous-item))
  15538. ((org-clocktable-try-shift 'up arg))
  15539. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15540. (org-support-shift-select
  15541. (org-call-for-shift-select 'previous-line))
  15542. (t (org-shiftselect-error))))
  15543. (defun org-shiftdown (&optional arg)
  15544. "Decrease item in timestamp or decrease priority of current headline.
  15545. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15546. depending on context. See the individual commands for more information."
  15547. (interactive "P")
  15548. (cond
  15549. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15550. ((and org-support-shift-select (org-region-active-p))
  15551. (org-call-for-shift-select 'next-line))
  15552. ((org-at-timestamp-p t)
  15553. (call-interactively (if org-edit-timestamp-down-means-later
  15554. 'org-timestamp-up 'org-timestamp-down)))
  15555. ((and (not (eq org-support-shift-select 'always))
  15556. org-enable-priority-commands
  15557. (org-on-heading-p))
  15558. (call-interactively 'org-priority-down))
  15559. ((and (not org-support-shift-select) (org-at-item-p))
  15560. (call-interactively 'org-next-item))
  15561. ((org-clocktable-try-shift 'down arg))
  15562. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15563. (org-support-shift-select
  15564. (org-call-for-shift-select 'next-line))
  15565. (t (org-shiftselect-error))))
  15566. (defun org-shiftright (&optional arg)
  15567. "Cycle the thing at point or in the current line, depending on context.
  15568. Depending on context, this does one of the following:
  15569. - switch a timestamp at point one day into the future
  15570. - on a headline, switch to the next TODO keyword.
  15571. - on an item, switch entire list to the next bullet type
  15572. - on a property line, switch to the next allowed value
  15573. - on a clocktable definition line, move time block into the future"
  15574. (interactive "P")
  15575. (cond
  15576. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15577. ((and org-support-shift-select (org-region-active-p))
  15578. (org-call-for-shift-select 'forward-char))
  15579. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15580. ((and (not (eq org-support-shift-select 'always))
  15581. (org-on-heading-p))
  15582. (let ((org-inhibit-logging
  15583. (not org-treat-S-cursor-todo-selection-as-state-change))
  15584. (org-inhibit-blocking
  15585. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15586. (org-call-with-arg 'org-todo 'right)))
  15587. ((or (and org-support-shift-select
  15588. (not (eq org-support-shift-select 'always))
  15589. (org-at-item-bullet-p))
  15590. (and (not org-support-shift-select) (org-at-item-p)))
  15591. (org-call-with-arg 'org-cycle-list-bullet nil))
  15592. ((and (not (eq org-support-shift-select 'always))
  15593. (org-at-property-p))
  15594. (call-interactively 'org-property-next-allowed-value))
  15595. ((org-clocktable-try-shift 'right arg))
  15596. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15597. (org-support-shift-select
  15598. (org-call-for-shift-select 'forward-char))
  15599. (t (org-shiftselect-error))))
  15600. (defun org-shiftleft (&optional arg)
  15601. "Cycle the thing at point or in the current line, depending on context.
  15602. Depending on context, this does one of the following:
  15603. - switch a timestamp at point one day into the past
  15604. - on a headline, switch to the previous TODO keyword.
  15605. - on an item, switch entire list to the previous bullet type
  15606. - on a property line, switch to the previous allowed value
  15607. - on a clocktable definition line, move time block into the past"
  15608. (interactive "P")
  15609. (cond
  15610. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15611. ((and org-support-shift-select (org-region-active-p))
  15612. (org-call-for-shift-select 'backward-char))
  15613. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15614. ((and (not (eq org-support-shift-select 'always))
  15615. (org-on-heading-p))
  15616. (let ((org-inhibit-logging
  15617. (not org-treat-S-cursor-todo-selection-as-state-change))
  15618. (org-inhibit-blocking
  15619. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15620. (org-call-with-arg 'org-todo 'left)))
  15621. ((or (and org-support-shift-select
  15622. (not (eq org-support-shift-select 'always))
  15623. (org-at-item-bullet-p))
  15624. (and (not org-support-shift-select) (org-at-item-p)))
  15625. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15626. ((and (not (eq org-support-shift-select 'always))
  15627. (org-at-property-p))
  15628. (call-interactively 'org-property-previous-allowed-value))
  15629. ((org-clocktable-try-shift 'left arg))
  15630. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15631. (org-support-shift-select
  15632. (org-call-for-shift-select 'backward-char))
  15633. (t (org-shiftselect-error))))
  15634. (defun org-shiftcontrolright ()
  15635. "Switch to next TODO set."
  15636. (interactive)
  15637. (cond
  15638. ((and org-support-shift-select (org-region-active-p))
  15639. (org-call-for-shift-select 'forward-word))
  15640. ((and (not (eq org-support-shift-select 'always))
  15641. (org-on-heading-p))
  15642. (org-call-with-arg 'org-todo 'nextset))
  15643. (org-support-shift-select
  15644. (org-call-for-shift-select 'forward-word))
  15645. (t (org-shiftselect-error))))
  15646. (defun org-shiftcontrolleft ()
  15647. "Switch to previous TODO set."
  15648. (interactive)
  15649. (cond
  15650. ((and org-support-shift-select (org-region-active-p))
  15651. (org-call-for-shift-select 'backward-word))
  15652. ((and (not (eq org-support-shift-select 'always))
  15653. (org-on-heading-p))
  15654. (org-call-with-arg 'org-todo 'previousset))
  15655. (org-support-shift-select
  15656. (org-call-for-shift-select 'backward-word))
  15657. (t (org-shiftselect-error))))
  15658. (defun org-ctrl-c-ret ()
  15659. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15660. (interactive)
  15661. (cond
  15662. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15663. (t (call-interactively 'org-insert-heading))))
  15664. (defun org-copy-special ()
  15665. "Copy region in table or copy current subtree.
  15666. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15667. See the individual commands for more information."
  15668. (interactive)
  15669. (call-interactively
  15670. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15671. (defun org-cut-special ()
  15672. "Cut region in table or cut current subtree.
  15673. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15674. See the individual commands for more information."
  15675. (interactive)
  15676. (call-interactively
  15677. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15678. (defun org-paste-special (arg)
  15679. "Paste rectangular region into table, or past subtree relative to level.
  15680. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15681. See the individual commands for more information."
  15682. (interactive "P")
  15683. (if (org-at-table-p)
  15684. (org-table-paste-rectangle)
  15685. (org-paste-subtree arg)))
  15686. (defun org-edit-special (&optional arg)
  15687. "Call a special editor for the stuff at point.
  15688. When at a table, call the formula editor with `org-table-edit-formulas'.
  15689. When at the first line of an src example, call `org-edit-src-code'.
  15690. When in an #+include line, visit the include file. Otherwise call
  15691. `ffap' to visit the file at point."
  15692. (interactive)
  15693. ;; possibly prep session before editing source
  15694. (when arg
  15695. (let* ((info (org-babel-get-src-block-info))
  15696. (lang (nth 0 info))
  15697. (params (nth 2 info))
  15698. (session (cdr (assoc :session params))))
  15699. (when (and info session) ;; we are in a source-code block with a session
  15700. (funcall
  15701. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15702. (cond ;; proceed with `org-edit-special'
  15703. ((save-excursion
  15704. (beginning-of-line 1)
  15705. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15706. (find-file (org-trim (match-string 1))))
  15707. ((org-edit-src-code))
  15708. ((org-edit-fixed-width-region))
  15709. ((org-at-table.el-p)
  15710. (org-edit-src-code))
  15711. ((org-at-table-p)
  15712. (call-interactively 'org-table-edit-formulas))
  15713. (t (call-interactively 'ffap))))
  15714. (defun org-ctrl-c-ctrl-c (&optional arg)
  15715. "Set tags in headline, or update according to changed information at point.
  15716. This command does many different things, depending on context:
  15717. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15718. this is what we do.
  15719. - If the cursor is on a statistics cookie, update it.
  15720. - If the cursor is in a headline, prompt for tags and insert them
  15721. into the current line, aligned to `org-tags-column'. When called
  15722. with prefix arg, realign all tags in the current buffer.
  15723. - If the cursor is in one of the special #+KEYWORD lines, this
  15724. triggers scanning the buffer for these lines and updating the
  15725. information.
  15726. - If the cursor is inside a table, realign the table. This command
  15727. works even if the automatic table editor has been turned off.
  15728. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15729. the entire table.
  15730. - If the cursor is at a footnote reference or definition, jump to
  15731. the corresponding definition or references, respectively.
  15732. - If the cursor is a the beginning of a dynamic block, update it.
  15733. - If the current buffer is a capture buffer, close note and file it.
  15734. - If the cursor is on a <<<target>>>, update radio targets and
  15735. corresponding links in this buffer.
  15736. - If the cursor is on a numbered item in a plain list, renumber the
  15737. ordered list.
  15738. - If the cursor is on a checkbox, toggle it.
  15739. - If the cursor is on a code block, evaluate it. The variable
  15740. `org-confirm-babel-evaluate' can be used to control prompting
  15741. before code block evaluation, by default every code block
  15742. evaluation requires confirmation. Code block evaluation can be
  15743. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15744. (interactive "P")
  15745. (let ((org-enable-table-editor t))
  15746. (cond
  15747. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15748. org-occur-highlights
  15749. org-latex-fragment-image-overlays)
  15750. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15751. (org-remove-occur-highlights)
  15752. (org-remove-latex-fragment-image-overlays)
  15753. (message "Temporary highlights/overlays removed from current buffer"))
  15754. ((and (local-variable-p 'org-finish-function (current-buffer))
  15755. (fboundp org-finish-function))
  15756. (funcall org-finish-function))
  15757. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15758. ((or (looking-at org-property-start-re)
  15759. (org-at-property-p))
  15760. (call-interactively 'org-property-action))
  15761. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15762. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15763. (or (org-on-heading-p) (org-at-item-p)))
  15764. (call-interactively 'org-update-statistics-cookies))
  15765. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15766. ((org-at-table.el-p)
  15767. (message "Use C-c ' to edit table.el tables"))
  15768. ((org-at-table-p)
  15769. (org-table-maybe-eval-formula)
  15770. (if arg
  15771. (call-interactively 'org-table-recalculate)
  15772. (org-table-maybe-recalculate-line))
  15773. (call-interactively 'org-table-align))
  15774. ((or (org-footnote-at-reference-p)
  15775. (org-footnote-at-definition-p))
  15776. (call-interactively 'org-footnote-action))
  15777. ((org-at-item-checkbox-p)
  15778. (call-interactively 'org-list-repair)
  15779. (call-interactively 'org-toggle-checkbox)
  15780. (org-list-send-list 'maybe))
  15781. ((org-at-item-p)
  15782. (call-interactively 'org-list-repair)
  15783. (when arg (call-interactively 'org-toggle-checkbox))
  15784. (org-list-send-list 'maybe))
  15785. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15786. ;; Dynamic block
  15787. (beginning-of-line 1)
  15788. (save-excursion (org-update-dblock)))
  15789. ((save-excursion
  15790. (beginning-of-line 1)
  15791. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15792. (cond
  15793. ((equal (match-string 1) "TBLFM")
  15794. ;; Recalculate the table before this line
  15795. (save-excursion
  15796. (beginning-of-line 1)
  15797. (skip-chars-backward " \r\n\t")
  15798. (if (org-at-table-p)
  15799. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15800. (t
  15801. (let ((org-inhibit-startup-visibility-stuff t)
  15802. (org-startup-align-all-tables nil))
  15803. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15804. (message "Local setup has been refreshed"))))
  15805. ((org-clock-update-time-maybe))
  15806. (t (error "C-c C-c can do nothing useful at this location")))))
  15807. (defun org-mode-restart ()
  15808. "Restart Org-mode, to scan again for special lines.
  15809. Also updates the keyword regular expressions."
  15810. (interactive)
  15811. (org-mode)
  15812. (message "Org-mode restarted"))
  15813. (defun org-kill-note-or-show-branches ()
  15814. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15815. (interactive)
  15816. (if (not org-finish-function)
  15817. (progn
  15818. (hide-subtree)
  15819. (call-interactively 'show-branches))
  15820. (let ((org-note-abort t))
  15821. (funcall org-finish-function))))
  15822. (defun org-return (&optional indent)
  15823. "Goto next table row or insert a newline.
  15824. Calls `org-table-next-row' or `newline', depending on context.
  15825. See the individual commands for more information."
  15826. (interactive)
  15827. (cond
  15828. ((bobp) (if indent (newline-and-indent) (newline)))
  15829. ((org-at-table-p)
  15830. (org-table-justify-field-maybe)
  15831. (call-interactively 'org-table-next-row))
  15832. ((and org-return-follows-link
  15833. (eq (get-text-property (point) 'face) 'org-link))
  15834. (call-interactively 'org-open-at-point))
  15835. ((and (org-at-heading-p)
  15836. (looking-at
  15837. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  15838. (org-show-entry)
  15839. (end-of-line 1)
  15840. (newline))
  15841. (t (if indent (newline-and-indent) (newline)))))
  15842. (defun org-return-indent ()
  15843. "Goto next table row or insert a newline and indent.
  15844. Calls `org-table-next-row' or `newline-and-indent', depending on
  15845. context. See the individual commands for more information."
  15846. (interactive)
  15847. (org-return t))
  15848. (defun org-ctrl-c-star ()
  15849. "Compute table, or change heading status of lines.
  15850. Calls `org-table-recalculate' or `org-toggle-heading',
  15851. depending on context."
  15852. (interactive)
  15853. (cond
  15854. ((org-at-table-p)
  15855. (call-interactively 'org-table-recalculate))
  15856. (t
  15857. ;; Convert all lines in region to list items
  15858. (call-interactively 'org-toggle-heading))))
  15859. (defun org-ctrl-c-minus ()
  15860. "Insert separator line in table or modify bullet status of line.
  15861. Also turns a plain line or a region of lines into list items.
  15862. Calls `org-table-insert-hline', `org-toggle-item', or
  15863. `org-cycle-list-bullet', depending on context."
  15864. (interactive)
  15865. (cond
  15866. ((org-at-table-p)
  15867. (call-interactively 'org-table-insert-hline))
  15868. ((org-region-active-p)
  15869. (call-interactively 'org-toggle-item))
  15870. ((org-in-item-p)
  15871. (call-interactively 'org-cycle-list-bullet))
  15872. (t
  15873. (call-interactively 'org-toggle-item))))
  15874. (defun org-toggle-item ()
  15875. "Convert headings or normal lines to items, items to normal lines.
  15876. If there is no active region, only the current line is considered.
  15877. If the first line in the region is a headline, convert all headlines to items.
  15878. If the first line in the region is an item, convert all items to normal lines.
  15879. If the first line is normal text, add an item bullet to each line."
  15880. (interactive)
  15881. (let (l2 l beg end)
  15882. (if (org-region-active-p)
  15883. (setq beg (region-beginning) end (region-end))
  15884. (setq beg (point-at-bol)
  15885. end (min (1+ (point-at-eol)) (point-max))))
  15886. (save-excursion
  15887. (goto-char end)
  15888. (setq l2 (org-current-line))
  15889. (goto-char beg)
  15890. (beginning-of-line 1)
  15891. (setq l (1- (org-current-line)))
  15892. (if (org-at-item-p)
  15893. ;; We already have items, de-itemize
  15894. (while (< (setq l (1+ l)) l2)
  15895. (when (org-at-item-p)
  15896. (skip-chars-forward " \t")
  15897. (delete-region (point) (match-end 0)))
  15898. (beginning-of-line 2))
  15899. (if (org-on-heading-p)
  15900. ;; Headings, convert to items
  15901. (while (< (setq l (1+ l)) l2)
  15902. (if (looking-at org-outline-regexp)
  15903. (replace-match (org-list-bullet-string "-") t t))
  15904. (beginning-of-line 2))
  15905. ;; normal lines, turn them into items
  15906. (while (< (setq l (1+ l)) l2)
  15907. (unless (org-at-item-p)
  15908. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15909. (replace-match
  15910. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  15911. (beginning-of-line 2)))))))
  15912. (defun org-toggle-heading (&optional nstars)
  15913. "Convert headings to normal text, or items or text to headings.
  15914. If there is no active region, only the current line is considered.
  15915. If the first line is a heading, remove the stars from all headlines
  15916. in the region.
  15917. If the first line is a plain list item, turn all plain list items
  15918. into headings.
  15919. If the first line is a normal line, turn each and every line in the
  15920. region into a heading.
  15921. When converting a line into a heading, the number of stars is chosen
  15922. such that the lines become children of the current entry. However,
  15923. when a prefix argument is given, its value determines the number of
  15924. stars to add."
  15925. (interactive "P")
  15926. (let (l2 l itemp beg end)
  15927. (if (org-region-active-p)
  15928. (setq beg (region-beginning) end (region-end))
  15929. (setq beg (point-at-bol)
  15930. end (min (1+ (point-at-eol)) (point-max))))
  15931. (save-excursion
  15932. (goto-char end)
  15933. (setq l2 (org-current-line))
  15934. (goto-char beg)
  15935. (beginning-of-line 1)
  15936. (setq l (1- (org-current-line)))
  15937. (if (org-on-heading-p)
  15938. ;; We already have headlines, de-star them
  15939. (while (< (setq l (1+ l)) l2)
  15940. (when (org-on-heading-p t)
  15941. (and (looking-at outline-regexp) (replace-match "")))
  15942. (beginning-of-line 2))
  15943. (setq itemp (org-at-item-p))
  15944. (let* ((stars
  15945. (if nstars
  15946. (make-string (prefix-numeric-value current-prefix-arg)
  15947. ?*)
  15948. (save-excursion
  15949. (if (re-search-backward org-complex-heading-regexp nil t)
  15950. (match-string 1) ""))))
  15951. (add-stars (cond (nstars "")
  15952. ((equal stars "") "*")
  15953. (org-odd-levels-only "**")
  15954. (t "*")))
  15955. (rpl (concat stars add-stars " ")))
  15956. (while (< (setq l (1+ l)) l2)
  15957. (if itemp
  15958. (and (org-at-item-p) (replace-match rpl t t))
  15959. (unless (org-on-heading-p)
  15960. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15961. (replace-match (concat rpl (match-string 2))))))
  15962. (beginning-of-line 2)))))))
  15963. (defun org-meta-return (&optional arg)
  15964. "Insert a new heading or wrap a region in a table.
  15965. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15966. See the individual commands for more information."
  15967. (interactive "P")
  15968. (cond
  15969. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15970. ((org-at-table-p)
  15971. (call-interactively 'org-table-wrap-region))
  15972. (t (call-interactively 'org-insert-heading))))
  15973. ;;; Menu entries
  15974. ;; Define the Org-mode menus
  15975. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15976. '("Tbl"
  15977. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15978. ["Next Field" org-cycle (org-at-table-p)]
  15979. ["Previous Field" org-shifttab (org-at-table-p)]
  15980. ["Next Row" org-return (org-at-table-p)]
  15981. "--"
  15982. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15983. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15984. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15985. "--"
  15986. ("Column"
  15987. ["Move Column Left" org-metaleft (org-at-table-p)]
  15988. ["Move Column Right" org-metaright (org-at-table-p)]
  15989. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15990. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15991. ("Row"
  15992. ["Move Row Up" org-metaup (org-at-table-p)]
  15993. ["Move Row Down" org-metadown (org-at-table-p)]
  15994. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15995. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15996. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15997. "--"
  15998. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15999. ("Rectangle"
  16000. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16001. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16002. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16003. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16004. "--"
  16005. ("Calculate"
  16006. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16007. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16008. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16009. "--"
  16010. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16011. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16012. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16013. "--"
  16014. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16015. "--"
  16016. ["Sum Column/Rectangle" org-table-sum
  16017. (or (org-at-table-p) (org-region-active-p))]
  16018. ["Which Column?" org-table-current-column (org-at-table-p)])
  16019. ["Debug Formulas"
  16020. org-table-toggle-formula-debugger
  16021. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16022. ["Show Col/Row Numbers"
  16023. org-table-toggle-coordinate-overlays
  16024. :style toggle
  16025. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16026. "--"
  16027. ["Create" org-table-create (and (not (org-at-table-p))
  16028. org-enable-table-editor)]
  16029. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16030. ["Import from File" org-table-import (not (org-at-table-p))]
  16031. ["Export to File" org-table-export (org-at-table-p)]
  16032. "--"
  16033. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16034. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16035. '("Org"
  16036. ("Show/Hide"
  16037. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16038. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16039. ["Sparse Tree..." org-sparse-tree t]
  16040. ["Reveal Context" org-reveal t]
  16041. ["Show All" show-all t]
  16042. "--"
  16043. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16044. "--"
  16045. ["New Heading" org-insert-heading t]
  16046. ("Navigate Headings"
  16047. ["Up" outline-up-heading t]
  16048. ["Next" outline-next-visible-heading t]
  16049. ["Previous" outline-previous-visible-heading t]
  16050. ["Next Same Level" outline-forward-same-level t]
  16051. ["Previous Same Level" outline-backward-same-level t]
  16052. "--"
  16053. ["Jump" org-goto t])
  16054. ("Edit Structure"
  16055. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16056. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16057. "--"
  16058. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16059. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16060. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16061. "--"
  16062. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16063. "--"
  16064. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16065. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16066. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16067. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16068. "--"
  16069. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16070. "--"
  16071. ["Convert to odd levels" org-convert-to-odd-levels t]
  16072. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16073. ("Editing"
  16074. ["Emphasis..." org-emphasize t]
  16075. ["Edit Source Example" org-edit-special t]
  16076. "--"
  16077. ["Footnote new/jump" org-footnote-action t]
  16078. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16079. ("Archive"
  16080. ["Archive (default method)" org-archive-subtree-default t]
  16081. "--"
  16082. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16083. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16084. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16085. )
  16086. "--"
  16087. ("Hyperlinks"
  16088. ["Store Link (Global)" org-store-link t]
  16089. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16090. ["Insert Link" org-insert-link t]
  16091. ["Follow Link" org-open-at-point t]
  16092. "--"
  16093. ["Next link" org-next-link t]
  16094. ["Previous link" org-previous-link t]
  16095. "--"
  16096. ["Descriptive Links"
  16097. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16098. :style radio
  16099. :selected (member '(org-link) buffer-invisibility-spec)]
  16100. ["Literal Links"
  16101. (progn
  16102. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16103. :style radio
  16104. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16105. "--"
  16106. ("TODO Lists"
  16107. ["TODO/DONE/-" org-todo t]
  16108. ("Select keyword"
  16109. ["Next keyword" org-shiftright (org-on-heading-p)]
  16110. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16111. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  16112. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16113. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16114. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16115. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16116. "--"
  16117. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16118. :selected org-enforce-todo-dependencies :style toggle :active t]
  16119. "Settings for tree at point"
  16120. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16121. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16122. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16123. ["Do Children parallel" org-toggle-ordered-property :style radio
  16124. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16125. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16126. "--"
  16127. ["Set Priority" org-priority t]
  16128. ["Priority Up" org-shiftup t]
  16129. ["Priority Down" org-shiftdown t]
  16130. "--"
  16131. ["Get news from all feeds" org-feed-update-all t]
  16132. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16133. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16134. ("TAGS and Properties"
  16135. ["Set Tags" org-set-tags-command t]
  16136. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16137. "--"
  16138. ["Set property" org-set-property t]
  16139. ["Column view of properties" org-columns t]
  16140. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16141. ("Dates and Scheduling"
  16142. ["Timestamp" org-time-stamp t]
  16143. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16144. ("Change Date"
  16145. ["1 Day Later" org-shiftright t]
  16146. ["1 Day Earlier" org-shiftleft t]
  16147. ["1 ... Later" org-shiftup t]
  16148. ["1 ... Earlier" org-shiftdown t])
  16149. ["Compute Time Range" org-evaluate-time-range t]
  16150. ["Schedule Item" org-schedule t]
  16151. ["Deadline" org-deadline t]
  16152. "--"
  16153. ["Custom time format" org-toggle-time-stamp-overlays
  16154. :style radio :selected org-display-custom-times]
  16155. "--"
  16156. ["Goto Calendar" org-goto-calendar t]
  16157. ["Date from Calendar" org-date-from-calendar t]
  16158. "--"
  16159. ["Start/Restart Timer" org-timer-start t]
  16160. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16161. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16162. ["Insert Timer String" org-timer t]
  16163. ["Insert Timer Item" org-timer-item t])
  16164. ("Logging work"
  16165. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16166. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16167. ["Clock out" org-clock-out t]
  16168. ["Clock cancel" org-clock-cancel t]
  16169. "--"
  16170. ["Mark as default task" org-clock-mark-default-task t]
  16171. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16172. ["Goto running clock" org-clock-goto t]
  16173. "--"
  16174. ["Display times" org-clock-display t]
  16175. ["Create clock table" org-clock-report t]
  16176. "--"
  16177. ["Record DONE time"
  16178. (progn (setq org-log-done (not org-log-done))
  16179. (message "Switching to %s will %s record a timestamp"
  16180. (car org-done-keywords)
  16181. (if org-log-done "automatically" "not")))
  16182. :style toggle :selected org-log-done])
  16183. "--"
  16184. ["Agenda Command..." org-agenda t]
  16185. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16186. ("File List for Agenda")
  16187. ("Special views current file"
  16188. ["TODO Tree" org-show-todo-tree t]
  16189. ["Check Deadlines" org-check-deadlines t]
  16190. ["Timeline" org-timeline t]
  16191. ["Tags/Property tree" org-match-sparse-tree t])
  16192. "--"
  16193. ["Export/Publish..." org-export t]
  16194. ("LaTeX"
  16195. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16196. :selected org-cdlatex-mode]
  16197. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16198. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16199. ["Modify math symbol" org-cdlatex-math-modify
  16200. (org-inside-LaTeX-fragment-p)]
  16201. ["Insert citation" org-reftex-citation t]
  16202. "--"
  16203. ["Template for BEAMER" org-insert-beamer-options-template t])
  16204. "--"
  16205. ("MobileOrg"
  16206. ["Push Files and Views" org-mobile-push t]
  16207. ["Get Captured and Flagged" org-mobile-pull t]
  16208. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16209. "--"
  16210. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16211. "--"
  16212. ("Documentation"
  16213. ["Show Version" org-version t]
  16214. ["Info Documentation" org-info t])
  16215. ("Customize"
  16216. ["Browse Org Group" org-customize t]
  16217. "--"
  16218. ["Expand This Menu" org-create-customize-menu
  16219. (fboundp 'customize-menu-create)])
  16220. ["Send bug report" org-submit-bug-report t]
  16221. "--"
  16222. ("Refresh/Reload"
  16223. ["Refresh setup current buffer" org-mode-restart t]
  16224. ["Reload Org (after update)" org-reload t]
  16225. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16226. ))
  16227. (defun org-info (&optional node)
  16228. "Read documentation for Org-mode in the info system.
  16229. With optional NODE, go directly to that node."
  16230. (interactive)
  16231. (info (format "(org)%s" (or node ""))))
  16232. ;;;###autoload
  16233. (defun org-submit-bug-report ()
  16234. "Submit a bug report on Org-mode via mail.
  16235. Don't hesitate to report any problems or inaccurate documentation.
  16236. If you don't have setup sending mail from (X)Emacs, please copy the
  16237. output buffer into your mail program, as it gives us important
  16238. information about your Org-mode version and configuration."
  16239. (interactive)
  16240. (require 'reporter)
  16241. (org-load-modules-maybe)
  16242. (org-require-autoloaded-modules)
  16243. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16244. (reporter-submit-bug-report
  16245. "emacs-orgmode@gnu.org"
  16246. (org-version)
  16247. (let (list)
  16248. (save-window-excursion
  16249. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16250. (delete-other-windows)
  16251. (erase-buffer)
  16252. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16253. We would like to add your full Org-mode and Outline configuration to the
  16254. bug report. This greatly simplifies the work of the maintainer and
  16255. other experts on the mailing list.
  16256. HOWEVER, some variables you have customized may contain private
  16257. information. The names of customers, colleagues, or friends, might
  16258. appear in the form of file names, tags, todo states, or search strings.
  16259. If you answer yes to the prompt, you might want to check and remove
  16260. such private information before sending the email.")
  16261. (add-text-properties (point-min) (point-max) '(face org-warning))
  16262. (when (yes-or-no-p "Include your Org-mode configuration ")
  16263. (mapatoms
  16264. (lambda (v)
  16265. (and (boundp v)
  16266. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16267. (or (and (symbol-value v)
  16268. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16269. (and
  16270. (get v 'custom-type) (get v 'standard-value)
  16271. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16272. (push v list)))))
  16273. (kill-buffer (get-buffer "*Warn about privacy*"))
  16274. list))
  16275. nil nil
  16276. "Remember to cover the basics, that is, what you expected to happen and
  16277. what in fact did happen. You don't know how to make a good report? See
  16278. http://orgmode.org/manual/Feedback.html#Feedback
  16279. Your bug report will be posted to the Org-mode mailing list.
  16280. ------------------------------------------------------------------------")
  16281. (save-excursion
  16282. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16283. (replace-match "\\1Bug: \\3 [\\2]")))))
  16284. (defun org-install-agenda-files-menu ()
  16285. (let ((bl (buffer-list)))
  16286. (save-excursion
  16287. (while bl
  16288. (set-buffer (pop bl))
  16289. (if (org-mode-p) (setq bl nil)))
  16290. (when (org-mode-p)
  16291. (easy-menu-change
  16292. '("Org") "File List for Agenda"
  16293. (append
  16294. (list
  16295. ["Edit File List" (org-edit-agenda-file-list) t]
  16296. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16297. ["Remove Current File from List" org-remove-file t]
  16298. ["Cycle through agenda files" org-cycle-agenda-files t]
  16299. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16300. "--")
  16301. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16302. ;;;; Documentation
  16303. ;;;###autoload
  16304. (defun org-require-autoloaded-modules ()
  16305. (interactive)
  16306. (mapc 'require
  16307. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16308. org-docbook org-exp org-html org-icalendar
  16309. org-id org-latex
  16310. org-publish org-remember org-table
  16311. org-timer org-xoxo)))
  16312. ;;;###autoload
  16313. (defun org-reload (&optional uncompiled)
  16314. "Reload all org lisp files.
  16315. With prefix arg UNCOMPILED, load the uncompiled versions."
  16316. (interactive "P")
  16317. (require 'find-func)
  16318. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16319. (dir-org (file-name-directory (org-find-library-name "org")))
  16320. (dir-org-contrib (ignore-errors
  16321. (file-name-directory
  16322. (org-find-library-name "org-contribdir"))))
  16323. (babel-files
  16324. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16325. (append (list nil "comint" "eval" "exp" "keys"
  16326. "lob" "ref" "table" "tangle")
  16327. (delq nil
  16328. (mapcar
  16329. (lambda (lang)
  16330. (when (cdr lang) (symbol-name (car lang))))
  16331. org-babel-load-languages)))))
  16332. (files
  16333. (append (directory-files dir-org t file-re)
  16334. babel-files
  16335. (and dir-org-contrib
  16336. (directory-files dir-org-contrib t file-re))))
  16337. (remove-re (concat (if (featurep 'xemacs)
  16338. "org-colview" "org-colview-xemacs")
  16339. "\\'")))
  16340. (setq files (mapcar 'file-name-sans-extension files))
  16341. (setq files (mapcar
  16342. (lambda (x) (if (string-match remove-re x) nil x))
  16343. files))
  16344. (setq files (delq nil files))
  16345. (mapc
  16346. (lambda (f)
  16347. (when (featurep (intern (file-name-nondirectory f)))
  16348. (if (and (not uncompiled)
  16349. (file-exists-p (concat f ".elc")))
  16350. (load (concat f ".elc") nil nil t)
  16351. (load (concat f ".el") nil nil t))))
  16352. files))
  16353. (org-version))
  16354. ;;;###autoload
  16355. (defun org-customize ()
  16356. "Call the customize function with org as argument."
  16357. (interactive)
  16358. (org-load-modules-maybe)
  16359. (org-require-autoloaded-modules)
  16360. (customize-browse 'org))
  16361. (defun org-create-customize-menu ()
  16362. "Create a full customization menu for Org-mode, insert it into the menu."
  16363. (interactive)
  16364. (org-load-modules-maybe)
  16365. (org-require-autoloaded-modules)
  16366. (if (fboundp 'customize-menu-create)
  16367. (progn
  16368. (easy-menu-change
  16369. '("Org") "Customize"
  16370. `(["Browse Org group" org-customize t]
  16371. "--"
  16372. ,(customize-menu-create 'org)
  16373. ["Set" Custom-set t]
  16374. ["Save" Custom-save t]
  16375. ["Reset to Current" Custom-reset-current t]
  16376. ["Reset to Saved" Custom-reset-saved t]
  16377. ["Reset to Standard Settings" Custom-reset-standard t]))
  16378. (message "\"Org\"-menu now contains full customization menu"))
  16379. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16380. ;;;; Miscellaneous stuff
  16381. ;;; Generally useful functions
  16382. (defun org-get-at-bol (property)
  16383. "Get text property PROPERTY at beginning of line."
  16384. (get-text-property (point-at-bol) property))
  16385. (defun org-find-text-property-in-string (prop s)
  16386. "Return the first non-nil value of property PROP in string S."
  16387. (or (get-text-property 0 prop s)
  16388. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16389. prop s)))
  16390. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16391. "Display the given MESSAGE as a warning."
  16392. (if (fboundp 'display-warning)
  16393. (display-warning 'org message
  16394. (if (featurep 'xemacs) 'warning :warning))
  16395. (let ((buf (get-buffer-create "*Org warnings*")))
  16396. (with-current-buffer buf
  16397. (goto-char (point-max))
  16398. (insert "Warning (Org): " message)
  16399. (unless (bolp)
  16400. (newline)))
  16401. (display-buffer buf)
  16402. (sit-for 0))))
  16403. (defun org-in-commented-line ()
  16404. "Is point in a line starting with `#'?"
  16405. (equal (char-after (point-at-bol)) ?#))
  16406. (defun org-in-indented-comment-line ()
  16407. "Is point in a line starting with `#' after some white space?"
  16408. (save-excursion
  16409. (save-match-data
  16410. (goto-char (point-at-bol))
  16411. (looking-at "[ \t]*#"))))
  16412. (defun org-in-verbatim-emphasis ()
  16413. (save-match-data
  16414. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16415. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16416. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16417. (if (and marker (marker-buffer marker)
  16418. (buffer-live-p (marker-buffer marker)))
  16419. (progn
  16420. (switch-to-buffer (marker-buffer marker))
  16421. (if (or (> marker (point-max)) (< marker (point-min)))
  16422. (widen))
  16423. (goto-char marker)
  16424. (org-show-context 'org-goto))
  16425. (if bookmark
  16426. (bookmark-jump bookmark)
  16427. (error "Cannot find location"))))
  16428. (defun org-quote-csv-field (s)
  16429. "Quote field for inclusion in CSV material."
  16430. (if (string-match "[\",]" s)
  16431. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16432. s))
  16433. (defun org-plist-delete (plist property)
  16434. "Delete PROPERTY from PLIST.
  16435. This is in contrast to merely setting it to 0."
  16436. (let (p)
  16437. (while plist
  16438. (if (not (eq property (car plist)))
  16439. (setq p (plist-put p (car plist) (nth 1 plist))))
  16440. (setq plist (cddr plist)))
  16441. p))
  16442. (defun org-force-self-insert (N)
  16443. "Needed to enforce self-insert under remapping."
  16444. (interactive "p")
  16445. (self-insert-command N))
  16446. (defun org-string-width (s)
  16447. "Compute width of string, ignoring invisible characters.
  16448. This ignores character with invisibility property `org-link', and also
  16449. characters with property `org-cwidth', because these will become invisible
  16450. upon the next fontification round."
  16451. (let (b l)
  16452. (when (or (eq t buffer-invisibility-spec)
  16453. (assq 'org-link buffer-invisibility-spec))
  16454. (while (setq b (text-property-any 0 (length s)
  16455. 'invisible 'org-link s))
  16456. (setq s (concat (substring s 0 b)
  16457. (substring s (or (next-single-property-change
  16458. b 'invisible s) (length s)))))))
  16459. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16460. (setq s (concat (substring s 0 b)
  16461. (substring s (or (next-single-property-change
  16462. b 'org-cwidth s) (length s))))))
  16463. (setq l (string-width s) b -1)
  16464. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16465. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16466. l))
  16467. (defun org-get-indentation (&optional line)
  16468. "Get the indentation of the current line, interpreting tabs.
  16469. When LINE is given, assume it represents a line and compute its indentation."
  16470. (if line
  16471. (if (string-match "^ *" (org-remove-tabs line))
  16472. (match-end 0))
  16473. (save-excursion
  16474. (beginning-of-line 1)
  16475. (skip-chars-forward " \t")
  16476. (current-column))))
  16477. (defun org-remove-tabs (s &optional width)
  16478. "Replace tabulators in S with spaces.
  16479. Assumes that s is a single line, starting in column 0."
  16480. (setq width (or width tab-width))
  16481. (while (string-match "\t" s)
  16482. (setq s (replace-match
  16483. (make-string
  16484. (- (* width (/ (+ (match-beginning 0) width) width))
  16485. (match-beginning 0)) ?\ )
  16486. t t s)))
  16487. s)
  16488. (defun org-fix-indentation (line ind)
  16489. "Fix indentation in LINE.
  16490. IND is a cons cell with target and minimum indentation.
  16491. If the current indentation in LINE is smaller than the minimum,
  16492. leave it alone. If it is larger than ind, set it to the target."
  16493. (let* ((l (org-remove-tabs line))
  16494. (i (org-get-indentation l))
  16495. (i1 (car ind)) (i2 (cdr ind)))
  16496. (if (>= i i2) (setq l (substring line i2)))
  16497. (if (> i1 0)
  16498. (concat (make-string i1 ?\ ) l)
  16499. l)))
  16500. (defun org-remove-indentation (code &optional n)
  16501. "Remove the maximum common indentation from the lines in CODE.
  16502. N may optionally be the number of spaces to remove."
  16503. (with-temp-buffer
  16504. (insert code)
  16505. (org-do-remove-indentation n)
  16506. (buffer-string)))
  16507. (defun org-do-remove-indentation (&optional n)
  16508. "Remove the maximum common indentation from the buffer."
  16509. (untabify (point-min) (point-max))
  16510. (let ((min 10000) re)
  16511. (if n
  16512. (setq min n)
  16513. (goto-char (point-min))
  16514. (while (re-search-forward "^ *[^ \n]" nil t)
  16515. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16516. (unless (or (= min 0) (= min 10000))
  16517. (setq re (format "^ \\{%d\\}" min))
  16518. (goto-char (point-min))
  16519. (while (re-search-forward re nil t)
  16520. (replace-match "")
  16521. (end-of-line 1))
  16522. min)))
  16523. (defun org-fill-template (template alist)
  16524. "Find each %key of ALIST in TEMPLATE and replace it."
  16525. (let ((case-fold-search nil)
  16526. entry key value)
  16527. (setq alist (sort (copy-sequence alist)
  16528. (lambda (a b) (< (length (car a)) (length (car b))))))
  16529. (while (setq entry (pop alist))
  16530. (setq template
  16531. (replace-regexp-in-string
  16532. (concat "%" (regexp-quote (car entry)))
  16533. (cdr entry) template t t)))
  16534. template))
  16535. (defun org-base-buffer (buffer)
  16536. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16537. (if (not buffer)
  16538. buffer
  16539. (or (buffer-base-buffer buffer)
  16540. buffer)))
  16541. (defun org-trim (s)
  16542. "Remove whitespace at beginning and end of string."
  16543. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16544. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16545. s)
  16546. (defun org-wrap (string &optional width lines)
  16547. "Wrap string to either a number of lines, or a width in characters.
  16548. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16549. that costs. If there is a word longer than WIDTH, the text is actually
  16550. wrapped to the length of that word.
  16551. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16552. many lines, whatever width that takes.
  16553. The return value is a list of lines, without newlines at the end."
  16554. (let* ((words (org-split-string string "[ \t\n]+"))
  16555. (maxword (apply 'max (mapcar 'org-string-width words)))
  16556. w ll)
  16557. (cond (width
  16558. (org-do-wrap words (max maxword width)))
  16559. (lines
  16560. (setq w maxword)
  16561. (setq ll (org-do-wrap words maxword))
  16562. (if (<= (length ll) lines)
  16563. ll
  16564. (setq ll words)
  16565. (while (> (length ll) lines)
  16566. (setq w (1+ w))
  16567. (setq ll (org-do-wrap words w)))
  16568. ll))
  16569. (t (error "Cannot wrap this")))))
  16570. (defun org-do-wrap (words width)
  16571. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16572. (let (lines line)
  16573. (while words
  16574. (setq line (pop words))
  16575. (while (and words (< (+ (length line) (length (car words))) width))
  16576. (setq line (concat line " " (pop words))))
  16577. (setq lines (push line lines)))
  16578. (nreverse lines)))
  16579. (defun org-split-string (string &optional separators)
  16580. "Splits STRING into substrings at SEPARATORS.
  16581. No empty strings are returned if there are matches at the beginning
  16582. and end of string."
  16583. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16584. (start 0)
  16585. notfirst
  16586. (list nil))
  16587. (while (and (string-match rexp string
  16588. (if (and notfirst
  16589. (= start (match-beginning 0))
  16590. (< start (length string)))
  16591. (1+ start) start))
  16592. (< (match-beginning 0) (length string)))
  16593. (setq notfirst t)
  16594. (or (eq (match-beginning 0) 0)
  16595. (and (eq (match-beginning 0) (match-end 0))
  16596. (eq (match-beginning 0) start))
  16597. (setq list
  16598. (cons (substring string start (match-beginning 0))
  16599. list)))
  16600. (setq start (match-end 0)))
  16601. (or (eq start (length string))
  16602. (setq list
  16603. (cons (substring string start)
  16604. list)))
  16605. (nreverse list)))
  16606. (defun org-quote-vert (s)
  16607. "Replace \"|\" with \"\\vert\"."
  16608. (while (string-match "|" s)
  16609. (setq s (replace-match "\\vert" t t s)))
  16610. s)
  16611. (defun org-uuidgen-p (s)
  16612. "Is S an ID created by UUIDGEN?"
  16613. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16614. (defun org-context ()
  16615. "Return a list of contexts of the current cursor position.
  16616. If several contexts apply, all are returned.
  16617. Each context entry is a list with a symbol naming the context, and
  16618. two positions indicating start and end of the context. Possible
  16619. contexts are:
  16620. :headline anywhere in a headline
  16621. :headline-stars on the leading stars in a headline
  16622. :todo-keyword on a TODO keyword (including DONE) in a headline
  16623. :tags on the TAGS in a headline
  16624. :priority on the priority cookie in a headline
  16625. :item on the first line of a plain list item
  16626. :item-bullet on the bullet/number of a plain list item
  16627. :checkbox on the checkbox in a plain list item
  16628. :table in an org-mode table
  16629. :table-special on a special filed in a table
  16630. :table-table in a table.el table
  16631. :link on a hyperlink
  16632. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16633. :target on a <<target>>
  16634. :radio-target on a <<<radio-target>>>
  16635. :latex-fragment on a LaTeX fragment
  16636. :latex-preview on a LaTeX fragment with overlayed preview image
  16637. This function expects the position to be visible because it uses font-lock
  16638. faces as a help to recognize the following contexts: :table-special, :link,
  16639. and :keyword."
  16640. (let* ((f (get-text-property (point) 'face))
  16641. (faces (if (listp f) f (list f)))
  16642. (p (point)) clist o)
  16643. ;; First the large context
  16644. (cond
  16645. ((org-on-heading-p t)
  16646. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16647. (when (progn
  16648. (beginning-of-line 1)
  16649. (looking-at org-todo-line-tags-regexp))
  16650. (push (org-point-in-group p 1 :headline-stars) clist)
  16651. (push (org-point-in-group p 2 :todo-keyword) clist)
  16652. (push (org-point-in-group p 4 :tags) clist))
  16653. (goto-char p)
  16654. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16655. (if (looking-at "\\[#[A-Z0-9]\\]")
  16656. (push (org-point-in-group p 0 :priority) clist)))
  16657. ((org-at-item-p)
  16658. (push (org-point-in-group p 2 :item-bullet) clist)
  16659. (push (list :item (point-at-bol)
  16660. (save-excursion (org-end-of-item) (point)))
  16661. clist)
  16662. (and (org-at-item-checkbox-p)
  16663. (push (org-point-in-group p 0 :checkbox) clist)))
  16664. ((org-at-table-p)
  16665. (push (list :table (org-table-begin) (org-table-end)) clist)
  16666. (if (memq 'org-formula faces)
  16667. (push (list :table-special
  16668. (previous-single-property-change p 'face)
  16669. (next-single-property-change p 'face)) clist)))
  16670. ((org-at-table-p 'any)
  16671. (push (list :table-table) clist)))
  16672. (goto-char p)
  16673. ;; Now the small context
  16674. (cond
  16675. ((org-at-timestamp-p)
  16676. (push (org-point-in-group p 0 :timestamp) clist))
  16677. ((memq 'org-link faces)
  16678. (push (list :link
  16679. (previous-single-property-change p 'face)
  16680. (next-single-property-change p 'face)) clist))
  16681. ((memq 'org-special-keyword faces)
  16682. (push (list :keyword
  16683. (previous-single-property-change p 'face)
  16684. (next-single-property-change p 'face)) clist))
  16685. ((org-on-target-p)
  16686. (push (org-point-in-group p 0 :target) clist)
  16687. (goto-char (1- (match-beginning 0)))
  16688. (if (looking-at org-radio-target-regexp)
  16689. (push (org-point-in-group p 0 :radio-target) clist))
  16690. (goto-char p))
  16691. ((setq o (car (delq nil
  16692. (mapcar
  16693. (lambda (x)
  16694. (if (memq x org-latex-fragment-image-overlays) x))
  16695. (overlays-at (point))))))
  16696. (push (list :latex-fragment
  16697. (overlay-start o) (overlay-end o)) clist)
  16698. (push (list :latex-preview
  16699. (overlay-start o) (overlay-end o)) clist))
  16700. ((org-inside-LaTeX-fragment-p)
  16701. ;; FIXME: positions wrong.
  16702. (push (list :latex-fragment (point) (point)) clist)))
  16703. (setq clist (nreverse (delq nil clist)))
  16704. clist))
  16705. ;; FIXME: Compare with at-regexp-p Do we need both?
  16706. (defun org-in-regexp (re &optional nlines visually)
  16707. "Check if point is inside a match of regexp.
  16708. Normally only the current line is checked, but you can include NLINES extra
  16709. lines both before and after point into the search.
  16710. If VISUALLY is set, require that the cursor is not after the match but
  16711. really on, so that the block visually is on the match."
  16712. (catch 'exit
  16713. (let ((pos (point))
  16714. (eol (point-at-eol (+ 1 (or nlines 0))))
  16715. (inc (if visually 1 0)))
  16716. (save-excursion
  16717. (beginning-of-line (- 1 (or nlines 0)))
  16718. (while (re-search-forward re eol t)
  16719. (if (and (<= (match-beginning 0) pos)
  16720. (>= (+ inc (match-end 0)) pos))
  16721. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16722. (defun org-at-regexp-p (regexp)
  16723. "Is point inside a match of REGEXP in the current line?"
  16724. (catch 'exit
  16725. (save-excursion
  16726. (let ((pos (point)) (end (point-at-eol)))
  16727. (beginning-of-line 1)
  16728. (while (re-search-forward regexp end t)
  16729. (if (and (<= (match-beginning 0) pos)
  16730. (>= (match-end 0) pos))
  16731. (throw 'exit t)))
  16732. nil))))
  16733. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  16734. "Returns t if the current point is between matches of START-RE and END-RE.
  16735. This will also return t if point is on one of the two matches or
  16736. in an unfinished block. END-RE can be a string or a form
  16737. returning a string.
  16738. An optional third argument bounds the search for START-RE. It
  16739. defaults to previous heading or `point-min'."
  16740. (let ((pos (point))
  16741. (limit (or bound (save-excursion (outline-previous-heading)))))
  16742. (save-excursion
  16743. ;; we're on a block when point is on start-re...
  16744. (or (org-at-regexp-p start-re)
  16745. ;; ... or start-re can be found above...
  16746. (and (re-search-backward start-re limit t)
  16747. ;; ... but no end-re between start-re and point.
  16748. (not (re-search-forward (eval end-re) pos t)))))))
  16749. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16750. "Call `multi-occur' with buffers for all agenda files."
  16751. (interactive "sOrg-files matching: \np")
  16752. (let* ((files (org-agenda-files))
  16753. (tnames (mapcar 'file-truename files))
  16754. (extra org-agenda-text-search-extra-files)
  16755. f)
  16756. (when (eq (car extra) 'agenda-archives)
  16757. (setq extra (cdr extra))
  16758. (setq files (org-add-archive-files files)))
  16759. (while (setq f (pop extra))
  16760. (unless (member (file-truename f) tnames)
  16761. (add-to-list 'files f 'append)
  16762. (add-to-list 'tnames (file-truename f) 'append)))
  16763. (multi-occur
  16764. (mapcar (lambda (x)
  16765. (with-current-buffer
  16766. (or (get-file-buffer x) (find-file-noselect x))
  16767. (widen)
  16768. (current-buffer)))
  16769. files)
  16770. regexp)))
  16771. (if (boundp 'occur-mode-find-occurrence-hook)
  16772. ;; Emacs 23
  16773. (add-hook 'occur-mode-find-occurrence-hook
  16774. (lambda ()
  16775. (when (org-mode-p)
  16776. (org-reveal))))
  16777. ;; Emacs 22
  16778. (defadvice occur-mode-goto-occurrence
  16779. (after org-occur-reveal activate)
  16780. (and (org-mode-p) (org-reveal)))
  16781. (defadvice occur-mode-goto-occurrence-other-window
  16782. (after org-occur-reveal activate)
  16783. (and (org-mode-p) (org-reveal)))
  16784. (defadvice occur-mode-display-occurrence
  16785. (after org-occur-reveal activate)
  16786. (when (org-mode-p)
  16787. (let ((pos (occur-mode-find-occurrence)))
  16788. (with-current-buffer (marker-buffer pos)
  16789. (save-excursion
  16790. (goto-char pos)
  16791. (org-reveal)))))))
  16792. (defun org-occur-link-in-agenda-files ()
  16793. "Create a link and search for it in the agendas.
  16794. The link is not stored in `org-stored-links', it is just created
  16795. for the search purpose."
  16796. (interactive)
  16797. (let ((link (condition-case nil
  16798. (org-store-link nil)
  16799. (error "Unable to create a link to here"))))
  16800. (org-occur-in-agenda-files (regexp-quote link))))
  16801. (defun org-uniquify (list)
  16802. "Remove duplicate elements from LIST."
  16803. (let (res)
  16804. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16805. res))
  16806. (defun org-delete-all (elts list)
  16807. "Remove all elements in ELTS from LIST."
  16808. (while elts
  16809. (setq list (delete (pop elts) list)))
  16810. list)
  16811. (defun org-count (cl-item cl-seq)
  16812. "Count the number of occurrences of ITEM in SEQ.
  16813. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16814. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16815. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16816. (while (< cl-start cl-end)
  16817. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16818. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16819. (setq cl-start (1+ cl-start)))
  16820. cl-count))
  16821. (defun org-remove-if (predicate seq)
  16822. "Remove everything from SEQ that fulfills PREDICATE."
  16823. (let (res e)
  16824. (while seq
  16825. (setq e (pop seq))
  16826. (if (not (funcall predicate e)) (push e res)))
  16827. (nreverse res)))
  16828. (defun org-remove-if-not (predicate seq)
  16829. "Remove everything from SEQ that does not fulfill PREDICATE."
  16830. (let (res e)
  16831. (while seq
  16832. (setq e (pop seq))
  16833. (if (funcall predicate e) (push e res)))
  16834. (nreverse res)))
  16835. (defun org-back-over-empty-lines ()
  16836. "Move backwards over whitespace, to the beginning of the first empty line.
  16837. Returns the number of empty lines passed."
  16838. (let ((pos (point)))
  16839. (skip-chars-backward " \t\n\r")
  16840. (beginning-of-line 2)
  16841. (goto-char (min (point) pos))
  16842. (count-lines (point) pos)))
  16843. (defun org-skip-whitespace ()
  16844. (skip-chars-forward " \t\n\r"))
  16845. (defun org-point-in-group (point group &optional context)
  16846. "Check if POINT is in match-group GROUP.
  16847. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16848. match. If the match group does not exist or point is not inside it,
  16849. return nil."
  16850. (and (match-beginning group)
  16851. (>= point (match-beginning group))
  16852. (<= point (match-end group))
  16853. (if context
  16854. (list context (match-beginning group) (match-end group))
  16855. t)))
  16856. (defun org-switch-to-buffer-other-window (&rest args)
  16857. "Switch to buffer in a second window on the current frame.
  16858. In particular, do not allow pop-up frames.
  16859. Returns the newly created buffer."
  16860. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16861. special-display-function)
  16862. (apply 'switch-to-buffer-other-window args)))
  16863. (defun org-combine-plists (&rest plists)
  16864. "Create a single property list from all plists in PLISTS.
  16865. The process starts by copying the first list, and then setting properties
  16866. from the other lists. Settings in the last list are the most significant
  16867. ones and overrule settings in the other lists."
  16868. (let ((rtn (copy-sequence (pop plists)))
  16869. p v ls)
  16870. (while plists
  16871. (setq ls (pop plists))
  16872. (while ls
  16873. (setq p (pop ls) v (pop ls))
  16874. (setq rtn (plist-put rtn p v))))
  16875. rtn))
  16876. (defun org-move-line-down (arg)
  16877. "Move the current line down. With prefix argument, move it past ARG lines."
  16878. (interactive "p")
  16879. (let ((col (current-column))
  16880. beg end pos)
  16881. (beginning-of-line 1) (setq beg (point))
  16882. (beginning-of-line 2) (setq end (point))
  16883. (beginning-of-line (+ 1 arg))
  16884. (setq pos (move-marker (make-marker) (point)))
  16885. (insert (delete-and-extract-region beg end))
  16886. (goto-char pos)
  16887. (org-move-to-column col)))
  16888. (defun org-move-line-up (arg)
  16889. "Move the current line up. With prefix argument, move it past ARG lines."
  16890. (interactive "p")
  16891. (let ((col (current-column))
  16892. beg end pos)
  16893. (beginning-of-line 1) (setq beg (point))
  16894. (beginning-of-line 2) (setq end (point))
  16895. (beginning-of-line (- arg))
  16896. (setq pos (move-marker (make-marker) (point)))
  16897. (insert (delete-and-extract-region beg end))
  16898. (goto-char pos)
  16899. (org-move-to-column col)))
  16900. (defun org-replace-escapes (string table)
  16901. "Replace %-escapes in STRING with values in TABLE.
  16902. TABLE is an association list with keys like \"%a\" and string values.
  16903. The sequences in STRING may contain normal field width and padding information,
  16904. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16905. so values can contain further %-escapes if they are define later in TABLE."
  16906. (let ((tbl (copy-alist table))
  16907. (case-fold-search nil)
  16908. (pchg 0)
  16909. e re rpl)
  16910. (while (setq e (pop tbl))
  16911. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16912. (when (and (cdr e) (string-match re (cdr e)))
  16913. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16914. (safe "SREF"))
  16915. (add-text-properties 0 3 (list 'sref sref) safe)
  16916. (setcdr e (replace-match safe t t (cdr e)))))
  16917. (while (string-match re string)
  16918. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16919. (cdr e)))
  16920. (setq string (replace-match rpl t t string))))
  16921. (while (setq pchg (next-property-change pchg string))
  16922. (let ((sref (get-text-property pchg 'sref string)))
  16923. (when (and sref (string-match "SREF" string pchg))
  16924. (setq string (replace-match sref t t string)))))
  16925. string))
  16926. (defun org-sublist (list start end)
  16927. "Return a section of LIST, from START to END.
  16928. Counting starts at 1."
  16929. (let (rtn (c start))
  16930. (setq list (nthcdr (1- start) list))
  16931. (while (and list (<= c end))
  16932. (push (pop list) rtn)
  16933. (setq c (1+ c)))
  16934. (nreverse rtn)))
  16935. (defun org-find-base-buffer-visiting (file)
  16936. "Like `find-buffer-visiting' but always return the base buffer and
  16937. not an indirect buffer."
  16938. (let ((buf (or (get-file-buffer file)
  16939. (find-buffer-visiting file))))
  16940. (if buf
  16941. (or (buffer-base-buffer buf) buf)
  16942. nil)))
  16943. (defun org-image-file-name-regexp (&optional extensions)
  16944. "Return regexp matching the file names of images.
  16945. If EXTENSIONS is given, only match these."
  16946. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16947. (image-file-name-regexp)
  16948. (let ((image-file-name-extensions
  16949. (or extensions
  16950. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16951. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16952. (concat "\\."
  16953. (regexp-opt (nconc (mapcar 'upcase
  16954. image-file-name-extensions)
  16955. image-file-name-extensions)
  16956. t)
  16957. "\\'"))))
  16958. (defun org-file-image-p (file &optional extensions)
  16959. "Return non-nil if FILE is an image."
  16960. (save-match-data
  16961. (string-match (org-image-file-name-regexp extensions) file)))
  16962. (defun org-get-cursor-date ()
  16963. "Return the date at cursor in as a time.
  16964. This works in the calendar and in the agenda, anywhere else it just
  16965. returns the current time."
  16966. (let (date day defd)
  16967. (cond
  16968. ((eq major-mode 'calendar-mode)
  16969. (setq date (calendar-cursor-to-date)
  16970. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16971. ((eq major-mode 'org-agenda-mode)
  16972. (setq day (get-text-property (point) 'day))
  16973. (if day
  16974. (setq date (calendar-gregorian-from-absolute day)
  16975. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16976. (nth 2 date))))))
  16977. (or defd (current-time))))
  16978. (defvar org-agenda-action-marker (make-marker)
  16979. "Marker pointing to the entry for the next agenda action.")
  16980. (defun org-mark-entry-for-agenda-action ()
  16981. "Mark the current entry as target of an agenda action.
  16982. Agenda actions are actions executed from the agenda with the key `k',
  16983. which make use of the date at the cursor."
  16984. (interactive)
  16985. (move-marker org-agenda-action-marker
  16986. (save-excursion (org-back-to-heading t) (point))
  16987. (current-buffer))
  16988. (message
  16989. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16990. ;;; Paragraph filling stuff.
  16991. ;; We want this to be just right, so use the full arsenal.
  16992. (defun org-indent-line-function ()
  16993. "Indent line like previous, but further if previous was headline or item."
  16994. (interactive)
  16995. (let* ((pos (point))
  16996. (itemp (org-at-item-p))
  16997. (case-fold-search t)
  16998. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16999. column bpos bcol tpos tcol bullet btype bullet-type)
  17000. ;; Find the previous relevant line
  17001. (beginning-of-line 1)
  17002. (cond
  17003. ;; Comments
  17004. ((looking-at "#") (setq column 0))
  17005. ;; Headings
  17006. ((looking-at "\\*+ ") (setq column 0))
  17007. ;; Drawers
  17008. ((and (looking-at "[ \t]*:END:")
  17009. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17010. (save-excursion
  17011. (goto-char (1- (match-beginning 1)))
  17012. (setq column (current-column))))
  17013. ;; Special blocks
  17014. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17015. (save-excursion
  17016. (re-search-backward
  17017. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17018. (setq column (org-get-indentation (match-string 0))))
  17019. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17020. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17021. (save-excursion
  17022. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17023. (setq column
  17024. (if (equal (downcase (match-string 1)) "src")
  17025. ;; src blocks: let `org-edit-src-exit' handle them
  17026. (org-get-indentation)
  17027. (org-get-indentation (match-string 0)))))
  17028. ;; Lists
  17029. ((org-in-item-p)
  17030. (org-beginning-of-item)
  17031. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  17032. (setq bpos (match-beginning 1) tpos (match-end 0)
  17033. bcol (progn (goto-char bpos) (current-column))
  17034. tcol (progn (goto-char tpos) (current-column))
  17035. bullet (match-string 1)
  17036. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  17037. (if (> tcol (+ bcol org-description-max-indent))
  17038. (setq tcol (+ bcol 5)))
  17039. (if (not itemp)
  17040. (setq column tcol)
  17041. (beginning-of-line 1)
  17042. (goto-char pos)
  17043. (if (looking-at "\\S-")
  17044. (progn
  17045. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  17046. (setq bullet (match-string 1)
  17047. btype (if (string-match "[0-9]" bullet) "n" bullet))
  17048. (setq column (if (equal btype bullet-type) bcol tcol)))
  17049. (setq column (org-get-indentation)))))
  17050. ;; This line has nothing special, look upside to get a clue about
  17051. ;; what to do.
  17052. (t
  17053. (beginning-of-line 0)
  17054. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  17055. (not (looking-at "[ \t]*:END:"))
  17056. (not (looking-at org-drawer-regexp)))
  17057. (beginning-of-line 0))
  17058. (cond
  17059. ;; There was an heading above.
  17060. ((looking-at "\\*+[ \t]+")
  17061. (if (not org-adapt-indentation)
  17062. (setq column 0)
  17063. (goto-char (match-end 0))
  17064. (setq column (current-column))))
  17065. ;; A drawer had started and is unfinished: indent consequently.
  17066. ((looking-at org-drawer-regexp)
  17067. (goto-char (1- (match-beginning 1)))
  17068. (setq column (current-column)))
  17069. ;; The drawer had ended: indent like its :END: line.
  17070. ((looking-at "\\([ \t]*\\):END:")
  17071. (goto-char (match-end 1))
  17072. (setq column (current-column)))
  17073. ;; There was a list that since ended: indent relatively to
  17074. ;; current heading.
  17075. ((org-in-item-p)
  17076. (outline-previous-heading)
  17077. (if (and org-adapt-indentation
  17078. (looking-at "\\*+[ \t]+"))
  17079. (progn
  17080. (goto-char (match-end 0))
  17081. (setq column (current-column)))
  17082. (setq column 0)))
  17083. ;; Else, nothing noticeable found: get indentation and go on.
  17084. (t (setq column (org-get-indentation))))))
  17085. (goto-char pos)
  17086. (if (<= (current-column) (current-indentation))
  17087. (org-indent-line-to column)
  17088. (save-excursion (org-indent-line-to column)))
  17089. (setq column (current-column))
  17090. (beginning-of-line 1)
  17091. (if (looking-at
  17092. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17093. (replace-match (concat (match-string 1)
  17094. (format org-property-format
  17095. (match-string 2) (match-string 3)))
  17096. t t))
  17097. (org-move-to-column column)))
  17098. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17099. "Variable to store copy of `adaptive-fill-regexp'.
  17100. Since `adaptive-fill-regexp' is set to never match, we need to
  17101. store a backup of its value before entering `org-mode' so that
  17102. the functionality can be provided as a fall-back.")
  17103. (defun org-set-autofill-regexps ()
  17104. (interactive)
  17105. ;; In the paragraph separator we include headlines, because filling
  17106. ;; text in a line directly attached to a headline would otherwise
  17107. ;; fill the headline as well.
  17108. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17109. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17110. ;; The paragraph starter includes hand-formatted lists.
  17111. (org-set-local
  17112. 'paragraph-start
  17113. (concat
  17114. "\f" "\\|"
  17115. "[ ]*$" "\\|"
  17116. "\\*+ " "\\|"
  17117. "[ \t]*#" "\\|"
  17118. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17119. "[ \t]*[:|]" "\\|"
  17120. "\\$\\$" "\\|"
  17121. "\\\\\\(begin\\|end\\|[][]\\)"))
  17122. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17123. ;; But only if the user has not turned off tables or fixed-width regions
  17124. (org-set-local
  17125. 'auto-fill-inhibit-regexp
  17126. (concat "\\*+ \\|#\\+"
  17127. "\\|[ \t]*" org-keyword-time-regexp
  17128. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17129. (concat
  17130. "\\|[ \t]*["
  17131. (if org-enable-table-editor "|" "")
  17132. (if org-enable-fixed-width-editor ":" "")
  17133. "]"))))
  17134. ;; We use our own fill-paragraph function, to make sure that tables
  17135. ;; and fixed-width regions are not wrapped. That function will pass
  17136. ;; through to `fill-paragraph' when appropriate.
  17137. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17138. ;; Adaptive filling: To get full control, first make sure that
  17139. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17140. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17141. (org-set-local 'org-adaptive-fill-regexp-backup
  17142. adaptive-fill-regexp))
  17143. (org-set-local 'adaptive-fill-regexp "\000")
  17144. (org-set-local 'adaptive-fill-function
  17145. 'org-adaptive-fill-function)
  17146. (org-set-local
  17147. 'align-mode-rules-list
  17148. '((org-in-buffer-settings
  17149. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17150. (modes . '(org-mode))))))
  17151. (defun org-fill-paragraph (&optional justify)
  17152. "Re-align a table, pass through to fill-paragraph if no table."
  17153. (let ((table-p (org-at-table-p))
  17154. (table.el-p (org-at-table.el-p)))
  17155. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17156. (save-excursion (goto-char (point-at-bol))
  17157. (looking-at outline-regexp)))
  17158. t) ; skip headlines
  17159. (table.el-p t) ; skip table.el tables
  17160. (table-p (org-table-align) t) ; align org-mode tables
  17161. (t nil)))) ; call paragraph-fill
  17162. ;; For reference, this is the default value of adaptive-fill-regexp
  17163. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17164. (defun org-adaptive-fill-function ()
  17165. "Return a fill prefix for org-mode files.
  17166. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17167. work correctly."
  17168. (cond
  17169. ;; Comment line
  17170. ((looking-at "#[ \t]+")
  17171. (match-string-no-properties 0))
  17172. ;; Description list
  17173. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17174. (save-excursion
  17175. (if (> (match-end 1) (+ (match-beginning 1)
  17176. org-description-max-indent))
  17177. (goto-char (+ (match-beginning 1) 5))
  17178. (goto-char (match-end 0)))
  17179. (make-string (current-column) ?\ )))
  17180. ;; Ordered or unordered list
  17181. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17182. (save-excursion
  17183. (goto-char (match-end 0))
  17184. (make-string (current-column) ?\ )))
  17185. ;; Other text
  17186. ((looking-at org-adaptive-fill-regexp-backup)
  17187. (match-string-no-properties 0))))
  17188. ;;; Other stuff.
  17189. (defun org-toggle-fixed-width-section (arg)
  17190. "Toggle the fixed-width export.
  17191. If there is no active region, the QUOTE keyword at the current headline is
  17192. inserted or removed. When present, it causes the text between this headline
  17193. and the next to be exported as fixed-width text, and unmodified.
  17194. If there is an active region, this command adds or removes a colon as the
  17195. first character of this line. If the first character of a line is a colon,
  17196. this line is also exported in fixed-width font."
  17197. (interactive "P")
  17198. (let* ((cc 0)
  17199. (regionp (org-region-active-p))
  17200. (beg (if regionp (region-beginning) (point)))
  17201. (end (if regionp (region-end)))
  17202. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17203. (case-fold-search nil)
  17204. (re "[ \t]*\\(: \\)")
  17205. off)
  17206. (if regionp
  17207. (save-excursion
  17208. (goto-char beg)
  17209. (setq cc (current-column))
  17210. (beginning-of-line 1)
  17211. (setq off (looking-at re))
  17212. (while (> nlines 0)
  17213. (setq nlines (1- nlines))
  17214. (beginning-of-line 1)
  17215. (cond
  17216. (arg
  17217. (org-move-to-column cc t)
  17218. (insert ": \n")
  17219. (forward-line -1))
  17220. ((and off (looking-at re))
  17221. (replace-match "" t t nil 1))
  17222. ((not off) (org-move-to-column cc t) (insert ": ")))
  17223. (forward-line 1)))
  17224. (save-excursion
  17225. (org-back-to-heading)
  17226. (if (looking-at (concat outline-regexp
  17227. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17228. (replace-match "" t t nil 1)
  17229. (if (looking-at outline-regexp)
  17230. (progn
  17231. (goto-char (match-end 0))
  17232. (insert org-quote-string " "))))))))
  17233. (defun org-reftex-citation ()
  17234. "Use reftex-citation to insert a citation into the buffer.
  17235. This looks for a line like
  17236. #+BIBLIOGRAPHY: foo plain option:-d
  17237. and derives from it that foo.bib is the bibliography file relevant
  17238. for this document. It then installs the necessary environment for RefTeX
  17239. to work in this buffer and calls `reftex-citation' to insert a citation
  17240. into the buffer.
  17241. Export of such citations to both LaTeX and HTML is handled by the contributed
  17242. package org-exp-bibtex by Taru Karttunen."
  17243. (interactive)
  17244. (let ((reftex-docstruct-symbol 'rds)
  17245. (reftex-cite-format "\\cite{%l}")
  17246. rds bib)
  17247. (save-excursion
  17248. (save-restriction
  17249. (widen)
  17250. (let ((case-fold-search t)
  17251. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17252. (if (not (save-excursion
  17253. (or (re-search-forward re nil t)
  17254. (re-search-backward re nil t))))
  17255. (error "No bibliography defined in file")
  17256. (setq bib (concat (match-string 1) ".bib")
  17257. rds (list (list 'bib bib)))))))
  17258. (call-interactively 'reftex-citation)))
  17259. ;;;; Functions extending outline functionality
  17260. (defun org-beginning-of-line (&optional arg)
  17261. "Go to the beginning of the current line. If that is invisible, continue
  17262. to a visible line beginning. This makes the function of C-a more intuitive.
  17263. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17264. first attempt, and only move to after the tags when the cursor is already
  17265. beyond the end of the headline."
  17266. (interactive "P")
  17267. (let ((pos (point))
  17268. (special (if (consp org-special-ctrl-a/e)
  17269. (car org-special-ctrl-a/e)
  17270. org-special-ctrl-a/e))
  17271. refpos)
  17272. (if (org-bound-and-true-p line-move-visual)
  17273. (beginning-of-visual-line 1)
  17274. (beginning-of-line 1))
  17275. (if (and arg (fboundp 'move-beginning-of-line))
  17276. (call-interactively 'move-beginning-of-line)
  17277. (if (bobp)
  17278. nil
  17279. (backward-char 1)
  17280. (if (org-truely-invisible-p)
  17281. (while (and (not (bobp)) (org-truely-invisible-p))
  17282. (backward-char 1)
  17283. (beginning-of-line 1))
  17284. (forward-char 1))))
  17285. (when special
  17286. (cond
  17287. ((and (looking-at org-complex-heading-regexp)
  17288. (= (char-after (match-end 1)) ?\ ))
  17289. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17290. (point-at-eol)))
  17291. (goto-char
  17292. (if (eq special t)
  17293. (cond ((> pos refpos) refpos)
  17294. ((= pos (point)) refpos)
  17295. (t (point)))
  17296. (cond ((> pos (point)) (point))
  17297. ((not (eq last-command this-command)) (point))
  17298. (t refpos)))))
  17299. ((org-at-item-p)
  17300. (goto-char
  17301. (if (eq special t)
  17302. (cond ((> pos (match-end 4)) (match-end 4))
  17303. ((= pos (point)) (match-end 4))
  17304. (t (point)))
  17305. (cond ((> pos (point)) (point))
  17306. ((not (eq last-command this-command)) (point))
  17307. (t (match-end 4))))))))
  17308. (org-no-warnings
  17309. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17310. (defun org-end-of-line (&optional arg)
  17311. "Go to the end of the line.
  17312. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17313. first attempt, and only move to after the tags when the cursor is already
  17314. beyond the end of the headline."
  17315. (interactive "P")
  17316. (let ((special (if (consp org-special-ctrl-a/e)
  17317. (cdr org-special-ctrl-a/e)
  17318. org-special-ctrl-a/e)))
  17319. (if (or (not special)
  17320. (not (org-on-heading-p))
  17321. arg)
  17322. (call-interactively
  17323. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17324. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17325. (t 'end-of-line)))
  17326. (let ((pos (point)))
  17327. (beginning-of-line 1)
  17328. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17329. (if (eq special t)
  17330. (if (or (< pos (match-beginning 1))
  17331. (= pos (match-end 0)))
  17332. (goto-char (match-beginning 1))
  17333. (goto-char (match-end 0)))
  17334. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17335. (goto-char (match-end 0))
  17336. (goto-char (match-beginning 1))))
  17337. (call-interactively (if (fboundp 'move-end-of-line)
  17338. 'move-end-of-line
  17339. 'end-of-line)))))
  17340. (org-no-warnings
  17341. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17342. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17343. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17344. (define-key org-mode-map [home] 'org-beginning-of-line)
  17345. (define-key org-mode-map [end] 'org-end-of-line)
  17346. (defun org-backward-sentence (&optional arg)
  17347. "Go to beginning of sentence, or beginning of table field.
  17348. This will call `backward-sentence' or `org-table-beginning-of-field',
  17349. depending on context."
  17350. (interactive "P")
  17351. (cond
  17352. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17353. (t (call-interactively 'backward-sentence))))
  17354. (defun org-forward-sentence (&optional arg)
  17355. "Go to end of sentence, or end of table field.
  17356. This will call `forward-sentence' or `org-table-end-of-field',
  17357. depending on context."
  17358. (interactive "P")
  17359. (cond
  17360. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17361. (t (call-interactively 'forward-sentence))))
  17362. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17363. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17364. (defun org-kill-line (&optional arg)
  17365. "Kill line, to tags or end of line."
  17366. (interactive "P")
  17367. (cond
  17368. ((or (not org-special-ctrl-k)
  17369. (bolp)
  17370. (not (org-on-heading-p)))
  17371. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17372. org-ctrl-k-protect-subtree)
  17373. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17374. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17375. (error "C-k aborted - would kill hidden subtree")))
  17376. (call-interactively 'kill-line))
  17377. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17378. (kill-region (point) (match-beginning 1))
  17379. (org-set-tags nil t))
  17380. (t (kill-region (point) (point-at-eol)))))
  17381. (define-key org-mode-map "\C-k" 'org-kill-line)
  17382. (defun org-yank (&optional arg)
  17383. "Yank. If the kill is a subtree, treat it specially.
  17384. This command will look at the current kill and check if is a single
  17385. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17386. cursor is at the beginning of a line or after the stars of a currently
  17387. empty headline, then the yank is handled specially. How exactly depends
  17388. on the value of the following variables, both set by default.
  17389. org-yank-folded-subtrees
  17390. When set, the subtree(s) will be folded after insertion, but only
  17391. if doing so would now swallow text after the yanked text.
  17392. org-yank-adjusted-subtrees
  17393. When set, the subtree will be promoted or demoted in order to
  17394. fit into the local outline tree structure, which means that the level
  17395. will be adjusted so that it becomes the smaller one of the two
  17396. *visible* surrounding headings.
  17397. Any prefix to this command will cause `yank' to be called directly with
  17398. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17399. will just
  17400. plainly yank the text as it is.
  17401. \[1] The test checks if the first non-white line is a heading
  17402. and if there are no other headings with fewer stars."
  17403. (interactive "P")
  17404. (org-yank-generic 'yank arg))
  17405. (defun org-yank-generic (command arg)
  17406. "Perform some yank-like command.
  17407. This function implements the behavior described in the `org-yank'
  17408. documentation. However, it has been generalized to work for any
  17409. interactive command with similar behavior."
  17410. ;; pretend to be command COMMAND
  17411. (setq this-command command)
  17412. (if arg
  17413. (call-interactively command)
  17414. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17415. (and (org-kill-is-subtree-p)
  17416. (or (bolp)
  17417. (and (looking-at "[ \t]*$")
  17418. (string-match
  17419. "\\`\\*+\\'"
  17420. (buffer-substring (point-at-bol) (point)))))))
  17421. swallowp)
  17422. (cond
  17423. ((and subtreep org-yank-folded-subtrees)
  17424. (let ((beg (point))
  17425. end)
  17426. (if (and subtreep org-yank-adjusted-subtrees)
  17427. (org-paste-subtree nil nil 'for-yank)
  17428. (call-interactively command))
  17429. (setq end (point))
  17430. (goto-char beg)
  17431. (when (and (bolp) subtreep
  17432. (not (setq swallowp
  17433. (org-yank-folding-would-swallow-text beg end))))
  17434. (or (looking-at outline-regexp)
  17435. (re-search-forward (concat "^" outline-regexp) end t))
  17436. (while (and (< (point) end) (looking-at outline-regexp))
  17437. (hide-subtree)
  17438. (org-cycle-show-empty-lines 'folded)
  17439. (condition-case nil
  17440. (outline-forward-same-level 1)
  17441. (error (goto-char end)))))
  17442. (when swallowp
  17443. (message
  17444. "Inserted text not folded because that would swallow text"))
  17445. (goto-char end)
  17446. (skip-chars-forward " \t\n\r")
  17447. (beginning-of-line 1)
  17448. (push-mark beg 'nomsg)))
  17449. ((and subtreep org-yank-adjusted-subtrees)
  17450. (let ((beg (point-at-bol)))
  17451. (org-paste-subtree nil nil 'for-yank)
  17452. (push-mark beg 'nomsg)))
  17453. (t
  17454. (call-interactively command))))))
  17455. (defun org-yank-folding-would-swallow-text (beg end)
  17456. "Would hide-subtree at BEG swallow any text after END?"
  17457. (let (level)
  17458. (save-excursion
  17459. (goto-char beg)
  17460. (when (or (looking-at outline-regexp)
  17461. (re-search-forward (concat "^" outline-regexp) end t))
  17462. (setq level (org-outline-level)))
  17463. (goto-char end)
  17464. (skip-chars-forward " \t\r\n\v\f")
  17465. (if (or (eobp)
  17466. (and (bolp) (looking-at org-outline-regexp)
  17467. (<= (org-outline-level) level)))
  17468. nil ; Nothing would be swallowed
  17469. t)))) ; something would swallow
  17470. (define-key org-mode-map "\C-y" 'org-yank)
  17471. (defun org-invisible-p ()
  17472. "Check if point is at a character currently not visible."
  17473. ;; Early versions of noutline don't have `outline-invisible-p'.
  17474. (if (fboundp 'outline-invisible-p)
  17475. (outline-invisible-p)
  17476. (get-char-property (point) 'invisible)))
  17477. (defun org-truely-invisible-p ()
  17478. "Check if point is at a character currently not visible.
  17479. This version does not only check the character property, but also
  17480. `visible-mode'."
  17481. ;; Early versions of noutline don't have `outline-invisible-p'.
  17482. (if (org-bound-and-true-p visible-mode)
  17483. nil
  17484. (if (fboundp 'outline-invisible-p)
  17485. (outline-invisible-p)
  17486. (get-char-property (point) 'invisible))))
  17487. (defun org-invisible-p2 ()
  17488. "Check if point is at a character currently not visible."
  17489. (save-excursion
  17490. (if (and (eolp) (not (bobp))) (backward-char 1))
  17491. ;; Early versions of noutline don't have `outline-invisible-p'.
  17492. (if (fboundp 'outline-invisible-p)
  17493. (outline-invisible-p)
  17494. (get-char-property (point) 'invisible))))
  17495. (defun org-back-to-heading (&optional invisible-ok)
  17496. "Call `outline-back-to-heading', but provide a better error message."
  17497. (condition-case nil
  17498. (outline-back-to-heading invisible-ok)
  17499. (error (error "Before first headline at position %d in buffer %s"
  17500. (point) (current-buffer)))))
  17501. (defun org-beginning-of-defun ()
  17502. "Go to the beginning of the subtree, i.e. back to the heading."
  17503. (org-back-to-heading))
  17504. (defun org-end-of-defun ()
  17505. "Go to the end of the subtree."
  17506. (org-end-of-subtree nil t))
  17507. (defun org-before-first-heading-p ()
  17508. "Before first heading?"
  17509. (save-excursion
  17510. (null (re-search-backward "^\\*+ " nil t))))
  17511. (defun org-on-heading-p (&optional ignored)
  17512. (outline-on-heading-p t))
  17513. (defun org-at-heading-p (&optional ignored)
  17514. (outline-on-heading-p t))
  17515. (defun org-point-at-end-of-empty-headline ()
  17516. "If point is at the end of an empty headline, return t, else nil.
  17517. If the heading only contains a TODO keyword, it is still still considered
  17518. empty."
  17519. (and (looking-at "[ \t]*$")
  17520. (save-excursion
  17521. (beginning-of-line 1)
  17522. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17523. "\\)?[ \t]*$")))))
  17524. (defun org-at-heading-or-item-p ()
  17525. (or (org-on-heading-p) (org-at-item-p)))
  17526. (defun org-on-target-p ()
  17527. (or (org-in-regexp org-radio-target-regexp)
  17528. (org-in-regexp org-target-regexp)))
  17529. (defun org-up-heading-all (arg)
  17530. "Move to the heading line of which the present line is a subheading.
  17531. This function considers both visible and invisible heading lines.
  17532. With argument, move up ARG levels."
  17533. (if (fboundp 'outline-up-heading-all)
  17534. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17535. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17536. (defun org-up-heading-safe ()
  17537. "Move to the heading line of which the present line is a subheading.
  17538. This version will not throw an error. It will return the level of the
  17539. headline found, or nil if no higher level is found.
  17540. Also, this function will be a lot faster than `outline-up-heading',
  17541. because it relies on stars being the outline starters. This can really
  17542. make a significant difference in outlines with very many siblings."
  17543. (let (start-level re)
  17544. (org-back-to-heading t)
  17545. (setq start-level (funcall outline-level))
  17546. (if (equal start-level 1)
  17547. nil
  17548. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17549. (if (re-search-backward re nil t)
  17550. (funcall outline-level)))))
  17551. (defun org-first-sibling-p ()
  17552. "Is this heading the first child of its parents?"
  17553. (interactive)
  17554. (let ((re (concat "^" outline-regexp))
  17555. level l)
  17556. (unless (org-at-heading-p t)
  17557. (error "Not at a heading"))
  17558. (setq level (funcall outline-level))
  17559. (save-excursion
  17560. (if (not (re-search-backward re nil t))
  17561. t
  17562. (setq l (funcall outline-level))
  17563. (< l level)))))
  17564. (defun org-goto-sibling (&optional previous)
  17565. "Goto the next sibling, even if it is invisible.
  17566. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17567. when a sibling was found. When none is found, return nil and don't
  17568. move point."
  17569. (let ((fun (if previous 're-search-backward 're-search-forward))
  17570. (pos (point))
  17571. (re (concat "^" outline-regexp))
  17572. level l)
  17573. (when (condition-case nil (org-back-to-heading t) (error nil))
  17574. (setq level (funcall outline-level))
  17575. (catch 'exit
  17576. (or previous (forward-char 1))
  17577. (while (funcall fun re nil t)
  17578. (setq l (funcall outline-level))
  17579. (when (< l level) (goto-char pos) (throw 'exit nil))
  17580. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17581. (goto-char pos)
  17582. nil))))
  17583. (defun org-show-siblings ()
  17584. "Show all siblings of the current headline."
  17585. (save-excursion
  17586. (while (org-goto-sibling) (org-flag-heading nil)))
  17587. (save-excursion
  17588. (while (org-goto-sibling 'previous)
  17589. (org-flag-heading nil))))
  17590. (defun org-show-hidden-entry ()
  17591. "Show an entry where even the heading is hidden."
  17592. (save-excursion
  17593. (org-show-entry)))
  17594. (defun org-flag-heading (flag &optional entry)
  17595. "Flag the current heading. FLAG non-nil means make invisible.
  17596. When ENTRY is non-nil, show the entire entry."
  17597. (save-excursion
  17598. (org-back-to-heading t)
  17599. ;; Check if we should show the entire entry
  17600. (if entry
  17601. (progn
  17602. (org-show-entry)
  17603. (save-excursion
  17604. (and (outline-next-heading)
  17605. (org-flag-heading nil))))
  17606. (outline-flag-region (max (point-min) (1- (point)))
  17607. (save-excursion (outline-end-of-heading) (point))
  17608. flag))))
  17609. (defun org-get-next-sibling ()
  17610. "Move to next heading of the same level, and return point.
  17611. If there is no such heading, return nil.
  17612. This is like outline-next-sibling, but invisible headings are ok."
  17613. (let ((level (funcall outline-level)))
  17614. (outline-next-heading)
  17615. (while (and (not (eobp)) (> (funcall outline-level) level))
  17616. (outline-next-heading))
  17617. (if (or (eobp) (< (funcall outline-level) level))
  17618. nil
  17619. (point))))
  17620. (defun org-get-last-sibling ()
  17621. "Move to previous heading of the same level, and return point.
  17622. If there is no such heading, return nil."
  17623. (let ((opoint (point))
  17624. (level (funcall outline-level)))
  17625. (outline-previous-heading)
  17626. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17627. (while (and (> (funcall outline-level) level)
  17628. (not (bobp)))
  17629. (outline-previous-heading))
  17630. (if (< (funcall outline-level) level)
  17631. nil
  17632. (point)))))
  17633. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17634. ;; This contains an exact copy of the original function, but it uses
  17635. ;; `org-back-to-heading', to make it work also in invisible
  17636. ;; trees. And is uses an invisible-OK argument.
  17637. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17638. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17639. ;; with many children and grandchildren etc, this can be much faster
  17640. ;; than the outline version.
  17641. (org-back-to-heading invisible-OK)
  17642. (let ((first t)
  17643. (level (funcall outline-level)))
  17644. (if (and (org-mode-p) (< level 1000))
  17645. ;; A true heading (not a plain list item), in Org-mode
  17646. ;; This means we can easily find the end by looking
  17647. ;; only for the right number of stars. Using a regexp to do
  17648. ;; this is so much faster than using a Lisp loop.
  17649. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17650. (forward-char 1)
  17651. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17652. ;; something else, do it the slow way
  17653. (while (and (not (eobp))
  17654. (or first (> (funcall outline-level) level)))
  17655. (setq first nil)
  17656. (outline-next-heading)))
  17657. (unless to-heading
  17658. (if (memq (preceding-char) '(?\n ?\^M))
  17659. (progn
  17660. ;; Go to end of line before heading
  17661. (forward-char -1)
  17662. (if (memq (preceding-char) '(?\n ?\^M))
  17663. ;; leave blank line before heading
  17664. (forward-char -1))))))
  17665. (point))
  17666. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17667. "Use Org version in org-mode, for dramatic speed-up."
  17668. (if (eq major-mode 'org-mode)
  17669. (progn
  17670. (org-end-of-subtree nil t)
  17671. (unless (eobp) (backward-char 1)))
  17672. ad-do-it))
  17673. (defun org-forward-same-level (arg &optional invisible-ok)
  17674. "Move forward to the arg'th subheading at same level as this one.
  17675. Stop at the first and last subheadings of a superior heading."
  17676. (interactive "p")
  17677. (org-back-to-heading invisible-ok)
  17678. (org-on-heading-p)
  17679. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17680. (re (format "^\\*\\{1,%d\\} " level))
  17681. l)
  17682. (forward-char 1)
  17683. (while (> arg 0)
  17684. (while (and (re-search-forward re nil 'move)
  17685. (setq l (- (match-end 0) (match-beginning 0) 1))
  17686. (= l level)
  17687. (not invisible-ok)
  17688. (progn (backward-char 1) (org-invisible-p)))
  17689. (if (< l level) (setq arg 1)))
  17690. (setq arg (1- arg)))
  17691. (beginning-of-line 1)))
  17692. (defun org-backward-same-level (arg &optional invisible-ok)
  17693. "Move backward to the arg'th subheading at same level as this one.
  17694. Stop at the first and last subheadings of a superior heading."
  17695. (interactive "p")
  17696. (org-back-to-heading)
  17697. (org-on-heading-p)
  17698. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17699. (re (format "^\\*\\{1,%d\\} " level))
  17700. l)
  17701. (while (> arg 0)
  17702. (while (and (re-search-backward re nil 'move)
  17703. (setq l (- (match-end 0) (match-beginning 0) 1))
  17704. (= l level)
  17705. (not invisible-ok)
  17706. (org-invisible-p))
  17707. (if (< l level) (setq arg 1)))
  17708. (setq arg (1- arg)))))
  17709. (defun org-show-subtree ()
  17710. "Show everything after this heading at deeper levels."
  17711. (outline-flag-region
  17712. (point)
  17713. (save-excursion
  17714. (org-end-of-subtree t t))
  17715. nil))
  17716. (defun org-show-entry ()
  17717. "Show the body directly following this heading.
  17718. Show the heading too, if it is currently invisible."
  17719. (interactive)
  17720. (save-excursion
  17721. (condition-case nil
  17722. (progn
  17723. (org-back-to-heading t)
  17724. (outline-flag-region
  17725. (max (point-min) (1- (point)))
  17726. (save-excursion
  17727. (if (re-search-forward
  17728. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17729. (match-beginning 1)
  17730. (point-max)))
  17731. nil)
  17732. (org-cycle-hide-drawers 'children))
  17733. (error nil))))
  17734. (defun org-make-options-regexp (kwds &optional extra)
  17735. "Make a regular expression for keyword lines."
  17736. (concat
  17737. "^"
  17738. "#?[ \t]*\\+\\("
  17739. (mapconcat 'regexp-quote kwds "\\|")
  17740. (if extra (concat "\\|" extra))
  17741. "\\):[ \t]*"
  17742. "\\(.*\\)"))
  17743. ;; Make isearch reveal the necessary context
  17744. (defun org-isearch-end ()
  17745. "Reveal context after isearch exits."
  17746. (when isearch-success ; only if search was successful
  17747. (if (featurep 'xemacs)
  17748. ;; Under XEmacs, the hook is run in the correct place,
  17749. ;; we directly show the context.
  17750. (org-show-context 'isearch)
  17751. ;; In Emacs the hook runs *before* restoring the overlays.
  17752. ;; So we have to use a one-time post-command-hook to do this.
  17753. ;; (Emacs 22 has a special variable, see function `org-mode')
  17754. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17755. isearch-mode-end-hook-quit)
  17756. ;; Only when the isearch was not quitted.
  17757. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17758. 'append 'local)))))
  17759. (defun org-isearch-post-command ()
  17760. "Remove self from hook, and show context."
  17761. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17762. (org-show-context 'isearch))
  17763. ;;;; Integration with and fixes for other packages
  17764. ;;; Imenu support
  17765. (defvar org-imenu-markers nil
  17766. "All markers currently used by Imenu.")
  17767. (make-variable-buffer-local 'org-imenu-markers)
  17768. (defun org-imenu-new-marker (&optional pos)
  17769. "Return a new marker for use by Imenu, and remember the marker."
  17770. (let ((m (make-marker)))
  17771. (move-marker m (or pos (point)))
  17772. (push m org-imenu-markers)
  17773. m))
  17774. (defun org-imenu-get-tree ()
  17775. "Produce the index for Imenu."
  17776. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17777. (setq org-imenu-markers nil)
  17778. (let* ((n org-imenu-depth)
  17779. (re (concat "^" outline-regexp))
  17780. (subs (make-vector (1+ n) nil))
  17781. (last-level 0)
  17782. m level head)
  17783. (save-excursion
  17784. (save-restriction
  17785. (widen)
  17786. (goto-char (point-max))
  17787. (while (re-search-backward re nil t)
  17788. (setq level (org-reduced-level (funcall outline-level)))
  17789. (when (<= level n)
  17790. (looking-at org-complex-heading-regexp)
  17791. (setq head (org-link-display-format
  17792. (org-match-string-no-properties 4))
  17793. m (org-imenu-new-marker))
  17794. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17795. (if (>= level last-level)
  17796. (push (cons head m) (aref subs level))
  17797. (push (cons head (aref subs (1+ level))) (aref subs level))
  17798. (loop for i from (1+ level) to n do (aset subs i nil)))
  17799. (setq last-level level)))))
  17800. (aref subs 1)))
  17801. (eval-after-load "imenu"
  17802. '(progn
  17803. (add-hook 'imenu-after-jump-hook
  17804. (lambda ()
  17805. (if (eq major-mode 'org-mode)
  17806. (org-show-context 'org-goto))))))
  17807. (defun org-link-display-format (link)
  17808. "Replace a link with either the description, or the link target
  17809. if no description is present"
  17810. (save-match-data
  17811. (if (string-match org-bracket-link-analytic-regexp link)
  17812. (replace-match (if (match-end 5)
  17813. (match-string 5 link)
  17814. (concat (match-string 1 link)
  17815. (match-string 3 link)))
  17816. nil t link)
  17817. link)))
  17818. ;; Speedbar support
  17819. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17820. "Overlay marking the agenda restriction line in speedbar.")
  17821. (overlay-put org-speedbar-restriction-lock-overlay
  17822. 'face 'org-agenda-restriction-lock)
  17823. (overlay-put org-speedbar-restriction-lock-overlay
  17824. 'help-echo "Agendas are currently limited to this item.")
  17825. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17826. (defun org-speedbar-set-agenda-restriction ()
  17827. "Restrict future agenda commands to the location at point in speedbar.
  17828. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17829. (interactive)
  17830. (require 'org-agenda)
  17831. (let (p m tp np dir txt)
  17832. (cond
  17833. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17834. 'org-imenu t))
  17835. (setq m (get-text-property p 'org-imenu-marker))
  17836. (with-current-buffer (marker-buffer m)
  17837. (goto-char m)
  17838. (org-agenda-set-restriction-lock 'subtree)))
  17839. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17840. 'speedbar-function 'speedbar-find-file))
  17841. (setq tp (previous-single-property-change
  17842. (1+ p) 'speedbar-function)
  17843. np (next-single-property-change
  17844. tp 'speedbar-function)
  17845. dir (speedbar-line-directory)
  17846. txt (buffer-substring-no-properties (or tp (point-min))
  17847. (or np (point-max))))
  17848. (with-current-buffer (find-file-noselect
  17849. (let ((default-directory dir))
  17850. (expand-file-name txt)))
  17851. (unless (org-mode-p)
  17852. (error "Cannot restrict to non-Org-mode file"))
  17853. (org-agenda-set-restriction-lock 'file)))
  17854. (t (error "Don't know how to restrict Org-mode's agenda")))
  17855. (move-overlay org-speedbar-restriction-lock-overlay
  17856. (point-at-bol) (point-at-eol))
  17857. (setq current-prefix-arg nil)
  17858. (org-agenda-maybe-redo)))
  17859. (eval-after-load "speedbar"
  17860. '(progn
  17861. (speedbar-add-supported-extension ".org")
  17862. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17863. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17864. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17865. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17866. (add-hook 'speedbar-visiting-tag-hook
  17867. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17868. ;;; Fixes and Hacks for problems with other packages
  17869. ;; Make flyspell not check words in links, to not mess up our keymap
  17870. (defun org-mode-flyspell-verify ()
  17871. "Don't let flyspell put overlays at active buttons."
  17872. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  17873. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  17874. (defun org-remove-flyspell-overlays-in (beg end)
  17875. "Remove flyspell overlays in region."
  17876. (and (org-bound-and-true-p flyspell-mode)
  17877. (fboundp 'flyspell-delete-region-overlays)
  17878. (flyspell-delete-region-overlays beg end))
  17879. (add-text-properties beg end '(org-no-flyspell t)))
  17880. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17881. (eval-after-load "bookmark"
  17882. '(if (boundp 'bookmark-after-jump-hook)
  17883. ;; We can use the hook
  17884. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17885. ;; Hook not available, use advice
  17886. (defadvice bookmark-jump (after org-make-visible activate)
  17887. "Make the position visible."
  17888. (org-bookmark-jump-unhide))))
  17889. ;; Make sure saveplace shows the location if it was hidden
  17890. (eval-after-load "saveplace"
  17891. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17892. "Make the position visible."
  17893. (org-bookmark-jump-unhide)))
  17894. ;; Make sure ecb shows the location if it was hidden
  17895. (eval-after-load "ecb"
  17896. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17897. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17898. (if (eq major-mode 'org-mode)
  17899. (org-show-context))))
  17900. (defun org-bookmark-jump-unhide ()
  17901. "Unhide the current position, to show the bookmark location."
  17902. (and (org-mode-p)
  17903. (or (org-invisible-p)
  17904. (save-excursion (goto-char (max (point-min) (1- (point))))
  17905. (org-invisible-p)))
  17906. (org-show-context 'bookmark-jump)))
  17907. ;; Make session.el ignore our circular variable
  17908. (eval-after-load "session"
  17909. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17910. ;;;; Experimental code
  17911. (defun org-closed-in-range ()
  17912. "Sparse tree of items closed in a certain time range.
  17913. Still experimental, may disappear in the future."
  17914. (interactive)
  17915. ;; Get the time interval from the user.
  17916. (let* ((time1 (org-float-time
  17917. (org-read-date nil 'to-time nil "Starting date: ")))
  17918. (time2 (org-float-time
  17919. (org-read-date nil 'to-time nil "End date:")))
  17920. ;; callback function
  17921. (callback (lambda ()
  17922. (let ((time
  17923. (org-float-time
  17924. (apply 'encode-time
  17925. (org-parse-time-string
  17926. (match-string 1))))))
  17927. ;; check if time in interval
  17928. (and (>= time time1) (<= time time2))))))
  17929. ;; make tree, check each match with the callback
  17930. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17931. ;;;; Finish up
  17932. (provide 'org)
  17933. (run-hooks 'org-load-hook)
  17934. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17935. ;;; org.el ends here