org.el 692 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553
  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: 6.35g
  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. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-entities)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. (require 'org-list)
  86. (require 'org-src)
  87. (require 'org-footnote)
  88. ;;;; Customization variables
  89. ;;; Version
  90. (defconst org-version "6.35g"
  91. "The version number of the file org.el.")
  92. (defun org-version (&optional here)
  93. "Show the org-mode version in the echo area.
  94. With prefix arg HERE, insert it at point."
  95. (interactive "P")
  96. (let* ((origin default-directory)
  97. (version org-version)
  98. (git-version)
  99. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  100. (when (and (file-exists-p (expand-file-name ".git" dir))
  101. (executable-find "git"))
  102. (unwind-protect
  103. (progn
  104. (cd dir)
  105. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  106. (with-current-buffer "*Shell Command Output*"
  107. (goto-char (point-min))
  108. (setq git-version (buffer-substring (point) (point-at-eol))))
  109. (subst-char-in-string ?- ?. git-version t)
  110. (when (string-match "\\S-"
  111. (shell-command-to-string
  112. "git diff-index --name-only HEAD --"))
  113. (setq git-version (concat git-version ".dirty")))
  114. (setq version (concat version " (" git-version ")"))))
  115. (cd origin)))
  116. (setq version (format "Org-mode version %s" version))
  117. (if here (insert version))
  118. (message version)))
  119. ;;; Compatibility constants
  120. ;;; The custom variables
  121. (defgroup org nil
  122. "Outline-based notes management and organizer."
  123. :tag "Org"
  124. :group 'outlines
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (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)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  171. (const :tag " docview: Links to doc-view buffers" org-docview)
  172. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  173. (const :tag " id: Global IDs for identifying entries" org-id)
  174. (const :tag " info: Links to Info nodes" org-info)
  175. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  176. (const :tag " habit: Track your consistency with habits" org-habit)
  177. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  178. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  179. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  180. (const :tag " mew Links to Mew folders/messages" org-mew)
  181. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  182. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  183. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  184. (const :tag " vm: Links to VM folders/messages" org-vm)
  185. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  186. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  187. (const :tag " mouse: Additional mouse support" org-mouse)
  188. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  189. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  200. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  201. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  202. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  203. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  204. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  205. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  206. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  207. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  208. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  209. (const :tag "C registry: A registry for Org-mode links" org-registry)
  210. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  211. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  212. (const :tag "C secretary: Team management with org-mode" org-secretary)
  213. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  214. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  215. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  216. (const :tag "C track: Keep up with Org-mode development" org-track)
  217. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  218. (defcustom org-support-shift-select nil
  219. "Non-nil means make shift-cursor commands select text when possible.
  220. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  221. selecting a region, or enlarge thusly regions started in this way.
  222. In Org-mode, in special contexts, these same keys are used for other
  223. purposes, important enough to compete with shift selection. Org tries
  224. to balance these needs by supporting `shift-select-mode' outside these
  225. special contexts, under control of this variable.
  226. The default of this variable is nil, to avoid confusing behavior. Shifted
  227. cursor keys will then execute Org commands in the following contexts:
  228. - on a headline, changing TODO state (left/right) and priority (up/down)
  229. - on a time stamp, changing the time
  230. - in a plain list item, changing the bullet type
  231. - in a property definition line, switching between allowed values
  232. - in the BEGIN line of a clock table (changing the time block).
  233. Outside these contexts, the commands will throw an error.
  234. When this variable is t and the cursor is not in a special context,
  235. Org-mode will support shift-selection for making and enlarging regions.
  236. To make this more effective, the bullet cycling will no longer happen
  237. anywhere in an item line, but only if the cursor is exactly on the bullet.
  238. If you set this variable to the symbol `always', then the keys
  239. will not be special in headlines, property lines, and item lines, to make
  240. shift selection work there as well. If this is what you want, you can
  241. use the following alternative commands: `C-c C-t' and `C-c ,' to
  242. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  243. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  244. edited by hand or in column view.
  245. However, when the cursor is on a timestamp, shift-cursor commands
  246. will still edit the time stamp - this is just too good to give up.
  247. XEmacs user should have this variable set to nil, because shift-select-mode
  248. is Emacs 23 only."
  249. :group 'org
  250. :type '(choice
  251. (const :tag "Never" nil)
  252. (const :tag "When outside special context" t)
  253. (const :tag "Everywhere except timestamps" always)))
  254. (defgroup org-startup nil
  255. "Options concerning startup of Org-mode."
  256. :tag "Org Startup"
  257. :group 'org)
  258. (defcustom org-startup-folded t
  259. "Non-nil means entering Org-mode will switch to OVERVIEW.
  260. This can also be configured on a per-file basis by adding one of
  261. the following lines anywhere in the buffer:
  262. #+STARTUP: fold (or `overview', this is equivalent)
  263. #+STARTUP: nofold (or `showall', this is equivalent)
  264. #+STARTUP: content
  265. #+STARTUP: showeverything"
  266. :group 'org-startup
  267. :type '(choice
  268. (const :tag "nofold: show all" nil)
  269. (const :tag "fold: overview" t)
  270. (const :tag "content: all headlines" content)
  271. (const :tag "show everything, even drawers" showeverything)))
  272. (defcustom org-startup-truncated t
  273. "Non-nil means entering Org-mode will set `truncate-lines'.
  274. This is useful since some lines containing links can be very long and
  275. uninteresting. Also tables look terrible when wrapped."
  276. :group 'org-startup
  277. :type 'boolean)
  278. (defcustom org-startup-indented nil
  279. "Non-nil means turn on `org-indent-mode' on startup.
  280. This can also be configured on a per-file basis by adding one of
  281. the following lines anywhere in the buffer:
  282. #+STARTUP: indent
  283. #+STARTUP: noindent"
  284. :group 'org-structure
  285. :type '(choice
  286. (const :tag "Not" nil)
  287. (const :tag "Globally (slow on startup in large files)" t)))
  288. (defcustom org-startup-with-beamer-mode nil
  289. "Non-nil means turn on `org-beamer-mode' on startup.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: beamer"
  293. :group 'org-startup
  294. :type 'boolean)
  295. (defcustom org-startup-align-all-tables nil
  296. "Non-nil means align all tables when visiting a file.
  297. This is useful when the column width in tables is forced with <N> cookies
  298. in table fields. Such tables will look correct only after the first re-align.
  299. This can also be configured on a per-file basis by adding one of
  300. the following lines anywhere in the buffer:
  301. #+STARTUP: align
  302. #+STARTUP: noalign"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-insert-mode-line-in-empty-file nil
  306. "Non-nil means insert the first line setting Org-mode in empty files.
  307. When the function `org-mode' is called interactively in an empty file, this
  308. normally means that the file name does not automatically trigger Org-mode.
  309. To ensure that the file will always be in Org-mode in the future, a
  310. line enforcing Org-mode will be inserted into the buffer, if this option
  311. has been set."
  312. :group 'org-startup
  313. :type 'boolean)
  314. (defcustom org-replace-disputed-keys nil
  315. "Non-nil means use alternative key bindings for some keys.
  316. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  317. These keys are also used by other packages like shift-selection-mode'
  318. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  319. If you want to use Org-mode together with one of these other modes,
  320. or more generally if you would like to move some Org-mode commands to
  321. other keys, set this variable and configure the keys with the variable
  322. `org-disputed-keys'.
  323. This option is only relevant at load-time of Org-mode, and must be set
  324. *before* org.el is loaded. Changing it requires a restart of Emacs to
  325. become effective."
  326. :group 'org-startup
  327. :type 'boolean)
  328. (defcustom org-use-extra-keys nil
  329. "Non-nil means use extra key sequence definitions for certain
  330. commands. This happens automatically if you run XEmacs or if
  331. window-system is nil. This variable lets you do the same
  332. manually. You must set it before loading org.
  333. Example: on Carbon Emacs 22 running graphically, with an external
  334. keyboard on a Powerbook, the default way of setting M-left might
  335. not work for either Alt or ESC. Setting this variable will make
  336. it work for ESC."
  337. :group 'org-startup
  338. :type 'boolean)
  339. (if (fboundp 'defvaralias)
  340. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  341. (defcustom org-disputed-keys
  342. '(([(shift up)] . [(meta p)])
  343. ([(shift down)] . [(meta n)])
  344. ([(shift left)] . [(meta -)])
  345. ([(shift right)] . [(meta +)])
  346. ([(control shift right)] . [(meta shift +)])
  347. ([(control shift left)] . [(meta shift -)]))
  348. "Keys for which Org-mode and other modes compete.
  349. This is an alist, cars are the default keys, second element specifies
  350. the alternative to use when `org-replace-disputed-keys' is t.
  351. Keys can be specified in any syntax supported by `define-key'.
  352. The value of this option takes effect only at Org-mode's startup,
  353. therefore you'll have to restart Emacs to apply it after changing."
  354. :group 'org-startup
  355. :type 'alist)
  356. (defun org-key (key)
  357. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  358. Or return the original if not disputed."
  359. (if org-replace-disputed-keys
  360. (let* ((nkey (key-description key))
  361. (x (org-find-if (lambda (x)
  362. (equal (key-description (car x)) nkey))
  363. org-disputed-keys)))
  364. (if x (cdr x) key))
  365. key))
  366. (defun org-find-if (predicate seq)
  367. (catch 'exit
  368. (while seq
  369. (if (funcall predicate (car seq))
  370. (throw 'exit (car seq))
  371. (pop seq)))))
  372. (defun org-defkey (keymap key def)
  373. "Define a key, possibly translated, as returned by `org-key'."
  374. (define-key keymap (org-key key) def))
  375. (defcustom org-ellipsis nil
  376. "The ellipsis to use in the Org-mode outline.
  377. When nil, just use the standard three dots. When a string, use that instead,
  378. When a face, use the standard 3 dots, but with the specified face.
  379. The change affects only Org-mode (which will then use its own display table).
  380. Changing this requires executing `M-x org-mode' in a buffer to become
  381. effective."
  382. :group 'org-startup
  383. :type '(choice (const :tag "Default" nil)
  384. (face :tag "Face" :value org-warning)
  385. (string :tag "String" :value "...#")))
  386. (defvar org-display-table nil
  387. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  388. (defgroup org-keywords nil
  389. "Keywords in Org-mode."
  390. :tag "Org Keywords"
  391. :group 'org)
  392. (defcustom org-deadline-string "DEADLINE:"
  393. "String to mark deadline entries.
  394. A deadline is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-deadline].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-scheduled-string "SCHEDULED:"
  401. "String to mark scheduled TODO entries.
  402. A schedule is this string, followed by a time stamp. Should be a word,
  403. terminated by a colon. You can insert a schedule keyword and
  404. a timestamp with \\[org-schedule].
  405. Changes become only effective after restarting Emacs."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-closed-string "CLOSED:"
  409. "String used as the prefix for timestamps logging closing a TODO entry."
  410. :group 'org-keywords
  411. :type 'string)
  412. (defcustom org-clock-string "CLOCK:"
  413. "String used as prefix for timestamps clocking work hours on an item."
  414. :group 'org-keywords
  415. :type 'string)
  416. (defcustom org-comment-string "COMMENT"
  417. "Entries starting with this keyword will never be exported.
  418. An entry can be toggled between COMMENT and normal with
  419. \\[org-toggle-comment].
  420. Changes become only effective after restarting Emacs."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defcustom org-quote-string "QUOTE"
  424. "Entries starting with this keyword will be exported in fixed-width font.
  425. Quoting applies only to the text in the entry following the headline, and does
  426. not extend beyond the next headline, even if that is lower level.
  427. An entry can be toggled between QUOTE and normal with
  428. \\[org-toggle-fixed-width-section]."
  429. :group 'org-keywords
  430. :type 'string)
  431. (defconst org-repeat-re
  432. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  433. "Regular expression for specifying repeated events.
  434. After a match, group 1 contains the repeat expression.")
  435. (defgroup org-structure nil
  436. "Options concerning the general structure of Org-mode files."
  437. :tag "Org Structure"
  438. :group 'org)
  439. (defgroup org-reveal-location nil
  440. "Options about how to make context of a location visible."
  441. :tag "Org Reveal Location"
  442. :group 'org-structure)
  443. (defconst org-context-choice
  444. '(choice
  445. (const :tag "Always" t)
  446. (const :tag "Never" nil)
  447. (repeat :greedy t :tag "Individual contexts"
  448. (cons
  449. (choice :tag "Context"
  450. (const agenda)
  451. (const org-goto)
  452. (const occur-tree)
  453. (const tags-tree)
  454. (const link-search)
  455. (const mark-goto)
  456. (const bookmark-jump)
  457. (const isearch)
  458. (const default))
  459. (boolean))))
  460. "Contexts for the reveal options.")
  461. (defcustom org-show-hierarchy-above '((default . t))
  462. "Non-nil means show full hierarchy when revealing a location.
  463. Org-mode often shows locations in an org-mode file which might have
  464. been invisible before. When this is set, the hierarchy of headings
  465. above the exposed location is shown.
  466. Turning this off for example for sparse trees makes them very compact.
  467. Instead of t, this can also be an alist specifying this option for different
  468. contexts. Valid contexts are
  469. agenda when exposing an entry from the agenda
  470. org-goto when using the command `org-goto' on key C-c C-j
  471. occur-tree when using the command `org-occur' on key C-c /
  472. tags-tree when constructing a sparse tree based on tags matches
  473. link-search when exposing search matches associated with a link
  474. mark-goto when exposing the jump goal of a mark
  475. bookmark-jump when exposing a bookmark location
  476. isearch when exiting from an incremental search
  477. default default for all contexts not set explicitly"
  478. :group 'org-reveal-location
  479. :type org-context-choice)
  480. (defcustom org-show-following-heading '((default . nil))
  481. "Non-nil means show following heading when revealing a location.
  482. Org-mode often shows locations in an org-mode file which might have
  483. been invisible before. When this is set, the heading following the
  484. match is shown.
  485. Turning this off for example for sparse trees makes them very compact,
  486. but makes it harder to edit the location of the match. In such a case,
  487. use the command \\[org-reveal] to show more context.
  488. Instead of t, this can also be an alist specifying this option for different
  489. contexts. See `org-show-hierarchy-above' for valid contexts."
  490. :group 'org-reveal-location
  491. :type org-context-choice)
  492. (defcustom org-show-siblings '((default . nil) (isearch t))
  493. "Non-nil means show all sibling heading when revealing a location.
  494. Org-mode often shows locations in an org-mode file which might have
  495. been invisible before. When this is set, the sibling of the current entry
  496. heading are all made visible. If `org-show-hierarchy-above' is t,
  497. the same happens on each level of the hierarchy above the current entry.
  498. By default this is on for the isearch context, off for all other contexts.
  499. Turning this off for example for sparse trees makes them very compact,
  500. but makes it harder to edit the location of the match. In such a case,
  501. use the command \\[org-reveal] to show more context.
  502. Instead of t, this can also be an alist specifying this option for different
  503. contexts. See `org-show-hierarchy-above' for valid contexts."
  504. :group 'org-reveal-location
  505. :type org-context-choice)
  506. (defcustom org-show-entry-below '((default . nil))
  507. "Non-nil means show the entry below a headline when revealing a location.
  508. Org-mode often shows locations in an org-mode file which might have
  509. been invisible before. When this is set, the text below the headline that is
  510. exposed is also shown.
  511. By default this is off for all contexts.
  512. Instead of t, this can also be an alist specifying this option for different
  513. contexts. See `org-show-hierarchy-above' for valid contexts."
  514. :group 'org-reveal-location
  515. :type org-context-choice)
  516. (defcustom org-indirect-buffer-display 'other-window
  517. "How should indirect tree buffers be displayed?
  518. This applies to indirect buffers created with the commands
  519. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  520. Valid values are:
  521. current-window Display in the current window
  522. other-window Just display in another window.
  523. dedicated-frame Create one new frame, and re-use it each time.
  524. new-frame Make a new frame each time. Note that in this case
  525. previously-made indirect buffers are kept, and you need to
  526. kill these buffers yourself."
  527. :group 'org-structure
  528. :group 'org-agenda-windows
  529. :type '(choice
  530. (const :tag "In current window" current-window)
  531. (const :tag "In current frame, other window" other-window)
  532. (const :tag "Each time a new frame" new-frame)
  533. (const :tag "One dedicated frame" dedicated-frame)))
  534. (defcustom org-use-speed-commands nil
  535. "Non-nil means activate single letter commands at beginning of a headline.
  536. This may also be a function to test for appropriate locations where speed
  537. commands should be active."
  538. :group 'org-structure
  539. :type '(choice
  540. (const :tag "Never" nil)
  541. (const :tag "At beginning of headline stars" t)
  542. (function)))
  543. (defcustom org-speed-commands-user nil
  544. "Alist of additional speed commands.
  545. This list will be checked before `org-speed-commands-default'
  546. when the variable `org-use-speed-commands' is non-nil
  547. and when the cursor is at the beginning of a headline.
  548. The car if each entry is a string with a single letter, which must
  549. be assigned to `self-insert-command' in the global map.
  550. The cdr is either a command to be called interactively, a function
  551. to be called, or a form to be evaluated.
  552. An entry that is just a list with a single string will be interpreted
  553. as a descriptive headline that will be added when listing the speed
  554. copmmands in the Help buffer using the `?' speed command."
  555. :group 'org-structure
  556. :type '(repeat :value ("k" . ignore)
  557. (choice :value ("k" . ignore)
  558. (list :tag "Descriptive Headline" (string :tag "Headline"))
  559. (cons :tag "Letter and Command"
  560. (string :tag "Command letter")
  561. (choice
  562. (function)
  563. (sexp))))))
  564. (defgroup org-cycle nil
  565. "Options concerning visibility cycling in Org-mode."
  566. :tag "Org Cycle"
  567. :group 'org-structure)
  568. (defcustom org-cycle-skip-children-state-if-no-children t
  569. "Non-nil means skip CHILDREN state in entries that don't have any."
  570. :group 'org-cycle
  571. :type 'boolean)
  572. (defcustom org-cycle-max-level nil
  573. "Maximum level which should still be subject to visibility cycling.
  574. Levels higher than this will, for cycling, be treated as text, not a headline.
  575. When `org-odd-levels-only' is set, a value of N in this variable actually
  576. means 2N-1 stars as the limiting headline.
  577. When nil, cycle all levels.
  578. Note that the limiting level of cycling is also influenced by
  579. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  580. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  581. than its value."
  582. :group 'org-cycle
  583. :type '(choice
  584. (const :tag "No limit" nil)
  585. (integer :tag "Maximum level")))
  586. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  587. "Names of drawers. Drawers are not opened by cycling on the headline above.
  588. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  589. this:
  590. :DRAWERNAME:
  591. .....
  592. :END:
  593. The drawer \"PROPERTIES\" is special for capturing properties through
  594. the property API.
  595. Drawers can be defined on the per-file basis with a line like:
  596. #+DRAWERS: HIDDEN STATE PROPERTIES"
  597. :group 'org-structure
  598. :group 'org-cycle
  599. :type '(repeat (string :tag "Drawer Name")))
  600. (defcustom org-hide-block-startup nil
  601. "Non-nil means entering Org-mode will fold all blocks.
  602. This can also be set in on a per-file basis with
  603. #+STARTUP: hideblocks
  604. #+STARTUP: showblocks"
  605. :group 'org-startup
  606. :group 'org-cycle
  607. :type 'boolean)
  608. (defcustom org-cycle-global-at-bob nil
  609. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  610. This makes it possible to do global cycling without having to use S-TAB or
  611. C-u TAB. For this special case to work, the first line of the buffer
  612. must not be a headline - it may be empty or some other text. When used in
  613. this way, `org-cycle-hook' is disables temporarily, to make sure the
  614. cursor stays at the beginning of the buffer.
  615. When this option is nil, don't do anything special at the beginning
  616. of the buffer."
  617. :group 'org-cycle
  618. :type 'boolean)
  619. (defcustom org-cycle-level-after-item/entry-creation t
  620. "Non-nil means cycle entry level or item indentation in new empty entries.
  621. When the cursor is at the end of an empty headline, i.e with only stars
  622. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  623. and then all possible anchestor states, before returning to the original state.
  624. This makes data entry extremely fast: M-RET to create a new headline,
  625. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  626. When the cursor is at the end of an empty plain list item, one TAB will
  627. make it a subitem, two or more tabs will back up to make this an item
  628. higher up in the item hierarchy."
  629. :group 'org-cycle
  630. :type 'boolean)
  631. (defcustom org-cycle-emulate-tab t
  632. "Where should `org-cycle' emulate TAB.
  633. nil Never
  634. white Only in completely white lines
  635. whitestart Only at the beginning of lines, before the first non-white char
  636. t Everywhere except in headlines
  637. exc-hl-bol Everywhere except at the start of a headline
  638. If TAB is used in a place where it does not emulate TAB, the current subtree
  639. visibility is cycled."
  640. :group 'org-cycle
  641. :type '(choice (const :tag "Never" nil)
  642. (const :tag "Only in completely white lines" white)
  643. (const :tag "Before first char in a line" whitestart)
  644. (const :tag "Everywhere except in headlines" t)
  645. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  646. ))
  647. (defcustom org-cycle-separator-lines 2
  648. "Number of empty lines needed to keep an empty line between collapsed trees.
  649. If you leave an empty line between the end of a subtree and the following
  650. headline, this empty line is hidden when the subtree is folded.
  651. Org-mode will leave (exactly) one empty line visible if the number of
  652. empty lines is equal or larger to the number given in this variable.
  653. So the default 2 means at least 2 empty lines after the end of a subtree
  654. are needed to produce free space between a collapsed subtree and the
  655. following headline.
  656. If the number is negative, and the number of empty lines is at least -N,
  657. all empty lines are shown.
  658. Special case: when 0, never leave empty lines in collapsed view."
  659. :group 'org-cycle
  660. :type 'integer)
  661. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  662. (defcustom org-pre-cycle-hook nil
  663. "Hook that is run before visibility cycling is happening.
  664. The function(s) in this hook must accept a single argument which indicates
  665. the new state that will be set right after running this hook. The
  666. argument is a symbol. Before a global state change, it can have the values
  667. `overview', `content', or `all'. Before a local state change, it can have
  668. the values `folded', `children', or `subtree'."
  669. :group 'org-cycle
  670. :type 'hook)
  671. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  672. org-cycle-hide-drawers
  673. org-cycle-show-empty-lines
  674. org-optimize-window-after-visibility-change)
  675. "Hook that is run after `org-cycle' has changed the buffer visibility.
  676. The function(s) in this hook must accept a single argument which indicates
  677. the new state that was set by the most recent `org-cycle' command. The
  678. argument is a symbol. After a global state change, it can have the values
  679. `overview', `content', or `all'. After a local state change, it can have
  680. the values `folded', `children', or `subtree'."
  681. :group 'org-cycle
  682. :type 'hook)
  683. (defgroup org-edit-structure nil
  684. "Options concerning structure editing in Org-mode."
  685. :tag "Org Edit Structure"
  686. :group 'org-structure)
  687. (defcustom org-odd-levels-only nil
  688. "Non-nil means skip even levels and only use odd levels for the outline.
  689. This has the effect that two stars are being added/taken away in
  690. promotion/demotion commands. It also influences how levels are
  691. handled by the exporters.
  692. Changing it requires restart of `font-lock-mode' to become effective
  693. for fontification also in regions already fontified.
  694. You may also set this on a per-file basis by adding one of the following
  695. lines to the buffer:
  696. #+STARTUP: odd
  697. #+STARTUP: oddeven"
  698. :group 'org-edit-structure
  699. :group 'org-appearance
  700. :type 'boolean)
  701. (defcustom org-adapt-indentation t
  702. "Non-nil means adapt indentation to outline node level.
  703. When this variable is set, Org assumes that you write outlines by
  704. indenting text in each node to align with the headline (after the stars).
  705. The following issues are influenced by this variable:
  706. - When this is set and the *entire* text in an entry is indented, the
  707. indentation is increased by one space in a demotion command, and
  708. decreased by one in a promotion command. If any line in the entry
  709. body starts with text at column 0, indentation is not changed at all.
  710. - Property drawers and planning information is inserted indented when
  711. this variable s set. When nil, they will not be indented.
  712. - TAB indents a line relative to context. The lines below a headline
  713. will be indented when this variable is set.
  714. Note that this is all about true indentation, by adding and removing
  715. space characters. See also `org-indent.el' which does level-dependent
  716. indentation in a virtual way, i.e. at display time in Emacs."
  717. :group 'org-edit-structure
  718. :type 'boolean)
  719. (defcustom org-special-ctrl-a/e nil
  720. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  721. When t, `C-a' will bring back the cursor to the beginning of the
  722. headline text, i.e. after the stars and after a possible TODO keyword.
  723. In an item, this will be the position after the bullet.
  724. When the cursor is already at that position, another `C-a' will bring
  725. it to the beginning of the line.
  726. `C-e' will jump to the end of the headline, ignoring the presence of tags
  727. in the headline. A second `C-e' will then jump to the true end of the
  728. line, after any tags. This also means that, when this variable is
  729. non-nil, `C-e' also will never jump beyond the end of the heading of a
  730. folded section, i.e. not after the ellipses.
  731. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  732. going to the true line boundary first. Only a directly following, identical
  733. keypress will bring the cursor to the special positions.
  734. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  735. set separately."
  736. :group 'org-edit-structure
  737. :type '(choice
  738. (const :tag "off" nil)
  739. (const :tag "on: after stars/bullet and before tags first" t)
  740. (const :tag "reversed: true line boundary first" reversed)
  741. (cons :tag "Set C-a and C-e separately"
  742. (choice :tag "Special C-a"
  743. (const :tag "off" nil)
  744. (const :tag "on: after stars/bullet first" t)
  745. (const :tag "reversed: before stars/bullet first" reversed))
  746. (choice :tag "Special C-e"
  747. (const :tag "off" nil)
  748. (const :tag "on: before tags first" t)
  749. (const :tag "reversed: after tags first" reversed)))))
  750. (if (fboundp 'defvaralias)
  751. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  752. (defcustom org-special-ctrl-k nil
  753. "Non-nil means `C-k' will behave specially in headlines.
  754. When nil, `C-k' will call the default `kill-line' command.
  755. When t, the following will happen while the cursor is in the headline:
  756. - When the cursor is at the beginning of a headline, kill the entire
  757. line and possible the folded subtree below the line.
  758. - When in the middle of the headline text, kill the headline up to the tags.
  759. - When after the headline text, kill the tags."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-yank-folded-subtrees t
  763. "Non-nil means when yanking subtrees, fold them.
  764. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  765. it starts with a heading and all other headings in it are either children
  766. or siblings, then fold all the subtrees. However, do this only if no
  767. text after the yank would be swallowed into a folded tree by this action."
  768. :group 'org-edit-structure
  769. :type 'boolean)
  770. (defcustom org-yank-adjusted-subtrees nil
  771. "Non-nil means when yanking subtrees, adjust the level.
  772. With this setting, `org-paste-subtree' is used to insert the subtree, see
  773. this function for details."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-M-RET-may-split-line '((default . t))
  777. "Non-nil means M-RET will split the line at the cursor position.
  778. When nil, it will go to the end of the line before making a
  779. new line.
  780. You may also set this option in a different way for different
  781. contexts. Valid contexts are:
  782. headline when creating a new headline
  783. item when creating a new item
  784. table in a table field
  785. default the value to be used for all contexts not explicitly
  786. customized"
  787. :group 'org-structure
  788. :group 'org-table
  789. :type '(choice
  790. (const :tag "Always" t)
  791. (const :tag "Never" nil)
  792. (repeat :greedy t :tag "Individual contexts"
  793. (cons
  794. (choice :tag "Context"
  795. (const headline)
  796. (const item)
  797. (const table)
  798. (const default))
  799. (boolean)))))
  800. (defcustom org-insert-heading-respect-content nil
  801. "Non-nil means insert new headings after the current subtree.
  802. When nil, the new heading is created directly after the current line.
  803. The commands \\[org-insert-heading-respect-content] and
  804. \\[org-insert-todo-heading-respect-content] turn this variable on
  805. for the duration of the command."
  806. :group 'org-structure
  807. :type 'boolean)
  808. (defcustom org-blank-before-new-entry '((heading . auto)
  809. (plain-list-item . auto))
  810. "Should `org-insert-heading' leave a blank line before new heading/item?
  811. The value is an alist, with `heading' and `plain-list-item' as car,
  812. and a boolean flag as cdr. For plain lists, if the variable
  813. `org-empty-line-terminates-plain-lists' is set, the setting here
  814. is ignored and no empty line is inserted, to keep the list in tact."
  815. :group 'org-edit-structure
  816. :type '(list
  817. (cons (const heading)
  818. (choice (const :tag "Never" nil)
  819. (const :tag "Always" t)
  820. (const :tag "Auto" auto)))
  821. (cons (const plain-list-item)
  822. (choice (const :tag "Never" nil)
  823. (const :tag "Always" t)
  824. (const :tag "Auto" auto)))))
  825. (defcustom org-insert-heading-hook nil
  826. "Hook being run after inserting a new heading."
  827. :group 'org-edit-structure
  828. :type 'hook)
  829. (defcustom org-enable-fixed-width-editor t
  830. "Non-nil means lines starting with \":\" are treated as fixed-width.
  831. This currently only means they are never auto-wrapped.
  832. When nil, such lines will be treated like ordinary lines.
  833. See also the QUOTE keyword."
  834. :group 'org-edit-structure
  835. :type 'boolean)
  836. (defcustom org-goto-auto-isearch t
  837. "Non-nil means typing characters in org-goto starts incremental search."
  838. :group 'org-edit-structure
  839. :type 'boolean)
  840. (defgroup org-sparse-trees nil
  841. "Options concerning sparse trees in Org-mode."
  842. :tag "Org Sparse Trees"
  843. :group 'org-structure)
  844. (defcustom org-highlight-sparse-tree-matches t
  845. "Non-nil means highlight all matches that define a sparse tree.
  846. The highlights will automatically disappear the next time the buffer is
  847. changed by an edit command."
  848. :group 'org-sparse-trees
  849. :type 'boolean)
  850. (defcustom org-remove-highlights-with-change t
  851. "Non-nil means any change to the buffer will remove temporary highlights.
  852. Such highlights are created by `org-occur' and `org-clock-display'.
  853. When nil, `C-c C-c needs to be used to get rid of the highlights.
  854. The highlights created by `org-preview-latex-fragment' always need
  855. `C-c C-c' to be removed."
  856. :group 'org-sparse-trees
  857. :group 'org-time
  858. :type 'boolean)
  859. (defcustom org-occur-hook '(org-first-headline-recenter)
  860. "Hook that is run after `org-occur' has constructed a sparse tree.
  861. This can be used to recenter the window to show as much of the structure
  862. as possible."
  863. :group 'org-sparse-trees
  864. :type 'hook)
  865. (defgroup org-imenu-and-speedbar nil
  866. "Options concerning imenu and speedbar in Org-mode."
  867. :tag "Org Imenu and Speedbar"
  868. :group 'org-structure)
  869. (defcustom org-imenu-depth 2
  870. "The maximum level for Imenu access to Org-mode headlines.
  871. This also applied for speedbar access."
  872. :group 'org-imenu-and-speedbar
  873. :type 'integer)
  874. (defgroup org-table nil
  875. "Options concerning tables in Org-mode."
  876. :tag "Org Table"
  877. :group 'org)
  878. (defcustom org-enable-table-editor 'optimized
  879. "Non-nil means lines starting with \"|\" are handled by the table editor.
  880. When nil, such lines will be treated like ordinary lines.
  881. When equal to the symbol `optimized', the table editor will be optimized to
  882. do the following:
  883. - Automatic overwrite mode in front of whitespace in table fields.
  884. This makes the structure of the table stay in tact as long as the edited
  885. field does not exceed the column width.
  886. - Minimize the number of realigns. Normally, the table is aligned each time
  887. TAB or RET are pressed to move to another field. With optimization this
  888. happens only if changes to a field might have changed the column width.
  889. Optimization requires replacing the functions `self-insert-command',
  890. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  891. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  892. very good at guessing when a re-align will be necessary, but you can always
  893. force one with \\[org-ctrl-c-ctrl-c].
  894. If you would like to use the optimized version in Org-mode, but the
  895. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  896. This variable can be used to turn on and off the table editor during a session,
  897. but in order to toggle optimization, a restart is required.
  898. See also the variable `org-table-auto-blank-field'."
  899. :group 'org-table
  900. :type '(choice
  901. (const :tag "off" nil)
  902. (const :tag "on" t)
  903. (const :tag "on, optimized" optimized)))
  904. (defcustom org-self-insert-cluster-for-undo t
  905. "Non-nil means cluster self-insert commands for undo when possible.
  906. If this is set, then, like in the Emacs command loop, 20 consecutive
  907. characters will be undone together.
  908. This is configurable, because there is some impact on typing performance."
  909. :group 'org-table
  910. :type 'boolean)
  911. (defcustom org-table-tab-recognizes-table.el t
  912. "Non-nil means TAB will automatically notice a table.el table.
  913. When it sees such a table, it moves point into it and - if necessary -
  914. calls `table-recognize-table'."
  915. :group 'org-table-editing
  916. :type 'boolean)
  917. (defgroup org-link nil
  918. "Options concerning links in Org-mode."
  919. :tag "Org Link"
  920. :group 'org)
  921. (defvar org-link-abbrev-alist-local nil
  922. "Buffer-local version of `org-link-abbrev-alist', which see.
  923. The value of this is taken from the #+LINK lines.")
  924. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  925. (defcustom org-link-abbrev-alist nil
  926. "Alist of link abbreviations.
  927. The car of each element is a string, to be replaced at the start of a link.
  928. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  929. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  930. [[linkkey:tag][description]]
  931. The 'linkkey' must be a word word, starting with a letter, followed
  932. by letters, numbers, '-' or '_'.
  933. If REPLACE is a string, the tag will simply be appended to create the link.
  934. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  935. the placeholder \"%h\" will cause a url-encoded version of the tag to
  936. be inserted at that point (see the function `url-hexify-string').
  937. REPLACE may also be a function that will be called with the tag as the
  938. only argument to create the link, which should be returned as a string.
  939. See the manual for examples."
  940. :group 'org-link
  941. :type '(repeat
  942. (cons
  943. (string :tag "Protocol")
  944. (choice
  945. (string :tag "Format")
  946. (function)))))
  947. (defcustom org-descriptive-links t
  948. "Non-nil means hide link part and only show description of bracket links.
  949. Bracket links are like [[link][description]]. This variable sets the initial
  950. state in new org-mode buffers. The setting can then be toggled on a
  951. per-buffer basis from the Org->Hyperlinks menu."
  952. :group 'org-link
  953. :type 'boolean)
  954. (defcustom org-link-file-path-type 'adaptive
  955. "How the path name in file links should be stored.
  956. Valid values are:
  957. relative Relative to the current directory, i.e. the directory of the file
  958. into which the link is being inserted.
  959. absolute Absolute path, if possible with ~ for home directory.
  960. noabbrev Absolute path, no abbreviation of home directory.
  961. adaptive Use relative path for files in the current directory and sub-
  962. directories of it. For other files, use an absolute path."
  963. :group 'org-link
  964. :type '(choice
  965. (const relative)
  966. (const absolute)
  967. (const noabbrev)
  968. (const adaptive)))
  969. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  970. "Types of links that should be activated in Org-mode files.
  971. This is a list of symbols, each leading to the activation of a certain link
  972. type. In principle, it does not hurt to turn on most link types - there may
  973. be a small gain when turning off unused link types. The types are:
  974. bracket The recommended [[link][description]] or [[link]] links with hiding.
  975. angular Links in angular brackets that may contain whitespace like
  976. <bbdb:Carsten Dominik>.
  977. plain Plain links in normal text, no whitespace, like http://google.com.
  978. radio Text that is matched by a radio target, see manual for details.
  979. tag Tag settings in a headline (link to tag search).
  980. date Time stamps (link to calendar).
  981. footnote Footnote labels.
  982. Changing this variable requires a restart of Emacs to become effective."
  983. :group 'org-link
  984. :type '(set :greedy t
  985. (const :tag "Double bracket links (new style)" bracket)
  986. (const :tag "Angular bracket links (old style)" angular)
  987. (const :tag "Plain text links" plain)
  988. (const :tag "Radio target matches" radio)
  989. (const :tag "Tags" tag)
  990. (const :tag "Timestamps" date)
  991. (const :tag "Footnotes" footnote)))
  992. (defcustom org-make-link-description-function nil
  993. "Function to use to generate link descriptions from links. If
  994. nil the link location will be used. This function must take two
  995. parameters; the first is the link and the second the description
  996. org-insert-link has generated, and should return the description
  997. to use."
  998. :group 'org-link
  999. :type 'function)
  1000. (defgroup org-link-store nil
  1001. "Options concerning storing links in Org-mode."
  1002. :tag "Org Store Link"
  1003. :group 'org-link)
  1004. (defcustom org-email-link-description-format "Email %c: %.30s"
  1005. "Format of the description part of a link to an email or usenet message.
  1006. The following %-escapes will be replaced by corresponding information:
  1007. %F full \"From\" field
  1008. %f name, taken from \"From\" field, address if no name
  1009. %T full \"To\" field
  1010. %t first name in \"To\" field, address if no name
  1011. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1012. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1013. %s subject
  1014. %m message-id.
  1015. You may use normal field width specification between the % and the letter.
  1016. This is for example useful to limit the length of the subject.
  1017. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1018. :group 'org-link-store
  1019. :type 'string)
  1020. (defcustom org-from-is-user-regexp
  1021. (let (r1 r2)
  1022. (when (and user-mail-address (not (string= user-mail-address "")))
  1023. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1024. (when (and user-full-name (not (string= user-full-name "")))
  1025. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1026. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1027. "Regexp matched against the \"From:\" header of an email or usenet message.
  1028. It should match if the message is from the user him/herself."
  1029. :group 'org-link-store
  1030. :type 'regexp)
  1031. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1032. "Non-nil means storing a link to an Org file will use entry IDs.
  1033. Note that before this variable is even considered, org-id must be loaded,
  1034. so please customize `org-modules' and turn it on.
  1035. The variable can have the following values:
  1036. t Create an ID if needed to make a link to the current entry.
  1037. create-if-interactive
  1038. If `org-store-link' is called directly (interactively, as a user
  1039. command), do create an ID to support the link. But when doing the
  1040. job for remember, only use the ID if it already exists. The
  1041. purpose of this setting is to avoid proliferation of unwanted
  1042. IDs, just because you happen to be in an Org file when you
  1043. call `org-remember' that automatically and preemptively
  1044. creates a link. If you do want to get an ID link in a remember
  1045. template to an entry not having an ID, create it first by
  1046. explicitly creating a link to it, using `C-c C-l' first.
  1047. create-if-interactive-and-no-custom-id
  1048. Like create-if-interactive, but do not create an ID if there is
  1049. a CUSTOM_ID property defined in the entry. This is the default.
  1050. use-existing
  1051. Use existing ID, do not create one.
  1052. nil Never use an ID to make a link, instead link using a text search for
  1053. the headline text."
  1054. :group 'org-link-store
  1055. :type '(choice
  1056. (const :tag "Create ID to make link" t)
  1057. (const :tag "Create if storing link interactively"
  1058. create-if-interactive)
  1059. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1060. create-if-interactive-and-no-custom-id)
  1061. (const :tag "Only use existing" use-existing)
  1062. (const :tag "Do not use ID to create link" nil)))
  1063. (defcustom org-context-in-file-links t
  1064. "Non-nil means file links from `org-store-link' contain context.
  1065. A search string will be added to the file name with :: as separator and
  1066. used to find the context when the link is activated by the command
  1067. `org-open-at-point'.
  1068. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1069. negates this setting for the duration of the command."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-keep-stored-link-after-insertion nil
  1073. "Non-nil means keep link in list for entire session.
  1074. The command `org-store-link' adds a link pointing to the current
  1075. location to an internal list. These links accumulate during a session.
  1076. The command `org-insert-link' can be used to insert links into any
  1077. Org-mode file (offering completion for all stored links). When this
  1078. option is nil, every link which has been inserted once using \\[org-insert-link]
  1079. will be removed from the list, to make completing the unused links
  1080. more efficient."
  1081. :group 'org-link-store
  1082. :type 'boolean)
  1083. (defgroup org-link-follow nil
  1084. "Options concerning following links in Org-mode."
  1085. :tag "Org Follow Link"
  1086. :group 'org-link)
  1087. (defcustom org-link-translation-function nil
  1088. "Function to translate links with different syntax to Org syntax.
  1089. This can be used to translate links created for example by the Planner
  1090. or emacs-wiki packages to Org syntax.
  1091. The function must accept two parameters, a TYPE containing the link
  1092. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1093. which is everything after the link protocol. It should return a cons
  1094. with possibly modified values of type and path.
  1095. Org contains a function for this, so if you set this variable to
  1096. `org-translate-link-from-planner', you should be able follow many
  1097. links created by planner."
  1098. :group 'org-link-follow
  1099. :type 'function)
  1100. (defcustom org-follow-link-hook nil
  1101. "Hook that is run after a link has been followed."
  1102. :group 'org-link-follow
  1103. :type 'hook)
  1104. (defcustom org-tab-follows-link nil
  1105. "Non-nil means on links TAB will follow the link.
  1106. Needs to be set before org.el is loaded.
  1107. This really should not be used, it does not make sense, and the
  1108. implementation is bad."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-return-follows-link nil
  1112. "Non-nil means on links RET will follow the link.
  1113. Needs to be set before org.el is loaded."
  1114. :group 'org-link-follow
  1115. :type 'boolean)
  1116. (defcustom org-mouse-1-follows-link
  1117. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1118. "Non-nil means mouse-1 on a link will follow the link.
  1119. A longer mouse click will still set point. Does not work on XEmacs.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mark-ring-length 4
  1124. "Number of different positions to be recorded in the ring
  1125. Changing this requires a restart of Emacs to work correctly."
  1126. :group 'org-link-follow
  1127. :type 'integer)
  1128. (defcustom org-link-frame-setup
  1129. '((vm . vm-visit-folder-other-frame)
  1130. (gnus . gnus-other-frame)
  1131. (file . find-file-other-window))
  1132. "Setup the frame configuration for following links.
  1133. When following a link with Emacs, it may often be useful to display
  1134. this link in another window or frame. This variable can be used to
  1135. set this up for the different types of links.
  1136. For VM, use any of
  1137. `vm-visit-folder'
  1138. `vm-visit-folder-other-frame'
  1139. For Gnus, use any of
  1140. `gnus'
  1141. `gnus-other-frame'
  1142. `org-gnus-no-new-news'
  1143. For FILE, use any of
  1144. `find-file'
  1145. `find-file-other-window'
  1146. `find-file-other-frame'
  1147. For the calendar, use the variable `calendar-setup'.
  1148. For BBDB, it is currently only possible to display the matches in
  1149. another window."
  1150. :group 'org-link-follow
  1151. :type '(list
  1152. (cons (const vm)
  1153. (choice
  1154. (const vm-visit-folder)
  1155. (const vm-visit-folder-other-window)
  1156. (const vm-visit-folder-other-frame)))
  1157. (cons (const gnus)
  1158. (choice
  1159. (const gnus)
  1160. (const gnus-other-frame)
  1161. (const org-gnus-no-new-news)))
  1162. (cons (const file)
  1163. (choice
  1164. (const find-file)
  1165. (const find-file-other-window)
  1166. (const find-file-other-frame)))))
  1167. (defcustom org-display-internal-link-with-indirect-buffer nil
  1168. "Non-nil means use indirect buffer to display infile links.
  1169. Activating internal links (from one location in a file to another location
  1170. in the same file) normally just jumps to the location. When the link is
  1171. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1172. another window. When this option is set, the other window actually displays
  1173. an indirect buffer clone of the current buffer, to avoid any visibility
  1174. changes to the current buffer."
  1175. :group 'org-link-follow
  1176. :type 'boolean)
  1177. (defcustom org-open-non-existing-files nil
  1178. "Non-nil means `org-open-file' will open non-existing files.
  1179. When nil, an error will be generated.
  1180. This variable applies only to external applications because they
  1181. might choke on non-existing files. If the link is to a file that
  1182. will be opened in Emacs, the variable is ignored."
  1183. :group 'org-link-follow
  1184. :type 'boolean)
  1185. (defcustom org-open-directory-means-index-dot-org nil
  1186. "Non-nil means a link to a directory really means to index.org.
  1187. When nil, following a directory link will run dired or open a finder/explorer
  1188. window on that directory."
  1189. :group 'org-link-follow
  1190. :type 'boolean)
  1191. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1192. "Function and arguments to call for following mailto links.
  1193. This is a list with the first element being a lisp function, and the
  1194. remaining elements being arguments to the function. In string arguments,
  1195. %a will be replaced by the address, and %s will be replaced by the subject
  1196. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1197. :group 'org-link-follow
  1198. :type '(choice
  1199. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1200. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1201. (const :tag "message-mail" (message-mail "%a" "%s"))
  1202. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1203. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1204. "Non-nil means ask for confirmation before executing shell links.
  1205. Shell links can be dangerous: just think about a link
  1206. [[shell:rm -rf ~/*][Google Search]]
  1207. This link would show up in your Org-mode document as \"Google Search\",
  1208. but really it would remove your entire home directory.
  1209. Therefore we advise against setting this variable to nil.
  1210. Just change it to `y-or-n-p' if you want to confirm with a
  1211. single keystroke rather than having to type \"yes\"."
  1212. :group 'org-link-follow
  1213. :type '(choice
  1214. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1215. (const :tag "with y-or-n (faster)" y-or-n-p)
  1216. (const :tag "no confirmation (dangerous)" nil)))
  1217. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1218. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1219. Elisp links can be dangerous: just think about a link
  1220. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1221. This link would show up in your Org-mode document as \"Google Search\",
  1222. but really it would remove your entire home directory.
  1223. Therefore we advise against setting this variable to nil.
  1224. Just change it to `y-or-n-p' if you want to confirm with a
  1225. single keystroke rather than having to type \"yes\"."
  1226. :group 'org-link-follow
  1227. :type '(choice
  1228. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1229. (const :tag "with y-or-n (faster)" y-or-n-p)
  1230. (const :tag "no confirmation (dangerous)" nil)))
  1231. (defconst org-file-apps-defaults-gnu
  1232. '((remote . emacs)
  1233. (system . mailcap)
  1234. (t . mailcap))
  1235. "Default file applications on a UNIX or GNU/Linux system.
  1236. See `org-file-apps'.")
  1237. (defconst org-file-apps-defaults-macosx
  1238. '((remote . emacs)
  1239. (t . "open %s")
  1240. (system . "open %s")
  1241. ("ps.gz" . "gv %s")
  1242. ("eps.gz" . "gv %s")
  1243. ("dvi" . "xdvi %s")
  1244. ("fig" . "xfig %s"))
  1245. "Default file applications on a MacOS X system.
  1246. The system \"open\" is known as a default, but we use X11 applications
  1247. for some files for which the OS does not have a good default.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-windowsnt
  1250. (list
  1251. '(remote . emacs)
  1252. (cons t
  1253. (list (if (featurep 'xemacs)
  1254. 'mswindows-shell-execute
  1255. 'w32-shell-execute)
  1256. "open" 'file))
  1257. (cons 'system
  1258. (list (if (featurep 'xemacs)
  1259. 'mswindows-shell-execute
  1260. 'w32-shell-execute)
  1261. "open" 'file)))
  1262. "Default file applications on a Windows NT system.
  1263. The system \"open\" is used for most files.
  1264. See `org-file-apps'.")
  1265. (defcustom org-file-apps
  1266. '(
  1267. (auto-mode . emacs)
  1268. ("\\.mm\\'" . default)
  1269. ("\\.x?html?\\'" . default)
  1270. ("\\.pdf\\'" . default)
  1271. )
  1272. "External applications for opening `file:path' items in a document.
  1273. Org-mode uses system defaults for different file types, but
  1274. you can use this variable to set the application for a given file
  1275. extension. The entries in this list are cons cells where the car identifies
  1276. files and the cdr the corresponding command. Possible values for the
  1277. file identifier are
  1278. \"regex\" Regular expression matched against the file: link. For
  1279. backward compatibility, this can also be a string with only
  1280. alphanumeric characters, which is then interpreted as an
  1281. extension.
  1282. `directory' Matches a directory
  1283. `remote' Matches a remote file, accessible through tramp or efs.
  1284. Remote files most likely should be visited through Emacs
  1285. because external applications cannot handle such paths.
  1286. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1287. so all files Emacs knows how to handle. Using this with
  1288. command `emacs' will open most files in Emacs. Beware that this
  1289. will also open html files inside Emacs, unless you add
  1290. (\"html\" . default) to the list as well.
  1291. t Default for files not matched by any of the other options.
  1292. `system' The system command to open files, like `open' on Windows
  1293. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1294. that will be selected if you call `C-c C-o' with a double
  1295. `C-u C-u' prefix.
  1296. Possible values for the command are:
  1297. `emacs' The file will be visited by the current Emacs process.
  1298. `default' Use the default application for this file type, which is the
  1299. association for t in the list, most likely in the system-specific
  1300. part.
  1301. This can be used to overrule an unwanted setting in the
  1302. system-specific variable.
  1303. `system' Use the system command for opening files, like \"open\".
  1304. This command is specified by the entry whose car is `system'.
  1305. Most likely, the system-specific version of this variable
  1306. does define this command, but you can overrule/replace it
  1307. here.
  1308. string A command to be executed by a shell; %s will be replaced
  1309. by the path to the file. If the file identifier is a regex,
  1310. %n will be replaced by the match of the nth match group.
  1311. sexp A Lisp form which will be evaluated. The file path will
  1312. be available in the Lisp variable `file', the link itself
  1313. in the Lisp variable `link'. If the file identifier is a regex,
  1314. the original match data will be restored, so subexpression
  1315. matches are accessible using (match-string n link).
  1316. For more examples, see the system specific constants
  1317. `org-file-apps-defaults-macosx'
  1318. `org-file-apps-defaults-windowsnt'
  1319. `org-file-apps-defaults-gnu'."
  1320. :group 'org-link-follow
  1321. :type '(repeat
  1322. (cons (choice :value ""
  1323. (string :tag "Extension")
  1324. (const :tag "System command to open files" system)
  1325. (const :tag "Default for unrecognized files" t)
  1326. (const :tag "Remote file" remote)
  1327. (const :tag "Links to a directory" directory)
  1328. (const :tag "Any files that have Emacs modes"
  1329. auto-mode))
  1330. (choice :value ""
  1331. (const :tag "Visit with Emacs" emacs)
  1332. (const :tag "Use default" default)
  1333. (const :tag "Use the system command" system)
  1334. (string :tag "Command")
  1335. (sexp :tag "Lisp form")))))
  1336. (defgroup org-refile nil
  1337. "Options concerning refiling entries in Org-mode."
  1338. :tag "Org Refile"
  1339. :group 'org)
  1340. (defcustom org-directory "~/org"
  1341. "Directory with org files.
  1342. This is just a default location to look for Org files. There is no need
  1343. at all to put your files into this directory. It is only used in the
  1344. following situations:
  1345. 1. When a remember template specifies a target file that is not an
  1346. absolute path. The path will then be interpreted relative to
  1347. `org-directory'
  1348. 2. When a remember note is filed away in an interactive way (when exiting the
  1349. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1350. with `org-directory' as the default path."
  1351. :group 'org-refile
  1352. :group 'org-remember
  1353. :type 'directory)
  1354. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1355. "Default target for storing notes.
  1356. Used by the hooks for remember.el. This can be a string, or nil to mean
  1357. the value of `remember-data-file'.
  1358. You can set this on a per-template basis with the variable
  1359. `org-remember-templates'."
  1360. :group 'org-refile
  1361. :group 'org-remember
  1362. :type '(choice
  1363. (const :tag "Default from remember-data-file" nil)
  1364. file))
  1365. (defcustom org-goto-interface 'outline
  1366. "The default interface to be used for `org-goto'.
  1367. Allowed values are:
  1368. outline The interface shows an outline of the relevant file
  1369. and the correct heading is found by moving through
  1370. the outline or by searching with incremental search.
  1371. outline-path-completion Headlines in the current buffer are offered via
  1372. completion. This is the interface also used by
  1373. the refile command."
  1374. :group 'org-refile
  1375. :type '(choice
  1376. (const :tag "Outline" outline)
  1377. (const :tag "Outline-path-completion" outline-path-completion)))
  1378. (defcustom org-goto-max-level 5
  1379. "Maximum level to be considered when running org-goto with refile interface."
  1380. :group 'org-refile
  1381. :type 'integer)
  1382. (defcustom org-reverse-note-order nil
  1383. "Non-nil means store new notes at the beginning of a file or entry.
  1384. When nil, new notes will be filed to the end of a file or entry.
  1385. This can also be a list with cons cells of regular expressions that
  1386. are matched against file names, and values."
  1387. :group 'org-remember
  1388. :group 'org-refile
  1389. :type '(choice
  1390. (const :tag "Reverse always" t)
  1391. (const :tag "Reverse never" nil)
  1392. (repeat :tag "By file name regexp"
  1393. (cons regexp boolean))))
  1394. (defcustom org-log-refile nil
  1395. "Information to record when a task is refiled.
  1396. Possible values are:
  1397. nil Don't add anything
  1398. time Add a time stamp to the task
  1399. note Prompt for a note and add it with template `org-log-note-headings'
  1400. This option can also be set with on a per-file-basis with
  1401. #+STARTUP: nologrefile
  1402. #+STARTUP: logrefile
  1403. #+STARTUP: lognoterefile
  1404. You can have local logging settings for a subtree by setting the LOGGING
  1405. property to one or more of these keywords.
  1406. When bulk-refiling from the agenda, the value `note' is forbidden and
  1407. will temporarily be changed to `time'."
  1408. :group 'org-refile
  1409. :group 'org-progress
  1410. :type '(choice
  1411. (const :tag "No logging" nil)
  1412. (const :tag "Record timestamp" time)
  1413. (const :tag "Record timestamp with note." note)))
  1414. (defcustom org-refile-targets nil
  1415. "Targets for refiling entries with \\[org-refile].
  1416. This is list of cons cells. Each cell contains:
  1417. - a specification of the files to be considered, either a list of files,
  1418. or a symbol whose function or variable value will be used to retrieve
  1419. a file name or a list of file names. If you use `org-agenda-files' for
  1420. that, all agenda files will be scanned for targets. Nil means consider
  1421. headings in the current buffer.
  1422. - A specification of how to find candidate refile targets. This may be
  1423. any of:
  1424. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1425. This tag has to be present in all target headlines, inheritance will
  1426. not be considered.
  1427. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1428. todo keyword.
  1429. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1430. headlines that are refiling targets.
  1431. - a cons cell (:level . N). Any headline of level N is considered a target.
  1432. Note that, when `org-odd-levels-only' is set, level corresponds to
  1433. order in hierarchy, not to the number of stars.
  1434. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1435. Note that, when `org-odd-levels-only' is set, level corresponds to
  1436. order in hierarchy, not to the number of stars.
  1437. You can set the variable `org-refile-target-verify-function' to a function
  1438. to verify each headline found by the simple critery above.
  1439. When this variable is nil, all top-level headlines in the current buffer
  1440. are used, equivalent to the value `((nil . (:level . 1))'."
  1441. :group 'org-refile
  1442. :type '(repeat
  1443. (cons
  1444. (choice :value org-agenda-files
  1445. (const :tag "All agenda files" org-agenda-files)
  1446. (const :tag "Current buffer" nil)
  1447. (function) (variable) (file))
  1448. (choice :tag "Identify target headline by"
  1449. (cons :tag "Specific tag" (const :value :tag) (string))
  1450. (cons :tag "TODO keyword" (const :value :todo) (string))
  1451. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1452. (cons :tag "Level number" (const :value :level) (integer))
  1453. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1454. (defcustom org-refile-target-verify-function nil
  1455. "Function to verify if the headline at point should be a refile target.
  1456. The function will be called without arguments, with point at the
  1457. beginning of the headline. It should return t and leave point
  1458. where it is if the headline is a valid target for refiling.
  1459. If the target should not be selected, the function must return nil.
  1460. In addition to this, it may move point to a place from where the search
  1461. should be continued. For example, the function may decide that the entire
  1462. subtree of the current entry should be excluded and move point to the end
  1463. of the subtree."
  1464. :group 'org-refile
  1465. :type 'function)
  1466. (defcustom org-refile-use-outline-path nil
  1467. "Non-nil means provide refile targets as paths.
  1468. So a level 3 headline will be available as level1/level2/level3.
  1469. When the value is `file', also include the file name (without directory)
  1470. into the path. In this case, you can also stop the completion after
  1471. the file name, to get entries inserted as top level in the file.
  1472. When `full-file-path', include the full file path."
  1473. :group 'org-refile
  1474. :type '(choice
  1475. (const :tag "Not" nil)
  1476. (const :tag "Yes" t)
  1477. (const :tag "Start with file name" file)
  1478. (const :tag "Start with full file path" full-file-path)))
  1479. (defcustom org-outline-path-complete-in-steps t
  1480. "Non-nil means complete the outline path in hierarchical steps.
  1481. When Org-mode uses the refile interface to select an outline path
  1482. \(see variable `org-refile-use-outline-path'), the completion of
  1483. the path can be done is a single go, or if can be done in steps down
  1484. the headline hierarchy. Going in steps is probably the best if you
  1485. do not use a special completion package like `ido' or `icicles'.
  1486. However, when using these packages, going in one step can be very
  1487. fast, while still showing the whole path to the entry."
  1488. :group 'org-refile
  1489. :type 'boolean)
  1490. (defcustom org-refile-allow-creating-parent-nodes nil
  1491. "Non-nil means allow to create new nodes as refile targets.
  1492. New nodes are then created by adding \"/new node name\" to the completion
  1493. of an existing node. When the value of this variable is `confirm',
  1494. new node creation must be confirmed by the user (recommended)
  1495. When nil, the completion must match an existing entry.
  1496. Note that, if the new heading is not seen by the criteria
  1497. listed in `org-refile-targets', multiple instances of the same
  1498. heading would be created by trying again to file under the new
  1499. heading."
  1500. :group 'org-refile
  1501. :type '(choice
  1502. (const :tag "Never" nil)
  1503. (const :tag "Always" t)
  1504. (const :tag "Prompt for confirmation" confirm)))
  1505. (defgroup org-todo nil
  1506. "Options concerning TODO items in Org-mode."
  1507. :tag "Org TODO"
  1508. :group 'org)
  1509. (defgroup org-progress nil
  1510. "Options concerning Progress logging in Org-mode."
  1511. :tag "Org Progress"
  1512. :group 'org-time)
  1513. (defvar org-todo-interpretation-widgets
  1514. '(
  1515. (:tag "Sequence (cycling hits every state)" sequence)
  1516. (:tag "Type (cycling directly to DONE)" type))
  1517. "The available interpretation symbols for customizing
  1518. `org-todo-keywords'.
  1519. Interested libraries should add to this list.")
  1520. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1521. "List of TODO entry keyword sequences and their interpretation.
  1522. \\<org-mode-map>This is a list of sequences.
  1523. Each sequence starts with a symbol, either `sequence' or `type',
  1524. indicating if the keywords should be interpreted as a sequence of
  1525. action steps, or as different types of TODO items. The first
  1526. keywords are states requiring action - these states will select a headline
  1527. for inclusion into the global TODO list Org-mode produces. If one of
  1528. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1529. signify that no further action is necessary. If \"|\" is not found,
  1530. the last keyword is treated as the only DONE state of the sequence.
  1531. The command \\[org-todo] cycles an entry through these states, and one
  1532. additional state where no keyword is present. For details about this
  1533. cycling, see the manual.
  1534. TODO keywords and interpretation can also be set on a per-file basis with
  1535. the special #+SEQ_TODO and #+TYP_TODO lines.
  1536. Each keyword can optionally specify a character for fast state selection
  1537. \(in combination with the variable `org-use-fast-todo-selection')
  1538. and specifiers for state change logging, using the same syntax
  1539. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1540. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1541. indicates to record a time stamp each time this state is selected.
  1542. Each keyword may also specify if a timestamp or a note should be
  1543. recorded when entering or leaving the state, by adding additional
  1544. characters in the parenthesis after the keyword. This looks like this:
  1545. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1546. record only the time of the state change. With X and Y being either
  1547. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1548. Y when leaving the state if and only if the *target* state does not
  1549. define X. You may omit any of the fast-selection key or X or /Y,
  1550. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1551. For backward compatibility, this variable may also be just a list
  1552. of keywords - in this case the interpretation (sequence or type) will be
  1553. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1554. :group 'org-todo
  1555. :group 'org-keywords
  1556. :type '(choice
  1557. (repeat :tag "Old syntax, just keywords"
  1558. (string :tag "Keyword"))
  1559. (repeat :tag "New syntax"
  1560. (cons
  1561. (choice
  1562. :tag "Interpretation"
  1563. ;;Quick and dirty way to see
  1564. ;;`org-todo-interpretations'. This takes the
  1565. ;;place of item arguments
  1566. :convert-widget
  1567. (lambda (widget)
  1568. (widget-put widget
  1569. :args (mapcar
  1570. #'(lambda (x)
  1571. (widget-convert
  1572. (cons 'const x)))
  1573. org-todo-interpretation-widgets))
  1574. widget))
  1575. (repeat
  1576. (string :tag "Keyword"))))))
  1577. (defvar org-todo-keywords-1 nil
  1578. "All TODO and DONE keywords active in a buffer.")
  1579. (make-variable-buffer-local 'org-todo-keywords-1)
  1580. (defvar org-todo-keywords-for-agenda nil)
  1581. (defvar org-done-keywords-for-agenda nil)
  1582. (defvar org-drawers-for-agenda nil)
  1583. (defvar org-todo-keyword-alist-for-agenda nil)
  1584. (defvar org-tag-alist-for-agenda nil)
  1585. (defvar org-agenda-contributing-files nil)
  1586. (defvar org-not-done-keywords nil)
  1587. (make-variable-buffer-local 'org-not-done-keywords)
  1588. (defvar org-done-keywords nil)
  1589. (make-variable-buffer-local 'org-done-keywords)
  1590. (defvar org-todo-heads nil)
  1591. (make-variable-buffer-local 'org-todo-heads)
  1592. (defvar org-todo-sets nil)
  1593. (make-variable-buffer-local 'org-todo-sets)
  1594. (defvar org-todo-log-states nil)
  1595. (make-variable-buffer-local 'org-todo-log-states)
  1596. (defvar org-todo-kwd-alist nil)
  1597. (make-variable-buffer-local 'org-todo-kwd-alist)
  1598. (defvar org-todo-key-alist nil)
  1599. (make-variable-buffer-local 'org-todo-key-alist)
  1600. (defvar org-todo-key-trigger nil)
  1601. (make-variable-buffer-local 'org-todo-key-trigger)
  1602. (defcustom org-todo-interpretation 'sequence
  1603. "Controls how TODO keywords are interpreted.
  1604. This variable is in principle obsolete and is only used for
  1605. backward compatibility, if the interpretation of todo keywords is
  1606. not given already in `org-todo-keywords'. See that variable for
  1607. more information."
  1608. :group 'org-todo
  1609. :group 'org-keywords
  1610. :type '(choice (const sequence)
  1611. (const type)))
  1612. (defcustom org-use-fast-todo-selection t
  1613. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1614. This variable describes if and under what circumstances the cycling
  1615. mechanism for TODO keywords will be replaced by a single-key, direct
  1616. selection scheme.
  1617. When nil, fast selection is never used.
  1618. When the symbol `prefix', it will be used when `org-todo' is called with
  1619. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1620. in an agenda buffer.
  1621. When t, fast selection is used by default. In this case, the prefix
  1622. argument forces cycling instead.
  1623. In all cases, the special interface is only used if access keys have actually
  1624. been assigned by the user, i.e. if keywords in the configuration are followed
  1625. by a letter in parenthesis, like TODO(t)."
  1626. :group 'org-todo
  1627. :type '(choice
  1628. (const :tag "Never" nil)
  1629. (const :tag "By default" t)
  1630. (const :tag "Only with C-u C-c C-t" prefix)))
  1631. (defcustom org-provide-todo-statistics t
  1632. "Non-nil means update todo statistics after insert and toggle.
  1633. ALL-HEADLINES means update todo statistics by including headlines
  1634. with no TODO keyword as well, counting them as not done.
  1635. A list of TODO keywords means the same, but skip keywords that are
  1636. not in this list.
  1637. When this is set, todo statistics is updated in the parent of the
  1638. current entry each time a todo state is changed."
  1639. :group 'org-todo
  1640. :type '(choice
  1641. (const :tag "Yes, only for TODO entries" t)
  1642. (const :tag "Yes, including all entries" 'all-headlines)
  1643. (repeat :tag "Yes, for TODOs in this list"
  1644. (string :tag "TODO keyword"))
  1645. (other :tag "No TODO statistics" nil)))
  1646. (defcustom org-hierarchical-todo-statistics t
  1647. "Non-nil means TODO statistics covers just direct children.
  1648. When nil, all entries in the subtree are considered.
  1649. This has only an effect if `org-provide-todo-statistics' is set.
  1650. To set this to nil for only a single subtree, use a COOKIE_DATA
  1651. property and include the word \"recursive\" into the value."
  1652. :group 'org-todo
  1653. :type 'boolean)
  1654. (defcustom org-after-todo-state-change-hook nil
  1655. "Hook which is run after the state of a TODO item was changed.
  1656. The new state (a string with a TODO keyword, or nil) is available in the
  1657. Lisp variable `state'."
  1658. :group 'org-todo
  1659. :type 'hook)
  1660. (defvar org-blocker-hook nil
  1661. "Hook for functions that are allowed to block a state change.
  1662. Each function gets as its single argument a property list, see
  1663. `org-trigger-hook' for more information about this list.
  1664. If any of the functions in this hook returns nil, the state change
  1665. is blocked.")
  1666. (defvar org-trigger-hook nil
  1667. "Hook for functions that are triggered by a state change.
  1668. Each function gets as its single argument a property list with at least
  1669. the following elements:
  1670. (:type type-of-change :position pos-at-entry-start
  1671. :from old-state :to new-state)
  1672. Depending on the type, more properties may be present.
  1673. This mechanism is currently implemented for:
  1674. TODO state changes
  1675. ------------------
  1676. :type todo-state-change
  1677. :from previous state (keyword as a string), or nil, or a symbol
  1678. 'todo' or 'done', to indicate the general type of state.
  1679. :to new state, like in :from")
  1680. (defcustom org-enforce-todo-dependencies nil
  1681. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1682. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1683. be blocked if any prior sibling is not yet done.
  1684. Finally, if the parent is blocked because of ordered siblings of its own,
  1685. the child will also be blocked.
  1686. This variable needs to be set before org.el is loaded, and you need to
  1687. restart Emacs after a change to make the change effective. The only way
  1688. to change is while Emacs is running is through the customize interface."
  1689. :set (lambda (var val)
  1690. (set var val)
  1691. (if val
  1692. (add-hook 'org-blocker-hook
  1693. 'org-block-todo-from-children-or-siblings-or-parent)
  1694. (remove-hook 'org-blocker-hook
  1695. 'org-block-todo-from-children-or-siblings-or-parent)))
  1696. :group 'org-todo
  1697. :type 'boolean)
  1698. (defcustom org-enforce-todo-checkbox-dependencies nil
  1699. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1700. When this is nil, checkboxes have no influence on switching TODO states.
  1701. When non-nil, you first need to check off all check boxes before the TODO
  1702. entry can be switched to DONE.
  1703. This variable needs to be set before org.el is loaded, and you need to
  1704. restart Emacs after a change to make the change effective. The only way
  1705. to change is while Emacs is running is through the customize interface."
  1706. :set (lambda (var val)
  1707. (set var val)
  1708. (if val
  1709. (add-hook 'org-blocker-hook
  1710. 'org-block-todo-from-checkboxes)
  1711. (remove-hook 'org-blocker-hook
  1712. 'org-block-todo-from-checkboxes)))
  1713. :group 'org-todo
  1714. :type 'boolean)
  1715. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1716. "Non-nil means inserting a TODO heading is treated as state change.
  1717. So when the command \\[org-insert-todo-heading] is used, state change
  1718. logging will apply if appropriate. When nil, the new TODO item will
  1719. be inserted directly, and no logging will take place."
  1720. :group 'org-todo
  1721. :type 'boolean)
  1722. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1723. "Non-nil means switching TODO states with S-cursor counts as state change.
  1724. This is the default behavior. However, setting this to nil allows a
  1725. convenient way to select a TODO state and bypass any logging associated
  1726. with that."
  1727. :group 'org-todo
  1728. :type 'boolean)
  1729. (defcustom org-todo-state-tags-triggers nil
  1730. "Tag changes that should be triggered by TODO state changes.
  1731. This is a list. Each entry is
  1732. (state-change (tag . flag) .......)
  1733. State-change can be a string with a state, and empty string to indicate the
  1734. state that has no TODO keyword, or it can be one of the symbols `todo'
  1735. or `done', meaning any not-done or done state, respectively."
  1736. :group 'org-todo
  1737. :group 'org-tags
  1738. :type '(repeat
  1739. (cons (choice :tag "When changing to"
  1740. (const :tag "Not-done state" todo)
  1741. (const :tag "Done state" done)
  1742. (string :tag "State"))
  1743. (repeat
  1744. (cons :tag "Tag action"
  1745. (string :tag "Tag")
  1746. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1747. (defcustom org-log-done nil
  1748. "Information to record when a task moves to the DONE state.
  1749. Possible values are:
  1750. nil Don't add anything, just change the keyword
  1751. time Add a time stamp to the task
  1752. note Prompt for a note and add it with template `org-log-note-headings'
  1753. This option can also be set with on a per-file-basis with
  1754. #+STARTUP: nologdone
  1755. #+STARTUP: logdone
  1756. #+STARTUP: lognotedone
  1757. You can have local logging settings for a subtree by setting the LOGGING
  1758. property to one or more of these keywords."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type '(choice
  1762. (const :tag "No logging" nil)
  1763. (const :tag "Record CLOSED timestamp" time)
  1764. (const :tag "Record CLOSED timestamp with note." note)))
  1765. ;; Normalize old uses of org-log-done.
  1766. (cond
  1767. ((eq org-log-done t) (setq org-log-done 'time))
  1768. ((and (listp org-log-done) (memq 'done org-log-done))
  1769. (setq org-log-done 'note)))
  1770. (defcustom org-log-reschedule nil
  1771. "Information to record when the scheduling date of a tasks is modified.
  1772. Possible values are:
  1773. nil Don't add anything, just change the date
  1774. time Add a time stamp to the task
  1775. note Prompt for a note and add it with template `org-log-note-headings'
  1776. This option can also be set with on a per-file-basis with
  1777. #+STARTUP: nologreschedule
  1778. #+STARTUP: logreschedule
  1779. #+STARTUP: lognotereschedule"
  1780. :group 'org-todo
  1781. :group 'org-progress
  1782. :type '(choice
  1783. (const :tag "No logging" nil)
  1784. (const :tag "Record timestamp" time)
  1785. (const :tag "Record timestamp with note." note)))
  1786. (defcustom org-log-redeadline nil
  1787. "Information to record when the deadline date of a tasks is modified.
  1788. Possible values are:
  1789. nil Don't add anything, just change the date
  1790. time Add a time stamp to the task
  1791. note Prompt for a note and add it with template `org-log-note-headings'
  1792. This option can also be set with on a per-file-basis with
  1793. #+STARTUP: nologredeadline
  1794. #+STARTUP: logredeadline
  1795. #+STARTUP: lognoteredeadline
  1796. You can have local logging settings for a subtree by setting the LOGGING
  1797. property to one or more of these keywords."
  1798. :group 'org-todo
  1799. :group 'org-progress
  1800. :type '(choice
  1801. (const :tag "No logging" nil)
  1802. (const :tag "Record timestamp" time)
  1803. (const :tag "Record timestamp with note." note)))
  1804. (defcustom org-log-note-clock-out nil
  1805. "Non-nil means record a note when clocking out of an item.
  1806. This can also be configured on a per-file basis by adding one of
  1807. the following lines anywhere in the buffer:
  1808. #+STARTUP: lognoteclock-out
  1809. #+STARTUP: nolognoteclock-out"
  1810. :group 'org-todo
  1811. :group 'org-progress
  1812. :type 'boolean)
  1813. (defcustom org-log-done-with-time t
  1814. "Non-nil means the CLOSED time stamp will contain date and time.
  1815. When nil, only the date will be recorded."
  1816. :group 'org-progress
  1817. :type 'boolean)
  1818. (defcustom org-log-note-headings
  1819. '((done . "CLOSING NOTE %t")
  1820. (state . "State %-12s from %-12S %t")
  1821. (note . "Note taken on %t")
  1822. (reschedule . "Rescheduled from %S on %t")
  1823. (delschedule . "Not scheduled, was %S on %t")
  1824. (redeadline . "New deadline from %S on %t")
  1825. (deldeadline . "Removed deadline, was %S on %t")
  1826. (refile . "Refiled on %t")
  1827. (clock-out . ""))
  1828. "Headings for notes added to entries.
  1829. The value is an alist, with the car being a symbol indicating the note
  1830. context, and the cdr is the heading to be used. The heading may also be the
  1831. empty string.
  1832. %t in the heading will be replaced by a time stamp.
  1833. %s will be replaced by the new TODO state, in double quotes.
  1834. %S will be replaced by the old TODO state, in double quotes.
  1835. %u will be replaced by the user name.
  1836. %U will be replaced by the full user name.
  1837. In fact, it is not a good idea to change the `state' entry, because
  1838. agenda log mode depends on the format of these entries."
  1839. :group 'org-todo
  1840. :group 'org-progress
  1841. :type '(list :greedy t
  1842. (cons (const :tag "Heading when closing an item" done) string)
  1843. (cons (const :tag
  1844. "Heading when changing todo state (todo sequence only)"
  1845. state) string)
  1846. (cons (const :tag "Heading when just taking a note" note) string)
  1847. (cons (const :tag "Heading when clocking out" clock-out) string)
  1848. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1849. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1850. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1851. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1852. (cons (const :tag "Heading when refiling" refile) string)))
  1853. (unless (assq 'note org-log-note-headings)
  1854. (push '(note . "%t") org-log-note-headings))
  1855. (defcustom org-log-into-drawer nil
  1856. "Non-nil means insert state change notes and time stamps into a drawer.
  1857. When nil, state changes notes will be inserted after the headline and
  1858. any scheduling and clock lines, but not inside a drawer.
  1859. The value of this variable should be the name of the drawer to use.
  1860. LOGBOOK is proposed at the default drawer for this purpose, you can
  1861. also set this to a string to define the drawer of your choice.
  1862. A value of t is also allowed, representing \"LOGBOOK\".
  1863. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1864. will be ignored.
  1865. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1866. a subtree."
  1867. :group 'org-todo
  1868. :group 'org-progress
  1869. :type '(choice
  1870. (const :tag "Not into a drawer" nil)
  1871. (const :tag "LOGBOOK" t)
  1872. (string :tag "Other")))
  1873. (if (fboundp 'defvaralias)
  1874. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1875. (defun org-log-into-drawer ()
  1876. "Return the value of `org-log-into-drawer', but let properties overrule.
  1877. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1878. used instead of the default value."
  1879. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1880. (cond
  1881. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1882. ((equal p "t") "LOGBOOK")
  1883. (t p))))
  1884. (defcustom org-log-state-notes-insert-after-drawers nil
  1885. "Non-nil means insert state change notes after any drawers in entry.
  1886. Only the drawers that *immediately* follow the headline and the
  1887. deadline/scheduled line are skipped.
  1888. When nil, insert notes right after the heading and perhaps the line
  1889. with deadline/scheduling if present.
  1890. This variable will have no effect if `org-log-into-drawer' is
  1891. set."
  1892. :group 'org-todo
  1893. :group 'org-progress
  1894. :type 'boolean)
  1895. (defcustom org-log-states-order-reversed t
  1896. "Non-nil means the latest state note will be directly after heading.
  1897. When nil, the state change notes will be ordered according to time."
  1898. :group 'org-todo
  1899. :group 'org-progress
  1900. :type 'boolean)
  1901. (defcustom org-todo-repeat-to-state nil
  1902. "The TODO state to which a repeater should return the repeating task.
  1903. By default this is the first task in a TODO sequence, or the previous state
  1904. in a TODO_TYP set. But you can specify another task here.
  1905. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1906. :group 'org-todo
  1907. :type '(choice (const :tag "Head of sequence" nil)
  1908. (string :tag "Specific state")))
  1909. (defcustom org-log-repeat 'time
  1910. "Non-nil means record moving through the DONE state when triggering repeat.
  1911. An auto-repeating task is immediately switched back to TODO when
  1912. marked DONE. If you are not logging state changes (by adding \"@\"
  1913. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1914. record a closing note, there will be no record of the task moving
  1915. through DONE. This variable forces taking a note anyway.
  1916. nil Don't force a record
  1917. time Record a time stamp
  1918. note Record a note
  1919. This option can also be set with on a per-file-basis with
  1920. #+STARTUP: logrepeat
  1921. #+STARTUP: lognoterepeat
  1922. #+STARTUP: nologrepeat
  1923. You can have local logging settings for a subtree by setting the LOGGING
  1924. property to one or more of these keywords."
  1925. :group 'org-todo
  1926. :group 'org-progress
  1927. :type '(choice
  1928. (const :tag "Don't force a record" nil)
  1929. (const :tag "Force recording the DONE state" time)
  1930. (const :tag "Force recording a note with the DONE state" note)))
  1931. (defgroup org-priorities nil
  1932. "Priorities in Org-mode."
  1933. :tag "Org Priorities"
  1934. :group 'org-todo)
  1935. (defcustom org-enable-priority-commands t
  1936. "Non-nil means priority commands are active.
  1937. When nil, these commands will be disabled, so that you never accidentally
  1938. set a priority."
  1939. :group 'org-priorities
  1940. :type 'boolean)
  1941. (defcustom org-highest-priority ?A
  1942. "The highest priority of TODO items. A character like ?A, ?B etc.
  1943. Must have a smaller ASCII number than `org-lowest-priority'."
  1944. :group 'org-priorities
  1945. :type 'character)
  1946. (defcustom org-lowest-priority ?C
  1947. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1948. Must have a larger ASCII number than `org-highest-priority'."
  1949. :group 'org-priorities
  1950. :type 'character)
  1951. (defcustom org-default-priority ?B
  1952. "The default priority of TODO items.
  1953. This is the priority an item get if no explicit priority is given."
  1954. :group 'org-priorities
  1955. :type 'character)
  1956. (defcustom org-priority-start-cycle-with-default t
  1957. "Non-nil means start with default priority when starting to cycle.
  1958. When this is nil, the first step in the cycle will be (depending on the
  1959. command used) one higher or lower that the default priority."
  1960. :group 'org-priorities
  1961. :type 'boolean)
  1962. (defgroup org-time nil
  1963. "Options concerning time stamps and deadlines in Org-mode."
  1964. :tag "Org Time"
  1965. :group 'org)
  1966. (defcustom org-insert-labeled-timestamps-at-point nil
  1967. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1968. When nil, these labeled time stamps are forces into the second line of an
  1969. entry, just after the headline. When scheduling from the global TODO list,
  1970. the time stamp will always be forced into the second line."
  1971. :group 'org-time
  1972. :type 'boolean)
  1973. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1974. "Formats for `format-time-string' which are used for time stamps.
  1975. It is not recommended to change this constant.")
  1976. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1977. "Number of minutes to round time stamps to.
  1978. These are two values, the first applies when first creating a time stamp.
  1979. The second applies when changing it with the commands `S-up' and `S-down'.
  1980. When changing the time stamp, this means that it will change in steps
  1981. of N minutes, as given by the second value.
  1982. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1983. numbers should be factors of 60, so for example 5, 10, 15.
  1984. When this is larger than 1, you can still force an exact time-stamp by using
  1985. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1986. and by using a prefix arg to `S-up/down' to specify the exact number
  1987. of minutes to shift."
  1988. :group 'org-time
  1989. :get '(lambda (var) ; Make sure all entries have 5 elements
  1990. (if (integerp (default-value var))
  1991. (list (default-value var) 5)
  1992. (default-value var)))
  1993. :type '(list
  1994. (integer :tag "when inserting times")
  1995. (integer :tag "when modifying times")))
  1996. ;; Normalize old customizations of this variable.
  1997. (when (integerp org-time-stamp-rounding-minutes)
  1998. (setq org-time-stamp-rounding-minutes
  1999. (list org-time-stamp-rounding-minutes
  2000. org-time-stamp-rounding-minutes)))
  2001. (defcustom org-display-custom-times nil
  2002. "Non-nil means overlay custom formats over all time stamps.
  2003. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2004. To turn this on on a per-file basis, insert anywhere in the file:
  2005. #+STARTUP: customtime"
  2006. :group 'org-time
  2007. :set 'set-default
  2008. :type 'sexp)
  2009. (make-variable-buffer-local 'org-display-custom-times)
  2010. (defcustom org-time-stamp-custom-formats
  2011. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2012. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2013. These are overlayed over the default ISO format if the variable
  2014. `org-display-custom-times' is set. Time like %H:%M should be at the
  2015. end of the second format. The custom formats are also honored by export
  2016. commands, if custom time display is turned on at the time of export."
  2017. :group 'org-time
  2018. :type 'sexp)
  2019. (defun org-time-stamp-format (&optional long inactive)
  2020. "Get the right format for a time string."
  2021. (let ((f (if long (cdr org-time-stamp-formats)
  2022. (car org-time-stamp-formats))))
  2023. (if inactive
  2024. (concat "[" (substring f 1 -1) "]")
  2025. f)))
  2026. (defcustom org-time-clocksum-format "%d:%02d"
  2027. "The format string used when creating CLOCKSUM lines, or when
  2028. org-mode generates a time duration."
  2029. :group 'org-time
  2030. :type 'string)
  2031. (defcustom org-time-clocksum-use-fractional nil
  2032. "If non-nil, \\[org-clock-display] uses fractional times.
  2033. org-mode generates a time duration."
  2034. :group 'org-time
  2035. :type 'boolean)
  2036. (defcustom org-time-clocksum-fractional-format "%.2f"
  2037. "The format string used when creating CLOCKSUM lines, or when
  2038. org-mode generates a time duration."
  2039. :group 'org-time
  2040. :type 'string)
  2041. (defcustom org-deadline-warning-days 14
  2042. "No. of days before expiration during which a deadline becomes active.
  2043. This variable governs the display in sparse trees and in the agenda.
  2044. When 0 or negative, it means use this number (the absolute value of it)
  2045. even if a deadline has a different individual lead time specified.
  2046. Custom commands can set this variable in the options section."
  2047. :group 'org-time
  2048. :group 'org-agenda-daily/weekly
  2049. :type 'integer)
  2050. (defcustom org-read-date-prefer-future t
  2051. "Non-nil means assume future for incomplete date input from user.
  2052. This affects the following situations:
  2053. 1. The user gives a month but not a year.
  2054. For example, if it is april and you enter \"feb 2\", this will be read
  2055. as feb 2, *next* year. \"May 5\", however, will be this year.
  2056. 2. The user gives a day, but no month.
  2057. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2058. read this as the third of *next* month. However, if you enter \"17\",
  2059. it will be considered as *this* month.
  2060. If you set this variable to the symbol `time', then also the following
  2061. will work:
  2062. 3. If the user gives a time, but no day. If the time is before now,
  2063. to will be interpreted as tomorrow.
  2064. Currently none of this works for ISO week specifications.
  2065. When this option is nil, the current day, month and year will always be
  2066. used as defaults."
  2067. :group 'org-time
  2068. :type '(choice
  2069. (const :tag "Never" nil)
  2070. (const :tag "Check month and day" t)
  2071. (const :tag "Check month, day, and time" time)))
  2072. (defcustom org-read-date-display-live t
  2073. "Non-nil means display current interpretation of date prompt live.
  2074. This display will be in an overlay, in the minibuffer."
  2075. :group 'org-time
  2076. :type 'boolean)
  2077. (defcustom org-read-date-popup-calendar t
  2078. "Non-nil means pop up a calendar when prompting for a date.
  2079. In the calendar, the date can be selected with mouse-1. However, the
  2080. minibuffer will also be active, and you can simply enter the date as well.
  2081. When nil, only the minibuffer will be available."
  2082. :group 'org-time
  2083. :type 'boolean)
  2084. (if (fboundp 'defvaralias)
  2085. (defvaralias 'org-popup-calendar-for-date-prompt
  2086. 'org-read-date-popup-calendar))
  2087. (defcustom org-read-date-minibuffer-setup-hook nil
  2088. "Hook to be used to set up keys for the date/time interface.
  2089. Add key definitions to `minibuffer-local-map', which will be a temporary
  2090. copy."
  2091. :group 'org-time
  2092. :type 'hook)
  2093. (defcustom org-extend-today-until 0
  2094. "The hour when your day really ends. Must be an integer.
  2095. This has influence for the following applications:
  2096. - When switching the agenda to \"today\". It it is still earlier than
  2097. the time given here, the day recognized as TODAY is actually yesterday.
  2098. - When a date is read from the user and it is still before the time given
  2099. here, the current date and time will be assumed to be yesterday, 23:59.
  2100. Also, timestamps inserted in remember templates follow this rule.
  2101. IMPORTANT: This is a feature whose implementation is and likely will
  2102. remain incomplete. Really, it is only here because past midnight seems to
  2103. be the favorite working time of John Wiegley :-)"
  2104. :group 'org-time
  2105. :type 'integer)
  2106. (defcustom org-edit-timestamp-down-means-later nil
  2107. "Non-nil means S-down will increase the time in a time stamp.
  2108. When nil, S-up will increase."
  2109. :group 'org-time
  2110. :type 'boolean)
  2111. (defcustom org-calendar-follow-timestamp-change t
  2112. "Non-nil means make the calendar window follow timestamp changes.
  2113. When a timestamp is modified and the calendar window is visible, it will be
  2114. moved to the new date."
  2115. :group 'org-time
  2116. :type 'boolean)
  2117. (defgroup org-tags nil
  2118. "Options concerning tags in Org-mode."
  2119. :tag "Org Tags"
  2120. :group 'org)
  2121. (defcustom org-tag-alist nil
  2122. "List of tags allowed in Org-mode files.
  2123. When this list is nil, Org-mode will base TAG input on what is already in the
  2124. buffer.
  2125. The value of this variable is an alist, the car of each entry must be a
  2126. keyword as a string, the cdr may be a character that is used to select
  2127. that tag through the fast-tag-selection interface.
  2128. See the manual for details."
  2129. :group 'org-tags
  2130. :type '(repeat
  2131. (choice
  2132. (cons (string :tag "Tag name")
  2133. (character :tag "Access char"))
  2134. (list :tag "Start radio group"
  2135. (const :startgroup)
  2136. (option (string :tag "Group description")))
  2137. (list :tag "End radio group"
  2138. (const :endgroup)
  2139. (option (string :tag "Group description")))
  2140. (const :tag "New line" (:newline)))))
  2141. (defcustom org-tag-persistent-alist nil
  2142. "List of tags that will always appear in all Org-mode files.
  2143. This is in addition to any in buffer settings or customizations
  2144. of `org-tag-alist'.
  2145. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2146. The value of this variable is an alist, the car of each entry must be a
  2147. keyword as a string, the cdr may be a character that is used to select
  2148. that tag through the fast-tag-selection interface.
  2149. See the manual for details.
  2150. To disable these tags on a per-file basis, insert anywhere in the file:
  2151. #+STARTUP: noptag"
  2152. :group 'org-tags
  2153. :type '(repeat
  2154. (choice
  2155. (cons (string :tag "Tag name")
  2156. (character :tag "Access char"))
  2157. (const :tag "Start radio group" (:startgroup))
  2158. (const :tag "End radio group" (:endgroup))
  2159. (const :tag "New line" (:newline)))))
  2160. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2161. "If non-nil, always offer completion for all tags of all agenda files.
  2162. Instead of customizing this variable directly, you might want to
  2163. set it locally for remember buffers, because there no list of
  2164. tags in that file can be created dynamically (there are none).
  2165. (add-hook 'org-remember-mode-hook
  2166. (lambda ()
  2167. (set (make-local-variable
  2168. 'org-complete-tags-always-offer-all-agenda-tags)
  2169. t)))"
  2170. :group 'org-tags
  2171. :type 'boolean)
  2172. (defvar org-file-tags nil
  2173. "List of tags that can be inherited by all entries in the file.
  2174. The tags will be inherited if the variable `org-use-tag-inheritance'
  2175. says they should be.
  2176. This variable is populated from #+FILETAGS lines.")
  2177. (defcustom org-use-fast-tag-selection 'auto
  2178. "Non-nil means use fast tag selection scheme.
  2179. This is a special interface to select and deselect tags with single keys.
  2180. When nil, fast selection is never used.
  2181. When the symbol `auto', fast selection is used if and only if selection
  2182. characters for tags have been configured, either through the variable
  2183. `org-tag-alist' or through a #+TAGS line in the buffer.
  2184. When t, fast selection is always used and selection keys are assigned
  2185. automatically if necessary."
  2186. :group 'org-tags
  2187. :type '(choice
  2188. (const :tag "Always" t)
  2189. (const :tag "Never" nil)
  2190. (const :tag "When selection characters are configured" 'auto)))
  2191. (defcustom org-fast-tag-selection-single-key nil
  2192. "Non-nil means fast tag selection exits after first change.
  2193. When nil, you have to press RET to exit it.
  2194. During fast tag selection, you can toggle this flag with `C-c'.
  2195. This variable can also have the value `expert'. In this case, the window
  2196. displaying the tags menu is not even shown, until you press C-c again."
  2197. :group 'org-tags
  2198. :type '(choice
  2199. (const :tag "No" nil)
  2200. (const :tag "Yes" t)
  2201. (const :tag "Expert" expert)))
  2202. (defvar org-fast-tag-selection-include-todo nil
  2203. "Non-nil means fast tags selection interface will also offer TODO states.
  2204. This is an undocumented feature, you should not rely on it.")
  2205. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2206. "The column to which tags should be indented in a headline.
  2207. If this number is positive, it specifies the column. If it is negative,
  2208. it means that the tags should be flushright to that column. For example,
  2209. -80 works well for a normal 80 character screen."
  2210. :group 'org-tags
  2211. :type 'integer)
  2212. (defcustom org-auto-align-tags t
  2213. "Non-nil means realign tags after pro/demotion of TODO state change.
  2214. These operations change the length of a headline and therefore shift
  2215. the tags around. With this options turned on, after each such operation
  2216. the tags are again aligned to `org-tags-column'."
  2217. :group 'org-tags
  2218. :type 'boolean)
  2219. (defcustom org-use-tag-inheritance t
  2220. "Non-nil means tags in levels apply also for sublevels.
  2221. When nil, only the tags directly given in a specific line apply there.
  2222. This may also be a list of tags that should be inherited, or a regexp that
  2223. matches tags that should be inherited. Additional control is possible
  2224. with the variable `org-tags-exclude-from-inheritance' which gives an
  2225. explicit list of tags to be excluded from inheritance., even if the value of
  2226. `org-use-tag-inheritance' would select it for inheritance.
  2227. If this option is t, a match early-on in a tree can lead to a large
  2228. number of matches in the subtree when constructing the agenda or creating
  2229. a sparse tree. If you only want to see the first match in a tree during
  2230. a search, check out the variable `org-tags-match-list-sublevels'."
  2231. :group 'org-tags
  2232. :type '(choice
  2233. (const :tag "Not" nil)
  2234. (const :tag "Always" t)
  2235. (repeat :tag "Specific tags" (string :tag "Tag"))
  2236. (regexp :tag "Tags matched by regexp")))
  2237. (defcustom org-tags-exclude-from-inheritance nil
  2238. "List of tags that should never be inherited.
  2239. This is a way to exclude a few tags from inheritance. For way to do
  2240. the opposite, to actively allow inheritance for selected tags,
  2241. see the variable `org-use-tag-inheritance'."
  2242. :group 'org-tags
  2243. :type '(repeat (string :tag "Tag")))
  2244. (defun org-tag-inherit-p (tag)
  2245. "Check if TAG is one that should be inherited."
  2246. (cond
  2247. ((member tag org-tags-exclude-from-inheritance) nil)
  2248. ((eq org-use-tag-inheritance t) t)
  2249. ((not org-use-tag-inheritance) nil)
  2250. ((stringp org-use-tag-inheritance)
  2251. (string-match org-use-tag-inheritance tag))
  2252. ((listp org-use-tag-inheritance)
  2253. (member tag org-use-tag-inheritance))
  2254. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2255. (defcustom org-tags-match-list-sublevels t
  2256. "Non-nil means list also sublevels of headlines matching a search.
  2257. This variable applies to tags/property searches, and also to stuck
  2258. projects because this search is based on a tags match as well.
  2259. When set to the symbol `indented', sublevels are indented with
  2260. leading dots.
  2261. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2262. the sublevels of a headline matching a tag search often also match
  2263. the same search. Listing all of them can create very long lists.
  2264. Setting this variable to nil causes subtrees of a match to be skipped.
  2265. This variable is semi-obsolete and probably should always be true. It
  2266. is better to limit inheritance to certain tags using the variables
  2267. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2268. :group 'org-tags
  2269. :type '(choice
  2270. (const :tag "No, don't list them" nil)
  2271. (const :tag "Yes, do list them" t)
  2272. (const :tag "List them, indented with leading dots" indented)))
  2273. (defcustom org-tags-sort-function nil
  2274. "When set, tags are sorted using this function as a comparator"
  2275. :group 'org-tags
  2276. :type '(choice
  2277. (const :tag "No sorting" nil)
  2278. (const :tag "Alphabetical" string<)
  2279. (const :tag "Reverse alphabetical" string>)
  2280. (function :tag "Custom function" nil)))
  2281. (defvar org-tags-history nil
  2282. "History of minibuffer reads for tags.")
  2283. (defvar org-last-tags-completion-table nil
  2284. "The last used completion table for tags.")
  2285. (defvar org-after-tags-change-hook nil
  2286. "Hook that is run after the tags in a line have changed.")
  2287. (defgroup org-properties nil
  2288. "Options concerning properties in Org-mode."
  2289. :tag "Org Properties"
  2290. :group 'org)
  2291. (defcustom org-property-format "%-10s %s"
  2292. "How property key/value pairs should be formatted by `indent-line'.
  2293. When `indent-line' hits a property definition, it will format the line
  2294. according to this format, mainly to make sure that the values are
  2295. lined-up with respect to each other."
  2296. :group 'org-properties
  2297. :type 'string)
  2298. (defcustom org-use-property-inheritance nil
  2299. "Non-nil means properties apply also for sublevels.
  2300. This setting is chiefly used during property searches. Turning it on can
  2301. cause significant overhead when doing a search, which is why it is not
  2302. on by default.
  2303. When nil, only the properties directly given in the current entry count.
  2304. When t, every property is inherited. The value may also be a list of
  2305. properties that should have inheritance, or a regular expression matching
  2306. properties that should be inherited.
  2307. However, note that some special properties use inheritance under special
  2308. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2309. and the properties ending in \"_ALL\" when they are used as descriptor
  2310. for valid values of a property.
  2311. Note for programmers:
  2312. When querying an entry with `org-entry-get', you can control if inheritance
  2313. should be used. By default, `org-entry-get' looks only at the local
  2314. properties. You can request inheritance by setting the inherit argument
  2315. to t (to force inheritance) or to `selective' (to respect the setting
  2316. in this variable)."
  2317. :group 'org-properties
  2318. :type '(choice
  2319. (const :tag "Not" nil)
  2320. (const :tag "Always" t)
  2321. (repeat :tag "Specific properties" (string :tag "Property"))
  2322. (regexp :tag "Properties matched by regexp")))
  2323. (defun org-property-inherit-p (property)
  2324. "Check if PROPERTY is one that should be inherited."
  2325. (cond
  2326. ((eq org-use-property-inheritance t) t)
  2327. ((not org-use-property-inheritance) nil)
  2328. ((stringp org-use-property-inheritance)
  2329. (string-match org-use-property-inheritance property))
  2330. ((listp org-use-property-inheritance)
  2331. (member property org-use-property-inheritance))
  2332. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2333. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2334. "The default column format, if no other format has been defined.
  2335. This variable can be set on the per-file basis by inserting a line
  2336. #+COLUMNS: %25ITEM ....."
  2337. :group 'org-properties
  2338. :type 'string)
  2339. (defcustom org-columns-ellipses ".."
  2340. "The ellipses to be used when a field in column view is truncated.
  2341. When this is the empty string, as many characters as possible are shown,
  2342. but then there will be no visual indication that the field has been truncated.
  2343. When this is a string of length N, the last N characters of a truncated
  2344. field are replaced by this string. If the column is narrower than the
  2345. ellipses string, only part of the ellipses string will be shown."
  2346. :group 'org-properties
  2347. :type 'string)
  2348. (defcustom org-columns-modify-value-for-display-function nil
  2349. "Function that modifies values for display in column view.
  2350. For example, it can be used to cut out a certain part from a time stamp.
  2351. The function must take 2 arguments:
  2352. column-title The title of the column (*not* the property name)
  2353. value The value that should be modified.
  2354. The function should return the value that should be displayed,
  2355. or nil if the normal value should be used."
  2356. :group 'org-properties
  2357. :type 'function)
  2358. (defcustom org-effort-property "Effort"
  2359. "The property that is being used to keep track of effort estimates.
  2360. Effort estimates given in this property need to have the format H:MM."
  2361. :group 'org-properties
  2362. :group 'org-progress
  2363. :type '(string :tag "Property"))
  2364. (defconst org-global-properties-fixed
  2365. '(("VISIBILITY_ALL" . "folded children content all")
  2366. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2367. "List of property/value pairs that can be inherited by any entry.
  2368. These are fixed values, for the preset properties. The user variable
  2369. that can be used to add to this list is `org-global-properties'.
  2370. The entries in this list are cons cells where the car is a property
  2371. name and cdr is a string with the value. If the value represents
  2372. multiple items like an \"_ALL\" property, separate the items by
  2373. spaces.")
  2374. (defcustom org-global-properties nil
  2375. "List of property/value pairs that can be inherited by any entry.
  2376. This list will be combined with the constant `org-global-properties-fixed'.
  2377. The entries in this list are cons cells where the car is a property
  2378. name and cdr is a string with the value.
  2379. You can set buffer-local values for the same purpose in the variable
  2380. `org-file-properties' this by adding lines like
  2381. #+PROPERTY: NAME VALUE"
  2382. :group 'org-properties
  2383. :type '(repeat
  2384. (cons (string :tag "Property")
  2385. (string :tag "Value"))))
  2386. (defvar org-file-properties nil
  2387. "List of property/value pairs that can be inherited by any entry.
  2388. Valid for the current buffer.
  2389. This variable is populated from #+PROPERTY lines.")
  2390. (make-variable-buffer-local 'org-file-properties)
  2391. (defgroup org-agenda nil
  2392. "Options concerning agenda views in Org-mode."
  2393. :tag "Org Agenda"
  2394. :group 'org)
  2395. (defvar org-category nil
  2396. "Variable used by org files to set a category for agenda display.
  2397. Such files should use a file variable to set it, for example
  2398. # -*- mode: org; org-category: \"ELisp\"
  2399. or contain a special line
  2400. #+CATEGORY: ELisp
  2401. If the file does not specify a category, then file's base name
  2402. is used instead.")
  2403. (make-variable-buffer-local 'org-category)
  2404. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2405. (defcustom org-agenda-files nil
  2406. "The files to be used for agenda display.
  2407. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2408. \\[org-remove-file]. You can also use customize to edit the list.
  2409. If an entry is a directory, all files in that directory that are matched by
  2410. `org-agenda-file-regexp' will be part of the file list.
  2411. If the value of the variable is not a list but a single file name, then
  2412. the list of agenda files is actually stored and maintained in that file, one
  2413. agenda file per line. In this file paths can be given relative to
  2414. `org-directory'. Tilde expansion and environment variable substitution
  2415. are also made."
  2416. :group 'org-agenda
  2417. :type '(choice
  2418. (repeat :tag "List of files and directories" file)
  2419. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2420. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2421. "Regular expression to match files for `org-agenda-files'.
  2422. If any element in the list in that variable contains a directory instead
  2423. of a normal file, all files in that directory that are matched by this
  2424. regular expression will be included."
  2425. :group 'org-agenda
  2426. :type 'regexp)
  2427. (defcustom org-agenda-text-search-extra-files nil
  2428. "List of extra files to be searched by text search commands.
  2429. These files will be search in addition to the agenda files by the
  2430. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2431. Note that these files will only be searched for text search commands,
  2432. not for the other agenda views like todo lists, tag searches or the weekly
  2433. agenda. This variable is intended to list notes and possibly archive files
  2434. that should also be searched by these two commands.
  2435. In fact, if the first element in the list is the symbol `agenda-archives',
  2436. than all archive files of all agenda files will be added to the search
  2437. scope."
  2438. :group 'org-agenda
  2439. :type '(set :greedy t
  2440. (const :tag "Agenda Archives" agenda-archives)
  2441. (repeat :inline t (file))))
  2442. (if (fboundp 'defvaralias)
  2443. (defvaralias 'org-agenda-multi-occur-extra-files
  2444. 'org-agenda-text-search-extra-files))
  2445. (defcustom org-agenda-skip-unavailable-files nil
  2446. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2447. A nil value means to remove them, after a query, from the list."
  2448. :group 'org-agenda
  2449. :type 'boolean)
  2450. (defcustom org-calendar-to-agenda-key [?c]
  2451. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2452. The command `org-calendar-goto-agenda' will be bound to this key. The
  2453. default is the character `c' because then `c' can be used to switch back and
  2454. forth between agenda and calendar."
  2455. :group 'org-agenda
  2456. :type 'sexp)
  2457. (defcustom org-calendar-agenda-action-key [?k]
  2458. "The key to be installed in `calendar-mode-map' for agenda-action.
  2459. The command `org-agenda-action' will be bound to this key. The
  2460. default is the character `k' because we use the same key in the agenda."
  2461. :group 'org-agenda
  2462. :type 'sexp)
  2463. (defcustom org-calendar-insert-diary-entry-key [?i]
  2464. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2465. This option is irrelevant until `org-agenda-diary-file' has been configured
  2466. to point to an Org-mode file. When that is the case, the command
  2467. `org-agenda-diary-entry' will be bound to the key given here, by default
  2468. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2469. if you want to continue doing this, you need to change this to a different
  2470. key."
  2471. :group 'org-agenda
  2472. :type 'sexp)
  2473. (defcustom org-agenda-diary-file 'diary-file
  2474. "File to which to add new entries with the `i' key in agenda and calendar.
  2475. When this is the symbol `diary-file', the functionality in the Emacs
  2476. calendar will be used to add entries to the `diary-file'. But when this
  2477. points to a file, `org-agenda-diary-entry' will be used instead."
  2478. :group 'org-agenda
  2479. :type '(choice
  2480. (const :tag "The standard Emacs diary file" diary-file)
  2481. (file :tag "Special Org file diary entries")))
  2482. (eval-after-load "calendar"
  2483. '(progn
  2484. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2485. 'org-calendar-goto-agenda)
  2486. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2487. 'org-agenda-action)
  2488. (add-hook 'calendar-mode-hook
  2489. (lambda ()
  2490. (unless (eq org-agenda-diary-file 'diary-file)
  2491. (define-key calendar-mode-map
  2492. org-calendar-insert-diary-entry-key
  2493. 'org-agenda-diary-entry))))))
  2494. (defgroup org-latex nil
  2495. "Options for embedding LaTeX code into Org-mode."
  2496. :tag "Org LaTeX"
  2497. :group 'org)
  2498. (defcustom org-format-latex-options
  2499. '(:foreground default :background default :scale 1.0
  2500. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2501. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2502. "Options for creating images from LaTeX fragments.
  2503. This is a property list with the following properties:
  2504. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2505. `default' means use the foreground of the default face.
  2506. :background the background color, or \"Transparent\".
  2507. `default' means use the background of the default face.
  2508. :scale a scaling factor for the size of the images.
  2509. :html-foreground, :html-background, :html-scale
  2510. the same numbers for HTML export.
  2511. :matchers a list indicating which matchers should be used to
  2512. find LaTeX fragments. Valid members of this list are:
  2513. \"begin\" find environments
  2514. \"$1\" find single characters surrounded by $.$
  2515. \"$\" find math expressions surrounded by $...$
  2516. \"$$\" find math expressions surrounded by $$....$$
  2517. \"\\(\" find math expressions surrounded by \\(...\\)
  2518. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2519. :group 'org-latex
  2520. :type 'plist)
  2521. (defcustom org-format-latex-signal-error t
  2522. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2523. When nil, just push out a message."
  2524. :group 'org-latex
  2525. :type 'boolean)
  2526. (defcustom org-format-latex-header "\\documentclass{article}
  2527. \\usepackage[usenames]{color}
  2528. \\usepackage{amsmath}
  2529. \\usepackage[mathscr]{eucal}
  2530. \\pagestyle{empty} % do not remove
  2531. \[PACKAGES]
  2532. \[DEFAULT-PACKAGES]
  2533. % The settings below are copied from fullpage.sty
  2534. \\setlength{\\textwidth}{\\paperwidth}
  2535. \\addtolength{\\textwidth}{-3cm}
  2536. \\setlength{\\oddsidemargin}{1.5cm}
  2537. \\addtolength{\\oddsidemargin}{-2.54cm}
  2538. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2539. \\setlength{\\textheight}{\\paperheight}
  2540. \\addtolength{\\textheight}{-\\headheight}
  2541. \\addtolength{\\textheight}{-\\headsep}
  2542. \\addtolength{\\textheight}{-\\footskip}
  2543. \\addtolength{\\textheight}{-3cm}
  2544. \\setlength{\\topmargin}{1.5cm}
  2545. \\addtolength{\\topmargin}{-2.54cm}"
  2546. "The document header used for processing LaTeX fragments.
  2547. It is imperative that this header make sure that no page number
  2548. appears on the page. The package defined in the variables
  2549. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2550. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2551. will be appended."
  2552. :group 'org-latex
  2553. :type 'string)
  2554. (defvar org-format-latex-header-extra nil)
  2555. ;; The following variables are defined here because is it also used
  2556. ;; when formatting latex fragments. Originally it was part of the
  2557. ;; LaTeX exporter, which is why the name includes "export".
  2558. (defcustom org-export-latex-default-packages-alist
  2559. '(("AUTO" "inputenc")
  2560. ("T1" "fontenc")
  2561. ("" "fixltx2e")
  2562. ("" "graphicx")
  2563. ("" "longtable")
  2564. ("" "float")
  2565. ("" "wrapfig")
  2566. ("" "soul")
  2567. ("" "t1enc")
  2568. ("" "textcomp")
  2569. ("" "marvosym")
  2570. ("" "wasysym")
  2571. ("" "latexsym")
  2572. ("" "amssymb")
  2573. ("" "hyperref")
  2574. "\\tolerance=1000"
  2575. )
  2576. "Alist of default packages to be inserted in the header.
  2577. Change this only if one of the packages here causes an incompatibility
  2578. with another package you are using.
  2579. The packages in this list are needed by one part or another of Org-mode
  2580. to function properly.
  2581. - inputenc, fontenc, t1enc: for basic font and character selection
  2582. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2583. for interpreting the entities in `org-entities'. You can skip some of these
  2584. packages if you don't use any of the symbols in it.
  2585. - graphicx: for including images
  2586. - float, wrapfig: for figure placement
  2587. - longtable: for long tables
  2588. - hyperref: for cross references
  2589. Therefore you should not modify this variable unless you know what you
  2590. are doing. The one reason to change it anyway is that you might be loading
  2591. some other package that conflicts with one of the default packages.
  2592. Each cell is of the format \( \"options\" \"package\" \)."
  2593. :group 'org-export-latex
  2594. :type '(repeat
  2595. (choice
  2596. (string :tag "A line of LaTeX")
  2597. (list :tag "options/package pair"
  2598. (string :tag "options")
  2599. (string :tag "package")))))
  2600. (defcustom org-export-latex-packages-alist nil
  2601. "Alist of packages to be inserted in every LaTeX the header.
  2602. These will be inserted after `org-export-latex-default-packages-alist'.
  2603. Each cell is of the format \( \"options\" \"package\" \).
  2604. Make sure that you only lis packages here which:
  2605. - you want in every file
  2606. - do not conflict with the default packages in
  2607. `org-export-latex-default-packages-alist'
  2608. - do not conflict with the setup in `org-format-latex-header'."
  2609. :group 'org-export-latex
  2610. :type '(repeat
  2611. (choice
  2612. (string :tag "A line of LaTeX")
  2613. (list :tag "options/package pair"
  2614. (string :tag "options")
  2615. (string :tag "package")))))
  2616. (defgroup org-appearance nil
  2617. "Settings for Org-mode appearance."
  2618. :tag "Org Appearance"
  2619. :group 'org)
  2620. (defcustom org-level-color-stars-only nil
  2621. "Non-nil means fontify only the stars in each headline.
  2622. When nil, the entire headline is fontified.
  2623. Changing it requires restart of `font-lock-mode' to become effective
  2624. also in regions already fontified."
  2625. :group 'org-appearance
  2626. :type 'boolean)
  2627. (defcustom org-hide-leading-stars nil
  2628. "Non-nil means hide the first N-1 stars in a headline.
  2629. This works by using the face `org-hide' for these stars. This
  2630. face is white for a light background, and black for a dark
  2631. background. You may have to customize the face `org-hide' to
  2632. make this work.
  2633. Changing it requires restart of `font-lock-mode' to become effective
  2634. also in regions already fontified.
  2635. You may also set this on a per-file basis by adding one of the following
  2636. lines to the buffer:
  2637. #+STARTUP: hidestars
  2638. #+STARTUP: showstars"
  2639. :group 'org-appearance
  2640. :type 'boolean)
  2641. (defcustom org-hidden-keywords nil
  2642. "List of keywords that should be hidden when typed in the org buffer.
  2643. For example, add #+TITLE to this list in order to make the
  2644. document title appear in the buffer without the initial #+TITLE:
  2645. keyword."
  2646. :group 'org-appearance
  2647. :type '(set (const :tag "#+AUTHOR" author)
  2648. (const :tag "#+DATE" date)
  2649. (const :tag "#+EMAIL" email)
  2650. (const :tag "#+TITLE" title)))
  2651. (defcustom org-fontify-done-headline nil
  2652. "Non-nil means change the face of a headline if it is marked DONE.
  2653. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2654. When this is non-nil, the headline after the keyword is set to the
  2655. `org-headline-done' as an additional indication."
  2656. :group 'org-appearance
  2657. :type 'boolean)
  2658. (defcustom org-fontify-emphasized-text t
  2659. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2660. Changing this variable requires a restart of Emacs to take effect."
  2661. :group 'org-appearance
  2662. :type 'boolean)
  2663. (defcustom org-fontify-whole-heading-line nil
  2664. "Non-nil means fontify the whole line for headings.
  2665. This is useful when setting a background color for the
  2666. org-level-* faces."
  2667. :group 'org-appearance
  2668. :type 'boolean)
  2669. (defcustom org-highlight-latex-fragments-and-specials nil
  2670. "Non-nil means fontify what is treated specially by the exporters."
  2671. :group 'org-appearance
  2672. :type 'boolean)
  2673. (defcustom org-hide-emphasis-markers nil
  2674. "Non-nil mean font-lock should hide the emphasis marker characters."
  2675. :group 'org-appearance
  2676. :type 'boolean)
  2677. (defvar org-emph-re nil
  2678. "Regular expression for matching emphasis.")
  2679. (defvar org-verbatim-re nil
  2680. "Regular expression for matching verbatim text.")
  2681. (defvar org-emphasis-regexp-components) ; defined just below
  2682. (defvar org-emphasis-alist) ; defined just below
  2683. (defun org-set-emph-re (var val)
  2684. "Set variable and compute the emphasis regular expression."
  2685. (set var val)
  2686. (when (and (boundp 'org-emphasis-alist)
  2687. (boundp 'org-emphasis-regexp-components)
  2688. org-emphasis-alist org-emphasis-regexp-components)
  2689. (let* ((e org-emphasis-regexp-components)
  2690. (pre (car e))
  2691. (post (nth 1 e))
  2692. (border (nth 2 e))
  2693. (body (nth 3 e))
  2694. (nl (nth 4 e))
  2695. (body1 (concat body "*?"))
  2696. (markers (mapconcat 'car org-emphasis-alist ""))
  2697. (vmarkers (mapconcat
  2698. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2699. org-emphasis-alist "")))
  2700. ;; make sure special characters appear at the right position in the class
  2701. (if (string-match "\\^" markers)
  2702. (setq markers (concat (replace-match "" t t markers) "^")))
  2703. (if (string-match "-" markers)
  2704. (setq markers (concat (replace-match "" t t markers) "-")))
  2705. (if (string-match "\\^" vmarkers)
  2706. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2707. (if (string-match "-" vmarkers)
  2708. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2709. (if (> nl 0)
  2710. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2711. (int-to-string nl) "\\}")))
  2712. ;; Make the regexp
  2713. (setq org-emph-re
  2714. (concat "\\([" pre "]\\|^\\)"
  2715. "\\("
  2716. "\\([" markers "]\\)"
  2717. "\\("
  2718. "[^" border "]\\|"
  2719. "[^" border "]"
  2720. body1
  2721. "[^" border "]"
  2722. "\\)"
  2723. "\\3\\)"
  2724. "\\([" post "]\\|$\\)"))
  2725. (setq org-verbatim-re
  2726. (concat "\\([" pre "]\\|^\\)"
  2727. "\\("
  2728. "\\([" vmarkers "]\\)"
  2729. "\\("
  2730. "[^" border "]\\|"
  2731. "[^" border "]"
  2732. body1
  2733. "[^" border "]"
  2734. "\\)"
  2735. "\\3\\)"
  2736. "\\([" post "]\\|$\\)")))))
  2737. (defcustom org-emphasis-regexp-components
  2738. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2739. "Components used to build the regular expression for emphasis.
  2740. This is a list with 6 entries. Terminology: In an emphasis string
  2741. like \" *strong word* \", we call the initial space PREMATCH, the final
  2742. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2743. and \"trong wor\" is the body. The different components in this variable
  2744. specify what is allowed/forbidden in each part:
  2745. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2746. post Chars allowed as postmatch. End of line will be allowed too.
  2747. border The chars *forbidden* as border characters.
  2748. body-regexp A regexp like \".\" to match a body character. Don't use
  2749. non-shy groups here, and don't allow newline here.
  2750. newline The maximum number of newlines allowed in an emphasis exp.
  2751. Use customize to modify this, or restart Emacs after changing it."
  2752. :group 'org-appearance
  2753. :set 'org-set-emph-re
  2754. :type '(list
  2755. (sexp :tag "Allowed chars in pre ")
  2756. (sexp :tag "Allowed chars in post ")
  2757. (sexp :tag "Forbidden chars in border ")
  2758. (sexp :tag "Regexp for body ")
  2759. (integer :tag "number of newlines allowed")
  2760. (option (boolean :tag "Please ignore this button"))))
  2761. (defcustom org-emphasis-alist
  2762. `(("*" bold "<b>" "</b>")
  2763. ("/" italic "<i>" "</i>")
  2764. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2765. ("=" org-code "<code>" "</code>" verbatim)
  2766. ("~" org-verbatim "<code>" "</code>" verbatim)
  2767. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2768. "<del>" "</del>")
  2769. )
  2770. "Special syntax for emphasized text.
  2771. Text starting and ending with a special character will be emphasized, for
  2772. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2773. characters, the face to be used by font-lock for highlighting in Org-mode
  2774. Emacs buffers, and the HTML tags to be used for this.
  2775. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2776. Use customize to modify this, or restart Emacs after changing it."
  2777. :group 'org-appearance
  2778. :set 'org-set-emph-re
  2779. :type '(repeat
  2780. (list
  2781. (string :tag "Marker character")
  2782. (choice
  2783. (face :tag "Font-lock-face")
  2784. (plist :tag "Face property list"))
  2785. (string :tag "HTML start tag")
  2786. (string :tag "HTML end tag")
  2787. (option (const verbatim)))))
  2788. (defvar org-protecting-blocks
  2789. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2790. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2791. This is needed for font-lock setup.")
  2792. ;;; Miscellaneous options
  2793. (defgroup org-completion nil
  2794. "Completion in Org-mode."
  2795. :tag "Org Completion"
  2796. :group 'org)
  2797. (defcustom org-completion-use-ido nil
  2798. "Non-nil means use ido completion wherever possible.
  2799. Note that `ido-mode' must be active for this variable to be relevant.
  2800. If you decide to turn this variable on, you might well want to turn off
  2801. `org-outline-path-complete-in-steps'.
  2802. See also `org-completion-use-iswitchb'."
  2803. :group 'org-completion
  2804. :type 'boolean)
  2805. (defcustom org-completion-use-iswitchb nil
  2806. "Non-nil means use iswitchb completion wherever possible.
  2807. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2808. If you decide to turn this variable on, you might well want to turn off
  2809. `org-outline-path-complete-in-steps'.
  2810. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2811. :group 'org-completion
  2812. :type 'boolean)
  2813. (defcustom org-completion-fallback-command 'hippie-expand
  2814. "The expansion command called by \\[org-complete] in normal context.
  2815. Normal means no org-mode-specific context."
  2816. :group 'org-completion
  2817. :type 'function)
  2818. ;;; Functions and variables from their packages
  2819. ;; Declared here to avoid compiler warnings
  2820. ;; XEmacs only
  2821. (defvar outline-mode-menu-heading)
  2822. (defvar outline-mode-menu-show)
  2823. (defvar outline-mode-menu-hide)
  2824. (defvar zmacs-regions) ; XEmacs regions
  2825. ;; Emacs only
  2826. (defvar mark-active)
  2827. ;; Various packages
  2828. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2829. (declare-function calendar-forward-day "cal-move" (arg))
  2830. (declare-function calendar-goto-date "cal-move" (date))
  2831. (declare-function calendar-goto-today "cal-move" ())
  2832. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2833. (defvar calc-embedded-close-formula)
  2834. (defvar calc-embedded-open-formula)
  2835. (declare-function cdlatex-tab "ext:cdlatex" ())
  2836. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2837. (defvar font-lock-unfontify-region-function)
  2838. (declare-function iswitchb-read-buffer "iswitchb"
  2839. (prompt &optional default require-match start matches-set))
  2840. (defvar iswitchb-temp-buflist)
  2841. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2842. (defvar org-agenda-tags-todo-honor-ignore-options)
  2843. (declare-function org-agenda-skip "org-agenda" ())
  2844. (declare-function
  2845. org-format-agenda-item "org-agenda"
  2846. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2847. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2848. (declare-function org-agenda-change-all-lines "org-agenda"
  2849. (newhead hdmarker &optional fixface just-this))
  2850. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2851. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2852. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2853. (beg end))
  2854. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2855. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2856. "org-agenda" (&optional end))
  2857. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2858. (declare-function org-indent-mode "org-indent" (&optional arg))
  2859. (declare-function parse-time-string "parse-time" (string))
  2860. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2861. (defvar remember-data-file)
  2862. (defvar texmathp-why)
  2863. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2864. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2865. (defvar w3m-current-url)
  2866. (defvar w3m-current-title)
  2867. (defvar org-latex-regexps)
  2868. ;;; Autoload and prepare some org modules
  2869. ;; Some table stuff that needs to be defined here, because it is used
  2870. ;; by the functions setting up org-mode or checking for table context.
  2871. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2872. "Detects an org-type or table-type table.")
  2873. (defconst org-table-line-regexp "^[ \t]*|"
  2874. "Detects an org-type table line.")
  2875. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2876. "Detects an org-type table line.")
  2877. (defconst org-table-hline-regexp "^[ \t]*|-"
  2878. "Detects an org-type table hline.")
  2879. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2880. "Detects a table-type table hline.")
  2881. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2882. "Searching from within a table (any type) this finds the first line
  2883. outside the table.")
  2884. ;; Autoload the functions in org-table.el that are needed by functions here.
  2885. (eval-and-compile
  2886. (org-autoload "org-table"
  2887. '(org-table-align org-table-begin org-table-blank-field
  2888. org-table-convert org-table-convert-region org-table-copy-down
  2889. org-table-copy-region org-table-create
  2890. org-table-create-or-convert-from-region
  2891. org-table-create-with-table.el org-table-current-dline
  2892. org-table-cut-region org-table-delete-column org-table-edit-field
  2893. org-table-edit-formulas org-table-end org-table-eval-formula
  2894. org-table-export org-table-field-info
  2895. org-table-get-stored-formulas org-table-goto-column
  2896. org-table-hline-and-move org-table-import org-table-insert-column
  2897. org-table-insert-hline org-table-insert-row org-table-iterate
  2898. org-table-justify-field-maybe org-table-kill-row
  2899. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2900. org-table-move-column org-table-move-column-left
  2901. org-table-move-column-right org-table-move-row
  2902. org-table-move-row-down org-table-move-row-up
  2903. org-table-next-field org-table-next-row org-table-paste-rectangle
  2904. org-table-previous-field org-table-recalculate
  2905. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2906. org-table-toggle-coordinate-overlays
  2907. org-table-toggle-formula-debugger org-table-wrap-region
  2908. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2909. (defun org-at-table-p (&optional table-type)
  2910. "Return t if the cursor is inside an org-type table.
  2911. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2912. (if org-enable-table-editor
  2913. (save-excursion
  2914. (beginning-of-line 1)
  2915. (looking-at (if table-type org-table-any-line-regexp
  2916. org-table-line-regexp)))
  2917. nil))
  2918. (defsubst org-table-p () (org-at-table-p))
  2919. (defun org-at-table.el-p ()
  2920. "Return t if and only if we are at a table.el table."
  2921. (and (org-at-table-p 'any)
  2922. (save-excursion
  2923. (goto-char (org-table-begin 'any))
  2924. (looking-at org-table1-hline-regexp))))
  2925. (defun org-table-recognize-table.el ()
  2926. "If there is a table.el table nearby, recognize it and move into it."
  2927. (if org-table-tab-recognizes-table.el
  2928. (if (org-at-table.el-p)
  2929. (progn
  2930. (beginning-of-line 1)
  2931. (if (looking-at org-table-dataline-regexp)
  2932. nil
  2933. (if (looking-at org-table1-hline-regexp)
  2934. (progn
  2935. (beginning-of-line 2)
  2936. (if (looking-at org-table-any-border-regexp)
  2937. (beginning-of-line -1)))))
  2938. (if (re-search-forward "|" (org-table-end t) t)
  2939. (progn
  2940. (require 'table)
  2941. (if (table--at-cell-p (point))
  2942. t
  2943. (message "recognizing table.el table...")
  2944. (table-recognize-table)
  2945. (message "recognizing table.el table...done")))
  2946. (error "This should not happen..."))
  2947. t)
  2948. nil)
  2949. nil))
  2950. (defun org-at-table-hline-p ()
  2951. "Return t if the cursor is inside a hline in a table."
  2952. (if org-enable-table-editor
  2953. (save-excursion
  2954. (beginning-of-line 1)
  2955. (looking-at org-table-hline-regexp))
  2956. nil))
  2957. (defvar org-table-clean-did-remove-column nil)
  2958. (defun org-table-map-tables (function)
  2959. "Apply FUNCTION to the start of all tables in the buffer."
  2960. (save-excursion
  2961. (save-restriction
  2962. (widen)
  2963. (goto-char (point-min))
  2964. (while (re-search-forward org-table-any-line-regexp nil t)
  2965. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2966. (beginning-of-line 1)
  2967. (when (looking-at org-table-line-regexp)
  2968. (save-excursion (funcall function))
  2969. (or (looking-at org-table-line-regexp)
  2970. (forward-char 1)))
  2971. (re-search-forward org-table-any-border-regexp nil 1))))
  2972. (message "Mapping tables: done"))
  2973. ;; Declare and autoload functions from org-exp.el & Co
  2974. (declare-function org-default-export-plist "org-exp")
  2975. (declare-function org-infile-export-plist "org-exp")
  2976. (declare-function org-get-current-options "org-exp")
  2977. (eval-and-compile
  2978. (org-autoload "org-exp"
  2979. '(org-export org-export-visible
  2980. org-insert-export-options-template
  2981. org-table-clean-before-export))
  2982. (org-autoload "org-ascii"
  2983. '(org-export-as-ascii org-export-ascii-preprocess
  2984. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2985. org-export-region-as-ascii))
  2986. (org-autoload "org-latex"
  2987. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2988. org-replace-region-by-latex org-export-region-as-latex
  2989. org-export-as-latex org-export-as-pdf
  2990. org-export-as-pdf-and-open))
  2991. (org-autoload "org-html"
  2992. '(org-export-as-html-and-open
  2993. org-export-as-html-batch org-export-as-html-to-buffer
  2994. org-replace-region-by-html org-export-region-as-html
  2995. org-export-as-html))
  2996. (org-autoload "org-docbook"
  2997. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2998. org-replace-region-by-docbook org-export-region-as-docbook
  2999. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3000. org-export-as-docbook))
  3001. (org-autoload "org-icalendar"
  3002. '(org-export-icalendar-this-file
  3003. org-export-icalendar-all-agenda-files
  3004. org-export-icalendar-combine-agenda-files))
  3005. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3006. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3007. ;; Declare and autoload functions from org-agenda.el
  3008. (eval-and-compile
  3009. (org-autoload "org-agenda"
  3010. '(org-agenda org-agenda-list org-search-view
  3011. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3012. org-diary org-agenda-to-appt
  3013. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3014. ;; Autoload org-remember
  3015. (eval-and-compile
  3016. (org-autoload "org-remember"
  3017. '(org-remember-insinuate org-remember-annotation
  3018. org-remember-apply-template org-remember org-remember-handler)))
  3019. ;; Autoload org-clock.el
  3020. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3021. (beg end))
  3022. (declare-function org-clock-update-mode-line "org-clock" ())
  3023. (declare-function org-resolve-clocks "org-clock"
  3024. (&optional also-non-dangling-p prompt last-valid))
  3025. (defvar org-clock-start-time)
  3026. (defvar org-clock-marker (make-marker)
  3027. "Marker recording the last clock-in.")
  3028. (defvar org-clock-hd-marker (make-marker)
  3029. "Marker recording the last clock-in, but the headline position.")
  3030. (defvar org-clock-heading ""
  3031. "The heading of the current clock entry.")
  3032. (defun org-clock-is-active ()
  3033. "Return non-nil if clock is currently running.
  3034. The return value is actually the clock marker."
  3035. (marker-buffer org-clock-marker))
  3036. (eval-and-compile
  3037. (org-autoload
  3038. "org-clock"
  3039. '(org-clock-in org-clock-out org-clock-cancel
  3040. org-clock-goto org-clock-sum org-clock-display
  3041. org-clock-remove-overlays org-clock-report
  3042. org-clocktable-shift org-dblock-write:clocktable
  3043. org-get-clocktable org-resolve-clocks)))
  3044. (defun org-clock-update-time-maybe ()
  3045. "If this is a CLOCK line, update it and return t.
  3046. Otherwise, return nil."
  3047. (interactive)
  3048. (save-excursion
  3049. (beginning-of-line 1)
  3050. (skip-chars-forward " \t")
  3051. (when (looking-at org-clock-string)
  3052. (let ((re (concat "[ \t]*" org-clock-string
  3053. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3054. "\\([ \t]*=>.*\\)?\\)?"))
  3055. ts te h m s neg)
  3056. (cond
  3057. ((not (looking-at re))
  3058. nil)
  3059. ((not (match-end 2))
  3060. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3061. (> org-clock-marker (point))
  3062. (<= org-clock-marker (point-at-eol)))
  3063. ;; The clock is running here
  3064. (setq org-clock-start-time
  3065. (apply 'encode-time
  3066. (org-parse-time-string (match-string 1))))
  3067. (org-clock-update-mode-line)))
  3068. (t
  3069. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3070. (end-of-line 1)
  3071. (setq ts (match-string 1)
  3072. te (match-string 3))
  3073. (setq s (- (org-float-time
  3074. (apply 'encode-time (org-parse-time-string te)))
  3075. (org-float-time
  3076. (apply 'encode-time (org-parse-time-string ts))))
  3077. neg (< s 0)
  3078. s (abs s)
  3079. h (floor (/ s 3600))
  3080. s (- s (* 3600 h))
  3081. m (floor (/ s 60))
  3082. s (- s (* 60 s)))
  3083. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3084. t))))))
  3085. (defun org-check-running-clock ()
  3086. "Check if the current buffer contains the running clock.
  3087. If yes, offer to stop it and to save the buffer with the changes."
  3088. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3089. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3090. (buffer-name))))
  3091. (org-clock-out)
  3092. (when (y-or-n-p "Save changed buffer?")
  3093. (save-buffer))))
  3094. (defun org-clocktable-try-shift (dir n)
  3095. "Check if this line starts a clock table, if yes, shift the time block."
  3096. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3097. (org-clocktable-shift dir n)))
  3098. ;; Autoload org-timer.el
  3099. (eval-and-compile
  3100. (org-autoload
  3101. "org-timer"
  3102. '(org-timer-start org-timer org-timer-item
  3103. org-timer-change-times-in-region
  3104. org-timer-set-timer
  3105. org-timer-reset-timers
  3106. org-timer-show-remaining-time)))
  3107. ;; Autoload org-feed.el
  3108. (eval-and-compile
  3109. (org-autoload
  3110. "org-feed"
  3111. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3112. ;; Autoload org-indent.el
  3113. ;; Define the variable already here, to make sure we have it.
  3114. (defvar org-indent-mode nil
  3115. "Non-nil if Org-Indent mode is enabled.
  3116. Use the command `org-indent-mode' to change this variable.")
  3117. (eval-and-compile
  3118. (org-autoload
  3119. "org-indent"
  3120. '(org-indent-mode)))
  3121. ;; Autoload org-mobile.el
  3122. (eval-and-compile
  3123. (org-autoload
  3124. "org-mobile"
  3125. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3126. ;; Autoload archiving code
  3127. ;; The stuff that is needed for cycling and tags has to be defined here.
  3128. (defgroup org-archive nil
  3129. "Options concerning archiving in Org-mode."
  3130. :tag "Org Archive"
  3131. :group 'org-structure)
  3132. (defcustom org-archive-location "%s_archive::"
  3133. "The location where subtrees should be archived.
  3134. The value of this variable is a string, consisting of two parts,
  3135. separated by a double-colon. The first part is a filename and
  3136. the second part is a headline.
  3137. When the filename is omitted, archiving happens in the same file.
  3138. %s in the filename will be replaced by the current file
  3139. name (without the directory part). Archiving to a different file
  3140. is useful to keep archived entries from contributing to the
  3141. Org-mode Agenda.
  3142. The archived entries will be filed as subtrees of the specified
  3143. headline. When the headline is omitted, the subtrees are simply
  3144. filed away at the end of the file, as top-level entries. Also in
  3145. the heading you can use %s to represent the file name, this can be
  3146. useful when using the same archive for a number of different files.
  3147. Here are a few examples:
  3148. \"%s_archive::\"
  3149. If the current file is Projects.org, archive in file
  3150. Projects.org_archive, as top-level trees. This is the default.
  3151. \"::* Archived Tasks\"
  3152. Archive in the current file, under the top-level headline
  3153. \"* Archived Tasks\".
  3154. \"~/org/archive.org::\"
  3155. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3156. \"~/org/archive.org::From %s\"
  3157. Archive in file ~/org/archive.org (absolute path), under headlines
  3158. \"From FILENAME\" where file name is the current file name.
  3159. \"basement::** Finished Tasks\"
  3160. Archive in file ./basement (relative path), as level 3 trees
  3161. below the level 2 heading \"** Finished Tasks\".
  3162. You may set this option on a per-file basis by adding to the buffer a
  3163. line like
  3164. #+ARCHIVE: basement::** Finished Tasks
  3165. You may also define it locally for a subtree by setting an ARCHIVE property
  3166. in the entry. If such a property is found in an entry, or anywhere up
  3167. the hierarchy, it will be used."
  3168. :group 'org-archive
  3169. :type 'string)
  3170. (defcustom org-archive-tag "ARCHIVE"
  3171. "The tag that marks a subtree as archived.
  3172. An archived subtree does not open during visibility cycling, and does
  3173. not contribute to the agenda listings.
  3174. After changing this, font-lock must be restarted in the relevant buffers to
  3175. get the proper fontification."
  3176. :group 'org-archive
  3177. :group 'org-keywords
  3178. :type 'string)
  3179. (defcustom org-agenda-skip-archived-trees t
  3180. "Non-nil means the agenda will skip any items located in archived trees.
  3181. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3182. variable is no longer recommended, you should leave it at the value t.
  3183. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3184. :group 'org-archive
  3185. :group 'org-agenda-skip
  3186. :type 'boolean)
  3187. (defcustom org-columns-skip-archived-trees t
  3188. "Non-nil means ignore archived trees when creating column view."
  3189. :group 'org-archive
  3190. :group 'org-properties
  3191. :type 'boolean)
  3192. (defcustom org-cycle-open-archived-trees nil
  3193. "Non-nil means `org-cycle' will open archived trees.
  3194. An archived tree is a tree marked with the tag ARCHIVE.
  3195. When nil, archived trees will stay folded. You can still open them with
  3196. normal outline commands like `show-all', but not with the cycling commands."
  3197. :group 'org-archive
  3198. :group 'org-cycle
  3199. :type 'boolean)
  3200. (defcustom org-sparse-tree-open-archived-trees nil
  3201. "Non-nil means sparse tree construction shows matches in archived trees.
  3202. When nil, matches in these trees are highlighted, but the trees are kept in
  3203. collapsed state."
  3204. :group 'org-archive
  3205. :group 'org-sparse-trees
  3206. :type 'boolean)
  3207. (defun org-cycle-hide-archived-subtrees (state)
  3208. "Re-hide all archived subtrees after a visibility state change."
  3209. (when (and (not org-cycle-open-archived-trees)
  3210. (not (memq state '(overview folded))))
  3211. (save-excursion
  3212. (let* ((globalp (memq state '(contents all)))
  3213. (beg (if globalp (point-min) (point)))
  3214. (end (if globalp (point-max) (org-end-of-subtree t))))
  3215. (org-hide-archived-subtrees beg end)
  3216. (goto-char beg)
  3217. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3218. (message "%s" (substitute-command-keys
  3219. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3220. (defun org-force-cycle-archived ()
  3221. "Cycle subtree even if it is archived."
  3222. (interactive)
  3223. (setq this-command 'org-cycle)
  3224. (let ((org-cycle-open-archived-trees t))
  3225. (call-interactively 'org-cycle)))
  3226. (defun org-hide-archived-subtrees (beg end)
  3227. "Re-hide all archived subtrees after a visibility state change."
  3228. (save-excursion
  3229. (let* ((re (concat ":" org-archive-tag ":")))
  3230. (goto-char beg)
  3231. (while (re-search-forward re end t)
  3232. (when (org-on-heading-p)
  3233. (org-flag-subtree t)
  3234. (org-end-of-subtree t))))))
  3235. (defun org-flag-subtree (flag)
  3236. (save-excursion
  3237. (org-back-to-heading t)
  3238. (outline-end-of-heading)
  3239. (outline-flag-region (point)
  3240. (progn (org-end-of-subtree t) (point))
  3241. flag)))
  3242. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3243. (eval-and-compile
  3244. (org-autoload "org-archive"
  3245. '(org-add-archive-files org-archive-subtree
  3246. org-archive-to-archive-sibling org-toggle-archive-tag
  3247. org-archive-subtree-default
  3248. org-archive-subtree-default-with-confirmation)))
  3249. ;; Autoload Column View Code
  3250. (declare-function org-columns-number-to-string "org-colview")
  3251. (declare-function org-columns-get-format-and-top-level "org-colview")
  3252. (declare-function org-columns-compute "org-colview")
  3253. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3254. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3255. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3256. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3257. ;; Autoload ID code
  3258. (declare-function org-id-store-link "org-id")
  3259. (declare-function org-id-locations-load "org-id")
  3260. (declare-function org-id-locations-save "org-id")
  3261. (defvar org-id-track-globally)
  3262. (org-autoload "org-id"
  3263. '(org-id-get-create org-id-new org-id-copy org-id-get
  3264. org-id-get-with-outline-path-completion
  3265. org-id-get-with-outline-drilling
  3266. org-id-goto org-id-find org-id-store-link))
  3267. ;; Autoload Plotting Code
  3268. (org-autoload "org-plot"
  3269. '(org-plot/gnuplot))
  3270. ;;; Variables for pre-computed regular expressions, all buffer local
  3271. (defvar org-drawer-regexp nil
  3272. "Matches first line of a hidden block.")
  3273. (make-variable-buffer-local 'org-drawer-regexp)
  3274. (defvar org-todo-regexp nil
  3275. "Matches any of the TODO state keywords.")
  3276. (make-variable-buffer-local 'org-todo-regexp)
  3277. (defvar org-not-done-regexp nil
  3278. "Matches any of the TODO state keywords except the last one.")
  3279. (make-variable-buffer-local 'org-not-done-regexp)
  3280. (defvar org-not-done-heading-regexp nil
  3281. "Matches a TODO headline that is not done.")
  3282. (make-variable-buffer-local 'org-not-done-regexp)
  3283. (defvar org-todo-line-regexp nil
  3284. "Matches a headline and puts TODO state into group 2 if present.")
  3285. (make-variable-buffer-local 'org-todo-line-regexp)
  3286. (defvar org-complex-heading-regexp nil
  3287. "Matches a headline and puts everything into groups:
  3288. group 1: the stars
  3289. group 2: The todo keyword, maybe
  3290. group 3: Priority cookie
  3291. group 4: True headline
  3292. group 5: Tags")
  3293. (make-variable-buffer-local 'org-complex-heading-regexp)
  3294. (defvar org-complex-heading-regexp-format nil)
  3295. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3296. (defvar org-todo-line-tags-regexp nil
  3297. "Matches a headline and puts TODO state into group 2 if present.
  3298. Also put tags into group 4 if tags are present.")
  3299. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3300. (defvar org-nl-done-regexp nil
  3301. "Matches newline followed by a headline with the DONE keyword.")
  3302. (make-variable-buffer-local 'org-nl-done-regexp)
  3303. (defvar org-looking-at-done-regexp nil
  3304. "Matches the DONE keyword a point.")
  3305. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3306. (defvar org-ds-keyword-length 12
  3307. "Maximum length of the Deadline and SCHEDULED keywords.")
  3308. (make-variable-buffer-local 'org-ds-keyword-length)
  3309. (defvar org-deadline-regexp nil
  3310. "Matches the DEADLINE keyword.")
  3311. (make-variable-buffer-local 'org-deadline-regexp)
  3312. (defvar org-deadline-time-regexp nil
  3313. "Matches the DEADLINE keyword together with a time stamp.")
  3314. (make-variable-buffer-local 'org-deadline-time-regexp)
  3315. (defvar org-deadline-line-regexp nil
  3316. "Matches the DEADLINE keyword and the rest of the line.")
  3317. (make-variable-buffer-local 'org-deadline-line-regexp)
  3318. (defvar org-scheduled-regexp nil
  3319. "Matches the SCHEDULED keyword.")
  3320. (make-variable-buffer-local 'org-scheduled-regexp)
  3321. (defvar org-scheduled-time-regexp nil
  3322. "Matches the SCHEDULED keyword together with a time stamp.")
  3323. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3324. (defvar org-closed-time-regexp nil
  3325. "Matches the CLOSED keyword together with a time stamp.")
  3326. (make-variable-buffer-local 'org-closed-time-regexp)
  3327. (defvar org-keyword-time-regexp nil
  3328. "Matches any of the 4 keywords, together with the time stamp.")
  3329. (make-variable-buffer-local 'org-keyword-time-regexp)
  3330. (defvar org-keyword-time-not-clock-regexp nil
  3331. "Matches any of the 3 keywords, together with the time stamp.")
  3332. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3333. (defvar org-maybe-keyword-time-regexp nil
  3334. "Matches a timestamp, possibly preceeded by a keyword.")
  3335. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3336. (defvar org-planning-or-clock-line-re nil
  3337. "Matches a line with planning or clock info.")
  3338. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3339. (defvar org-all-time-keywords nil
  3340. "List of time keywords.")
  3341. (make-variable-buffer-local 'org-all-time-keywords)
  3342. (defconst org-plain-time-of-day-regexp
  3343. (concat
  3344. "\\(\\<[012]?[0-9]"
  3345. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3346. "\\(--?"
  3347. "\\(\\<[012]?[0-9]"
  3348. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3349. "\\)?")
  3350. "Regular expression to match a plain time or time range.
  3351. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3352. groups carry important information:
  3353. 0 the full match
  3354. 1 the first time, range or not
  3355. 8 the second time, if it is a range.")
  3356. (defconst org-plain-time-extension-regexp
  3357. (concat
  3358. "\\(\\<[012]?[0-9]"
  3359. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3360. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3361. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3362. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3363. groups carry important information:
  3364. 0 the full match
  3365. 7 hours of duration
  3366. 9 minutes of duration")
  3367. (defconst org-stamp-time-of-day-regexp
  3368. (concat
  3369. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3370. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3371. "\\(--?"
  3372. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3373. "Regular expression to match a timestamp time or time range.
  3374. After a match, the following groups carry important information:
  3375. 0 the full match
  3376. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3377. 2 the first time, range or not
  3378. 4 the second time, if it is a range.")
  3379. (defconst org-startup-options
  3380. '(("fold" org-startup-folded t)
  3381. ("overview" org-startup-folded t)
  3382. ("nofold" org-startup-folded nil)
  3383. ("showall" org-startup-folded nil)
  3384. ("showeverything" org-startup-folded showeverything)
  3385. ("content" org-startup-folded content)
  3386. ("indent" org-startup-indented t)
  3387. ("noindent" org-startup-indented nil)
  3388. ("hidestars" org-hide-leading-stars t)
  3389. ("showstars" org-hide-leading-stars nil)
  3390. ("odd" org-odd-levels-only t)
  3391. ("oddeven" org-odd-levels-only nil)
  3392. ("align" org-startup-align-all-tables t)
  3393. ("noalign" org-startup-align-all-tables nil)
  3394. ("customtime" org-display-custom-times t)
  3395. ("logdone" org-log-done time)
  3396. ("lognotedone" org-log-done note)
  3397. ("nologdone" org-log-done nil)
  3398. ("lognoteclock-out" org-log-note-clock-out t)
  3399. ("nolognoteclock-out" org-log-note-clock-out nil)
  3400. ("logrepeat" org-log-repeat state)
  3401. ("lognoterepeat" org-log-repeat note)
  3402. ("nologrepeat" org-log-repeat nil)
  3403. ("logreschedule" org-log-reschedule time)
  3404. ("lognotereschedule" org-log-reschedule note)
  3405. ("nologreschedule" org-log-reschedule nil)
  3406. ("logredeadline" org-log-redeadline time)
  3407. ("lognoteredeadline" org-log-redeadline note)
  3408. ("nologredeadline" org-log-redeadline nil)
  3409. ("logrefile" org-log-refile time)
  3410. ("lognoterefile" org-log-refile note)
  3411. ("nologrefile" org-log-refile nil)
  3412. ("fninline" org-footnote-define-inline t)
  3413. ("nofninline" org-footnote-define-inline nil)
  3414. ("fnlocal" org-footnote-section nil)
  3415. ("fnauto" org-footnote-auto-label t)
  3416. ("fnprompt" org-footnote-auto-label nil)
  3417. ("fnconfirm" org-footnote-auto-label confirm)
  3418. ("fnplain" org-footnote-auto-label plain)
  3419. ("fnadjust" org-footnote-auto-adjust t)
  3420. ("nofnadjust" org-footnote-auto-adjust nil)
  3421. ("constcgs" constants-unit-system cgs)
  3422. ("constSI" constants-unit-system SI)
  3423. ("noptag" org-tag-persistent-alist nil)
  3424. ("hideblocks" org-hide-block-startup t)
  3425. ("nohideblocks" org-hide-block-startup nil)
  3426. ("beamer" org-startup-with-beamer-mode t))
  3427. "Variable associated with STARTUP options for org-mode.
  3428. Each element is a list of three items: The startup options as written
  3429. in the #+STARTUP line, the corresponding variable, and the value to
  3430. set this variable to if the option is found. An optional forth element PUSH
  3431. means to push this value onto the list in the variable.")
  3432. (defun org-set-regexps-and-options ()
  3433. "Precompute regular expressions for current buffer."
  3434. (when (org-mode-p)
  3435. (org-set-local 'org-todo-kwd-alist nil)
  3436. (org-set-local 'org-todo-key-alist nil)
  3437. (org-set-local 'org-todo-key-trigger nil)
  3438. (org-set-local 'org-todo-keywords-1 nil)
  3439. (org-set-local 'org-done-keywords nil)
  3440. (org-set-local 'org-todo-heads nil)
  3441. (org-set-local 'org-todo-sets nil)
  3442. (org-set-local 'org-todo-log-states nil)
  3443. (org-set-local 'org-file-properties nil)
  3444. (org-set-local 'org-file-tags nil)
  3445. (let ((re (org-make-options-regexp
  3446. '("CATEGORY" "TODO" "COLUMNS"
  3447. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3448. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3449. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3450. (splitre "[ \t]+")
  3451. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3452. tail sep kws1 prio props ftags drawers beamer-p
  3453. ext-setup-or-nil setup-contents (start 0))
  3454. (save-excursion
  3455. (save-restriction
  3456. (widen)
  3457. (goto-char (point-min))
  3458. (while (or (and ext-setup-or-nil
  3459. (string-match re ext-setup-or-nil start)
  3460. (setq start (match-end 0)))
  3461. (and (setq ext-setup-or-nil nil start 0)
  3462. (re-search-forward re nil t)))
  3463. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3464. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3465. (cond
  3466. ((equal key "CATEGORY")
  3467. (if (string-match "[ \t]+$" value)
  3468. (setq value (replace-match "" t t value)))
  3469. (setq cat value))
  3470. ((member key '("SEQ_TODO" "TODO"))
  3471. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3472. ((equal key "TYP_TODO")
  3473. (push (cons 'type (org-split-string value splitre)) kwds))
  3474. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3475. ;; general TODO-like setup
  3476. (push (cons (intern (downcase (match-string 1 key)))
  3477. (org-split-string value splitre)) kwds))
  3478. ((equal key "TAGS")
  3479. (setq tags (append tags (if tags '("\\n") nil)
  3480. (org-split-string value splitre))))
  3481. ((equal key "COLUMNS")
  3482. (org-set-local 'org-columns-default-format value))
  3483. ((equal key "LINK")
  3484. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3485. (push (cons (match-string 1 value)
  3486. (org-trim (match-string 2 value)))
  3487. links)))
  3488. ((equal key "PRIORITIES")
  3489. (setq prio (org-split-string value " +")))
  3490. ((equal key "PROPERTY")
  3491. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3492. (push (cons (match-string 1 value) (match-string 2 value))
  3493. props)))
  3494. ((equal key "FILETAGS")
  3495. (when (string-match "\\S-" value)
  3496. (setq ftags
  3497. (append
  3498. ftags
  3499. (apply 'append
  3500. (mapcar (lambda (x) (org-split-string x ":"))
  3501. (org-split-string value)))))))
  3502. ((equal key "DRAWERS")
  3503. (setq drawers (org-split-string value splitre)))
  3504. ((equal key "CONSTANTS")
  3505. (setq const (append const (org-split-string value splitre))))
  3506. ((equal key "STARTUP")
  3507. (let ((opts (org-split-string value splitre))
  3508. l var val)
  3509. (while (setq l (pop opts))
  3510. (when (setq l (assoc l org-startup-options))
  3511. (setq var (nth 1 l) val (nth 2 l))
  3512. (if (not (nth 3 l))
  3513. (set (make-local-variable var) val)
  3514. (if (not (listp (symbol-value var)))
  3515. (set (make-local-variable var) nil))
  3516. (set (make-local-variable var) (symbol-value var))
  3517. (add-to-list var val))))))
  3518. ((equal key "ARCHIVE")
  3519. (string-match " *$" value)
  3520. (setq arch (replace-match "" t t value))
  3521. (remove-text-properties 0 (length arch)
  3522. '(face t fontified t) arch))
  3523. ((equal key "LATEX_CLASS")
  3524. (setq beamer-p (equal value "beamer")))
  3525. ((equal key "SETUPFILE")
  3526. (setq setup-contents (org-file-contents
  3527. (expand-file-name
  3528. (org-remove-double-quotes value))
  3529. 'noerror))
  3530. (if (not ext-setup-or-nil)
  3531. (setq ext-setup-or-nil setup-contents start 0)
  3532. (setq ext-setup-or-nil
  3533. (concat (substring ext-setup-or-nil 0 start)
  3534. "\n" setup-contents "\n"
  3535. (substring ext-setup-or-nil start)))))
  3536. ))))
  3537. (when cat
  3538. (org-set-local 'org-category (intern cat))
  3539. (push (cons "CATEGORY" cat) props))
  3540. (when prio
  3541. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3542. (setq prio (mapcar 'string-to-char prio))
  3543. (org-set-local 'org-highest-priority (nth 0 prio))
  3544. (org-set-local 'org-lowest-priority (nth 1 prio))
  3545. (org-set-local 'org-default-priority (nth 2 prio)))
  3546. (and props (org-set-local 'org-file-properties (nreverse props)))
  3547. (and ftags (org-set-local 'org-file-tags
  3548. (mapcar 'org-add-prop-inherited ftags)))
  3549. (and drawers (org-set-local 'org-drawers drawers))
  3550. (and arch (org-set-local 'org-archive-location arch))
  3551. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3552. ;; Process the TODO keywords
  3553. (unless kwds
  3554. ;; Use the global values as if they had been given locally.
  3555. (setq kwds (default-value 'org-todo-keywords))
  3556. (if (stringp (car kwds))
  3557. (setq kwds (list (cons org-todo-interpretation
  3558. (default-value 'org-todo-keywords)))))
  3559. (setq kwds (reverse kwds)))
  3560. (setq kwds (nreverse kwds))
  3561. (let (inter kws kw)
  3562. (while (setq kws (pop kwds))
  3563. (let ((kws (or
  3564. (run-hook-with-args-until-success
  3565. 'org-todo-setup-filter-hook kws)
  3566. kws)))
  3567. (setq inter (pop kws) sep (member "|" kws)
  3568. kws0 (delete "|" (copy-sequence kws))
  3569. kwsa nil
  3570. kws1 (mapcar
  3571. (lambda (x)
  3572. ;; 1 2
  3573. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3574. (progn
  3575. (setq kw (match-string 1 x)
  3576. key (and (match-end 2) (match-string 2 x))
  3577. log (org-extract-log-state-settings x))
  3578. (push (cons kw (and key (string-to-char key))) kwsa)
  3579. (and log (push log org-todo-log-states))
  3580. kw)
  3581. (error "Invalid TODO keyword %s" x)))
  3582. kws0)
  3583. kwsa (if kwsa (append '((:startgroup))
  3584. (nreverse kwsa)
  3585. '((:endgroup))))
  3586. hw (car kws1)
  3587. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3588. tail (list inter hw (car dws) (org-last dws))))
  3589. (add-to-list 'org-todo-heads hw 'append)
  3590. (push kws1 org-todo-sets)
  3591. (setq org-done-keywords (append org-done-keywords dws nil))
  3592. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3593. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3594. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3595. (setq org-todo-sets (nreverse org-todo-sets)
  3596. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3597. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3598. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3599. ;; Process the constants
  3600. (when const
  3601. (let (e cst)
  3602. (while (setq e (pop const))
  3603. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3604. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3605. (setq org-table-formula-constants-local cst)))
  3606. ;; Process the tags.
  3607. (when tags
  3608. (let (e tgs)
  3609. (while (setq e (pop tags))
  3610. (cond
  3611. ((equal e "{") (push '(:startgroup) tgs))
  3612. ((equal e "}") (push '(:endgroup) tgs))
  3613. ((equal e "\\n") (push '(:newline) tgs))
  3614. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3615. (push (cons (match-string 1 e)
  3616. (string-to-char (match-string 2 e)))
  3617. tgs))
  3618. (t (push (list e) tgs))))
  3619. (org-set-local 'org-tag-alist nil)
  3620. (while (setq e (pop tgs))
  3621. (or (and (stringp (car e))
  3622. (assoc (car e) org-tag-alist))
  3623. (push e org-tag-alist)))))
  3624. ;; Compute the regular expressions and other local variables
  3625. (if (not org-done-keywords)
  3626. (setq org-done-keywords (and org-todo-keywords-1
  3627. (list (org-last org-todo-keywords-1)))))
  3628. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3629. (length org-scheduled-string)
  3630. (length org-clock-string)
  3631. (length org-closed-string)))
  3632. org-drawer-regexp
  3633. (concat "^[ \t]*:\\("
  3634. (mapconcat 'regexp-quote org-drawers "\\|")
  3635. "\\):[ \t]*$")
  3636. org-not-done-keywords
  3637. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3638. org-todo-regexp
  3639. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3640. "\\|") "\\)\\>")
  3641. org-not-done-regexp
  3642. (concat "\\<\\("
  3643. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3644. "\\)\\>")
  3645. org-not-done-heading-regexp
  3646. (concat "^\\(\\*+\\)[ \t]+\\("
  3647. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3648. "\\)\\>")
  3649. org-todo-line-regexp
  3650. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3651. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3652. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3653. org-complex-heading-regexp
  3654. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3655. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3656. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3657. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3658. org-complex-heading-regexp-format
  3659. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3660. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3661. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3662. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3663. org-nl-done-regexp
  3664. (concat "\n\\*+[ \t]+"
  3665. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3666. "\\)" "\\>")
  3667. org-todo-line-tags-regexp
  3668. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3669. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3670. (org-re
  3671. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3672. org-looking-at-done-regexp
  3673. (concat "^" "\\(?:"
  3674. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3675. "\\>")
  3676. org-deadline-regexp (concat "\\<" org-deadline-string)
  3677. org-deadline-time-regexp
  3678. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3679. org-deadline-line-regexp
  3680. (concat "\\<\\(" org-deadline-string "\\).*")
  3681. org-scheduled-regexp
  3682. (concat "\\<" org-scheduled-string)
  3683. org-scheduled-time-regexp
  3684. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3685. org-closed-time-regexp
  3686. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3687. org-keyword-time-regexp
  3688. (concat "\\<\\(" org-scheduled-string
  3689. "\\|" org-deadline-string
  3690. "\\|" org-closed-string
  3691. "\\|" org-clock-string "\\)"
  3692. " *[[<]\\([^]>]+\\)[]>]")
  3693. org-keyword-time-not-clock-regexp
  3694. (concat "\\<\\(" org-scheduled-string
  3695. "\\|" org-deadline-string
  3696. "\\|" org-closed-string
  3697. "\\)"
  3698. " *[[<]\\([^]>]+\\)[]>]")
  3699. org-maybe-keyword-time-regexp
  3700. (concat "\\(\\<\\(" org-scheduled-string
  3701. "\\|" org-deadline-string
  3702. "\\|" org-closed-string
  3703. "\\|" org-clock-string "\\)\\)?"
  3704. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3705. org-planning-or-clock-line-re
  3706. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3707. "\\|" org-deadline-string
  3708. "\\|" org-closed-string "\\|" org-clock-string
  3709. "\\)\\>\\)")
  3710. org-all-time-keywords
  3711. (mapcar (lambda (w) (substring w 0 -1))
  3712. (list org-scheduled-string org-deadline-string
  3713. org-clock-string org-closed-string))
  3714. )
  3715. (org-compute-latex-and-specials-regexp)
  3716. (org-set-font-lock-defaults))))
  3717. (defun org-file-contents (file &optional noerror)
  3718. "Return the contents of FILE, as a string."
  3719. (if (or (not file)
  3720. (not (file-readable-p file)))
  3721. (if noerror
  3722. (progn
  3723. (message "Cannot read file %s" file)
  3724. (ding) (sit-for 2)
  3725. "")
  3726. (error "Cannot read file %s" file))
  3727. (with-temp-buffer
  3728. (insert-file-contents file)
  3729. (buffer-string))))
  3730. (defun org-extract-log-state-settings (x)
  3731. "Extract the log state setting from a TODO keyword string.
  3732. This will extract info from a string like \"WAIT(w@/!)\"."
  3733. (let (kw key log1 log2)
  3734. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3735. (setq kw (match-string 1 x)
  3736. key (and (match-end 2) (match-string 2 x))
  3737. log1 (and (match-end 3) (match-string 3 x))
  3738. log2 (and (match-end 4) (match-string 4 x)))
  3739. (and (or log1 log2)
  3740. (list kw
  3741. (and log1 (if (equal log1 "!") 'time 'note))
  3742. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3743. (defun org-remove-keyword-keys (list)
  3744. "Remove a pair of parenthesis at the end of each string in LIST."
  3745. (mapcar (lambda (x)
  3746. (if (string-match "(.*)$" x)
  3747. (substring x 0 (match-beginning 0))
  3748. x))
  3749. list))
  3750. (defun org-assign-fast-keys (alist)
  3751. "Assign fast keys to a keyword-key alist.
  3752. Respect keys that are already there."
  3753. (let (new e (alt ?0))
  3754. (while (setq e (pop alist))
  3755. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3756. (cdr e)) ;; Key already assigned.
  3757. (push e new)
  3758. (let ((clist (string-to-list (downcase (car e))))
  3759. (used (append new alist)))
  3760. (when (= (car clist) ?@)
  3761. (pop clist))
  3762. (while (and clist (rassoc (car clist) used))
  3763. (pop clist))
  3764. (unless clist
  3765. (while (rassoc alt used)
  3766. (incf alt)))
  3767. (push (cons (car e) (or (car clist) alt)) new))))
  3768. (nreverse new)))
  3769. ;;; Some variables used in various places
  3770. (defvar org-window-configuration nil
  3771. "Used in various places to store a window configuration.")
  3772. (defvar org-selected-window nil
  3773. "Used in various places to store a window configuration.")
  3774. (defvar org-finish-function nil
  3775. "Function to be called when `C-c C-c' is used.
  3776. This is for getting out of special buffers like remember.")
  3777. ;; FIXME: Occasionally check by commenting these, to make sure
  3778. ;; no other functions uses these, forgetting to let-bind them.
  3779. (defvar entry)
  3780. (defvar last-state)
  3781. (defvar date)
  3782. ;; Defined somewhere in this file, but used before definition.
  3783. (defvar org-entities) ;; defined in org-entities.el
  3784. (defvar org-struct-menu)
  3785. (defvar org-org-menu)
  3786. (defvar org-tbl-menu)
  3787. ;;;; Define the Org-mode
  3788. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3789. (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."))
  3790. ;; We use a before-change function to check if a table might need
  3791. ;; an update.
  3792. (defvar org-table-may-need-update t
  3793. "Indicates that a table might need an update.
  3794. This variable is set by `org-before-change-function'.
  3795. `org-table-align' sets it back to nil.")
  3796. (defun org-before-change-function (beg end)
  3797. "Every change indicates that a table might need an update."
  3798. (setq org-table-may-need-update t))
  3799. (defvar org-mode-map)
  3800. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3801. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3802. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3803. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3804. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3805. (defvar org-table-buffer-is-an nil)
  3806. (defconst org-outline-regexp "\\*+ ")
  3807. ;;;###autoload
  3808. (define-derived-mode org-mode outline-mode "Org"
  3809. "Outline-based notes management and organizer, alias
  3810. \"Carsten's outline-mode for keeping track of everything.\"
  3811. Org-mode develops organizational tasks around a NOTES file which
  3812. contains information about projects as plain text. Org-mode is
  3813. implemented on top of outline-mode, which is ideal to keep the content
  3814. of large files well structured. It supports ToDo items, deadlines and
  3815. time stamps, which magically appear in the diary listing of the Emacs
  3816. calendar. Tables are easily created with a built-in table editor.
  3817. Plain text URL-like links connect to websites, emails (VM), Usenet
  3818. messages (Gnus), BBDB entries, and any files related to the project.
  3819. For printing and sharing of notes, an Org-mode file (or a part of it)
  3820. can be exported as a structured ASCII or HTML file.
  3821. The following commands are available:
  3822. \\{org-mode-map}"
  3823. ;; Get rid of Outline menus, they are not needed
  3824. ;; Need to do this here because define-derived-mode sets up
  3825. ;; the keymap so late. Still, it is a waste to call this each time
  3826. ;; we switch another buffer into org-mode.
  3827. (if (featurep 'xemacs)
  3828. (when (boundp 'outline-mode-menu-heading)
  3829. ;; Assume this is Greg's port, it used easymenu
  3830. (easy-menu-remove outline-mode-menu-heading)
  3831. (easy-menu-remove outline-mode-menu-show)
  3832. (easy-menu-remove outline-mode-menu-hide))
  3833. (define-key org-mode-map [menu-bar headings] 'undefined)
  3834. (define-key org-mode-map [menu-bar hide] 'undefined)
  3835. (define-key org-mode-map [menu-bar show] 'undefined))
  3836. (org-load-modules-maybe)
  3837. (easy-menu-add org-org-menu)
  3838. (easy-menu-add org-tbl-menu)
  3839. (org-install-agenda-files-menu)
  3840. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3841. (org-add-to-invisibility-spec '(org-cwidth))
  3842. (org-add-to-invisibility-spec '(org-hide-block . t))
  3843. (when (featurep 'xemacs)
  3844. (org-set-local 'line-move-ignore-invisible t))
  3845. (org-set-local 'outline-regexp org-outline-regexp)
  3846. (org-set-local 'outline-level 'org-outline-level)
  3847. (when (and org-ellipsis
  3848. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3849. (fboundp 'make-glyph-code))
  3850. (unless org-display-table
  3851. (setq org-display-table (make-display-table)))
  3852. (set-display-table-slot
  3853. org-display-table 4
  3854. (vconcat (mapcar
  3855. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3856. org-ellipsis)))
  3857. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3858. (setq buffer-display-table org-display-table))
  3859. (org-set-regexps-and-options)
  3860. (when (and org-tag-faces (not org-tags-special-faces-re))
  3861. ;; tag faces set outside customize.... force initialization.
  3862. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3863. ;; Calc embedded
  3864. (org-set-local 'calc-embedded-open-mode "# ")
  3865. (modify-syntax-entry ?# "<")
  3866. (modify-syntax-entry ?@ "w")
  3867. (if org-startup-truncated (setq truncate-lines t))
  3868. (org-set-local 'font-lock-unfontify-region-function
  3869. 'org-unfontify-region)
  3870. ;; Activate before-change-function
  3871. (org-set-local 'org-table-may-need-update t)
  3872. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3873. 'local)
  3874. ;; Check for running clock before killing a buffer
  3875. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3876. ;; Paragraphs and auto-filling
  3877. (org-set-autofill-regexps)
  3878. (setq indent-line-function 'org-indent-line-function)
  3879. (org-update-radio-target-regexp)
  3880. ;; Make sure dependence stuff works reliably, even for users who set it
  3881. ;; too late :-(
  3882. (if org-enforce-todo-dependencies
  3883. (add-hook 'org-blocker-hook
  3884. 'org-block-todo-from-children-or-siblings-or-parent)
  3885. (remove-hook 'org-blocker-hook
  3886. 'org-block-todo-from-children-or-siblings-or-parent))
  3887. (if org-enforce-todo-checkbox-dependencies
  3888. (add-hook 'org-blocker-hook
  3889. 'org-block-todo-from-checkboxes)
  3890. (remove-hook 'org-blocker-hook
  3891. 'org-block-todo-from-checkboxes))
  3892. ;; Comment characters
  3893. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3894. (org-set-local 'comment-padding " ")
  3895. ;; Align options lines
  3896. (org-set-local
  3897. 'align-mode-rules-list
  3898. '((org-in-buffer-settings
  3899. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3900. (modes . '(org-mode)))))
  3901. ;; Imenu
  3902. (org-set-local 'imenu-create-index-function
  3903. 'org-imenu-get-tree)
  3904. ;; Make isearch reveal context
  3905. (if (or (featurep 'xemacs)
  3906. (not (boundp 'outline-isearch-open-invisible-function)))
  3907. ;; Emacs 21 and XEmacs make use of the hook
  3908. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3909. ;; Emacs 22 deals with this through a special variable
  3910. (org-set-local 'outline-isearch-open-invisible-function
  3911. (lambda (&rest ignore) (org-show-context 'isearch))))
  3912. ;; Turn on org-beamer-mode?
  3913. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3914. ;; If empty file that did not turn on org-mode automatically, make it to.
  3915. (if (and org-insert-mode-line-in-empty-file
  3916. (interactive-p)
  3917. (= (point-min) (point-max)))
  3918. (insert "# -*- mode: org -*-\n\n"))
  3919. (unless org-inhibit-startup
  3920. (when org-startup-align-all-tables
  3921. (let ((bmp (buffer-modified-p)))
  3922. (org-table-map-tables 'org-table-align)
  3923. (set-buffer-modified-p bmp)))
  3924. (when org-startup-indented
  3925. (require 'org-indent)
  3926. (org-indent-mode 1))
  3927. (unless org-inhibit-startup-visibility-stuff
  3928. (org-set-startup-visibility))))
  3929. (when (fboundp 'abbrev-table-put)
  3930. (abbrev-table-put org-mode-abbrev-table
  3931. :parents (list text-mode-abbrev-table)))
  3932. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3933. (defun org-current-time ()
  3934. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3935. (if (> (car org-time-stamp-rounding-minutes) 1)
  3936. (let ((r (car org-time-stamp-rounding-minutes))
  3937. (time (decode-time)))
  3938. (apply 'encode-time
  3939. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3940. (nthcdr 2 time))))
  3941. (current-time)))
  3942. ;;;; Font-Lock stuff, including the activators
  3943. (defvar org-mouse-map (make-sparse-keymap))
  3944. (org-defkey org-mouse-map
  3945. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3946. (org-defkey org-mouse-map
  3947. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3948. (when org-mouse-1-follows-link
  3949. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3950. (when org-tab-follows-link
  3951. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3952. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3953. (require 'font-lock)
  3954. (defconst org-non-link-chars "]\t\n\r<>")
  3955. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3956. "shell" "elisp"))
  3957. (defvar org-link-types-re nil
  3958. "Matches a link that has a url-like prefix like \"http:\"")
  3959. (defvar org-link-re-with-space nil
  3960. "Matches a link with spaces, optional angular brackets around it.")
  3961. (defvar org-link-re-with-space2 nil
  3962. "Matches a link with spaces, optional angular brackets around it.")
  3963. (defvar org-link-re-with-space3 nil
  3964. "Matches a link with spaces, only for internal part in bracket links.")
  3965. (defvar org-angle-link-re nil
  3966. "Matches link with angular brackets, spaces are allowed.")
  3967. (defvar org-plain-link-re nil
  3968. "Matches plain link, without spaces.")
  3969. (defvar org-bracket-link-regexp nil
  3970. "Matches a link in double brackets.")
  3971. (defvar org-bracket-link-analytic-regexp nil
  3972. "Regular expression used to analyze links.
  3973. Here is what the match groups contain after a match:
  3974. 1: http:
  3975. 2: http
  3976. 3: path
  3977. 4: [desc]
  3978. 5: desc")
  3979. (defvar org-bracket-link-analytic-regexp++ nil
  3980. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3981. (defvar org-any-link-re nil
  3982. "Regular expression matching any link.")
  3983. (defun org-make-link-regexps ()
  3984. "Update the link regular expressions.
  3985. This should be called after the variable `org-link-types' has changed."
  3986. (setq org-link-types-re
  3987. (concat
  3988. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3989. org-link-re-with-space
  3990. (concat
  3991. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3992. "\\([^" org-non-link-chars " ]"
  3993. "[^" org-non-link-chars "]*"
  3994. "[^" org-non-link-chars " ]\\)>?")
  3995. org-link-re-with-space2
  3996. (concat
  3997. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3998. "\\([^" org-non-link-chars " ]"
  3999. "[^\t\n\r]*"
  4000. "[^" org-non-link-chars " ]\\)>?")
  4001. org-link-re-with-space3
  4002. (concat
  4003. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4004. "\\([^" org-non-link-chars " ]"
  4005. "[^\t\n\r]*\\)")
  4006. org-angle-link-re
  4007. (concat
  4008. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4009. "\\([^" org-non-link-chars " ]"
  4010. "[^" org-non-link-chars "]*"
  4011. "\\)>")
  4012. org-plain-link-re
  4013. (concat
  4014. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4015. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4016. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4017. org-bracket-link-regexp
  4018. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4019. org-bracket-link-analytic-regexp
  4020. (concat
  4021. "\\[\\["
  4022. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4023. "\\([^]]+\\)"
  4024. "\\]"
  4025. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4026. "\\]")
  4027. org-bracket-link-analytic-regexp++
  4028. (concat
  4029. "\\[\\["
  4030. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4031. "\\([^]]+\\)"
  4032. "\\]"
  4033. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4034. "\\]")
  4035. org-any-link-re
  4036. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4037. org-angle-link-re "\\)\\|\\("
  4038. org-plain-link-re "\\)")))
  4039. (org-make-link-regexps)
  4040. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4041. "Regular expression for fast time stamp matching.")
  4042. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4043. "Regular expression for fast time stamp matching.")
  4044. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4045. "Regular expression matching time strings for analysis.
  4046. This one does not require the space after the date, so it can be used
  4047. on a string that terminates immediately after the date.")
  4048. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4049. "Regular expression matching time strings for analysis.")
  4050. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4051. "Regular expression matching time stamps, with groups.")
  4052. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4053. "Regular expression matching time stamps (also [..]), with groups.")
  4054. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4055. "Regular expression matching a time stamp range.")
  4056. (defconst org-tr-regexp-both
  4057. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4058. "Regular expression matching a time stamp range.")
  4059. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4060. org-ts-regexp "\\)?")
  4061. "Regular expression matching a time stamp or time stamp range.")
  4062. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4063. org-ts-regexp-both "\\)?")
  4064. "Regular expression matching a time stamp or time stamp range.
  4065. The time stamps may be either active or inactive.")
  4066. (defvar org-emph-face nil)
  4067. (defun org-do-emphasis-faces (limit)
  4068. "Run through the buffer and add overlays to links."
  4069. (let (rtn a)
  4070. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4071. (if (not (= (char-after (match-beginning 3))
  4072. (char-after (match-beginning 4))))
  4073. (progn
  4074. (setq rtn t)
  4075. (setq a (assoc (match-string 3) org-emphasis-alist))
  4076. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4077. 'face
  4078. (nth 1 a))
  4079. (and (nth 4 a)
  4080. (org-remove-flyspell-overlays-in
  4081. (match-beginning 0) (match-end 0)))
  4082. (add-text-properties (match-beginning 2) (match-end 2)
  4083. '(font-lock-multiline t))
  4084. (when org-hide-emphasis-markers
  4085. (add-text-properties (match-end 4) (match-beginning 5)
  4086. '(invisible org-link))
  4087. (add-text-properties (match-beginning 3) (match-end 3)
  4088. '(invisible org-link)))))
  4089. (backward-char 1))
  4090. rtn))
  4091. (defun org-emphasize (&optional char)
  4092. "Insert or change an emphasis, i.e. a font like bold or italic.
  4093. If there is an active region, change that region to a new emphasis.
  4094. If there is no region, just insert the marker characters and position
  4095. the cursor between them.
  4096. CHAR should be either the marker character, or the first character of the
  4097. HTML tag associated with that emphasis. If CHAR is a space, the means
  4098. to remove the emphasis of the selected region.
  4099. If char is not given (for example in an interactive call) it
  4100. will be prompted for."
  4101. (interactive)
  4102. (let ((eal org-emphasis-alist) e det
  4103. (erc org-emphasis-regexp-components)
  4104. (prompt "")
  4105. (string "") beg end move tag c s)
  4106. (if (org-region-active-p)
  4107. (setq beg (region-beginning) end (region-end)
  4108. string (buffer-substring beg end))
  4109. (setq move t))
  4110. (while (setq e (pop eal))
  4111. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4112. c (aref tag 0))
  4113. (push (cons c (string-to-char (car e))) det)
  4114. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4115. (substring tag 1)))))
  4116. (setq det (nreverse det))
  4117. (unless char
  4118. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4119. (setq char (read-char-exclusive)))
  4120. (setq char (or (cdr (assoc char det)) char))
  4121. (if (equal char ?\ )
  4122. (setq s "" move nil)
  4123. (unless (assoc (char-to-string char) org-emphasis-alist)
  4124. (error "No such emphasis marker: \"%c\"" char))
  4125. (setq s (char-to-string char)))
  4126. (while (and (> (length string) 1)
  4127. (equal (substring string 0 1) (substring string -1))
  4128. (assoc (substring string 0 1) org-emphasis-alist))
  4129. (setq string (substring string 1 -1)))
  4130. (setq string (concat s string s))
  4131. (if beg (delete-region beg end))
  4132. (unless (or (bolp)
  4133. (string-match (concat "[" (nth 0 erc) "\n]")
  4134. (char-to-string (char-before (point)))))
  4135. (insert " "))
  4136. (unless (or (eobp)
  4137. (string-match (concat "[" (nth 1 erc) "\n]")
  4138. (char-to-string (char-after (point)))))
  4139. (insert " ") (backward-char 1))
  4140. (insert string)
  4141. (and move (backward-char 1))))
  4142. (defconst org-nonsticky-props
  4143. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4144. (defsubst org-rear-nonsticky-at (pos)
  4145. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4146. (defun org-activate-plain-links (limit)
  4147. "Run through the buffer and add overlays to links."
  4148. (catch 'exit
  4149. (let (f)
  4150. (if (re-search-forward org-plain-link-re limit t)
  4151. (progn
  4152. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4153. (setq f (get-text-property (match-beginning 0) 'face))
  4154. (if (or (eq f 'org-tag)
  4155. (and (listp f) (memq 'org-tag f)))
  4156. nil
  4157. (add-text-properties (match-beginning 0) (match-end 0)
  4158. (list 'mouse-face 'highlight
  4159. 'face 'org-link
  4160. 'keymap org-mouse-map))
  4161. (org-rear-nonsticky-at (match-end 0)))
  4162. t)))))
  4163. (defun org-activate-code (limit)
  4164. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4165. (progn
  4166. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4167. (remove-text-properties (match-beginning 0) (match-end 0)
  4168. '(display t invisible t intangible t))
  4169. t)))
  4170. (defun org-fontify-meta-lines-and-blocks (limit)
  4171. "Fontify #+ lines and blocks, in the correct ways."
  4172. (let ((case-fold-search t))
  4173. (if (re-search-forward
  4174. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4175. limit t)
  4176. (let ((beg (match-beginning 0))
  4177. (beg1 (line-beginning-position 2))
  4178. (dc1 (downcase (match-string 2)))
  4179. (dc3 (downcase (match-string 3)))
  4180. end end1 quoting block-type)
  4181. (cond
  4182. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4183. ;; a single line of backend-specific content
  4184. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4185. (remove-text-properties (match-beginning 0) (match-end 0)
  4186. '(display t invisible t intangible t))
  4187. (add-text-properties (match-beginning 1) (match-end 3)
  4188. '(font-lock-fontified t face org-meta-line))
  4189. (add-text-properties (match-beginning 6) (match-end 6)
  4190. '(font-lock-fontified t face org-block))
  4191. t)
  4192. ((and (match-end 4) (equal dc3 "begin"))
  4193. ;; Truely a block
  4194. (setq block-type (downcase (match-string 5))
  4195. quoting (member block-type org-protecting-blocks))
  4196. (when (re-search-forward
  4197. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4198. nil t) ;; on purpose, we look further than LIMIT
  4199. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4200. (when quoting
  4201. (remove-text-properties beg end
  4202. '(display t invisible t intangible t)))
  4203. (add-text-properties
  4204. beg end
  4205. '(font-lock-fontified t font-lock-multiline t))
  4206. (add-text-properties beg beg1 '(face org-meta-line))
  4207. (add-text-properties end1 end '(face org-meta-line))
  4208. (cond
  4209. (quoting
  4210. (add-text-properties beg1 end1 '(face org-block)))
  4211. ((not org-fontify-quote-and-verse-blocks))
  4212. ((string= block-type "quote")
  4213. (add-text-properties beg1 end1 '(face org-quote)))
  4214. ((string= block-type "verse")
  4215. (add-text-properties beg1 end1 '(face org-verse))))
  4216. t))
  4217. ((member dc1 '("title:" "author:" "email:" "date:"))
  4218. (add-text-properties
  4219. beg (match-end 3)
  4220. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4221. '(font-lock-fontified t invisible t)
  4222. '(font-lock-fontified t face org-document-info-keyword)))
  4223. (add-text-properties
  4224. (match-beginning 6) (match-end 6)
  4225. (if (string-equal dc1 "title:")
  4226. '(font-lock-fontified t face org-document-title)
  4227. '(font-lock-fontified t face org-document-info))))
  4228. ((not (member (char-after beg) '(?\ ?\t)))
  4229. ;; just any other in-buffer setting, but not indented
  4230. (add-text-properties
  4231. beg (match-end 0)
  4232. '(font-lock-fontified t face org-meta-line))
  4233. t)
  4234. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4235. "orgtbl:" "tblfm:" "tblname:"))
  4236. (and (match-end 4) (equal dc3 "attr")))
  4237. (add-text-properties
  4238. beg (match-end 0)
  4239. '(font-lock-fontified t face org-meta-line))
  4240. t)
  4241. ((member dc3 '(" " ""))
  4242. (add-text-properties
  4243. beg (match-end 0)
  4244. '(font-lock-fontified t face font-lock-comment-face)))
  4245. (t nil))))))
  4246. (defun org-activate-angle-links (limit)
  4247. "Run through the buffer and add overlays to links."
  4248. (if (re-search-forward org-angle-link-re limit t)
  4249. (progn
  4250. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4251. (add-text-properties (match-beginning 0) (match-end 0)
  4252. (list 'mouse-face 'highlight
  4253. 'keymap org-mouse-map))
  4254. (org-rear-nonsticky-at (match-end 0))
  4255. t)))
  4256. (defun org-activate-footnote-links (limit)
  4257. "Run through the buffer and add overlays to links."
  4258. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4259. limit t)
  4260. (progn
  4261. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4262. (add-text-properties (match-beginning 2) (match-end 2)
  4263. (list 'mouse-face 'highlight
  4264. 'keymap org-mouse-map
  4265. 'help-echo
  4266. (if (= (point-at-bol) (match-beginning 2))
  4267. "Footnote definition"
  4268. "Footnote reference")
  4269. ))
  4270. (org-rear-nonsticky-at (match-end 2))
  4271. t)))
  4272. (defun org-activate-bracket-links (limit)
  4273. "Run through the buffer and add overlays to bracketed links."
  4274. (if (re-search-forward org-bracket-link-regexp limit t)
  4275. (let* ((help (concat "LINK: "
  4276. (org-match-string-no-properties 1)))
  4277. ;; FIXME: above we should remove the escapes.
  4278. ;; but that requires another match, protecting match data,
  4279. ;; a lot of overhead for font-lock.
  4280. (ip (org-maybe-intangible
  4281. (list 'invisible 'org-link
  4282. 'keymap org-mouse-map 'mouse-face 'highlight
  4283. 'font-lock-multiline t 'help-echo help)))
  4284. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4285. 'font-lock-multiline t 'help-echo help)))
  4286. ;; We need to remove the invisible property here. Table narrowing
  4287. ;; may have made some of this invisible.
  4288. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4289. (remove-text-properties (match-beginning 0) (match-end 0)
  4290. '(invisible nil))
  4291. (if (match-end 3)
  4292. (progn
  4293. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4294. (org-rear-nonsticky-at (match-beginning 3))
  4295. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4296. (org-rear-nonsticky-at (match-end 3))
  4297. (add-text-properties (match-end 3) (match-end 0) ip)
  4298. (org-rear-nonsticky-at (match-end 0)))
  4299. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4300. (org-rear-nonsticky-at (match-beginning 1))
  4301. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4302. (org-rear-nonsticky-at (match-end 1))
  4303. (add-text-properties (match-end 1) (match-end 0) ip)
  4304. (org-rear-nonsticky-at (match-end 0)))
  4305. t)))
  4306. (defun org-activate-dates (limit)
  4307. "Run through the buffer and add overlays to dates."
  4308. (if (re-search-forward org-tsr-regexp-both limit t)
  4309. (progn
  4310. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4311. (add-text-properties (match-beginning 0) (match-end 0)
  4312. (list 'mouse-face 'highlight
  4313. 'keymap org-mouse-map))
  4314. (org-rear-nonsticky-at (match-end 0))
  4315. (when org-display-custom-times
  4316. (if (match-end 3)
  4317. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4318. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4319. t)))
  4320. (defvar org-target-link-regexp nil
  4321. "Regular expression matching radio targets in plain text.")
  4322. (make-variable-buffer-local 'org-target-link-regexp)
  4323. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4324. "Regular expression matching a link target.")
  4325. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4326. "Regular expression matching a radio target.")
  4327. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4328. "Regular expression matching any target.")
  4329. (defun org-activate-target-links (limit)
  4330. "Run through the buffer and add overlays to target matches."
  4331. (when org-target-link-regexp
  4332. (let ((case-fold-search t))
  4333. (if (re-search-forward org-target-link-regexp limit t)
  4334. (progn
  4335. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4336. (add-text-properties (match-beginning 0) (match-end 0)
  4337. (list 'mouse-face 'highlight
  4338. 'keymap org-mouse-map
  4339. 'help-echo "Radio target link"
  4340. 'org-linked-text t))
  4341. (org-rear-nonsticky-at (match-end 0))
  4342. t)))))
  4343. (defun org-update-radio-target-regexp ()
  4344. "Find all radio targets in this file and update the regular expression."
  4345. (interactive)
  4346. (when (memq 'radio org-activate-links)
  4347. (setq org-target-link-regexp
  4348. (org-make-target-link-regexp (org-all-targets 'radio)))
  4349. (org-restart-font-lock)))
  4350. (defun org-hide-wide-columns (limit)
  4351. (let (s e)
  4352. (setq s (text-property-any (point) (or limit (point-max))
  4353. 'org-cwidth t))
  4354. (when s
  4355. (setq e (next-single-property-change s 'org-cwidth))
  4356. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4357. (goto-char e)
  4358. t)))
  4359. (defvar org-latex-and-specials-regexp nil
  4360. "Regular expression for highlighting export special stuff.")
  4361. (defvar org-match-substring-regexp)
  4362. (defvar org-match-substring-with-braces-regexp)
  4363. ;; This should be with the exporter code, but we also use if for font-locking
  4364. (defconst org-export-html-special-string-regexps
  4365. '(("\\\\-" . "&shy;")
  4366. ("---\\([^-]\\)" . "&mdash;\\1")
  4367. ("--\\([^-]\\)" . "&ndash;\\1")
  4368. ("\\.\\.\\." . "&hellip;"))
  4369. "Regular expressions for special string conversion.")
  4370. (defun org-compute-latex-and-specials-regexp ()
  4371. "Compute regular expression for stuff treated specially by exporters."
  4372. (if (not org-highlight-latex-fragments-and-specials)
  4373. (org-set-local 'org-latex-and-specials-regexp nil)
  4374. (require 'org-exp)
  4375. (let*
  4376. ((matchers (plist-get org-format-latex-options :matchers))
  4377. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4378. org-latex-regexps)))
  4379. (org-export-allow-BIND nil)
  4380. (options (org-combine-plists (org-default-export-plist)
  4381. (org-infile-export-plist)))
  4382. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4383. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4384. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4385. (org-export-html-expand (plist-get options :expand-quoted-html))
  4386. (org-export-with-special-strings (plist-get options :special-strings))
  4387. (re-sub
  4388. (cond
  4389. ((equal org-export-with-sub-superscripts '{})
  4390. (list org-match-substring-with-braces-regexp))
  4391. (org-export-with-sub-superscripts
  4392. (list org-match-substring-regexp))
  4393. (t nil)))
  4394. (re-latex
  4395. (if org-export-with-LaTeX-fragments
  4396. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4397. (re-macros
  4398. (if org-export-with-TeX-macros
  4399. (list (concat "\\\\"
  4400. (regexp-opt
  4401. (append (mapcar 'car (append org-entities-user
  4402. org-entities))
  4403. (if (boundp 'org-latex-entities)
  4404. (mapcar (lambda (x)
  4405. (or (car-safe x) x))
  4406. org-latex-entities)
  4407. nil))
  4408. 'words))) ; FIXME
  4409. ))
  4410. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4411. (re-special (if org-export-with-special-strings
  4412. (mapcar (lambda (x) (car x))
  4413. org-export-html-special-string-regexps)))
  4414. (re-rest
  4415. (delq nil
  4416. (list
  4417. (if org-export-html-expand "@<[^>\n]+>")
  4418. ))))
  4419. (org-set-local
  4420. 'org-latex-and-specials-regexp
  4421. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4422. re-rest) "\\|")))))
  4423. (defun org-do-latex-and-special-faces (limit)
  4424. "Run through the buffer and add overlays to links."
  4425. (when org-latex-and-specials-regexp
  4426. (let (rtn d)
  4427. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4428. limit t))
  4429. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4430. 'face))
  4431. '(org-code org-verbatim underline)))
  4432. (progn
  4433. (setq rtn t
  4434. d (cond ((member (char-after (1+ (match-beginning 0)))
  4435. '(?_ ?^)) 1)
  4436. (t 0)))
  4437. (font-lock-prepend-text-property
  4438. (+ d (match-beginning 0)) (match-end 0)
  4439. 'face 'org-latex-and-export-specials)
  4440. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4441. '(font-lock-multiline t)))))
  4442. rtn)))
  4443. (defun org-restart-font-lock ()
  4444. "Restart font-lock-mode, to force refontification."
  4445. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4446. (font-lock-mode -1)
  4447. (font-lock-mode 1)))
  4448. (defun org-all-targets (&optional radio)
  4449. "Return a list of all targets in this file.
  4450. With optional argument RADIO, only find radio targets."
  4451. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4452. rtn)
  4453. (save-excursion
  4454. (goto-char (point-min))
  4455. (while (re-search-forward re nil t)
  4456. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4457. rtn)))
  4458. (defun org-make-target-link-regexp (targets)
  4459. "Make regular expression matching all strings in TARGETS.
  4460. The regular expression finds the targets also if there is a line break
  4461. between words."
  4462. (and targets
  4463. (concat
  4464. "\\<\\("
  4465. (mapconcat
  4466. (lambda (x)
  4467. (while (string-match " +" x)
  4468. (setq x (replace-match "\\s-+" t t x)))
  4469. x)
  4470. targets
  4471. "\\|")
  4472. "\\)\\>")))
  4473. (defun org-activate-tags (limit)
  4474. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4475. (progn
  4476. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4477. (add-text-properties (match-beginning 1) (match-end 1)
  4478. (list 'mouse-face 'highlight
  4479. 'keymap org-mouse-map))
  4480. (org-rear-nonsticky-at (match-end 1))
  4481. t)))
  4482. (defun org-outline-level ()
  4483. "Compute the outline level of the heading at point.
  4484. This function assumes that the cursor is at the beginning of a line matched
  4485. by outline-regexp. Otherwise it returns garbage.
  4486. If this is called at a normal headline, the level is the number of stars.
  4487. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4488. For plain list items, if they are matched by `outline-regexp', this returns
  4489. 1000 plus the line indentation."
  4490. (save-excursion
  4491. (looking-at outline-regexp)
  4492. (if (match-beginning 1)
  4493. (+ (org-get-string-indentation (match-string 1)) 1000)
  4494. (1- (- (match-end 0) (match-beginning 0))))))
  4495. (defvar org-font-lock-keywords nil)
  4496. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4497. "Regular expression matching a property line.")
  4498. (defvar org-font-lock-hook nil
  4499. "Functions to be called for special font lock stuff.")
  4500. (defun org-font-lock-hook (limit)
  4501. (run-hook-with-args 'org-font-lock-hook limit))
  4502. (defun org-set-font-lock-defaults ()
  4503. (let* ((em org-fontify-emphasized-text)
  4504. (lk org-activate-links)
  4505. (org-font-lock-extra-keywords
  4506. (list
  4507. ;; Call the hook
  4508. '(org-font-lock-hook)
  4509. ;; Headlines
  4510. `(,(if org-fontify-whole-heading-line
  4511. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4512. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4513. (1 (org-get-level-face 1))
  4514. (2 (org-get-level-face 2))
  4515. (3 (org-get-level-face 3)))
  4516. ;; Table lines
  4517. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4518. (1 'org-table t))
  4519. ;; Table internals
  4520. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4521. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4522. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4523. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4524. ;; Drawers
  4525. (list org-drawer-regexp '(0 'org-special-keyword t))
  4526. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4527. ;; Properties
  4528. (list org-property-re
  4529. '(1 'org-special-keyword t)
  4530. '(3 'org-property-value t))
  4531. ;; Links
  4532. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4533. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4534. (if (memq 'plain lk) '(org-activate-plain-links))
  4535. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4536. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4537. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4538. (if (memq 'footnote lk) '(org-activate-footnote-links
  4539. (2 'org-footnote t)))
  4540. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4541. '(org-hide-wide-columns (0 nil append))
  4542. ;; TODO lines
  4543. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4544. '(1 (org-get-todo-face 1) t))
  4545. ;; DONE
  4546. (if org-fontify-done-headline
  4547. (list (concat "^[*]+ +\\<\\("
  4548. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4549. "\\)\\(.*\\)")
  4550. '(2 'org-headline-done t))
  4551. nil)
  4552. ;; Priorities
  4553. '(org-font-lock-add-priority-faces)
  4554. ;; Tags
  4555. '(org-font-lock-add-tag-faces)
  4556. ;; Special keywords
  4557. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4558. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4559. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4560. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4561. ;; Emphasis
  4562. (if em
  4563. (if (featurep 'xemacs)
  4564. '(org-do-emphasis-faces (0 nil append))
  4565. '(org-do-emphasis-faces)))
  4566. ;; Checkboxes
  4567. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4568. 2 'org-checkbox prepend)
  4569. (if org-provide-checkbox-statistics
  4570. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4571. (0 (org-get-checkbox-statistics-face) t)))
  4572. ;; Description list items
  4573. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4574. 2 'bold prepend)
  4575. ;; ARCHIVEd headings
  4576. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4577. '(1 'org-archived prepend))
  4578. ;; Specials
  4579. '(org-do-latex-and-special-faces)
  4580. ;; Code
  4581. '(org-activate-code (1 'org-code t))
  4582. ;; COMMENT
  4583. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4584. "\\|" org-quote-string "\\)\\>")
  4585. '(1 'org-special-keyword t))
  4586. '("^#.*" (0 'font-lock-comment-face t))
  4587. ;; Blocks and meta lines
  4588. '(org-fontify-meta-lines-and-blocks)
  4589. )))
  4590. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4591. ;; Now set the full font-lock-keywords
  4592. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4593. (org-set-local 'font-lock-defaults
  4594. '(org-font-lock-keywords t nil nil backward-paragraph))
  4595. (kill-local-variable 'font-lock-keywords) nil))
  4596. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4597. "Fontify string S like in Org-mode"
  4598. (with-temp-buffer
  4599. (insert s)
  4600. (let ((org-odd-levels-only odd-levels))
  4601. (org-mode)
  4602. (font-lock-fontify-buffer)
  4603. (buffer-string))))
  4604. (defvar org-m nil)
  4605. (defvar org-l nil)
  4606. (defvar org-f nil)
  4607. (defun org-get-level-face (n)
  4608. "Get the right face for match N in font-lock matching of headlines."
  4609. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4610. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4611. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4612. (cond
  4613. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4614. ((eq n 2) org-f)
  4615. (t (if org-level-color-stars-only nil org-f))))
  4616. (defun org-get-todo-face (kwd)
  4617. "Get the right face for a TODO keyword KWD.
  4618. If KWD is a number, get the corresponding match group."
  4619. (if (numberp kwd) (setq kwd (match-string kwd)))
  4620. (or (org-face-from-face-or-color
  4621. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4622. (and (member kwd org-done-keywords) 'org-done)
  4623. 'org-todo))
  4624. (defun org-face-from-face-or-color (context inherit face-or-color)
  4625. "Create a face list that inherits INHERIT, but sets the foreground color.
  4626. When FACE-OR-COLOR is not a string, just return it."
  4627. (if (stringp face-or-color)
  4628. (list :inherit inherit
  4629. (cdr (assoc context org-faces-easy-properties))
  4630. face-or-color)
  4631. face-or-color))
  4632. (defun org-font-lock-add-tag-faces (limit)
  4633. "Add the special tag faces."
  4634. (when (and org-tag-faces org-tags-special-faces-re)
  4635. (while (re-search-forward org-tags-special-faces-re limit t)
  4636. (add-text-properties (match-beginning 1) (match-end 1)
  4637. (list 'face (org-get-tag-face 1)
  4638. 'font-lock-fontified t))
  4639. (backward-char 1))))
  4640. (defun org-font-lock-add-priority-faces (limit)
  4641. "Add the special priority faces."
  4642. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4643. (add-text-properties
  4644. (match-beginning 0) (match-end 0)
  4645. (list 'face (or (org-face-from-face-or-color
  4646. 'priority 'org-special-keyword
  4647. (cdr (assoc (char-after (match-beginning 1))
  4648. org-priority-faces)))
  4649. 'org-special-keyword)
  4650. 'font-lock-fontified t))))
  4651. (defun org-get-tag-face (kwd)
  4652. "Get the right face for a TODO keyword KWD.
  4653. If KWD is a number, get the corresponding match group."
  4654. (if (numberp kwd) (setq kwd (match-string kwd)))
  4655. (or (org-face-from-face-or-color
  4656. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4657. 'org-tag))
  4658. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4659. "Remove fontification and activation overlays from links."
  4660. (font-lock-default-unfontify-region beg end)
  4661. (let* ((buffer-undo-list t)
  4662. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4663. (inhibit-modification-hooks t)
  4664. deactivate-mark buffer-file-name buffer-file-truename)
  4665. (remove-text-properties
  4666. beg end
  4667. (if org-indent-mode
  4668. ;; also remove line-prefix and wrap-prefix properties
  4669. '(mouse-face t keymap t org-linked-text t
  4670. invisible t intangible t
  4671. line-prefix t wrap-prefix t
  4672. org-no-flyspell t)
  4673. '(mouse-face t keymap t org-linked-text t
  4674. invisible t intangible t
  4675. org-no-flyspell t)))))
  4676. ;;;; Visibility cycling, including org-goto and indirect buffer
  4677. ;;; Cycling
  4678. (defvar org-cycle-global-status nil)
  4679. (make-variable-buffer-local 'org-cycle-global-status)
  4680. (defvar org-cycle-subtree-status nil)
  4681. (make-variable-buffer-local 'org-cycle-subtree-status)
  4682. ;;;###autoload
  4683. (defvar org-inlinetask-min-level)
  4684. (defun org-cycle (&optional arg)
  4685. "TAB-action and visibility cycling for Org-mode.
  4686. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4687. is outline visibility cycling, but it also invokes other actions
  4688. in special contexts.
  4689. - When this function is called with a prefix argument, rotate the entire
  4690. buffer through 3 states (global cycling)
  4691. 1. OVERVIEW: Show only top-level headlines.
  4692. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4693. 3. SHOW ALL: Show everything.
  4694. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4695. determined by the variable `org-startup-folded', and by any VISIBILITY
  4696. properties in the buffer.
  4697. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4698. including any drawers.
  4699. - When inside a table, re-align the table and move to the next field.
  4700. - When point is at the beginning of a headline, rotate the subtree started
  4701. by this line through 3 different states (local cycling)
  4702. 1. FOLDED: Only the main headline is shown.
  4703. 2. CHILDREN: The main headline and the direct children are shown.
  4704. From this state, you can move to one of the children
  4705. and zoom in further.
  4706. 3. SUBTREE: Show the entire subtree, including body text.
  4707. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4708. - When point is at the beginning of an empty headline and the variable
  4709. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4710. of the headline by demoting and promoting it to likely levels. This
  4711. speeds up creation document structure by presing TAB once or several
  4712. times right after creating a new headline.
  4713. - When there is a numeric prefix, go up to a heading with level ARG, do
  4714. a `show-subtree' and return to the previous cursor position. If ARG
  4715. is negative, go up that many levels.
  4716. - When point is not at the beginning of a headline, execute the global
  4717. binding for TAB, which is re-indenting the line. See the option
  4718. `org-cycle-emulate-tab' for details.
  4719. - Special case: if point is at the beginning of the buffer and there is
  4720. no headline in line 1, this function will act as if called with prefix arg.
  4721. But only if also the variable `org-cycle-global-at-bob' is t."
  4722. (interactive "P")
  4723. (org-load-modules-maybe)
  4724. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4725. (and org-cycle-level-after-item/entry-creation
  4726. (or (org-cycle-level)
  4727. (org-cycle-item-indentation))))
  4728. (let* ((limit-level
  4729. (or org-cycle-max-level
  4730. (and (boundp 'org-inlinetask-min-level)
  4731. org-inlinetask-min-level
  4732. (1- org-inlinetask-min-level))))
  4733. (nstars (and limit-level
  4734. (if org-odd-levels-only
  4735. (and limit-level (1- (* limit-level 2)))
  4736. limit-level)))
  4737. (outline-regexp
  4738. (cond
  4739. ((not (org-mode-p)) outline-regexp)
  4740. ((or (eq org-cycle-include-plain-lists 'integrate)
  4741. (and org-cycle-include-plain-lists (org-at-item-p)))
  4742. (concat "\\(?:\\*"
  4743. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4744. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4745. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4746. (bob-special (and org-cycle-global-at-bob (bobp)
  4747. (not (looking-at outline-regexp))))
  4748. (org-cycle-hook
  4749. (if bob-special
  4750. (delq 'org-optimize-window-after-visibility-change
  4751. (copy-sequence org-cycle-hook))
  4752. org-cycle-hook))
  4753. (pos (point)))
  4754. (if (or bob-special (equal arg '(4)))
  4755. ;; special case: use global cycling
  4756. (setq arg t))
  4757. (cond
  4758. ((equal arg '(16))
  4759. (org-set-startup-visibility)
  4760. (message "Startup visibility, plus VISIBILITY properties"))
  4761. ((equal arg '(64))
  4762. (show-all)
  4763. (message "Entire buffer visible, including drawers"))
  4764. ((org-at-table-p 'any)
  4765. ;; Enter the table or move to the next field in the table
  4766. (if (org-at-table.el-p)
  4767. (message "Use C-c ' to edit table.el tables")
  4768. (if arg (org-table-edit-field t)
  4769. (org-table-justify-field-maybe)
  4770. (call-interactively 'org-table-next-field))))
  4771. ((run-hook-with-args-until-success
  4772. 'org-tab-after-check-for-table-hook))
  4773. ((eq arg t) ;; Global cycling
  4774. (org-cycle-internal-global))
  4775. ((and org-drawers org-drawer-regexp
  4776. (save-excursion
  4777. (beginning-of-line 1)
  4778. (looking-at org-drawer-regexp)))
  4779. ;; Toggle block visibility
  4780. (org-flag-drawer
  4781. (not (get-char-property (match-end 0) 'invisible))))
  4782. ((integerp arg)
  4783. ;; Show-subtree, ARG levels up from here.
  4784. (save-excursion
  4785. (org-back-to-heading)
  4786. (outline-up-heading (if (< arg 0) (- arg)
  4787. (- (funcall outline-level) arg)))
  4788. (org-show-subtree)))
  4789. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4790. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4791. (org-cycle-internal-local))
  4792. ;; TAB emulation and template completion
  4793. (buffer-read-only (org-back-to-heading))
  4794. ((run-hook-with-args-until-success
  4795. 'org-tab-after-check-for-cycling-hook))
  4796. ((org-try-structure-completion))
  4797. ((org-try-cdlatex-tab))
  4798. ((run-hook-with-args-until-success
  4799. 'org-tab-before-tab-emulation-hook))
  4800. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4801. (or (not (bolp))
  4802. (not (looking-at outline-regexp))))
  4803. (call-interactively (global-key-binding "\t")))
  4804. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4805. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4806. (or (and (eq org-cycle-emulate-tab 'white)
  4807. (= (match-end 0) (point-at-eol)))
  4808. (and (eq org-cycle-emulate-tab 'whitestart)
  4809. (>= (match-end 0) pos))))
  4810. t
  4811. (eq org-cycle-emulate-tab t))
  4812. (call-interactively (global-key-binding "\t")))
  4813. (t (save-excursion
  4814. (org-back-to-heading)
  4815. (org-cycle)))))))
  4816. (defun org-cycle-internal-global ()
  4817. "Do the global cycling action."
  4818. (cond
  4819. ((and (eq last-command this-command)
  4820. (eq org-cycle-global-status 'overview))
  4821. ;; We just created the overview - now do table of contents
  4822. ;; This can be slow in very large buffers, so indicate action
  4823. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4824. (message "CONTENTS...")
  4825. (org-content)
  4826. (message "CONTENTS...done")
  4827. (setq org-cycle-global-status 'contents)
  4828. (run-hook-with-args 'org-cycle-hook 'contents))
  4829. ((and (eq last-command this-command)
  4830. (eq org-cycle-global-status 'contents))
  4831. ;; We just showed the table of contents - now show everything
  4832. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4833. (show-all)
  4834. (message "SHOW ALL")
  4835. (setq org-cycle-global-status 'all)
  4836. (run-hook-with-args 'org-cycle-hook 'all))
  4837. (t
  4838. ;; Default action: go to overview
  4839. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4840. (org-overview)
  4841. (message "OVERVIEW")
  4842. (setq org-cycle-global-status 'overview)
  4843. (run-hook-with-args 'org-cycle-hook 'overview))))
  4844. (defun org-cycle-internal-local ()
  4845. "Do the local cycling action."
  4846. (org-back-to-heading)
  4847. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4848. ;; First, some boundaries
  4849. (save-excursion
  4850. (org-back-to-heading)
  4851. (setq level (funcall outline-level))
  4852. (save-excursion
  4853. (beginning-of-line 2)
  4854. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4855. ; XEmacs does not have `next-single-char-property-change'
  4856. ; I'm not sure about Emacs 21.
  4857. (while (and (not (eobp)) ;; this is like `next-line'
  4858. (get-char-property (1- (point)) 'invisible))
  4859. (beginning-of-line 2))
  4860. (while (and (not (eobp)) ;; this is like `next-line'
  4861. (get-char-property (1- (point)) 'invisible))
  4862. (goto-char (next-single-char-property-change (point) 'invisible))
  4863. ;;;??? (or (bolp) (beginning-of-line 2))))
  4864. (and (eolp) (beginning-of-line 2))))
  4865. (setq eol (point)))
  4866. (outline-end-of-heading) (setq eoh (point))
  4867. (save-excursion
  4868. (outline-next-heading)
  4869. (setq has-children (and (org-at-heading-p t)
  4870. (> (funcall outline-level) level))))
  4871. (org-end-of-subtree t)
  4872. (unless (eobp)
  4873. (skip-chars-forward " \t\n")
  4874. (beginning-of-line 1) ; in case this is an item
  4875. )
  4876. (setq eos (if (eobp) (point) (1- (point)))))
  4877. ;; Find out what to do next and set `this-command'
  4878. (cond
  4879. ((= eos eoh)
  4880. ;; Nothing is hidden behind this heading
  4881. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4882. (message "EMPTY ENTRY")
  4883. (setq org-cycle-subtree-status nil)
  4884. (save-excursion
  4885. (goto-char eos)
  4886. (outline-next-heading)
  4887. (if (org-invisible-p) (org-flag-heading nil))))
  4888. ((and (or (>= eol eos)
  4889. (not (string-match "\\S-" (buffer-substring eol eos))))
  4890. (or has-children
  4891. (not (setq children-skipped
  4892. org-cycle-skip-children-state-if-no-children))))
  4893. ;; Entire subtree is hidden in one line: children view
  4894. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4895. (org-show-entry)
  4896. (show-children)
  4897. (message "CHILDREN")
  4898. (save-excursion
  4899. (goto-char eos)
  4900. (outline-next-heading)
  4901. (if (org-invisible-p) (org-flag-heading nil)))
  4902. (setq org-cycle-subtree-status 'children)
  4903. (run-hook-with-args 'org-cycle-hook 'children))
  4904. ((or children-skipped
  4905. (and (eq last-command this-command)
  4906. (eq org-cycle-subtree-status 'children)))
  4907. ;; We just showed the children, or no children are there,
  4908. ;; now show everything.
  4909. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4910. (org-show-subtree)
  4911. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4912. (setq org-cycle-subtree-status 'subtree)
  4913. (run-hook-with-args 'org-cycle-hook 'subtree))
  4914. (t
  4915. ;; Default action: hide the subtree.
  4916. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4917. (hide-subtree)
  4918. (message "FOLDED")
  4919. (setq org-cycle-subtree-status 'folded)
  4920. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4921. ;;;###autoload
  4922. (defun org-global-cycle (&optional arg)
  4923. "Cycle the global visibility. For details see `org-cycle'.
  4924. With C-u prefix arg, switch to startup visibility.
  4925. With a numeric prefix, show all headlines up to that level."
  4926. (interactive "P")
  4927. (let ((org-cycle-include-plain-lists
  4928. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4929. (cond
  4930. ((integerp arg)
  4931. (show-all)
  4932. (hide-sublevels arg)
  4933. (setq org-cycle-global-status 'contents))
  4934. ((equal arg '(4))
  4935. (org-set-startup-visibility)
  4936. (message "Startup visibility, plus VISIBILITY properties."))
  4937. (t
  4938. (org-cycle '(4))))))
  4939. (defun org-set-startup-visibility ()
  4940. "Set the visibility required by startup options and properties."
  4941. (cond
  4942. ((eq org-startup-folded t)
  4943. (org-cycle '(4)))
  4944. ((eq org-startup-folded 'content)
  4945. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4946. (org-cycle '(4)) (org-cycle '(4)))))
  4947. (unless (eq org-startup-folded 'showeverything)
  4948. (if org-hide-block-startup (org-hide-block-all))
  4949. (org-set-visibility-according-to-property 'no-cleanup)
  4950. (org-cycle-hide-archived-subtrees 'all)
  4951. (org-cycle-hide-drawers 'all)
  4952. (org-cycle-show-empty-lines 'all)))
  4953. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4954. "Switch subtree visibilities according to :VISIBILITY: property."
  4955. (interactive)
  4956. (let (org-show-entry-below state)
  4957. (save-excursion
  4958. (goto-char (point-min))
  4959. (while (re-search-forward
  4960. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4961. nil t)
  4962. (setq state (match-string 1))
  4963. (save-excursion
  4964. (org-back-to-heading t)
  4965. (hide-subtree)
  4966. (org-reveal)
  4967. (cond
  4968. ((equal state '("fold" "folded"))
  4969. (hide-subtree))
  4970. ((equal state "children")
  4971. (org-show-hidden-entry)
  4972. (show-children))
  4973. ((equal state "content")
  4974. (save-excursion
  4975. (save-restriction
  4976. (org-narrow-to-subtree)
  4977. (org-content))))
  4978. ((member state '("all" "showall"))
  4979. (show-subtree)))))
  4980. (unless no-cleanup
  4981. (org-cycle-hide-archived-subtrees 'all)
  4982. (org-cycle-hide-drawers 'all)
  4983. (org-cycle-show-empty-lines 'all)))))
  4984. (defun org-overview ()
  4985. "Switch to overview mode, showing only top-level headlines.
  4986. Really, this shows all headlines with level equal or greater than the level
  4987. of the first headline in the buffer. This is important, because if the
  4988. first headline is not level one, then (hide-sublevels 1) gives confusing
  4989. results."
  4990. (interactive)
  4991. (let ((level (save-excursion
  4992. (goto-char (point-min))
  4993. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4994. (progn
  4995. (goto-char (match-beginning 0))
  4996. (funcall outline-level))))))
  4997. (and level (hide-sublevels level))))
  4998. (defun org-content (&optional arg)
  4999. "Show all headlines in the buffer, like a table of contents.
  5000. With numerical argument N, show content up to level N."
  5001. (interactive "P")
  5002. (save-excursion
  5003. ;; Visit all headings and show their offspring
  5004. (and (integerp arg) (org-overview))
  5005. (goto-char (point-max))
  5006. (catch 'exit
  5007. (while (and (progn (condition-case nil
  5008. (outline-previous-visible-heading 1)
  5009. (error (goto-char (point-min))))
  5010. t)
  5011. (looking-at outline-regexp))
  5012. (if (integerp arg)
  5013. (show-children (1- arg))
  5014. (show-branches))
  5015. (if (bobp) (throw 'exit nil))))))
  5016. (defun org-optimize-window-after-visibility-change (state)
  5017. "Adjust the window after a change in outline visibility.
  5018. This function is the default value of the hook `org-cycle-hook'."
  5019. (when (get-buffer-window (current-buffer))
  5020. (cond
  5021. ((eq state 'content) nil)
  5022. ((eq state 'all) nil)
  5023. ((eq state 'folded) nil)
  5024. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5025. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5026. (defun org-remove-empty-overlays-at (pos)
  5027. "Remove outline overlays that do not contain non-white stuff."
  5028. (mapc
  5029. (lambda (o)
  5030. (and (eq 'outline (org-overlay-get o 'invisible))
  5031. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  5032. (org-overlay-end o))))
  5033. (org-delete-overlay o)))
  5034. (org-overlays-at pos)))
  5035. (defun org-clean-visibility-after-subtree-move ()
  5036. "Fix visibility issues after moving a subtree."
  5037. ;; First, find a reasonable region to look at:
  5038. ;; Start two siblings above, end three below
  5039. (let* ((beg (save-excursion
  5040. (and (org-get-last-sibling)
  5041. (org-get-last-sibling))
  5042. (point)))
  5043. (end (save-excursion
  5044. (and (org-get-next-sibling)
  5045. (org-get-next-sibling)
  5046. (org-get-next-sibling))
  5047. (if (org-at-heading-p)
  5048. (point-at-eol)
  5049. (point))))
  5050. (level (looking-at "\\*+"))
  5051. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5052. (save-excursion
  5053. (save-restriction
  5054. (narrow-to-region beg end)
  5055. (when re
  5056. ;; Properly fold already folded siblings
  5057. (goto-char (point-min))
  5058. (while (re-search-forward re nil t)
  5059. (if (and (not (org-invisible-p))
  5060. (save-excursion
  5061. (goto-char (point-at-eol)) (org-invisible-p)))
  5062. (hide-entry))))
  5063. (org-cycle-show-empty-lines 'overview)
  5064. (org-cycle-hide-drawers 'overview)))))
  5065. (defun org-cycle-show-empty-lines (state)
  5066. "Show empty lines above all visible headlines.
  5067. The region to be covered depends on STATE when called through
  5068. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5069. entire buffer covered. Note that an empty line is only shown if there
  5070. are at least `org-cycle-separator-lines' empty lines before the headline."
  5071. (when (not (= org-cycle-separator-lines 0))
  5072. (save-excursion
  5073. (let* ((n (abs org-cycle-separator-lines))
  5074. (re (cond
  5075. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5076. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5077. (t (let ((ns (number-to-string (- n 2))))
  5078. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5079. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5080. beg end b e)
  5081. (cond
  5082. ((memq state '(overview contents t))
  5083. (setq beg (point-min) end (point-max)))
  5084. ((memq state '(children folded))
  5085. (setq beg (point) end (progn (org-end-of-subtree t t)
  5086. (beginning-of-line 2)
  5087. (point)))))
  5088. (when beg
  5089. (goto-char beg)
  5090. (while (re-search-forward re end t)
  5091. (unless (get-char-property (match-end 1) 'invisible)
  5092. (setq e (match-end 1))
  5093. (if (< org-cycle-separator-lines 0)
  5094. (setq b (save-excursion
  5095. (goto-char (match-beginning 0))
  5096. (org-back-over-empty-lines)
  5097. (if (save-excursion
  5098. (goto-char (max (point-min) (1- (point))))
  5099. (org-on-heading-p))
  5100. (1- (point))
  5101. (point))))
  5102. (setq b (match-beginning 1)))
  5103. (outline-flag-region b e nil)))))))
  5104. ;; Never hide empty lines at the end of the file.
  5105. (save-excursion
  5106. (goto-char (point-max))
  5107. (outline-previous-heading)
  5108. (outline-end-of-heading)
  5109. (if (and (looking-at "[ \t\n]+")
  5110. (= (match-end 0) (point-max)))
  5111. (outline-flag-region (point) (match-end 0) nil))))
  5112. (defun org-show-empty-lines-in-parent ()
  5113. "Move to the parent and re-show empty lines before visible headlines."
  5114. (save-excursion
  5115. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5116. (org-cycle-show-empty-lines context))))
  5117. (defun org-files-list ()
  5118. "Return `org-agenda-files' list, plus all open org-mode files.
  5119. This is useful for operations that need to scan all of a user's
  5120. open and agenda-wise Org files."
  5121. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5122. (dolist (buf (buffer-list))
  5123. (with-current-buffer buf
  5124. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5125. (let ((file (expand-file-name (buffer-file-name))))
  5126. (unless (member file files)
  5127. (push file files))))))
  5128. files))
  5129. (defsubst org-entry-beginning-position ()
  5130. "Return the beginning position of the current entry."
  5131. (save-excursion (outline-back-to-heading t) (point)))
  5132. (defsubst org-entry-end-position ()
  5133. "Return the end position of the current entry."
  5134. (save-excursion (outline-next-heading) (point)))
  5135. (defun org-cycle-hide-drawers (state)
  5136. "Re-hide all drawers after a visibility state change."
  5137. (when (and (org-mode-p)
  5138. (not (memq state '(overview folded contents))))
  5139. (save-excursion
  5140. (let* ((globalp (memq state '(contents all)))
  5141. (beg (if globalp (point-min) (point)))
  5142. (end (if globalp (point-max)
  5143. (if (eq state 'children)
  5144. (save-excursion (outline-next-heading) (point))
  5145. (org-end-of-subtree t)))))
  5146. (goto-char beg)
  5147. (while (re-search-forward org-drawer-regexp end t)
  5148. (org-flag-drawer t))))))
  5149. (defun org-flag-drawer (flag)
  5150. (save-excursion
  5151. (beginning-of-line 1)
  5152. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5153. (let ((b (match-end 0))
  5154. (outline-regexp org-outline-regexp))
  5155. (if (re-search-forward
  5156. "^[ \t]*:END:"
  5157. (save-excursion (outline-next-heading) (point)) t)
  5158. (outline-flag-region b (point-at-eol) flag)
  5159. (error ":END: line missing at position %s" b))))))
  5160. (defun org-subtree-end-visible-p ()
  5161. "Is the end of the current subtree visible?"
  5162. (pos-visible-in-window-p
  5163. (save-excursion (org-end-of-subtree t) (point))))
  5164. (defun org-first-headline-recenter (&optional N)
  5165. "Move cursor to the first headline and recenter the headline.
  5166. Optional argument N means put the headline into the Nth line of the window."
  5167. (goto-char (point-min))
  5168. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5169. (beginning-of-line)
  5170. (recenter (prefix-numeric-value N))))
  5171. ;;; Saving and restoring visibility
  5172. (defun org-outline-overlay-data (&optional use-markers)
  5173. "Return a list of the locations of all outline overlays.
  5174. The are overlays with the `invisible' property value `outline'.
  5175. The return valus is a list of cons cells, with start and stop
  5176. positions for each overlay.
  5177. If USE-MARKERS is set, return the positions as markers."
  5178. (let (beg end)
  5179. (save-excursion
  5180. (save-restriction
  5181. (widen)
  5182. (delq nil
  5183. (mapcar (lambda (o)
  5184. (when (eq (org-overlay-get o 'invisible) 'outline)
  5185. (setq beg (org-overlay-start o)
  5186. end (org-overlay-end o))
  5187. (and beg end (> end beg)
  5188. (if use-markers
  5189. (cons (move-marker (make-marker) beg)
  5190. (move-marker (make-marker) end))
  5191. (cons beg end)))))
  5192. (org-overlays-in (point-min) (point-max))))))))
  5193. (defun org-set-outline-overlay-data (data)
  5194. "Create visibility overlays for all positions in DATA.
  5195. DATA should have been made by `org-outline-overlay-data'."
  5196. (let (o)
  5197. (save-excursion
  5198. (save-restriction
  5199. (widen)
  5200. (show-all)
  5201. (mapc (lambda (c)
  5202. (setq o (org-make-overlay (car c) (cdr c)))
  5203. (org-overlay-put o 'invisible 'outline))
  5204. data)))))
  5205. (defmacro org-save-outline-visibility (use-markers &rest body)
  5206. "Save and restore outline visibility around BODY.
  5207. If USE-MARKERS is non-nil, use markers for the positions.
  5208. This means that the buffer may change while running BODY,
  5209. but it also means that the buffer should stay alive
  5210. during the operation, because otherwise all these markers will
  5211. point nowhere."
  5212. `(let ((data (org-outline-overlay-data ,use-markers)))
  5213. (unwind-protect
  5214. (progn
  5215. ,@body
  5216. (org-set-outline-overlay-data data))
  5217. (when ,use-markers
  5218. (mapc (lambda (c)
  5219. (and (markerp (car c)) (move-marker (car c) nil))
  5220. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5221. data)))))
  5222. ;;; Folding of blocks
  5223. (defconst org-block-regexp
  5224. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5225. "Regular expression for hiding blocks.")
  5226. (defvar org-hide-block-overlays nil
  5227. "Overlays hiding blocks.")
  5228. (make-variable-buffer-local 'org-hide-block-overlays)
  5229. (defun org-block-map (function &optional start end)
  5230. "Call func at the head of all source blocks in the current
  5231. buffer. Optional arguments START and END can be used to limit
  5232. the range."
  5233. (let ((start (or start (point-min)))
  5234. (end (or end (point-max))))
  5235. (save-excursion
  5236. (goto-char start)
  5237. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5238. (save-excursion
  5239. (save-match-data
  5240. (goto-char (match-beginning 0))
  5241. (funcall function)))))))
  5242. (defun org-hide-block-toggle-all ()
  5243. "Toggle the visibility of all blocks in the current buffer."
  5244. (org-block-map #'org-hide-block-toggle))
  5245. (defun org-hide-block-all ()
  5246. "Fold all blocks in the current buffer."
  5247. (interactive)
  5248. (org-show-block-all)
  5249. (org-block-map #'org-hide-block-toggle-maybe))
  5250. (defun org-show-block-all ()
  5251. "Unfold all blocks in the current buffer."
  5252. (mapc 'org-delete-overlay org-hide-block-overlays)
  5253. (setq org-hide-block-overlays nil))
  5254. (defun org-hide-block-toggle-maybe ()
  5255. "Toggle visibility of block at point."
  5256. (interactive)
  5257. (let ((case-fold-search t))
  5258. (if (save-excursion
  5259. (beginning-of-line 1)
  5260. (looking-at org-block-regexp))
  5261. (progn (org-hide-block-toggle)
  5262. t) ;; to signal that we took action
  5263. nil))) ;; to signal that we did not
  5264. (defun org-hide-block-toggle (&optional force)
  5265. "Toggle the visibility of the current block."
  5266. (interactive)
  5267. (save-excursion
  5268. (beginning-of-line)
  5269. (if (re-search-forward org-block-regexp nil t)
  5270. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5271. (end (match-end 0)) ;; end of entire body
  5272. ov)
  5273. (if (memq t (mapcar (lambda (overlay)
  5274. (eq (org-overlay-get overlay 'invisible)
  5275. 'org-hide-block))
  5276. (org-overlays-at start)))
  5277. (if (or (not force) (eq force 'off))
  5278. (mapc (lambda (ov)
  5279. (when (member ov org-hide-block-overlays)
  5280. (setq org-hide-block-overlays
  5281. (delq ov org-hide-block-overlays)))
  5282. (when (eq (org-overlay-get ov 'invisible)
  5283. 'org-hide-block)
  5284. (org-delete-overlay ov)))
  5285. (org-overlays-at start)))
  5286. (setq ov (org-make-overlay start end))
  5287. (org-overlay-put ov 'invisible 'org-hide-block)
  5288. ;; make the block accessible to isearch
  5289. (org-overlay-put
  5290. ov 'isearch-open-invisible
  5291. (lambda (ov)
  5292. (when (member ov org-hide-block-overlays)
  5293. (setq org-hide-block-overlays
  5294. (delq ov org-hide-block-overlays)))
  5295. (when (eq (org-overlay-get ov 'invisible)
  5296. 'org-hide-block)
  5297. (org-delete-overlay ov))))
  5298. (push ov org-hide-block-overlays)))
  5299. (error "Not looking at a source block"))))
  5300. ;; org-tab-after-check-for-cycling-hook
  5301. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5302. ;; Remove overlays when changing major mode
  5303. (add-hook 'org-mode-hook
  5304. (lambda () (org-add-hook 'change-major-mode-hook
  5305. 'org-show-block-all 'append 'local)))
  5306. ;;; Org-goto
  5307. (defvar org-goto-window-configuration nil)
  5308. (defvar org-goto-marker nil)
  5309. (defvar org-goto-map
  5310. (let ((map (make-sparse-keymap)))
  5311. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5312. (while (setq cmd (pop cmds))
  5313. (substitute-key-definition cmd cmd map global-map)))
  5314. (suppress-keymap map)
  5315. (org-defkey map "\C-m" 'org-goto-ret)
  5316. (org-defkey map [(return)] 'org-goto-ret)
  5317. (org-defkey map [(left)] 'org-goto-left)
  5318. (org-defkey map [(right)] 'org-goto-right)
  5319. (org-defkey map [(control ?g)] 'org-goto-quit)
  5320. (org-defkey map "\C-i" 'org-cycle)
  5321. (org-defkey map [(tab)] 'org-cycle)
  5322. (org-defkey map [(down)] 'outline-next-visible-heading)
  5323. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5324. (if org-goto-auto-isearch
  5325. (if (fboundp 'define-key-after)
  5326. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5327. nil)
  5328. (org-defkey map "q" 'org-goto-quit)
  5329. (org-defkey map "n" 'outline-next-visible-heading)
  5330. (org-defkey map "p" 'outline-previous-visible-heading)
  5331. (org-defkey map "f" 'outline-forward-same-level)
  5332. (org-defkey map "b" 'outline-backward-same-level)
  5333. (org-defkey map "u" 'outline-up-heading))
  5334. (org-defkey map "/" 'org-occur)
  5335. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5336. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5337. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5338. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5339. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5340. map))
  5341. (defconst org-goto-help
  5342. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5343. RET=jump to location [Q]uit and return to previous location
  5344. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5345. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5346. ;; FIXME: Docstring does not mention both interfaces
  5347. (defun org-goto (&optional alternative-interface)
  5348. "Look up a different location in the current file, keeping current visibility.
  5349. When you want look-up or go to a different location in a document, the
  5350. fastest way is often to fold the entire buffer and then dive into the tree.
  5351. This method has the disadvantage, that the previous location will be folded,
  5352. which may not be what you want.
  5353. This command works around this by showing a copy of the current buffer
  5354. in an indirect buffer, in overview mode. You can dive into the tree in
  5355. that copy, use org-occur and incremental search to find a location.
  5356. When pressing RET or `Q', the command returns to the original buffer in
  5357. which the visibility is still unchanged. After RET is will also jump to
  5358. the location selected in the indirect buffer and expose the
  5359. the headline hierarchy above."
  5360. (interactive "P")
  5361. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5362. (org-refile-use-outline-path t)
  5363. (org-refile-target-verify-function nil)
  5364. (interface
  5365. (if (not alternative-interface)
  5366. org-goto-interface
  5367. (if (eq org-goto-interface 'outline)
  5368. 'outline-path-completion
  5369. 'outline)))
  5370. (org-goto-start-pos (point))
  5371. (selected-point
  5372. (if (eq interface 'outline)
  5373. (car (org-get-location (current-buffer) org-goto-help))
  5374. (nth 3 (org-refile-get-location "Goto: ")))))
  5375. (if selected-point
  5376. (progn
  5377. (org-mark-ring-push org-goto-start-pos)
  5378. (goto-char selected-point)
  5379. (if (or (org-invisible-p) (org-invisible-p2))
  5380. (org-show-context 'org-goto)))
  5381. (message "Quit"))))
  5382. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5383. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5384. (defvar org-goto-local-auto-isearch-map) ; defined below
  5385. (defun org-get-location (buf help)
  5386. "Let the user select a location in the Org-mode buffer BUF.
  5387. This function uses a recursive edit. It returns the selected position
  5388. or nil."
  5389. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5390. (isearch-hide-immediately nil)
  5391. (isearch-search-fun-function
  5392. (lambda () 'org-goto-local-search-headings))
  5393. (org-goto-selected-point org-goto-exit-command)
  5394. (pop-up-frames nil)
  5395. (special-display-buffer-names nil)
  5396. (special-display-regexps nil)
  5397. (special-display-function nil))
  5398. (save-excursion
  5399. (save-window-excursion
  5400. (delete-other-windows)
  5401. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5402. (switch-to-buffer
  5403. (condition-case nil
  5404. (make-indirect-buffer (current-buffer) "*org-goto*")
  5405. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5406. (with-output-to-temp-buffer "*Help*"
  5407. (princ help))
  5408. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5409. (setq buffer-read-only nil)
  5410. (let ((org-startup-truncated t)
  5411. (org-startup-folded nil)
  5412. (org-startup-align-all-tables nil))
  5413. (org-mode)
  5414. (org-overview))
  5415. (setq buffer-read-only t)
  5416. (if (and (boundp 'org-goto-start-pos)
  5417. (integer-or-marker-p org-goto-start-pos))
  5418. (let ((org-show-hierarchy-above t)
  5419. (org-show-siblings t)
  5420. (org-show-following-heading t))
  5421. (goto-char org-goto-start-pos)
  5422. (and (org-invisible-p) (org-show-context)))
  5423. (goto-char (point-min)))
  5424. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5425. (message "Select location and press RET")
  5426. (use-local-map org-goto-map)
  5427. (recursive-edit)
  5428. ))
  5429. (kill-buffer "*org-goto*")
  5430. (cons org-goto-selected-point org-goto-exit-command)))
  5431. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5432. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5433. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5434. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5435. (defun org-goto-local-search-headings (string bound noerror)
  5436. "Search and make sure that any matches are in headlines."
  5437. (catch 'return
  5438. (while (if isearch-forward
  5439. (search-forward string bound noerror)
  5440. (search-backward string bound noerror))
  5441. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5442. (and (member :headline context)
  5443. (not (member :tags context))))
  5444. (throw 'return (point))))))
  5445. (defun org-goto-local-auto-isearch ()
  5446. "Start isearch."
  5447. (interactive)
  5448. (goto-char (point-min))
  5449. (let ((keys (this-command-keys)))
  5450. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5451. (isearch-mode t)
  5452. (isearch-process-search-char (string-to-char keys)))))
  5453. (defun org-goto-ret (&optional arg)
  5454. "Finish `org-goto' by going to the new location."
  5455. (interactive "P")
  5456. (setq org-goto-selected-point (point)
  5457. org-goto-exit-command 'return)
  5458. (throw 'exit nil))
  5459. (defun org-goto-left ()
  5460. "Finish `org-goto' by going to the new location."
  5461. (interactive)
  5462. (if (org-on-heading-p)
  5463. (progn
  5464. (beginning-of-line 1)
  5465. (setq org-goto-selected-point (point)
  5466. org-goto-exit-command 'left)
  5467. (throw 'exit nil))
  5468. (error "Not on a heading")))
  5469. (defun org-goto-right ()
  5470. "Finish `org-goto' by going to the new location."
  5471. (interactive)
  5472. (if (org-on-heading-p)
  5473. (progn
  5474. (setq org-goto-selected-point (point)
  5475. org-goto-exit-command 'right)
  5476. (throw 'exit nil))
  5477. (error "Not on a heading")))
  5478. (defun org-goto-quit ()
  5479. "Finish `org-goto' without cursor motion."
  5480. (interactive)
  5481. (setq org-goto-selected-point nil)
  5482. (setq org-goto-exit-command 'quit)
  5483. (throw 'exit nil))
  5484. ;;; Indirect buffer display of subtrees
  5485. (defvar org-indirect-dedicated-frame nil
  5486. "This is the frame being used for indirect tree display.")
  5487. (defvar org-last-indirect-buffer nil)
  5488. (defun org-tree-to-indirect-buffer (&optional arg)
  5489. "Create indirect buffer and narrow it to current subtree.
  5490. With numerical prefix ARG, go up to this level and then take that tree.
  5491. If ARG is negative, go up that many levels.
  5492. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5493. indirect buffer previously made with this command, to avoid proliferation of
  5494. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5495. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5496. is kept so that you can work with several indirect buffers at the same time.
  5497. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5498. requests that a new frame be made for the new buffer, so that the dedicated
  5499. frame is not changed."
  5500. (interactive "P")
  5501. (let ((cbuf (current-buffer))
  5502. (cwin (selected-window))
  5503. (pos (point))
  5504. beg end level heading ibuf)
  5505. (save-excursion
  5506. (org-back-to-heading t)
  5507. (when (numberp arg)
  5508. (setq level (org-outline-level))
  5509. (if (< arg 0) (setq arg (+ level arg)))
  5510. (while (> (setq level (org-outline-level)) arg)
  5511. (outline-up-heading 1 t)))
  5512. (setq beg (point)
  5513. heading (org-get-heading))
  5514. (org-end-of-subtree t t)
  5515. (if (org-on-heading-p) (backward-char 1))
  5516. (setq end (point)))
  5517. (if (and (buffer-live-p org-last-indirect-buffer)
  5518. (not (eq org-indirect-buffer-display 'new-frame))
  5519. (not arg))
  5520. (kill-buffer org-last-indirect-buffer))
  5521. (setq ibuf (org-get-indirect-buffer cbuf)
  5522. org-last-indirect-buffer ibuf)
  5523. (cond
  5524. ((or (eq org-indirect-buffer-display 'new-frame)
  5525. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5526. (select-frame (make-frame))
  5527. (delete-other-windows)
  5528. (switch-to-buffer ibuf)
  5529. (org-set-frame-title heading))
  5530. ((eq org-indirect-buffer-display 'dedicated-frame)
  5531. (raise-frame
  5532. (select-frame (or (and org-indirect-dedicated-frame
  5533. (frame-live-p org-indirect-dedicated-frame)
  5534. org-indirect-dedicated-frame)
  5535. (setq org-indirect-dedicated-frame (make-frame)))))
  5536. (delete-other-windows)
  5537. (switch-to-buffer ibuf)
  5538. (org-set-frame-title (concat "Indirect: " heading)))
  5539. ((eq org-indirect-buffer-display 'current-window)
  5540. (switch-to-buffer ibuf))
  5541. ((eq org-indirect-buffer-display 'other-window)
  5542. (pop-to-buffer ibuf))
  5543. (t (error "Invalid value")))
  5544. (if (featurep 'xemacs)
  5545. (save-excursion (org-mode) (turn-on-font-lock)))
  5546. (narrow-to-region beg end)
  5547. (show-all)
  5548. (goto-char pos)
  5549. (and (window-live-p cwin) (select-window cwin))))
  5550. (defun org-get-indirect-buffer (&optional buffer)
  5551. (setq buffer (or buffer (current-buffer)))
  5552. (let ((n 1) (base (buffer-name buffer)) bname)
  5553. (while (buffer-live-p
  5554. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5555. (setq n (1+ n)))
  5556. (condition-case nil
  5557. (make-indirect-buffer buffer bname 'clone)
  5558. (error (make-indirect-buffer buffer bname)))))
  5559. (defun org-set-frame-title (title)
  5560. "Set the title of the current frame to the string TITLE."
  5561. ;; FIXME: how to name a single frame in XEmacs???
  5562. (unless (featurep 'xemacs)
  5563. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5564. ;;;; Structure editing
  5565. ;;; Inserting headlines
  5566. (defun org-previous-line-empty-p ()
  5567. (save-excursion
  5568. (and (not (bobp))
  5569. (or (beginning-of-line 0) t)
  5570. (save-match-data
  5571. (looking-at "[ \t]*$")))))
  5572. (defun org-insert-heading (&optional force-heading invisible-ok)
  5573. "Insert a new heading or item with same depth at point.
  5574. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5575. If point is at the beginning of a headline, insert a sibling before the
  5576. current headline. If point is not at the beginning, do not split the line,
  5577. but create the new headline after the current line.
  5578. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5579. This is important for non-interactive uses of the command."
  5580. (interactive "P")
  5581. (if (or (= (buffer-size) 0)
  5582. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5583. (org-on-heading-p))))
  5584. (not (org-in-item-p))))
  5585. (insert "\n* ")
  5586. (when (or force-heading (not (org-insert-item)))
  5587. (let* ((empty-line-p nil)
  5588. (head (save-excursion
  5589. (condition-case nil
  5590. (progn
  5591. (org-back-to-heading invisible-ok)
  5592. (setq empty-line-p (org-previous-line-empty-p))
  5593. (match-string 0))
  5594. (error "*"))))
  5595. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5596. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5597. pos hide-previous previous-pos)
  5598. (cond
  5599. ((and (org-on-heading-p) (bolp)
  5600. (or (bobp)
  5601. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5602. ;; insert before the current line
  5603. (open-line (if blank 2 1)))
  5604. ((and (bolp)
  5605. (not org-insert-heading-respect-content)
  5606. (or (bobp)
  5607. (save-excursion
  5608. (backward-char 1) (not (org-invisible-p)))))
  5609. ;; insert right here
  5610. nil)
  5611. (t
  5612. ;; somewhere in the line
  5613. (save-excursion
  5614. (setq previous-pos (point-at-bol))
  5615. (end-of-line)
  5616. (setq hide-previous (org-invisible-p)))
  5617. (and org-insert-heading-respect-content (org-show-subtree))
  5618. (let ((split
  5619. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5620. (save-excursion
  5621. (let ((p (point)))
  5622. (goto-char (point-at-bol))
  5623. (and (looking-at org-complex-heading-regexp)
  5624. (> p (match-beginning 4)))))))
  5625. tags pos)
  5626. (cond
  5627. (org-insert-heading-respect-content
  5628. (org-end-of-subtree nil t)
  5629. (or (bolp) (newline))
  5630. (or (org-previous-line-empty-p)
  5631. (and blank (newline)))
  5632. (open-line 1))
  5633. ((org-on-heading-p)
  5634. (when hide-previous
  5635. (show-children)
  5636. (org-show-entry))
  5637. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5638. (setq tags (and (match-end 2) (match-string 2)))
  5639. (and (match-end 1)
  5640. (delete-region (match-beginning 1) (match-end 1)))
  5641. (setq pos (point-at-bol))
  5642. (or split (end-of-line 1))
  5643. (delete-horizontal-space)
  5644. (if (string-match "\\`\\*+\\'"
  5645. (buffer-substring (point-at-bol) (point)))
  5646. (insert " "))
  5647. (newline (if blank 2 1))
  5648. (when tags
  5649. (save-excursion
  5650. (goto-char pos)
  5651. (end-of-line 1)
  5652. (insert " " tags)
  5653. (org-set-tags nil 'align))))
  5654. (t
  5655. (or split (end-of-line 1))
  5656. (newline (if blank 2 1)))))))
  5657. (insert head) (just-one-space)
  5658. (setq pos (point))
  5659. (end-of-line 1)
  5660. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5661. (when (and org-insert-heading-respect-content hide-previous)
  5662. (save-excursion
  5663. (goto-char previous-pos)
  5664. (hide-subtree)))
  5665. (run-hooks 'org-insert-heading-hook)))))
  5666. (defun org-get-heading (&optional no-tags)
  5667. "Return the heading of the current entry, without the stars."
  5668. (save-excursion
  5669. (org-back-to-heading t)
  5670. (if (looking-at
  5671. (if no-tags
  5672. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5673. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5674. (match-string 1) "")))
  5675. (defun org-heading-components ()
  5676. "Return the components of the current heading.
  5677. This is a list with the following elements:
  5678. - the level as an integer
  5679. - the reduced level, different if `org-odd-levels-only' is set.
  5680. - the TODO keyword, or nil
  5681. - the priority character, like ?A, or nil if no priority is given
  5682. - the headline text itself, or the tags string if no headline text
  5683. - the tags string, or nil."
  5684. (save-excursion
  5685. (org-back-to-heading t)
  5686. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5687. (list (length (match-string 1))
  5688. (org-reduced-level (length (match-string 1)))
  5689. (org-match-string-no-properties 2)
  5690. (and (match-end 3) (aref (match-string 3) 2))
  5691. (org-match-string-no-properties 4)
  5692. (org-match-string-no-properties 5)))))
  5693. (defun org-get-entry ()
  5694. "Get the entry text, after heading, entire subtree."
  5695. (save-excursion
  5696. (org-back-to-heading t)
  5697. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5698. (defun org-insert-heading-after-current ()
  5699. "Insert a new heading with same level as current, after current subtree."
  5700. (interactive)
  5701. (org-back-to-heading)
  5702. (org-insert-heading)
  5703. (org-move-subtree-down)
  5704. (end-of-line 1))
  5705. (defun org-insert-heading-respect-content ()
  5706. (interactive)
  5707. (let ((org-insert-heading-respect-content t))
  5708. (org-insert-heading t)))
  5709. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5710. (interactive "P")
  5711. (let ((org-insert-heading-respect-content t))
  5712. (org-insert-todo-heading force-state t)))
  5713. (defun org-insert-todo-heading (arg &optional force-heading)
  5714. "Insert a new heading with the same level and TODO state as current heading.
  5715. If the heading has no TODO state, or if the state is DONE, use the first
  5716. state (TODO by default). Also with prefix arg, force first state."
  5717. (interactive "P")
  5718. (when (or force-heading (not (org-insert-item 'checkbox)))
  5719. (org-insert-heading force-heading)
  5720. (save-excursion
  5721. (org-back-to-heading)
  5722. (outline-previous-heading)
  5723. (looking-at org-todo-line-regexp))
  5724. (let*
  5725. ((new-mark-x
  5726. (if (or arg
  5727. (not (match-beginning 2))
  5728. (member (match-string 2) org-done-keywords))
  5729. (car org-todo-keywords-1)
  5730. (match-string 2)))
  5731. (new-mark
  5732. (or
  5733. (run-hook-with-args-until-success
  5734. 'org-todo-get-default-hook new-mark-x nil)
  5735. new-mark-x)))
  5736. (beginning-of-line 1)
  5737. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5738. (if org-treat-insert-todo-heading-as-state-change
  5739. (org-todo new-mark)
  5740. (insert new-mark " "))))
  5741. (when org-provide-todo-statistics
  5742. (org-update-parent-todo-statistics))))
  5743. (defun org-insert-subheading (arg)
  5744. "Insert a new subheading and demote it.
  5745. Works for outline headings and for plain lists alike."
  5746. (interactive "P")
  5747. (org-insert-heading arg)
  5748. (cond
  5749. ((org-on-heading-p) (org-do-demote))
  5750. ((org-at-item-p) (org-indent-item 1))))
  5751. (defun org-insert-todo-subheading (arg)
  5752. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5753. Works for outline headings and for plain lists alike."
  5754. (interactive "P")
  5755. (org-insert-todo-heading arg)
  5756. (cond
  5757. ((org-on-heading-p) (org-do-demote))
  5758. ((org-at-item-p) (org-indent-item 1))))
  5759. ;;; Promotion and Demotion
  5760. (defvar org-after-demote-entry-hook nil
  5761. "Hook run after an entry has been demoted.
  5762. The cursor will be at the beginning of the entry.
  5763. When a subtree is being demoted, the hook will be called for each node.")
  5764. (defvar org-after-promote-entry-hook nil
  5765. "Hook run after an entry has been promoted.
  5766. The cursor will be at the beginning of the entry.
  5767. When a subtree is being promoted, the hook will be called for each node.")
  5768. (defun org-promote-subtree ()
  5769. "Promote the entire subtree.
  5770. See also `org-promote'."
  5771. (interactive)
  5772. (save-excursion
  5773. (org-map-tree 'org-promote))
  5774. (org-fix-position-after-promote))
  5775. (defun org-demote-subtree ()
  5776. "Demote the entire subtree. See `org-demote'.
  5777. See also `org-promote'."
  5778. (interactive)
  5779. (save-excursion
  5780. (org-map-tree 'org-demote))
  5781. (org-fix-position-after-promote))
  5782. (defun org-do-promote ()
  5783. "Promote the current heading higher up the tree.
  5784. If the region is active in `transient-mark-mode', promote all headings
  5785. in the region."
  5786. (interactive)
  5787. (save-excursion
  5788. (if (org-region-active-p)
  5789. (org-map-region 'org-promote (region-beginning) (region-end))
  5790. (org-promote)))
  5791. (org-fix-position-after-promote))
  5792. (defun org-do-demote ()
  5793. "Demote the current heading lower down the tree.
  5794. If the region is active in `transient-mark-mode', demote all headings
  5795. in the region."
  5796. (interactive)
  5797. (save-excursion
  5798. (if (org-region-active-p)
  5799. (org-map-region 'org-demote (region-beginning) (region-end))
  5800. (org-demote)))
  5801. (org-fix-position-after-promote))
  5802. (defun org-fix-position-after-promote ()
  5803. "Make sure that after pro/demotion cursor position is right."
  5804. (let ((pos (point)))
  5805. (when (save-excursion
  5806. (beginning-of-line 1)
  5807. (looking-at org-todo-line-regexp)
  5808. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5809. (cond ((eobp) (insert " "))
  5810. ((eolp) (insert " "))
  5811. ((equal (char-after) ?\ ) (forward-char 1))))))
  5812. (defun org-current-level ()
  5813. "Return the level of the current entry, or nil if before the first headline.
  5814. The level is the number of stars at the beginning of the headline."
  5815. (save-excursion
  5816. (condition-case nil
  5817. (progn
  5818. (org-back-to-heading t)
  5819. (funcall outline-level))
  5820. (error nil))))
  5821. (defun org-get-previous-line-level ()
  5822. "Return the outline depth of the last headline before the current line.
  5823. Returns 0 for the first headline in the buffer, and nil if before the
  5824. first headline."
  5825. (let ((current-level (org-current-level))
  5826. (prev-level (when (> (line-number-at-pos) 1)
  5827. (save-excursion
  5828. (beginning-of-line 0)
  5829. (org-current-level)))))
  5830. (cond ((null current-level) nil) ; Before first headline
  5831. ((null prev-level) 0) ; At first headline
  5832. (prev-level))))
  5833. (defun org-reduced-level (l)
  5834. "Compute the effective level of a heading.
  5835. This takes into account the setting of `org-odd-levels-only'."
  5836. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5837. (defun org-level-increment ()
  5838. "Return the number of stars that will be added or removed at a
  5839. time to headlines when structure editing, based on the value of
  5840. `org-odd-levels-only'."
  5841. (if org-odd-levels-only 2 1))
  5842. (defun org-get-valid-level (level &optional change)
  5843. "Rectify a level change under the influence of `org-odd-levels-only'
  5844. LEVEL is a current level, CHANGE is by how much the level should be
  5845. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5846. even level numbers will become the next higher odd number."
  5847. (if org-odd-levels-only
  5848. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5849. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5850. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5851. (max 1 (+ level (or change 0)))))
  5852. (if (boundp 'define-obsolete-function-alias)
  5853. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5854. (define-obsolete-function-alias 'org-get-legal-level
  5855. 'org-get-valid-level)
  5856. (define-obsolete-function-alias 'org-get-legal-level
  5857. 'org-get-valid-level "23.1")))
  5858. (defun org-promote ()
  5859. "Promote the current heading higher up the tree.
  5860. If the region is active in `transient-mark-mode', promote all headings
  5861. in the region."
  5862. (org-back-to-heading t)
  5863. (let* ((level (save-match-data (funcall outline-level)))
  5864. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5865. (diff (abs (- level (length up-head) -1))))
  5866. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5867. (replace-match up-head nil t)
  5868. ;; Fixup tag positioning
  5869. (and org-auto-align-tags (org-set-tags nil t))
  5870. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5871. (run-hooks 'org-after-promote-entry-hook)))
  5872. (defun org-demote ()
  5873. "Demote the current heading lower down the tree.
  5874. If the region is active in `transient-mark-mode', demote all headings
  5875. in the region."
  5876. (org-back-to-heading t)
  5877. (let* ((level (save-match-data (funcall outline-level)))
  5878. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5879. (diff (abs (- level (length down-head) -1))))
  5880. (replace-match down-head nil t)
  5881. ;; Fixup tag positioning
  5882. (and org-auto-align-tags (org-set-tags nil t))
  5883. (if org-adapt-indentation (org-fixup-indentation diff))
  5884. (run-hooks 'org-after-demote-entry-hook)))
  5885. (defun org-cycle-level ()
  5886. "Cycle the level of an empty headline through possible states.
  5887. This goes first to child, then to parent, level, then up the hierarchy.
  5888. After top level, it switches back to sibling level."
  5889. (interactive)
  5890. (let ((org-adapt-indentation nil))
  5891. (when (org-point-at-end-of-empty-headline)
  5892. (setq this-command 'org-cycle-level) ; Only needed for caching
  5893. (let ((cur-level (org-current-level))
  5894. (prev-level (org-get-previous-line-level)))
  5895. (cond
  5896. ;; If first headline in file, promote to top-level.
  5897. ((= prev-level 0)
  5898. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5899. do (org-do-promote)))
  5900. ;; If same level as prev, demote one.
  5901. ((= prev-level cur-level)
  5902. (org-do-demote))
  5903. ;; If parent is top-level, promote to top level if not already.
  5904. ((= prev-level 1)
  5905. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5906. do (org-do-promote)))
  5907. ;; If top-level, return to prev-level.
  5908. ((= cur-level 1)
  5909. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5910. do (org-do-demote)))
  5911. ;; If less than prev-level, promote one.
  5912. ((< cur-level prev-level)
  5913. (org-do-promote))
  5914. ;; If deeper than prev-level, promote until higher than
  5915. ;; prev-level.
  5916. ((> cur-level prev-level)
  5917. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5918. do (org-do-promote))))
  5919. t))))
  5920. (defun org-map-tree (fun)
  5921. "Call FUN for every heading underneath the current one."
  5922. (org-back-to-heading)
  5923. (let ((level (funcall outline-level)))
  5924. (save-excursion
  5925. (funcall fun)
  5926. (while (and (progn
  5927. (outline-next-heading)
  5928. (> (funcall outline-level) level))
  5929. (not (eobp)))
  5930. (funcall fun)))))
  5931. (defun org-map-region (fun beg end)
  5932. "Call FUN for every heading between BEG and END."
  5933. (let ((org-ignore-region t))
  5934. (save-excursion
  5935. (setq end (copy-marker end))
  5936. (goto-char beg)
  5937. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5938. (< (point) end))
  5939. (funcall fun))
  5940. (while (and (progn
  5941. (outline-next-heading)
  5942. (< (point) end))
  5943. (not (eobp)))
  5944. (funcall fun)))))
  5945. (defun org-fixup-indentation (diff)
  5946. "Change the indentation in the current entry by DIFF
  5947. However, if any line in the current entry has no indentation, or if it
  5948. would end up with no indentation after the change, nothing at all is done."
  5949. (save-excursion
  5950. (let ((end (save-excursion (outline-next-heading)
  5951. (point-marker)))
  5952. (prohibit (if (> diff 0)
  5953. "^\\S-"
  5954. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5955. col)
  5956. (unless (save-excursion (end-of-line 1)
  5957. (re-search-forward prohibit end t))
  5958. (while (and (< (point) end)
  5959. (re-search-forward "^[ \t]+" end t))
  5960. (goto-char (match-end 0))
  5961. (setq col (current-column))
  5962. (if (< diff 0) (replace-match ""))
  5963. (org-indent-to-column (+ diff col))))
  5964. (move-marker end nil))))
  5965. (defun org-convert-to-odd-levels ()
  5966. "Convert an org-mode file with all levels allowed to one with odd levels.
  5967. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5968. level 5 etc."
  5969. (interactive)
  5970. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5971. (let ((outline-regexp org-outline-regexp)
  5972. (outline-level 'org-outline-level)
  5973. (org-odd-levels-only nil) n)
  5974. (save-excursion
  5975. (goto-char (point-min))
  5976. (while (re-search-forward "^\\*\\*+ " nil t)
  5977. (setq n (- (length (match-string 0)) 2))
  5978. (while (>= (setq n (1- n)) 0)
  5979. (org-demote))
  5980. (end-of-line 1))))))
  5981. (defun org-convert-to-oddeven-levels ()
  5982. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5983. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5984. section with an even level, conversion would destroy the structure of the file. An error
  5985. is signaled in this case."
  5986. (interactive)
  5987. (goto-char (point-min))
  5988. ;; First check if there are no even levels
  5989. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5990. (org-show-context t)
  5991. (error "Not all levels are odd in this file. Conversion not possible"))
  5992. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5993. (let ((outline-regexp org-outline-regexp)
  5994. (outline-level 'org-outline-level)
  5995. (org-odd-levels-only nil) n)
  5996. (save-excursion
  5997. (goto-char (point-min))
  5998. (while (re-search-forward "^\\*\\*+ " nil t)
  5999. (setq n (/ (1- (length (match-string 0))) 2))
  6000. (while (>= (setq n (1- n)) 0)
  6001. (org-promote))
  6002. (end-of-line 1))))))
  6003. (defun org-tr-level (n)
  6004. "Make N odd if required."
  6005. (if org-odd-levels-only (1+ (/ n 2)) n))
  6006. ;;; Vertical tree motion, cutting and pasting of subtrees
  6007. (defun org-move-subtree-up (&optional arg)
  6008. "Move the current subtree up past ARG headlines of the same level."
  6009. (interactive "p")
  6010. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6011. (defun org-move-subtree-down (&optional arg)
  6012. "Move the current subtree down past ARG headlines of the same level."
  6013. (interactive "p")
  6014. (setq arg (prefix-numeric-value arg))
  6015. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6016. 'org-get-last-sibling))
  6017. (ins-point (make-marker))
  6018. (cnt (abs arg))
  6019. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6020. ;; Select the tree
  6021. (org-back-to-heading)
  6022. (setq beg0 (point))
  6023. (save-excursion
  6024. (setq ne-beg (org-back-over-empty-lines))
  6025. (setq beg (point)))
  6026. (save-match-data
  6027. (save-excursion (outline-end-of-heading)
  6028. (setq folded (org-invisible-p)))
  6029. (outline-end-of-subtree))
  6030. (outline-next-heading)
  6031. (setq ne-end (org-back-over-empty-lines))
  6032. (setq end (point))
  6033. (goto-char beg0)
  6034. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6035. ;; include less whitespace
  6036. (save-excursion
  6037. (goto-char beg)
  6038. (forward-line (- ne-beg ne-end))
  6039. (setq beg (point))))
  6040. ;; Find insertion point, with error handling
  6041. (while (> cnt 0)
  6042. (or (and (funcall movfunc) (looking-at outline-regexp))
  6043. (progn (goto-char beg0)
  6044. (error "Cannot move past superior level or buffer limit")))
  6045. (setq cnt (1- cnt)))
  6046. (if (> arg 0)
  6047. ;; Moving forward - still need to move over subtree
  6048. (progn (org-end-of-subtree t t)
  6049. (save-excursion
  6050. (org-back-over-empty-lines)
  6051. (or (bolp) (newline)))))
  6052. (setq ne-ins (org-back-over-empty-lines))
  6053. (move-marker ins-point (point))
  6054. (setq txt (buffer-substring beg end))
  6055. (org-save-markers-in-region beg end)
  6056. (delete-region beg end)
  6057. (org-remove-empty-overlays-at beg)
  6058. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6059. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6060. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6061. (let ((bbb (point)))
  6062. (insert-before-markers txt)
  6063. (org-reinstall-markers-in-region bbb)
  6064. (move-marker ins-point bbb))
  6065. (or (bolp) (insert "\n"))
  6066. (setq ins-end (point))
  6067. (goto-char ins-point)
  6068. (org-skip-whitespace)
  6069. (when (and (< arg 0)
  6070. (org-first-sibling-p)
  6071. (> ne-ins ne-beg))
  6072. ;; Move whitespace back to beginning
  6073. (save-excursion
  6074. (goto-char ins-end)
  6075. (let ((kill-whole-line t))
  6076. (kill-line (- ne-ins ne-beg)) (point)))
  6077. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6078. (move-marker ins-point nil)
  6079. (if folded
  6080. (hide-subtree)
  6081. (org-show-entry)
  6082. (show-children)
  6083. (org-cycle-hide-drawers 'children))
  6084. (org-clean-visibility-after-subtree-move)))
  6085. (defvar org-subtree-clip ""
  6086. "Clipboard for cut and paste of subtrees.
  6087. This is actually only a copy of the kill, because we use the normal kill
  6088. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6089. (defvar org-subtree-clip-folded nil
  6090. "Was the last copied subtree folded?
  6091. This is used to fold the tree back after pasting.")
  6092. (defun org-cut-subtree (&optional n)
  6093. "Cut the current subtree into the clipboard.
  6094. With prefix arg N, cut this many sequential subtrees.
  6095. This is a short-hand for marking the subtree and then cutting it."
  6096. (interactive "p")
  6097. (org-copy-subtree n 'cut))
  6098. (defun org-copy-subtree (&optional n cut force-store-markers)
  6099. "Cut the current subtree into the clipboard.
  6100. With prefix arg N, cut this many sequential subtrees.
  6101. This is a short-hand for marking the subtree and then copying it.
  6102. If CUT is non-nil, actually cut the subtree.
  6103. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6104. of some markers in the region, even if CUT is non-nil. This is
  6105. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6106. (interactive "p")
  6107. (let (beg end folded (beg0 (point)))
  6108. (if (interactive-p)
  6109. (org-back-to-heading nil) ; take what looks like a subtree
  6110. (org-back-to-heading t)) ; take what is really there
  6111. (org-back-over-empty-lines)
  6112. (setq beg (point))
  6113. (skip-chars-forward " \t\r\n")
  6114. (save-match-data
  6115. (save-excursion (outline-end-of-heading)
  6116. (setq folded (org-invisible-p)))
  6117. (condition-case nil
  6118. (org-forward-same-level (1- n) t)
  6119. (error nil))
  6120. (org-end-of-subtree t t))
  6121. (org-back-over-empty-lines)
  6122. (setq end (point))
  6123. (goto-char beg0)
  6124. (when (> end beg)
  6125. (setq org-subtree-clip-folded folded)
  6126. (when (or cut force-store-markers)
  6127. (org-save-markers-in-region beg end))
  6128. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6129. (setq org-subtree-clip (current-kill 0))
  6130. (message "%s: Subtree(s) with %d characters"
  6131. (if cut "Cut" "Copied")
  6132. (length org-subtree-clip)))))
  6133. (defun org-paste-subtree (&optional level tree for-yank)
  6134. "Paste the clipboard as a subtree, with modification of headline level.
  6135. The entire subtree is promoted or demoted in order to match a new headline
  6136. level.
  6137. If the cursor is at the beginning of a headline, the same level as
  6138. that headline is used to paste the tree
  6139. If not, the new level is derived from the *visible* headings
  6140. before and after the insertion point, and taken to be the inferior headline
  6141. level of the two. So if the previous visible heading is level 3 and the
  6142. next is level 4 (or vice versa), level 4 will be used for insertion.
  6143. This makes sure that the subtree remains an independent subtree and does
  6144. not swallow low level entries.
  6145. You can also force a different level, either by using a numeric prefix
  6146. argument, or by inserting the heading marker by hand. For example, if the
  6147. cursor is after \"*****\", then the tree will be shifted to level 5.
  6148. If optional TREE is given, use this text instead of the kill ring.
  6149. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6150. move back over whitespace before inserting, and move point to the end of
  6151. the inserted text when done."
  6152. (interactive "P")
  6153. (setq tree (or tree (and kill-ring (current-kill 0))))
  6154. (unless (org-kill-is-subtree-p tree)
  6155. (error "%s"
  6156. (substitute-command-keys
  6157. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6158. (let* ((visp (not (org-invisible-p)))
  6159. (txt tree)
  6160. (^re (concat "^\\(" outline-regexp "\\)"))
  6161. (re (concat "\\(" outline-regexp "\\)"))
  6162. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6163. (old-level (if (string-match ^re txt)
  6164. (- (match-end 0) (match-beginning 0) 1)
  6165. -1))
  6166. (force-level (cond (level (prefix-numeric-value level))
  6167. ((and (looking-at "[ \t]*$")
  6168. (string-match
  6169. ^re_ (buffer-substring
  6170. (point-at-bol) (point))))
  6171. (- (match-end 1) (match-beginning 1)))
  6172. ((and (bolp)
  6173. (looking-at org-outline-regexp))
  6174. (- (match-end 0) (point) 1))
  6175. (t nil)))
  6176. (previous-level (save-excursion
  6177. (condition-case nil
  6178. (progn
  6179. (outline-previous-visible-heading 1)
  6180. (if (looking-at re)
  6181. (- (match-end 0) (match-beginning 0) 1)
  6182. 1))
  6183. (error 1))))
  6184. (next-level (save-excursion
  6185. (condition-case nil
  6186. (progn
  6187. (or (looking-at outline-regexp)
  6188. (outline-next-visible-heading 1))
  6189. (if (looking-at re)
  6190. (- (match-end 0) (match-beginning 0) 1)
  6191. 1))
  6192. (error 1))))
  6193. (new-level (or force-level (max previous-level next-level)))
  6194. (shift (if (or (= old-level -1)
  6195. (= new-level -1)
  6196. (= old-level new-level))
  6197. 0
  6198. (- new-level old-level)))
  6199. (delta (if (> shift 0) -1 1))
  6200. (func (if (> shift 0) 'org-demote 'org-promote))
  6201. (org-odd-levels-only nil)
  6202. beg end newend)
  6203. ;; Remove the forced level indicator
  6204. (if force-level
  6205. (delete-region (point-at-bol) (point)))
  6206. ;; Paste
  6207. (beginning-of-line 1)
  6208. (unless for-yank (org-back-over-empty-lines))
  6209. (setq beg (point))
  6210. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6211. (insert-before-markers txt)
  6212. (unless (string-match "\n\\'" txt) (insert "\n"))
  6213. (setq newend (point))
  6214. (org-reinstall-markers-in-region beg)
  6215. (setq end (point))
  6216. (goto-char beg)
  6217. (skip-chars-forward " \t\n\r")
  6218. (setq beg (point))
  6219. (if (and (org-invisible-p) visp)
  6220. (save-excursion (outline-show-heading)))
  6221. ;; Shift if necessary
  6222. (unless (= shift 0)
  6223. (save-restriction
  6224. (narrow-to-region beg end)
  6225. (while (not (= shift 0))
  6226. (org-map-region func (point-min) (point-max))
  6227. (setq shift (+ delta shift)))
  6228. (goto-char (point-min))
  6229. (setq newend (point-max))))
  6230. (when (or (interactive-p) for-yank)
  6231. (message "Clipboard pasted as level %d subtree" new-level))
  6232. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6233. kill-ring
  6234. (eq org-subtree-clip (current-kill 0))
  6235. org-subtree-clip-folded)
  6236. ;; The tree was folded before it was killed/copied
  6237. (hide-subtree))
  6238. (and for-yank (goto-char newend))))
  6239. (defun org-kill-is-subtree-p (&optional txt)
  6240. "Check if the current kill is an outline subtree, or a set of trees.
  6241. Returns nil if kill does not start with a headline, or if the first
  6242. headline level is not the largest headline level in the tree.
  6243. So this will actually accept several entries of equal levels as well,
  6244. which is OK for `org-paste-subtree'.
  6245. If optional TXT is given, check this string instead of the current kill."
  6246. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6247. (start-level (and kill
  6248. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6249. org-outline-regexp "\\)")
  6250. kill)
  6251. (- (match-end 2) (match-beginning 2) 1)))
  6252. (re (concat "^" org-outline-regexp))
  6253. (start (1+ (or (match-beginning 2) -1))))
  6254. (if (not start-level)
  6255. (progn
  6256. nil) ;; does not even start with a heading
  6257. (catch 'exit
  6258. (while (setq start (string-match re kill (1+ start)))
  6259. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6260. (throw 'exit nil)))
  6261. t))))
  6262. (defvar org-markers-to-move nil
  6263. "Markers that should be moved with a cut-and-paste operation.
  6264. Those markers are stored together with their positions relative to
  6265. the start of the region.")
  6266. (defun org-save-markers-in-region (beg end)
  6267. "Check markers in region.
  6268. If these markers are between BEG and END, record their position relative
  6269. to BEG, so that after moving the block of text, we can put the markers back
  6270. into place.
  6271. This function gets called just before an entry or tree gets cut from the
  6272. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6273. called immediately, to move the markers with the entries."
  6274. (setq org-markers-to-move nil)
  6275. (when (featurep 'org-clock)
  6276. (org-clock-save-markers-for-cut-and-paste beg end))
  6277. (when (featurep 'org-agenda)
  6278. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6279. (defun org-check-and-save-marker (marker beg end)
  6280. "Check if MARKER is between BEG and END.
  6281. If yes, remember the marker and the distance to BEG."
  6282. (when (and (marker-buffer marker)
  6283. (equal (marker-buffer marker) (current-buffer)))
  6284. (if (and (>= marker beg) (< marker end))
  6285. (push (cons marker (- marker beg)) org-markers-to-move))))
  6286. (defun org-reinstall-markers-in-region (beg)
  6287. "Move all remembered markers to their position relative to BEG."
  6288. (mapc (lambda (x)
  6289. (move-marker (car x) (+ beg (cdr x))))
  6290. org-markers-to-move)
  6291. (setq org-markers-to-move nil))
  6292. (defun org-narrow-to-subtree ()
  6293. "Narrow buffer to the current subtree."
  6294. (interactive)
  6295. (save-excursion
  6296. (save-match-data
  6297. (narrow-to-region
  6298. (progn (org-back-to-heading t) (point))
  6299. (progn (org-end-of-subtree t t)
  6300. (if (org-on-heading-p) (backward-char 1))
  6301. (point))))))
  6302. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6303. "Clone the task (subtree) at point N times.
  6304. The clones will be inserted as siblings.
  6305. In interactive use, the user will be prompted for the number of clones
  6306. to be produced, and for a time SHIFT, which may be a repeater as used
  6307. in time stamps, for example `+3d'.
  6308. When a valid repeater is given and the entry contains any time stamps,
  6309. the clones will become a sequence in time, with time stamps in the
  6310. subtree shifted for each clone produced. If SHIFT is nil or the
  6311. empty string, time stamps will be left alone.
  6312. If the original subtree did contain time stamps with a repeater,
  6313. the following will happen:
  6314. - the repeater will be removed in each clone
  6315. - an additional clone will be produced, with the current, unshifted
  6316. date(s) in the entry.
  6317. - the original entry will be placed *after* all the clones, with
  6318. repeater intact.
  6319. - the start days in the repeater in the original entry will be shifted
  6320. to past the last clone.
  6321. I this way you can spell out a number of instances of a repeating task,
  6322. and still retain the repeater to cover future instances of the task."
  6323. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6324. (let (beg end template task
  6325. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6326. (if (not (and (integerp n) (> n 0)))
  6327. (error "Invalid number of replications %s" n))
  6328. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6329. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6330. shift)))
  6331. (error "Invalid shift specification %s" shift))
  6332. (when doshift
  6333. (setq shift-n (string-to-number (match-string 1 shift))
  6334. shift-what (cdr (assoc (match-string 2 shift)
  6335. '(("d" . day) ("w" . week)
  6336. ("m" . month) ("y" . year))))))
  6337. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6338. (setq nmin 1 nmax n)
  6339. (org-back-to-heading t)
  6340. (setq beg (point))
  6341. (org-end-of-subtree t t)
  6342. (or (bolp) (insert "\n"))
  6343. (setq end (point))
  6344. (setq template (buffer-substring beg end))
  6345. (when (and doshift
  6346. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6347. (delete-region beg end)
  6348. (setq end beg)
  6349. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6350. (goto-char end)
  6351. (loop for n from nmin to nmax do
  6352. (if (not doshift)
  6353. (setq task template)
  6354. (with-temp-buffer
  6355. (insert template)
  6356. (org-mode)
  6357. (goto-char (point-min))
  6358. (while (re-search-forward org-ts-regexp-both nil t)
  6359. (org-timestamp-change (* n shift-n) shift-what))
  6360. (unless (= n n-no-remove)
  6361. (goto-char (point-min))
  6362. (while (re-search-forward org-ts-regexp nil t)
  6363. (save-excursion
  6364. (goto-char (match-beginning 0))
  6365. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6366. (delete-region (match-beginning 1) (match-end 1))))))
  6367. (setq task (buffer-string))))
  6368. (insert task))
  6369. (goto-char beg)))
  6370. ;;; Outline Sorting
  6371. (defun org-sort (with-case)
  6372. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6373. Optional argument WITH-CASE means sort case-sensitively.
  6374. With a double prefix argument, also remove duplicate entries."
  6375. (interactive "P")
  6376. (if (org-at-table-p)
  6377. (org-call-with-arg 'org-table-sort-lines with-case)
  6378. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6379. (defun org-sort-remove-invisible (s)
  6380. (remove-text-properties 0 (length s) org-rm-props s)
  6381. (while (string-match org-bracket-link-regexp s)
  6382. (setq s (replace-match (if (match-end 2)
  6383. (match-string 3 s)
  6384. (match-string 1 s)) t t s)))
  6385. s)
  6386. (defvar org-priority-regexp) ; defined later in the file
  6387. (defvar org-after-sorting-entries-or-items-hook nil
  6388. "Hook that is run after a bunch of entries or items have been sorted.
  6389. When children are sorted, the cursor is in the parent line when this
  6390. hook gets called. When a region or a plain list is sorted, the cursor
  6391. will be in the first entry of the sorted region/list.")
  6392. (defun org-sort-entries-or-items
  6393. (&optional with-case sorting-type getkey-func compare-func property)
  6394. "Sort entries on a certain level of an outline tree, or plain list items.
  6395. If there is an active region, the entries in the region are sorted.
  6396. Else, if the cursor is before the first entry, sort the top-level items.
  6397. Else, the children of the entry at point are sorted.
  6398. If the cursor is at the first item in a plain list, the list items will be
  6399. sorted.
  6400. Sorting can be alphabetically, numerically, by date/time as given by
  6401. a time stamp, by a property or by priority.
  6402. The command prompts for the sorting type unless it has been given to the
  6403. function through the SORTING-TYPE argument, which needs to a character,
  6404. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6405. precise meaning of each character:
  6406. n Numerically, by converting the beginning of the entry/item to a number.
  6407. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6408. t By date/time, either the first active time stamp in the entry, or, if
  6409. none exist, by the first inactive one.
  6410. In items, only the first line will be checked.
  6411. s By the scheduled date/time.
  6412. d By deadline date/time.
  6413. c By creation time, which is assumed to be the first inactive time stamp
  6414. at the beginning of a line.
  6415. p By priority according to the cookie.
  6416. r By the value of a property.
  6417. Capital letters will reverse the sort order.
  6418. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6419. called with point at the beginning of the record. It must return either
  6420. a string or a number that should serve as the sorting key for that record.
  6421. Comparing entries ignores case by default. However, with an optional argument
  6422. WITH-CASE, the sorting considers case as well."
  6423. (interactive "P")
  6424. (let ((case-func (if with-case 'identity 'downcase))
  6425. start beg end stars re re2
  6426. txt what tmp plain-list-p)
  6427. ;; Find beginning and end of region to sort
  6428. (cond
  6429. ((org-region-active-p)
  6430. ;; we will sort the region
  6431. (setq end (region-end)
  6432. what "region")
  6433. (goto-char (region-beginning))
  6434. (if (not (org-on-heading-p)) (outline-next-heading))
  6435. (setq start (point)))
  6436. ((org-at-item-p)
  6437. ;; we will sort this plain list
  6438. (org-beginning-of-item-list) (setq start (point))
  6439. (org-end-of-item-list)
  6440. (or (bolp) (insert "\n"))
  6441. (setq end (point))
  6442. (goto-char start)
  6443. (setq plain-list-p t
  6444. what "plain list"))
  6445. ((or (org-on-heading-p)
  6446. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6447. ;; we will sort the children of the current headline
  6448. (org-back-to-heading)
  6449. (setq start (point)
  6450. end (progn (org-end-of-subtree t t)
  6451. (or (bolp) (insert "\n"))
  6452. (org-back-over-empty-lines)
  6453. (point))
  6454. what "children")
  6455. (goto-char start)
  6456. (show-subtree)
  6457. (outline-next-heading))
  6458. (t
  6459. ;; we will sort the top-level entries in this file
  6460. (goto-char (point-min))
  6461. (or (org-on-heading-p) (outline-next-heading))
  6462. (setq start (point))
  6463. (goto-char (point-max))
  6464. (beginning-of-line 1)
  6465. (when (looking-at ".*?\\S-")
  6466. ;; File ends in a non-white line
  6467. (end-of-line 1)
  6468. (insert "\n"))
  6469. (setq end (point-max))
  6470. (setq what "top-level")
  6471. (goto-char start)
  6472. (show-all)))
  6473. (setq beg (point))
  6474. (if (>= beg end) (error "Nothing to sort"))
  6475. (unless plain-list-p
  6476. (looking-at "\\(\\*+\\)")
  6477. (setq stars (match-string 1)
  6478. re (concat "^" (regexp-quote stars) " +")
  6479. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6480. txt (buffer-substring beg end))
  6481. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6482. (if (and (not (equal stars "*")) (string-match re2 txt))
  6483. (error "Region to sort contains a level above the first entry")))
  6484. (unless sorting-type
  6485. (message
  6486. (if plain-list-p
  6487. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6488. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6489. [t]ime [s]cheduled [d]eadline [c]reated
  6490. A/N/T/S/D/C/P/O/F means reversed:")
  6491. what)
  6492. (setq sorting-type (read-char-exclusive))
  6493. (and (= (downcase sorting-type) ?f)
  6494. (setq getkey-func
  6495. (org-icompleting-read "Sort using function: "
  6496. obarray 'fboundp t nil nil))
  6497. (setq getkey-func (intern getkey-func)))
  6498. (and (= (downcase sorting-type) ?r)
  6499. (setq property
  6500. (org-icompleting-read "Property: "
  6501. (mapcar 'list (org-buffer-property-keys t))
  6502. nil t))))
  6503. (message "Sorting entries...")
  6504. (save-restriction
  6505. (narrow-to-region start end)
  6506. (let ((dcst (downcase sorting-type))
  6507. (case-fold-search nil)
  6508. (now (current-time)))
  6509. (sort-subr
  6510. (/= dcst sorting-type)
  6511. ;; This function moves to the beginning character of the "record" to
  6512. ;; be sorted.
  6513. (if plain-list-p
  6514. (lambda nil
  6515. (if (org-at-item-p) t (goto-char (point-max))))
  6516. (lambda nil
  6517. (if (re-search-forward re nil t)
  6518. (goto-char (match-beginning 0))
  6519. (goto-char (point-max)))))
  6520. ;; This function moves to the last character of the "record" being
  6521. ;; sorted.
  6522. (if plain-list-p
  6523. 'org-end-of-item
  6524. (lambda nil
  6525. (save-match-data
  6526. (condition-case nil
  6527. (outline-forward-same-level 1)
  6528. (error
  6529. (goto-char (point-max)))))))
  6530. ;; This function returns the value that gets sorted against.
  6531. (if plain-list-p
  6532. (lambda nil
  6533. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6534. (cond
  6535. ((= dcst ?n)
  6536. (string-to-number (buffer-substring (match-end 0)
  6537. (point-at-eol))))
  6538. ((= dcst ?a)
  6539. (buffer-substring (match-end 0) (point-at-eol)))
  6540. ((= dcst ?t)
  6541. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6542. (re-search-forward org-ts-regexp-both
  6543. (point-at-eol) t))
  6544. (org-time-string-to-seconds (match-string 0))
  6545. (org-float-time now)))
  6546. ((= dcst ?f)
  6547. (if getkey-func
  6548. (progn
  6549. (setq tmp (funcall getkey-func))
  6550. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6551. tmp)
  6552. (error "Invalid key function `%s'" getkey-func)))
  6553. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6554. (lambda nil
  6555. (cond
  6556. ((= dcst ?n)
  6557. (if (looking-at org-complex-heading-regexp)
  6558. (string-to-number (match-string 4))
  6559. nil))
  6560. ((= dcst ?a)
  6561. (if (looking-at org-complex-heading-regexp)
  6562. (funcall case-func (match-string 4))
  6563. nil))
  6564. ((= dcst ?t)
  6565. (let ((end (save-excursion (outline-next-heading) (point))))
  6566. (if (or (re-search-forward org-ts-regexp end t)
  6567. (re-search-forward org-ts-regexp-both end t))
  6568. (org-time-string-to-seconds (match-string 0))
  6569. (org-float-time now))))
  6570. ((= dcst ?c)
  6571. (let ((end (save-excursion (outline-next-heading) (point))))
  6572. (if (re-search-forward
  6573. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6574. end t)
  6575. (org-time-string-to-seconds (match-string 0))
  6576. (org-float-time now))))
  6577. ((= dcst ?s)
  6578. (let ((end (save-excursion (outline-next-heading) (point))))
  6579. (if (re-search-forward org-scheduled-time-regexp end t)
  6580. (org-time-string-to-seconds (match-string 1))
  6581. (org-float-time now))))
  6582. ((= dcst ?d)
  6583. (let ((end (save-excursion (outline-next-heading) (point))))
  6584. (if (re-search-forward org-deadline-time-regexp end t)
  6585. (org-time-string-to-seconds (match-string 1))
  6586. (org-float-time now))))
  6587. ((= dcst ?p)
  6588. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6589. (string-to-char (match-string 2))
  6590. org-default-priority))
  6591. ((= dcst ?r)
  6592. (or (org-entry-get nil property) ""))
  6593. ((= dcst ?o)
  6594. (if (looking-at org-complex-heading-regexp)
  6595. (- 9999 (length (member (match-string 2)
  6596. org-todo-keywords-1)))))
  6597. ((= dcst ?f)
  6598. (if getkey-func
  6599. (progn
  6600. (setq tmp (funcall getkey-func))
  6601. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6602. tmp)
  6603. (error "Invalid key function `%s'" getkey-func)))
  6604. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6605. nil
  6606. (cond
  6607. ((= dcst ?a) 'string<)
  6608. ((= dcst ?f) compare-func)
  6609. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6610. (t nil)))))
  6611. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6612. (message "Sorting entries...done")))
  6613. (defun org-do-sort (table what &optional with-case sorting-type)
  6614. "Sort TABLE of WHAT according to SORTING-TYPE.
  6615. The user will be prompted for the SORTING-TYPE if the call to this
  6616. function does not specify it. WHAT is only for the prompt, to indicate
  6617. what is being sorted. The sorting key will be extracted from
  6618. the car of the elements of the table.
  6619. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6620. (unless sorting-type
  6621. (message
  6622. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6623. what)
  6624. (setq sorting-type (read-char-exclusive)))
  6625. (let ((dcst (downcase sorting-type))
  6626. extractfun comparefun)
  6627. ;; Define the appropriate functions
  6628. (cond
  6629. ((= dcst ?n)
  6630. (setq extractfun 'string-to-number
  6631. comparefun (if (= dcst sorting-type) '< '>)))
  6632. ((= dcst ?a)
  6633. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6634. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6635. comparefun (if (= dcst sorting-type)
  6636. 'string<
  6637. (lambda (a b) (and (not (string< a b))
  6638. (not (string= a b)))))))
  6639. ((= dcst ?t)
  6640. (setq extractfun
  6641. (lambda (x)
  6642. (if (or (string-match org-ts-regexp x)
  6643. (string-match org-ts-regexp-both x))
  6644. (org-float-time
  6645. (org-time-string-to-time (match-string 0 x)))
  6646. 0))
  6647. comparefun (if (= dcst sorting-type) '< '>)))
  6648. (t (error "Invalid sorting type `%c'" sorting-type)))
  6649. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6650. table)
  6651. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6652. ;;; The orgstruct minor mode
  6653. ;; Define a minor mode which can be used in other modes in order to
  6654. ;; integrate the org-mode structure editing commands.
  6655. ;; This is really a hack, because the org-mode structure commands use
  6656. ;; keys which normally belong to the major mode. Here is how it
  6657. ;; works: The minor mode defines all the keys necessary to operate the
  6658. ;; structure commands, but wraps the commands into a function which
  6659. ;; tests if the cursor is currently at a headline or a plain list
  6660. ;; item. If that is the case, the structure command is used,
  6661. ;; temporarily setting many Org-mode variables like regular
  6662. ;; expressions for filling etc. However, when any of those keys is
  6663. ;; used at a different location, function uses `key-binding' to look
  6664. ;; up if the key has an associated command in another currently active
  6665. ;; keymap (minor modes, major mode, global), and executes that
  6666. ;; command. There might be problems if any of the keys is otherwise
  6667. ;; used as a prefix key.
  6668. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6669. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6670. ;; addresses this by checking explicitly for both bindings.
  6671. (defvar orgstruct-mode-map (make-sparse-keymap)
  6672. "Keymap for the minor `orgstruct-mode'.")
  6673. (defvar org-local-vars nil
  6674. "List of local variables, for use by `orgstruct-mode'")
  6675. ;;;###autoload
  6676. (define-minor-mode orgstruct-mode
  6677. "Toggle the minor more `orgstruct-mode'.
  6678. This mode is for using Org-mode structure commands in other modes.
  6679. The following key behave as if Org-mode was active, if the cursor
  6680. is on a headline, or on a plain list item (both in the definition
  6681. of Org-mode).
  6682. M-up Move entry/item up
  6683. M-down Move entry/item down
  6684. M-left Promote
  6685. M-right Demote
  6686. M-S-up Move entry/item up
  6687. M-S-down Move entry/item down
  6688. M-S-left Promote subtree
  6689. M-S-right Demote subtree
  6690. M-q Fill paragraph and items like in Org-mode
  6691. C-c ^ Sort entries
  6692. C-c - Cycle list bullet
  6693. TAB Cycle item visibility
  6694. M-RET Insert new heading/item
  6695. S-M-RET Insert new TODO heading / Checkbox item
  6696. C-c C-c Set tags / toggle checkbox"
  6697. nil " OrgStruct" nil
  6698. (org-load-modules-maybe)
  6699. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6700. ;;;###autoload
  6701. (defun turn-on-orgstruct ()
  6702. "Unconditionally turn on `orgstruct-mode'."
  6703. (orgstruct-mode 1))
  6704. (defun orgstruct++-mode (&optional arg)
  6705. "Toggle `orgstruct-mode', the enhanced version of it.
  6706. In addition to setting orgstruct-mode, this also exports all indentation
  6707. and autofilling variables from org-mode into the buffer. It will also
  6708. recognize item context in multiline items.
  6709. Note that turning off orgstruct-mode will *not* remove the
  6710. indentation/paragraph settings. This can only be done by refreshing the
  6711. major mode, for example with \\[normal-mode]."
  6712. (interactive "P")
  6713. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6714. (if (< arg 1)
  6715. (orgstruct-mode -1)
  6716. (orgstruct-mode 1)
  6717. (let (var val)
  6718. (mapc
  6719. (lambda (x)
  6720. (when (string-match
  6721. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6722. (symbol-name (car x)))
  6723. (setq var (car x) val (nth 1 x))
  6724. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6725. org-local-vars)
  6726. (org-set-local 'orgstruct-is-++ t))))
  6727. (defvar orgstruct-is-++ nil
  6728. "Is orgstruct-mode in ++ version in the current-buffer?")
  6729. (make-variable-buffer-local 'orgstruct-is-++)
  6730. ;;;###autoload
  6731. (defun turn-on-orgstruct++ ()
  6732. "Unconditionally turn on `orgstruct++-mode'."
  6733. (orgstruct++-mode 1))
  6734. (defun orgstruct-error ()
  6735. "Error when there is no default binding for a structure key."
  6736. (interactive)
  6737. (error "This key has no function outside structure elements"))
  6738. (defun orgstruct-setup ()
  6739. "Setup orgstruct keymaps."
  6740. (let ((nfunc 0)
  6741. (bindings
  6742. (list
  6743. '([(meta up)] org-metaup)
  6744. '([(meta down)] org-metadown)
  6745. '([(meta left)] org-metaleft)
  6746. '([(meta right)] org-metaright)
  6747. '([(meta shift up)] org-shiftmetaup)
  6748. '([(meta shift down)] org-shiftmetadown)
  6749. '([(meta shift left)] org-shiftmetaleft)
  6750. '([(meta shift right)] org-shiftmetaright)
  6751. '([?\e (up)] org-metaup)
  6752. '([?\e (down)] org-metadown)
  6753. '([?\e (left)] org-metaleft)
  6754. '([?\e (right)] org-metaright)
  6755. '([?\e (shift up)] org-shiftmetaup)
  6756. '([?\e (shift down)] org-shiftmetadown)
  6757. '([?\e (shift left)] org-shiftmetaleft)
  6758. '([?\e (shift right)] org-shiftmetaright)
  6759. '([(shift up)] org-shiftup)
  6760. '([(shift down)] org-shiftdown)
  6761. '([(shift left)] org-shiftleft)
  6762. '([(shift right)] org-shiftright)
  6763. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6764. '("\M-q" fill-paragraph)
  6765. '("\C-c^" org-sort)
  6766. '("\C-c-" org-cycle-list-bullet)))
  6767. elt key fun cmd)
  6768. (while (setq elt (pop bindings))
  6769. (setq nfunc (1+ nfunc))
  6770. (setq key (org-key (car elt))
  6771. fun (nth 1 elt)
  6772. cmd (orgstruct-make-binding fun nfunc key))
  6773. (org-defkey orgstruct-mode-map key cmd))
  6774. ;; Special treatment needed for TAB and RET
  6775. (org-defkey orgstruct-mode-map [(tab)]
  6776. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6777. (org-defkey orgstruct-mode-map "\C-i"
  6778. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6779. (org-defkey orgstruct-mode-map "\M-\C-m"
  6780. (orgstruct-make-binding 'org-insert-heading 105
  6781. "\M-\C-m" [(meta return)]))
  6782. (org-defkey orgstruct-mode-map [(meta return)]
  6783. (orgstruct-make-binding 'org-insert-heading 106
  6784. [(meta return)] "\M-\C-m"))
  6785. (org-defkey orgstruct-mode-map [(shift meta return)]
  6786. (orgstruct-make-binding 'org-insert-todo-heading 107
  6787. [(meta return)] "\M-\C-m"))
  6788. (org-defkey orgstruct-mode-map "\e\C-m"
  6789. (orgstruct-make-binding 'org-insert-heading 108
  6790. "\e\C-m" [?\e (return)]))
  6791. (org-defkey orgstruct-mode-map [?\e (return)]
  6792. (orgstruct-make-binding 'org-insert-heading 109
  6793. [?\e (return)] "\e\C-m"))
  6794. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6795. (orgstruct-make-binding 'org-insert-todo-heading 110
  6796. [?\e (return)] "\e\C-m"))
  6797. (unless org-local-vars
  6798. (setq org-local-vars (org-get-local-variables)))
  6799. t))
  6800. (defun orgstruct-make-binding (fun n &rest keys)
  6801. "Create a function for binding in the structure minor mode.
  6802. FUN is the command to call inside a table. N is used to create a unique
  6803. command name. KEYS are keys that should be checked in for a command
  6804. to execute outside of tables."
  6805. (eval
  6806. (list 'defun
  6807. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6808. '(arg)
  6809. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6810. "Outside of structure, run the binding of `"
  6811. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6812. "'.")
  6813. '(interactive "p")
  6814. (list 'if
  6815. `(org-context-p 'headline 'item
  6816. (and orgstruct-is-++
  6817. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6818. 'item-body))
  6819. (list 'org-run-like-in-org-mode (list 'quote fun))
  6820. (list 'let '(orgstruct-mode)
  6821. (list 'call-interactively
  6822. (append '(or)
  6823. (mapcar (lambda (k)
  6824. (list 'key-binding k))
  6825. keys)
  6826. '('orgstruct-error))))))))
  6827. (defun org-context-p (&rest contexts)
  6828. "Check if local context is any of CONTEXTS.
  6829. Possible values in the list of contexts are `table', `headline', and `item'."
  6830. (let ((pos (point)))
  6831. (goto-char (point-at-bol))
  6832. (prog1 (or (and (memq 'table contexts)
  6833. (looking-at "[ \t]*|"))
  6834. (and (memq 'headline contexts)
  6835. ;;????????? (looking-at "\\*+"))
  6836. (looking-at outline-regexp))
  6837. (and (memq 'item contexts)
  6838. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6839. (and (memq 'item-body contexts)
  6840. (org-in-item-p)))
  6841. (goto-char pos))))
  6842. (defun org-get-local-variables ()
  6843. "Return a list of all local variables in an org-mode buffer."
  6844. (let (varlist)
  6845. (with-current-buffer (get-buffer-create "*Org tmp*")
  6846. (erase-buffer)
  6847. (org-mode)
  6848. (setq varlist (buffer-local-variables)))
  6849. (kill-buffer "*Org tmp*")
  6850. (delq nil
  6851. (mapcar
  6852. (lambda (x)
  6853. (setq x
  6854. (if (symbolp x)
  6855. (list x)
  6856. (list (car x) (list 'quote (cdr x)))))
  6857. (if (string-match
  6858. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6859. (symbol-name (car x)))
  6860. x nil))
  6861. varlist))))
  6862. ;;;###autoload
  6863. (defun org-run-like-in-org-mode (cmd)
  6864. "Run a command, pretending that the current buffer is in Org-mode.
  6865. This will temporarily bind local variables that are typically bound in
  6866. Org-mode to the values they have in Org-mode, and then interactively
  6867. call CMD."
  6868. (org-load-modules-maybe)
  6869. (unless org-local-vars
  6870. (setq org-local-vars (org-get-local-variables)))
  6871. (eval (list 'let org-local-vars
  6872. (list 'call-interactively (list 'quote cmd)))))
  6873. ;;;; Archiving
  6874. (defun org-get-category (&optional pos)
  6875. "Get the category applying to position POS."
  6876. (get-text-property (or pos (point)) 'org-category))
  6877. (defun org-refresh-category-properties ()
  6878. "Refresh category text properties in the buffer."
  6879. (let ((def-cat (cond
  6880. ((null org-category)
  6881. (if buffer-file-name
  6882. (file-name-sans-extension
  6883. (file-name-nondirectory buffer-file-name))
  6884. "???"))
  6885. ((symbolp org-category) (symbol-name org-category))
  6886. (t org-category)))
  6887. beg end cat pos optionp)
  6888. (org-unmodified
  6889. (save-excursion
  6890. (save-restriction
  6891. (widen)
  6892. (goto-char (point-min))
  6893. (put-text-property (point) (point-max) 'org-category def-cat)
  6894. (while (re-search-forward
  6895. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6896. (setq pos (match-end 0)
  6897. optionp (equal (char-after (match-beginning 0)) ?#)
  6898. cat (org-trim (match-string 2)))
  6899. (if optionp
  6900. (setq beg (point-at-bol) end (point-max))
  6901. (org-back-to-heading t)
  6902. (setq beg (point) end (org-end-of-subtree t t)))
  6903. (put-text-property beg end 'org-category cat)
  6904. (goto-char pos)))))))
  6905. ;;;; Link Stuff
  6906. ;;; Link abbreviations
  6907. (defun org-link-expand-abbrev (link)
  6908. "Apply replacements as defined in `org-link-abbrev-alist."
  6909. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6910. (let* ((key (match-string 1 link))
  6911. (as (or (assoc key org-link-abbrev-alist-local)
  6912. (assoc key org-link-abbrev-alist)))
  6913. (tag (and (match-end 2) (match-string 3 link)))
  6914. rpl)
  6915. (if (not as)
  6916. link
  6917. (setq rpl (cdr as))
  6918. (cond
  6919. ((symbolp rpl) (funcall rpl tag))
  6920. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6921. ((string-match "%h" rpl)
  6922. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6923. (t (concat rpl tag)))))
  6924. link))
  6925. ;;; Storing and inserting links
  6926. (defvar org-insert-link-history nil
  6927. "Minibuffer history for links inserted with `org-insert-link'.")
  6928. (defvar org-stored-links nil
  6929. "Contains the links stored with `org-store-link'.")
  6930. (defvar org-store-link-plist nil
  6931. "Plist with info about the most recently link created with `org-store-link'.")
  6932. (defvar org-link-protocols nil
  6933. "Link protocols added to Org-mode using `org-add-link-type'.")
  6934. (defvar org-store-link-functions nil
  6935. "List of functions that are called to create and store a link.
  6936. Each function will be called in turn until one returns a non-nil
  6937. value. Each function should check if it is responsible for creating
  6938. this link (for example by looking at the major mode).
  6939. If not, it must exit and return nil.
  6940. If yes, it should return a non-nil value after a calling
  6941. `org-store-link-props' with a list of properties and values.
  6942. Special properties are:
  6943. :type The link prefix. like \"http\". This must be given.
  6944. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6945. This is obligatory as well.
  6946. :description Optional default description for the second pair
  6947. of brackets in an Org-mode link. The user can still change
  6948. this when inserting this link into an Org-mode buffer.
  6949. In addition to these, any additional properties can be specified
  6950. and then used in remember templates.")
  6951. (defun org-add-link-type (type &optional follow export)
  6952. "Add TYPE to the list of `org-link-types'.
  6953. Re-compute all regular expressions depending on `org-link-types'
  6954. FOLLOW and EXPORT are two functions.
  6955. FOLLOW should take the link path as the single argument and do whatever
  6956. is necessary to follow the link, for example find a file or display
  6957. a mail message.
  6958. EXPORT should format the link path for export to one of the export formats.
  6959. It should be a function accepting three arguments:
  6960. path the path of the link, the text after the prefix (like \"http:\")
  6961. desc the description of the link, if any, nil if there was no description
  6962. format the export format, a symbol like `html' or `latex'.
  6963. The function may use the FORMAT information to return different values
  6964. depending on the format. The return value will be put literally into
  6965. the exported file.
  6966. Org-mode has a built-in default for exporting links. If you are happy with
  6967. this default, there is no need to define an export function for the link
  6968. type. For a simple example of an export function, see `org-bbdb.el'."
  6969. (add-to-list 'org-link-types type t)
  6970. (org-make-link-regexps)
  6971. (if (assoc type org-link-protocols)
  6972. (setcdr (assoc type org-link-protocols) (list follow export))
  6973. (push (list type follow export) org-link-protocols)))
  6974. (defvar org-agenda-buffer-name)
  6975. ;;;###autoload
  6976. (defun org-store-link (arg)
  6977. "\\<org-mode-map>Store an org-link to the current location.
  6978. This link is added to `org-stored-links' and can later be inserted
  6979. into an org-buffer with \\[org-insert-link].
  6980. For some link types, a prefix arg is interpreted:
  6981. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6982. For file links, arg negates `org-context-in-file-links'."
  6983. (interactive "P")
  6984. (org-load-modules-maybe)
  6985. (setq org-store-link-plist nil) ; reset
  6986. (let ((outline-regexp (org-get-limited-outline-regexp))
  6987. link cpltxt desc description search txt custom-id)
  6988. (cond
  6989. ((run-hook-with-args-until-success 'org-store-link-functions)
  6990. (setq link (plist-get org-store-link-plist :link)
  6991. desc (or (plist-get org-store-link-plist :description) link)))
  6992. ((equal (buffer-name) "*Org Edit Src Example*")
  6993. (let (label gc)
  6994. (while (or (not label)
  6995. (save-excursion
  6996. (save-restriction
  6997. (widen)
  6998. (goto-char (point-min))
  6999. (re-search-forward
  7000. (regexp-quote (format org-coderef-label-format label))
  7001. nil t))))
  7002. (when label (message "Label exists already") (sit-for 2))
  7003. (setq label (read-string "Code line label: " label)))
  7004. (end-of-line 1)
  7005. (setq link (format org-coderef-label-format label))
  7006. (setq gc (- 79 (length link)))
  7007. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7008. (insert link)
  7009. (setq link (concat "(" label ")") desc nil)))
  7010. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7011. ;; We are in the agenda, link to referenced location
  7012. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7013. (get-text-property (point) 'org-marker))))
  7014. (when m
  7015. (org-with-point-at m
  7016. (call-interactively 'org-store-link)))))
  7017. ((eq major-mode 'calendar-mode)
  7018. (let ((cd (calendar-cursor-to-date)))
  7019. (setq link
  7020. (format-time-string
  7021. (car org-time-stamp-formats)
  7022. (apply 'encode-time
  7023. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7024. nil nil nil))))
  7025. (org-store-link-props :type "calendar" :date cd)))
  7026. ((eq major-mode 'w3-mode)
  7027. (setq cpltxt (if (and (buffer-name)
  7028. (not (string-match "Untitled" (buffer-name))))
  7029. (buffer-name)
  7030. (url-view-url t))
  7031. link (org-make-link (url-view-url t)))
  7032. (org-store-link-props :type "w3" :url (url-view-url t)))
  7033. ((eq major-mode 'w3m-mode)
  7034. (setq cpltxt (or w3m-current-title w3m-current-url)
  7035. link (org-make-link w3m-current-url))
  7036. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7037. ((setq search (run-hook-with-args-until-success
  7038. 'org-create-file-search-functions))
  7039. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7040. "::" search))
  7041. (setq cpltxt (or description link)))
  7042. ((eq major-mode 'image-mode)
  7043. (setq cpltxt (concat "file:"
  7044. (abbreviate-file-name buffer-file-name))
  7045. link (org-make-link cpltxt))
  7046. (org-store-link-props :type "image" :file buffer-file-name))
  7047. ((eq major-mode 'dired-mode)
  7048. ;; link to the file in the current line
  7049. (let ((file (dired-get-filename nil t)))
  7050. (setq file (if file
  7051. (abbreviate-file-name
  7052. (expand-file-name (dired-get-filename nil t)))
  7053. ;; otherwise, no file so use current directory.
  7054. default-directory))
  7055. (setq cpltxt (concat "file:" file)
  7056. link (org-make-link cpltxt))))
  7057. ((and buffer-file-name (org-mode-p))
  7058. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7059. (cond
  7060. ((org-in-regexp "<<\\(.*?\\)>>")
  7061. (setq cpltxt
  7062. (concat "file:"
  7063. (abbreviate-file-name buffer-file-name)
  7064. "::" (match-string 1))
  7065. link (org-make-link cpltxt)))
  7066. ((and (featurep 'org-id)
  7067. (or (eq org-link-to-org-use-id t)
  7068. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7069. (interactive-p))
  7070. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7071. (interactive-p)
  7072. (not custom-id))
  7073. (and org-link-to-org-use-id
  7074. (condition-case nil
  7075. (org-entry-get nil "ID")
  7076. (error nil)))))
  7077. ;; We can make a link using the ID.
  7078. (setq link (condition-case nil
  7079. (prog1 (org-id-store-link)
  7080. (setq desc (plist-get org-store-link-plist
  7081. :description)))
  7082. (error
  7083. ;; probably before first headline, link to file only
  7084. (concat "file:"
  7085. (abbreviate-file-name buffer-file-name))))))
  7086. (t
  7087. ;; Just link to current headline
  7088. (setq cpltxt (concat "file:"
  7089. (abbreviate-file-name buffer-file-name)))
  7090. ;; Add a context search string
  7091. (when (org-xor org-context-in-file-links arg)
  7092. (setq txt (cond
  7093. ((org-on-heading-p) nil)
  7094. ((org-region-active-p)
  7095. (buffer-substring (region-beginning) (region-end)))
  7096. (t nil)))
  7097. (when (or (null txt) (string-match "\\S-" txt))
  7098. (setq cpltxt
  7099. (concat cpltxt "::"
  7100. (condition-case nil
  7101. (org-make-org-heading-search-string txt)
  7102. (error "")))
  7103. desc (or (nth 4 (ignore-errors
  7104. (org-heading-components))) "NONE"))))
  7105. (if (string-match "::\\'" cpltxt)
  7106. (setq cpltxt (substring cpltxt 0 -2)))
  7107. (setq link (org-make-link cpltxt)))))
  7108. ((buffer-file-name (buffer-base-buffer))
  7109. ;; Just link to this file here.
  7110. (setq cpltxt (concat "file:"
  7111. (abbreviate-file-name
  7112. (buffer-file-name (buffer-base-buffer)))))
  7113. ;; Add a context string
  7114. (when (org-xor org-context-in-file-links arg)
  7115. (setq txt (if (org-region-active-p)
  7116. (buffer-substring (region-beginning) (region-end))
  7117. (buffer-substring (point-at-bol) (point-at-eol))))
  7118. ;; Only use search option if there is some text.
  7119. (when (string-match "\\S-" txt)
  7120. (setq cpltxt
  7121. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7122. desc "NONE")))
  7123. (setq link (org-make-link cpltxt)))
  7124. ((interactive-p)
  7125. (error "Cannot link to a buffer which is not visiting a file"))
  7126. (t (setq link nil)))
  7127. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7128. (setq link (or link cpltxt)
  7129. desc (or desc cpltxt))
  7130. (if (equal desc "NONE") (setq desc nil))
  7131. (if (and (or (interactive-p) executing-kbd-macro) link)
  7132. (progn
  7133. (setq org-stored-links
  7134. (cons (list link desc) org-stored-links))
  7135. (message "Stored: %s" (or desc link))
  7136. (when custom-id
  7137. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7138. "::#" custom-id))
  7139. (setq org-stored-links
  7140. (cons (list link desc) org-stored-links))))
  7141. (and link (org-make-link-string link desc)))))
  7142. (defun org-store-link-props (&rest plist)
  7143. "Store link properties, extract names and addresses."
  7144. (let (x adr)
  7145. (when (setq x (plist-get plist :from))
  7146. (setq adr (mail-extract-address-components x))
  7147. (setq plist (plist-put plist :fromname (car adr)))
  7148. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7149. (when (setq x (plist-get plist :to))
  7150. (setq adr (mail-extract-address-components x))
  7151. (setq plist (plist-put plist :toname (car adr)))
  7152. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7153. (let ((from (plist-get plist :from))
  7154. (to (plist-get plist :to)))
  7155. (when (and from to org-from-is-user-regexp)
  7156. (setq plist
  7157. (plist-put plist :fromto
  7158. (if (string-match org-from-is-user-regexp from)
  7159. (concat "to %t")
  7160. (concat "from %f"))))))
  7161. (setq org-store-link-plist plist))
  7162. (defun org-add-link-props (&rest plist)
  7163. "Add these properties to the link property list."
  7164. (let (key value)
  7165. (while plist
  7166. (setq key (pop plist) value (pop plist))
  7167. (setq org-store-link-plist
  7168. (plist-put org-store-link-plist key value)))))
  7169. (defun org-email-link-description (&optional fmt)
  7170. "Return the description part of an email link.
  7171. This takes information from `org-store-link-plist' and formats it
  7172. according to FMT (default from `org-email-link-description-format')."
  7173. (setq fmt (or fmt org-email-link-description-format))
  7174. (let* ((p org-store-link-plist)
  7175. (to (plist-get p :toaddress))
  7176. (from (plist-get p :fromaddress))
  7177. (table
  7178. (list
  7179. (cons "%c" (plist-get p :fromto))
  7180. (cons "%F" (plist-get p :from))
  7181. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7182. (cons "%T" (plist-get p :to))
  7183. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7184. (cons "%s" (plist-get p :subject))
  7185. (cons "%m" (plist-get p :message-id)))))
  7186. (when (string-match "%c" fmt)
  7187. ;; Check if the user wrote this message
  7188. (if (and org-from-is-user-regexp from to
  7189. (save-match-data (string-match org-from-is-user-regexp from)))
  7190. (setq fmt (replace-match "to %t" t t fmt))
  7191. (setq fmt (replace-match "from %f" t t fmt))))
  7192. (org-replace-escapes fmt table)))
  7193. (defun org-make-org-heading-search-string (&optional string heading)
  7194. "Make search string for STRING or current headline."
  7195. (interactive)
  7196. (let ((s (or string (org-get-heading))))
  7197. (unless (and string (not heading))
  7198. ;; We are using a headline, clean up garbage in there.
  7199. (if (string-match org-todo-regexp s)
  7200. (setq s (replace-match "" t t s)))
  7201. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7202. (setq s (replace-match "" t t s)))
  7203. (setq s (org-trim s))
  7204. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7205. org-comment-string "\\)") s)
  7206. (setq s (replace-match "" t t s)))
  7207. (while (string-match org-ts-regexp s)
  7208. (setq s (replace-match "" t t s))))
  7209. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7210. (setq s (replace-match " " t t s)))
  7211. (or string (setq s (concat "*" s))) ; Add * for headlines
  7212. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7213. (defun org-make-link (&rest strings)
  7214. "Concatenate STRINGS."
  7215. (apply 'concat strings))
  7216. (defun org-make-link-string (link &optional description)
  7217. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7218. (unless (string-match "\\S-" link)
  7219. (error "Empty link"))
  7220. (when (and description
  7221. (stringp description)
  7222. (not (string-match "\\S-" description)))
  7223. (setq description nil))
  7224. (when (stringp description)
  7225. ;; Remove brackets from the description, they are fatal.
  7226. (while (string-match "\\[" description)
  7227. (setq description (replace-match "{" t t description)))
  7228. (while (string-match "\\]" description)
  7229. (setq description (replace-match "}" t t description))))
  7230. (when (equal (org-link-escape link) description)
  7231. ;; No description needed, it is identical
  7232. (setq description nil))
  7233. (when (and (not description)
  7234. (not (equal link (org-link-escape link))))
  7235. (setq description (org-extract-attributes link)))
  7236. (concat "[[" (org-link-escape link) "]"
  7237. (if description (concat "[" description "]") "")
  7238. "]"))
  7239. (defconst org-link-escape-chars
  7240. '((?\ . "%20")
  7241. (?\[ . "%5B")
  7242. (?\] . "%5D")
  7243. (?\340 . "%E0") ; `a
  7244. (?\342 . "%E2") ; ^a
  7245. (?\347 . "%E7") ; ,c
  7246. (?\350 . "%E8") ; `e
  7247. (?\351 . "%E9") ; 'e
  7248. (?\352 . "%EA") ; ^e
  7249. (?\356 . "%EE") ; ^i
  7250. (?\364 . "%F4") ; ^o
  7251. (?\371 . "%F9") ; `u
  7252. (?\373 . "%FB") ; ^u
  7253. (?\; . "%3B")
  7254. ;; (?? . "%3F")
  7255. (?= . "%3D")
  7256. (?+ . "%2B")
  7257. )
  7258. "Association list of escapes for some characters problematic in links.
  7259. This is the list that is used for internal purposes.")
  7260. (defvar org-url-encoding-use-url-hexify nil)
  7261. (defconst org-link-escape-chars-browser
  7262. '((?\ . "%20")) ; 32 for the SPC char
  7263. "Association list of escapes for some characters problematic in links.
  7264. This is the list that is used before handing over to the browser.")
  7265. (defun org-link-escape (text &optional table)
  7266. "Escape characters in TEXT that are problematic for links."
  7267. (if (and org-url-encoding-use-url-hexify (not table))
  7268. (url-hexify-string text)
  7269. (setq table (or table org-link-escape-chars))
  7270. (when text
  7271. (let ((re (mapconcat (lambda (x) (regexp-quote
  7272. (char-to-string (car x))))
  7273. table "\\|")))
  7274. (while (string-match re text)
  7275. (setq text
  7276. (replace-match
  7277. (cdr (assoc (string-to-char (match-string 0 text))
  7278. table))
  7279. t t text)))
  7280. text))))
  7281. (defun org-link-unescape (text &optional table)
  7282. "Reverse the action of `org-link-escape'."
  7283. (if (and org-url-encoding-use-url-hexify (not table))
  7284. (url-unhex-string text)
  7285. (setq table (or table org-link-escape-chars))
  7286. (when text
  7287. (let ((case-fold-search t)
  7288. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7289. table "\\|")))
  7290. (while (string-match re text)
  7291. (setq text
  7292. (replace-match
  7293. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7294. table)))
  7295. t t text)))
  7296. text))))
  7297. (defun org-xor (a b)
  7298. "Exclusive or."
  7299. (if a (not b) b))
  7300. (defun org-fixup-message-id-for-http (s)
  7301. "Replace special characters in a message id, so it can be used in an http query."
  7302. (while (string-match "<" s)
  7303. (setq s (replace-match "%3C" t t s)))
  7304. (while (string-match ">" s)
  7305. (setq s (replace-match "%3E" t t s)))
  7306. (while (string-match "@" s)
  7307. (setq s (replace-match "%40" t t s)))
  7308. s)
  7309. ;;;###autoload
  7310. (defun org-insert-link-global ()
  7311. "Insert a link like Org-mode does.
  7312. This command can be called in any mode to insert a link in Org-mode syntax."
  7313. (interactive)
  7314. (org-load-modules-maybe)
  7315. (org-run-like-in-org-mode 'org-insert-link))
  7316. (defun org-insert-link (&optional complete-file link-location)
  7317. "Insert a link. At the prompt, enter the link.
  7318. Completion can be used to insert any of the link protocol prefixes like
  7319. http or ftp in use.
  7320. The history can be used to select a link previously stored with
  7321. `org-store-link'. When the empty string is entered (i.e. if you just
  7322. press RET at the prompt), the link defaults to the most recently
  7323. stored link. As SPC triggers completion in the minibuffer, you need to
  7324. use M-SPC or C-q SPC to force the insertion of a space character.
  7325. You will also be prompted for a description, and if one is given, it will
  7326. be displayed in the buffer instead of the link.
  7327. If there is already a link at point, this command will allow you to edit link
  7328. and description parts.
  7329. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7330. be selected using completion. The path to the file will be relative to the
  7331. current directory if the file is in the current directory or a subdirectory.
  7332. Otherwise, the link will be the absolute path as completed in the minibuffer
  7333. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7334. option `org-link-file-path-type'.
  7335. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7336. the current directory or below.
  7337. With three \\[universal-argument] prefixes, negate the meaning of
  7338. `org-keep-stored-link-after-insertion'.
  7339. If `org-make-link-description-function' is non-nil, this function will be
  7340. called with the link target, and the result will be the default
  7341. link description.
  7342. If the LINK-LOCATION parameter is non-nil, this value will be
  7343. used as the link location instead of reading one interactively."
  7344. (interactive "P")
  7345. (let* ((wcf (current-window-configuration))
  7346. (region (if (org-region-active-p)
  7347. (buffer-substring (region-beginning) (region-end))))
  7348. (remove (and region (list (region-beginning) (region-end))))
  7349. (desc region)
  7350. tmphist ; byte-compile incorrectly complains about this
  7351. (link link-location)
  7352. entry file all-prefixes)
  7353. (cond
  7354. (link-location) ; specified by arg, just use it.
  7355. ((org-in-regexp org-bracket-link-regexp 1)
  7356. ;; We do have a link at point, and we are going to edit it.
  7357. (setq remove (list (match-beginning 0) (match-end 0)))
  7358. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7359. (setq link (read-string "Link: "
  7360. (org-link-unescape
  7361. (org-match-string-no-properties 1)))))
  7362. ((or (org-in-regexp org-angle-link-re)
  7363. (org-in-regexp org-plain-link-re))
  7364. ;; Convert to bracket link
  7365. (setq remove (list (match-beginning 0) (match-end 0))
  7366. link (read-string "Link: "
  7367. (org-remove-angle-brackets (match-string 0)))))
  7368. ((member complete-file '((4) (16)))
  7369. ;; Completing read for file names.
  7370. (setq link (org-file-complete-link complete-file)))
  7371. (t
  7372. ;; Read link, with completion for stored links.
  7373. (with-output-to-temp-buffer "*Org Links*"
  7374. (princ "Insert a link.
  7375. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7376. (when org-stored-links
  7377. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7378. (princ (mapconcat
  7379. (lambda (x)
  7380. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7381. (reverse org-stored-links) "\n"))))
  7382. (let ((cw (selected-window)))
  7383. (select-window (get-buffer-window "*Org Links*" 'visible))
  7384. (setq truncate-lines t)
  7385. (unless (pos-visible-in-window-p (point-max))
  7386. (org-fit-window-to-buffer))
  7387. (and (window-live-p cw) (select-window cw)))
  7388. ;; Fake a link history, containing the stored links.
  7389. (setq tmphist (append (mapcar 'car org-stored-links)
  7390. org-insert-link-history))
  7391. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7392. (mapcar 'car org-link-abbrev-alist)
  7393. org-link-types))
  7394. (unwind-protect
  7395. (progn
  7396. (setq link
  7397. (let ((org-completion-use-ido nil)
  7398. (org-completion-use-iswitchb nil))
  7399. (org-completing-read
  7400. "Link: "
  7401. (append
  7402. (mapcar (lambda (x) (list (concat x ":")))
  7403. all-prefixes)
  7404. (mapcar 'car org-stored-links))
  7405. nil nil nil
  7406. 'tmphist
  7407. (car (car org-stored-links)))))
  7408. (if (not (string-match "\\S-" link))
  7409. (error "No link selected"))
  7410. (if (or (member link all-prefixes)
  7411. (and (equal ":" (substring link -1))
  7412. (member (substring link 0 -1) all-prefixes)
  7413. (setq link (substring link 0 -1))))
  7414. (setq link (org-link-try-special-completion link))))
  7415. (set-window-configuration wcf)
  7416. (kill-buffer "*Org Links*"))
  7417. (setq entry (assoc link org-stored-links))
  7418. (or entry (push link org-insert-link-history))
  7419. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7420. (not org-keep-stored-link-after-insertion))
  7421. (setq org-stored-links (delq (assoc link org-stored-links)
  7422. org-stored-links)))
  7423. (setq desc (or desc (nth 1 entry)))))
  7424. (if (string-match org-plain-link-re link)
  7425. ;; URL-like link, normalize the use of angular brackets.
  7426. (setq link (org-make-link (org-remove-angle-brackets link))))
  7427. ;; Check if we are linking to the current file with a search option
  7428. ;; If yes, simplify the link by using only the search option.
  7429. (when (and buffer-file-name
  7430. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7431. (let* ((path (match-string 1 link))
  7432. (case-fold-search nil)
  7433. (search (match-string 2 link)))
  7434. (save-match-data
  7435. (if (equal (file-truename buffer-file-name) (file-truename path))
  7436. ;; We are linking to this same file, with a search option
  7437. (setq link search)))))
  7438. ;; Check if we can/should use a relative path. If yes, simplify the link
  7439. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7440. (let* ((type (match-string 1 link))
  7441. (path (match-string 2 link))
  7442. (origpath path)
  7443. (case-fold-search nil))
  7444. (cond
  7445. ((or (eq org-link-file-path-type 'absolute)
  7446. (equal complete-file '(16)))
  7447. (setq path (abbreviate-file-name (expand-file-name path))))
  7448. ((eq org-link-file-path-type 'noabbrev)
  7449. (setq path (expand-file-name path)))
  7450. ((eq org-link-file-path-type 'relative)
  7451. (setq path (file-relative-name path)))
  7452. (t
  7453. (save-match-data
  7454. (if (string-match (concat "^" (regexp-quote
  7455. (file-name-as-directory
  7456. (expand-file-name "."))))
  7457. (expand-file-name path))
  7458. ;; We are linking a file with relative path name.
  7459. (setq path (substring (expand-file-name path)
  7460. (match-end 0)))
  7461. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7462. (setq link (concat type path))
  7463. (if (equal desc origpath)
  7464. (setq desc path))))
  7465. (if org-make-link-description-function
  7466. (setq desc (funcall org-make-link-description-function link desc)))
  7467. (setq desc (read-string "Description: " desc))
  7468. (unless (string-match "\\S-" desc) (setq desc nil))
  7469. (if remove (apply 'delete-region remove))
  7470. (insert (org-make-link-string link desc))))
  7471. (defun org-link-try-special-completion (type)
  7472. "If there is completion support for link type TYPE, offer it."
  7473. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7474. (if (functionp fun)
  7475. (funcall fun)
  7476. (read-string "Link (no completion support): " (concat type ":")))))
  7477. (defun org-file-complete-link (&optional arg)
  7478. "Create a file link using completion."
  7479. (let (file link)
  7480. (setq file (read-file-name "File: "))
  7481. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7482. (pwd1 (file-name-as-directory (abbreviate-file-name
  7483. (expand-file-name ".")))))
  7484. (cond
  7485. ((equal arg '(16))
  7486. (setq link (org-make-link
  7487. "file:"
  7488. (abbreviate-file-name (expand-file-name file)))))
  7489. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7490. (setq link (org-make-link "file:" (match-string 1 file))))
  7491. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7492. (expand-file-name file))
  7493. (setq link (org-make-link
  7494. "file:" (match-string 1 (expand-file-name file)))))
  7495. (t (setq link (org-make-link "file:" file)))))
  7496. link))
  7497. (defun org-completing-read (&rest args)
  7498. "Completing-read with SPACE being a normal character."
  7499. (let ((minibuffer-local-completion-map
  7500. (copy-keymap minibuffer-local-completion-map)))
  7501. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7502. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7503. (apply 'org-icompleting-read args)))
  7504. (defun org-completing-read-no-i (&rest args)
  7505. (let (org-completion-use-ido org-completion-use-iswitchb)
  7506. (apply 'org-completing-read args)))
  7507. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7508. "Use iswitch as a completing-read replacement to choose from choices.
  7509. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7510. from."
  7511. (let* ((iswitchb-use-virtual-buffers nil)
  7512. (iswitchb-make-buflist-hook
  7513. (lambda ()
  7514. (setq iswitchb-temp-buflist choices))))
  7515. (iswitchb-read-buffer prompt)))
  7516. (defun org-icompleting-read (&rest args)
  7517. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7518. (org-without-partial-completion
  7519. (if (and org-completion-use-ido
  7520. (fboundp 'ido-completing-read)
  7521. (boundp 'ido-mode) ido-mode
  7522. (listp (second args)))
  7523. (let ((ido-enter-matching-directory nil))
  7524. (apply 'ido-completing-read (concat (car args))
  7525. (if (consp (car (nth 1 args)))
  7526. (mapcar (lambda (x) (car x)) (nth 1 args))
  7527. (nth 1 args))
  7528. (cddr args)))
  7529. (if (and org-completion-use-iswitchb
  7530. (boundp 'iswitchb-mode) iswitchb-mode
  7531. (listp (second args)))
  7532. (apply 'org-iswitchb-completing-read (concat (car args))
  7533. (if (consp (car (nth 1 args)))
  7534. (mapcar (lambda (x) (car x)) (nth 1 args))
  7535. (nth 1 args))
  7536. (cddr args))
  7537. (apply 'completing-read args)))))
  7538. (defun org-extract-attributes (s)
  7539. "Extract the attributes cookie from a string and set as text property."
  7540. (let (a attr (start 0) key value)
  7541. (save-match-data
  7542. (when (string-match "{{\\([^}]+\\)}}$" s)
  7543. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7544. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7545. (setq key (match-string 1 a) value (match-string 2 a)
  7546. start (match-end 0)
  7547. attr (plist-put attr (intern key) value))))
  7548. (org-add-props s nil 'org-attr attr))
  7549. s))
  7550. (defun org-extract-attributes-from-string (tag)
  7551. (let (key value attr)
  7552. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7553. (setq key (match-string 1 tag) value (match-string 2 tag)
  7554. tag (replace-match "" t t tag)
  7555. attr (plist-put attr (intern key) value)))
  7556. (cons tag attr)))
  7557. (defun org-attributes-to-string (plist)
  7558. "Format a property list into an HTML attribute list."
  7559. (let ((s "") key value)
  7560. (while plist
  7561. (setq key (pop plist) value (pop plist))
  7562. (and value
  7563. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7564. s))
  7565. ;;; Opening/following a link
  7566. (defvar org-link-search-failed nil)
  7567. (defvar org-open-link-functions nil
  7568. "Hook for functions finding a plain text link.
  7569. These functions must take a single argument, the link content.
  7570. They will be called for links that look like [[link text][description]]
  7571. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7572. like a filename (e.g. \"./blue.png\").
  7573. These functions will be called *before* Org attempts to resolve the
  7574. link by doing text searches in the current buffer - so if you want a
  7575. link \"[[target]]\" to still find \"<<target>>\", your function should
  7576. handle this as a special case.
  7577. When the function does handle the link, it must return a non-nil value.
  7578. If it decides that it is not responsible for this link, it must return
  7579. nil to indicate that that Org-mode can continue with other options
  7580. like exact and fuzzy text search.")
  7581. (defun org-next-link ()
  7582. "Move forward to the next link.
  7583. If the link is in hidden text, expose it."
  7584. (interactive)
  7585. (when (and org-link-search-failed (eq this-command last-command))
  7586. (goto-char (point-min))
  7587. (message "Link search wrapped back to beginning of buffer"))
  7588. (setq org-link-search-failed nil)
  7589. (let* ((pos (point))
  7590. (ct (org-context))
  7591. (a (assoc :link ct)))
  7592. (if a (goto-char (nth 2 a)))
  7593. (if (re-search-forward org-any-link-re nil t)
  7594. (progn
  7595. (goto-char (match-beginning 0))
  7596. (if (org-invisible-p) (org-show-context)))
  7597. (goto-char pos)
  7598. (setq org-link-search-failed t)
  7599. (error "No further link found"))))
  7600. (defun org-previous-link ()
  7601. "Move backward to the previous link.
  7602. If the link is in hidden text, expose it."
  7603. (interactive)
  7604. (when (and org-link-search-failed (eq this-command last-command))
  7605. (goto-char (point-max))
  7606. (message "Link search wrapped back to end of buffer"))
  7607. (setq org-link-search-failed nil)
  7608. (let* ((pos (point))
  7609. (ct (org-context))
  7610. (a (assoc :link ct)))
  7611. (if a (goto-char (nth 1 a)))
  7612. (if (re-search-backward org-any-link-re nil t)
  7613. (progn
  7614. (goto-char (match-beginning 0))
  7615. (if (org-invisible-p) (org-show-context)))
  7616. (goto-char pos)
  7617. (setq org-link-search-failed t)
  7618. (error "No further link found"))))
  7619. (defun org-translate-link (s)
  7620. "Translate a link string if a translation function has been defined."
  7621. (if (and org-link-translation-function
  7622. (fboundp org-link-translation-function)
  7623. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7624. (progn
  7625. (setq s (funcall org-link-translation-function
  7626. (match-string 1) (match-string 2)))
  7627. (concat (car s) ":" (cdr s)))
  7628. s))
  7629. (defun org-translate-link-from-planner (type path)
  7630. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7631. This is still an experimental function, your mileage may vary."
  7632. (cond
  7633. ((member type '("http" "https" "news" "ftp"))
  7634. ;; standard Internet links are the same.
  7635. nil)
  7636. ((and (equal type "irc") (string-match "^//" path))
  7637. ;; Planner has two / at the beginning of an irc link, we have 1.
  7638. ;; We should have zero, actually....
  7639. (setq path (substring path 1)))
  7640. ((and (equal type "lisp") (string-match "^/" path))
  7641. ;; Planner has a slash, we do not.
  7642. (setq type "elisp" path (substring path 1)))
  7643. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7644. ;; A typical message link. Planner has the id after the final slash,
  7645. ;; we separate it with a hash mark
  7646. (setq path (concat (match-string 1 path) "#"
  7647. (org-remove-angle-brackets (match-string 2 path)))))
  7648. )
  7649. (cons type path))
  7650. (defun org-find-file-at-mouse (ev)
  7651. "Open file link or URL at mouse."
  7652. (interactive "e")
  7653. (mouse-set-point ev)
  7654. (org-open-at-point 'in-emacs))
  7655. (defun org-open-at-mouse (ev)
  7656. "Open file link or URL at mouse."
  7657. (interactive "e")
  7658. (mouse-set-point ev)
  7659. (if (eq major-mode 'org-agenda-mode)
  7660. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7661. (org-open-at-point))
  7662. (defvar org-window-config-before-follow-link nil
  7663. "The window configuration before following a link.
  7664. This is saved in case the need arises to restore it.")
  7665. (defvar org-open-link-marker (make-marker)
  7666. "Marker pointing to the location where `org-open-at-point; was called.")
  7667. ;;;###autoload
  7668. (defun org-open-at-point-global ()
  7669. "Follow a link like Org-mode does.
  7670. This command can be called in any mode to follow a link that has
  7671. Org-mode syntax."
  7672. (interactive)
  7673. (org-run-like-in-org-mode 'org-open-at-point))
  7674. ;;;###autoload
  7675. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7676. "Open a link in the string S, as if it was in Org-mode."
  7677. (interactive "sLink: \nP")
  7678. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7679. (with-temp-buffer
  7680. (let ((org-inhibit-startup t))
  7681. (org-mode)
  7682. (insert s)
  7683. (goto-char (point-min))
  7684. (when reference-buffer
  7685. (setq org-link-abbrev-alist-local
  7686. (with-current-buffer reference-buffer
  7687. org-link-abbrev-alist-local)))
  7688. (org-open-at-point arg reference-buffer)))))
  7689. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7690. "Open link at or after point.
  7691. If there is no link at point, this function will search forward up to
  7692. the end of the current line.
  7693. Normally, files will be opened by an appropriate application. If the
  7694. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7695. With a double prefix argument, try to open outside of Emacs, in the
  7696. application the system uses for this file type."
  7697. (interactive "P")
  7698. (org-load-modules-maybe)
  7699. (move-marker org-open-link-marker (point))
  7700. (setq org-window-config-before-follow-link (current-window-configuration))
  7701. (org-remove-occur-highlights nil nil t)
  7702. (cond
  7703. ((and (org-on-heading-p)
  7704. (not (org-in-regexp
  7705. (concat org-plain-link-re "\\|"
  7706. org-bracket-link-regexp "\\|"
  7707. org-angle-link-re "\\|"
  7708. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7709. (not (get-text-property (point) 'org-linked-text)))
  7710. (or (org-offer-links-in-entry in-emacs)
  7711. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7712. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7713. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7714. (org-footnote-action))
  7715. (t
  7716. (let (type path link line search (pos (point)))
  7717. (catch 'match
  7718. (save-excursion
  7719. (skip-chars-forward "^]\n\r")
  7720. (when (org-in-regexp org-bracket-link-regexp 1)
  7721. (setq link (org-extract-attributes
  7722. (org-link-unescape (org-match-string-no-properties 1))))
  7723. (while (string-match " *\n *" link)
  7724. (setq link (replace-match " " t t link)))
  7725. (setq link (org-link-expand-abbrev link))
  7726. (cond
  7727. ((or (file-name-absolute-p link)
  7728. (string-match "^\\.\\.?/" link))
  7729. (setq type "file" path link))
  7730. ((string-match org-link-re-with-space3 link)
  7731. (setq type (match-string 1 link) path (match-string 2 link)))
  7732. (t (setq type "thisfile" path link)))
  7733. (throw 'match t)))
  7734. (when (get-text-property (point) 'org-linked-text)
  7735. (setq type "thisfile"
  7736. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7737. (1+ (point)) (point))
  7738. path (buffer-substring
  7739. (previous-single-property-change pos 'org-linked-text)
  7740. (next-single-property-change pos 'org-linked-text)))
  7741. (throw 'match t))
  7742. (save-excursion
  7743. (when (or (org-in-regexp org-angle-link-re)
  7744. (org-in-regexp org-plain-link-re))
  7745. (setq type (match-string 1) path (match-string 2))
  7746. (throw 'match t)))
  7747. (save-excursion
  7748. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7749. (setq type "tags"
  7750. path (match-string 1))
  7751. (while (string-match ":" path)
  7752. (setq path (replace-match "+" t t path)))
  7753. (throw 'match t)))
  7754. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7755. (setq type "tree-match"
  7756. path (match-string 1))
  7757. (throw 'match t)))
  7758. (unless path
  7759. (error "No link found"))
  7760. ;; switch back to reference buffer
  7761. ;; needed when if called in a temporary buffer through
  7762. ;; org-open-link-from-string
  7763. (with-current-buffer (or reference-buffer (current-buffer))
  7764. ;; Remove any trailing spaces in path
  7765. (if (string-match " +\\'" path)
  7766. (setq path (replace-match "" t t path)))
  7767. (if (and org-link-translation-function
  7768. (fboundp org-link-translation-function))
  7769. ;; Check if we need to translate the link
  7770. (let ((tmp (funcall org-link-translation-function type path)))
  7771. (setq type (car tmp) path (cdr tmp))))
  7772. (cond
  7773. ((assoc type org-link-protocols)
  7774. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7775. ((equal type "mailto")
  7776. (let ((cmd (car org-link-mailto-program))
  7777. (args (cdr org-link-mailto-program)) args1
  7778. (address path) (subject "") a)
  7779. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7780. (setq address (match-string 1 path)
  7781. subject (org-link-escape (match-string 2 path))))
  7782. (while args
  7783. (cond
  7784. ((not (stringp (car args))) (push (pop args) args1))
  7785. (t (setq a (pop args))
  7786. (if (string-match "%a" a)
  7787. (setq a (replace-match address t t a)))
  7788. (if (string-match "%s" a)
  7789. (setq a (replace-match subject t t a)))
  7790. (push a args1))))
  7791. (apply cmd (nreverse args1))))
  7792. ((member type '("http" "https" "ftp" "news"))
  7793. (browse-url (concat type ":" (org-link-escape
  7794. path org-link-escape-chars-browser))))
  7795. ((member type '("message"))
  7796. (browse-url (concat type ":" path)))
  7797. ((string= type "tags")
  7798. (org-tags-view in-emacs path))
  7799. ((string= type "tree-match")
  7800. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7801. ((string= type "file")
  7802. (if (string-match "::\\([0-9]+\\)\\'" path)
  7803. (setq line (string-to-number (match-string 1 path))
  7804. path (substring path 0 (match-beginning 0)))
  7805. (if (string-match "::\\(.+\\)\\'" path)
  7806. (setq search (match-string 1 path)
  7807. path (substring path 0 (match-beginning 0)))))
  7808. (if (string-match "[*?{]" (file-name-nondirectory path))
  7809. (dired path)
  7810. (org-open-file path in-emacs line search)))
  7811. ((string= type "news")
  7812. (require 'org-gnus)
  7813. (org-gnus-follow-link path))
  7814. ((string= type "shell")
  7815. (let ((cmd path))
  7816. (if (or (not org-confirm-shell-link-function)
  7817. (funcall org-confirm-shell-link-function
  7818. (format "Execute \"%s\" in shell? "
  7819. (org-add-props cmd nil
  7820. 'face 'org-warning))))
  7821. (progn
  7822. (message "Executing %s" cmd)
  7823. (shell-command cmd))
  7824. (error "Abort"))))
  7825. ((string= type "elisp")
  7826. (let ((cmd path))
  7827. (if (or (not org-confirm-elisp-link-function)
  7828. (funcall org-confirm-elisp-link-function
  7829. (format "Execute \"%s\" as elisp? "
  7830. (org-add-props cmd nil
  7831. 'face 'org-warning))))
  7832. (message "%s => %s" cmd
  7833. (if (equal (string-to-char cmd) ?\()
  7834. (eval (read cmd))
  7835. (call-interactively (read cmd))))
  7836. (error "Abort"))))
  7837. ((and (string= type "thisfile")
  7838. (run-hook-with-args-until-success
  7839. 'org-open-link-functions path)))
  7840. ((string= type "thisfile")
  7841. (if in-emacs
  7842. (switch-to-buffer-other-window
  7843. (org-get-buffer-for-internal-link (current-buffer)))
  7844. (org-mark-ring-push))
  7845. (let ((cmd `(org-link-search
  7846. ,path
  7847. ,(cond ((equal in-emacs '(4)) 'occur)
  7848. ((equal in-emacs '(16)) 'org-occur)
  7849. (t nil))
  7850. ,pos)))
  7851. (condition-case nil (eval cmd)
  7852. (error (progn (widen) (eval cmd))))))
  7853. (t
  7854. (browse-url-at-point)))))))
  7855. (move-marker org-open-link-marker nil)
  7856. (run-hook-with-args 'org-follow-link-hook))
  7857. (defun org-offer-links-in-entry (&optional nth zero)
  7858. "Offer links in the current entry and follow the selected link.
  7859. If there is only one link, follow it immediately as well.
  7860. If NTH is an integer, immediately pick the NTH link found.
  7861. If ZERO is a string, check also this string for a link, and if
  7862. there is one, offer it as link number zero."
  7863. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7864. "\\(" org-angle-link-re "\\)\\|"
  7865. "\\(" org-plain-link-re "\\)"))
  7866. (cnt ?0)
  7867. (in-emacs (if (integerp nth) nil nth))
  7868. have-zero end links link c)
  7869. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7870. (push (match-string 0 zero) links)
  7871. (setq cnt (1- cnt) have-zero t))
  7872. (save-excursion
  7873. (org-back-to-heading t)
  7874. (setq end (save-excursion (outline-next-heading) (point)))
  7875. (while (re-search-forward re end t)
  7876. (push (match-string 0) links))
  7877. (setq links (org-uniquify (reverse links))))
  7878. (cond
  7879. ((null links)
  7880. (message "No links"))
  7881. ((equal (length links) 1)
  7882. (setq link (list (car links))))
  7883. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7884. (setq link (nth (if have-zero nth (1- nth)) links)))
  7885. (t ; we have to select a link
  7886. (save-excursion
  7887. (save-window-excursion
  7888. (delete-other-windows)
  7889. (with-output-to-temp-buffer "*Select Link*"
  7890. (mapc (lambda (l)
  7891. (if (not (string-match org-bracket-link-regexp l))
  7892. (princ (format "[%c] %s\n" (incf cnt)
  7893. (org-remove-angle-brackets l)))
  7894. (if (match-end 3)
  7895. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7896. (match-string 3 l) (match-string 1 l)))
  7897. (princ (format "[%c] %s\n" (incf cnt)
  7898. (match-string 1 l))))))
  7899. links))
  7900. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7901. (message "Select link to open, RET to open all:")
  7902. (setq c (read-char-exclusive))
  7903. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7904. (when (equal c ?q) (error "Abort"))
  7905. (if (equal c ?\C-m)
  7906. (setq link links)
  7907. (setq nth (- c ?0))
  7908. (if have-zero (setq nth (1+ nth)))
  7909. (unless (and (integerp nth) (>= (length links) nth))
  7910. (error "Invalid link selection"))
  7911. (setq link (list (nth (1- nth) links))))))
  7912. (if link
  7913. (let ((buf (current-buffer)))
  7914. (dolist (l link)
  7915. (org-open-link-from-string l in-emacs buf))
  7916. t)
  7917. nil)))
  7918. ;; Add special file links that specify the way of opening
  7919. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7920. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7921. (defun org-open-file-with-system (path)
  7922. "Open file at PATH using the system way of opeing it."
  7923. (org-open-file path 'system))
  7924. (defun org-open-file-with-emacs (path)
  7925. "Open file at PATH in emacs."
  7926. (org-open-file path 'emacs))
  7927. (defun org-remove-file-link-modifiers ()
  7928. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7929. (goto-char (point-min))
  7930. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7931. (org-if-unprotected
  7932. (replace-match "file:" t t))))
  7933. (eval-after-load "org-exp"
  7934. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7935. 'org-remove-file-link-modifiers))
  7936. ;;;; Time estimates
  7937. (defun org-get-effort (&optional pom)
  7938. "Get the effort estimate for the current entry."
  7939. (org-entry-get pom org-effort-property))
  7940. ;;; File search
  7941. (defvar org-create-file-search-functions nil
  7942. "List of functions to construct the right search string for a file link.
  7943. These functions are called in turn with point at the location to
  7944. which the link should point.
  7945. A function in the hook should first test if it would like to
  7946. handle this file type, for example by checking the major-mode or
  7947. the file extension. If it decides not to handle this file, it
  7948. should just return nil to give other functions a chance. If it
  7949. does handle the file, it must return the search string to be used
  7950. when following the link. The search string will be part of the
  7951. file link, given after a double colon, and `org-open-at-point'
  7952. will automatically search for it. If special measures must be
  7953. taken to make the search successful, another function should be
  7954. added to the companion hook `org-execute-file-search-functions',
  7955. which see.
  7956. A function in this hook may also use `setq' to set the variable
  7957. `description' to provide a suggestion for the descriptive text to
  7958. be used for this link when it gets inserted into an Org-mode
  7959. buffer with \\[org-insert-link].")
  7960. (defvar org-execute-file-search-functions nil
  7961. "List of functions to execute a file search triggered by a link.
  7962. Functions added to this hook must accept a single argument, the
  7963. search string that was part of the file link, the part after the
  7964. double colon. The function must first check if it would like to
  7965. handle this search, for example by checking the major-mode or the
  7966. file extension. If it decides not to handle this search, it
  7967. should just return nil to give other functions a chance. If it
  7968. does handle the search, it must return a non-nil value to keep
  7969. other functions from trying.
  7970. Each function can access the current prefix argument through the
  7971. variable `current-prefix-argument'. Note that a single prefix is
  7972. used to force opening a link in Emacs, so it may be good to only
  7973. use a numeric or double prefix to guide the search function.
  7974. In case this is needed, a function in this hook can also restore
  7975. the window configuration before `org-open-at-point' was called using:
  7976. (set-window-configuration org-window-config-before-follow-link)")
  7977. (defun org-link-search (s &optional type avoid-pos)
  7978. "Search for a link search option.
  7979. If S is surrounded by forward slashes, it is interpreted as a
  7980. regular expression. In org-mode files, this will create an `org-occur'
  7981. sparse tree. In ordinary files, `occur' will be used to list matches.
  7982. If the current buffer is in `dired-mode', grep will be used to search
  7983. in all files. If AVOID-POS is given, ignore matches near that position."
  7984. (let ((case-fold-search t)
  7985. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7986. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7987. (append '(("") (" ") ("\t") ("\n"))
  7988. org-emphasis-alist)
  7989. "\\|") "\\)"))
  7990. (pos (point))
  7991. (pre nil) (post nil)
  7992. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7993. (cond
  7994. ;; First check if there are any special
  7995. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7996. ;; Now try the builtin stuff
  7997. ((and (equal (string-to-char s0) ?#)
  7998. (> (length s0) 1)
  7999. (save-excursion
  8000. (goto-char (point-min))
  8001. (and
  8002. (re-search-forward
  8003. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8004. (setq type 'dedicated
  8005. pos (match-beginning 0))))
  8006. ;; There is an exact target for this
  8007. (goto-char pos)
  8008. (org-back-to-heading t)))
  8009. ((save-excursion
  8010. (goto-char (point-min))
  8011. (and
  8012. (re-search-forward
  8013. (concat "<<" (regexp-quote s0) ">>") nil t)
  8014. (setq type 'dedicated
  8015. pos (match-beginning 0))))
  8016. ;; There is an exact target for this
  8017. (goto-char pos))
  8018. ((and (string-match "^(\\(.*\\))$" s0)
  8019. (save-excursion
  8020. (goto-char (point-min))
  8021. (and
  8022. (re-search-forward
  8023. (concat "[^[]" (regexp-quote
  8024. (format org-coderef-label-format
  8025. (match-string 1 s0))))
  8026. nil t)
  8027. (setq type 'dedicated
  8028. pos (1+ (match-beginning 0))))))
  8029. ;; There is a coderef target for this
  8030. (goto-char pos))
  8031. ((string-match "^/\\(.*\\)/$" s)
  8032. ;; A regular expression
  8033. (cond
  8034. ((org-mode-p)
  8035. (org-occur (match-string 1 s)))
  8036. ;;((eq major-mode 'dired-mode)
  8037. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8038. (t (org-do-occur (match-string 1 s)))))
  8039. (t
  8040. ;; A normal search strings
  8041. (when (equal (string-to-char s) ?*)
  8042. ;; Anchor on headlines, post may include tags.
  8043. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8044. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8045. s (substring s 1)))
  8046. (remove-text-properties
  8047. 0 (length s)
  8048. '(face nil mouse-face nil keymap nil fontified nil) s)
  8049. ;; Make a series of regular expressions to find a match
  8050. (setq words (org-split-string s "[ \n\r\t]+")
  8051. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8052. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8053. "\\)" markers)
  8054. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8055. re2a (concat "[ \t\r\n]" re2a_)
  8056. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8057. re4 (concat "[^a-zA-Z_]" re4_)
  8058. re1 (concat pre re2 post)
  8059. re3 (concat pre (if pre re4_ re4) post)
  8060. re5 (concat pre ".*" re4)
  8061. re2 (concat pre re2)
  8062. re2a (concat pre (if pre re2a_ re2a))
  8063. re4 (concat pre (if pre re4_ re4))
  8064. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8065. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8066. re5 "\\)"
  8067. ))
  8068. (cond
  8069. ((eq type 'org-occur) (org-occur reall))
  8070. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8071. (t (goto-char (point-min))
  8072. (setq type 'fuzzy)
  8073. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8074. (org-search-not-self 1 re1 nil t)
  8075. (org-search-not-self 1 re2 nil t)
  8076. (org-search-not-self 1 re2a nil t)
  8077. (org-search-not-self 1 re3 nil t)
  8078. (org-search-not-self 1 re4 nil t)
  8079. (org-search-not-self 1 re5 nil t)
  8080. )
  8081. (goto-char (match-beginning 1))
  8082. (goto-char pos)
  8083. (error "No match")))))
  8084. (t
  8085. ;; Normal string-search
  8086. (goto-char (point-min))
  8087. (if (search-forward s nil t)
  8088. (goto-char (match-beginning 0))
  8089. (error "No match"))))
  8090. (and (org-mode-p) (org-show-context 'link-search))
  8091. type))
  8092. (defun org-search-not-self (group &rest args)
  8093. "Execute `re-search-forward', but only accept matches that do not
  8094. enclose the position of `org-open-link-marker'."
  8095. (let ((m org-open-link-marker))
  8096. (catch 'exit
  8097. (while (apply 're-search-forward args)
  8098. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8099. (goto-char (match-end group))
  8100. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8101. (> (match-beginning 0) (marker-position m))
  8102. (< (match-end 0) (marker-position m)))
  8103. (save-match-data
  8104. (or (not (org-in-regexp
  8105. org-bracket-link-analytic-regexp 1))
  8106. (not (match-end 4)) ; no description
  8107. (and (<= (match-beginning 4) (point))
  8108. (>= (match-end 4) (point))))))
  8109. (throw 'exit (point))))))))
  8110. (defun org-get-buffer-for-internal-link (buffer)
  8111. "Return a buffer to be used for displaying the link target of internal links."
  8112. (cond
  8113. ((not org-display-internal-link-with-indirect-buffer)
  8114. buffer)
  8115. ((string-match "(Clone)$" (buffer-name buffer))
  8116. (message "Buffer is already a clone, not making another one")
  8117. ;; we also do not modify visibility in this case
  8118. buffer)
  8119. (t ; make a new indirect buffer for displaying the link
  8120. (let* ((bn (buffer-name buffer))
  8121. (ibn (concat bn "(Clone)"))
  8122. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8123. (with-current-buffer ib (org-overview))
  8124. ib))))
  8125. (defun org-do-occur (regexp &optional cleanup)
  8126. "Call the Emacs command `occur'.
  8127. If CLEANUP is non-nil, remove the printout of the regular expression
  8128. in the *Occur* buffer. This is useful if the regex is long and not useful
  8129. to read."
  8130. (occur regexp)
  8131. (when cleanup
  8132. (let ((cwin (selected-window)) win beg end)
  8133. (when (setq win (get-buffer-window "*Occur*"))
  8134. (select-window win))
  8135. (goto-char (point-min))
  8136. (when (re-search-forward "match[a-z]+" nil t)
  8137. (setq beg (match-end 0))
  8138. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8139. (setq end (1- (match-beginning 0)))))
  8140. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8141. (goto-char (point-min))
  8142. (select-window cwin))))
  8143. ;;; The mark ring for links jumps
  8144. (defvar org-mark-ring nil
  8145. "Mark ring for positions before jumps in Org-mode.")
  8146. (defvar org-mark-ring-last-goto nil
  8147. "Last position in the mark ring used to go back.")
  8148. ;; Fill and close the ring
  8149. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8150. (loop for i from 1 to org-mark-ring-length do
  8151. (push (make-marker) org-mark-ring))
  8152. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8153. org-mark-ring)
  8154. (defun org-mark-ring-push (&optional pos buffer)
  8155. "Put the current position or POS into the mark ring and rotate it."
  8156. (interactive)
  8157. (setq pos (or pos (point)))
  8158. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8159. (move-marker (car org-mark-ring)
  8160. (or pos (point))
  8161. (or buffer (current-buffer)))
  8162. (message "%s"
  8163. (substitute-command-keys
  8164. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8165. (defun org-mark-ring-goto (&optional n)
  8166. "Jump to the previous position in the mark ring.
  8167. With prefix arg N, jump back that many stored positions. When
  8168. called several times in succession, walk through the entire ring.
  8169. Org-mode commands jumping to a different position in the current file,
  8170. or to another Org-mode file, automatically push the old position
  8171. onto the ring."
  8172. (interactive "p")
  8173. (let (p m)
  8174. (if (eq last-command this-command)
  8175. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8176. (setq p org-mark-ring))
  8177. (setq org-mark-ring-last-goto p)
  8178. (setq m (car p))
  8179. (switch-to-buffer (marker-buffer m))
  8180. (goto-char m)
  8181. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8182. (defun org-remove-angle-brackets (s)
  8183. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8184. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8185. s)
  8186. (defun org-add-angle-brackets (s)
  8187. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8188. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8189. s)
  8190. (defun org-remove-double-quotes (s)
  8191. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8192. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8193. s)
  8194. ;;; Following specific links
  8195. (defun org-follow-timestamp-link ()
  8196. (cond
  8197. ((org-at-date-range-p t)
  8198. (let ((org-agenda-start-on-weekday)
  8199. (t1 (match-string 1))
  8200. (t2 (match-string 2)))
  8201. (setq t1 (time-to-days (org-time-string-to-time t1))
  8202. t2 (time-to-days (org-time-string-to-time t2)))
  8203. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8204. ((org-at-timestamp-p t)
  8205. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8206. (substring (match-string 1) 0 10)))
  8207. 1))
  8208. (t (error "This should not happen"))))
  8209. ;;; Following file links
  8210. (defvar org-wait nil)
  8211. (defun org-open-file (path &optional in-emacs line search)
  8212. "Open the file at PATH.
  8213. First, this expands any special file name abbreviations. Then the
  8214. configuration variable `org-file-apps' is checked if it contains an
  8215. entry for this file type, and if yes, the corresponding command is launched.
  8216. If no application is found, Emacs simply visits the file.
  8217. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8218. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8219. and to use an external application to visit the file.
  8220. Optional LINE specifies a line to go to, optional SEARCH a string to
  8221. search for. If LINE or SEARCH is given, but IN-EMACS is nil, it will
  8222. be assumed that org-open-file was called to open a file: link, and the
  8223. original link to match against org-file-apps will be reconstructed
  8224. from PATH and whichever of LINE or SEARCH is given.
  8225. If the file does not exist, an error is thrown."
  8226. (let* ((file (if (equal path "")
  8227. buffer-file-name
  8228. (substitute-in-file-name (expand-file-name path))))
  8229. (apps (append org-file-apps (org-default-apps)))
  8230. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8231. (dirp (if remp nil (file-directory-p file)))
  8232. (file (if (and dirp org-open-directory-means-index-dot-org)
  8233. (concat (file-name-as-directory file) "index.org")
  8234. file))
  8235. (a-m-a-p (assq 'auto-mode apps))
  8236. (dfile (downcase file))
  8237. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8238. (link (cond ((and (eq line nil)
  8239. (eq search nil))
  8240. file)
  8241. (line
  8242. (concat file "::" (number-to-string line)))
  8243. (search
  8244. (concat file "::" search))))
  8245. (dlink (downcase link))
  8246. (old-buffer (current-buffer))
  8247. (old-pos (point))
  8248. (old-mode major-mode)
  8249. ext cmd link-match-data)
  8250. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8251. (setq ext (match-string 1 dfile))
  8252. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8253. (setq ext (match-string 1 dfile))))
  8254. (cond
  8255. ((member in-emacs '((16) system))
  8256. (setq cmd (cdr (assoc 'system apps))))
  8257. (in-emacs (setq cmd 'emacs))
  8258. (t
  8259. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8260. (and dirp (cdr (assoc 'directory apps)))
  8261. ;; if we find a match in org-file-apps, store the match
  8262. ;; data for later
  8263. (let* ((re-list1 (org-apps-regexp-alist apps nil))
  8264. (re-list2
  8265. (if a-m-a-p
  8266. (org-apps-regexp-alist apps a-m-a-p)
  8267. re-list1))
  8268. (private-match
  8269. (assoc-default dlink re-list1 'string-match))
  8270. (general-match
  8271. (assoc-default dfile re-list2 'string-match)))
  8272. (if private-match
  8273. (progn (setq link-match-data (match-data))
  8274. private-match)
  8275. general-match))
  8276. (cdr (assoc ext apps))
  8277. (cdr (assoc t apps))))))
  8278. (when (eq cmd 'system)
  8279. (setq cmd (cdr (assoc 'system apps))))
  8280. (when (eq cmd 'default)
  8281. (setq cmd (cdr (assoc t apps))))
  8282. (when (eq cmd 'mailcap)
  8283. (require 'mailcap)
  8284. (mailcap-parse-mailcaps)
  8285. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8286. (command (mailcap-mime-info mime-type)))
  8287. (if (stringp command)
  8288. (setq cmd command)
  8289. (setq cmd 'emacs))))
  8290. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8291. (not (file-exists-p file))
  8292. (not org-open-non-existing-files))
  8293. (error "No such file: %s" file))
  8294. (cond
  8295. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8296. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8297. (while (string-match "['\"]%s['\"]" cmd)
  8298. (setq cmd (replace-match "%s" t t cmd)))
  8299. (while (string-match "%s" cmd)
  8300. (setq cmd (replace-match
  8301. (save-match-data
  8302. (shell-quote-argument
  8303. (convert-standard-filename file)))
  8304. t t cmd)))
  8305. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8306. (save-match-data
  8307. (let ((match-index 1)
  8308. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8309. (set-match-data link-match-data)
  8310. (while (<= match-index number-of-groups)
  8311. (let ((regex (concat "%" (number-to-string match-index)))
  8312. (replace-with (match-string match-index dlink)))
  8313. (while (string-match regex cmd)
  8314. (setq cmd (replace-match replace-with t t cmd))))
  8315. (setq match-index (+ match-index 1)))))
  8316. (save-window-excursion
  8317. (start-process-shell-command cmd nil cmd)
  8318. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8319. ))
  8320. ((or (stringp cmd)
  8321. (eq cmd 'emacs))
  8322. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8323. (widen)
  8324. (if line (org-goto-line line)
  8325. (if search (org-link-search search))))
  8326. ((consp cmd)
  8327. (let ((file (convert-standard-filename file)))
  8328. (save-match-data
  8329. (set-match-data link-match-data)
  8330. (eval cmd))))
  8331. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8332. (and (org-mode-p) (eq old-mode 'org-mode)
  8333. (or (not (equal old-buffer (current-buffer)))
  8334. (not (equal old-pos (point))))
  8335. (org-mark-ring-push old-pos old-buffer))))
  8336. (defun org-default-apps ()
  8337. "Return the default applications for this operating system."
  8338. (cond
  8339. ((eq system-type 'darwin)
  8340. org-file-apps-defaults-macosx)
  8341. ((eq system-type 'windows-nt)
  8342. org-file-apps-defaults-windowsnt)
  8343. (t org-file-apps-defaults-gnu)))
  8344. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8345. "Convert extensions to regular expressions in the cars of LIST.
  8346. Also, weed out any non-string entries, because the return value is used
  8347. only for regexp matching.
  8348. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8349. point to the symbol `emacs', indicating that the file should
  8350. be opened in Emacs."
  8351. (append
  8352. (delq nil
  8353. (mapcar (lambda (x)
  8354. (if (not (stringp (car x)))
  8355. nil
  8356. (if (string-match "\\W" (car x))
  8357. x
  8358. (cons (concat "\\." (car x) "\\(::.*\\)?\\'")
  8359. (cdr x)))))
  8360. list))
  8361. (if add-auto-mode
  8362. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8363. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8364. (defun org-file-remote-p (file)
  8365. "Test whether FILE specifies a location on a remote system.
  8366. Return non-nil if the location is indeed remote.
  8367. For example, the filename \"/user@host:/foo\" specifies a location
  8368. on the system \"/user@host:\"."
  8369. (cond ((fboundp 'file-remote-p)
  8370. (file-remote-p file))
  8371. ((fboundp 'tramp-handle-file-remote-p)
  8372. (tramp-handle-file-remote-p file))
  8373. ((and (boundp 'ange-ftp-name-format)
  8374. (string-match (car ange-ftp-name-format) file))
  8375. t)
  8376. (t nil)))
  8377. ;;;; Refiling
  8378. (defun org-get-org-file ()
  8379. "Read a filename, with default directory `org-directory'."
  8380. (let ((default (or org-default-notes-file remember-data-file)))
  8381. (read-file-name (format "File name [%s]: " default)
  8382. (file-name-as-directory org-directory)
  8383. default)))
  8384. (defun org-notes-order-reversed-p ()
  8385. "Check if the current file should receive notes in reversed order."
  8386. (cond
  8387. ((not org-reverse-note-order) nil)
  8388. ((eq t org-reverse-note-order) t)
  8389. ((not (listp org-reverse-note-order)) nil)
  8390. (t (catch 'exit
  8391. (let ((all org-reverse-note-order)
  8392. entry)
  8393. (while (setq entry (pop all))
  8394. (if (string-match (car entry) buffer-file-name)
  8395. (throw 'exit (cdr entry))))
  8396. nil)))))
  8397. (defvar org-refile-target-table nil
  8398. "The list of refile targets, created by `org-refile'.")
  8399. (defvar org-agenda-new-buffers nil
  8400. "Buffers created to visit agenda files.")
  8401. (defun org-get-refile-targets (&optional default-buffer)
  8402. "Produce a table with refile targets."
  8403. (let ((case-fold-search nil)
  8404. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8405. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8406. targets txt re files f desc descre fast-path-p level pos0)
  8407. (message "Getting targets...")
  8408. (with-current-buffer (or default-buffer (current-buffer))
  8409. (while (setq entry (pop entries))
  8410. (setq files (car entry) desc (cdr entry))
  8411. (setq fast-path-p nil)
  8412. (cond
  8413. ((null files) (setq files (list (current-buffer))))
  8414. ((eq files 'org-agenda-files)
  8415. (setq files (org-agenda-files 'unrestricted)))
  8416. ((and (symbolp files) (fboundp files))
  8417. (setq files (funcall files)))
  8418. ((and (symbolp files) (boundp files))
  8419. (setq files (symbol-value files))))
  8420. (if (stringp files) (setq files (list files)))
  8421. (cond
  8422. ((eq (car desc) :tag)
  8423. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8424. ((eq (car desc) :todo)
  8425. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8426. ((eq (car desc) :regexp)
  8427. (setq descre (cdr desc)))
  8428. ((eq (car desc) :level)
  8429. (setq descre (concat "^\\*\\{" (number-to-string
  8430. (if org-odd-levels-only
  8431. (1- (* 2 (cdr desc)))
  8432. (cdr desc)))
  8433. "\\}[ \t]")))
  8434. ((eq (car desc) :maxlevel)
  8435. (setq fast-path-p t)
  8436. (setq descre (concat "^\\*\\{1," (number-to-string
  8437. (if org-odd-levels-only
  8438. (1- (* 2 (cdr desc)))
  8439. (cdr desc)))
  8440. "\\}[ \t]")))
  8441. (t (error "Bad refiling target description %s" desc)))
  8442. (while (setq f (pop files))
  8443. (with-current-buffer
  8444. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8445. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8446. (setq f (and f (expand-file-name f)))
  8447. (if (eq org-refile-use-outline-path 'file)
  8448. (push (list (file-name-nondirectory f) f nil nil) targets))
  8449. (save-excursion
  8450. (save-restriction
  8451. (widen)
  8452. (goto-char (point-min))
  8453. (while (re-search-forward descre nil t)
  8454. (goto-char (setq pos0 (point-at-bol)))
  8455. (catch 'next
  8456. (when org-refile-target-verify-function
  8457. (save-match-data
  8458. (or (funcall org-refile-target-verify-function)
  8459. (throw 'next t))))
  8460. (when (looking-at org-complex-heading-regexp)
  8461. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8462. txt (org-link-display-format (match-string 4))
  8463. re (concat "^" (regexp-quote
  8464. (buffer-substring (match-beginning 1)
  8465. (match-end 4)))))
  8466. (if (match-end 5) (setq re (concat re "[ \t]+"
  8467. (regexp-quote
  8468. (match-string 5)))))
  8469. (setq re (concat re "[ \t]*$"))
  8470. (when org-refile-use-outline-path
  8471. (setq txt (mapconcat 'org-protect-slash
  8472. (append
  8473. (if (eq org-refile-use-outline-path 'file)
  8474. (list (file-name-nondirectory
  8475. (buffer-file-name (buffer-base-buffer))))
  8476. (if (eq org-refile-use-outline-path 'full-file-path)
  8477. (list (buffer-file-name (buffer-base-buffer)))))
  8478. (org-get-outline-path fast-path-p level txt)
  8479. (list txt))
  8480. "/")))
  8481. (push (list txt f re (point)) targets)))
  8482. (when (= (point) pos0)
  8483. ;; verification function has not moved point
  8484. (goto-char (point-at-eol))))))))))
  8485. (message "Getting targets...done")
  8486. (nreverse targets)))
  8487. (defun org-protect-slash (s)
  8488. (while (string-match "/" s)
  8489. (setq s (replace-match "\\" t t s)))
  8490. s)
  8491. (defvar org-olpa (make-vector 20 nil))
  8492. (defun org-get-outline-path (&optional fastp level heading)
  8493. "Return the outline path to the current entry, as a list.
  8494. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8495. routine which makes outline path derivations for an entire file,
  8496. avoiding backtracing."
  8497. (if fastp
  8498. (progn
  8499. (if (> level 19)
  8500. (error "Outline path failure, more than 19 levels."))
  8501. (loop for i from level upto 19 do
  8502. (aset org-olpa i nil))
  8503. (prog1
  8504. (delq nil (append org-olpa nil))
  8505. (aset org-olpa level heading)))
  8506. (let (rtn case-fold-search)
  8507. (save-excursion
  8508. (save-restriction
  8509. (widen)
  8510. (while (org-up-heading-safe)
  8511. (when (looking-at org-complex-heading-regexp)
  8512. (push (org-match-string-no-properties 4) rtn)))
  8513. rtn)))))
  8514. (defun org-format-outline-path (path &optional width prefix)
  8515. "Format the outlie path PATH for display.
  8516. Width is the maximum number of characters that is available.
  8517. Prefix is a prefix to be included in the returned string,
  8518. such as the file name."
  8519. (setq width (or width 79))
  8520. (if prefix (setq width (- width (length prefix))))
  8521. (if (not path)
  8522. (or prefix "")
  8523. (let* ((nsteps (length path))
  8524. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8525. (maxwidth (if (<= total-width width)
  8526. 10000 ;; everything fits
  8527. ;; we need to shorten the level headings
  8528. (/ (- width nsteps) nsteps)))
  8529. (org-odd-levels-only nil)
  8530. (n 0)
  8531. (total (1+ (length prefix))))
  8532. (setq maxwidth (max maxwidth 10))
  8533. (concat prefix
  8534. (mapconcat
  8535. (lambda (h)
  8536. (setq n (1+ n))
  8537. (if (and (= n nsteps) (< maxwidth 10000))
  8538. (setq maxwidth (- total-width total)))
  8539. (if (< (length h) maxwidth)
  8540. (progn (setq total (+ total (length h) 1)) h)
  8541. (setq h (substring h 0 (- maxwidth 2))
  8542. total (+ total maxwidth 1))
  8543. (if (string-match "[ \t]+\\'" h)
  8544. (setq h (substring h 0 (match-beginning 0))))
  8545. (setq h (concat h "..")))
  8546. (org-add-props h nil 'face
  8547. (nth (% (1- n) org-n-level-faces)
  8548. org-level-faces))
  8549. h)
  8550. path "/")))))
  8551. (defun org-display-outline-path (&optional file current)
  8552. "Display the current outline path in the echo area."
  8553. (interactive "P")
  8554. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8555. (case-fold-search nil)
  8556. (path (and (org-mode-p) (org-get-outline-path))))
  8557. (if current (setq path (append path
  8558. (save-excursion
  8559. (org-back-to-heading t)
  8560. (if (looking-at org-complex-heading-regexp)
  8561. (list (match-string 4)))))))
  8562. (message "%s"
  8563. (org-format-outline-path
  8564. path
  8565. (1- (frame-width))
  8566. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8567. (defvar org-refile-history nil
  8568. "History for refiling operations.")
  8569. (defvar org-after-refile-insert-hook nil
  8570. "Hook run after `org-refile' has inserted its stuff at the new location.
  8571. Note that this is still *before* the stuff will be removed from
  8572. the *old* location.")
  8573. (defun org-refile (&optional goto default-buffer rfloc)
  8574. "Move the entry at point to another heading.
  8575. The list of target headings is compiled using the information in
  8576. `org-refile-targets', which see. This list is created before each use
  8577. and will therefore always be up-to-date.
  8578. At the target location, the entry is filed as a subitem of the target heading.
  8579. Depending on `org-reverse-note-order', the new subitem will either be the
  8580. first or the last subitem.
  8581. If there is an active region, all entries in that region will be moved.
  8582. However, the region must fulfil the requirement that the first heading
  8583. is the first one sets the top-level of the moved text - at most siblings
  8584. below it are allowed.
  8585. With prefix arg GOTO, the command will only visit the target location,
  8586. not actually move anything.
  8587. With a double prefix `C-u C-u', go to the location where the last refiling
  8588. operation has put the subtree.
  8589. With a prefix argument of `2', refile to the running clock.
  8590. RFLOC can be a refile location obtained in a different way.
  8591. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8592. (interactive "P")
  8593. (let* ((cbuf (current-buffer))
  8594. (regionp (org-region-active-p))
  8595. (region-start (and regionp (region-beginning)))
  8596. (region-end (and regionp (region-end)))
  8597. (region-length (and regionp (- region-end region-start)))
  8598. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8599. pos it nbuf file re level reversed)
  8600. (setq last-command nil)
  8601. (when regionp
  8602. (goto-char region-start)
  8603. (or (bolp) (goto-char (point-at-bol)))
  8604. (setq region-start (point))
  8605. (unless (org-kill-is-subtree-p
  8606. (buffer-substring region-start region-end))
  8607. (error "The region is not a (sequence of) subtree(s)")))
  8608. (if (equal goto '(16))
  8609. (org-refile-goto-last-stored)
  8610. (when (or
  8611. (and (equal goto 2)
  8612. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8613. (prog1
  8614. (setq it (list (or org-clock-heading "running clock")
  8615. (buffer-file-name
  8616. (marker-buffer org-clock-hd-marker))
  8617. ""
  8618. (marker-position org-clock-hd-marker)))
  8619. (setq goto nil)))
  8620. (setq it (or rfloc
  8621. (save-excursion
  8622. (org-refile-get-location
  8623. (if goto "Goto: " "Refile to: ") default-buffer
  8624. org-refile-allow-creating-parent-nodes)))))
  8625. (setq file (nth 1 it)
  8626. re (nth 2 it)
  8627. pos (nth 3 it))
  8628. (if (and (not goto)
  8629. pos
  8630. (equal (buffer-file-name) file)
  8631. (if regionp
  8632. (and (>= pos region-start)
  8633. (<= pos region-end))
  8634. (and (>= pos (point))
  8635. (< pos (save-excursion
  8636. (org-end-of-subtree t t))))))
  8637. (error "Cannot refile to position inside the tree or region"))
  8638. (setq nbuf (or (find-buffer-visiting file)
  8639. (find-file-noselect file)))
  8640. (if goto
  8641. (progn
  8642. (switch-to-buffer nbuf)
  8643. (goto-char pos)
  8644. (org-show-context 'org-goto))
  8645. (if regionp
  8646. (progn
  8647. (org-kill-new (buffer-substring region-start region-end))
  8648. (org-save-markers-in-region region-start region-end))
  8649. (org-copy-subtree 1 nil t))
  8650. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8651. (find-file-noselect file)))
  8652. (setq reversed (org-notes-order-reversed-p))
  8653. (save-excursion
  8654. (save-restriction
  8655. (widen)
  8656. (if pos
  8657. (progn
  8658. (goto-char pos)
  8659. (looking-at outline-regexp)
  8660. (setq level (org-get-valid-level (funcall outline-level) 1))
  8661. (goto-char
  8662. (if reversed
  8663. (or (outline-next-heading) (point-max))
  8664. (or (save-excursion (org-get-next-sibling))
  8665. (org-end-of-subtree t t)
  8666. (point-max)))))
  8667. (setq level 1)
  8668. (if (not reversed)
  8669. (goto-char (point-max))
  8670. (goto-char (point-min))
  8671. (or (outline-next-heading) (goto-char (point-max)))))
  8672. (if (not (bolp)) (newline))
  8673. (org-paste-subtree level)
  8674. (when org-log-refile
  8675. (org-add-log-setup 'refile nil nil 'findpos
  8676. org-log-refile)
  8677. (unless (eq org-log-refile 'note)
  8678. (save-excursion (org-add-log-note))))
  8679. (and org-auto-align-tags (org-set-tags nil t))
  8680. (bookmark-set "org-refile-last-stored")
  8681. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8682. (run-hooks 'org-after-refile-insert-hook))))
  8683. (if regionp
  8684. (delete-region (point) (+ (point) region-length))
  8685. (org-cut-subtree))
  8686. (when (featurep 'org-inlinetask)
  8687. (org-inlinetask-remove-END-maybe))
  8688. (setq org-markers-to-move nil)
  8689. (message "Refiled to \"%s\"" (car it))))))
  8690. (org-reveal))
  8691. (defun org-refile-goto-last-stored ()
  8692. "Go to the location where the last refile was stored."
  8693. (interactive)
  8694. (bookmark-jump "org-refile-last-stored")
  8695. (message "This is the location of the last refile"))
  8696. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8697. "Prompt the user for a refile location, using PROMPT."
  8698. (let ((org-refile-targets org-refile-targets)
  8699. (org-refile-use-outline-path org-refile-use-outline-path))
  8700. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8701. (unless org-refile-target-table
  8702. (error "No refile targets"))
  8703. (let* ((cbuf (current-buffer))
  8704. (partial-completion-mode nil)
  8705. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8706. (cfunc (if (and org-refile-use-outline-path
  8707. org-outline-path-complete-in-steps)
  8708. 'org-olpath-completing-read
  8709. 'org-icompleting-read))
  8710. (extra (if org-refile-use-outline-path "/" ""))
  8711. (filename (and cfn (expand-file-name cfn)))
  8712. (tbl (mapcar
  8713. (lambda (x)
  8714. (if (and (not (member org-refile-use-outline-path
  8715. '(file full-file-path)))
  8716. (not (equal filename (nth 1 x))))
  8717. (cons (concat (car x) extra " ("
  8718. (file-name-nondirectory (nth 1 x)) ")")
  8719. (cdr x))
  8720. (cons (concat (car x) extra) (cdr x))))
  8721. org-refile-target-table))
  8722. (completion-ignore-case t)
  8723. pa answ parent-target child parent old-hist)
  8724. (setq old-hist org-refile-history)
  8725. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8726. nil 'org-refile-history))
  8727. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8728. (if pa
  8729. (progn
  8730. (when (or (not org-refile-history)
  8731. (not (eq old-hist org-refile-history))
  8732. (not (equal (car pa) (car org-refile-history))))
  8733. (setq org-refile-history
  8734. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8735. org-refile-history
  8736. (cdr org-refile-history))))
  8737. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8738. (pop org-refile-history)))
  8739. pa)
  8740. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8741. (progn
  8742. (setq parent (match-string 1 answ)
  8743. child (match-string 2 answ))
  8744. (setq parent-target (or (assoc parent tbl)
  8745. (assoc (concat parent "/") tbl)))
  8746. (when (and parent-target
  8747. (or (eq new-nodes t)
  8748. (and (eq new-nodes 'confirm)
  8749. (y-or-n-p (format "Create new node \"%s\"? "
  8750. child)))))
  8751. (org-refile-new-child parent-target child)))
  8752. (error "Invalid target location")))))
  8753. (defun org-refile-new-child (parent-target child)
  8754. "Use refile target PARENT-TARGET to add new CHILD below it."
  8755. (unless parent-target
  8756. (error "Cannot find parent for new node"))
  8757. (let ((file (nth 1 parent-target))
  8758. (pos (nth 3 parent-target))
  8759. level)
  8760. (with-current-buffer (or (find-buffer-visiting file)
  8761. (find-file-noselect file))
  8762. (save-excursion
  8763. (save-restriction
  8764. (widen)
  8765. (if pos
  8766. (goto-char pos)
  8767. (goto-char (point-max))
  8768. (if (not (bolp)) (newline)))
  8769. (when (looking-at outline-regexp)
  8770. (setq level (funcall outline-level))
  8771. (org-end-of-subtree t t))
  8772. (org-back-over-empty-lines)
  8773. (insert "\n" (make-string
  8774. (if pos (org-get-valid-level level 1) 1) ?*)
  8775. " " child "\n")
  8776. (beginning-of-line 0)
  8777. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8778. (defun org-olpath-completing-read (prompt collection &rest args)
  8779. "Read an outline path like a file name."
  8780. (let ((thetable collection)
  8781. (org-completion-use-ido nil) ; does not work with ido.
  8782. (org-completion-use-iswitchb nil)) ; or iswitchb
  8783. (apply
  8784. 'org-icompleting-read prompt
  8785. (lambda (string predicate &optional flag)
  8786. (let (rtn r f (l (length string)))
  8787. (cond
  8788. ((eq flag nil)
  8789. ;; try completion
  8790. (try-completion string thetable))
  8791. ((eq flag t)
  8792. ;; all-completions
  8793. (setq rtn (all-completions string thetable predicate))
  8794. (mapcar
  8795. (lambda (x)
  8796. (setq r (substring x l))
  8797. (if (string-match " ([^)]*)$" x)
  8798. (setq f (match-string 0 x))
  8799. (setq f ""))
  8800. (if (string-match "/" r)
  8801. (concat string (substring r 0 (match-end 0)) f)
  8802. x))
  8803. rtn))
  8804. ((eq flag 'lambda)
  8805. ;; exact match?
  8806. (assoc string thetable)))
  8807. ))
  8808. args)))
  8809. ;;;; Dynamic blocks
  8810. (defun org-find-dblock (name)
  8811. "Find the first dynamic block with name NAME in the buffer.
  8812. If not found, stay at current position and return nil."
  8813. (let (pos)
  8814. (save-excursion
  8815. (goto-char (point-min))
  8816. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8817. nil t)
  8818. (match-beginning 0))))
  8819. (if pos (goto-char pos))
  8820. pos))
  8821. (defconst org-dblock-start-re
  8822. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8823. "Matches the start line of a dynamic block, with parameters.")
  8824. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8825. "Matches the end of a dynamic block.")
  8826. (defun org-create-dblock (plist)
  8827. "Create a dynamic block section, with parameters taken from PLIST.
  8828. PLIST must contain a :name entry which is used as name of the block."
  8829. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8830. (end-of-line 1)
  8831. (newline))
  8832. (let ((col (current-column))
  8833. (name (plist-get plist :name)))
  8834. (insert "#+BEGIN: " name)
  8835. (while plist
  8836. (if (eq (car plist) :name)
  8837. (setq plist (cddr plist))
  8838. (insert " " (prin1-to-string (pop plist)))))
  8839. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8840. (beginning-of-line -2)))
  8841. (defun org-prepare-dblock ()
  8842. "Prepare dynamic block for refresh.
  8843. This empties the block, puts the cursor at the insert position and returns
  8844. the property list including an extra property :name with the block name."
  8845. (unless (looking-at org-dblock-start-re)
  8846. (error "Not at a dynamic block"))
  8847. (let* ((begdel (1+ (match-end 0)))
  8848. (name (org-no-properties (match-string 1)))
  8849. (params (append (list :name name)
  8850. (read (concat "(" (match-string 3) ")")))))
  8851. (save-excursion
  8852. (beginning-of-line 1)
  8853. (skip-chars-forward " \t")
  8854. (setq params (plist-put params :indentation-column (current-column))))
  8855. (unless (re-search-forward org-dblock-end-re nil t)
  8856. (error "Dynamic block not terminated"))
  8857. (setq params
  8858. (append params
  8859. (list :content (buffer-substring
  8860. begdel (match-beginning 0)))))
  8861. (delete-region begdel (match-beginning 0))
  8862. (goto-char begdel)
  8863. (open-line 1)
  8864. params))
  8865. (defun org-map-dblocks (&optional command)
  8866. "Apply COMMAND to all dynamic blocks in the current buffer.
  8867. If COMMAND is not given, use `org-update-dblock'."
  8868. (let ((cmd (or command 'org-update-dblock)))
  8869. (save-excursion
  8870. (goto-char (point-min))
  8871. (while (re-search-forward org-dblock-start-re nil t)
  8872. (goto-char (match-beginning 0))
  8873. (save-excursion
  8874. (condition-case nil
  8875. (funcall cmd)
  8876. (error (message "Error during update of dynamic block"))))
  8877. (unless (re-search-forward org-dblock-end-re nil t)
  8878. (error "Dynamic block not terminated"))))))
  8879. (defun org-dblock-update (&optional arg)
  8880. "User command for updating dynamic blocks.
  8881. Update the dynamic block at point. With prefix ARG, update all dynamic
  8882. blocks in the buffer."
  8883. (interactive "P")
  8884. (if arg
  8885. (org-update-all-dblocks)
  8886. (or (looking-at org-dblock-start-re)
  8887. (org-beginning-of-dblock))
  8888. (org-update-dblock)))
  8889. (defun org-update-dblock ()
  8890. "Update the dynamic block at point
  8891. This means to empty the block, parse for parameters and then call
  8892. the correct writing function."
  8893. (save-window-excursion
  8894. (let* ((pos (point))
  8895. (line (org-current-line))
  8896. (params (org-prepare-dblock))
  8897. (name (plist-get params :name))
  8898. (indent (plist-get params :indentation-column))
  8899. (cmd (intern (concat "org-dblock-write:" name))))
  8900. (message "Updating dynamic block `%s' at line %d..." name line)
  8901. (funcall cmd params)
  8902. (message "Updating dynamic block `%s' at line %d...done" name line)
  8903. (goto-char pos)
  8904. (when (and indent (> indent 0))
  8905. (setq indent (make-string indent ?\ ))
  8906. (save-excursion
  8907. (org-beginning-of-dblock)
  8908. (forward-line 1)
  8909. (while (not (looking-at org-dblock-end-re))
  8910. (insert indent)
  8911. (beginning-of-line 2))
  8912. (when (looking-at org-dblock-end-re)
  8913. (and (looking-at "[ \t]+")
  8914. (replace-match ""))
  8915. (insert indent)))))))
  8916. (defun org-beginning-of-dblock ()
  8917. "Find the beginning of the dynamic block at point.
  8918. Error if there is no such block at point."
  8919. (let ((pos (point))
  8920. beg)
  8921. (end-of-line 1)
  8922. (if (and (re-search-backward org-dblock-start-re nil t)
  8923. (setq beg (match-beginning 0))
  8924. (re-search-forward org-dblock-end-re nil t)
  8925. (> (match-end 0) pos))
  8926. (goto-char beg)
  8927. (goto-char pos)
  8928. (error "Not in a dynamic block"))))
  8929. (defun org-update-all-dblocks ()
  8930. "Update all dynamic blocks in the buffer.
  8931. This function can be used in a hook."
  8932. (when (org-mode-p)
  8933. (org-map-dblocks 'org-update-dblock)))
  8934. ;;;; Completion
  8935. (defconst org-additional-option-like-keywords
  8936. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8937. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8938. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8939. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8940. "BEGIN:" "END:"
  8941. "ORGTBL" "TBLFM:" "TBLNAME:"
  8942. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8943. "BEGIN_QUOTE" "END_QUOTE"
  8944. "BEGIN_VERSE" "END_VERSE"
  8945. "BEGIN_CENTER" "END_CENTER"
  8946. "BEGIN_SRC" "END_SRC"
  8947. "CATEGORY" "COLUMNS"
  8948. "CAPTION" "LABEL"
  8949. "SETUPFILE"
  8950. "BIND"
  8951. "MACRO"))
  8952. (defcustom org-structure-template-alist
  8953. '(
  8954. ("s" "#+begin_src ?\n\n#+end_src"
  8955. "<src lang=\"?\">\n\n</src>")
  8956. ("e" "#+begin_example\n?\n#+end_example"
  8957. "<example>\n?\n</example>")
  8958. ("q" "#+begin_quote\n?\n#+end_quote"
  8959. "<quote>\n?\n</quote>")
  8960. ("v" "#+begin_verse\n?\n#+end_verse"
  8961. "<verse>\n?\n/verse>")
  8962. ("c" "#+begin_center\n?\n#+end_center"
  8963. "<center>\n?\n/center>")
  8964. ("l" "#+begin_latex\n?\n#+end_latex"
  8965. "<literal style=\"latex\">\n?\n</literal>")
  8966. ("L" "#+latex: "
  8967. "<literal style=\"latex\">?</literal>")
  8968. ("h" "#+begin_html\n?\n#+end_html"
  8969. "<literal style=\"html\">\n?\n</literal>")
  8970. ("H" "#+html: "
  8971. "<literal style=\"html\">?</literal>")
  8972. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8973. ("A" "#+ascii: ")
  8974. ("i" "#+include %file ?"
  8975. "<include file=%file markup=\"?\">")
  8976. )
  8977. "Structure completion elements.
  8978. This is a list of abbreviation keys and values. The value gets inserted
  8979. it you type @samp{.} followed by the key and then the completion key,
  8980. usually `M-TAB'. %file will be replaced by a file name after prompting
  8981. for the file using completion.
  8982. There are two templates for each key, the first uses the original Org syntax,
  8983. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8984. the default when the /org-mtags.el/ module has been loaded. See also the
  8985. variable `org-mtags-prefer-muse-templates'.
  8986. This is an experimental feature, it is undecided if it is going to stay in."
  8987. :group 'org-completion
  8988. :type '(repeat
  8989. (string :tag "Key")
  8990. (string :tag "Template")
  8991. (string :tag "Muse Template")))
  8992. (defun org-try-structure-completion ()
  8993. "Try to complete a structure template before point.
  8994. This looks for strings like \"<e\" on an otherwise empty line and
  8995. expands them."
  8996. (let ((l (buffer-substring (point-at-bol) (point)))
  8997. a)
  8998. (when (and (looking-at "[ \t]*$")
  8999. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9000. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9001. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9002. (match-beginning 1)) a)
  9003. t)))
  9004. (defun org-complete-expand-structure-template (start cell)
  9005. "Expand a structure template."
  9006. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9007. (rpl (nth (if musep 2 1) cell))
  9008. (ind ""))
  9009. (delete-region start (point))
  9010. (when (string-match "\\`#\\+" rpl)
  9011. (cond
  9012. ((bolp))
  9013. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9014. (setq ind (buffer-substring (point-at-bol) (point))))
  9015. (t (newline))))
  9016. (setq start (point))
  9017. (if (string-match "%file" rpl)
  9018. (setq rpl (replace-match
  9019. (concat
  9020. "\""
  9021. (save-match-data
  9022. (abbreviate-file-name (read-file-name "Include file: ")))
  9023. "\"")
  9024. t t rpl)))
  9025. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9026. (concat "\n" ind)))
  9027. (insert rpl)
  9028. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9029. (defun org-complete (&optional arg)
  9030. "Perform completion on word at point.
  9031. At the beginning of a headline, this completes TODO keywords as given in
  9032. `org-todo-keywords'.
  9033. If the current word is preceded by a backslash, completes the TeX symbols
  9034. that are supported for HTML support.
  9035. If the current word is preceded by \"#+\", completes special words for
  9036. setting file options.
  9037. In the line after \"#+STARTUP:, complete valid keywords.\"
  9038. At all other locations, this simply calls the value of
  9039. `org-completion-fallback-command'."
  9040. (interactive "P")
  9041. (org-without-partial-completion
  9042. (catch 'exit
  9043. (let* ((a nil)
  9044. (end (point))
  9045. (beg1 (save-excursion
  9046. (skip-chars-backward (org-re "[:alnum:]_@"))
  9047. (point)))
  9048. (beg (save-excursion
  9049. (skip-chars-backward "a-zA-Z0-9_:$")
  9050. (point)))
  9051. (confirm (lambda (x) (stringp (car x))))
  9052. (searchhead (equal (char-before beg) ?*))
  9053. (struct
  9054. (when (and (member (char-before beg1) '(?. ?<))
  9055. (setq a (assoc (buffer-substring beg1 (point))
  9056. org-structure-template-alist)))
  9057. (org-complete-expand-structure-template (1- beg1) a)
  9058. (throw 'exit t)))
  9059. (tag (and (equal (char-before beg1) ?:)
  9060. (equal (char-after (point-at-bol)) ?*)))
  9061. (prop (and (equal (char-before beg1) ?:)
  9062. (not (equal (char-after (point-at-bol)) ?*))))
  9063. (texp (equal (char-before beg) ?\\))
  9064. (link (equal (char-before beg) ?\[))
  9065. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9066. beg)
  9067. "#+"))
  9068. (startup (string-match "^#\\+STARTUP:.*"
  9069. (buffer-substring (point-at-bol) (point))))
  9070. (completion-ignore-case opt)
  9071. (type nil)
  9072. (tbl nil)
  9073. (table (cond
  9074. (opt
  9075. (setq type :opt)
  9076. (require 'org-exp)
  9077. (append
  9078. (delq nil
  9079. (mapcar
  9080. (lambda (x)
  9081. (if (string-match
  9082. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9083. (cons (match-string 2 x)
  9084. (match-string 1 x))))
  9085. (org-split-string (org-get-current-options) "\n")))
  9086. (mapcar 'list org-additional-option-like-keywords)))
  9087. (startup
  9088. (setq type :startup)
  9089. org-startup-options)
  9090. (link (append org-link-abbrev-alist-local
  9091. org-link-abbrev-alist))
  9092. (texp
  9093. (setq type :tex)
  9094. (append org-entities-user org-entities))
  9095. ((string-match "\\`\\*+[ \t]+\\'"
  9096. (buffer-substring (point-at-bol) beg))
  9097. (setq type :todo)
  9098. (mapcar 'list org-todo-keywords-1))
  9099. (searchhead
  9100. (setq type :searchhead)
  9101. (save-excursion
  9102. (goto-char (point-min))
  9103. (while (re-search-forward org-todo-line-regexp nil t)
  9104. (push (list
  9105. (org-make-org-heading-search-string
  9106. (match-string 3) t))
  9107. tbl)))
  9108. tbl)
  9109. (tag (setq type :tag beg beg1)
  9110. (or org-tag-alist (org-get-buffer-tags)))
  9111. (prop (setq type :prop beg beg1)
  9112. (mapcar 'list (org-buffer-property-keys nil t t)))
  9113. (t (progn
  9114. (call-interactively org-completion-fallback-command)
  9115. (throw 'exit nil)))))
  9116. (pattern (buffer-substring-no-properties beg end))
  9117. (completion (try-completion pattern table confirm)))
  9118. (cond ((eq completion t)
  9119. (if (not (assoc (upcase pattern) table))
  9120. (message "Already complete")
  9121. (if (and (equal type :opt)
  9122. (not (member (car (assoc (upcase pattern) table))
  9123. org-additional-option-like-keywords)))
  9124. (insert (substring (cdr (assoc (upcase pattern) table))
  9125. (length pattern)))
  9126. (if (memq type '(:tag :prop)) (insert ":")))))
  9127. ((null completion)
  9128. (message "Can't find completion for \"%s\"" pattern)
  9129. (ding))
  9130. ((not (string= pattern completion))
  9131. (delete-region beg end)
  9132. (if (string-match " +$" completion)
  9133. (setq completion (replace-match "" t t completion)))
  9134. (insert completion)
  9135. (if (get-buffer-window "*Completions*")
  9136. (delete-window (get-buffer-window "*Completions*")))
  9137. (if (assoc completion table)
  9138. (if (eq type :todo) (insert " ")
  9139. (if (memq type '(:tag :prop)) (insert ":"))))
  9140. (if (and (equal type :opt) (assoc completion table))
  9141. (message "%s" (substitute-command-keys
  9142. "Press \\[org-complete] again to insert example settings"))))
  9143. (t
  9144. (message "Making completion list...")
  9145. (let ((list (sort (all-completions pattern table confirm)
  9146. 'string<)))
  9147. (with-output-to-temp-buffer "*Completions*"
  9148. (condition-case nil
  9149. ;; Protection needed for XEmacs and emacs 21
  9150. (display-completion-list list pattern)
  9151. (error (display-completion-list list)))))
  9152. (message "Making completion list...%s" "done")))))))
  9153. ;;;; TODO, DEADLINE, Comments
  9154. (defun org-toggle-comment ()
  9155. "Change the COMMENT state of an entry."
  9156. (interactive)
  9157. (save-excursion
  9158. (org-back-to-heading)
  9159. (let (case-fold-search)
  9160. (if (looking-at (concat outline-regexp
  9161. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9162. (replace-match "" t t nil 1)
  9163. (if (looking-at outline-regexp)
  9164. (progn
  9165. (goto-char (match-end 0))
  9166. (insert org-comment-string " ")))))))
  9167. (defvar org-last-todo-state-is-todo nil
  9168. "This is non-nil when the last TODO state change led to a TODO state.
  9169. If the last change removed the TODO tag or switched to DONE, then
  9170. this is nil.")
  9171. (defvar org-setting-tags nil) ; dynamically skipped
  9172. (defun org-parse-local-options (string var)
  9173. "Parse STRING for startup setting relevant for variable VAR."
  9174. (let ((rtn (symbol-value var))
  9175. e opts)
  9176. (save-match-data
  9177. (if (or (not string) (not (string-match "\\S-" string)))
  9178. rtn
  9179. (setq opts (delq nil (mapcar (lambda (x)
  9180. (setq e (assoc x org-startup-options))
  9181. (if (eq (nth 1 e) var) e nil))
  9182. (org-split-string string "[ \t]+"))))
  9183. (if (not opts)
  9184. rtn
  9185. (setq rtn nil)
  9186. (while (setq e (pop opts))
  9187. (if (not (nth 3 e))
  9188. (setq rtn (nth 2 e))
  9189. (if (not (listp rtn)) (setq rtn nil))
  9190. (push (nth 2 e) rtn)))
  9191. rtn)))))
  9192. (defvar org-todo-setup-filter-hook nil
  9193. "Hook for functions that pre-filter todo specs.
  9194. Each function takes a todo spec and returns either `nil' or the spec
  9195. transformed into canonical form." )
  9196. (defvar org-todo-get-default-hook nil
  9197. "Hook for functions that get a default item for todo.
  9198. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9199. `nil' or a string to be used for the todo mark." )
  9200. (defvar org-agenda-headline-snapshot-before-repeat)
  9201. (defun org-todo (&optional arg)
  9202. "Change the TODO state of an item.
  9203. The state of an item is given by a keyword at the start of the heading,
  9204. like
  9205. *** TODO Write paper
  9206. *** DONE Call mom
  9207. The different keywords are specified in the variable `org-todo-keywords'.
  9208. By default the available states are \"TODO\" and \"DONE\".
  9209. So for this example: when the item starts with TODO, it is changed to DONE.
  9210. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9211. DONE are present, add TODO at the beginning of the heading.
  9212. With C-u prefix arg, use completion to determine the new state.
  9213. With numeric prefix arg, switch to that state.
  9214. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9215. With a triple C-u prefix, circumvent any state blocking.
  9216. For calling through lisp, arg is also interpreted in the following way:
  9217. 'none -> empty state
  9218. \"\"(empty string) -> switch to empty state
  9219. 'done -> switch to DONE
  9220. 'nextset -> switch to the next set of keywords
  9221. 'previousset -> switch to the previous set of keywords
  9222. \"WAITING\" -> switch to the specified keyword, but only if it
  9223. really is a member of `org-todo-keywords'."
  9224. (interactive "P")
  9225. (if (equal arg '(16)) (setq arg 'nextset))
  9226. (let ((org-blocker-hook org-blocker-hook)
  9227. (case-fold-search nil))
  9228. (when (equal arg '(64))
  9229. (setq arg nil org-blocker-hook nil))
  9230. (when (and org-blocker-hook
  9231. (or org-inhibit-blocking
  9232. (org-entry-get nil "NOBLOCKING")))
  9233. (setq org-blocker-hook nil))
  9234. (save-excursion
  9235. (catch 'exit
  9236. (org-back-to-heading t)
  9237. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9238. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9239. (looking-at " *"))
  9240. (let* ((match-data (match-data))
  9241. (startpos (point-at-bol))
  9242. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9243. (org-log-done org-log-done)
  9244. (org-log-repeat org-log-repeat)
  9245. (org-todo-log-states org-todo-log-states)
  9246. (this (match-string 1))
  9247. (hl-pos (match-beginning 0))
  9248. (head (org-get-todo-sequence-head this))
  9249. (ass (assoc head org-todo-kwd-alist))
  9250. (interpret (nth 1 ass))
  9251. (done-word (nth 3 ass))
  9252. (final-done-word (nth 4 ass))
  9253. (last-state (or this ""))
  9254. (completion-ignore-case t)
  9255. (member (member this org-todo-keywords-1))
  9256. (tail (cdr member))
  9257. (state (cond
  9258. ((and org-todo-key-trigger
  9259. (or (and (equal arg '(4))
  9260. (eq org-use-fast-todo-selection 'prefix))
  9261. (and (not arg) org-use-fast-todo-selection
  9262. (not (eq org-use-fast-todo-selection
  9263. 'prefix)))))
  9264. ;; Use fast selection
  9265. (org-fast-todo-selection))
  9266. ((and (equal arg '(4))
  9267. (or (not org-use-fast-todo-selection)
  9268. (not org-todo-key-trigger)))
  9269. ;; Read a state with completion
  9270. (org-icompleting-read
  9271. "State: " (mapcar (lambda(x) (list x))
  9272. org-todo-keywords-1)
  9273. nil t))
  9274. ((eq arg 'right)
  9275. (if this
  9276. (if tail (car tail) nil)
  9277. (car org-todo-keywords-1)))
  9278. ((eq arg 'left)
  9279. (if (equal member org-todo-keywords-1)
  9280. nil
  9281. (if this
  9282. (nth (- (length org-todo-keywords-1)
  9283. (length tail) 2)
  9284. org-todo-keywords-1)
  9285. (org-last org-todo-keywords-1))))
  9286. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9287. (setq arg nil))) ; hack to fall back to cycling
  9288. (arg
  9289. ;; user or caller requests a specific state
  9290. (cond
  9291. ((equal arg "") nil)
  9292. ((eq arg 'none) nil)
  9293. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9294. ((eq arg 'nextset)
  9295. (or (car (cdr (member head org-todo-heads)))
  9296. (car org-todo-heads)))
  9297. ((eq arg 'previousset)
  9298. (let ((org-todo-heads (reverse org-todo-heads)))
  9299. (or (car (cdr (member head org-todo-heads)))
  9300. (car org-todo-heads))))
  9301. ((car (member arg org-todo-keywords-1)))
  9302. ((stringp arg)
  9303. (error "State `%s' not valid in this file" arg))
  9304. ((nth (1- (prefix-numeric-value arg))
  9305. org-todo-keywords-1))))
  9306. ((null member) (or head (car org-todo-keywords-1)))
  9307. ((equal this final-done-word) nil) ;; -> make empty
  9308. ((null tail) nil) ;; -> first entry
  9309. ((memq interpret '(type priority))
  9310. (if (eq this-command last-command)
  9311. (car tail)
  9312. (if (> (length tail) 0)
  9313. (or done-word (car org-done-keywords))
  9314. nil)))
  9315. (t
  9316. (car tail))))
  9317. (state (or
  9318. (run-hook-with-args-until-success
  9319. 'org-todo-get-default-hook state last-state)
  9320. state))
  9321. (next (if state (concat " " state " ") " "))
  9322. (change-plist (list :type 'todo-state-change :from this :to state
  9323. :position startpos))
  9324. dolog now-done-p)
  9325. (when org-blocker-hook
  9326. (setq org-last-todo-state-is-todo
  9327. (not (member this org-done-keywords)))
  9328. (unless (save-excursion
  9329. (save-match-data
  9330. (run-hook-with-args-until-failure
  9331. 'org-blocker-hook change-plist)))
  9332. (if (interactive-p)
  9333. (error "TODO state change from %s to %s blocked" this state)
  9334. ;; fail silently
  9335. (message "TODO state change from %s to %s blocked" this state)
  9336. (throw 'exit nil))))
  9337. (store-match-data match-data)
  9338. (replace-match next t t)
  9339. (unless (pos-visible-in-window-p hl-pos)
  9340. (message "TODO state changed to %s" (org-trim next)))
  9341. (unless head
  9342. (setq head (org-get-todo-sequence-head state)
  9343. ass (assoc head org-todo-kwd-alist)
  9344. interpret (nth 1 ass)
  9345. done-word (nth 3 ass)
  9346. final-done-word (nth 4 ass)))
  9347. (when (memq arg '(nextset previousset))
  9348. (message "Keyword-Set %d/%d: %s"
  9349. (- (length org-todo-sets) -1
  9350. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9351. (length org-todo-sets)
  9352. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9353. (setq org-last-todo-state-is-todo
  9354. (not (member state org-done-keywords)))
  9355. (setq now-done-p (and (member state org-done-keywords)
  9356. (not (member this org-done-keywords))))
  9357. (and logging (org-local-logging logging))
  9358. (when (and (or org-todo-log-states org-log-done)
  9359. (not (eq org-inhibit-logging t))
  9360. (not (memq arg '(nextset previousset))))
  9361. ;; we need to look at recording a time and note
  9362. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9363. (nth 2 (assoc this org-todo-log-states))))
  9364. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9365. (setq dolog 'time))
  9366. (when (and state
  9367. (member state org-not-done-keywords)
  9368. (not (member this org-not-done-keywords)))
  9369. ;; This is now a todo state and was not one before
  9370. ;; If there was a CLOSED time stamp, get rid of it.
  9371. (org-add-planning-info nil nil 'closed))
  9372. (when (and now-done-p org-log-done)
  9373. ;; It is now done, and it was not done before
  9374. (org-add-planning-info 'closed (org-current-time))
  9375. (if (and (not dolog) (eq 'note org-log-done))
  9376. (org-add-log-setup 'done state this 'findpos 'note)))
  9377. (when (and state dolog)
  9378. ;; This is a non-nil state, and we need to log it
  9379. (org-add-log-setup 'state state this 'findpos dolog)))
  9380. ;; Fixup tag positioning
  9381. (org-todo-trigger-tag-changes state)
  9382. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9383. (when org-provide-todo-statistics
  9384. (org-update-parent-todo-statistics))
  9385. (run-hooks 'org-after-todo-state-change-hook)
  9386. (if (and arg (not (member state org-done-keywords)))
  9387. (setq head (org-get-todo-sequence-head state)))
  9388. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9389. ;; Do we need to trigger a repeat?
  9390. (when now-done-p
  9391. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9392. ;; This is for the agenda, take a snapshot of the headline.
  9393. (save-match-data
  9394. (setq org-agenda-headline-snapshot-before-repeat
  9395. (org-get-heading))))
  9396. (org-auto-repeat-maybe state))
  9397. ;; Fixup cursor location if close to the keyword
  9398. (if (and (outline-on-heading-p)
  9399. (not (bolp))
  9400. (save-excursion (beginning-of-line 1)
  9401. (looking-at org-todo-line-regexp))
  9402. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9403. (progn
  9404. (goto-char (or (match-end 2) (match-end 1)))
  9405. (and (looking-at " ") (just-one-space))))
  9406. (when org-trigger-hook
  9407. (save-excursion
  9408. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9409. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9410. "Block turning an entry into a TODO, using the hierarchy.
  9411. This checks whether the current task should be blocked from state
  9412. changes. Such blocking occurs when:
  9413. 1. The task has children which are not all in a completed state.
  9414. 2. A task has a parent with the property :ORDERED:, and there
  9415. are siblings prior to the current task with incomplete
  9416. status.
  9417. 3. The parent of the task is blocked because it has siblings that should
  9418. be done first, or is child of a block grandparent TODO entry."
  9419. (if (not org-enforce-todo-dependencies)
  9420. t ; if locally turned off don't block
  9421. (catch 'dont-block
  9422. ;; If this is not a todo state change, or if this entry is already DONE,
  9423. ;; do not block
  9424. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9425. (member (plist-get change-plist :from)
  9426. (cons 'done org-done-keywords))
  9427. (member (plist-get change-plist :to)
  9428. (cons 'todo org-not-done-keywords))
  9429. (not (plist-get change-plist :to)))
  9430. (throw 'dont-block t))
  9431. ;; If this task has children, and any are undone, it's blocked
  9432. (save-excursion
  9433. (org-back-to-heading t)
  9434. (let ((this-level (funcall outline-level)))
  9435. (outline-next-heading)
  9436. (let ((child-level (funcall outline-level)))
  9437. (while (and (not (eobp))
  9438. (> child-level this-level))
  9439. ;; this todo has children, check whether they are all
  9440. ;; completed
  9441. (if (and (not (org-entry-is-done-p))
  9442. (org-entry-is-todo-p))
  9443. (throw 'dont-block nil))
  9444. (outline-next-heading)
  9445. (setq child-level (funcall outline-level))))))
  9446. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9447. ;; any previous siblings are undone, it's blocked
  9448. (save-excursion
  9449. (org-back-to-heading t)
  9450. (let* ((pos (point))
  9451. (parent-pos (and (org-up-heading-safe) (point))))
  9452. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9453. (when (and (org-entry-get (point) "ORDERED")
  9454. (forward-line 1)
  9455. (re-search-forward org-not-done-heading-regexp pos t))
  9456. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9457. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9458. (while t
  9459. (goto-char parent-pos)
  9460. (if (not (looking-at org-not-done-heading-regexp))
  9461. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9462. (setq pos (point))
  9463. (setq parent-pos (and (org-up-heading-safe) (point)))
  9464. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9465. (when (and (org-entry-get (point) "ORDERED")
  9466. (forward-line 1)
  9467. (re-search-forward org-not-done-heading-regexp pos t))
  9468. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9469. (defcustom org-track-ordered-property-with-tag nil
  9470. "Should the ORDERED property also be shown as a tag?
  9471. The ORDERED property decides if an entry should require subtasks to be
  9472. completed in sequence. Since a property is not very visible, setting
  9473. this option means that toggling the ORDERED property with the command
  9474. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9475. not relevant for the behavior, but it makes things more visible.
  9476. Note that toggling the tag with tags commands will not change the property
  9477. and therefore not influence behavior!
  9478. This can be t, meaning the tag ORDERED should be used, It can also be a
  9479. string to select a different tag for this task."
  9480. :group 'org-todo
  9481. :type '(choice
  9482. (const :tag "No tracking" nil)
  9483. (const :tag "Track with ORDERED tag" t)
  9484. (string :tag "Use other tag")))
  9485. (defun org-toggle-ordered-property ()
  9486. "Toggle the ORDERED property of the current entry.
  9487. For better visibility, you can track the value of this property with a tag.
  9488. See variable `org-track-ordered-property-with-tag'."
  9489. (interactive)
  9490. (let* ((t1 org-track-ordered-property-with-tag)
  9491. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9492. (save-excursion
  9493. (org-back-to-heading)
  9494. (if (org-entry-get nil "ORDERED")
  9495. (progn
  9496. (org-delete-property "ORDERED")
  9497. (and tag (org-toggle-tag tag 'off))
  9498. (message "Subtasks can be completed in arbitrary order"))
  9499. (org-entry-put nil "ORDERED" "t")
  9500. (and tag (org-toggle-tag tag 'on))
  9501. (message "Subtasks must be completed in sequence")))))
  9502. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9503. (defun org-block-todo-from-checkboxes (change-plist)
  9504. "Block turning an entry into a TODO, using checkboxes.
  9505. This checks whether the current task should be blocked from state
  9506. changes because there are unchecked boxes in this entry."
  9507. (if (not org-enforce-todo-checkbox-dependencies)
  9508. t ; if locally turned off don't block
  9509. (catch 'dont-block
  9510. ;; If this is not a todo state change, or if this entry is already DONE,
  9511. ;; do not block
  9512. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9513. (member (plist-get change-plist :from)
  9514. (cons 'done org-done-keywords))
  9515. (member (plist-get change-plist :to)
  9516. (cons 'todo org-not-done-keywords))
  9517. (not (plist-get change-plist :to)))
  9518. (throw 'dont-block t))
  9519. ;; If this task has checkboxes that are not checked, it's blocked
  9520. (save-excursion
  9521. (org-back-to-heading t)
  9522. (let ((beg (point)) end)
  9523. (outline-next-heading)
  9524. (setq end (point))
  9525. (goto-char beg)
  9526. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9527. end t)
  9528. (progn
  9529. (if (boundp 'org-blocked-by-checkboxes)
  9530. (setq org-blocked-by-checkboxes t))
  9531. (throw 'dont-block nil)))))
  9532. t))) ; do not block
  9533. (defun org-entry-blocked-p ()
  9534. "Is the current entry blocked?"
  9535. (if (org-entry-get nil "NOBLOCKING")
  9536. nil ;; Never block this entry
  9537. (not
  9538. (run-hook-with-args-until-failure
  9539. 'org-blocker-hook
  9540. (list :type 'todo-state-change
  9541. :position (point)
  9542. :from 'todo
  9543. :to 'done)))))
  9544. (defun org-update-statistics-cookies (all)
  9545. "Update the statistics cookie, either from TODO or from checkboxes.
  9546. This should be called with the cursor in a line with a statistics cookie."
  9547. (interactive "P")
  9548. (if all
  9549. (progn
  9550. (org-update-checkbox-count 'all)
  9551. (org-map-entries 'org-update-parent-todo-statistics))
  9552. (if (not (org-on-heading-p))
  9553. (org-update-checkbox-count)
  9554. (let ((pos (move-marker (make-marker) (point)))
  9555. end l1 l2)
  9556. (ignore-errors (org-back-to-heading t))
  9557. (if (not (org-on-heading-p))
  9558. (org-update-checkbox-count)
  9559. (setq l1 (org-outline-level))
  9560. (setq end (save-excursion
  9561. (outline-next-heading)
  9562. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9563. (point)))
  9564. (if (and (save-excursion
  9565. (re-search-forward
  9566. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9567. (not (save-excursion (re-search-forward
  9568. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9569. (org-update-checkbox-count)
  9570. (if (and l2 (> l2 l1))
  9571. (progn
  9572. (goto-char end)
  9573. (org-update-parent-todo-statistics))
  9574. (goto-char pos)
  9575. (beginning-of-line 1)
  9576. (while (re-search-forward
  9577. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9578. (point-at-eol) t)
  9579. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9580. (goto-char pos)
  9581. (move-marker pos nil)))))
  9582. (defvar org-entry-property-inherited-from) ;; defined below
  9583. (defun org-update-parent-todo-statistics ()
  9584. "Update any statistics cookie in the parent of the current headline.
  9585. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9586. the entire subtree and this will travel up the hierarchy and update
  9587. statistics everywhere."
  9588. (interactive)
  9589. (let* ((lim 0) prop
  9590. (recursive (or (not org-hierarchical-todo-statistics)
  9591. (string-match
  9592. "\\<recursive\\>"
  9593. (or (setq prop (org-entry-get
  9594. nil "COOKIE_DATA" 'inherit)) ""))))
  9595. (lim (or (and prop (marker-position
  9596. org-entry-property-inherited-from))
  9597. lim))
  9598. (first t)
  9599. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9600. level ltoggle l1 new ndel
  9601. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9602. (catch 'exit
  9603. (save-excursion
  9604. (beginning-of-line 1)
  9605. (if (org-at-heading-p)
  9606. (setq ltoggle (funcall outline-level))
  9607. (error "This should not happen"))
  9608. (while (and (setq level (org-up-heading-safe))
  9609. (or recursive first)
  9610. (>= (point) lim))
  9611. (setq first nil cookie-present nil)
  9612. (unless (and level
  9613. (not (string-match
  9614. "\\<checkbox\\>"
  9615. (downcase
  9616. (or (org-entry-get
  9617. nil "COOKIE_DATA")
  9618. "")))))
  9619. (throw 'exit nil))
  9620. (while (re-search-forward box-re (point-at-eol) t)
  9621. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9622. (setq is-percent (match-end 2))
  9623. (save-match-data
  9624. (unless (outline-next-heading) (throw 'exit nil))
  9625. (while (and (looking-at org-complex-heading-regexp)
  9626. (> (setq l1 (length (match-string 1))) level))
  9627. (setq kwd (and (or recursive (= l1 ltoggle))
  9628. (match-string 2)))
  9629. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9630. (and (listp org-provide-todo-statistics)
  9631. (or (member kwd org-provide-todo-statistics)
  9632. (member kwd org-done-keywords))))
  9633. (setq cnt-all (1+ cnt-all))
  9634. (if (eq org-provide-todo-statistics t)
  9635. (and kwd (setq cnt-all (1+ cnt-all)))))
  9636. (and (member kwd org-done-keywords)
  9637. (setq cnt-done (1+ cnt-done)))
  9638. (outline-next-heading)))
  9639. (setq new
  9640. (if is-percent
  9641. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9642. (format "[%d/%d]" cnt-done cnt-all))
  9643. ndel (- (match-end 0) (match-beginning 0)))
  9644. (goto-char (match-beginning 0))
  9645. (insert new)
  9646. (delete-region (point) (+ (point) ndel)))
  9647. (when cookie-present
  9648. (run-hook-with-args 'org-after-todo-statistics-hook
  9649. cnt-done (- cnt-all cnt-done))))))
  9650. (run-hooks 'org-todo-statistics-hook)))
  9651. (defvar org-after-todo-statistics-hook nil
  9652. "Hook that is called after a TODO statistics cookie has been updated.
  9653. Each function is called with two arguments: the number of not-done entries
  9654. and the number of done entries.
  9655. For example, the following function, when added to this hook, will switch
  9656. an entry to DONE when all children are done, and back to TODO when new
  9657. entries are set to a TODO status. Note that this hook is only called
  9658. when there is a statistics cookie in the headline!
  9659. (defun org-summary-todo (n-done n-not-done)
  9660. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9661. (let (org-log-done org-log-states) ; turn off logging
  9662. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9663. ")
  9664. (defvar org-todo-statistics-hook nil
  9665. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9666. This hook runs even if there is no statistics cookie present, in which case
  9667. `org-after-todo-statistics-hook' would not run.")
  9668. (defun org-todo-trigger-tag-changes (state)
  9669. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9670. (let ((l org-todo-state-tags-triggers)
  9671. changes)
  9672. (when (or (not state) (equal state ""))
  9673. (setq changes (append changes (cdr (assoc "" l)))))
  9674. (when (and (stringp state) (> (length state) 0))
  9675. (setq changes (append changes (cdr (assoc state l)))))
  9676. (when (member state org-not-done-keywords)
  9677. (setq changes (append changes (cdr (assoc 'todo l)))))
  9678. (when (member state org-done-keywords)
  9679. (setq changes (append changes (cdr (assoc 'done l)))))
  9680. (dolist (c changes)
  9681. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9682. (defun org-local-logging (value)
  9683. "Get logging settings from a property VALUE."
  9684. (let* (words w a)
  9685. ;; directly set the variables, they are already local.
  9686. (setq org-log-done nil
  9687. org-log-repeat nil
  9688. org-todo-log-states nil)
  9689. (setq words (org-split-string value))
  9690. (while (setq w (pop words))
  9691. (cond
  9692. ((setq a (assoc w org-startup-options))
  9693. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9694. (set (nth 1 a) (nth 2 a))))
  9695. ((setq a (org-extract-log-state-settings w))
  9696. (and (member (car a) org-todo-keywords-1)
  9697. (push a org-todo-log-states)))))))
  9698. (defun org-get-todo-sequence-head (kwd)
  9699. "Return the head of the TODO sequence to which KWD belongs.
  9700. If KWD is not set, check if there is a text property remembering the
  9701. right sequence."
  9702. (let (p)
  9703. (cond
  9704. ((not kwd)
  9705. (or (get-text-property (point-at-bol) 'org-todo-head)
  9706. (progn
  9707. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9708. nil (point-at-eol)))
  9709. (get-text-property p 'org-todo-head))))
  9710. ((not (member kwd org-todo-keywords-1))
  9711. (car org-todo-keywords-1))
  9712. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9713. (defun org-fast-todo-selection ()
  9714. "Fast TODO keyword selection with single keys.
  9715. Returns the new TODO keyword, or nil if no state change should occur."
  9716. (let* ((fulltable org-todo-key-alist)
  9717. (done-keywords org-done-keywords) ;; needed for the faces.
  9718. (maxlen (apply 'max (mapcar
  9719. (lambda (x)
  9720. (if (stringp (car x)) (string-width (car x)) 0))
  9721. fulltable)))
  9722. (expert nil)
  9723. (fwidth (+ maxlen 3 1 3))
  9724. (ncol (/ (- (window-width) 4) fwidth))
  9725. tg cnt e c tbl
  9726. groups ingroup)
  9727. (save-excursion
  9728. (save-window-excursion
  9729. (if expert
  9730. (set-buffer (get-buffer-create " *Org todo*"))
  9731. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9732. (erase-buffer)
  9733. (org-set-local 'org-done-keywords done-keywords)
  9734. (setq tbl fulltable cnt 0)
  9735. (while (setq e (pop tbl))
  9736. (cond
  9737. ((equal e '(:startgroup))
  9738. (push '() groups) (setq ingroup t)
  9739. (when (not (= cnt 0))
  9740. (setq cnt 0)
  9741. (insert "\n"))
  9742. (insert "{ "))
  9743. ((equal e '(:endgroup))
  9744. (setq ingroup nil cnt 0)
  9745. (insert "}\n"))
  9746. ((equal e '(:newline))
  9747. (when (not (= cnt 0))
  9748. (setq cnt 0)
  9749. (insert "\n")
  9750. (setq e (car tbl))
  9751. (while (equal (car tbl) '(:newline))
  9752. (insert "\n")
  9753. (setq tbl (cdr tbl)))))
  9754. (t
  9755. (setq tg (car e) c (cdr e))
  9756. (if ingroup (push tg (car groups)))
  9757. (setq tg (org-add-props tg nil 'face
  9758. (org-get-todo-face tg)))
  9759. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9760. (insert "[" c "] " tg (make-string
  9761. (- fwidth 4 (length tg)) ?\ ))
  9762. (when (= (setq cnt (1+ cnt)) ncol)
  9763. (insert "\n")
  9764. (if ingroup (insert " "))
  9765. (setq cnt 0)))))
  9766. (insert "\n")
  9767. (goto-char (point-min))
  9768. (if (not expert) (org-fit-window-to-buffer))
  9769. (message "[a-z..]:Set [SPC]:clear")
  9770. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9771. (cond
  9772. ((or (= c ?\C-g)
  9773. (and (= c ?q) (not (rassoc c fulltable))))
  9774. (setq quit-flag t))
  9775. ((= c ?\ ) nil)
  9776. ((setq e (rassoc c fulltable) tg (car e))
  9777. tg)
  9778. (t (setq quit-flag t)))))))
  9779. (defun org-entry-is-todo-p ()
  9780. (member (org-get-todo-state) org-not-done-keywords))
  9781. (defun org-entry-is-done-p ()
  9782. (member (org-get-todo-state) org-done-keywords))
  9783. (defun org-get-todo-state ()
  9784. (save-excursion
  9785. (org-back-to-heading t)
  9786. (and (looking-at org-todo-line-regexp)
  9787. (match-end 2)
  9788. (match-string 2))))
  9789. (defun org-at-date-range-p (&optional inactive-ok)
  9790. "Is the cursor inside a date range?"
  9791. (interactive)
  9792. (save-excursion
  9793. (catch 'exit
  9794. (let ((pos (point)))
  9795. (skip-chars-backward "^[<\r\n")
  9796. (skip-chars-backward "<[")
  9797. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9798. (>= (match-end 0) pos)
  9799. (throw 'exit t))
  9800. (skip-chars-backward "^<[\r\n")
  9801. (skip-chars-backward "<[")
  9802. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9803. (>= (match-end 0) pos)
  9804. (throw 'exit t)))
  9805. nil)))
  9806. (defun org-get-repeat (&optional tagline)
  9807. "Check if there is a deadline/schedule with repeater in this entry."
  9808. (save-match-data
  9809. (save-excursion
  9810. (org-back-to-heading t)
  9811. (and (re-search-forward (if tagline
  9812. (concat tagline "\\s-*" org-repeat-re)
  9813. org-repeat-re)
  9814. (org-entry-end-position) t)
  9815. (match-string-no-properties 1)))))
  9816. (defvar org-last-changed-timestamp)
  9817. (defvar org-last-inserted-timestamp)
  9818. (defvar org-log-post-message)
  9819. (defvar org-log-note-purpose)
  9820. (defvar org-log-note-how)
  9821. (defvar org-log-note-extra)
  9822. (defun org-auto-repeat-maybe (done-word)
  9823. "Check if the current headline contains a repeated deadline/schedule.
  9824. If yes, set TODO state back to what it was and change the base date
  9825. of repeating deadline/scheduled time stamps to new date.
  9826. This function is run automatically after each state change to a DONE state."
  9827. ;; last-state is dynamically scoped into this function
  9828. (let* ((repeat (org-get-repeat))
  9829. (aa (assoc last-state org-todo-kwd-alist))
  9830. (interpret (nth 1 aa))
  9831. (head (nth 2 aa))
  9832. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9833. (msg "Entry repeats: ")
  9834. (org-log-done nil)
  9835. (org-todo-log-states nil)
  9836. (nshiftmax 10) (nshift 0)
  9837. re type n what ts time to-state)
  9838. (when repeat
  9839. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9840. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  9841. org-todo-repeat-to-state))
  9842. (unless (and to-state (member to-state org-todo-keywords-1))
  9843. (setq to-state (if (eq interpret 'type) last-state head)))
  9844. (org-todo to-state)
  9845. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9846. (org-time-stamp-format t t)))
  9847. (when org-log-repeat
  9848. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9849. (memq 'org-add-log-note post-command-hook))
  9850. ;; OK, we are already setup for some record
  9851. (if (eq org-log-repeat 'note)
  9852. ;; make sure we take a note, not only a time stamp
  9853. (setq org-log-note-how 'note))
  9854. ;; Set up for taking a record
  9855. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9856. last-state
  9857. 'findpos org-log-repeat)))
  9858. (org-back-to-heading t)
  9859. (org-add-planning-info nil nil 'closed)
  9860. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9861. org-deadline-time-regexp "\\)\\|\\("
  9862. org-ts-regexp "\\)"))
  9863. (while (re-search-forward
  9864. re (save-excursion (outline-next-heading) (point)) t)
  9865. (setq type (if (match-end 1) org-scheduled-string
  9866. (if (match-end 3) org-deadline-string "Plain:"))
  9867. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9868. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9869. (setq n (string-to-number (match-string 2 ts))
  9870. what (match-string 3 ts))
  9871. (if (equal what "w") (setq n (* n 7) what "d"))
  9872. ;; Preparation, see if we need to modify the start date for the change
  9873. (when (match-end 1)
  9874. (setq time (save-match-data (org-time-string-to-time ts)))
  9875. (cond
  9876. ((equal (match-string 1 ts) ".")
  9877. ;; Shift starting date to today
  9878. (org-timestamp-change
  9879. (- (time-to-days (current-time)) (time-to-days time))
  9880. 'day))
  9881. ((equal (match-string 1 ts) "+")
  9882. (while (or (= nshift 0)
  9883. (<= (time-to-days time) (time-to-days (current-time))))
  9884. (when (= (incf nshift) nshiftmax)
  9885. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9886. (error "Abort")))
  9887. (org-timestamp-change n (cdr (assoc what whata)))
  9888. (org-at-timestamp-p t)
  9889. (setq ts (match-string 1))
  9890. (setq time (save-match-data (org-time-string-to-time ts))))
  9891. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9892. ;; rematch, so that we have everything in place for the real shift
  9893. (org-at-timestamp-p t)
  9894. (setq ts (match-string 1))
  9895. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9896. (org-timestamp-change n (cdr (assoc what whata)))
  9897. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9898. (setq org-log-post-message msg)
  9899. (message "%s" msg))))
  9900. (defun org-show-todo-tree (arg)
  9901. "Make a compact tree which shows all headlines marked with TODO.
  9902. The tree will show the lines where the regexp matches, and all higher
  9903. headlines above the match.
  9904. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9905. With a numeric prefix N, construct a sparse tree for the Nth element
  9906. of `org-todo-keywords-1'."
  9907. (interactive "P")
  9908. (let ((case-fold-search nil)
  9909. (kwd-re
  9910. (cond ((null arg) org-not-done-regexp)
  9911. ((equal arg '(4))
  9912. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9913. (mapcar 'list org-todo-keywords-1))))
  9914. (concat "\\("
  9915. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9916. "\\)\\>")))
  9917. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9918. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9919. org-todo-keywords-1)))
  9920. (t (error "Invalid prefix argument: %s" arg)))))
  9921. (message "%d TODO entries found"
  9922. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9923. (defun org-deadline (&optional remove time)
  9924. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9925. With argument REMOVE, remove any deadline from the item.
  9926. When TIME is set, it should be an internal time specification, and the
  9927. scheduling will use the corresponding date."
  9928. (interactive "P")
  9929. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9930. (repeater (and old-date
  9931. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9932. (match-string 1 old-date))))
  9933. (if remove
  9934. (progn
  9935. (when (and old-date org-log-redeadline)
  9936. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9937. org-log-redeadline))
  9938. (org-remove-timestamp-with-keyword org-deadline-string)
  9939. (message "Item no longer has a deadline."))
  9940. (org-add-planning-info 'deadline time 'closed)
  9941. (when (and old-date org-log-redeadline
  9942. (not (equal old-date
  9943. (substring org-last-inserted-timestamp 1 -1))))
  9944. (org-add-log-setup 'redeadline nil old-date 'findpos
  9945. org-log-redeadline))
  9946. (when repeater
  9947. (save-excursion
  9948. (org-back-to-heading t)
  9949. (when (re-search-forward (concat org-deadline-string " "
  9950. org-last-inserted-timestamp)
  9951. (save-excursion
  9952. (outline-next-heading) (point)) t)
  9953. (goto-char (1- (match-end 0)))
  9954. (insert " " repeater)
  9955. (setq org-last-inserted-timestamp
  9956. (concat (substring org-last-inserted-timestamp 0 -1)
  9957. " " repeater
  9958. (substring org-last-inserted-timestamp -1))))))
  9959. (message "Deadline on %s" org-last-inserted-timestamp))))
  9960. (defun org-schedule (&optional remove time)
  9961. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9962. With argument REMOVE, remove any scheduling date from the item.
  9963. When TIME is set, it should be an internal time specification, and the
  9964. scheduling will use the corresponding date."
  9965. (interactive "P")
  9966. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9967. (repeater (and old-date
  9968. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9969. (match-string 1 old-date))))
  9970. (if remove
  9971. (progn
  9972. (when (and old-date org-log-reschedule)
  9973. (org-add-log-setup 'delschedule nil old-date 'findpos
  9974. org-log-reschedule))
  9975. (org-remove-timestamp-with-keyword org-scheduled-string)
  9976. (message "Item is no longer scheduled."))
  9977. (org-add-planning-info 'scheduled time 'closed)
  9978. (when (and old-date org-log-reschedule
  9979. (not (equal old-date
  9980. (substring org-last-inserted-timestamp 1 -1))))
  9981. (org-add-log-setup 'reschedule nil old-date 'findpos
  9982. org-log-reschedule))
  9983. (when repeater
  9984. (save-excursion
  9985. (org-back-to-heading t)
  9986. (when (re-search-forward (concat org-scheduled-string " "
  9987. org-last-inserted-timestamp)
  9988. (save-excursion
  9989. (outline-next-heading) (point)) t)
  9990. (goto-char (1- (match-end 0)))
  9991. (insert " " repeater)
  9992. (setq org-last-inserted-timestamp
  9993. (concat (substring org-last-inserted-timestamp 0 -1)
  9994. " " repeater
  9995. (substring org-last-inserted-timestamp -1))))))
  9996. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9997. (defun org-get-scheduled-time (pom &optional inherit)
  9998. "Get the scheduled time as a time tuple, of a format suitable
  9999. for calling org-schedule with, or if there is no scheduling,
  10000. returns nil."
  10001. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10002. (when time
  10003. (apply 'encode-time (org-parse-time-string time)))))
  10004. (defun org-get-deadline-time (pom &optional inherit)
  10005. "Get the deadine as a time tuple, of a format suitable for
  10006. calling org-deadline with, or if there is no scheduling, returns
  10007. nil."
  10008. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10009. (when time
  10010. (apply 'encode-time (org-parse-time-string time)))))
  10011. (defun org-remove-timestamp-with-keyword (keyword)
  10012. "Remove all time stamps with KEYWORD in the current entry."
  10013. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10014. beg)
  10015. (save-excursion
  10016. (org-back-to-heading t)
  10017. (setq beg (point))
  10018. (outline-next-heading)
  10019. (while (re-search-backward re beg t)
  10020. (replace-match "")
  10021. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10022. (equal (char-before) ?\ ))
  10023. (backward-delete-char 1)
  10024. (if (string-match "^[ \t]*$" (buffer-substring
  10025. (point-at-bol) (point-at-eol)))
  10026. (delete-region (point-at-bol)
  10027. (min (point-max) (1+ (point-at-eol))))))))))
  10028. (defun org-add-planning-info (what &optional time &rest remove)
  10029. "Insert new timestamp with keyword in the line directly after the headline.
  10030. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10031. If non is given, the user is prompted for a date.
  10032. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10033. be removed."
  10034. (interactive)
  10035. (let (org-time-was-given org-end-time-was-given ts
  10036. end default-time default-input)
  10037. (catch 'exit
  10038. (when (and (not time) (memq what '(scheduled deadline)))
  10039. ;; Try to get a default date/time from existing timestamp
  10040. (save-excursion
  10041. (org-back-to-heading t)
  10042. (setq end (save-excursion (outline-next-heading) (point)))
  10043. (when (re-search-forward (if (eq what 'scheduled)
  10044. org-scheduled-time-regexp
  10045. org-deadline-time-regexp)
  10046. end t)
  10047. (setq ts (match-string 1)
  10048. default-time
  10049. (apply 'encode-time (org-parse-time-string ts))
  10050. default-input (and ts (org-get-compact-tod ts))))))
  10051. (when what
  10052. ;; If necessary, get the time from the user
  10053. (setq time (or time (org-read-date nil 'to-time nil nil
  10054. default-time default-input))))
  10055. (when (and org-insert-labeled-timestamps-at-point
  10056. (member what '(scheduled deadline)))
  10057. (insert
  10058. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10059. (org-insert-time-stamp time org-time-was-given
  10060. nil nil nil (list org-end-time-was-given))
  10061. (setq what nil))
  10062. (save-excursion
  10063. (save-restriction
  10064. (let (col list elt ts buffer-invisibility-spec)
  10065. (org-back-to-heading t)
  10066. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10067. (goto-char (match-end 1))
  10068. (setq col (current-column))
  10069. (goto-char (match-end 0))
  10070. (if (eobp) (insert "\n") (forward-char 1))
  10071. (when (and (not what)
  10072. (not (looking-at
  10073. (concat "[ \t]*"
  10074. org-keyword-time-not-clock-regexp))))
  10075. ;; Nothing to add, nothing to remove...... :-)
  10076. (throw 'exit nil))
  10077. (if (and (not (looking-at outline-regexp))
  10078. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10079. "[^\r\n]*"))
  10080. (not (equal (match-string 1) org-clock-string)))
  10081. (narrow-to-region (match-beginning 0) (match-end 0))
  10082. (insert-before-markers "\n")
  10083. (backward-char 1)
  10084. (narrow-to-region (point) (point))
  10085. (and org-adapt-indentation (org-indent-to-column col)))
  10086. ;; Check if we have to remove something.
  10087. (setq list (cons what remove))
  10088. (while list
  10089. (setq elt (pop list))
  10090. (goto-char (point-min))
  10091. (when (or (and (eq elt 'scheduled)
  10092. (re-search-forward org-scheduled-time-regexp nil t))
  10093. (and (eq elt 'deadline)
  10094. (re-search-forward org-deadline-time-regexp nil t))
  10095. (and (eq elt 'closed)
  10096. (re-search-forward org-closed-time-regexp nil t)))
  10097. (replace-match "")
  10098. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10099. (skip-chars-backward " ")
  10100. (if (looking-at " +") (replace-match ""))))
  10101. (goto-char (point-max))
  10102. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10103. (when what
  10104. (insert
  10105. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10106. (cond ((eq what 'scheduled) org-scheduled-string)
  10107. ((eq what 'deadline) org-deadline-string)
  10108. ((eq what 'closed) org-closed-string))
  10109. " ")
  10110. (setq ts (org-insert-time-stamp
  10111. time
  10112. (or org-time-was-given
  10113. (and (eq what 'closed) org-log-done-with-time))
  10114. (eq what 'closed)
  10115. nil nil (list org-end-time-was-given)))
  10116. (end-of-line 1))
  10117. (goto-char (point-min))
  10118. (widen)
  10119. (if (and (looking-at "[ \t]+\n")
  10120. (equal (char-before) ?\n))
  10121. (delete-region (1- (point)) (point-at-eol)))
  10122. ts))))))
  10123. (defvar org-log-note-marker (make-marker))
  10124. (defvar org-log-note-purpose nil)
  10125. (defvar org-log-note-state nil)
  10126. (defvar org-log-note-previous-state nil)
  10127. (defvar org-log-note-how nil)
  10128. (defvar org-log-note-extra nil)
  10129. (defvar org-log-note-window-configuration nil)
  10130. (defvar org-log-note-return-to (make-marker))
  10131. (defvar org-log-post-message nil
  10132. "Message to be displayed after a log note has been stored.
  10133. The auto-repeater uses this.")
  10134. (defun org-add-note ()
  10135. "Add a note to the current entry.
  10136. This is done in the same way as adding a state change note."
  10137. (interactive)
  10138. (org-add-log-setup 'note nil nil 'findpos nil))
  10139. (defvar org-property-end-re)
  10140. (defun org-add-log-setup (&optional purpose state prev-state
  10141. findpos how &optional extra)
  10142. "Set up the post command hook to take a note.
  10143. If this is about to TODO state change, the new state is expected in STATE.
  10144. When FINDPOS is non-nil, find the correct position for the note in
  10145. the current entry. If not, assume that it can be inserted at point.
  10146. HOW is an indicator what kind of note should be created.
  10147. EXTRA is additional text that will be inserted into the notes buffer."
  10148. (let* ((org-log-into-drawer (org-log-into-drawer))
  10149. (drawer (cond ((stringp org-log-into-drawer)
  10150. org-log-into-drawer)
  10151. (org-log-into-drawer "LOGBOOK")
  10152. (t nil))))
  10153. (save-restriction
  10154. (save-excursion
  10155. (when findpos
  10156. (org-back-to-heading t)
  10157. (narrow-to-region (point) (save-excursion
  10158. (outline-next-heading) (point)))
  10159. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10160. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10161. "[^\r\n]*\\)?"))
  10162. (goto-char (match-end 0))
  10163. (cond
  10164. (drawer
  10165. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10166. nil t)
  10167. (progn
  10168. (goto-char (match-end 0))
  10169. (or org-log-states-order-reversed
  10170. (and (re-search-forward org-property-end-re nil t)
  10171. (goto-char (1- (match-beginning 0))))))
  10172. (insert "\n:" drawer ":\n:END:")
  10173. (beginning-of-line 0)
  10174. (org-indent-line-function)
  10175. (beginning-of-line 2)
  10176. (org-indent-line-function)
  10177. (end-of-line 0)))
  10178. ((and org-log-state-notes-insert-after-drawers
  10179. (save-excursion
  10180. (forward-line) (looking-at org-drawer-regexp)))
  10181. (forward-line)
  10182. (while (looking-at org-drawer-regexp)
  10183. (goto-char (match-end 0))
  10184. (re-search-forward org-property-end-re (point-max) t)
  10185. (forward-line))
  10186. (forward-line -1)))
  10187. (unless org-log-states-order-reversed
  10188. (and (= (char-after) ?\n) (forward-char 1))
  10189. (org-skip-over-state-notes)
  10190. (skip-chars-backward " \t\n\r")))
  10191. (move-marker org-log-note-marker (point))
  10192. (setq org-log-note-purpose purpose
  10193. org-log-note-state state
  10194. org-log-note-previous-state prev-state
  10195. org-log-note-how how
  10196. org-log-note-extra extra)
  10197. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10198. (defun org-skip-over-state-notes ()
  10199. "Skip past the list of State notes in an entry."
  10200. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10201. (while (looking-at "[ \t]*- State")
  10202. (condition-case nil
  10203. (org-next-item)
  10204. (error (org-end-of-item)))))
  10205. (defun org-add-log-note (&optional purpose)
  10206. "Pop up a window for taking a note, and add this note later at point."
  10207. (remove-hook 'post-command-hook 'org-add-log-note)
  10208. (setq org-log-note-window-configuration (current-window-configuration))
  10209. (delete-other-windows)
  10210. (move-marker org-log-note-return-to (point))
  10211. (switch-to-buffer (marker-buffer org-log-note-marker))
  10212. (goto-char org-log-note-marker)
  10213. (org-switch-to-buffer-other-window "*Org Note*")
  10214. (erase-buffer)
  10215. (if (memq org-log-note-how '(time state))
  10216. (let (current-prefix-arg) (org-store-log-note))
  10217. (let ((org-inhibit-startup t)) (org-mode))
  10218. (insert (format "# Insert note for %s.
  10219. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10220. (cond
  10221. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10222. ((eq org-log-note-purpose 'done) "closed todo item")
  10223. ((eq org-log-note-purpose 'state)
  10224. (format "state change from \"%s\" to \"%s\""
  10225. (or org-log-note-previous-state "")
  10226. (or org-log-note-state "")))
  10227. ((eq org-log-note-purpose 'reschedule)
  10228. "rescheduling")
  10229. ((eq org-log-note-purpose 'delschedule)
  10230. "no longer scheduled")
  10231. ((eq org-log-note-purpose 'redeadline)
  10232. "changing deadline")
  10233. ((eq org-log-note-purpose 'deldeadline)
  10234. "removing deadline")
  10235. ((eq org-log-note-purpose 'refile)
  10236. "refiling")
  10237. ((eq org-log-note-purpose 'note)
  10238. "this entry")
  10239. (t (error "This should not happen")))))
  10240. (if org-log-note-extra (insert org-log-note-extra))
  10241. (org-set-local 'org-finish-function 'org-store-log-note)))
  10242. (defvar org-note-abort nil) ; dynamically scoped
  10243. (defun org-store-log-note ()
  10244. "Finish taking a log note, and insert it to where it belongs."
  10245. (let ((txt (buffer-string))
  10246. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10247. lines ind)
  10248. (kill-buffer (current-buffer))
  10249. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10250. (setq txt (replace-match "" t t txt)))
  10251. (if (string-match "\\s-+\\'" txt)
  10252. (setq txt (replace-match "" t t txt)))
  10253. (setq lines (org-split-string txt "\n"))
  10254. (when (and note (string-match "\\S-" note))
  10255. (setq note
  10256. (org-replace-escapes
  10257. note
  10258. (list (cons "%u" (user-login-name))
  10259. (cons "%U" user-full-name)
  10260. (cons "%t" (format-time-string
  10261. (org-time-stamp-format 'long 'inactive)
  10262. (current-time)))
  10263. (cons "%s" (if org-log-note-state
  10264. (concat "\"" org-log-note-state "\"")
  10265. ""))
  10266. (cons "%S" (if org-log-note-previous-state
  10267. (concat "\"" org-log-note-previous-state "\"")
  10268. "\"\"")))))
  10269. (if lines (setq note (concat note " \\\\")))
  10270. (push note lines))
  10271. (when (or current-prefix-arg org-note-abort)
  10272. (when org-log-into-drawer
  10273. (org-remove-empty-drawer-at
  10274. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10275. org-log-note-marker))
  10276. (setq lines nil))
  10277. (when lines
  10278. (with-current-buffer (marker-buffer org-log-note-marker)
  10279. (save-excursion
  10280. (goto-char org-log-note-marker)
  10281. (move-marker org-log-note-marker nil)
  10282. (end-of-line 1)
  10283. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10284. (insert "- " (pop lines))
  10285. (org-indent-line-function)
  10286. (beginning-of-line 1)
  10287. (looking-at "[ \t]*")
  10288. (setq ind (concat (match-string 0) " "))
  10289. (end-of-line 1)
  10290. (while lines (insert "\n" ind (pop lines)))
  10291. (message "Note stored")
  10292. (org-back-to-heading t)
  10293. (org-cycle-hide-drawers 'children)))))
  10294. (set-window-configuration org-log-note-window-configuration)
  10295. (with-current-buffer (marker-buffer org-log-note-return-to)
  10296. (goto-char org-log-note-return-to))
  10297. (move-marker org-log-note-return-to nil)
  10298. (and org-log-post-message (message "%s" org-log-post-message)))
  10299. (defun org-remove-empty-drawer-at (drawer pos)
  10300. "Remove an empty drawer DRAWER at position POS.
  10301. POS may also be a marker."
  10302. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10303. (save-excursion
  10304. (save-restriction
  10305. (widen)
  10306. (goto-char pos)
  10307. (if (org-in-regexp
  10308. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10309. (replace-match ""))))))
  10310. (defun org-sparse-tree (&optional arg)
  10311. "Create a sparse tree, prompt for the details.
  10312. This command can create sparse trees. You first need to select the type
  10313. of match used to create the tree:
  10314. t Show entries with a specific TODO keyword.
  10315. m Show entries selected by a tags/property match.
  10316. p Enter a property name and its value (both with completion on existing
  10317. names/values) and show entries with that property.
  10318. / Show entries matching a regular expression (`r' can be used as well)
  10319. d Show deadlines due within `org-deadline-warning-days'.
  10320. b Show deadlines and scheduled items before a date.
  10321. a Show deadlines and scheduled items after a date."
  10322. (interactive "P")
  10323. (let (ans kwd value)
  10324. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10325. (setq ans (read-char-exclusive))
  10326. (cond
  10327. ((equal ans ?d)
  10328. (call-interactively 'org-check-deadlines))
  10329. ((equal ans ?b)
  10330. (call-interactively 'org-check-before-date))
  10331. ((equal ans ?a)
  10332. (call-interactively 'org-check-after-date))
  10333. ((equal ans ?t)
  10334. (org-show-todo-tree '(4)))
  10335. ((member ans '(?T ?m))
  10336. (call-interactively 'org-match-sparse-tree))
  10337. ((member ans '(?p ?P))
  10338. (setq kwd (org-icompleting-read "Property: "
  10339. (mapcar 'list (org-buffer-property-keys))))
  10340. (setq value (org-icompleting-read "Value: "
  10341. (mapcar 'list (org-property-values kwd))))
  10342. (unless (string-match "\\`{.*}\\'" value)
  10343. (setq value (concat "\"" value "\"")))
  10344. (org-match-sparse-tree arg (concat kwd "=" value)))
  10345. ((member ans '(?r ?R ?/))
  10346. (call-interactively 'org-occur))
  10347. (t (error "No such sparse tree command \"%c\"" ans)))))
  10348. (defvar org-occur-highlights nil
  10349. "List of overlays used for occur matches.")
  10350. (make-variable-buffer-local 'org-occur-highlights)
  10351. (defvar org-occur-parameters nil
  10352. "Parameters of the active org-occur calls.
  10353. This is a list, each call to org-occur pushes as cons cell,
  10354. containing the regular expression and the callback, onto the list.
  10355. The list can contain several entries if `org-occur' has been called
  10356. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10357. will only contain one set of parameters. When the highlights are
  10358. removed (for example with `C-c C-c', or with the next edit (depending
  10359. on `org-remove-highlights-with-change'), this variable is emptied
  10360. as well.")
  10361. (make-variable-buffer-local 'org-occur-parameters)
  10362. (defun org-occur (regexp &optional keep-previous callback)
  10363. "Make a compact tree which shows all matches of REGEXP.
  10364. The tree will show the lines where the regexp matches, and all higher
  10365. headlines above the match. It will also show the heading after the match,
  10366. to make sure editing the matching entry is easy.
  10367. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10368. call to `org-occur' will be kept, to allow stacking of calls to this
  10369. command.
  10370. If CALLBACK is non-nil, it is a function which is called to confirm
  10371. that the match should indeed be shown."
  10372. (interactive "sRegexp: \nP")
  10373. (when (equal regexp "")
  10374. (error "Regexp cannot be empty"))
  10375. (unless keep-previous
  10376. (org-remove-occur-highlights nil nil t))
  10377. (push (cons regexp callback) org-occur-parameters)
  10378. (let ((cnt 0))
  10379. (save-excursion
  10380. (goto-char (point-min))
  10381. (if (or (not keep-previous) ; do not want to keep
  10382. (not org-occur-highlights)) ; no previous matches
  10383. ;; hide everything
  10384. (org-overview))
  10385. (while (re-search-forward regexp nil t)
  10386. (when (or (not callback)
  10387. (save-match-data (funcall callback)))
  10388. (setq cnt (1+ cnt))
  10389. (when org-highlight-sparse-tree-matches
  10390. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10391. (org-show-context 'occur-tree))))
  10392. (when org-remove-highlights-with-change
  10393. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10394. nil 'local))
  10395. (unless org-sparse-tree-open-archived-trees
  10396. (org-hide-archived-subtrees (point-min) (point-max)))
  10397. (run-hooks 'org-occur-hook)
  10398. (if (interactive-p)
  10399. (message "%d match(es) for regexp %s" cnt regexp))
  10400. cnt))
  10401. (defun org-show-context (&optional key)
  10402. "Make sure point and context and visible.
  10403. How much context is shown depends upon the variables
  10404. `org-show-hierarchy-above', `org-show-following-heading'. and
  10405. `org-show-siblings'."
  10406. (let ((heading-p (org-on-heading-p t))
  10407. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10408. (following-p (org-get-alist-option org-show-following-heading key))
  10409. (entry-p (org-get-alist-option org-show-entry-below key))
  10410. (siblings-p (org-get-alist-option org-show-siblings key)))
  10411. (catch 'exit
  10412. ;; Show heading or entry text
  10413. (if (and heading-p (not entry-p))
  10414. (org-flag-heading nil) ; only show the heading
  10415. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10416. (org-show-hidden-entry))) ; show entire entry
  10417. (when following-p
  10418. ;; Show next sibling, or heading below text
  10419. (save-excursion
  10420. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10421. (org-flag-heading nil))))
  10422. (when siblings-p (org-show-siblings))
  10423. (when hierarchy-p
  10424. ;; show all higher headings, possibly with siblings
  10425. (save-excursion
  10426. (while (and (condition-case nil
  10427. (progn (org-up-heading-all 1) t)
  10428. (error nil))
  10429. (not (bobp)))
  10430. (org-flag-heading nil)
  10431. (when siblings-p (org-show-siblings))))))))
  10432. (defvar org-reveal-start-hook nil
  10433. "Hook run before revealing a location.")
  10434. (defun org-reveal (&optional siblings)
  10435. "Show current entry, hierarchy above it, and the following headline.
  10436. This can be used to show a consistent set of context around locations
  10437. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10438. not t for the search context.
  10439. With optional argument SIBLINGS, on each level of the hierarchy all
  10440. siblings are shown. This repairs the tree structure to what it would
  10441. look like when opened with hierarchical calls to `org-cycle'.
  10442. With double optional argument `C-u C-u', go to the parent and show the
  10443. entire tree."
  10444. (interactive "P")
  10445. (run-hooks 'org-reveal-start-hook)
  10446. (let ((org-show-hierarchy-above t)
  10447. (org-show-following-heading t)
  10448. (org-show-siblings (if siblings t org-show-siblings)))
  10449. (org-show-context nil))
  10450. (when (equal siblings '(16))
  10451. (save-excursion
  10452. (when (org-up-heading-safe)
  10453. (org-show-subtree)
  10454. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10455. (defun org-highlight-new-match (beg end)
  10456. "Highlight from BEG to END and mark the highlight is an occur headline."
  10457. (let ((ov (org-make-overlay beg end)))
  10458. (org-overlay-put ov 'face 'secondary-selection)
  10459. (push ov org-occur-highlights)))
  10460. (defun org-remove-occur-highlights (&optional beg end noremove)
  10461. "Remove the occur highlights from the buffer.
  10462. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10463. from the `before-change-functions' in the current buffer."
  10464. (interactive)
  10465. (unless org-inhibit-highlight-removal
  10466. (mapc 'org-delete-overlay org-occur-highlights)
  10467. (setq org-occur-highlights nil)
  10468. (setq org-occur-parameters nil)
  10469. (unless noremove
  10470. (remove-hook 'before-change-functions
  10471. 'org-remove-occur-highlights 'local))))
  10472. ;;;; Priorities
  10473. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10474. "Regular expression matching the priority indicator.")
  10475. (defvar org-remove-priority-next-time nil)
  10476. (defun org-priority-up ()
  10477. "Increase the priority of the current item."
  10478. (interactive)
  10479. (org-priority 'up))
  10480. (defun org-priority-down ()
  10481. "Decrease the priority of the current item."
  10482. (interactive)
  10483. (org-priority 'down))
  10484. (defun org-priority (&optional action)
  10485. "Change the priority of an item by ARG.
  10486. ACTION can be `set', `up', `down', or a character."
  10487. (interactive)
  10488. (unless org-enable-priority-commands
  10489. (error "Priority commands are disabled"))
  10490. (setq action (or action 'set))
  10491. (let (current new news have remove)
  10492. (save-excursion
  10493. (org-back-to-heading t)
  10494. (if (looking-at org-priority-regexp)
  10495. (setq current (string-to-char (match-string 2))
  10496. have t)
  10497. (setq current org-default-priority))
  10498. (cond
  10499. ((eq action 'remove)
  10500. (setq remove t new ?\ ))
  10501. ((or (eq action 'set)
  10502. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10503. (if (not (eq action 'set))
  10504. (setq new action)
  10505. (message "Priority %c-%c, SPC to remove: "
  10506. org-highest-priority org-lowest-priority)
  10507. (setq new (read-char-exclusive)))
  10508. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10509. (= (upcase org-lowest-priority) org-lowest-priority))
  10510. (setq new (upcase new)))
  10511. (cond ((equal new ?\ ) (setq remove t))
  10512. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10513. (error "Priority must be between `%c' and `%c'"
  10514. org-highest-priority org-lowest-priority))))
  10515. ((eq action 'up)
  10516. (if (and (not have) (eq last-command this-command))
  10517. (setq new org-lowest-priority)
  10518. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10519. org-default-priority (1- current)))))
  10520. ((eq action 'down)
  10521. (if (and (not have) (eq last-command this-command))
  10522. (setq new org-highest-priority)
  10523. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10524. org-default-priority (1+ current)))))
  10525. (t (error "Invalid action")))
  10526. (if (or (< (upcase new) org-highest-priority)
  10527. (> (upcase new) org-lowest-priority))
  10528. (setq remove t))
  10529. (setq news (format "%c" new))
  10530. (if have
  10531. (if remove
  10532. (replace-match "" t t nil 1)
  10533. (replace-match news t t nil 2))
  10534. (if remove
  10535. (error "No priority cookie found in line")
  10536. (let ((case-fold-search nil))
  10537. (looking-at org-todo-line-regexp))
  10538. (if (match-end 2)
  10539. (progn
  10540. (goto-char (match-end 2))
  10541. (insert " [#" news "]"))
  10542. (goto-char (match-beginning 3))
  10543. (insert "[#" news "] "))))
  10544. (org-preserve-lc (org-set-tags nil 'align)))
  10545. (if remove
  10546. (message "Priority removed")
  10547. (message "Priority of current item set to %s" news))))
  10548. (defun org-get-priority (s)
  10549. "Find priority cookie and return priority."
  10550. (save-match-data
  10551. (if (not (string-match org-priority-regexp s))
  10552. (* 1000 (- org-lowest-priority org-default-priority))
  10553. (* 1000 (- org-lowest-priority
  10554. (string-to-char (match-string 2 s)))))))
  10555. ;;;; Tags
  10556. (defvar org-agenda-archives-mode)
  10557. (defvar org-map-continue-from nil
  10558. "Position from where mapping should continue.
  10559. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10560. (defvar org-scanner-tags nil
  10561. "The current tag list while the tags scanner is running.")
  10562. (defvar org-trust-scanner-tags nil
  10563. "Should `org-get-tags-at' use the tags fro the scanner.
  10564. This is for internal dynamical scoping only.
  10565. When this is non-nil, the function `org-get-tags-at' will return the value
  10566. of `org-scanner-tags' instead of building the list by itself. This
  10567. can lead to large speed-ups when the tags scanner is used in a file with
  10568. many entries, and when the list of tags is retrieved, for example to
  10569. obtain a list of properties. Building the tags list for each entry in such
  10570. a file becomes an N^2 operation - but with this variable set, it scales
  10571. as N.")
  10572. (defun org-scan-tags (action matcher &optional todo-only)
  10573. "Scan headline tags with inheritance and produce output ACTION.
  10574. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10575. or `agenda' to produce an entry list for an agenda view. It can also be
  10576. a Lisp form or a function that should be called at each matched headline, in
  10577. this case the return value is a list of all return values from these calls.
  10578. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10579. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10580. only lines with a TODO keyword are included in the output."
  10581. (require 'org-agenda)
  10582. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10583. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10584. (org-re
  10585. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10586. (props (list 'face 'default
  10587. 'done-face 'org-agenda-done
  10588. 'undone-face 'default
  10589. 'mouse-face 'highlight
  10590. 'org-not-done-regexp org-not-done-regexp
  10591. 'org-todo-regexp org-todo-regexp
  10592. 'help-echo
  10593. (format "mouse-2 or RET jump to org file %s"
  10594. (abbreviate-file-name
  10595. (or (buffer-file-name (buffer-base-buffer))
  10596. (buffer-name (buffer-base-buffer)))))))
  10597. (case-fold-search nil)
  10598. (org-map-continue-from nil)
  10599. lspos tags tags-list
  10600. (tags-alist (list (cons 0 org-file-tags)))
  10601. (llast 0) rtn rtn1 level category i txt
  10602. todo marker entry priority)
  10603. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10604. (setq action (list 'lambda nil action)))
  10605. (save-excursion
  10606. (goto-char (point-min))
  10607. (when (eq action 'sparse-tree)
  10608. (org-overview)
  10609. (org-remove-occur-highlights))
  10610. (while (re-search-forward re nil t)
  10611. (catch :skip
  10612. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10613. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10614. (goto-char (setq lspos (match-beginning 0)))
  10615. (setq level (org-reduced-level (funcall outline-level))
  10616. category (org-get-category))
  10617. (setq i llast llast level)
  10618. ;; remove tag lists from same and sublevels
  10619. (while (>= i level)
  10620. (when (setq entry (assoc i tags-alist))
  10621. (setq tags-alist (delete entry tags-alist)))
  10622. (setq i (1- i)))
  10623. ;; add the next tags
  10624. (when tags
  10625. (setq tags (org-split-string tags ":")
  10626. tags-alist
  10627. (cons (cons level tags) tags-alist)))
  10628. ;; compile tags for current headline
  10629. (setq tags-list
  10630. (if org-use-tag-inheritance
  10631. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10632. tags)
  10633. org-scanner-tags tags-list)
  10634. (when org-use-tag-inheritance
  10635. (setcdr (car tags-alist)
  10636. (mapcar (lambda (x)
  10637. (setq x (copy-sequence x))
  10638. (org-add-prop-inherited x))
  10639. (cdar tags-alist))))
  10640. (when (and tags org-use-tag-inheritance
  10641. (or (not (eq t org-use-tag-inheritance))
  10642. org-tags-exclude-from-inheritance))
  10643. ;; selective inheritance, remove uninherited ones
  10644. (setcdr (car tags-alist)
  10645. (org-remove-uniherited-tags (cdar tags-alist))))
  10646. (when (and (or (not todo-only)
  10647. (and (member todo org-not-done-keywords)
  10648. (or (not org-agenda-tags-todo-honor-ignore-options)
  10649. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10650. (let ((case-fold-search t)) (eval matcher))
  10651. (or
  10652. (not (member org-archive-tag tags-list))
  10653. ;; we have an archive tag, should we use this anyway?
  10654. (or (not org-agenda-skip-archived-trees)
  10655. (and (eq action 'agenda) org-agenda-archives-mode))))
  10656. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10657. ;; select this headline
  10658. (cond
  10659. ((eq action 'sparse-tree)
  10660. (and org-highlight-sparse-tree-matches
  10661. (org-get-heading) (match-end 0)
  10662. (org-highlight-new-match
  10663. (match-beginning 0) (match-beginning 1)))
  10664. (org-show-context 'tags-tree))
  10665. ((eq action 'agenda)
  10666. (setq txt (org-format-agenda-item
  10667. ""
  10668. (concat
  10669. (if (eq org-tags-match-list-sublevels 'indented)
  10670. (make-string (1- level) ?.) "")
  10671. (org-get-heading))
  10672. category
  10673. tags-list
  10674. )
  10675. priority (org-get-priority txt))
  10676. (goto-char lspos)
  10677. (setq marker (org-agenda-new-marker))
  10678. (org-add-props txt props
  10679. 'org-marker marker 'org-hd-marker marker 'org-category category
  10680. 'todo-state todo
  10681. 'priority priority 'type "tagsmatch")
  10682. (push txt rtn))
  10683. ((functionp action)
  10684. (setq org-map-continue-from nil)
  10685. (save-excursion
  10686. (setq rtn1 (funcall action))
  10687. (push rtn1 rtn)))
  10688. (t (error "Invalid action")))
  10689. ;; if we are to skip sublevels, jump to end of subtree
  10690. (unless org-tags-match-list-sublevels
  10691. (org-end-of-subtree t)
  10692. (backward-char 1))))
  10693. ;; Get the correct position from where to continue
  10694. (if org-map-continue-from
  10695. (goto-char org-map-continue-from)
  10696. (and (= (point) lspos) (end-of-line 1)))))
  10697. (when (and (eq action 'sparse-tree)
  10698. (not org-sparse-tree-open-archived-trees))
  10699. (org-hide-archived-subtrees (point-min) (point-max)))
  10700. (nreverse rtn)))
  10701. (defun org-remove-uniherited-tags (tags)
  10702. "Remove all tags that are not inherited from the list TAGS."
  10703. (cond
  10704. ((eq org-use-tag-inheritance t)
  10705. (if org-tags-exclude-from-inheritance
  10706. (org-delete-all org-tags-exclude-from-inheritance tags)
  10707. tags))
  10708. ((not org-use-tag-inheritance) nil)
  10709. ((stringp org-use-tag-inheritance)
  10710. (delq nil (mapcar
  10711. (lambda (x)
  10712. (if (and (string-match org-use-tag-inheritance x)
  10713. (not (member x org-tags-exclude-from-inheritance)))
  10714. x nil))
  10715. tags)))
  10716. ((listp org-use-tag-inheritance)
  10717. (delq nil (mapcar
  10718. (lambda (x)
  10719. (if (member x org-use-tag-inheritance) x nil))
  10720. tags)))))
  10721. (defvar todo-only) ;; dynamically scoped
  10722. (defun org-match-sparse-tree (&optional todo-only match)
  10723. "Create a sparse tree according to tags string MATCH.
  10724. MATCH can contain positive and negative selection of tags, like
  10725. \"+WORK+URGENT-WITHBOSS\".
  10726. If optional argument TODO-ONLY is non-nil, only select lines that are
  10727. also TODO lines."
  10728. (interactive "P")
  10729. (org-prepare-agenda-buffers (list (current-buffer)))
  10730. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10731. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10732. (defvar org-cached-props nil)
  10733. (defun org-cached-entry-get (pom property)
  10734. (if (or (eq t org-use-property-inheritance)
  10735. (and (stringp org-use-property-inheritance)
  10736. (string-match org-use-property-inheritance property))
  10737. (and (listp org-use-property-inheritance)
  10738. (member property org-use-property-inheritance)))
  10739. ;; Caching is not possible, check it directly
  10740. (org-entry-get pom property 'inherit)
  10741. ;; Get all properties, so that we can do complicated checks easily
  10742. (cdr (assoc property (or org-cached-props
  10743. (setq org-cached-props
  10744. (org-entry-properties pom)))))))
  10745. (defun org-global-tags-completion-table (&optional files)
  10746. "Return the list of all tags in all agenda buffer/files."
  10747. (save-excursion
  10748. (org-uniquify
  10749. (delq nil
  10750. (apply 'append
  10751. (mapcar
  10752. (lambda (file)
  10753. (set-buffer (find-file-noselect file))
  10754. (append (org-get-buffer-tags)
  10755. (mapcar (lambda (x) (if (stringp (car-safe x))
  10756. (list (car-safe x)) nil))
  10757. org-tag-alist)))
  10758. (if (and files (car files))
  10759. files
  10760. (org-agenda-files))))))))
  10761. (defun org-make-tags-matcher (match)
  10762. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10763. ;; todo-only is scoped dynamically into this function, and the function
  10764. ;; may change it if the matcher asks for it.
  10765. (unless match
  10766. ;; Get a new match request, with completion
  10767. (let ((org-last-tags-completion-table
  10768. (org-global-tags-completion-table)))
  10769. (setq match (org-completing-read-no-i
  10770. "Match: " 'org-tags-completion-function nil nil nil
  10771. 'org-tags-history))))
  10772. ;; Parse the string and create a lisp form
  10773. (let ((match0 match)
  10774. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10775. minus tag mm
  10776. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10777. orterms term orlist re-p str-p level-p level-op time-p
  10778. prop-p pn pv po cat-p gv rest)
  10779. (if (string-match "/+" match)
  10780. ;; match contains also a todo-matching request
  10781. (progn
  10782. (setq tagsmatch (substring match 0 (match-beginning 0))
  10783. todomatch (substring match (match-end 0)))
  10784. (if (string-match "^!" todomatch)
  10785. (setq todo-only t todomatch (substring todomatch 1)))
  10786. (if (string-match "^\\s-*$" todomatch)
  10787. (setq todomatch nil)))
  10788. ;; only matching tags
  10789. (setq tagsmatch match todomatch nil))
  10790. ;; Make the tags matcher
  10791. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10792. (setq tagsmatcher t)
  10793. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10794. (while (setq term (pop orterms))
  10795. (while (and (equal (substring term -1) "\\") orterms)
  10796. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10797. (while (string-match re term)
  10798. (setq rest (substring term (match-end 0))
  10799. minus (and (match-end 1)
  10800. (equal (match-string 1 term) "-"))
  10801. tag (match-string 2 term)
  10802. re-p (equal (string-to-char tag) ?{)
  10803. level-p (match-end 4)
  10804. prop-p (match-end 5)
  10805. mm (cond
  10806. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10807. (level-p
  10808. (setq level-op (org-op-to-function (match-string 3 term)))
  10809. `(,level-op level ,(string-to-number
  10810. (match-string 4 term))))
  10811. (prop-p
  10812. (setq pn (match-string 5 term)
  10813. po (match-string 6 term)
  10814. pv (match-string 7 term)
  10815. cat-p (equal pn "CATEGORY")
  10816. re-p (equal (string-to-char pv) ?{)
  10817. str-p (equal (string-to-char pv) ?\")
  10818. time-p (save-match-data
  10819. (string-match "^\"[[<].*[]>]\"$" pv))
  10820. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10821. (if time-p (setq pv (org-matcher-time pv)))
  10822. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10823. (cond
  10824. ((equal pn "CATEGORY")
  10825. (setq gv '(get-text-property (point) 'org-category)))
  10826. ((equal pn "TODO")
  10827. (setq gv 'todo))
  10828. (t
  10829. (setq gv `(org-cached-entry-get nil ,pn))))
  10830. (if re-p
  10831. (if (eq po 'org<>)
  10832. `(not (string-match ,pv (or ,gv "")))
  10833. `(string-match ,pv (or ,gv "")))
  10834. (if str-p
  10835. `(,po (or ,gv "") ,pv)
  10836. `(,po (string-to-number (or ,gv ""))
  10837. ,(string-to-number pv) ))))
  10838. (t `(member ,tag tags-list)))
  10839. mm (if minus (list 'not mm) mm)
  10840. term rest)
  10841. (push mm tagsmatcher))
  10842. (push (if (> (length tagsmatcher) 1)
  10843. (cons 'and tagsmatcher)
  10844. (car tagsmatcher))
  10845. orlist)
  10846. (setq tagsmatcher nil))
  10847. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10848. (setq tagsmatcher
  10849. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10850. ;; Make the todo matcher
  10851. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10852. (setq todomatcher t)
  10853. (setq orterms (org-split-string todomatch "|") orlist nil)
  10854. (while (setq term (pop orterms))
  10855. (while (string-match re term)
  10856. (setq minus (and (match-end 1)
  10857. (equal (match-string 1 term) "-"))
  10858. kwd (match-string 2 term)
  10859. re-p (equal (string-to-char kwd) ?{)
  10860. term (substring term (match-end 0))
  10861. mm (if re-p
  10862. `(string-match ,(substring kwd 1 -1) todo)
  10863. (list 'equal 'todo kwd))
  10864. mm (if minus (list 'not mm) mm))
  10865. (push mm todomatcher))
  10866. (push (if (> (length todomatcher) 1)
  10867. (cons 'and todomatcher)
  10868. (car todomatcher))
  10869. orlist)
  10870. (setq todomatcher nil))
  10871. (setq todomatcher (if (> (length orlist) 1)
  10872. (cons 'or orlist) (car orlist))))
  10873. ;; Return the string and lisp forms of the matcher
  10874. (setq matcher (if todomatcher
  10875. (list 'and tagsmatcher todomatcher)
  10876. tagsmatcher))
  10877. (cons match0 matcher)))
  10878. (defun org-op-to-function (op &optional stringp)
  10879. "Turn an operator into the appropriate function."
  10880. (setq op
  10881. (cond
  10882. ((equal op "<" ) '(< string< org-time<))
  10883. ((equal op ">" ) '(> org-string> org-time>))
  10884. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10885. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10886. ((member op '("=" "==")) '(= string= org-time=))
  10887. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10888. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10889. (defun org<> (a b) (not (= a b)))
  10890. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10891. (defun org-string>= (a b) (not (string< a b)))
  10892. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10893. (defun org-string<> (a b) (not (string= a b)))
  10894. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10895. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10896. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10897. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10898. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10899. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10900. (defun org-2ft (s)
  10901. "Convert S to a floating point time.
  10902. If S is already a number, just return it. If it is a string, parse
  10903. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10904. (cond
  10905. ((numberp s) s)
  10906. ((stringp s)
  10907. (condition-case nil
  10908. (float-time (apply 'encode-time (org-parse-time-string s)))
  10909. (error 0.)))
  10910. (t 0.)))
  10911. (defun org-time-today ()
  10912. "Time in seconds today at 0:00.
  10913. Returns the float number of seconds since the beginning of the
  10914. epoch to the beginning of today (00:00)."
  10915. (float-time (apply 'encode-time
  10916. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10917. (defun org-matcher-time (s)
  10918. "Interpret a time comparison value."
  10919. (save-match-data
  10920. (cond
  10921. ((string= s "<now>") (float-time))
  10922. ((string= s "<today>") (org-time-today))
  10923. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10924. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10925. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10926. (+ (org-time-today)
  10927. (* (string-to-number (match-string 1 s))
  10928. (cdr (assoc (match-string 2 s)
  10929. '(("d" . 86400.0) ("w" . 604800.0)
  10930. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10931. (t (org-2ft s)))))
  10932. (defun org-match-any-p (re list)
  10933. "Does re match any element of list?"
  10934. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10935. (delq nil list))
  10936. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10937. (defvar org-tags-overlay (org-make-overlay 1 1))
  10938. (org-detach-overlay org-tags-overlay)
  10939. (defun org-get-local-tags-at (&optional pos)
  10940. "Get a list of tags defined in the current headline."
  10941. (org-get-tags-at pos 'local))
  10942. (defun org-get-local-tags ()
  10943. "Get a list of tags defined in the current headline."
  10944. (org-get-tags-at nil 'local))
  10945. (defun org-get-tags-at (&optional pos local)
  10946. "Get a list of all headline tags applicable at POS.
  10947. POS defaults to point. If tags are inherited, the list contains
  10948. the targets in the same sequence as the headlines appear, i.e.
  10949. the tags of the current headline come last.
  10950. When LOCAL is non-nil, only return tags from the current headline,
  10951. ignore inherited ones."
  10952. (interactive)
  10953. (if (and org-trust-scanner-tags
  10954. (or (not pos) (equal pos (point)))
  10955. (not local))
  10956. org-scanner-tags
  10957. (let (tags ltags lastpos parent)
  10958. (save-excursion
  10959. (save-restriction
  10960. (widen)
  10961. (goto-char (or pos (point)))
  10962. (save-match-data
  10963. (catch 'done
  10964. (condition-case nil
  10965. (progn
  10966. (org-back-to-heading t)
  10967. (while (not (equal lastpos (point)))
  10968. (setq lastpos (point))
  10969. (when (looking-at
  10970. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10971. (setq ltags (org-split-string
  10972. (org-match-string-no-properties 1) ":"))
  10973. (when parent
  10974. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10975. (setq tags (append
  10976. (if parent
  10977. (org-remove-uniherited-tags ltags)
  10978. ltags)
  10979. tags)))
  10980. (or org-use-tag-inheritance (throw 'done t))
  10981. (if local (throw 'done t))
  10982. (or (org-up-heading-safe) (error nil))
  10983. (setq parent t)))
  10984. (error nil)))))
  10985. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10986. (defun org-add-prop-inherited (s)
  10987. (add-text-properties 0 (length s) '(inherited t) s)
  10988. s)
  10989. (defun org-toggle-tag (tag &optional onoff)
  10990. "Toggle the tag TAG for the current line.
  10991. If ONOFF is `on' or `off', don't toggle but set to this state."
  10992. (let (res current)
  10993. (save-excursion
  10994. (org-back-to-heading t)
  10995. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10996. (point-at-eol) t)
  10997. (progn
  10998. (setq current (match-string 1))
  10999. (replace-match ""))
  11000. (setq current ""))
  11001. (setq current (nreverse (org-split-string current ":")))
  11002. (cond
  11003. ((eq onoff 'on)
  11004. (setq res t)
  11005. (or (member tag current) (push tag current)))
  11006. ((eq onoff 'off)
  11007. (or (not (member tag current)) (setq current (delete tag current))))
  11008. (t (if (member tag current)
  11009. (setq current (delete tag current))
  11010. (setq res t)
  11011. (push tag current))))
  11012. (end-of-line 1)
  11013. (if current
  11014. (progn
  11015. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11016. (org-set-tags nil t))
  11017. (delete-horizontal-space))
  11018. (run-hooks 'org-after-tags-change-hook))
  11019. res))
  11020. (defun org-align-tags-here (to-col)
  11021. ;; Assumes that this is a headline
  11022. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11023. (beginning-of-line 1)
  11024. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11025. (< pos (match-beginning 2)))
  11026. (progn
  11027. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11028. (goto-char (match-beginning 1))
  11029. (insert " ")
  11030. (delete-region (point) (1+ (match-beginning 2)))
  11031. (setq ncol (max (1+ (current-column))
  11032. (1+ col)
  11033. (if (> to-col 0)
  11034. to-col
  11035. (- (abs to-col) tags-l))))
  11036. (setq p (point))
  11037. (insert (make-string (- ncol (current-column)) ?\ ))
  11038. (setq ncol (current-column))
  11039. (when indent-tabs-mode (tabify p (point-at-eol)))
  11040. (org-move-to-column (min ncol col) t))
  11041. (goto-char pos))))
  11042. (defun org-set-tags-command (&optional arg just-align)
  11043. "Call the set-tags command for the current entry."
  11044. (interactive "P")
  11045. (if (org-on-heading-p)
  11046. (org-set-tags arg just-align)
  11047. (save-excursion
  11048. (org-back-to-heading t)
  11049. (org-set-tags arg just-align))))
  11050. (defun org-set-tags-to (data)
  11051. "Set the tags of the current entry to DATA, replacing the current tags.
  11052. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11053. If DATA is nil or the empty string, any tags will be removed."
  11054. (interactive "sTags: ")
  11055. (setq data
  11056. (cond
  11057. ((eq data nil) "")
  11058. ((equal data "") "")
  11059. ((stringp data)
  11060. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11061. ":"))
  11062. ((listp data)
  11063. (concat ":" (mapconcat 'identity data ":") ":"))
  11064. (t nil)))
  11065. (when data
  11066. (save-excursion
  11067. (org-back-to-heading t)
  11068. (when (looking-at org-complex-heading-regexp)
  11069. (if (match-end 5)
  11070. (progn
  11071. (goto-char (match-beginning 5))
  11072. (insert data)
  11073. (delete-region (point) (point-at-eol))
  11074. (org-set-tags nil 'align))
  11075. (goto-char (point-at-eol))
  11076. (insert " " data)
  11077. (org-set-tags nil 'align)))
  11078. (beginning-of-line 1)
  11079. (if (looking-at ".*?\\([ \t]+\\)$")
  11080. (delete-region (match-beginning 1) (match-end 1))))))
  11081. (defun org-set-tags (&optional arg just-align)
  11082. "Set the tags for the current headline.
  11083. With prefix ARG, realign all tags in headings in the current buffer."
  11084. (interactive "P")
  11085. (let* ((re (concat "^" outline-regexp))
  11086. (current (org-get-tags-string))
  11087. (col (current-column))
  11088. (org-setting-tags t)
  11089. table current-tags inherited-tags ; computed below when needed
  11090. tags p0 c0 c1 rpl)
  11091. (if arg
  11092. (save-excursion
  11093. (goto-char (point-min))
  11094. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11095. (while (re-search-forward re nil t)
  11096. (org-set-tags nil t)
  11097. (end-of-line 1)))
  11098. (message "All tags realigned to column %d" org-tags-column))
  11099. (if just-align
  11100. (setq tags current)
  11101. ;; Get a new set of tags from the user
  11102. (save-excursion
  11103. (setq table (append org-tag-persistent-alist
  11104. (or org-tag-alist (org-get-buffer-tags))
  11105. (and org-complete-tags-always-offer-all-agenda-tags
  11106. (org-global-tags-completion-table (org-agenda-files))))
  11107. org-last-tags-completion-table table
  11108. current-tags (org-split-string current ":")
  11109. inherited-tags (nreverse
  11110. (nthcdr (length current-tags)
  11111. (nreverse (org-get-tags-at))))
  11112. tags
  11113. (if (or (eq t org-use-fast-tag-selection)
  11114. (and org-use-fast-tag-selection
  11115. (delq nil (mapcar 'cdr table))))
  11116. (org-fast-tag-selection
  11117. current-tags inherited-tags table
  11118. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11119. (let ((org-add-colon-after-tag-completion t))
  11120. (org-trim
  11121. (org-without-partial-completion
  11122. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11123. nil nil current 'org-tags-history)))))))
  11124. (while (string-match "[-+&]+" tags)
  11125. ;; No boolean logic, just a list
  11126. (setq tags (replace-match ":" t t tags))))
  11127. (if org-tags-sort-function
  11128. (setq tags (mapconcat 'identity
  11129. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11130. org-tags-sort-function) ":")))
  11131. (if (string-match "\\`[\t ]*\\'" tags)
  11132. (setq tags "")
  11133. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11134. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11135. ;; Insert new tags at the correct column
  11136. (beginning-of-line 1)
  11137. (cond
  11138. ((and (equal current "") (equal tags "")))
  11139. ((re-search-forward
  11140. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11141. (point-at-eol) t)
  11142. (if (equal tags "")
  11143. (setq rpl "")
  11144. (goto-char (match-beginning 0))
  11145. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11146. (1+ (point)) (point))
  11147. c1 (max (1+ c0) (if (> org-tags-column 0)
  11148. org-tags-column
  11149. (- (- org-tags-column) (length tags))))
  11150. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11151. (replace-match rpl t t)
  11152. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11153. tags)
  11154. (t (error "Tags alignment failed")))
  11155. (org-move-to-column col)
  11156. (unless just-align
  11157. (run-hooks 'org-after-tags-change-hook)))))
  11158. (defun org-change-tag-in-region (beg end tag off)
  11159. "Add or remove TAG for each entry in the region.
  11160. This works in the agenda, and also in an org-mode buffer."
  11161. (interactive
  11162. (list (region-beginning) (region-end)
  11163. (let ((org-last-tags-completion-table
  11164. (if (org-mode-p)
  11165. (org-get-buffer-tags)
  11166. (org-global-tags-completion-table))))
  11167. (org-icompleting-read
  11168. "Tag: " 'org-tags-completion-function nil nil nil
  11169. 'org-tags-history))
  11170. (progn
  11171. (message "[s]et or [r]emove? ")
  11172. (equal (read-char-exclusive) ?r))))
  11173. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11174. (let ((agendap (equal major-mode 'org-agenda-mode))
  11175. l1 l2 m buf pos newhead (cnt 0))
  11176. (goto-char end)
  11177. (setq l2 (1- (org-current-line)))
  11178. (goto-char beg)
  11179. (setq l1 (org-current-line))
  11180. (loop for l from l1 to l2 do
  11181. (org-goto-line l)
  11182. (setq m (get-text-property (point) 'org-hd-marker))
  11183. (when (or (and (org-mode-p) (org-on-heading-p))
  11184. (and agendap m))
  11185. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11186. pos (if agendap m (point)))
  11187. (with-current-buffer buf
  11188. (save-excursion
  11189. (save-restriction
  11190. (goto-char pos)
  11191. (setq cnt (1+ cnt))
  11192. (org-toggle-tag tag (if off 'off 'on))
  11193. (setq newhead (org-get-heading)))))
  11194. (and agendap (org-agenda-change-all-lines newhead m))))
  11195. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11196. (defun org-tags-completion-function (string predicate &optional flag)
  11197. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11198. (confirm (lambda (x) (stringp (car x)))))
  11199. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11200. (setq s1 (match-string 1 string)
  11201. s2 (match-string 2 string))
  11202. (setq s1 "" s2 string))
  11203. (cond
  11204. ((eq flag nil)
  11205. ;; try completion
  11206. (setq rtn (try-completion s2 ctable confirm))
  11207. (if (stringp rtn)
  11208. (setq rtn
  11209. (concat s1 s2 (substring rtn (length s2))
  11210. (if (and org-add-colon-after-tag-completion
  11211. (assoc rtn ctable))
  11212. ":" ""))))
  11213. rtn)
  11214. ((eq flag t)
  11215. ;; all-completions
  11216. (all-completions s2 ctable confirm)
  11217. )
  11218. ((eq flag 'lambda)
  11219. ;; exact match?
  11220. (assoc s2 ctable)))
  11221. ))
  11222. (defun org-fast-tag-insert (kwd tags face &optional end)
  11223. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11224. (insert (format "%-12s" (concat kwd ":"))
  11225. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11226. (or end "")))
  11227. (defun org-fast-tag-show-exit (flag)
  11228. (save-excursion
  11229. (org-goto-line 3)
  11230. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11231. (replace-match ""))
  11232. (when flag
  11233. (end-of-line 1)
  11234. (org-move-to-column (- (window-width) 19) t)
  11235. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11236. (defun org-set-current-tags-overlay (current prefix)
  11237. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11238. (if (featurep 'xemacs)
  11239. (org-overlay-display org-tags-overlay (concat prefix s)
  11240. 'secondary-selection)
  11241. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11242. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11243. (defvar org-last-tag-selection-key nil)
  11244. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11245. "Fast tag selection with single keys.
  11246. CURRENT is the current list of tags in the headline, INHERITED is the
  11247. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11248. possibly with grouping information. TODO-TABLE is a similar table with
  11249. TODO keywords, should these have keys assigned to them.
  11250. If the keys are nil, a-z are automatically assigned.
  11251. Returns the new tags string, or nil to not change the current settings."
  11252. (let* ((fulltable (append table todo-table))
  11253. (maxlen (apply 'max (mapcar
  11254. (lambda (x)
  11255. (if (stringp (car x)) (string-width (car x)) 0))
  11256. fulltable)))
  11257. (buf (current-buffer))
  11258. (expert (eq org-fast-tag-selection-single-key 'expert))
  11259. (buffer-tags nil)
  11260. (fwidth (+ maxlen 3 1 3))
  11261. (ncol (/ (- (window-width) 4) fwidth))
  11262. (i-face 'org-done)
  11263. (c-face 'org-todo)
  11264. tg cnt e c char c1 c2 ntable tbl rtn
  11265. ov-start ov-end ov-prefix
  11266. (exit-after-next org-fast-tag-selection-single-key)
  11267. (done-keywords org-done-keywords)
  11268. groups ingroup)
  11269. (save-excursion
  11270. (beginning-of-line 1)
  11271. (if (looking-at
  11272. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11273. (setq ov-start (match-beginning 1)
  11274. ov-end (match-end 1)
  11275. ov-prefix "")
  11276. (setq ov-start (1- (point-at-eol))
  11277. ov-end (1+ ov-start))
  11278. (skip-chars-forward "^\n\r")
  11279. (setq ov-prefix
  11280. (concat
  11281. (buffer-substring (1- (point)) (point))
  11282. (if (> (current-column) org-tags-column)
  11283. " "
  11284. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11285. (org-move-overlay org-tags-overlay ov-start ov-end)
  11286. (save-window-excursion
  11287. (if expert
  11288. (set-buffer (get-buffer-create " *Org tags*"))
  11289. (delete-other-windows)
  11290. (split-window-vertically)
  11291. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11292. (erase-buffer)
  11293. (org-set-local 'org-done-keywords done-keywords)
  11294. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11295. (org-fast-tag-insert "Current" current c-face "\n\n")
  11296. (org-fast-tag-show-exit exit-after-next)
  11297. (org-set-current-tags-overlay current ov-prefix)
  11298. (setq tbl fulltable char ?a cnt 0)
  11299. (while (setq e (pop tbl))
  11300. (cond
  11301. ((equal (car e) :startgroup)
  11302. (push '() groups) (setq ingroup t)
  11303. (when (not (= cnt 0))
  11304. (setq cnt 0)
  11305. (insert "\n"))
  11306. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11307. ((equal (car e) :endgroup)
  11308. (setq ingroup nil cnt 0)
  11309. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11310. ((equal e '(:newline))
  11311. (when (not (= cnt 0))
  11312. (setq cnt 0)
  11313. (insert "\n")
  11314. (setq e (car tbl))
  11315. (while (equal (car tbl) '(:newline))
  11316. (insert "\n")
  11317. (setq tbl (cdr tbl)))))
  11318. (t
  11319. (setq tg (copy-sequence (car e)) c2 nil)
  11320. (if (cdr e)
  11321. (setq c (cdr e))
  11322. ;; automatically assign a character.
  11323. (setq c1 (string-to-char
  11324. (downcase (substring
  11325. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11326. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11327. (while (or (rassoc char ntable) (rassoc char table))
  11328. (setq char (1+ char)))
  11329. (setq c2 c1))
  11330. (setq c (or c2 char)))
  11331. (if ingroup (push tg (car groups)))
  11332. (setq tg (org-add-props tg nil 'face
  11333. (cond
  11334. ((not (assoc tg table))
  11335. (org-get-todo-face tg))
  11336. ((member tg current) c-face)
  11337. ((member tg inherited) i-face)
  11338. (t nil))))
  11339. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11340. (insert "[" c "] " tg (make-string
  11341. (- fwidth 4 (length tg)) ?\ ))
  11342. (push (cons tg c) ntable)
  11343. (when (= (setq cnt (1+ cnt)) ncol)
  11344. (insert "\n")
  11345. (if ingroup (insert " "))
  11346. (setq cnt 0)))))
  11347. (setq ntable (nreverse ntable))
  11348. (insert "\n")
  11349. (goto-char (point-min))
  11350. (if (not expert) (org-fit-window-to-buffer))
  11351. (setq rtn
  11352. (catch 'exit
  11353. (while t
  11354. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11355. (if (not groups) "no " "")
  11356. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11357. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11358. (setq org-last-tag-selection-key c)
  11359. (cond
  11360. ((= c ?\r) (throw 'exit t))
  11361. ((= c ?!)
  11362. (setq groups (not groups))
  11363. (goto-char (point-min))
  11364. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11365. ((= c ?\C-c)
  11366. (if (not expert)
  11367. (org-fast-tag-show-exit
  11368. (setq exit-after-next (not exit-after-next)))
  11369. (setq expert nil)
  11370. (delete-other-windows)
  11371. (split-window-vertically)
  11372. (org-switch-to-buffer-other-window " *Org tags*")
  11373. (org-fit-window-to-buffer)))
  11374. ((or (= c ?\C-g)
  11375. (and (= c ?q) (not (rassoc c ntable))))
  11376. (org-detach-overlay org-tags-overlay)
  11377. (setq quit-flag t))
  11378. ((= c ?\ )
  11379. (setq current nil)
  11380. (if exit-after-next (setq exit-after-next 'now)))
  11381. ((= c ?\t)
  11382. (condition-case nil
  11383. (setq tg (org-icompleting-read
  11384. "Tag: "
  11385. (or buffer-tags
  11386. (with-current-buffer buf
  11387. (org-get-buffer-tags)))))
  11388. (quit (setq tg "")))
  11389. (when (string-match "\\S-" tg)
  11390. (add-to-list 'buffer-tags (list tg))
  11391. (if (member tg current)
  11392. (setq current (delete tg current))
  11393. (push tg current)))
  11394. (if exit-after-next (setq exit-after-next 'now)))
  11395. ((setq e (rassoc c todo-table) tg (car e))
  11396. (with-current-buffer buf
  11397. (save-excursion (org-todo tg)))
  11398. (if exit-after-next (setq exit-after-next 'now)))
  11399. ((setq e (rassoc c ntable) tg (car e))
  11400. (if (member tg current)
  11401. (setq current (delete tg current))
  11402. (loop for g in groups do
  11403. (if (member tg g)
  11404. (mapc (lambda (x)
  11405. (setq current (delete x current)))
  11406. g)))
  11407. (push tg current))
  11408. (if exit-after-next (setq exit-after-next 'now))))
  11409. ;; Create a sorted list
  11410. (setq current
  11411. (sort current
  11412. (lambda (a b)
  11413. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11414. (if (eq exit-after-next 'now) (throw 'exit t))
  11415. (goto-char (point-min))
  11416. (beginning-of-line 2)
  11417. (delete-region (point) (point-at-eol))
  11418. (org-fast-tag-insert "Current" current c-face)
  11419. (org-set-current-tags-overlay current ov-prefix)
  11420. (while (re-search-forward
  11421. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11422. (setq tg (match-string 1))
  11423. (add-text-properties
  11424. (match-beginning 1) (match-end 1)
  11425. (list 'face
  11426. (cond
  11427. ((member tg current) c-face)
  11428. ((member tg inherited) i-face)
  11429. (t (get-text-property (match-beginning 1) 'face))))))
  11430. (goto-char (point-min)))))
  11431. (org-detach-overlay org-tags-overlay)
  11432. (if rtn
  11433. (mapconcat 'identity current ":")
  11434. nil))))
  11435. (defun org-get-tags-string ()
  11436. "Get the TAGS string in the current headline."
  11437. (unless (org-on-heading-p t)
  11438. (error "Not on a heading"))
  11439. (save-excursion
  11440. (beginning-of-line 1)
  11441. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11442. (org-match-string-no-properties 1)
  11443. "")))
  11444. (defun org-get-tags ()
  11445. "Get the list of tags specified in the current headline."
  11446. (org-split-string (org-get-tags-string) ":"))
  11447. (defun org-get-buffer-tags ()
  11448. "Get a table of all tags used in the buffer, for completion."
  11449. (let (tags)
  11450. (save-excursion
  11451. (goto-char (point-min))
  11452. (while (re-search-forward
  11453. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11454. (when (equal (char-after (point-at-bol 0)) ?*)
  11455. (mapc (lambda (x) (add-to-list 'tags x))
  11456. (org-split-string (org-match-string-no-properties 1) ":")))))
  11457. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11458. (mapcar 'list tags)))
  11459. ;;;; The mapping API
  11460. ;;;###autoload
  11461. (defun org-map-entries (func &optional match scope &rest skip)
  11462. "Call FUNC at each headline selected by MATCH in SCOPE.
  11463. FUNC is a function or a lisp form. The function will be called without
  11464. arguments, with the cursor positioned at the beginning of the headline.
  11465. The return values of all calls to the function will be collected and
  11466. returned as a list.
  11467. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11468. does not need to preserve point. After evaluation, the cursor will be
  11469. moved to the end of the line (presumably of the headline of the
  11470. processed entry) and search continues from there. Under some
  11471. circumstances, this may not produce the wanted results. For example,
  11472. if you have removed (e.g. archived) the current (sub)tree it could
  11473. mean that the next entry will be skipped entirely. In such cases, you
  11474. can specify the position from where search should continue by making
  11475. FUNC set the variable `org-map-continue-from' to the desired buffer
  11476. position.
  11477. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11478. Only headlines that are matched by this query will be considered during
  11479. the iteration. When MATCH is nil or t, all headlines will be
  11480. visited by the iteration.
  11481. SCOPE determines the scope of this command. It can be any of:
  11482. nil The current buffer, respecting the restriction if any
  11483. tree The subtree started with the entry at point
  11484. file The current buffer, without restriction
  11485. file-with-archives
  11486. The current buffer, and any archives associated with it
  11487. agenda All agenda files
  11488. agenda-with-archives
  11489. All agenda files with any archive files associated with them
  11490. \(file1 file2 ...)
  11491. If this is a list, all files in the list will be scanned
  11492. The remaining args are treated as settings for the skipping facilities of
  11493. the scanner. The following items can be given here:
  11494. archive skip trees with the archive tag.
  11495. comment skip trees with the COMMENT keyword
  11496. function or Emacs Lisp form:
  11497. will be used as value for `org-agenda-skip-function', so whenever
  11498. the function returns t, FUNC will not be called for that
  11499. entry and search will continue from the point where the
  11500. function leaves it.
  11501. If your function needs to retrieve the tags including inherited tags
  11502. at the *current* entry, you can use the value of the variable
  11503. `org-scanner-tags' which will be much faster than getting the value
  11504. with `org-get-tags-at'. If your function gets properties with
  11505. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11506. to t around the call to `org-entry-properties' to get the same speedup.
  11507. Note that if your function moves around to retrieve tags and properties at
  11508. a *different* entry, you cannot use these techniques."
  11509. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11510. (org-agenda-skip-archived-trees (memq 'archive skip))
  11511. (org-agenda-skip-comment-trees (memq 'comment skip))
  11512. (org-agenda-skip-function
  11513. (car (org-delete-all '(comment archive) skip)))
  11514. (org-tags-match-list-sublevels t)
  11515. matcher file res
  11516. org-todo-keywords-for-agenda
  11517. org-done-keywords-for-agenda
  11518. org-todo-keyword-alist-for-agenda
  11519. org-drawers-for-agenda
  11520. org-tag-alist-for-agenda)
  11521. (cond
  11522. ((eq match t) (setq matcher t))
  11523. ((eq match nil) (setq matcher t))
  11524. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11525. (save-excursion
  11526. (save-restriction
  11527. (when (eq scope 'tree)
  11528. (org-back-to-heading t)
  11529. (org-narrow-to-subtree)
  11530. (setq scope nil))
  11531. (if (not scope)
  11532. (progn
  11533. (org-prepare-agenda-buffers
  11534. (list (buffer-file-name (current-buffer))))
  11535. (setq res (org-scan-tags func matcher)))
  11536. ;; Get the right scope
  11537. (cond
  11538. ((and scope (listp scope) (symbolp (car scope)))
  11539. (setq scope (eval scope)))
  11540. ((eq scope 'agenda)
  11541. (setq scope (org-agenda-files t)))
  11542. ((eq scope 'agenda-with-archives)
  11543. (setq scope (org-agenda-files t))
  11544. (setq scope (org-add-archive-files scope)))
  11545. ((eq scope 'file)
  11546. (setq scope (list (buffer-file-name))))
  11547. ((eq scope 'file-with-archives)
  11548. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11549. (org-prepare-agenda-buffers scope)
  11550. (while (setq file (pop scope))
  11551. (with-current-buffer (org-find-base-buffer-visiting file)
  11552. (save-excursion
  11553. (save-restriction
  11554. (widen)
  11555. (goto-char (point-min))
  11556. (setq res (append res (org-scan-tags func matcher))))))))))
  11557. res))
  11558. ;;;; Properties
  11559. ;;; Setting and retrieving properties
  11560. (defconst org-special-properties
  11561. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11562. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11563. "The special properties valid in Org-mode.
  11564. These are properties that are not defined in the property drawer,
  11565. but in some other way.")
  11566. (defconst org-default-properties
  11567. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11568. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11569. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11570. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11571. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11572. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11573. "Some properties that are used by Org-mode for various purposes.
  11574. Being in this list makes sure that they are offered for completion.")
  11575. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11576. "Regular expression matching the first line of a property drawer.")
  11577. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11578. "Regular expression matching the last line of a property drawer.")
  11579. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11580. "Regular expression matching the first line of a property drawer.")
  11581. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11582. "Regular expression matching the first line of a property drawer.")
  11583. (defconst org-property-drawer-re
  11584. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11585. org-property-end-re "\\)\n?")
  11586. "Matches an entire property drawer.")
  11587. (defconst org-clock-drawer-re
  11588. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11589. org-property-end-re "\\)\n?")
  11590. "Matches an entire clock drawer.")
  11591. (defun org-property-action ()
  11592. "Do an action on properties."
  11593. (interactive)
  11594. (let (c)
  11595. (org-at-property-p)
  11596. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11597. (setq c (read-char-exclusive))
  11598. (cond
  11599. ((equal c ?s)
  11600. (call-interactively 'org-set-property))
  11601. ((equal c ?d)
  11602. (call-interactively 'org-delete-property))
  11603. ((equal c ?D)
  11604. (call-interactively 'org-delete-property-globally))
  11605. ((equal c ?c)
  11606. (call-interactively 'org-compute-property-at-point))
  11607. (t (error "No such property action %c" c)))))
  11608. (defun org-set-effort (&optional value)
  11609. "Set the effort property of the current entry.
  11610. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11611. allowed value."
  11612. (interactive "P")
  11613. (if (equal value 0) (setq value 10))
  11614. (let* ((completion-ignore-case t)
  11615. (prop org-effort-property)
  11616. (cur (org-entry-get nil prop))
  11617. (allowed (org-property-get-allowed-values nil prop 'table))
  11618. (existing (mapcar 'list (org-property-values prop)))
  11619. rpl
  11620. (val (cond
  11621. ((stringp value) value)
  11622. ((and allowed (integerp value))
  11623. (or (car (nth (1- value) allowed))
  11624. (car (org-last allowed))))
  11625. (allowed
  11626. (message "Select 1-9,0, [RET%s]: %s"
  11627. (if cur (concat "=" cur) "")
  11628. (mapconcat 'car allowed " "))
  11629. (setq rpl (read-char-exclusive))
  11630. (if (equal rpl ?\r)
  11631. cur
  11632. (setq rpl (- rpl ?0))
  11633. (if (equal rpl 0) (setq rpl 10))
  11634. (if (and (> rpl 0) (<= rpl (length allowed)))
  11635. (car (nth (1- rpl) allowed))
  11636. (org-completing-read "Effort: " allowed nil))))
  11637. (t
  11638. (let (org-completion-use-ido org-completion-use-iswitchb)
  11639. (org-completing-read
  11640. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11641. (concat "[" cur "]") "")
  11642. ": ")
  11643. existing nil nil "" nil cur))))))
  11644. (unless (equal (org-entry-get nil prop) val)
  11645. (org-entry-put nil prop val))
  11646. (message "%s is now %s" prop val)))
  11647. (defun org-at-property-p ()
  11648. "Is cursor inside a property drawer?"
  11649. (save-excursion
  11650. (beginning-of-line 1)
  11651. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11652. (let ((match (match-data)) ;; Keep match-data for use by calling
  11653. (p (point)) ;; procedures.
  11654. (range (unless (org-before-first-heading-p)
  11655. (org-get-property-block))))
  11656. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11657. (set-match-data match))))))
  11658. (defun org-get-property-block (&optional beg end force)
  11659. "Return the (beg . end) range of the body of the property drawer.
  11660. BEG and END can be beginning and end of subtree, if not given
  11661. they will be found.
  11662. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11663. (catch 'exit
  11664. (save-excursion
  11665. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11666. (end (or end (progn (outline-next-heading) (point)))))
  11667. (goto-char beg)
  11668. (if (re-search-forward org-property-start-re end t)
  11669. (setq beg (1+ (match-end 0)))
  11670. (if force
  11671. (save-excursion
  11672. (org-insert-property-drawer)
  11673. (setq end (progn (outline-next-heading) (point))))
  11674. (throw 'exit nil))
  11675. (goto-char beg)
  11676. (if (re-search-forward org-property-start-re end t)
  11677. (setq beg (1+ (match-end 0)))))
  11678. (if (re-search-forward org-property-end-re end t)
  11679. (setq end (match-beginning 0))
  11680. (or force (throw 'exit nil))
  11681. (goto-char beg)
  11682. (setq end beg)
  11683. (org-indent-line-function)
  11684. (insert ":END:\n"))
  11685. (cons beg end)))))
  11686. (defun org-entry-properties (&optional pom which specific)
  11687. "Get all properties of the entry at point-or-marker POM.
  11688. This includes the TODO keyword, the tags, time strings for deadline,
  11689. scheduled, and clocking, and any additional properties defined in the
  11690. entry. The return value is an alist, keys may occur multiple times
  11691. if the property key was used several times.
  11692. POM may also be nil, in which case the current entry is used.
  11693. If WHICH is nil or `all', get all properties. If WHICH is
  11694. `special' or `standard', only get that subclass. If WHICH
  11695. is a string only get exactly this property. Specific can be a string, the
  11696. specific property we are interested in. Specifying it can speed
  11697. things up because then unnecessary parsing is avoided."
  11698. (setq which (or which 'all))
  11699. (org-with-point-at pom
  11700. (let ((clockstr (substring org-clock-string 0 -1))
  11701. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11702. (case-fold-search nil)
  11703. beg end range props sum-props key value string clocksum)
  11704. (save-excursion
  11705. (when (condition-case nil
  11706. (and (org-mode-p) (org-back-to-heading t))
  11707. (error nil))
  11708. (setq beg (point))
  11709. (setq sum-props (get-text-property (point) 'org-summaries))
  11710. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11711. (outline-next-heading)
  11712. (setq end (point))
  11713. (when (memq which '(all special))
  11714. ;; Get the special properties, like TODO and tags
  11715. (goto-char beg)
  11716. (when (and (or (not specific) (string= specific "TODO"))
  11717. (looking-at org-todo-line-regexp) (match-end 2))
  11718. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11719. (when (and (or (not specific) (string= specific "PRIORITY"))
  11720. (looking-at org-priority-regexp))
  11721. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11722. (when (and (or (not specific) (string= specific "TAGS"))
  11723. (setq value (org-get-tags-string))
  11724. (string-match "\\S-" value))
  11725. (push (cons "TAGS" value) props))
  11726. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11727. (setq value (org-get-tags-at)))
  11728. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11729. ":"))
  11730. props))
  11731. (when (or (not specific) (string= specific "BLOCKED"))
  11732. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11733. (when (or (not specific)
  11734. (member specific org-all-time-keywords)
  11735. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11736. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11737. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11738. string (if (equal key clockstr)
  11739. (org-no-properties
  11740. (org-trim
  11741. (buffer-substring
  11742. (match-beginning 3) (goto-char (point-at-eol)))))
  11743. (substring (org-match-string-no-properties 3) 1 -1)))
  11744. (unless key
  11745. (if (= (char-after (match-beginning 3)) ?\[)
  11746. (setq key "TIMESTAMP_IA")
  11747. (setq key "TIMESTAMP")))
  11748. (when (or (equal key clockstr) (not (assoc key props)))
  11749. (push (cons key string) props))))
  11750. )
  11751. (when (memq which '(all standard))
  11752. ;; Get the standard properties, like :PROP: ...
  11753. (setq range (org-get-property-block beg end))
  11754. (when range
  11755. (goto-char (car range))
  11756. (while (re-search-forward
  11757. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11758. (cdr range) t)
  11759. (setq key (org-match-string-no-properties 1)
  11760. value (org-trim (or (org-match-string-no-properties 2) "")))
  11761. (unless (member key excluded)
  11762. (push (cons key (or value "")) props)))))
  11763. (if clocksum
  11764. (push (cons "CLOCKSUM"
  11765. (org-columns-number-to-string (/ (float clocksum) 60.)
  11766. 'add_times))
  11767. props))
  11768. (unless (assoc "CATEGORY" props)
  11769. (setq value (or (org-get-category)
  11770. (progn (org-refresh-category-properties)
  11771. (org-get-category))))
  11772. (push (cons "CATEGORY" value) props))
  11773. (append sum-props (nreverse props)))))))
  11774. (defun org-entry-get (pom property &optional inherit)
  11775. "Get value of PROPERTY for entry at point-or-marker POM.
  11776. If INHERIT is non-nil and the entry does not have the property,
  11777. then also check higher levels of the hierarchy.
  11778. If INHERIT is the symbol `selective', use inheritance only if the setting
  11779. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11780. If the property is present but empty, the return value is the empty string.
  11781. If the property is not present at all, nil is returned."
  11782. (org-with-point-at pom
  11783. (if (and inherit (if (eq inherit 'selective)
  11784. (org-property-inherit-p property)
  11785. t))
  11786. (org-entry-get-with-inheritance property)
  11787. (if (member property org-special-properties)
  11788. ;; We need a special property. Use `org-entry-properties' to
  11789. ;; retrieve it, but specify the wanted property
  11790. (cdr (assoc property (org-entry-properties nil 'special property)))
  11791. (let ((range (org-get-property-block)))
  11792. (if (and range
  11793. (goto-char (car range))
  11794. (re-search-forward
  11795. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11796. (cdr range) t))
  11797. ;; Found the property, return it.
  11798. (if (match-end 1)
  11799. (org-match-string-no-properties 1)
  11800. "")))))))
  11801. (defun org-property-or-variable-value (var &optional inherit)
  11802. "Check if there is a property fixing the value of VAR.
  11803. If yes, return this value. If not, return the current value of the variable."
  11804. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11805. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11806. (read prop)
  11807. (symbol-value var))))
  11808. (defun org-entry-delete (pom property)
  11809. "Delete the property PROPERTY from entry at point-or-marker POM."
  11810. (org-with-point-at pom
  11811. (if (member property org-special-properties)
  11812. nil ; cannot delete these properties.
  11813. (let ((range (org-get-property-block)))
  11814. (if (and range
  11815. (goto-char (car range))
  11816. (re-search-forward
  11817. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11818. (cdr range) t))
  11819. (progn
  11820. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11821. t)
  11822. nil)))))
  11823. ;; Multi-values properties are properties that contain multiple values
  11824. ;; These values are assumed to be single words, separated by whitespace.
  11825. (defun org-entry-add-to-multivalued-property (pom property value)
  11826. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11827. (let* ((old (org-entry-get pom property))
  11828. (values (and old (org-split-string old "[ \t]"))))
  11829. (setq value (org-entry-protect-space value))
  11830. (unless (member value values)
  11831. (setq values (cons value values))
  11832. (org-entry-put pom property
  11833. (mapconcat 'identity values " ")))))
  11834. (defun org-entry-remove-from-multivalued-property (pom property value)
  11835. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11836. (let* ((old (org-entry-get pom property))
  11837. (values (and old (org-split-string old "[ \t]"))))
  11838. (setq value (org-entry-protect-space value))
  11839. (when (member value values)
  11840. (setq values (delete value values))
  11841. (org-entry-put pom property
  11842. (mapconcat 'identity values " ")))))
  11843. (defun org-entry-member-in-multivalued-property (pom property value)
  11844. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11845. (let* ((old (org-entry-get pom property))
  11846. (values (and old (org-split-string old "[ \t]"))))
  11847. (setq value (org-entry-protect-space value))
  11848. (member value values)))
  11849. (defun org-entry-get-multivalued-property (pom property)
  11850. "Return a list of values in a multivalued property."
  11851. (let* ((value (org-entry-get pom property))
  11852. (values (and value (org-split-string value "[ \t]"))))
  11853. (mapcar 'org-entry-restore-space values)))
  11854. (defun org-entry-put-multivalued-property (pom property &rest values)
  11855. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11856. VALUES should be a list of strings. Spaces will be protected."
  11857. (org-entry-put pom property
  11858. (mapconcat 'org-entry-protect-space values " "))
  11859. (let* ((value (org-entry-get pom property))
  11860. (values (and value (org-split-string value "[ \t]"))))
  11861. (mapcar 'org-entry-restore-space values)))
  11862. (defun org-entry-protect-space (s)
  11863. "Protect spaces and newline in string S."
  11864. (while (string-match " " s)
  11865. (setq s (replace-match "%20" t t s)))
  11866. (while (string-match "\n" s)
  11867. (setq s (replace-match "%0A" t t s)))
  11868. s)
  11869. (defun org-entry-restore-space (s)
  11870. "Restore spaces and newline in string S."
  11871. (while (string-match "%20" s)
  11872. (setq s (replace-match " " t t s)))
  11873. (while (string-match "%0A" s)
  11874. (setq s (replace-match "\n" t t s)))
  11875. s)
  11876. (defvar org-entry-property-inherited-from (make-marker)
  11877. "Marker pointing to the entry from where a property was inherited.
  11878. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11879. location of the entry where the inheritance search matched. If there was
  11880. no match, the marker will point nowhere.
  11881. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11882. is set.")
  11883. (defun org-entry-get-with-inheritance (property)
  11884. "Get entry property, and search higher levels if not present."
  11885. (move-marker org-entry-property-inherited-from nil)
  11886. (let (tmp)
  11887. (save-excursion
  11888. (save-restriction
  11889. (widen)
  11890. (catch 'ex
  11891. (while t
  11892. (when (setq tmp (org-entry-get nil property))
  11893. (org-back-to-heading t)
  11894. (move-marker org-entry-property-inherited-from (point))
  11895. (throw 'ex tmp))
  11896. (or (org-up-heading-safe) (throw 'ex nil)))))
  11897. (or tmp
  11898. (cdr (assoc property org-file-properties))
  11899. (cdr (assoc property org-global-properties))
  11900. (cdr (assoc property org-global-properties-fixed))))))
  11901. (defvar org-property-changed-functions nil
  11902. "Hook called when the value of a property has changed.
  11903. Each hook function should accept two arguments, the name of the property
  11904. and the new value.")
  11905. (defun org-entry-put (pom property value)
  11906. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11907. (org-with-point-at pom
  11908. (org-back-to-heading t)
  11909. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11910. range)
  11911. (cond
  11912. ((equal property "TODO")
  11913. (when (and (stringp value) (string-match "\\S-" value)
  11914. (not (member value org-todo-keywords-1)))
  11915. (error "\"%s\" is not a valid TODO state" value))
  11916. (if (or (not value)
  11917. (not (string-match "\\S-" value)))
  11918. (setq value 'none))
  11919. (org-todo value)
  11920. (org-set-tags nil 'align))
  11921. ((equal property "PRIORITY")
  11922. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11923. (string-to-char value) ?\ ))
  11924. (org-set-tags nil 'align))
  11925. ((equal property "SCHEDULED")
  11926. (if (re-search-forward org-scheduled-time-regexp end t)
  11927. (cond
  11928. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11929. ((eq value 'later) (org-timestamp-change 1 'day))
  11930. (t (call-interactively 'org-schedule)))
  11931. (call-interactively 'org-schedule)))
  11932. ((equal property "DEADLINE")
  11933. (if (re-search-forward org-deadline-time-regexp end t)
  11934. (cond
  11935. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11936. ((eq value 'later) (org-timestamp-change 1 'day))
  11937. (t (call-interactively 'org-deadline)))
  11938. (call-interactively 'org-deadline)))
  11939. ((member property org-special-properties)
  11940. (error "The %s property can not yet be set with `org-entry-put'"
  11941. property))
  11942. (t ; a non-special property
  11943. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11944. (setq range (org-get-property-block beg end 'force))
  11945. (goto-char (car range))
  11946. (if (re-search-forward
  11947. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11948. (progn
  11949. (delete-region (match-beginning 1) (match-end 1))
  11950. (goto-char (match-beginning 1)))
  11951. (goto-char (cdr range))
  11952. (insert "\n")
  11953. (backward-char 1)
  11954. (org-indent-line-function)
  11955. (insert ":" property ":"))
  11956. (and value (insert " " value))
  11957. (org-indent-line-function)))))
  11958. (run-hook-with-args 'org-property-changed-functions property value)))
  11959. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11960. "Get all property keys in the current buffer.
  11961. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11962. like tags and TODO state.
  11963. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11964. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11965. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11966. formats in the current buffer."
  11967. (let (rtn range cfmt s p)
  11968. (save-excursion
  11969. (save-restriction
  11970. (widen)
  11971. (goto-char (point-min))
  11972. (while (re-search-forward org-property-start-re nil t)
  11973. (setq range (org-get-property-block))
  11974. (goto-char (car range))
  11975. (while (re-search-forward
  11976. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11977. (cdr range) t)
  11978. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11979. (outline-next-heading))))
  11980. (when include-specials
  11981. (setq rtn (append org-special-properties rtn)))
  11982. (when include-defaults
  11983. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11984. (add-to-list 'rtn org-effort-property))
  11985. (when include-columns
  11986. (save-excursion
  11987. (save-restriction
  11988. (widen)
  11989. (goto-char (point-min))
  11990. (while (re-search-forward
  11991. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11992. nil t)
  11993. (setq cfmt (match-string 2) s 0)
  11994. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11995. cfmt s)
  11996. (setq s (match-end 0)
  11997. p (match-string 1 cfmt))
  11998. (unless (or (equal p "ITEM")
  11999. (member p org-special-properties))
  12000. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12001. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12002. (defun org-property-values (key)
  12003. "Return a list of all values of property KEY."
  12004. (save-excursion
  12005. (save-restriction
  12006. (widen)
  12007. (goto-char (point-min))
  12008. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12009. values)
  12010. (while (re-search-forward re nil t)
  12011. (add-to-list 'values (org-trim (match-string 1))))
  12012. (delete "" values)))))
  12013. (defun org-insert-property-drawer ()
  12014. "Insert a property drawer into the current entry."
  12015. (interactive)
  12016. (org-back-to-heading t)
  12017. (looking-at outline-regexp)
  12018. (let ((indent (if org-adapt-indentation
  12019. (- (match-end 0)(match-beginning 0))
  12020. 0))
  12021. (beg (point))
  12022. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12023. end hiddenp)
  12024. (outline-next-heading)
  12025. (setq end (point))
  12026. (goto-char beg)
  12027. (while (re-search-forward re end t))
  12028. (setq hiddenp (org-invisible-p))
  12029. (end-of-line 1)
  12030. (and (equal (char-after) ?\n) (forward-char 1))
  12031. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12032. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12033. ;; just skip this line
  12034. (beginning-of-line 2)
  12035. ;; Drawer start, find the end
  12036. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12037. (beginning-of-line 1)))
  12038. (org-skip-over-state-notes)
  12039. (skip-chars-backward " \t\n\r")
  12040. (if (eq (char-before) ?*) (forward-char 1))
  12041. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12042. (beginning-of-line 0)
  12043. (org-indent-to-column indent)
  12044. (beginning-of-line 2)
  12045. (org-indent-to-column indent)
  12046. (beginning-of-line 0)
  12047. (if hiddenp
  12048. (save-excursion
  12049. (org-back-to-heading t)
  12050. (hide-entry))
  12051. (org-flag-drawer t))))
  12052. (defun org-set-property (property value)
  12053. "In the current entry, set PROPERTY to VALUE.
  12054. When called interactively, this will prompt for a property name, offering
  12055. completion on existing and default properties. And then it will prompt
  12056. for a value, offering completion either on allowed values (via an inherited
  12057. xxx_ALL property) or on existing values in other instances of this property
  12058. in the current file."
  12059. (interactive
  12060. (let* ((completion-ignore-case t)
  12061. (keys (org-buffer-property-keys nil t t))
  12062. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12063. (prop (if (member prop0 keys)
  12064. prop0
  12065. (or (cdr (assoc (downcase prop0)
  12066. (mapcar (lambda (x) (cons (downcase x) x))
  12067. keys)))
  12068. prop0)))
  12069. (cur (org-entry-get nil prop))
  12070. (prompt (concat prop " value"
  12071. (if (and cur (string-match "\\S-" cur))
  12072. (concat " [" cur "]") "") ": "))
  12073. (allowed (org-property-get-allowed-values nil prop 'table))
  12074. (existing (mapcar 'list (org-property-values prop)))
  12075. (val (if allowed
  12076. (org-completing-read prompt allowed nil
  12077. (not (get-text-property 0 'org-unrestricted
  12078. (caar allowed))))
  12079. (let (org-completion-use-ido org-completion-use-iswitchb)
  12080. (org-completing-read prompt existing nil nil "" nil cur)))))
  12081. (list prop (if (equal val "") cur val))))
  12082. (unless (equal (org-entry-get nil property) value)
  12083. (org-entry-put nil property value)))
  12084. (defun org-delete-property (property)
  12085. "In the current entry, delete PROPERTY."
  12086. (interactive
  12087. (let* ((completion-ignore-case t)
  12088. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12089. (list prop)))
  12090. (message "Property %s %s" property
  12091. (if (org-entry-delete nil property)
  12092. "deleted"
  12093. "was not present in the entry")))
  12094. (defun org-delete-property-globally (property)
  12095. "Remove PROPERTY globally, from all entries."
  12096. (interactive
  12097. (let* ((completion-ignore-case t)
  12098. (prop (org-icompleting-read
  12099. "Globally remove property: "
  12100. (mapcar 'list (org-buffer-property-keys)))))
  12101. (list prop)))
  12102. (save-excursion
  12103. (save-restriction
  12104. (widen)
  12105. (goto-char (point-min))
  12106. (let ((cnt 0))
  12107. (while (re-search-forward
  12108. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12109. nil t)
  12110. (setq cnt (1+ cnt))
  12111. (replace-match ""))
  12112. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12113. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12114. (defun org-compute-property-at-point ()
  12115. "Compute the property at point.
  12116. This looks for an enclosing column format, extracts the operator and
  12117. then applies it to the property in the column format's scope."
  12118. (interactive)
  12119. (unless (org-at-property-p)
  12120. (error "Not at a property"))
  12121. (let ((prop (org-match-string-no-properties 2)))
  12122. (org-columns-get-format-and-top-level)
  12123. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12124. (error "No operator defined for property %s" prop))
  12125. (org-columns-compute prop)))
  12126. (defvar org-property-allowed-value-functions nil
  12127. "Hook for functions supplying allowed values for a specific property.
  12128. The functions must take a single argument, the name of the property, and
  12129. return a flat list of allowed values. If \":ETC\" is one of
  12130. the values, this means that these values are intended as defaults for
  12131. completion, but that other values should be allowed too.
  12132. The functions must return nil if they are not responsible for this
  12133. property.")
  12134. (defun org-property-get-allowed-values (pom property &optional table)
  12135. "Get allowed values for the property PROPERTY.
  12136. When TABLE is non-nil, return an alist that can directly be used for
  12137. completion."
  12138. (let (vals)
  12139. (cond
  12140. ((equal property "TODO")
  12141. (setq vals (org-with-point-at pom
  12142. (append org-todo-keywords-1 '("")))))
  12143. ((equal property "PRIORITY")
  12144. (let ((n org-lowest-priority))
  12145. (while (>= n org-highest-priority)
  12146. (push (char-to-string n) vals)
  12147. (setq n (1- n)))))
  12148. ((member property org-special-properties))
  12149. ((setq vals (run-hook-with-args-until-success
  12150. 'org-property-allowed-value-functions property)))
  12151. (t
  12152. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12153. (when (and vals (string-match "\\S-" vals))
  12154. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12155. (setq vals (mapcar (lambda (x)
  12156. (cond ((stringp x) x)
  12157. ((numberp x) (number-to-string x))
  12158. ((symbolp x) (symbol-name x))
  12159. (t "???")))
  12160. vals)))))
  12161. (when (member ":ETC" vals)
  12162. (setq vals (remove ":ETC" vals))
  12163. (org-add-props (car vals) '(org-unrestricted t)))
  12164. (if table (mapcar 'list vals) vals)))
  12165. (defun org-property-previous-allowed-value (&optional previous)
  12166. "Switch to the next allowed value for this property."
  12167. (interactive)
  12168. (org-property-next-allowed-value t))
  12169. (defun org-property-next-allowed-value (&optional previous)
  12170. "Switch to the next allowed value for this property."
  12171. (interactive)
  12172. (unless (org-at-property-p)
  12173. (error "Not at a property"))
  12174. (let* ((key (match-string 2))
  12175. (value (match-string 3))
  12176. (allowed (or (org-property-get-allowed-values (point) key)
  12177. (and (member value '("[ ]" "[-]" "[X]"))
  12178. '("[ ]" "[X]"))))
  12179. nval)
  12180. (unless allowed
  12181. (error "Allowed values for this property have not been defined"))
  12182. (if previous (setq allowed (reverse allowed)))
  12183. (if (member value allowed)
  12184. (setq nval (car (cdr (member value allowed)))))
  12185. (setq nval (or nval (car allowed)))
  12186. (if (equal nval value)
  12187. (error "Only one allowed value for this property"))
  12188. (org-at-property-p)
  12189. (replace-match (concat " :" key ": " nval) t t)
  12190. (org-indent-line-function)
  12191. (beginning-of-line 1)
  12192. (skip-chars-forward " \t")
  12193. (run-hook-with-args 'org-property-changed-functions key nval)))
  12194. (defun org-find-entry-with-id (ident)
  12195. "Locate the entry that contains the ID property with exact value IDENT.
  12196. IDENT can be a string, a symbol or a number, this function will search for
  12197. the string representation of it.
  12198. Return the position where this entry starts, or nil if there is no such entry."
  12199. (interactive "sID: ")
  12200. (let ((id (cond
  12201. ((stringp ident) ident)
  12202. ((symbol-name ident) (symbol-name ident))
  12203. ((numberp ident) (number-to-string ident))
  12204. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12205. (case-fold-search nil))
  12206. (save-excursion
  12207. (save-restriction
  12208. (widen)
  12209. (goto-char (point-min))
  12210. (when (re-search-forward
  12211. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12212. nil t)
  12213. (org-back-to-heading t)
  12214. (point))))))
  12215. ;;;; Timestamps
  12216. (defvar org-last-changed-timestamp nil)
  12217. (defvar org-last-inserted-timestamp nil
  12218. "The last time stamp inserted with `org-insert-time-stamp'.")
  12219. (defvar org-time-was-given) ; dynamically scoped parameter
  12220. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12221. (defvar org-ts-what) ; dynamically scoped parameter
  12222. (defun org-time-stamp (arg &optional inactive)
  12223. "Prompt for a date/time and insert a time stamp.
  12224. If the user specifies a time like HH:MM, or if this command is called
  12225. with a prefix argument, the time stamp will contain date and time.
  12226. Otherwise, only the date will be included. All parts of a date not
  12227. specified by the user will be filled in from the current date/time.
  12228. So if you press just return without typing anything, the time stamp
  12229. will represent the current date/time. If there is already a timestamp
  12230. at the cursor, it will be modified."
  12231. (interactive "P")
  12232. (let* ((ts nil)
  12233. (default-time
  12234. ;; Default time is either today, or, when entering a range,
  12235. ;; the range start.
  12236. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12237. (save-excursion
  12238. (re-search-backward
  12239. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12240. (- (point) 20) t)))
  12241. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12242. (current-time)))
  12243. (default-input (and ts (org-get-compact-tod ts)))
  12244. org-time-was-given org-end-time-was-given time)
  12245. (cond
  12246. ((and (org-at-timestamp-p t)
  12247. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12248. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12249. (insert "--")
  12250. (setq time (let ((this-command this-command))
  12251. (org-read-date arg 'totime nil nil
  12252. default-time default-input)))
  12253. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12254. ((org-at-timestamp-p t)
  12255. (setq time (let ((this-command this-command))
  12256. (org-read-date arg 'totime nil nil default-time default-input)))
  12257. (when (org-at-timestamp-p t) ; just to get the match data
  12258. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12259. (replace-match "")
  12260. (setq org-last-changed-timestamp
  12261. (org-insert-time-stamp
  12262. time (or org-time-was-given arg)
  12263. inactive nil nil (list org-end-time-was-given))))
  12264. (message "Timestamp updated"))
  12265. (t
  12266. (setq time (let ((this-command this-command))
  12267. (org-read-date arg 'totime nil nil default-time default-input)))
  12268. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12269. nil nil (list org-end-time-was-given))))))
  12270. ;; FIXME: can we use this for something else, like computing time differences?
  12271. (defun org-get-compact-tod (s)
  12272. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12273. (let* ((t1 (match-string 1 s))
  12274. (h1 (string-to-number (match-string 2 s)))
  12275. (m1 (string-to-number (match-string 3 s)))
  12276. (t2 (and (match-end 4) (match-string 5 s)))
  12277. (h2 (and t2 (string-to-number (match-string 6 s))))
  12278. (m2 (and t2 (string-to-number (match-string 7 s))))
  12279. dh dm)
  12280. (if (not t2)
  12281. t1
  12282. (setq dh (- h2 h1) dm (- m2 m1))
  12283. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12284. (concat t1 "+" (number-to-string dh)
  12285. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12286. (defun org-time-stamp-inactive (&optional arg)
  12287. "Insert an inactive time stamp.
  12288. An inactive time stamp is enclosed in square brackets instead of angle
  12289. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12290. does not link to the calendar and cannot be changed with the S-cursor keys.
  12291. So these are more for recording a certain time/date."
  12292. (interactive "P")
  12293. (org-time-stamp arg 'inactive))
  12294. (defvar org-date-ovl (org-make-overlay 1 1))
  12295. (org-overlay-put org-date-ovl 'face 'org-warning)
  12296. (org-detach-overlay org-date-ovl)
  12297. (defvar org-ans1) ; dynamically scoped parameter
  12298. (defvar org-ans2) ; dynamically scoped parameter
  12299. (defvar org-plain-time-of-day-regexp) ; defined below
  12300. (defvar org-overriding-default-time nil) ; dynamically scoped
  12301. (defvar org-read-date-overlay nil)
  12302. (defvar org-dcst nil) ; dynamically scoped
  12303. (defvar org-read-date-history nil)
  12304. (defvar org-read-date-final-answer nil)
  12305. (defun org-read-date (&optional with-time to-time from-string prompt
  12306. default-time default-input)
  12307. "Read a date, possibly a time, and make things smooth for the user.
  12308. The prompt will suggest to enter an ISO date, but you can also enter anything
  12309. which will at least partially be understood by `parse-time-string'.
  12310. Unrecognized parts of the date will default to the current day, month, year,
  12311. hour and minute. If this command is called to replace a timestamp at point,
  12312. of to enter the second timestamp of a range, the default time is taken from the
  12313. existing stamp. For example,
  12314. 3-2-5 --> 2003-02-05
  12315. feb 15 --> currentyear-02-15
  12316. sep 12 9 --> 2009-09-12
  12317. 12:45 --> today 12:45
  12318. 22 sept 0:34 --> currentyear-09-22 0:34
  12319. 12 --> currentyear-currentmonth-12
  12320. Fri --> nearest Friday (today or later)
  12321. etc.
  12322. Furthermore you can specify a relative date by giving, as the *first* thing
  12323. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12324. change in days weeks, months, years.
  12325. With a single plus or minus, the date is relative to today. With a double
  12326. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12327. +4d --> four days from today
  12328. +4 --> same as above
  12329. +2w --> two weeks from today
  12330. ++5 --> five days from default date
  12331. The function understands only English month and weekday abbreviations,
  12332. but this can be configured with the variables `parse-time-months' and
  12333. `parse-time-weekdays'.
  12334. While prompting, a calendar is popped up - you can also select the
  12335. date with the mouse (button 1). The calendar shows a period of three
  12336. months. To scroll it to other months, use the keys `>' and `<'.
  12337. If you don't like the calendar, turn it off with
  12338. \(setq org-read-date-popup-calendar nil)
  12339. With optional argument TO-TIME, the date will immediately be converted
  12340. to an internal time.
  12341. With an optional argument WITH-TIME, the prompt will suggest to also
  12342. insert a time. Note that when WITH-TIME is not set, you can still
  12343. enter a time, and this function will inform the calling routine about
  12344. this change. The calling routine may then choose to change the format
  12345. used to insert the time stamp into the buffer to include the time.
  12346. With optional argument FROM-STRING, read from this string instead from
  12347. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12348. the time/date that is used for everything that is not specified by the
  12349. user."
  12350. (require 'parse-time)
  12351. (let* ((org-time-stamp-rounding-minutes
  12352. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12353. (org-dcst org-display-custom-times)
  12354. (ct (org-current-time))
  12355. (def (or org-overriding-default-time default-time ct))
  12356. (defdecode (decode-time def))
  12357. (dummy (progn
  12358. (when (< (nth 2 defdecode) org-extend-today-until)
  12359. (setcar (nthcdr 2 defdecode) -1)
  12360. (setcar (nthcdr 1 defdecode) 59)
  12361. (setq def (apply 'encode-time defdecode)
  12362. defdecode (decode-time def)))))
  12363. (calendar-frame-setup nil)
  12364. (calendar-move-hook nil)
  12365. (calendar-view-diary-initially-flag nil)
  12366. (view-diary-entries-initially nil)
  12367. (calendar-view-holidays-initially-flag nil)
  12368. (view-calendar-holidays-initially nil)
  12369. (timestr (format-time-string
  12370. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12371. (prompt (concat (if prompt (concat prompt " ") "")
  12372. (format "Date+time [%s]: " timestr)))
  12373. ans (org-ans0 "") org-ans1 org-ans2 final)
  12374. (cond
  12375. (from-string (setq ans from-string))
  12376. (org-read-date-popup-calendar
  12377. (save-excursion
  12378. (save-window-excursion
  12379. (calendar)
  12380. (calendar-forward-day (- (time-to-days def)
  12381. (calendar-absolute-from-gregorian
  12382. (calendar-current-date))))
  12383. (org-eval-in-calendar nil t)
  12384. (let* ((old-map (current-local-map))
  12385. (map (copy-keymap calendar-mode-map))
  12386. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12387. (org-defkey map (kbd "RET") 'org-calendar-select)
  12388. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12389. 'org-calendar-select-mouse)
  12390. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12391. 'org-calendar-select-mouse)
  12392. (org-defkey minibuffer-local-map [(meta shift left)]
  12393. (lambda () (interactive)
  12394. (org-eval-in-calendar '(calendar-backward-month 1))))
  12395. (org-defkey minibuffer-local-map [(meta shift right)]
  12396. (lambda () (interactive)
  12397. (org-eval-in-calendar '(calendar-forward-month 1))))
  12398. (org-defkey minibuffer-local-map [(meta shift up)]
  12399. (lambda () (interactive)
  12400. (org-eval-in-calendar '(calendar-backward-year 1))))
  12401. (org-defkey minibuffer-local-map [(meta shift down)]
  12402. (lambda () (interactive)
  12403. (org-eval-in-calendar '(calendar-forward-year 1))))
  12404. (org-defkey minibuffer-local-map [?\e (shift left)]
  12405. (lambda () (interactive)
  12406. (org-eval-in-calendar '(calendar-backward-month 1))))
  12407. (org-defkey minibuffer-local-map [?\e (shift right)]
  12408. (lambda () (interactive)
  12409. (org-eval-in-calendar '(calendar-forward-month 1))))
  12410. (org-defkey minibuffer-local-map [?\e (shift up)]
  12411. (lambda () (interactive)
  12412. (org-eval-in-calendar '(calendar-backward-year 1))))
  12413. (org-defkey minibuffer-local-map [?\e (shift down)]
  12414. (lambda () (interactive)
  12415. (org-eval-in-calendar '(calendar-forward-year 1))))
  12416. (org-defkey minibuffer-local-map [(shift up)]
  12417. (lambda () (interactive)
  12418. (org-eval-in-calendar '(calendar-backward-week 1))))
  12419. (org-defkey minibuffer-local-map [(shift down)]
  12420. (lambda () (interactive)
  12421. (org-eval-in-calendar '(calendar-forward-week 1))))
  12422. (org-defkey minibuffer-local-map [(shift left)]
  12423. (lambda () (interactive)
  12424. (org-eval-in-calendar '(calendar-backward-day 1))))
  12425. (org-defkey minibuffer-local-map [(shift right)]
  12426. (lambda () (interactive)
  12427. (org-eval-in-calendar '(calendar-forward-day 1))))
  12428. (org-defkey minibuffer-local-map ">"
  12429. (lambda () (interactive)
  12430. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12431. (org-defkey minibuffer-local-map "<"
  12432. (lambda () (interactive)
  12433. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12434. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12435. (unwind-protect
  12436. (progn
  12437. (use-local-map map)
  12438. (add-hook 'post-command-hook 'org-read-date-display)
  12439. (setq org-ans0 (read-string prompt default-input
  12440. 'org-read-date-history nil))
  12441. ;; org-ans0: from prompt
  12442. ;; org-ans1: from mouse click
  12443. ;; org-ans2: from calendar motion
  12444. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12445. (remove-hook 'post-command-hook 'org-read-date-display)
  12446. (use-local-map old-map)
  12447. (when org-read-date-overlay
  12448. (org-delete-overlay org-read-date-overlay)
  12449. (setq org-read-date-overlay nil)))))))
  12450. (t ; Naked prompt only
  12451. (unwind-protect
  12452. (setq ans (read-string prompt default-input
  12453. 'org-read-date-history timestr))
  12454. (when org-read-date-overlay
  12455. (org-delete-overlay org-read-date-overlay)
  12456. (setq org-read-date-overlay nil)))))
  12457. (setq final (org-read-date-analyze ans def defdecode))
  12458. (setq org-read-date-final-answer ans)
  12459. (if to-time
  12460. (apply 'encode-time final)
  12461. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12462. (format "%04d-%02d-%02d %02d:%02d"
  12463. (nth 5 final) (nth 4 final) (nth 3 final)
  12464. (nth 2 final) (nth 1 final))
  12465. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12466. (defvar def)
  12467. (defvar defdecode)
  12468. (defvar with-time)
  12469. (defvar org-read-date-analyze-futurep nil)
  12470. (defun org-read-date-display ()
  12471. "Display the current date prompt interpretation in the minibuffer."
  12472. (when org-read-date-display-live
  12473. (when org-read-date-overlay
  12474. (org-delete-overlay org-read-date-overlay))
  12475. (let ((p (point)))
  12476. (end-of-line 1)
  12477. (while (not (equal (buffer-substring
  12478. (max (point-min) (- (point) 4)) (point))
  12479. " "))
  12480. (insert " "))
  12481. (goto-char p))
  12482. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12483. " " (or org-ans1 org-ans2)))
  12484. (org-end-time-was-given nil)
  12485. (f (org-read-date-analyze ans def defdecode))
  12486. (fmts (if org-dcst
  12487. org-time-stamp-custom-formats
  12488. org-time-stamp-formats))
  12489. (fmt (if (or with-time
  12490. (and (boundp 'org-time-was-given) org-time-was-given))
  12491. (cdr fmts)
  12492. (car fmts)))
  12493. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12494. (when (and org-end-time-was-given
  12495. (string-match org-plain-time-of-day-regexp txt))
  12496. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12497. org-end-time-was-given
  12498. (substring txt (match-end 0)))))
  12499. (when org-read-date-analyze-futurep
  12500. (setq txt (concat txt " (=>F)")))
  12501. (setq org-read-date-overlay
  12502. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12503. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12504. (defun org-read-date-analyze (ans def defdecode)
  12505. "Analyse the combined answer of the date prompt."
  12506. ;; FIXME: cleanup and comment
  12507. (let ((nowdecode (decode-time (current-time)))
  12508. delta deltan deltaw deltadef year month day
  12509. hour minute second wday pm h2 m2 tl wday1
  12510. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12511. (setq org-read-date-analyze-futurep nil)
  12512. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12513. (setq ans "+0"))
  12514. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12515. (setq ans (replace-match "" t t ans)
  12516. deltan (car delta)
  12517. deltaw (nth 1 delta)
  12518. deltadef (nth 2 delta)))
  12519. ;; Check if there is an iso week date in there
  12520. ;; If yes, store the info and postpone interpreting it until the rest
  12521. ;; of the parsing is done
  12522. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12523. (setq iso-year (if (match-end 1)
  12524. (org-small-year-to-year
  12525. (string-to-number (match-string 1 ans))))
  12526. iso-weekday (if (match-end 3)
  12527. (string-to-number (match-string 3 ans)))
  12528. iso-week (string-to-number (match-string 2 ans)))
  12529. (setq ans (replace-match "" t t ans)))
  12530. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12531. (when (string-match
  12532. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12533. (setq year (if (match-end 2)
  12534. (string-to-number (match-string 2 ans))
  12535. (progn (setq kill-year t)
  12536. (string-to-number (format-time-string "%Y"))))
  12537. month (string-to-number (match-string 3 ans))
  12538. day (string-to-number (match-string 4 ans)))
  12539. (if (< year 100) (setq year (+ 2000 year)))
  12540. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12541. t nil ans)))
  12542. ;; Help matching american dates, like 5/30 or 5/30/7
  12543. (when (string-match
  12544. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12545. (setq year (if (match-end 4)
  12546. (string-to-number (match-string 4 ans))
  12547. (progn (setq kill-year t)
  12548. (string-to-number (format-time-string "%Y"))))
  12549. month (string-to-number (match-string 1 ans))
  12550. day (string-to-number (match-string 2 ans)))
  12551. (if (< year 100) (setq year (+ 2000 year)))
  12552. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12553. t nil ans)))
  12554. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12555. ;; If there is a time with am/pm, and *no* time without it, we convert
  12556. ;; so that matching will be successful.
  12557. (loop for i from 1 to 2 do ; twice, for end time as well
  12558. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12559. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12560. (setq hour (string-to-number (match-string 1 ans))
  12561. minute (if (match-end 3)
  12562. (string-to-number (match-string 3 ans))
  12563. 0)
  12564. pm (equal ?p
  12565. (string-to-char (downcase (match-string 4 ans)))))
  12566. (if (and (= hour 12) (not pm))
  12567. (setq hour 0)
  12568. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12569. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12570. t t ans))))
  12571. ;; Check if a time range is given as a duration
  12572. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12573. (setq hour (string-to-number (match-string 1 ans))
  12574. h2 (+ hour (string-to-number (match-string 3 ans)))
  12575. minute (string-to-number (match-string 2 ans))
  12576. m2 (+ minute (if (match-end 5) (string-to-number
  12577. (match-string 5 ans))0)))
  12578. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12579. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12580. t t ans)))
  12581. ;; Check if there is a time range
  12582. (when (boundp 'org-end-time-was-given)
  12583. (setq org-time-was-given nil)
  12584. (when (and (string-match org-plain-time-of-day-regexp ans)
  12585. (match-end 8))
  12586. (setq org-end-time-was-given (match-string 8 ans))
  12587. (setq ans (concat (substring ans 0 (match-beginning 7))
  12588. (substring ans (match-end 7))))))
  12589. (setq tl (parse-time-string ans)
  12590. day (or (nth 3 tl) (nth 3 defdecode))
  12591. month (or (nth 4 tl)
  12592. (if (and org-read-date-prefer-future
  12593. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12594. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12595. (nth 4 defdecode)))
  12596. year (or (and (not kill-year) (nth 5 tl))
  12597. (if (and org-read-date-prefer-future
  12598. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12599. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12600. (nth 5 defdecode)))
  12601. hour (or (nth 2 tl) (nth 2 defdecode))
  12602. minute (or (nth 1 tl) (nth 1 defdecode))
  12603. second (or (nth 0 tl) 0)
  12604. wday (nth 6 tl))
  12605. (when (and (eq org-read-date-prefer-future 'time)
  12606. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12607. (equal day (nth 3 nowdecode))
  12608. (equal month (nth 4 nowdecode))
  12609. (equal year (nth 5 nowdecode))
  12610. (nth 2 tl)
  12611. (or (< (nth 2 tl) (nth 2 nowdecode))
  12612. (and (= (nth 2 tl) (nth 2 nowdecode))
  12613. (nth 1 tl)
  12614. (< (nth 1 tl) (nth 1 nowdecode)))))
  12615. (setq day (1+ day)
  12616. futurep t))
  12617. ;; Special date definitions below
  12618. (cond
  12619. (iso-week
  12620. ;; There was an iso week
  12621. (require 'cal-iso)
  12622. (setq futurep nil)
  12623. (setq year (or iso-year year)
  12624. day (or iso-weekday wday 1)
  12625. wday nil ; to make sure that the trigger below does not match
  12626. iso-date (calendar-gregorian-from-absolute
  12627. (calendar-absolute-from-iso
  12628. (list iso-week day year))))
  12629. ; FIXME: Should we also push ISO weeks into the future?
  12630. ; (when (and org-read-date-prefer-future
  12631. ; (not iso-year)
  12632. ; (< (calendar-absolute-from-gregorian iso-date)
  12633. ; (time-to-days (current-time))))
  12634. ; (setq year (1+ year)
  12635. ; iso-date (calendar-gregorian-from-absolute
  12636. ; (calendar-absolute-from-iso
  12637. ; (list iso-week day year)))))
  12638. (setq month (car iso-date)
  12639. year (nth 2 iso-date)
  12640. day (nth 1 iso-date)))
  12641. (deltan
  12642. (setq futurep nil)
  12643. (unless deltadef
  12644. (let ((now (decode-time (current-time))))
  12645. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12646. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12647. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12648. ((equal deltaw "m") (setq month (+ month deltan)))
  12649. ((equal deltaw "y") (setq year (+ year deltan)))))
  12650. ((and wday (not (nth 3 tl)))
  12651. (setq futurep nil)
  12652. ;; Weekday was given, but no day, so pick that day in the week
  12653. ;; on or after the derived date.
  12654. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12655. (unless (equal wday wday1)
  12656. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12657. (if (and (boundp 'org-time-was-given)
  12658. (nth 2 tl))
  12659. (setq org-time-was-given t))
  12660. (if (< year 100) (setq year (+ 2000 year)))
  12661. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12662. (setq org-read-date-analyze-futurep futurep)
  12663. (list second minute hour day month year)))
  12664. (defvar parse-time-weekdays)
  12665. (defun org-read-date-get-relative (s today default)
  12666. "Check string S for special relative date string.
  12667. TODAY and DEFAULT are internal times, for today and for a default.
  12668. Return shift list (N what def-flag)
  12669. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12670. N is the number of WHATs to shift.
  12671. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12672. the DEFAULT date rather than TODAY."
  12673. (when (and
  12674. (string-match
  12675. (concat
  12676. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12677. "\\([0-9]+\\)?"
  12678. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12679. "\\([ \t]\\|$\\)") s)
  12680. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12681. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12682. (string-to-char (substring (match-string 1 s) -1))
  12683. ?+))
  12684. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12685. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12686. (what (if (match-end 3) (match-string 3 s) "d"))
  12687. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12688. (date (if rel default today))
  12689. (wday (nth 6 (decode-time date)))
  12690. delta)
  12691. (if wday1
  12692. (progn
  12693. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12694. (if (= dir ?-) (setq delta (- delta 7)))
  12695. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12696. (list delta "d" rel))
  12697. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12698. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12699. "Turn a user-specified date into the internal representation.
  12700. The internal representation needed by the calendar is (month day year).
  12701. This is a wrapper to handle the brain-dead convention in calendar that
  12702. user function argument order change dependent on argument order."
  12703. (if (boundp 'calendar-date-style)
  12704. (cond
  12705. ((eq calendar-date-style 'american)
  12706. (list arg1 arg2 arg3))
  12707. ((eq calendar-date-style 'european)
  12708. (list arg2 arg1 arg3))
  12709. ((eq calendar-date-style 'iso)
  12710. (list arg2 arg3 arg1)))
  12711. (if (org-bound-and-true-p european-calendar-style)
  12712. (list arg2 arg1 arg3)
  12713. (list arg1 arg2 arg3))))
  12714. (defun org-eval-in-calendar (form &optional keepdate)
  12715. "Eval FORM in the calendar window and return to current window.
  12716. Also, store the cursor date in variable org-ans2."
  12717. (let ((sf (selected-frame))
  12718. (sw (selected-window)))
  12719. (select-window (get-buffer-window "*Calendar*" t))
  12720. (eval form)
  12721. (when (and (not keepdate) (calendar-cursor-to-date))
  12722. (let* ((date (calendar-cursor-to-date))
  12723. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12724. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12725. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12726. (select-window sw)
  12727. (org-select-frame-set-input-focus sf)))
  12728. (defun org-calendar-select ()
  12729. "Return to `org-read-date' with the date currently selected.
  12730. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12731. (interactive)
  12732. (when (calendar-cursor-to-date)
  12733. (let* ((date (calendar-cursor-to-date))
  12734. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12735. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12736. (if (active-minibuffer-window) (exit-minibuffer))))
  12737. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12738. "Insert a date stamp for the date given by the internal TIME.
  12739. WITH-HM means use the stamp format that includes the time of the day.
  12740. INACTIVE means use square brackets instead of angular ones, so that the
  12741. stamp will not contribute to the agenda.
  12742. PRE and POST are optional strings to be inserted before and after the
  12743. stamp.
  12744. The command returns the inserted time stamp."
  12745. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12746. stamp)
  12747. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12748. (insert-before-markers (or pre ""))
  12749. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12750. (when (listp extra)
  12751. (setq extra (car extra))
  12752. (if (and (stringp extra)
  12753. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12754. (setq extra (format "-%02d:%02d"
  12755. (string-to-number (match-string 1 extra))
  12756. (string-to-number (match-string 2 extra))))
  12757. (setq extra nil)))
  12758. (when extra
  12759. (backward-char 1)
  12760. (insert-before-markers extra)
  12761. (forward-char 1))
  12762. (insert-before-markers (or post ""))
  12763. (setq org-last-inserted-timestamp stamp)))
  12764. (defun org-toggle-time-stamp-overlays ()
  12765. "Toggle the use of custom time stamp formats."
  12766. (interactive)
  12767. (setq org-display-custom-times (not org-display-custom-times))
  12768. (unless org-display-custom-times
  12769. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12770. (while (setq p (next-single-property-change p 'display))
  12771. (if (and (get-text-property p 'display)
  12772. (eq (get-text-property p 'face) 'org-date))
  12773. (remove-text-properties
  12774. p (setq p (next-single-property-change p 'display))
  12775. '(display t))))
  12776. (set-buffer-modified-p bmp)))
  12777. (if (featurep 'xemacs)
  12778. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12779. (org-restart-font-lock)
  12780. (setq org-table-may-need-update t)
  12781. (if org-display-custom-times
  12782. (message "Time stamps are overlayed with custom format")
  12783. (message "Time stamp overlays removed")))
  12784. (defun org-display-custom-time (beg end)
  12785. "Overlay modified time stamp format over timestamp between BEG and END."
  12786. (let* ((ts (buffer-substring beg end))
  12787. t1 w1 with-hm tf time str w2 (off 0))
  12788. (save-match-data
  12789. (setq t1 (org-parse-time-string ts t))
  12790. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12791. (setq off (- (match-end 0) (match-beginning 0)))))
  12792. (setq end (- end off))
  12793. (setq w1 (- end beg)
  12794. with-hm (and (nth 1 t1) (nth 2 t1))
  12795. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12796. time (org-fix-decoded-time t1)
  12797. str (org-add-props
  12798. (format-time-string
  12799. (substring tf 1 -1) (apply 'encode-time time))
  12800. nil 'mouse-face 'highlight)
  12801. w2 (length str))
  12802. (if (not (= w2 w1))
  12803. (add-text-properties (1+ beg) (+ 2 beg)
  12804. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12805. (if (featurep 'xemacs)
  12806. (progn
  12807. (put-text-property beg end 'invisible t)
  12808. (put-text-property beg end 'end-glyph (make-glyph str)))
  12809. (put-text-property beg end 'display str))))
  12810. (defun org-translate-time (string)
  12811. "Translate all timestamps in STRING to custom format.
  12812. But do this only if the variable `org-display-custom-times' is set."
  12813. (when org-display-custom-times
  12814. (save-match-data
  12815. (let* ((start 0)
  12816. (re org-ts-regexp-both)
  12817. t1 with-hm inactive tf time str beg end)
  12818. (while (setq start (string-match re string start))
  12819. (setq beg (match-beginning 0)
  12820. end (match-end 0)
  12821. t1 (save-match-data
  12822. (org-parse-time-string (substring string beg end) t))
  12823. with-hm (and (nth 1 t1) (nth 2 t1))
  12824. inactive (equal (substring string beg (1+ beg)) "[")
  12825. tf (funcall (if with-hm 'cdr 'car)
  12826. org-time-stamp-custom-formats)
  12827. time (org-fix-decoded-time t1)
  12828. str (format-time-string
  12829. (concat
  12830. (if inactive "[" "<") (substring tf 1 -1)
  12831. (if inactive "]" ">"))
  12832. (apply 'encode-time time))
  12833. string (replace-match str t t string)
  12834. start (+ start (length str)))))))
  12835. string)
  12836. (defun org-fix-decoded-time (time)
  12837. "Set 0 instead of nil for the first 6 elements of time.
  12838. Don't touch the rest."
  12839. (let ((n 0))
  12840. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12841. (defun org-days-to-time (timestamp-string)
  12842. "Difference between TIMESTAMP-STRING and now in days."
  12843. (- (time-to-days (org-time-string-to-time timestamp-string))
  12844. (time-to-days (current-time))))
  12845. (defun org-deadline-close (timestamp-string &optional ndays)
  12846. "Is the time in TIMESTAMP-STRING close to the current date?"
  12847. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12848. (and (< (org-days-to-time timestamp-string) ndays)
  12849. (not (org-entry-is-done-p))))
  12850. (defun org-get-wdays (ts)
  12851. "Get the deadline lead time appropriate for timestring TS."
  12852. (cond
  12853. ((<= org-deadline-warning-days 0)
  12854. ;; 0 or negative, enforce this value no matter what
  12855. (- org-deadline-warning-days))
  12856. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12857. ;; lead time is specified.
  12858. (floor (* (string-to-number (match-string 1 ts))
  12859. (cdr (assoc (match-string 2 ts)
  12860. '(("d" . 1) ("w" . 7)
  12861. ("m" . 30.4) ("y" . 365.25)))))))
  12862. ;; go for the default.
  12863. (t org-deadline-warning-days)))
  12864. (defun org-calendar-select-mouse (ev)
  12865. "Return to `org-read-date' with the date currently selected.
  12866. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12867. (interactive "e")
  12868. (mouse-set-point ev)
  12869. (when (calendar-cursor-to-date)
  12870. (let* ((date (calendar-cursor-to-date))
  12871. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12872. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12873. (if (active-minibuffer-window) (exit-minibuffer))))
  12874. (defun org-check-deadlines (ndays)
  12875. "Check if there are any deadlines due or past due.
  12876. A deadline is considered due if it happens within `org-deadline-warning-days'
  12877. days from today's date. If the deadline appears in an entry marked DONE,
  12878. it is not shown. The prefix arg NDAYS can be used to test that many
  12879. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12880. (interactive "P")
  12881. (let* ((org-warn-days
  12882. (cond
  12883. ((equal ndays '(4)) 100000)
  12884. (ndays (prefix-numeric-value ndays))
  12885. (t (abs org-deadline-warning-days))))
  12886. (case-fold-search nil)
  12887. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12888. (callback
  12889. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12890. (message "%d deadlines past-due or due within %d days"
  12891. (org-occur regexp nil callback)
  12892. org-warn-days)))
  12893. (defun org-check-before-date (date)
  12894. "Check if there are deadlines or scheduled entries before DATE."
  12895. (interactive (list (org-read-date)))
  12896. (let ((case-fold-search nil)
  12897. (regexp (concat "\\<\\(" org-deadline-string
  12898. "\\|" org-scheduled-string
  12899. "\\) *<\\([^>]+\\)>"))
  12900. (callback
  12901. (lambda () (time-less-p
  12902. (org-time-string-to-time (match-string 2))
  12903. (org-time-string-to-time date)))))
  12904. (message "%d entries before %s"
  12905. (org-occur regexp nil callback) date)))
  12906. (defun org-check-after-date (date)
  12907. "Check if there are deadlines or scheduled entries after DATE."
  12908. (interactive (list (org-read-date)))
  12909. (let ((case-fold-search nil)
  12910. (regexp (concat "\\<\\(" org-deadline-string
  12911. "\\|" org-scheduled-string
  12912. "\\) *<\\([^>]+\\)>"))
  12913. (callback
  12914. (lambda () (not
  12915. (time-less-p
  12916. (org-time-string-to-time (match-string 2))
  12917. (org-time-string-to-time date))))))
  12918. (message "%d entries after %s"
  12919. (org-occur regexp nil callback) date)))
  12920. (defun org-evaluate-time-range (&optional to-buffer)
  12921. "Evaluate a time range by computing the difference between start and end.
  12922. Normally the result is just printed in the echo area, but with prefix arg
  12923. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12924. If the time range is actually in a table, the result is inserted into the
  12925. next column.
  12926. For time difference computation, a year is assumed to be exactly 365
  12927. days in order to avoid rounding problems."
  12928. (interactive "P")
  12929. (or
  12930. (org-clock-update-time-maybe)
  12931. (save-excursion
  12932. (unless (org-at-date-range-p t)
  12933. (goto-char (point-at-bol))
  12934. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12935. (if (not (org-at-date-range-p t))
  12936. (error "Not at a time-stamp range, and none found in current line")))
  12937. (let* ((ts1 (match-string 1))
  12938. (ts2 (match-string 2))
  12939. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12940. (match-end (match-end 0))
  12941. (time1 (org-time-string-to-time ts1))
  12942. (time2 (org-time-string-to-time ts2))
  12943. (t1 (org-float-time time1))
  12944. (t2 (org-float-time time2))
  12945. (diff (abs (- t2 t1)))
  12946. (negative (< (- t2 t1) 0))
  12947. ;; (ys (floor (* 365 24 60 60)))
  12948. (ds (* 24 60 60))
  12949. (hs (* 60 60))
  12950. (fy "%dy %dd %02d:%02d")
  12951. (fy1 "%dy %dd")
  12952. (fd "%dd %02d:%02d")
  12953. (fd1 "%dd")
  12954. (fh "%02d:%02d")
  12955. y d h m align)
  12956. (if havetime
  12957. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12958. y 0
  12959. d (floor (/ diff ds)) diff (mod diff ds)
  12960. h (floor (/ diff hs)) diff (mod diff hs)
  12961. m (floor (/ diff 60)))
  12962. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12963. y 0
  12964. d (floor (+ (/ diff ds) 0.5))
  12965. h 0 m 0))
  12966. (if (not to-buffer)
  12967. (message "%s" (org-make-tdiff-string y d h m))
  12968. (if (org-at-table-p)
  12969. (progn
  12970. (goto-char match-end)
  12971. (setq align t)
  12972. (and (looking-at " *|") (goto-char (match-end 0))))
  12973. (goto-char match-end))
  12974. (if (looking-at
  12975. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12976. (replace-match ""))
  12977. (if negative (insert " -"))
  12978. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12979. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12980. (insert " " (format fh h m))))
  12981. (if align (org-table-align))
  12982. (message "Time difference inserted")))))
  12983. (defun org-make-tdiff-string (y d h m)
  12984. (let ((fmt "")
  12985. (l nil))
  12986. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12987. l (push y l)))
  12988. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12989. l (push d l)))
  12990. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12991. l (push h l)))
  12992. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12993. l (push m l)))
  12994. (apply 'format fmt (nreverse l))))
  12995. (defun org-time-string-to-time (s)
  12996. (apply 'encode-time (org-parse-time-string s)))
  12997. (defun org-time-string-to-seconds (s)
  12998. (org-float-time (org-time-string-to-time s)))
  12999. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13000. "Convert a time stamp to an absolute day number.
  13001. If there is a specifyer for a cyclic time stamp, get the closest date to
  13002. DAYNR.
  13003. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13004. the variable date is bound by the calendar when this is called."
  13005. (cond
  13006. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13007. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13008. daynr
  13009. (+ daynr 1000)))
  13010. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13011. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13012. (time-to-days (current-time))) (match-string 0 s)
  13013. prefer show-all))
  13014. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13015. (defun org-days-to-iso-week (days)
  13016. "Return the iso week number."
  13017. (require 'cal-iso)
  13018. (car (calendar-iso-from-absolute days)))
  13019. (defun org-small-year-to-year (year)
  13020. "Convert 2-digit years into 4-digit years.
  13021. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13022. The year 2000 cannot be abbreviated. Any year larger than 99
  13023. is returned unchanged."
  13024. (if (< year 38)
  13025. (setq year (+ 2000 year))
  13026. (if (< year 100)
  13027. (setq year (+ 1900 year))))
  13028. year)
  13029. (defun org-time-from-absolute (d)
  13030. "Return the time corresponding to date D.
  13031. D may be an absolute day number, or a calendar-type list (month day year)."
  13032. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13033. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13034. (defun org-calendar-holiday ()
  13035. "List of holidays, for Diary display in Org-mode."
  13036. (require 'holidays)
  13037. (let ((hl (funcall
  13038. (if (fboundp 'calendar-check-holidays)
  13039. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13040. (if hl (mapconcat 'identity hl "; "))))
  13041. (defun org-diary-sexp-entry (sexp entry date)
  13042. "Process a SEXP diary ENTRY for DATE."
  13043. (require 'diary-lib)
  13044. (let ((result (if calendar-debug-sexp
  13045. (let ((stack-trace-on-error t))
  13046. (eval (car (read-from-string sexp))))
  13047. (condition-case nil
  13048. (eval (car (read-from-string sexp)))
  13049. (error
  13050. (beep)
  13051. (message "Bad sexp at line %d in %s: %s"
  13052. (org-current-line)
  13053. (buffer-file-name) sexp)
  13054. (sleep-for 2))))))
  13055. (cond ((stringp result) result)
  13056. ((and (consp result)
  13057. (stringp (cdr result))) (cdr result))
  13058. (result entry)
  13059. (t nil))))
  13060. (defun org-diary-to-ical-string (frombuf)
  13061. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13062. This uses the icalendar.el library."
  13063. (let* ((tmpdir (if (featurep 'xemacs)
  13064. (temp-directory)
  13065. temporary-file-directory))
  13066. (tmpfile (make-temp-name
  13067. (expand-file-name "orgics" tmpdir)))
  13068. buf rtn b e)
  13069. (with-current-buffer frombuf
  13070. (icalendar-export-region (point-min) (point-max) tmpfile)
  13071. (setq buf (find-buffer-visiting tmpfile))
  13072. (set-buffer buf)
  13073. (goto-char (point-min))
  13074. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13075. (setq b (match-beginning 0)))
  13076. (goto-char (point-max))
  13077. (if (re-search-backward "^END:VEVENT" nil t)
  13078. (setq e (match-end 0)))
  13079. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13080. (kill-buffer buf)
  13081. (delete-file tmpfile)
  13082. rtn))
  13083. (defun org-closest-date (start current change prefer show-all)
  13084. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13085. When PREFER is `past' return a date that is either CURRENT or past.
  13086. When PREFER is `future', return a date that is either CURRENT or future.
  13087. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13088. ;; Make the proper lists from the dates
  13089. (catch 'exit
  13090. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13091. dn dw sday cday n1 n2 n0
  13092. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13093. (setq start (org-date-to-gregorian start)
  13094. current (org-date-to-gregorian
  13095. (if show-all
  13096. current
  13097. (time-to-days (current-time))))
  13098. sday (calendar-absolute-from-gregorian start)
  13099. cday (calendar-absolute-from-gregorian current))
  13100. (if (<= cday sday) (throw 'exit sday))
  13101. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13102. (setq dn (string-to-number (match-string 1 change))
  13103. dw (cdr (assoc (match-string 2 change) a1)))
  13104. (error "Invalid change specifyer: %s" change))
  13105. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13106. (cond
  13107. ((eq dw 'day)
  13108. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13109. n2 (+ n1 dn)))
  13110. ((eq dw 'year)
  13111. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13112. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13113. (setq date1 (list m d y1)
  13114. n1 (calendar-absolute-from-gregorian date1)
  13115. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13116. n2 (calendar-absolute-from-gregorian date2)))
  13117. ((eq dw 'month)
  13118. ;; approx number of month between the two dates
  13119. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13120. ;; How often does dn fit in there?
  13121. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13122. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13123. m (+ m nm)
  13124. ny (floor (/ m 12))
  13125. y (+ y ny)
  13126. m (- m (* ny 12)))
  13127. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13128. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13129. (setq m2 (+ m dn) y2 y)
  13130. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13131. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13132. (while (<= n2 cday)
  13133. (setq n1 n2 m m2 y y2)
  13134. (setq m2 (+ m dn) y2 y)
  13135. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13136. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13137. ;; Make sure n1 is the earlier date
  13138. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13139. (if show-all
  13140. (cond
  13141. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13142. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13143. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13144. (cond
  13145. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13146. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13147. (t (if (= cday n1) n1 n2)))))))
  13148. (defun org-date-to-gregorian (date)
  13149. "Turn any specification of DATE into a gregorian date for the calendar."
  13150. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13151. ((and (listp date) (= (length date) 3)) date)
  13152. ((stringp date)
  13153. (setq date (org-parse-time-string date))
  13154. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13155. ((listp date)
  13156. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13157. (defun org-parse-time-string (s &optional nodefault)
  13158. "Parse the standard Org-mode time string.
  13159. This should be a lot faster than the normal `parse-time-string'.
  13160. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13161. hour and minute fields will be nil if not given."
  13162. (if (string-match org-ts-regexp0 s)
  13163. (list 0
  13164. (if (or (match-beginning 8) (not nodefault))
  13165. (string-to-number (or (match-string 8 s) "0")))
  13166. (if (or (match-beginning 7) (not nodefault))
  13167. (string-to-number (or (match-string 7 s) "0")))
  13168. (string-to-number (match-string 4 s))
  13169. (string-to-number (match-string 3 s))
  13170. (string-to-number (match-string 2 s))
  13171. nil nil nil)
  13172. (error "Not a standard Org-mode time string: %s" s)))
  13173. (defun org-timestamp-up (&optional arg)
  13174. "Increase the date item at the cursor by one.
  13175. If the cursor is on the year, change the year. If it is on the month or
  13176. the day, change that.
  13177. With prefix ARG, change by that many units."
  13178. (interactive "p")
  13179. (org-timestamp-change (prefix-numeric-value arg)))
  13180. (defun org-timestamp-down (&optional arg)
  13181. "Decrease the date item at the cursor by one.
  13182. If the cursor is on the year, change the year. If it is on the month or
  13183. the day, change that.
  13184. With prefix ARG, change by that many units."
  13185. (interactive "p")
  13186. (org-timestamp-change (- (prefix-numeric-value arg))))
  13187. (defun org-timestamp-up-day (&optional arg)
  13188. "Increase the date in the time stamp by one day.
  13189. With prefix ARG, change that many days."
  13190. (interactive "p")
  13191. (if (and (not (org-at-timestamp-p t))
  13192. (org-on-heading-p))
  13193. (org-todo 'up)
  13194. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13195. (defun org-timestamp-down-day (&optional arg)
  13196. "Decrease the date in the time stamp by one day.
  13197. With prefix ARG, change that many days."
  13198. (interactive "p")
  13199. (if (and (not (org-at-timestamp-p t))
  13200. (org-on-heading-p))
  13201. (org-todo 'down)
  13202. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13203. (defun org-at-timestamp-p (&optional inactive-ok)
  13204. "Determine if the cursor is in or at a timestamp."
  13205. (interactive)
  13206. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13207. (pos (point))
  13208. (ans (or (looking-at tsr)
  13209. (save-excursion
  13210. (skip-chars-backward "^[<\n\r\t")
  13211. (if (> (point) (point-min)) (backward-char 1))
  13212. (and (looking-at tsr)
  13213. (> (- (match-end 0) pos) -1))))))
  13214. (and ans
  13215. (boundp 'org-ts-what)
  13216. (setq org-ts-what
  13217. (cond
  13218. ((= pos (match-beginning 0)) 'bracket)
  13219. ((= pos (1- (match-end 0))) 'bracket)
  13220. ((org-pos-in-match-range pos 2) 'year)
  13221. ((org-pos-in-match-range pos 3) 'month)
  13222. ((org-pos-in-match-range pos 7) 'hour)
  13223. ((org-pos-in-match-range pos 8) 'minute)
  13224. ((or (org-pos-in-match-range pos 4)
  13225. (org-pos-in-match-range pos 5)) 'day)
  13226. ((and (> pos (or (match-end 8) (match-end 5)))
  13227. (< pos (match-end 0)))
  13228. (- pos (or (match-end 8) (match-end 5))))
  13229. (t 'day))))
  13230. ans))
  13231. (defun org-toggle-timestamp-type ()
  13232. "Toggle the type (<active> or [inactive]) of a time stamp."
  13233. (interactive)
  13234. (when (org-at-timestamp-p t)
  13235. (let ((beg (match-beginning 0)) (end (match-end 0))
  13236. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13237. (save-excursion
  13238. (goto-char beg)
  13239. (while (re-search-forward "[][<>]" end t)
  13240. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13241. t t)))
  13242. (message "Timestamp is now %sactive"
  13243. (if (equal (char-after beg) ?<) "" "in")))))
  13244. (defun org-timestamp-change (n &optional what)
  13245. "Change the date in the time stamp at point.
  13246. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13247. `year', `minute', `second'. If WHAT is not given, the cursor position
  13248. in the timestamp determines what will be changed."
  13249. (let ((pos (point))
  13250. with-hm inactive
  13251. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13252. org-ts-what
  13253. extra rem
  13254. ts time time0)
  13255. (if (not (org-at-timestamp-p t))
  13256. (error "Not at a timestamp"))
  13257. (if (and (not what) (eq org-ts-what 'bracket))
  13258. (org-toggle-timestamp-type)
  13259. (if (and (not what) (not (eq org-ts-what 'day))
  13260. org-display-custom-times
  13261. (get-text-property (point) 'display)
  13262. (not (get-text-property (1- (point)) 'display)))
  13263. (setq org-ts-what 'day))
  13264. (setq org-ts-what (or what org-ts-what)
  13265. inactive (= (char-after (match-beginning 0)) ?\[)
  13266. ts (match-string 0))
  13267. (replace-match "")
  13268. (if (string-match
  13269. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13270. ts)
  13271. (setq extra (match-string 1 ts)))
  13272. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13273. (setq with-hm t))
  13274. (setq time0 (org-parse-time-string ts))
  13275. (when (and (eq org-ts-what 'minute)
  13276. (eq current-prefix-arg nil))
  13277. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13278. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13279. (setcar (cdr time0) (+ (nth 1 time0)
  13280. (if (> n 0) (- rem) (- dm rem))))))
  13281. (setq time
  13282. (encode-time (or (car time0) 0)
  13283. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13284. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13285. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13286. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13287. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13288. (nthcdr 6 time0)))
  13289. (when (and (member org-ts-what '(hour minute))
  13290. extra
  13291. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13292. (setq extra (org-modify-ts-extra
  13293. extra
  13294. (if (eq org-ts-what 'hour) 2 5)
  13295. n dm)))
  13296. (when (integerp org-ts-what)
  13297. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13298. (if (eq what 'calendar)
  13299. (let ((cal-date (org-get-date-from-calendar)))
  13300. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13301. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13302. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13303. (setcar time0 (or (car time0) 0))
  13304. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13305. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13306. (setq time (apply 'encode-time time0))))
  13307. (setq org-last-changed-timestamp
  13308. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13309. (org-clock-update-time-maybe)
  13310. (goto-char pos)
  13311. ;; Try to recenter the calendar window, if any
  13312. (if (and org-calendar-follow-timestamp-change
  13313. (get-buffer-window "*Calendar*" t)
  13314. (memq org-ts-what '(day month year)))
  13315. (org-recenter-calendar (time-to-days time))))))
  13316. (defun org-modify-ts-extra (s pos n dm)
  13317. "Change the different parts of the lead-time and repeat fields in timestamp."
  13318. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13319. ng h m new rem)
  13320. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13321. (cond
  13322. ((or (org-pos-in-match-range pos 2)
  13323. (org-pos-in-match-range pos 3))
  13324. (setq m (string-to-number (match-string 3 s))
  13325. h (string-to-number (match-string 2 s)))
  13326. (if (org-pos-in-match-range pos 2)
  13327. (setq h (+ h n))
  13328. (setq n (* dm (org-no-warnings (signum n))))
  13329. (when (not (= 0 (setq rem (% m dm))))
  13330. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13331. (setq m (+ m n)))
  13332. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13333. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13334. (setq h (min 24 (max 0 h)))
  13335. (setq ng 1 new (format "-%02d:%02d" h m)))
  13336. ((org-pos-in-match-range pos 6)
  13337. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13338. ((org-pos-in-match-range pos 5)
  13339. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13340. ((org-pos-in-match-range pos 9)
  13341. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13342. ((org-pos-in-match-range pos 8)
  13343. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13344. (when ng
  13345. (setq s (concat
  13346. (substring s 0 (match-beginning ng))
  13347. new
  13348. (substring s (match-end ng))))))
  13349. s))
  13350. (defun org-recenter-calendar (date)
  13351. "If the calendar is visible, recenter it to DATE."
  13352. (let* ((win (selected-window))
  13353. (cwin (get-buffer-window "*Calendar*" t))
  13354. (calendar-move-hook nil))
  13355. (when cwin
  13356. (select-window cwin)
  13357. (calendar-goto-date (if (listp date) date
  13358. (calendar-gregorian-from-absolute date)))
  13359. (select-window win))))
  13360. (defun org-goto-calendar (&optional arg)
  13361. "Go to the Emacs calendar at the current date.
  13362. If there is a time stamp in the current line, go to that date.
  13363. A prefix ARG can be used to force the current date."
  13364. (interactive "P")
  13365. (let ((tsr org-ts-regexp) diff
  13366. (calendar-move-hook nil)
  13367. (calendar-view-holidays-initially-flag nil)
  13368. (view-calendar-holidays-initially nil)
  13369. (calendar-view-diary-initially-flag nil)
  13370. (view-diary-entries-initially nil))
  13371. (if (or (org-at-timestamp-p)
  13372. (save-excursion
  13373. (beginning-of-line 1)
  13374. (looking-at (concat ".*" tsr))))
  13375. (let ((d1 (time-to-days (current-time)))
  13376. (d2 (time-to-days
  13377. (org-time-string-to-time (match-string 1)))))
  13378. (setq diff (- d2 d1))))
  13379. (calendar)
  13380. (calendar-goto-today)
  13381. (if (and diff (not arg)) (calendar-forward-day diff))))
  13382. (defun org-get-date-from-calendar ()
  13383. "Return a list (month day year) of date at point in calendar."
  13384. (with-current-buffer "*Calendar*"
  13385. (save-match-data
  13386. (calendar-cursor-to-date))))
  13387. (defun org-date-from-calendar ()
  13388. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13389. If there is already a time stamp at the cursor position, update it."
  13390. (interactive)
  13391. (if (org-at-timestamp-p t)
  13392. (org-timestamp-change 0 'calendar)
  13393. (let ((cal-date (org-get-date-from-calendar)))
  13394. (org-insert-time-stamp
  13395. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13396. (defun org-minutes-to-hh:mm-string (m)
  13397. "Compute H:MM from a number of minutes."
  13398. (let ((h (/ m 60)))
  13399. (setq m (- m (* 60 h)))
  13400. (format org-time-clocksum-format h m)))
  13401. (defun org-hh:mm-string-to-minutes (s)
  13402. "Convert a string H:MM to a number of minutes.
  13403. If the string is just a number, interpret it as minutes.
  13404. In fact, the first hh:mm or number in the string will be taken,
  13405. there can be extra stuff in the string.
  13406. If no number is found, the return value is 0."
  13407. (cond
  13408. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13409. (+ (* (string-to-number (match-string 1 s)) 60)
  13410. (string-to-number (match-string 2 s))))
  13411. ((string-match "\\([0-9]+\\)" s)
  13412. (string-to-number (match-string 1 s)))
  13413. (t 0)))
  13414. ;;;; Files
  13415. (defun org-save-all-org-buffers ()
  13416. "Save all Org-mode buffers without user confirmation."
  13417. (interactive)
  13418. (message "Saving all Org-mode buffers...")
  13419. (save-some-buffers t 'org-mode-p)
  13420. (when (featurep 'org-id) (org-id-locations-save))
  13421. (message "Saving all Org-mode buffers... done"))
  13422. (defun org-revert-all-org-buffers ()
  13423. "Revert all Org-mode buffers.
  13424. Prompt for confirmation when there are unsaved changes.
  13425. Be sure you know what you are doing before letting this function
  13426. overwrite your changes.
  13427. This function is useful in a setup where one tracks org files
  13428. with a version control system, to revert on one machine after pulling
  13429. changes from another. I believe the procedure must be like this:
  13430. 1. M-x org-save-all-org-buffers
  13431. 2. Pull changes from the other machine, resolve conflicts
  13432. 3. M-x org-revert-all-org-buffers"
  13433. (interactive)
  13434. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13435. (error "Abort"))
  13436. (save-excursion
  13437. (save-window-excursion
  13438. (mapc
  13439. (lambda (b)
  13440. (when (and (with-current-buffer b (org-mode-p))
  13441. (with-current-buffer b buffer-file-name))
  13442. (switch-to-buffer b)
  13443. (revert-buffer t 'no-confirm)))
  13444. (buffer-list))
  13445. (when (and (featurep 'org-id) org-id-track-globally)
  13446. (org-id-locations-load)))))
  13447. ;;;; Agenda files
  13448. ;;;###autoload
  13449. (defun org-iswitchb (&optional arg)
  13450. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13451. With a prefix argument, restrict available to files.
  13452. With two prefix arguments, restrict available buffers to agenda files."
  13453. (interactive "P")
  13454. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13455. ((equal arg '(16)) (org-buffer-list 'agenda))
  13456. (t (org-buffer-list)))))
  13457. (switch-to-buffer
  13458. (org-icompleting-read "Org buffer: "
  13459. (mapcar 'list (mapcar 'buffer-name blist))
  13460. nil t))))
  13461. ;;;###autoload
  13462. (defalias 'org-ido-switchb 'org-iswitchb)
  13463. (defun org-buffer-list (&optional predicate exclude-tmp)
  13464. "Return a list of Org buffers.
  13465. PREDICATE can be `export', `files' or `agenda'.
  13466. export restrict the list to Export buffers.
  13467. files restrict the list to buffers visiting Org files.
  13468. agenda restrict the list to buffers visiting agenda files.
  13469. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13470. (let* ((bfn nil)
  13471. (agenda-files (and (eq predicate 'agenda)
  13472. (mapcar 'file-truename (org-agenda-files t))))
  13473. (filter
  13474. (cond
  13475. ((eq predicate 'files)
  13476. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13477. ((eq predicate 'export)
  13478. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13479. ((eq predicate 'agenda)
  13480. (lambda (b)
  13481. (with-current-buffer b
  13482. (and (eq major-mode 'org-mode)
  13483. (setq bfn (buffer-file-name b))
  13484. (member (file-truename bfn) agenda-files)))))
  13485. (t (lambda (b) (with-current-buffer b
  13486. (or (eq major-mode 'org-mode)
  13487. (string-match "\*Org .*Export"
  13488. (buffer-name b)))))))))
  13489. (delq nil
  13490. (mapcar
  13491. (lambda(b)
  13492. (if (and (funcall filter b)
  13493. (or (not exclude-tmp)
  13494. (not (string-match "tmp" (buffer-name b)))))
  13495. b
  13496. nil))
  13497. (buffer-list)))))
  13498. (defun org-agenda-files (&optional unrestricted archives)
  13499. "Get the list of agenda files.
  13500. Optional UNRESTRICTED means return the full list even if a restriction
  13501. is currently in place.
  13502. When ARCHIVES is t, include all archive files that are really being
  13503. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13504. `org-agenda-archives-mode' is t."
  13505. (let ((files
  13506. (cond
  13507. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13508. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13509. ((listp org-agenda-files) org-agenda-files)
  13510. (t (error "Invalid value of `org-agenda-files'")))))
  13511. (setq files (apply 'append
  13512. (mapcar (lambda (f)
  13513. (if (file-directory-p f)
  13514. (directory-files
  13515. f t org-agenda-file-regexp)
  13516. (list f)))
  13517. files)))
  13518. (when org-agenda-skip-unavailable-files
  13519. (setq files (delq nil
  13520. (mapcar (function
  13521. (lambda (file)
  13522. (and (file-readable-p file) file)))
  13523. files))))
  13524. (when (or (eq archives t)
  13525. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13526. (setq files (org-add-archive-files files)))
  13527. files))
  13528. (defun org-edit-agenda-file-list ()
  13529. "Edit the list of agenda files.
  13530. Depending on setup, this either uses customize to edit the variable
  13531. `org-agenda-files', or it visits the file that is holding the list. In the
  13532. latter case, the buffer is set up in a way that saving it automatically kills
  13533. the buffer and restores the previous window configuration."
  13534. (interactive)
  13535. (if (stringp org-agenda-files)
  13536. (let ((cw (current-window-configuration)))
  13537. (find-file org-agenda-files)
  13538. (org-set-local 'org-window-configuration cw)
  13539. (org-add-hook 'after-save-hook
  13540. (lambda ()
  13541. (set-window-configuration
  13542. (prog1 org-window-configuration
  13543. (kill-buffer (current-buffer))))
  13544. (org-install-agenda-files-menu)
  13545. (message "New agenda file list installed"))
  13546. nil 'local)
  13547. (message "%s" (substitute-command-keys
  13548. "Edit list and finish with \\[save-buffer]")))
  13549. (customize-variable 'org-agenda-files)))
  13550. (defun org-store-new-agenda-file-list (list)
  13551. "Set new value for the agenda file list and save it correctly."
  13552. (if (stringp org-agenda-files)
  13553. (let ((fe (org-read-agenda-file-list t)) b u)
  13554. (while (setq b (find-buffer-visiting org-agenda-files))
  13555. (kill-buffer b))
  13556. (with-temp-file org-agenda-files
  13557. (insert
  13558. (mapconcat
  13559. (lambda (f) ;; Keep un-expanded entries.
  13560. (if (setq u (assoc f fe))
  13561. (cdr u)
  13562. f))
  13563. list "\n")
  13564. "\n")))
  13565. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13566. (org-insert-mode-line-in-empty-file nil))
  13567. (setq org-agenda-files list)
  13568. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13569. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13570. "Read the list of agenda files from a file.
  13571. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13572. filenames, used by `org-store-new-agenda-file-list' to write back
  13573. un-expanded file names."
  13574. (when (file-directory-p org-agenda-files)
  13575. (error "`org-agenda-files' cannot be a single directory"))
  13576. (when (stringp org-agenda-files)
  13577. (with-temp-buffer
  13578. (insert-file-contents org-agenda-files)
  13579. (mapcar
  13580. (lambda (f)
  13581. (let ((e (expand-file-name (substitute-in-file-name f)
  13582. org-directory)))
  13583. (if pair-with-expansion
  13584. (cons e f)
  13585. e)))
  13586. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13587. ;;;###autoload
  13588. (defun org-cycle-agenda-files ()
  13589. "Cycle through the files in `org-agenda-files'.
  13590. If the current buffer visits an agenda file, find the next one in the list.
  13591. If the current buffer does not, find the first agenda file."
  13592. (interactive)
  13593. (let* ((fs (org-agenda-files t))
  13594. (files (append fs (list (car fs))))
  13595. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13596. file)
  13597. (unless files (error "No agenda files"))
  13598. (catch 'exit
  13599. (while (setq file (pop files))
  13600. (if (equal (file-truename file) tcf)
  13601. (when (car files)
  13602. (find-file (car files))
  13603. (throw 'exit t))))
  13604. (find-file (car fs)))
  13605. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13606. (defun org-agenda-file-to-front (&optional to-end)
  13607. "Move/add the current file to the top of the agenda file list.
  13608. If the file is not present in the list, it is added to the front. If it is
  13609. present, it is moved there. With optional argument TO-END, add/move to the
  13610. end of the list."
  13611. (interactive "P")
  13612. (let ((org-agenda-skip-unavailable-files nil)
  13613. (file-alist (mapcar (lambda (x)
  13614. (cons (file-truename x) x))
  13615. (org-agenda-files t)))
  13616. (ctf (file-truename buffer-file-name))
  13617. x had)
  13618. (setq x (assoc ctf file-alist) had x)
  13619. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13620. (if to-end
  13621. (setq file-alist (append (delq x file-alist) (list x)))
  13622. (setq file-alist (cons x (delq x file-alist))))
  13623. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13624. (org-install-agenda-files-menu)
  13625. (message "File %s to %s of agenda file list"
  13626. (if had "moved" "added") (if to-end "end" "front"))))
  13627. (defun org-remove-file (&optional file)
  13628. "Remove current file from the list of files in variable `org-agenda-files'.
  13629. These are the files which are being checked for agenda entries.
  13630. Optional argument FILE means use this file instead of the current."
  13631. (interactive)
  13632. (let* ((org-agenda-skip-unavailable-files nil)
  13633. (file (or file buffer-file-name))
  13634. (true-file (file-truename file))
  13635. (afile (abbreviate-file-name file))
  13636. (files (delq nil (mapcar
  13637. (lambda (x)
  13638. (if (equal true-file
  13639. (file-truename x))
  13640. nil x))
  13641. (org-agenda-files t)))))
  13642. (if (not (= (length files) (length (org-agenda-files t))))
  13643. (progn
  13644. (org-store-new-agenda-file-list files)
  13645. (org-install-agenda-files-menu)
  13646. (message "Removed file: %s" afile))
  13647. (message "File was not in list: %s (not removed)" afile))))
  13648. (defun org-file-menu-entry (file)
  13649. (vector file (list 'find-file file) t))
  13650. (defun org-check-agenda-file (file)
  13651. "Make sure FILE exists. If not, ask user what to do."
  13652. (when (not (file-exists-p file))
  13653. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13654. (abbreviate-file-name file))
  13655. (let ((r (downcase (read-char-exclusive))))
  13656. (cond
  13657. ((equal r ?r)
  13658. (org-remove-file file)
  13659. (throw 'nextfile t))
  13660. (t (error "Abort"))))))
  13661. (defun org-get-agenda-file-buffer (file)
  13662. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13663. it to the list of buffers which might be released later."
  13664. (let ((buf (org-find-base-buffer-visiting file)))
  13665. (if buf
  13666. buf ; just return it
  13667. ;; Make a new buffer and remember it
  13668. (setq buf (find-file-noselect file))
  13669. (if buf (push buf org-agenda-new-buffers))
  13670. buf)))
  13671. (defun org-release-buffers (blist)
  13672. "Release all buffers in list, asking the user for confirmation when needed.
  13673. When a buffer is unmodified, it is just killed. When modified, it is saved
  13674. \(if the user agrees) and then killed."
  13675. (let (buf file)
  13676. (while (setq buf (pop blist))
  13677. (setq file (buffer-file-name buf))
  13678. (when (and (buffer-modified-p buf)
  13679. file
  13680. (y-or-n-p (format "Save file %s? " file)))
  13681. (with-current-buffer buf (save-buffer)))
  13682. (kill-buffer buf))))
  13683. (defun org-prepare-agenda-buffers (files)
  13684. "Create buffers for all agenda files, protect archived trees and comments."
  13685. (interactive)
  13686. (let ((pa '(:org-archived t))
  13687. (pc '(:org-comment t))
  13688. (pall '(:org-archived t :org-comment t))
  13689. (inhibit-read-only t)
  13690. (rea (concat ":" org-archive-tag ":"))
  13691. bmp file re)
  13692. (save-excursion
  13693. (save-restriction
  13694. (while (setq file (pop files))
  13695. (catch 'nextfile
  13696. (if (bufferp file)
  13697. (set-buffer file)
  13698. (org-check-agenda-file file)
  13699. (set-buffer (org-get-agenda-file-buffer file)))
  13700. (widen)
  13701. (setq bmp (buffer-modified-p))
  13702. (org-refresh-category-properties)
  13703. (setq org-todo-keywords-for-agenda
  13704. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13705. (setq org-done-keywords-for-agenda
  13706. (append org-done-keywords-for-agenda org-done-keywords))
  13707. (setq org-todo-keyword-alist-for-agenda
  13708. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13709. (setq org-drawers-for-agenda
  13710. (append org-drawers-for-agenda org-drawers))
  13711. (setq org-tag-alist-for-agenda
  13712. (append org-tag-alist-for-agenda org-tag-alist))
  13713. (save-excursion
  13714. (remove-text-properties (point-min) (point-max) pall)
  13715. (when org-agenda-skip-archived-trees
  13716. (goto-char (point-min))
  13717. (while (re-search-forward rea nil t)
  13718. (if (org-on-heading-p t)
  13719. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13720. (goto-char (point-min))
  13721. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13722. (while (re-search-forward re nil t)
  13723. (add-text-properties
  13724. (match-beginning 0) (org-end-of-subtree t) pc)))
  13725. (set-buffer-modified-p bmp)))))
  13726. (setq org-todo-keywords-for-agenda
  13727. (org-uniquify org-todo-keywords-for-agenda))
  13728. (setq org-todo-keyword-alist-for-agenda
  13729. (org-uniquify org-todo-keyword-alist-for-agenda)
  13730. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13731. ;;;; Embedded LaTeX
  13732. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13733. "Keymap for the minor `org-cdlatex-mode'.")
  13734. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13735. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13736. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13737. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13738. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13739. (defvar org-cdlatex-texmathp-advice-is-done nil
  13740. "Flag remembering if we have applied the advice to texmathp already.")
  13741. (define-minor-mode org-cdlatex-mode
  13742. "Toggle the minor `org-cdlatex-mode'.
  13743. This mode supports entering LaTeX environment and math in LaTeX fragments
  13744. in Org-mode.
  13745. \\{org-cdlatex-mode-map}"
  13746. nil " OCDL" nil
  13747. (when org-cdlatex-mode (require 'cdlatex))
  13748. (unless org-cdlatex-texmathp-advice-is-done
  13749. (setq org-cdlatex-texmathp-advice-is-done t)
  13750. (defadvice texmathp (around org-math-always-on activate)
  13751. "Always return t in org-mode buffers.
  13752. This is because we want to insert math symbols without dollars even outside
  13753. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13754. an embedded LaTeX fragment, let texmathp do its job.
  13755. \\[org-cdlatex-mode-map]"
  13756. (interactive)
  13757. (let (p)
  13758. (cond
  13759. ((not (org-mode-p)) ad-do-it)
  13760. ((eq this-command 'cdlatex-math-symbol)
  13761. (setq ad-return-value t
  13762. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13763. (t
  13764. (let ((p (org-inside-LaTeX-fragment-p)))
  13765. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13766. (setq ad-return-value t
  13767. texmathp-why '("Org-mode embedded math" . 0))
  13768. (if p ad-do-it)))))))))
  13769. (defun turn-on-org-cdlatex ()
  13770. "Unconditionally turn on `org-cdlatex-mode'."
  13771. (org-cdlatex-mode 1))
  13772. (defun org-inside-LaTeX-fragment-p ()
  13773. "Test if point is inside a LaTeX fragment.
  13774. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13775. sequence appearing also before point.
  13776. Even though the matchers for math are configurable, this function assumes
  13777. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13778. delimiters are skipped when they have been removed by customization.
  13779. The return value is nil, or a cons cell with the delimiter and
  13780. and the position of this delimiter.
  13781. This function does a reasonably good job, but can locally be fooled by
  13782. for example currency specifications. For example it will assume being in
  13783. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13784. fragments that are properly closed, but during editing, we have to live
  13785. with the uncertainty caused by missing closing delimiters. This function
  13786. looks only before point, not after."
  13787. (catch 'exit
  13788. (let ((pos (point))
  13789. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13790. (lim (progn
  13791. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13792. (point)))
  13793. dd-on str (start 0) m re)
  13794. (goto-char pos)
  13795. (when dodollar
  13796. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13797. re (nth 1 (assoc "$" org-latex-regexps)))
  13798. (while (string-match re str start)
  13799. (cond
  13800. ((= (match-end 0) (length str))
  13801. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13802. ((= (match-end 0) (- (length str) 5))
  13803. (throw 'exit nil))
  13804. (t (setq start (match-end 0))))))
  13805. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13806. (goto-char pos)
  13807. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13808. (and (match-beginning 2) (throw 'exit nil))
  13809. ;; count $$
  13810. (while (re-search-backward "\\$\\$" lim t)
  13811. (setq dd-on (not dd-on)))
  13812. (goto-char pos)
  13813. (if dd-on (cons "$$" m))))))
  13814. (defun org-inside-latex-macro-p ()
  13815. "Is point inside a LaTeX macro or its arguments?"
  13816. (save-match-data
  13817. (org-in-regexp
  13818. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13819. (defun test ()
  13820. (interactive)
  13821. (message "%s" (org-inside-latex-macro-p)))
  13822. (defun org-try-cdlatex-tab ()
  13823. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13824. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13825. - inside a LaTeX fragment, or
  13826. - after the first word in a line, where an abbreviation expansion could
  13827. insert a LaTeX environment."
  13828. (when org-cdlatex-mode
  13829. (cond
  13830. ((save-excursion
  13831. (skip-chars-backward "a-zA-Z0-9*")
  13832. (skip-chars-backward " \t")
  13833. (bolp))
  13834. (cdlatex-tab) t)
  13835. ((org-inside-LaTeX-fragment-p)
  13836. (cdlatex-tab) t)
  13837. (t nil))))
  13838. (defun org-cdlatex-underscore-caret (&optional arg)
  13839. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13840. Revert to the normal definition outside of these fragments."
  13841. (interactive "P")
  13842. (if (org-inside-LaTeX-fragment-p)
  13843. (call-interactively 'cdlatex-sub-superscript)
  13844. (let (org-cdlatex-mode)
  13845. (call-interactively (key-binding (vector last-input-event))))))
  13846. (defun org-cdlatex-math-modify (&optional arg)
  13847. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13848. Revert to the normal definition outside of these fragments."
  13849. (interactive "P")
  13850. (if (org-inside-LaTeX-fragment-p)
  13851. (call-interactively 'cdlatex-math-modify)
  13852. (let (org-cdlatex-mode)
  13853. (call-interactively (key-binding (vector last-input-event))))))
  13854. (defvar org-latex-fragment-image-overlays nil
  13855. "List of overlays carrying the images of latex fragments.")
  13856. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13857. (defun org-remove-latex-fragment-image-overlays ()
  13858. "Remove all overlays with LaTeX fragment images in current buffer."
  13859. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13860. (setq org-latex-fragment-image-overlays nil))
  13861. (defun org-preview-latex-fragment (&optional subtree)
  13862. "Preview the LaTeX fragment at point, or all locally or globally.
  13863. If the cursor is in a LaTeX fragment, create the image and overlay
  13864. it over the source code. If there is no fragment at point, display
  13865. all fragments in the current text, from one headline to the next. With
  13866. prefix SUBTREE, display all fragments in the current subtree. With a
  13867. double prefix `C-u C-u', or when the cursor is before the first headline,
  13868. display all fragments in the buffer.
  13869. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13870. (interactive "P")
  13871. (org-remove-latex-fragment-image-overlays)
  13872. (save-excursion
  13873. (save-restriction
  13874. (let (beg end at msg)
  13875. (cond
  13876. ((or (equal subtree '(16))
  13877. (not (save-excursion
  13878. (re-search-backward (concat "^" outline-regexp) nil t))))
  13879. (setq beg (point-min) end (point-max)
  13880. msg "Creating images for buffer...%s"))
  13881. ((equal subtree '(4))
  13882. (org-back-to-heading)
  13883. (setq beg (point) end (org-end-of-subtree t)
  13884. msg "Creating images for subtree...%s"))
  13885. (t
  13886. (if (setq at (org-inside-LaTeX-fragment-p))
  13887. (goto-char (max (point-min) (- (cdr at) 2)))
  13888. (org-back-to-heading))
  13889. (setq beg (point) end (progn (outline-next-heading) (point))
  13890. msg (if at "Creating image...%s"
  13891. "Creating images for entry...%s"))))
  13892. (message msg "")
  13893. (narrow-to-region beg end)
  13894. (goto-char beg)
  13895. (org-format-latex
  13896. (concat "ltxpng/" (file-name-sans-extension
  13897. (file-name-nondirectory
  13898. buffer-file-name)))
  13899. default-directory 'overlays msg at 'forbuffer)
  13900. (message msg "done. Use `C-c C-c' to remove images.")))))
  13901. (defvar org-latex-regexps
  13902. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13903. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13904. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13905. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13906. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13907. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13908. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13909. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13910. "Regular expressions for matching embedded LaTeX.")
  13911. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13912. "Replace LaTeX fragments with links to an image, and produce images.
  13913. Some of the options can be changed using the variable
  13914. `org-format-latex-options'."
  13915. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13916. (let* ((prefixnodir (file-name-nondirectory prefix))
  13917. (absprefix (expand-file-name prefix dir))
  13918. (todir (file-name-directory absprefix))
  13919. (opt org-format-latex-options)
  13920. (matchers (plist-get opt :matchers))
  13921. (re-list org-latex-regexps)
  13922. (org-format-latex-header-extra
  13923. (plist-get (org-infile-export-plist) :latex-header-extra))
  13924. (cnt 0) txt hash link beg end re e checkdir
  13925. executables-checked
  13926. m n block linkfile movefile ov)
  13927. ;; Check the different regular expressions
  13928. (while (setq e (pop re-list))
  13929. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13930. block (if (nth 3 e) "\n\n" ""))
  13931. (when (member m matchers)
  13932. (goto-char (point-min))
  13933. (while (re-search-forward re nil t)
  13934. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13935. (not (get-text-property (match-beginning n)
  13936. 'org-protected))
  13937. (or (not overlays)
  13938. (not (eq (get-char-property (match-beginning n)
  13939. 'org-overlay-type)
  13940. 'org-latex-overlay))))
  13941. (setq txt (match-string n)
  13942. beg (match-beginning n) end (match-end n)
  13943. cnt (1+ cnt))
  13944. (let (print-length print-level) ; make sure full list is printed
  13945. (setq hash (sha1 (prin1-to-string
  13946. (list org-format-latex-header
  13947. org-format-latex-header-extra
  13948. org-export-latex-default-packages-alist
  13949. org-export-latex-packages-alist
  13950. org-format-latex-options
  13951. forbuffer txt)))
  13952. linkfile (format "%s_%s.png" prefix hash)
  13953. movefile (format "%s_%s.png" absprefix hash)))
  13954. (setq link (concat block "[[file:" linkfile "]]" block))
  13955. (if msg (message msg cnt))
  13956. (goto-char beg)
  13957. (unless checkdir ; make sure the directory exists
  13958. (setq checkdir t)
  13959. (or (file-directory-p todir) (make-directory todir)))
  13960. (unless executables-checked
  13961. (org-check-external-command
  13962. "latex" "needed to convert LaTeX fragments to images")
  13963. (org-check-external-command
  13964. "dvipng" "needed to convert LaTeX fragments to images")
  13965. (setq executables-checked t))
  13966. (unless (file-exists-p movefile)
  13967. (org-create-formula-image
  13968. txt movefile opt forbuffer))
  13969. (if overlays
  13970. (progn
  13971. (mapc (lambda (o)
  13972. (if (eq (org-overlay-get o 'org-overlay-type)
  13973. 'org-latex-overlay)
  13974. (org-delete-overlay o)))
  13975. (org-overlays-in beg end))
  13976. (setq ov (org-make-overlay beg end))
  13977. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13978. (if (featurep 'xemacs)
  13979. (progn
  13980. (org-overlay-put ov 'invisible t)
  13981. (org-overlay-put
  13982. ov 'end-glyph
  13983. (make-glyph (vector 'png :file movefile))))
  13984. (org-overlay-put
  13985. ov 'display
  13986. (list 'image :type 'png :file movefile :ascent 'center)))
  13987. (push ov org-latex-fragment-image-overlays)
  13988. (goto-char end))
  13989. (delete-region beg end)
  13990. (insert (org-add-props link
  13991. (list 'org-latex-src
  13992. (replace-regexp-in-string "\"" "" txt)))))))))))
  13993. ;; This function borrows from Ganesh Swami's latex2png.el
  13994. (defun org-create-formula-image (string tofile options buffer)
  13995. "This calls dvipng."
  13996. (require 'org-latex)
  13997. (let* ((tmpdir (if (featurep 'xemacs)
  13998. (temp-directory)
  13999. temporary-file-directory))
  14000. (texfilebase (make-temp-name
  14001. (expand-file-name "orgtex" tmpdir)))
  14002. (texfile (concat texfilebase ".tex"))
  14003. (dvifile (concat texfilebase ".dvi"))
  14004. (pngfile (concat texfilebase ".png"))
  14005. (fnh (if (featurep 'xemacs)
  14006. (font-height (get-face-font 'default))
  14007. (face-attribute 'default :height nil)))
  14008. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14009. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14010. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14011. "Black"))
  14012. (bg (or (plist-get options (if buffer :background :html-background))
  14013. "Transparent")))
  14014. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14015. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14016. (with-temp-file texfile
  14017. (insert (org-splice-latex-header
  14018. org-format-latex-header
  14019. org-export-latex-default-packages-alist
  14020. org-export-latex-packages-alist
  14021. org-format-latex-header-extra))
  14022. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14023. (require 'org-latex)
  14024. (org-export-latex-fix-inputenc))
  14025. (let ((dir default-directory))
  14026. (condition-case nil
  14027. (progn
  14028. (cd tmpdir)
  14029. (call-process "latex" nil nil nil texfile))
  14030. (error nil))
  14031. (cd dir))
  14032. (if (not (file-exists-p dvifile))
  14033. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14034. (condition-case nil
  14035. (call-process "dvipng" nil nil nil
  14036. "-fg" fg "-bg" bg
  14037. "-D" dpi
  14038. ;;"-x" scale "-y" scale
  14039. "-T" "tight"
  14040. "-o" pngfile
  14041. dvifile)
  14042. (error nil))
  14043. (if (not (file-exists-p pngfile))
  14044. (if org-format-latex-signal-error
  14045. (error "Failed to create png file from %s" texfile)
  14046. (message "Failed to create png file from %s" texfile)
  14047. nil)
  14048. ;; Use the requested file name and clean up
  14049. (copy-file pngfile tofile 'replace)
  14050. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14051. (delete-file (concat texfilebase e)))
  14052. pngfile))))
  14053. (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
  14054. "Fill a LaTeX header template TPL.
  14055. In the template, the following place holders will be recognized:
  14056. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14057. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14058. [PACKAGES] \\usepackage statements for PKG
  14059. [NO-PACKAGES] do not include PKG
  14060. [EXTRA] the string EXTRA
  14061. [NO-EXTRA] do not include EXTRA
  14062. For backward compatibility, if both the positive and the negative place
  14063. holder is missing, the positive one (without the \"NO-\") will be
  14064. assumed to be present at the end of the template.
  14065. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14066. EXTRA is a string."
  14067. (let (rpl (end ""))
  14068. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14069. (setq rpl (if (or (match-end 1) (not def-pkg))
  14070. "" (org-latex-packages-to-string def-pkg t))
  14071. tpl (replace-match rpl t t tpl))
  14072. (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
  14073. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14074. (setq rpl (if (or (match-end 1) (not pkg))
  14075. "" (org-latex-packages-to-string pkg t))
  14076. tpl (replace-match rpl t t tpl))
  14077. (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
  14078. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14079. (setq rpl (if (or (match-end 1) (not extra))
  14080. "" (concat extra "\n"))
  14081. tpl (replace-match rpl t t tpl))
  14082. (if (and extra (string-match "\\S-" extra))
  14083. (setq end (concat end "\n" extra))))
  14084. (if (string-match "\\S-" end)
  14085. (concat tpl "\n" end)
  14086. tpl)))
  14087. (defun org-latex-packages-to-string (pkg &optional newline)
  14088. "Turn an alist of packages into a string with the \\usepackage macros."
  14089. (setq pkg (mapconcat (lambda(p)
  14090. (cond
  14091. ((stringp p) p)
  14092. ((equal "" (car p))
  14093. (format "\\usepackage{%s}" (cadr p)))
  14094. (t
  14095. (format "\\usepackage[%s]{%s}"
  14096. (car p) (cadr p)))))
  14097. pkg
  14098. "\n"))
  14099. (if newline (concat pkg "\n") pkg))
  14100. (defun org-dvipng-color (attr)
  14101. "Return an rgb color specification for dvipng."
  14102. (apply 'format "rgb %s %s %s"
  14103. (mapcar 'org-normalize-color
  14104. (color-values (face-attribute 'default attr nil)))))
  14105. (defun org-normalize-color (value)
  14106. "Return string to be used as color value for an RGB component."
  14107. (format "%g" (/ value 65535.0)))
  14108. ;;;; Key bindings
  14109. ;; Make `C-c C-x' a prefix key
  14110. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14111. ;; TAB key with modifiers
  14112. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14113. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14114. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14115. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14116. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14117. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14118. ;; The following line is necessary under Suse GNU/Linux
  14119. (unless (featurep 'xemacs)
  14120. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14121. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14122. (define-key org-mode-map [backtab] 'org-shifttab)
  14123. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14124. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14125. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14126. ;; Cursor keys with modifiers
  14127. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14128. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14129. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14130. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14131. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14132. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14133. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14134. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14135. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14136. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14137. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14138. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14139. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14140. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14141. ;;; Extra keys for tty access.
  14142. ;; We only set them when really needed because otherwise the
  14143. ;; menus don't show the simple keys
  14144. (when (or org-use-extra-keys
  14145. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14146. (not window-system))
  14147. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14148. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14149. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14150. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14151. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14152. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14153. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14154. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14155. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14156. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14157. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14158. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14159. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14160. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14161. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14162. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14163. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14164. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14165. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14166. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14167. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14168. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14169. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14170. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14171. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14172. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14173. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14174. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14175. ;; All the other keys
  14176. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14177. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14178. (if (boundp 'narrow-map)
  14179. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14180. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14181. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14182. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14183. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14184. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14185. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14186. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14187. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14188. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14189. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14190. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14191. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14192. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14193. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14194. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14195. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14196. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14197. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14198. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14199. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14200. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14201. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14202. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14203. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14204. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14205. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14206. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14207. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14208. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14209. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14210. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14211. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14212. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14213. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14214. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14215. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14216. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14217. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14218. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14219. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14220. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14221. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14222. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14223. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14224. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14225. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14226. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14227. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14228. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14229. (org-defkey org-mode-map "\C-m" 'org-return)
  14230. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14231. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14232. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14233. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14234. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14235. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14236. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14237. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14238. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14239. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14240. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14241. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14242. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14243. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14244. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14245. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14246. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14247. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14248. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14249. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14250. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14251. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14252. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14253. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14254. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14255. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14256. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14257. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14258. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14259. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14260. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14261. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14262. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14263. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14264. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14265. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14266. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14267. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14268. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14269. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14270. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14271. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14272. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14273. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14274. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14275. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14276. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14277. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14278. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14279. (when (featurep 'xemacs)
  14280. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14281. (defconst org-speed-commands-default
  14282. '(
  14283. ("Outline Navigation")
  14284. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14285. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14286. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14287. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14288. ("u" . (org-speed-move-safe 'outline-up-heading))
  14289. ("j" . org-goto)
  14290. ("g" . (org-refile t))
  14291. ("Outline Visibility")
  14292. ("c" . org-cycle)
  14293. ("C" . org-shifttab)
  14294. (" " . org-display-outline-path)
  14295. ("Outline Structure Editing")
  14296. ("U" . org-shiftmetaup)
  14297. ("D" . org-shiftmetadown)
  14298. ("r" . org-metaright)
  14299. ("l" . org-metaleft)
  14300. ("R" . org-shiftmetaright)
  14301. ("L" . org-shiftmetaleft)
  14302. ("i" . (progn (forward-char 1) (call-interactively
  14303. 'org-insert-heading-respect-content)))
  14304. ("^" . org-sort)
  14305. ("w" . org-refile)
  14306. ("a" . org-archive-subtree-default-with-confirmation)
  14307. ("." . outline-mark-subtree)
  14308. ("Clock Commands")
  14309. ("I" . org-clock-in)
  14310. ("O" . org-clock-out)
  14311. ("Meta Data Editing")
  14312. ("t" . org-todo)
  14313. ("0" . (org-priority ?\ ))
  14314. ("1" . (org-priority ?A))
  14315. ("2" . (org-priority ?B))
  14316. ("3" . (org-priority ?C))
  14317. (";" . org-set-tags-command)
  14318. ("e" . org-set-effort)
  14319. ("Agenda Views etc")
  14320. ("v" . org-agenda)
  14321. ("/" . org-sparse-tree)
  14322. ("Misc")
  14323. ("o" . org-open-at-point)
  14324. ("?" . org-speed-command-help)
  14325. )
  14326. "The default speed commands.")
  14327. (defun org-print-speed-command (e)
  14328. (if (> (length (car e)) 1)
  14329. (progn
  14330. (princ "\n")
  14331. (princ (car e))
  14332. (princ "\n")
  14333. (princ (make-string (length (car e)) ?-))
  14334. (princ "\n"))
  14335. (princ (car e))
  14336. (princ " ")
  14337. (if (symbolp (cdr e))
  14338. (princ (symbol-name (cdr e)))
  14339. (prin1 (cdr e)))
  14340. (princ "\n")))
  14341. (defun org-speed-command-help ()
  14342. "Show the available speed commands."
  14343. (interactive)
  14344. (if (not org-use-speed-commands)
  14345. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14346. (with-output-to-temp-buffer "*Help*"
  14347. (princ "User-defined Speed commands\n===========================\n")
  14348. (mapc 'org-print-speed-command org-speed-commands-user)
  14349. (princ "\n")
  14350. (princ "Built-in Speed commands\n=======================\n")
  14351. (mapc 'org-print-speed-command org-speed-commands-default))
  14352. (with-current-buffer "*Help*"
  14353. (setq truncate-lines t))))
  14354. (defun org-speed-move-safe (cmd)
  14355. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14356. If not, return to the original position and throw an error."
  14357. (interactive)
  14358. (let ((pos (point)))
  14359. (call-interactively cmd)
  14360. (unless (and (bolp) (org-on-heading-p))
  14361. (goto-char pos)
  14362. (error "Boundary reached while executing %s" cmd))))
  14363. (defvar org-self-insert-command-undo-counter 0)
  14364. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14365. (defvar org-speed-command nil)
  14366. (defun org-self-insert-command (N)
  14367. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14368. If the cursor is in a table looking at whitespace, the whitespace is
  14369. overwritten, and the table is not marked as requiring realignment."
  14370. (interactive "p")
  14371. (cond
  14372. ((and org-use-speed-commands
  14373. (or (and (bolp) (looking-at outline-regexp))
  14374. (and (functionp org-use-speed-commands)
  14375. (funcall org-use-speed-commands)))
  14376. (setq
  14377. org-speed-command
  14378. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14379. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14380. (cond
  14381. ((commandp org-speed-command)
  14382. (setq this-command org-speed-command)
  14383. (call-interactively org-speed-command))
  14384. ((functionp org-speed-command)
  14385. (funcall org-speed-command))
  14386. ((and org-speed-command (listp org-speed-command))
  14387. (eval org-speed-command))
  14388. (t (let (org-use-speed-commands)
  14389. (call-interactively 'org-self-insert-command)))))
  14390. ((and
  14391. (org-table-p)
  14392. (progn
  14393. ;; check if we blank the field, and if that triggers align
  14394. (and (featurep 'org-table) org-table-auto-blank-field
  14395. (member last-command
  14396. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14397. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14398. ;; got extra space, this field does not determine column width
  14399. (let (org-table-may-need-update) (org-table-blank-field))
  14400. ;; no extra space, this field may determine column width
  14401. (org-table-blank-field)))
  14402. t)
  14403. (eq N 1)
  14404. (looking-at "[^|\n]* |"))
  14405. (let (org-table-may-need-update)
  14406. (goto-char (1- (match-end 0)))
  14407. (delete-backward-char 1)
  14408. (goto-char (match-beginning 0))
  14409. (self-insert-command N)))
  14410. (t
  14411. (setq org-table-may-need-update t)
  14412. (self-insert-command N)
  14413. (org-fix-tags-on-the-fly)
  14414. (if org-self-insert-cluster-for-undo
  14415. (if (not (eq last-command 'org-self-insert-command))
  14416. (setq org-self-insert-command-undo-counter 1)
  14417. (if (>= org-self-insert-command-undo-counter 20)
  14418. (setq org-self-insert-command-undo-counter 1)
  14419. (and (> org-self-insert-command-undo-counter 0)
  14420. buffer-undo-list
  14421. (not (cadr buffer-undo-list)) ; remove nil entry
  14422. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14423. (setq org-self-insert-command-undo-counter
  14424. (1+ org-self-insert-command-undo-counter))))))))
  14425. (defun org-fix-tags-on-the-fly ()
  14426. (when (and (equal (char-after (point-at-bol)) ?*)
  14427. (org-on-heading-p))
  14428. (org-align-tags-here org-tags-column)))
  14429. (defun org-delete-backward-char (N)
  14430. "Like `delete-backward-char', insert whitespace at field end in tables.
  14431. When deleting backwards, in tables this function will insert whitespace in
  14432. front of the next \"|\" separator, to keep the table aligned. The table will
  14433. still be marked for re-alignment if the field did fill the entire column,
  14434. because, in this case the deletion might narrow the column."
  14435. (interactive "p")
  14436. (if (and (org-table-p)
  14437. (eq N 1)
  14438. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14439. (looking-at ".*?|"))
  14440. (let ((pos (point))
  14441. (noalign (looking-at "[^|\n\r]* |"))
  14442. (c org-table-may-need-update))
  14443. (backward-delete-char N)
  14444. (skip-chars-forward "^|")
  14445. (insert " ")
  14446. (goto-char (1- pos))
  14447. ;; noalign: if there were two spaces at the end, this field
  14448. ;; does not determine the width of the column.
  14449. (if noalign (setq org-table-may-need-update c)))
  14450. (backward-delete-char N)
  14451. (org-fix-tags-on-the-fly)))
  14452. (defun org-delete-char (N)
  14453. "Like `delete-char', but insert whitespace at field end in tables.
  14454. When deleting characters, in tables this function will insert whitespace in
  14455. front of the next \"|\" separator, to keep the table aligned. The table will
  14456. still be marked for re-alignment if the field did fill the entire column,
  14457. because, in this case the deletion might narrow the column."
  14458. (interactive "p")
  14459. (if (and (org-table-p)
  14460. (not (bolp))
  14461. (not (= (char-after) ?|))
  14462. (eq N 1))
  14463. (if (looking-at ".*?|")
  14464. (let ((pos (point))
  14465. (noalign (looking-at "[^|\n\r]* |"))
  14466. (c org-table-may-need-update))
  14467. (replace-match (concat
  14468. (substring (match-string 0) 1 -1)
  14469. " |"))
  14470. (goto-char pos)
  14471. ;; noalign: if there were two spaces at the end, this field
  14472. ;; does not determine the width of the column.
  14473. (if noalign (setq org-table-may-need-update c)))
  14474. (delete-char N))
  14475. (delete-char N)
  14476. (org-fix-tags-on-the-fly)))
  14477. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14478. (put 'org-self-insert-command 'delete-selection t)
  14479. (put 'orgtbl-self-insert-command 'delete-selection t)
  14480. (put 'org-delete-char 'delete-selection 'supersede)
  14481. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14482. (put 'org-yank 'delete-selection 'yank)
  14483. ;; Make `flyspell-mode' delay after some commands
  14484. (put 'org-self-insert-command 'flyspell-delayed t)
  14485. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14486. (put 'org-delete-char 'flyspell-delayed t)
  14487. (put 'org-delete-backward-char 'flyspell-delayed t)
  14488. ;; Make pabbrev-mode expand after org-mode commands
  14489. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14490. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14491. ;; How to do this: Measure non-white length of current string
  14492. ;; If equal to column width, we should realign.
  14493. (defun org-remap (map &rest commands)
  14494. "In MAP, remap the functions given in COMMANDS.
  14495. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14496. (let (new old)
  14497. (while commands
  14498. (setq old (pop commands) new (pop commands))
  14499. (if (fboundp 'command-remapping)
  14500. (org-defkey map (vector 'remap old) new)
  14501. (substitute-key-definition old new map global-map)))))
  14502. (when (eq org-enable-table-editor 'optimized)
  14503. ;; If the user wants maximum table support, we need to hijack
  14504. ;; some standard editing functions
  14505. (org-remap org-mode-map
  14506. 'self-insert-command 'org-self-insert-command
  14507. 'delete-char 'org-delete-char
  14508. 'delete-backward-char 'org-delete-backward-char)
  14509. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14510. (defvar org-ctrl-c-ctrl-c-hook nil
  14511. "Hook for functions attaching themselves to `C-c C-c'.
  14512. This can be used to add additional functionality to the C-c C-c key which
  14513. executes context-dependent commands.
  14514. Each function will be called with no arguments. The function must check
  14515. if the context is appropriate for it to act. If yes, it should do its
  14516. thing and then return a non-nil value. If the context is wrong,
  14517. just do nothing and return nil.")
  14518. (defvar org-tab-first-hook nil
  14519. "Hook for functions to attach themselves to TAB.
  14520. See `org-ctrl-c-ctrl-c-hook' for more information.
  14521. This hook runs as the first action when TAB is pressed, even before
  14522. `org-cycle' messes around with the `outline-regexp' to cater for
  14523. inline tasks and plain list item folding.
  14524. If any function in this hook returns t, not other actions like table
  14525. field motion visibility cycling will be done.")
  14526. (defvar org-tab-after-check-for-table-hook nil
  14527. "Hook for functions to attach themselves to TAB.
  14528. See `org-ctrl-c-ctrl-c-hook' for more information.
  14529. This hook runs after it has been established that the cursor is not in a
  14530. table, but before checking if the cursor is in a headline or if global cycling
  14531. should be done.
  14532. If any function in this hook returns t, not other actions like visibility
  14533. cycling will be done.")
  14534. (defvar org-tab-after-check-for-cycling-hook nil
  14535. "Hook for functions to attach themselves to TAB.
  14536. See `org-ctrl-c-ctrl-c-hook' for more information.
  14537. This hook runs after it has been established that not table field motion and
  14538. not visibility should be done because of current context. This is probably
  14539. the place where a package like yasnippets can hook in.")
  14540. (defvar org-tab-before-tab-emulation-hook nil
  14541. "Hook for functions to attach themselves to TAB.
  14542. See `org-ctrl-c-ctrl-c-hook' for more information.
  14543. This hook runs after every other options for TAB have been exhausted, but
  14544. before indentation and \t insertion takes place.")
  14545. (defvar org-metaleft-hook nil
  14546. "Hook for functions attaching themselves to `M-left'.
  14547. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14548. (defvar org-metaright-hook nil
  14549. "Hook for functions attaching themselves to `M-right'.
  14550. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14551. (defvar org-metaup-hook nil
  14552. "Hook for functions attaching themselves to `M-up'.
  14553. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14554. (defvar org-metadown-hook nil
  14555. "Hook for functions attaching themselves to `M-down'.
  14556. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14557. (defvar org-shiftmetaleft-hook nil
  14558. "Hook for functions attaching themselves to `M-S-left'.
  14559. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14560. (defvar org-shiftmetaright-hook nil
  14561. "Hook for functions attaching themselves to `M-S-right'.
  14562. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14563. (defvar org-shiftmetaup-hook nil
  14564. "Hook for functions attaching themselves to `M-S-up'.
  14565. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14566. (defvar org-shiftmetadown-hook nil
  14567. "Hook for functions attaching themselves to `M-S-down'.
  14568. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14569. (defvar org-metareturn-hook nil
  14570. "Hook for functions attaching themselves to `M-RET'.
  14571. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14572. (defun org-modifier-cursor-error ()
  14573. "Throw an error, a modified cursor command was applied in wrong context."
  14574. (error "This command is active in special context like tables, headlines or items"))
  14575. (defun org-shiftselect-error ()
  14576. "Throw an error because Shift-Cursor command was applied in wrong context."
  14577. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14578. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14579. (error "This command works only in special context like headlines or timestamps")))
  14580. (defun org-call-for-shift-select (cmd)
  14581. (let ((this-command-keys-shift-translated t))
  14582. (call-interactively cmd)))
  14583. (defun org-shifttab (&optional arg)
  14584. "Global visibility cycling or move to previous table field.
  14585. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14586. on context.
  14587. See the individual commands for more information."
  14588. (interactive "P")
  14589. (cond
  14590. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14591. ((integerp arg)
  14592. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14593. (message "Content view to level: %d" arg)
  14594. (org-content (prefix-numeric-value arg2))
  14595. (setq org-cycle-global-status 'overview)))
  14596. (t (call-interactively 'org-global-cycle))))
  14597. (defun org-shiftmetaleft ()
  14598. "Promote subtree or delete table column.
  14599. Calls `org-promote-subtree', `org-outdent-item',
  14600. or `org-table-delete-column', depending on context.
  14601. See the individual commands for more information."
  14602. (interactive)
  14603. (cond
  14604. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14605. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14606. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14607. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14608. (t (org-modifier-cursor-error))))
  14609. (defun org-shiftmetaright ()
  14610. "Demote subtree or insert table column.
  14611. Calls `org-demote-subtree', `org-indent-item',
  14612. or `org-table-insert-column', depending on context.
  14613. See the individual commands for more information."
  14614. (interactive)
  14615. (cond
  14616. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14617. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14618. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14619. ((org-at-item-p) (call-interactively 'org-indent-item))
  14620. (t (org-modifier-cursor-error))))
  14621. (defun org-shiftmetaup (&optional arg)
  14622. "Move subtree up or kill table row.
  14623. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14624. `org-move-item-up' depending on context. See the individual commands
  14625. for more information."
  14626. (interactive "P")
  14627. (cond
  14628. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14629. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14630. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14631. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14632. (t (org-modifier-cursor-error))))
  14633. (defun org-shiftmetadown (&optional arg)
  14634. "Move subtree down or insert table row.
  14635. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14636. `org-move-item-down', depending on context. See the individual
  14637. commands for more information."
  14638. (interactive "P")
  14639. (cond
  14640. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14641. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14642. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14643. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14644. (t (org-modifier-cursor-error))))
  14645. (defun org-metaleft (&optional arg)
  14646. "Promote heading or move table column to left.
  14647. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14648. With no specific context, calls the Emacs default `backward-word'.
  14649. See the individual commands for more information."
  14650. (interactive "P")
  14651. (cond
  14652. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14653. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14654. ((or (org-on-heading-p)
  14655. (and (org-region-active-p)
  14656. (save-excursion
  14657. (goto-char (region-beginning))
  14658. (org-on-heading-p))))
  14659. (call-interactively 'org-do-promote))
  14660. ((or (org-at-item-p)
  14661. (and (org-region-active-p)
  14662. (save-excursion
  14663. (goto-char (region-beginning))
  14664. (org-at-item-p))))
  14665. (call-interactively 'org-outdent-item))
  14666. (t (call-interactively 'backward-word))))
  14667. (defun org-metaright (&optional arg)
  14668. "Demote subtree or move table column to right.
  14669. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14670. With no specific context, calls the Emacs default `forward-word'.
  14671. See the individual commands for more information."
  14672. (interactive "P")
  14673. (cond
  14674. ((run-hook-with-args-until-success 'org-metaright-hook))
  14675. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14676. ((or (org-on-heading-p)
  14677. (and (org-region-active-p)
  14678. (save-excursion
  14679. (goto-char (region-beginning))
  14680. (org-on-heading-p))))
  14681. (call-interactively 'org-do-demote))
  14682. ((or (org-at-item-p)
  14683. (and (org-region-active-p)
  14684. (save-excursion
  14685. (goto-char (region-beginning))
  14686. (org-at-item-p))))
  14687. (call-interactively 'org-indent-item))
  14688. (t (call-interactively 'forward-word))))
  14689. (defun org-metaup (&optional arg)
  14690. "Move subtree up or move table row up.
  14691. Calls `org-move-subtree-up' or `org-table-move-row' or
  14692. `org-move-item-up', depending on context. See the individual commands
  14693. for more information."
  14694. (interactive "P")
  14695. (cond
  14696. ((run-hook-with-args-until-success 'org-metaup-hook))
  14697. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14698. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14699. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14700. (t (transpose-lines 1) (beginning-of-line -1))))
  14701. (defun org-metadown (&optional arg)
  14702. "Move subtree down or move table row down.
  14703. Calls `org-move-subtree-down' or `org-table-move-row' or
  14704. `org-move-item-down', depending on context. See the individual
  14705. commands for more information."
  14706. (interactive "P")
  14707. (cond
  14708. ((run-hook-with-args-until-success 'org-metadown-hook))
  14709. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14710. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14711. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14712. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14713. (defun org-shiftup (&optional arg)
  14714. "Increase item in timestamp or increase priority of current headline.
  14715. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14716. depending on context. See the individual commands for more information."
  14717. (interactive "P")
  14718. (cond
  14719. ((and org-support-shift-select (org-region-active-p))
  14720. (org-call-for-shift-select 'previous-line))
  14721. ((org-at-timestamp-p t)
  14722. (call-interactively (if org-edit-timestamp-down-means-later
  14723. 'org-timestamp-down 'org-timestamp-up)))
  14724. ((and (not (eq org-support-shift-select 'always))
  14725. org-enable-priority-commands
  14726. (org-on-heading-p))
  14727. (call-interactively 'org-priority-up))
  14728. ((and (not org-support-shift-select) (org-at-item-p))
  14729. (call-interactively 'org-previous-item))
  14730. ((org-clocktable-try-shift 'up arg))
  14731. (org-support-shift-select
  14732. (org-call-for-shift-select 'previous-line))
  14733. (t (org-shiftselect-error))))
  14734. (defun org-shiftdown (&optional arg)
  14735. "Decrease item in timestamp or decrease priority of current headline.
  14736. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14737. depending on context. See the individual commands for more information."
  14738. (interactive "P")
  14739. (cond
  14740. ((and org-support-shift-select (org-region-active-p))
  14741. (org-call-for-shift-select 'next-line))
  14742. ((org-at-timestamp-p t)
  14743. (call-interactively (if org-edit-timestamp-down-means-later
  14744. 'org-timestamp-up 'org-timestamp-down)))
  14745. ((and (not (eq org-support-shift-select 'always))
  14746. org-enable-priority-commands
  14747. (org-on-heading-p))
  14748. (call-interactively 'org-priority-down))
  14749. ((and (not org-support-shift-select) (org-at-item-p))
  14750. (call-interactively 'org-next-item))
  14751. ((org-clocktable-try-shift 'down arg))
  14752. (org-support-shift-select
  14753. (org-call-for-shift-select 'next-line))
  14754. (t (org-shiftselect-error))))
  14755. (defun org-shiftright (&optional arg)
  14756. "Cycle the thing at point or in the current line, depending on context.
  14757. Depending on context, this does one of the following:
  14758. - switch a timestamp at point one day into the future
  14759. - on a headline, switch to the next TODO keyword.
  14760. - on an item, switch entire list to the next bullet type
  14761. - on a property line, switch to the next allowed value
  14762. - on a clocktable definition line, move time block into the future"
  14763. (interactive "P")
  14764. (cond
  14765. ((and org-support-shift-select (org-region-active-p))
  14766. (org-call-for-shift-select 'forward-char))
  14767. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14768. ((and (not (eq org-support-shift-select 'always))
  14769. (org-on-heading-p))
  14770. (let ((org-inhibit-logging
  14771. (not org-treat-S-cursor-todo-selection-as-state-change))
  14772. (org-inhibit-blocking
  14773. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14774. (org-call-with-arg 'org-todo 'right)))
  14775. ((or (and org-support-shift-select
  14776. (not (eq org-support-shift-select 'always))
  14777. (org-at-item-bullet-p))
  14778. (and (not org-support-shift-select) (org-at-item-p)))
  14779. (org-call-with-arg 'org-cycle-list-bullet nil))
  14780. ((and (not (eq org-support-shift-select 'always))
  14781. (org-at-property-p))
  14782. (call-interactively 'org-property-next-allowed-value))
  14783. ((org-clocktable-try-shift 'right arg))
  14784. (org-support-shift-select
  14785. (org-call-for-shift-select 'forward-char))
  14786. (t (org-shiftselect-error))))
  14787. (defun org-shiftleft (&optional arg)
  14788. "Cycle the thing at point or in the current line, depending on context.
  14789. Depending on context, this does one of the following:
  14790. - switch a timestamp at point one day into the past
  14791. - on a headline, switch to the previous TODO keyword.
  14792. - on an item, switch entire list to the previous bullet type
  14793. - on a property line, switch to the previous allowed value
  14794. - on a clocktable definition line, move time block into the past"
  14795. (interactive "P")
  14796. (cond
  14797. ((and org-support-shift-select (org-region-active-p))
  14798. (org-call-for-shift-select 'backward-char))
  14799. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14800. ((and (not (eq org-support-shift-select 'always))
  14801. (org-on-heading-p))
  14802. (let ((org-inhibit-logging
  14803. (not org-treat-S-cursor-todo-selection-as-state-change))
  14804. (org-inhibit-blocking
  14805. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14806. (org-call-with-arg 'org-todo 'left)))
  14807. ((or (and org-support-shift-select
  14808. (not (eq org-support-shift-select 'always))
  14809. (org-at-item-bullet-p))
  14810. (and (not org-support-shift-select) (org-at-item-p)))
  14811. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14812. ((and (not (eq org-support-shift-select 'always))
  14813. (org-at-property-p))
  14814. (call-interactively 'org-property-previous-allowed-value))
  14815. ((org-clocktable-try-shift 'left arg))
  14816. (org-support-shift-select
  14817. (org-call-for-shift-select 'backward-char))
  14818. (t (org-shiftselect-error))))
  14819. (defun org-shiftcontrolright ()
  14820. "Switch to next TODO set."
  14821. (interactive)
  14822. (cond
  14823. ((and org-support-shift-select (org-region-active-p))
  14824. (org-call-for-shift-select 'forward-word))
  14825. ((and (not (eq org-support-shift-select 'always))
  14826. (org-on-heading-p))
  14827. (org-call-with-arg 'org-todo 'nextset))
  14828. (org-support-shift-select
  14829. (org-call-for-shift-select 'forward-word))
  14830. (t (org-shiftselect-error))))
  14831. (defun org-shiftcontrolleft ()
  14832. "Switch to previous TODO set."
  14833. (interactive)
  14834. (cond
  14835. ((and org-support-shift-select (org-region-active-p))
  14836. (org-call-for-shift-select 'backward-word))
  14837. ((and (not (eq org-support-shift-select 'always))
  14838. (org-on-heading-p))
  14839. (org-call-with-arg 'org-todo 'previousset))
  14840. (org-support-shift-select
  14841. (org-call-for-shift-select 'backward-word))
  14842. (t (org-shiftselect-error))))
  14843. (defun org-ctrl-c-ret ()
  14844. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14845. (interactive)
  14846. (cond
  14847. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14848. (t (call-interactively 'org-insert-heading))))
  14849. (defun org-copy-special ()
  14850. "Copy region in table or copy current subtree.
  14851. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14852. See the individual commands for more information."
  14853. (interactive)
  14854. (call-interactively
  14855. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14856. (defun org-cut-special ()
  14857. "Cut region in table or cut current subtree.
  14858. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14859. See the individual commands for more information."
  14860. (interactive)
  14861. (call-interactively
  14862. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14863. (defun org-paste-special (arg)
  14864. "Paste rectangular region into table, or past subtree relative to level.
  14865. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14866. See the individual commands for more information."
  14867. (interactive "P")
  14868. (if (org-at-table-p)
  14869. (org-table-paste-rectangle)
  14870. (org-paste-subtree arg)))
  14871. (defun org-edit-special ()
  14872. "Call a special editor for the stuff at point.
  14873. When at a table, call the formula editor with `org-table-edit-formulas'.
  14874. When at the first line of an src example, call `org-edit-src-code'.
  14875. When in an #+include line, visit the include file. Otherwise call
  14876. `ffap' to visit the file at point."
  14877. (interactive)
  14878. (cond
  14879. ((org-at-table.el-p)
  14880. (org-edit-src-code))
  14881. ((org-at-table-p)
  14882. (call-interactively 'org-table-edit-formulas))
  14883. ((save-excursion
  14884. (beginning-of-line 1)
  14885. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14886. (find-file (org-trim (match-string 1))))
  14887. ((org-edit-src-code))
  14888. ((org-edit-fixed-width-region))
  14889. (t (call-interactively 'ffap))))
  14890. (defun org-ctrl-c-ctrl-c (&optional arg)
  14891. "Set tags in headline, or update according to changed information at point.
  14892. This command does many different things, depending on context:
  14893. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14894. this is what we do.
  14895. - If the cursor is on a statistics cookie, update it.
  14896. - If the cursor is in a headline, prompt for tags and insert them
  14897. into the current line, aligned to `org-tags-column'. When called
  14898. with prefix arg, realign all tags in the current buffer.
  14899. - If the cursor is in one of the special #+KEYWORD lines, this
  14900. triggers scanning the buffer for these lines and updating the
  14901. information.
  14902. - If the cursor is inside a table, realign the table. This command
  14903. works even if the automatic table editor has been turned off.
  14904. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14905. the entire table.
  14906. - If the cursor is at a footnote reference or definition, jump to
  14907. the corresponding definition or references, respectively.
  14908. - If the cursor is a the beginning of a dynamic block, update it.
  14909. - If the current buffer is a remember buffer, close note and file
  14910. it. A prefix argument of 1 files to the default location
  14911. without further interaction. A prefix argument of 2 files to
  14912. the currently clocking task.
  14913. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14914. links in this buffer.
  14915. - If the cursor is on a numbered item in a plain list, renumber the
  14916. ordered list.
  14917. - If the cursor is on a checkbox, toggle it."
  14918. (interactive "P")
  14919. (let ((org-enable-table-editor t))
  14920. (cond
  14921. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14922. org-occur-highlights
  14923. org-latex-fragment-image-overlays)
  14924. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14925. (org-remove-occur-highlights)
  14926. (org-remove-latex-fragment-image-overlays)
  14927. (message "Temporary highlights/overlays removed from current buffer"))
  14928. ((and (local-variable-p 'org-finish-function (current-buffer))
  14929. (fboundp org-finish-function))
  14930. (funcall org-finish-function))
  14931. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14932. ((or (looking-at org-property-start-re)
  14933. (org-at-property-p))
  14934. (call-interactively 'org-property-action))
  14935. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14936. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14937. (or (org-on-heading-p) (org-at-item-p)))
  14938. (call-interactively 'org-update-statistics-cookies))
  14939. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14940. ((org-at-table.el-p)
  14941. (message "Use C-c ' to edit table.el tables"))
  14942. ((org-at-table-p)
  14943. (org-table-maybe-eval-formula)
  14944. (if arg
  14945. (call-interactively 'org-table-recalculate)
  14946. (org-table-maybe-recalculate-line))
  14947. (call-interactively 'org-table-align))
  14948. ((or (org-footnote-at-reference-p)
  14949. (org-footnote-at-definition-p))
  14950. (call-interactively 'org-footnote-action))
  14951. ((org-at-item-checkbox-p)
  14952. (call-interactively 'org-toggle-checkbox))
  14953. ((org-at-item-p)
  14954. (if arg
  14955. (call-interactively 'org-toggle-checkbox)
  14956. (call-interactively 'org-maybe-renumber-ordered-list)))
  14957. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14958. ;; Dynamic block
  14959. (beginning-of-line 1)
  14960. (save-excursion (org-update-dblock)))
  14961. ((save-excursion
  14962. (beginning-of-line 1)
  14963. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14964. (cond
  14965. ((equal (match-string 1) "TBLFM")
  14966. ;; Recalculate the table before this line
  14967. (save-excursion
  14968. (beginning-of-line 1)
  14969. (skip-chars-backward " \r\n\t")
  14970. (if (org-at-table-p)
  14971. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14972. (t
  14973. (let ((org-inhibit-startup-visibility-stuff t)
  14974. (org-startup-align-all-tables nil))
  14975. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14976. (message "Local setup has been refreshed"))))
  14977. ((org-clock-update-time-maybe))
  14978. (t (error "C-c C-c can do nothing useful at this location")))))
  14979. (defun org-mode-restart ()
  14980. "Restart Org-mode, to scan again for special lines.
  14981. Also updates the keyword regular expressions."
  14982. (interactive)
  14983. (org-mode)
  14984. (message "Org-mode restarted"))
  14985. (defun org-kill-note-or-show-branches ()
  14986. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14987. (interactive)
  14988. (if (not org-finish-function)
  14989. (call-interactively 'show-branches)
  14990. (let ((org-note-abort t))
  14991. (funcall org-finish-function))))
  14992. (defun org-return (&optional indent)
  14993. "Goto next table row or insert a newline.
  14994. Calls `org-table-next-row' or `newline', depending on context.
  14995. See the individual commands for more information."
  14996. (interactive)
  14997. (cond
  14998. ((bobp) (if indent (newline-and-indent) (newline)))
  14999. ((org-at-table-p)
  15000. (org-table-justify-field-maybe)
  15001. (call-interactively 'org-table-next-row))
  15002. ((and org-return-follows-link
  15003. (eq (get-text-property (point) 'face) 'org-link))
  15004. (call-interactively 'org-open-at-point))
  15005. ((and (org-at-heading-p)
  15006. (looking-at
  15007. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15008. (org-show-entry)
  15009. (end-of-line 1)
  15010. (newline))
  15011. (t (if indent (newline-and-indent) (newline)))))
  15012. (defun org-return-indent ()
  15013. "Goto next table row or insert a newline and indent.
  15014. Calls `org-table-next-row' or `newline-and-indent', depending on
  15015. context. See the individual commands for more information."
  15016. (interactive)
  15017. (org-return t))
  15018. (defun org-ctrl-c-star ()
  15019. "Compute table, or change heading status of lines.
  15020. Calls `org-table-recalculate' or `org-toggle-heading',
  15021. depending on context."
  15022. (interactive)
  15023. (cond
  15024. ((org-at-table-p)
  15025. (call-interactively 'org-table-recalculate))
  15026. (t
  15027. ;; Convert all lines in region to list items
  15028. (call-interactively 'org-toggle-heading))))
  15029. (defun org-ctrl-c-minus ()
  15030. "Insert separator line in table or modify bullet status of line.
  15031. Also turns a plain line or a region of lines into list items.
  15032. Calls `org-table-insert-hline', `org-toggle-item', or
  15033. `org-cycle-list-bullet', depending on context."
  15034. (interactive)
  15035. (cond
  15036. ((org-at-table-p)
  15037. (call-interactively 'org-table-insert-hline))
  15038. ((org-region-active-p)
  15039. (call-interactively 'org-toggle-item))
  15040. ((org-in-item-p)
  15041. (call-interactively 'org-cycle-list-bullet))
  15042. (t
  15043. (call-interactively 'org-toggle-item))))
  15044. (defun org-toggle-item ()
  15045. "Convert headings or normal lines to items, items to normal lines.
  15046. If there is no active region, only the current line is considered.
  15047. If the first line in the region is a headline, convert all headlines to items.
  15048. If the first line in the region is an item, convert all items to normal lines.
  15049. If the first line is normal text, add an item bullet to each line."
  15050. (interactive)
  15051. (let (l2 l beg end)
  15052. (if (org-region-active-p)
  15053. (setq beg (region-beginning) end (region-end))
  15054. (setq beg (point-at-bol)
  15055. end (min (1+ (point-at-eol)) (point-max))))
  15056. (save-excursion
  15057. (goto-char end)
  15058. (setq l2 (org-current-line))
  15059. (goto-char beg)
  15060. (beginning-of-line 1)
  15061. (setq l (1- (org-current-line)))
  15062. (if (org-at-item-p)
  15063. ;; We already have items, de-itemize
  15064. (while (< (setq l (1+ l)) l2)
  15065. (when (org-at-item-p)
  15066. (goto-char (match-beginning 2))
  15067. (delete-region (match-beginning 2) (match-end 2))
  15068. (and (looking-at "[ \t]+") (replace-match "")))
  15069. (beginning-of-line 2))
  15070. (if (org-on-heading-p)
  15071. ;; Headings, convert to items
  15072. (while (< (setq l (1+ l)) l2)
  15073. (if (looking-at org-outline-regexp)
  15074. (replace-match "- " t t))
  15075. (beginning-of-line 2))
  15076. ;; normal lines, turn them into items
  15077. (while (< (setq l (1+ l)) l2)
  15078. (unless (org-at-item-p)
  15079. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15080. (replace-match "\\1- \\2")))
  15081. (beginning-of-line 2)))))))
  15082. (defun org-toggle-heading (&optional nstars)
  15083. "Convert headings to normal text, or items or text to headings.
  15084. If there is no active region, only the current line is considered.
  15085. If the first line is a heading, remove the stars from all headlines
  15086. in the region.
  15087. If the first line is a plain list item, turn all plain list items
  15088. into headings.
  15089. If the first line is a normal line, turn each and every line in the
  15090. region into a heading.
  15091. When converting a line into a heading, the number of stars is chosen
  15092. such that the lines become children of the current entry. However,
  15093. when a prefix argument is given, its value determines the number of
  15094. stars to add."
  15095. (interactive "P")
  15096. (let (l2 l itemp beg end)
  15097. (if (org-region-active-p)
  15098. (setq beg (region-beginning) end (region-end))
  15099. (setq beg (point-at-bol)
  15100. end (min (1+ (point-at-eol)) (point-max))))
  15101. (save-excursion
  15102. (goto-char end)
  15103. (setq l2 (org-current-line))
  15104. (goto-char beg)
  15105. (beginning-of-line 1)
  15106. (setq l (1- (org-current-line)))
  15107. (if (org-on-heading-p)
  15108. ;; We already have headlines, de-star them
  15109. (while (< (setq l (1+ l)) l2)
  15110. (when (org-on-heading-p t)
  15111. (and (looking-at outline-regexp) (replace-match "")))
  15112. (beginning-of-line 2))
  15113. (setq itemp (org-at-item-p))
  15114. (let* ((stars
  15115. (if nstars
  15116. (make-string (prefix-numeric-value current-prefix-arg)
  15117. ?*)
  15118. (save-excursion
  15119. (if (re-search-backward org-complex-heading-regexp nil t)
  15120. (match-string 1) ""))))
  15121. (add-stars (cond (nstars "")
  15122. ((equal stars "") "*")
  15123. (org-odd-levels-only "**")
  15124. (t "*")))
  15125. (rpl (concat stars add-stars " ")))
  15126. (while (< (setq l (1+ l)) l2)
  15127. (if itemp
  15128. (and (org-at-item-p) (replace-match rpl t t))
  15129. (unless (org-on-heading-p)
  15130. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15131. (replace-match (concat rpl (match-string 2))))))
  15132. (beginning-of-line 2)))))))
  15133. (defun org-meta-return (&optional arg)
  15134. "Insert a new heading or wrap a region in a table.
  15135. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15136. See the individual commands for more information."
  15137. (interactive "P")
  15138. (cond
  15139. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15140. ((org-at-table-p)
  15141. (call-interactively 'org-table-wrap-region))
  15142. (t (call-interactively 'org-insert-heading))))
  15143. ;;; Menu entries
  15144. ;; Define the Org-mode menus
  15145. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15146. '("Tbl"
  15147. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15148. ["Next Field" org-cycle (org-at-table-p)]
  15149. ["Previous Field" org-shifttab (org-at-table-p)]
  15150. ["Next Row" org-return (org-at-table-p)]
  15151. "--"
  15152. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15153. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15154. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15155. "--"
  15156. ("Column"
  15157. ["Move Column Left" org-metaleft (org-at-table-p)]
  15158. ["Move Column Right" org-metaright (org-at-table-p)]
  15159. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15160. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15161. ("Row"
  15162. ["Move Row Up" org-metaup (org-at-table-p)]
  15163. ["Move Row Down" org-metadown (org-at-table-p)]
  15164. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15165. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15166. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15167. "--"
  15168. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15169. ("Rectangle"
  15170. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15171. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15172. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15173. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15174. "--"
  15175. ("Calculate"
  15176. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15177. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15178. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15179. "--"
  15180. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15181. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15182. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15183. "--"
  15184. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15185. "--"
  15186. ["Sum Column/Rectangle" org-table-sum
  15187. (or (org-at-table-p) (org-region-active-p))]
  15188. ["Which Column?" org-table-current-column (org-at-table-p)])
  15189. ["Debug Formulas"
  15190. org-table-toggle-formula-debugger
  15191. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15192. ["Show Col/Row Numbers"
  15193. org-table-toggle-coordinate-overlays
  15194. :style toggle
  15195. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15196. "--"
  15197. ["Create" org-table-create (and (not (org-at-table-p))
  15198. org-enable-table-editor)]
  15199. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15200. ["Import from File" org-table-import (not (org-at-table-p))]
  15201. ["Export to File" org-table-export (org-at-table-p)]
  15202. "--"
  15203. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15204. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15205. '("Org"
  15206. ("Show/Hide"
  15207. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15208. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15209. ["Sparse Tree..." org-sparse-tree t]
  15210. ["Reveal Context" org-reveal t]
  15211. ["Show All" show-all t]
  15212. "--"
  15213. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15214. "--"
  15215. ["New Heading" org-insert-heading t]
  15216. ("Navigate Headings"
  15217. ["Up" outline-up-heading t]
  15218. ["Next" outline-next-visible-heading t]
  15219. ["Previous" outline-previous-visible-heading t]
  15220. ["Next Same Level" outline-forward-same-level t]
  15221. ["Previous Same Level" outline-backward-same-level t]
  15222. "--"
  15223. ["Jump" org-goto t])
  15224. ("Edit Structure"
  15225. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15226. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15227. "--"
  15228. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15229. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15230. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15231. "--"
  15232. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15233. "--"
  15234. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15235. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15236. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15237. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15238. "--"
  15239. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15240. "--"
  15241. ["Convert to odd levels" org-convert-to-odd-levels t]
  15242. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15243. ("Editing"
  15244. ["Emphasis..." org-emphasize t]
  15245. ["Edit Source Example" org-edit-special t]
  15246. "--"
  15247. ["Footnote new/jump" org-footnote-action t]
  15248. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15249. ("Archive"
  15250. ["Archive (default method)" org-archive-subtree-default t]
  15251. "--"
  15252. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15253. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15254. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15255. )
  15256. "--"
  15257. ("Hyperlinks"
  15258. ["Store Link (Global)" org-store-link t]
  15259. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15260. ["Insert Link" org-insert-link t]
  15261. ["Follow Link" org-open-at-point t]
  15262. "--"
  15263. ["Next link" org-next-link t]
  15264. ["Previous link" org-previous-link t]
  15265. "--"
  15266. ["Descriptive Links"
  15267. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15268. :style radio
  15269. :selected (member '(org-link) buffer-invisibility-spec)]
  15270. ["Literal Links"
  15271. (progn
  15272. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15273. :style radio
  15274. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15275. "--"
  15276. ("TODO Lists"
  15277. ["TODO/DONE/-" org-todo t]
  15278. ("Select keyword"
  15279. ["Next keyword" org-shiftright (org-on-heading-p)]
  15280. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15281. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15282. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15283. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15284. ["Show TODO Tree" org-show-todo-tree t]
  15285. ["Global TODO list" org-todo-list t]
  15286. "--"
  15287. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15288. :selected org-enforce-todo-dependencies :style toggle :active t]
  15289. "Settings for tree at point"
  15290. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15291. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15292. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15293. ["Do Children parallel" org-toggle-ordered-property :style radio
  15294. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15295. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15296. "--"
  15297. ["Set Priority" org-priority t]
  15298. ["Priority Up" org-shiftup t]
  15299. ["Priority Down" org-shiftdown t]
  15300. "--"
  15301. ["Get news from all feeds" org-feed-update-all t]
  15302. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15303. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15304. ("TAGS and Properties"
  15305. ["Set Tags" org-set-tags-command t]
  15306. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15307. "--"
  15308. ["Set property" org-set-property t]
  15309. ["Column view of properties" org-columns t]
  15310. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15311. ("Dates and Scheduling"
  15312. ["Timestamp" org-time-stamp t]
  15313. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15314. ("Change Date"
  15315. ["1 Day Later" org-shiftright t]
  15316. ["1 Day Earlier" org-shiftleft t]
  15317. ["1 ... Later" org-shiftup t]
  15318. ["1 ... Earlier" org-shiftdown t])
  15319. ["Compute Time Range" org-evaluate-time-range t]
  15320. ["Schedule Item" org-schedule t]
  15321. ["Deadline" org-deadline t]
  15322. "--"
  15323. ["Custom time format" org-toggle-time-stamp-overlays
  15324. :style radio :selected org-display-custom-times]
  15325. "--"
  15326. ["Goto Calendar" org-goto-calendar t]
  15327. ["Date from Calendar" org-date-from-calendar t]
  15328. "--"
  15329. ["Start/Restart Timer" org-timer-start t]
  15330. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15331. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15332. ["Insert Timer String" org-timer t]
  15333. ["Insert Timer Item" org-timer-item t])
  15334. ("Logging work"
  15335. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15336. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15337. ["Clock out" org-clock-out t]
  15338. ["Clock cancel" org-clock-cancel t]
  15339. "--"
  15340. ["Mark as default task" org-clock-mark-default-task t]
  15341. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15342. ["Goto running clock" org-clock-goto t]
  15343. "--"
  15344. ["Display times" org-clock-display t]
  15345. ["Create clock table" org-clock-report t]
  15346. "--"
  15347. ["Record DONE time"
  15348. (progn (setq org-log-done (not org-log-done))
  15349. (message "Switching to %s will %s record a timestamp"
  15350. (car org-done-keywords)
  15351. (if org-log-done "automatically" "not")))
  15352. :style toggle :selected org-log-done])
  15353. "--"
  15354. ["Agenda Command..." org-agenda t]
  15355. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15356. ("File List for Agenda")
  15357. ("Special views current file"
  15358. ["TODO Tree" org-show-todo-tree t]
  15359. ["Check Deadlines" org-check-deadlines t]
  15360. ["Timeline" org-timeline t]
  15361. ["Tags/Property tree" org-match-sparse-tree t])
  15362. "--"
  15363. ["Export/Publish..." org-export t]
  15364. ("LaTeX"
  15365. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15366. :selected org-cdlatex-mode]
  15367. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15368. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15369. ["Modify math symbol" org-cdlatex-math-modify
  15370. (org-inside-LaTeX-fragment-p)]
  15371. ["Insert citation" org-reftex-citation t]
  15372. "--"
  15373. ["Export LaTeX fragments as images"
  15374. (if (featurep 'org-exp)
  15375. (setq org-export-with-LaTeX-fragments
  15376. (not org-export-with-LaTeX-fragments))
  15377. (require 'org-exp))
  15378. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15379. org-export-with-LaTeX-fragments)]
  15380. "--"
  15381. ["Template for BEAMER" org-beamer-settings-template t])
  15382. "--"
  15383. ("MobileOrg"
  15384. ["Push Files and Views" org-mobile-push t]
  15385. ["Get Captured and Flagged" org-mobile-pull t]
  15386. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15387. "--"
  15388. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15389. "--"
  15390. ("Documentation"
  15391. ["Show Version" org-version t]
  15392. ["Info Documentation" org-info t])
  15393. ("Customize"
  15394. ["Browse Org Group" org-customize t]
  15395. "--"
  15396. ["Expand This Menu" org-create-customize-menu
  15397. (fboundp 'customize-menu-create)])
  15398. ["Send bug report" org-submit-bug-report t]
  15399. "--"
  15400. ("Refresh/Reload"
  15401. ["Refresh setup current buffer" org-mode-restart t]
  15402. ["Reload Org (after update)" org-reload t]
  15403. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15404. ))
  15405. (defun org-info (&optional node)
  15406. "Read documentation for Org-mode in the info system.
  15407. With optional NODE, go directly to that node."
  15408. (interactive)
  15409. (info (format "(org)%s" (or node ""))))
  15410. ;;;###autoload
  15411. (defun org-submit-bug-report ()
  15412. "Submit a bug report on Org-mode via mail.
  15413. Don't hesitate to report any problems or inaccurate documentation.
  15414. If you don't have setup sending mail from (X)Emacs, please copy the
  15415. output buffer into your mail program, as it gives us important
  15416. information about your Org-mode version and configuration."
  15417. (interactive)
  15418. (require 'reporter)
  15419. (org-load-modules-maybe)
  15420. (org-require-autoloaded-modules)
  15421. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15422. (reporter-submit-bug-report
  15423. "emacs-orgmode@gnu.org"
  15424. (org-version)
  15425. (let (list)
  15426. (save-window-excursion
  15427. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15428. (delete-other-windows)
  15429. (erase-buffer)
  15430. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15431. We would like to add your full Org-mode and Outline configuration to the
  15432. bug report. This greatly simplifies the work of the maintainer and
  15433. other experts on the mailing list.
  15434. HOWEVER, some variables you have customized may contain private
  15435. information. The names of customers, colleagues, or friends, might
  15436. appear in the form of file names, tags, todo states, or search strings.
  15437. If you answer yes to the prompt, you might want to check and remove
  15438. such private information before sending the email.")
  15439. (add-text-properties (point-min) (point-max) '(face org-warning))
  15440. (when (yes-or-no-p "Include your Org-mode configuration ")
  15441. (mapatoms
  15442. (lambda (v)
  15443. (and (boundp v)
  15444. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15445. (or (and (symbol-value v)
  15446. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15447. (and
  15448. (get v 'custom-type) (get v 'standard-value)
  15449. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15450. (push v list)))))
  15451. (kill-buffer (get-buffer "*Warn about privacy*"))
  15452. list))
  15453. nil nil
  15454. "Remember to cover the basics, that is, what you expected to happen and
  15455. what in fact did happen. You don't know how to make a good report? See
  15456. http://orgmode.org/manual/Feedback.html#Feedback
  15457. Your bug report will be posted to the Org-mode mailing list.
  15458. ------------------------------------------------------------------------")
  15459. (save-excursion
  15460. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15461. (replace-match "\\1Bug: \\3 [\\2]")))))
  15462. (defun org-install-agenda-files-menu ()
  15463. (let ((bl (buffer-list)))
  15464. (save-excursion
  15465. (while bl
  15466. (set-buffer (pop bl))
  15467. (if (org-mode-p) (setq bl nil)))
  15468. (when (org-mode-p)
  15469. (easy-menu-change
  15470. '("Org") "File List for Agenda"
  15471. (append
  15472. (list
  15473. ["Edit File List" (org-edit-agenda-file-list) t]
  15474. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15475. ["Remove Current File from List" org-remove-file t]
  15476. ["Cycle through agenda files" org-cycle-agenda-files t]
  15477. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15478. "--")
  15479. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15480. ;;;; Documentation
  15481. ;;;###autoload
  15482. (defun org-require-autoloaded-modules ()
  15483. (interactive)
  15484. (mapc 'require
  15485. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15486. org-docbook org-exp org-html org-icalendar
  15487. org-id org-latex
  15488. org-publish org-remember org-table
  15489. org-timer org-xoxo)))
  15490. ;;;###autoload
  15491. (defun org-reload (&optional uncompiled)
  15492. "Reload all org lisp files.
  15493. With prefix arg UNCOMPILED, load the uncompiled versions."
  15494. (interactive "P")
  15495. (require 'find-func)
  15496. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15497. (dir-org (file-name-directory (org-find-library-name "org")))
  15498. (dir-org-contrib (ignore-errors
  15499. (file-name-directory
  15500. (org-find-library-name "org-contribdir"))))
  15501. (files
  15502. (append (directory-files dir-org t file-re)
  15503. (and dir-org-contrib
  15504. (directory-files dir-org-contrib t file-re))))
  15505. (remove-re (concat (if (featurep 'xemacs)
  15506. "org-colview" "org-colview-xemacs")
  15507. "\\'")))
  15508. (setq files (mapcar 'file-name-sans-extension files))
  15509. (setq files (mapcar
  15510. (lambda (x) (if (string-match remove-re x) nil x))
  15511. files))
  15512. (setq files (delq nil files))
  15513. (mapc
  15514. (lambda (f)
  15515. (when (featurep (intern (file-name-nondirectory f)))
  15516. (if (and (not uncompiled)
  15517. (file-exists-p (concat f ".elc")))
  15518. (load (concat f ".elc") nil nil t)
  15519. (load (concat f ".el") nil nil t))))
  15520. files))
  15521. (org-version))
  15522. ;;;###autoload
  15523. (defun org-customize ()
  15524. "Call the customize function with org as argument."
  15525. (interactive)
  15526. (org-load-modules-maybe)
  15527. (org-require-autoloaded-modules)
  15528. (customize-browse 'org))
  15529. (defun org-create-customize-menu ()
  15530. "Create a full customization menu for Org-mode, insert it into the menu."
  15531. (interactive)
  15532. (org-load-modules-maybe)
  15533. (org-require-autoloaded-modules)
  15534. (if (fboundp 'customize-menu-create)
  15535. (progn
  15536. (easy-menu-change
  15537. '("Org") "Customize"
  15538. `(["Browse Org group" org-customize t]
  15539. "--"
  15540. ,(customize-menu-create 'org)
  15541. ["Set" Custom-set t]
  15542. ["Save" Custom-save t]
  15543. ["Reset to Current" Custom-reset-current t]
  15544. ["Reset to Saved" Custom-reset-saved t]
  15545. ["Reset to Standard Settings" Custom-reset-standard t]))
  15546. (message "\"Org\"-menu now contains full customization menu"))
  15547. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15548. ;;;; Miscellaneous stuff
  15549. ;;; Generally useful functions
  15550. (defun org-get-at-bol (property)
  15551. "Get text property PROPERTY at beginning of line."
  15552. (get-text-property (point-at-bol) property))
  15553. (defun org-find-text-property-in-string (prop s)
  15554. "Return the first non-nil value of property PROP in string S."
  15555. (or (get-text-property 0 prop s)
  15556. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15557. prop s)))
  15558. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15559. "Display the given MESSAGE as a warning."
  15560. (if (fboundp 'display-warning)
  15561. (display-warning 'org message
  15562. (if (featurep 'xemacs)
  15563. 'warning
  15564. :warning))
  15565. (let ((buf (get-buffer-create "*Org warnings*")))
  15566. (with-current-buffer buf
  15567. (goto-char (point-max))
  15568. (insert "Warning (Org): " message)
  15569. (unless (bolp)
  15570. (newline)))
  15571. (display-buffer buf)
  15572. (sit-for 0))))
  15573. (defun org-in-commented-line ()
  15574. "Is point in a line starting with `#'?"
  15575. (equal (char-after (point-at-bol)) ?#))
  15576. (defun org-in-verbatim-emphasis ()
  15577. (save-match-data
  15578. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15579. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15580. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15581. (if (and marker (marker-buffer marker)
  15582. (buffer-live-p (marker-buffer marker)))
  15583. (progn
  15584. (switch-to-buffer (marker-buffer marker))
  15585. (if (or (> marker (point-max)) (< marker (point-min)))
  15586. (widen))
  15587. (goto-char marker)
  15588. (org-show-context 'org-goto))
  15589. (if bookmark
  15590. (bookmark-jump bookmark)
  15591. (error "Cannot find location"))))
  15592. (defun org-quote-csv-field (s)
  15593. "Quote field for inclusion in CSV material."
  15594. (if (string-match "[\",]" s)
  15595. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15596. s))
  15597. (defun org-plist-delete (plist property)
  15598. "Delete PROPERTY from PLIST.
  15599. This is in contrast to merely setting it to 0."
  15600. (let (p)
  15601. (while plist
  15602. (if (not (eq property (car plist)))
  15603. (setq p (plist-put p (car plist) (nth 1 plist))))
  15604. (setq plist (cddr plist)))
  15605. p))
  15606. (defun org-force-self-insert (N)
  15607. "Needed to enforce self-insert under remapping."
  15608. (interactive "p")
  15609. (self-insert-command N))
  15610. (defun org-string-width (s)
  15611. "Compute width of string, ignoring invisible characters.
  15612. This ignores character with invisibility property `org-link', and also
  15613. characters with property `org-cwidth', because these will become invisible
  15614. upon the next fontification round."
  15615. (let (b l)
  15616. (when (or (eq t buffer-invisibility-spec)
  15617. (assq 'org-link buffer-invisibility-spec))
  15618. (while (setq b (text-property-any 0 (length s)
  15619. 'invisible 'org-link s))
  15620. (setq s (concat (substring s 0 b)
  15621. (substring s (or (next-single-property-change
  15622. b 'invisible s) (length s)))))))
  15623. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15624. (setq s (concat (substring s 0 b)
  15625. (substring s (or (next-single-property-change
  15626. b 'org-cwidth s) (length s))))))
  15627. (setq l (string-width s) b -1)
  15628. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15629. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15630. l))
  15631. (defun org-get-indentation (&optional line)
  15632. "Get the indentation of the current line, interpreting tabs.
  15633. When LINE is given, assume it represents a line and compute its indentation."
  15634. (if line
  15635. (if (string-match "^ *" (org-remove-tabs line))
  15636. (match-end 0))
  15637. (save-excursion
  15638. (beginning-of-line 1)
  15639. (skip-chars-forward " \t")
  15640. (current-column))))
  15641. (defun org-remove-tabs (s &optional width)
  15642. "Replace tabulators in S with spaces.
  15643. Assumes that s is a single line, starting in column 0."
  15644. (setq width (or width tab-width))
  15645. (while (string-match "\t" s)
  15646. (setq s (replace-match
  15647. (make-string
  15648. (- (* width (/ (+ (match-beginning 0) width) width))
  15649. (match-beginning 0)) ?\ )
  15650. t t s)))
  15651. s)
  15652. (defun org-fix-indentation (line ind)
  15653. "Fix indentation in LINE.
  15654. IND is a cons cell with target and minimum indentation.
  15655. If the current indentation in LINE is smaller than the minimum,
  15656. leave it alone. If it is larger than ind, set it to the target."
  15657. (let* ((l (org-remove-tabs line))
  15658. (i (org-get-indentation l))
  15659. (i1 (car ind)) (i2 (cdr ind)))
  15660. (if (>= i i2) (setq l (substring line i2)))
  15661. (if (> i1 0)
  15662. (concat (make-string i1 ?\ ) l)
  15663. l)))
  15664. (defun org-remove-indentation (code &optional n)
  15665. "Remove the maximum common indentation from the lines in CODE.
  15666. N may optionally be the number of spaces to remove."
  15667. (with-temp-buffer
  15668. (insert code)
  15669. (org-do-remove-indentation n)
  15670. (buffer-string)))
  15671. (defun org-do-remove-indentation (&optional n)
  15672. "Remove the maximum common indentation from the buffer."
  15673. (untabify (point-min) (point-max))
  15674. (let ((min 10000) re)
  15675. (if n
  15676. (setq min n)
  15677. (goto-char (point-min))
  15678. (while (re-search-forward "^ *[^ \n]" nil t)
  15679. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15680. (unless (or (= min 0) (= min 10000))
  15681. (setq re (format "^ \\{%d\\}" min))
  15682. (goto-char (point-min))
  15683. (while (re-search-forward re nil t)
  15684. (replace-match "")
  15685. (end-of-line 1))
  15686. min)))
  15687. (defun org-fill-template (template alist)
  15688. "Find each %key of ALIST in TEMPLATE and replace it."
  15689. (let ((case-fold-search nil)
  15690. entry key value)
  15691. (setq alist (sort (copy-sequence alist)
  15692. (lambda (a b) (< (length (car a)) (length (car b))))))
  15693. (while (setq entry (pop alist))
  15694. (setq template
  15695. (replace-regexp-in-string
  15696. (concat "%" (regexp-quote (car entry)))
  15697. (cdr entry) template t t)))
  15698. template))
  15699. (defun org-base-buffer (buffer)
  15700. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15701. (if (not buffer)
  15702. buffer
  15703. (or (buffer-base-buffer buffer)
  15704. buffer)))
  15705. (defun org-trim (s)
  15706. "Remove whitespace at beginning and end of string."
  15707. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15708. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15709. s)
  15710. (defun org-wrap (string &optional width lines)
  15711. "Wrap string to either a number of lines, or a width in characters.
  15712. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15713. that costs. If there is a word longer than WIDTH, the text is actually
  15714. wrapped to the length of that word.
  15715. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15716. many lines, whatever width that takes.
  15717. The return value is a list of lines, without newlines at the end."
  15718. (let* ((words (org-split-string string "[ \t\n]+"))
  15719. (maxword (apply 'max (mapcar 'org-string-width words)))
  15720. w ll)
  15721. (cond (width
  15722. (org-do-wrap words (max maxword width)))
  15723. (lines
  15724. (setq w maxword)
  15725. (setq ll (org-do-wrap words maxword))
  15726. (if (<= (length ll) lines)
  15727. ll
  15728. (setq ll words)
  15729. (while (> (length ll) lines)
  15730. (setq w (1+ w))
  15731. (setq ll (org-do-wrap words w)))
  15732. ll))
  15733. (t (error "Cannot wrap this")))))
  15734. (defun org-do-wrap (words width)
  15735. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15736. (let (lines line)
  15737. (while words
  15738. (setq line (pop words))
  15739. (while (and words (< (+ (length line) (length (car words))) width))
  15740. (setq line (concat line " " (pop words))))
  15741. (setq lines (push line lines)))
  15742. (nreverse lines)))
  15743. (defun org-split-string (string &optional separators)
  15744. "Splits STRING into substrings at SEPARATORS.
  15745. No empty strings are returned if there are matches at the beginning
  15746. and end of string."
  15747. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15748. (start 0)
  15749. notfirst
  15750. (list nil))
  15751. (while (and (string-match rexp string
  15752. (if (and notfirst
  15753. (= start (match-beginning 0))
  15754. (< start (length string)))
  15755. (1+ start) start))
  15756. (< (match-beginning 0) (length string)))
  15757. (setq notfirst t)
  15758. (or (eq (match-beginning 0) 0)
  15759. (and (eq (match-beginning 0) (match-end 0))
  15760. (eq (match-beginning 0) start))
  15761. (setq list
  15762. (cons (substring string start (match-beginning 0))
  15763. list)))
  15764. (setq start (match-end 0)))
  15765. (or (eq start (length string))
  15766. (setq list
  15767. (cons (substring string start)
  15768. list)))
  15769. (nreverse list)))
  15770. (defun org-quote-vert (s)
  15771. "Replace \"|\" with \"\\vert\"."
  15772. (while (string-match "|" s)
  15773. (setq s (replace-match "\\vert" t t s)))
  15774. s)
  15775. (defun org-uuidgen-p (s)
  15776. "Is S an ID created by UUIDGEN?"
  15777. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15778. (defun org-context ()
  15779. "Return a list of contexts of the current cursor position.
  15780. If several contexts apply, all are returned.
  15781. Each context entry is a list with a symbol naming the context, and
  15782. two positions indicating start and end of the context. Possible
  15783. contexts are:
  15784. :headline anywhere in a headline
  15785. :headline-stars on the leading stars in a headline
  15786. :todo-keyword on a TODO keyword (including DONE) in a headline
  15787. :tags on the TAGS in a headline
  15788. :priority on the priority cookie in a headline
  15789. :item on the first line of a plain list item
  15790. :item-bullet on the bullet/number of a plain list item
  15791. :checkbox on the checkbox in a plain list item
  15792. :table in an org-mode table
  15793. :table-special on a special filed in a table
  15794. :table-table in a table.el table
  15795. :link on a hyperlink
  15796. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15797. :target on a <<target>>
  15798. :radio-target on a <<<radio-target>>>
  15799. :latex-fragment on a LaTeX fragment
  15800. :latex-preview on a LaTeX fragment with overlayed preview image
  15801. This function expects the position to be visible because it uses font-lock
  15802. faces as a help to recognize the following contexts: :table-special, :link,
  15803. and :keyword."
  15804. (let* ((f (get-text-property (point) 'face))
  15805. (faces (if (listp f) f (list f)))
  15806. (p (point)) clist o)
  15807. ;; First the large context
  15808. (cond
  15809. ((org-on-heading-p t)
  15810. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15811. (when (progn
  15812. (beginning-of-line 1)
  15813. (looking-at org-todo-line-tags-regexp))
  15814. (push (org-point-in-group p 1 :headline-stars) clist)
  15815. (push (org-point-in-group p 2 :todo-keyword) clist)
  15816. (push (org-point-in-group p 4 :tags) clist))
  15817. (goto-char p)
  15818. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15819. (if (looking-at "\\[#[A-Z0-9]\\]")
  15820. (push (org-point-in-group p 0 :priority) clist)))
  15821. ((org-at-item-p)
  15822. (push (org-point-in-group p 2 :item-bullet) clist)
  15823. (push (list :item (point-at-bol)
  15824. (save-excursion (org-end-of-item) (point)))
  15825. clist)
  15826. (and (org-at-item-checkbox-p)
  15827. (push (org-point-in-group p 0 :checkbox) clist)))
  15828. ((org-at-table-p)
  15829. (push (list :table (org-table-begin) (org-table-end)) clist)
  15830. (if (memq 'org-formula faces)
  15831. (push (list :table-special
  15832. (previous-single-property-change p 'face)
  15833. (next-single-property-change p 'face)) clist)))
  15834. ((org-at-table-p 'any)
  15835. (push (list :table-table) clist)))
  15836. (goto-char p)
  15837. ;; Now the small context
  15838. (cond
  15839. ((org-at-timestamp-p)
  15840. (push (org-point-in-group p 0 :timestamp) clist))
  15841. ((memq 'org-link faces)
  15842. (push (list :link
  15843. (previous-single-property-change p 'face)
  15844. (next-single-property-change p 'face)) clist))
  15845. ((memq 'org-special-keyword faces)
  15846. (push (list :keyword
  15847. (previous-single-property-change p 'face)
  15848. (next-single-property-change p 'face)) clist))
  15849. ((org-on-target-p)
  15850. (push (org-point-in-group p 0 :target) clist)
  15851. (goto-char (1- (match-beginning 0)))
  15852. (if (looking-at org-radio-target-regexp)
  15853. (push (org-point-in-group p 0 :radio-target) clist))
  15854. (goto-char p))
  15855. ((setq o (car (delq nil
  15856. (mapcar
  15857. (lambda (x)
  15858. (if (memq x org-latex-fragment-image-overlays) x))
  15859. (org-overlays-at (point))))))
  15860. (push (list :latex-fragment
  15861. (org-overlay-start o) (org-overlay-end o)) clist)
  15862. (push (list :latex-preview
  15863. (org-overlay-start o) (org-overlay-end o)) clist))
  15864. ((org-inside-LaTeX-fragment-p)
  15865. ;; FIXME: positions wrong.
  15866. (push (list :latex-fragment (point) (point)) clist)))
  15867. (setq clist (nreverse (delq nil clist)))
  15868. clist))
  15869. ;; FIXME: Compare with at-regexp-p Do we need both?
  15870. (defun org-in-regexp (re &optional nlines visually)
  15871. "Check if point is inside a match of regexp.
  15872. Normally only the current line is checked, but you can include NLINES extra
  15873. lines both before and after point into the search.
  15874. If VISUALLY is set, require that the cursor is not after the match but
  15875. really on, so that the block visually is on the match."
  15876. (catch 'exit
  15877. (let ((pos (point))
  15878. (eol (point-at-eol (+ 1 (or nlines 0))))
  15879. (inc (if visually 1 0)))
  15880. (save-excursion
  15881. (beginning-of-line (- 1 (or nlines 0)))
  15882. (while (re-search-forward re eol t)
  15883. (if (and (<= (match-beginning 0) pos)
  15884. (>= (+ inc (match-end 0)) pos))
  15885. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15886. (defun org-at-regexp-p (regexp)
  15887. "Is point inside a match of REGEXP in the current line?"
  15888. (catch 'exit
  15889. (save-excursion
  15890. (let ((pos (point)) (end (point-at-eol)))
  15891. (beginning-of-line 1)
  15892. (while (re-search-forward regexp end t)
  15893. (if (and (<= (match-beginning 0) pos)
  15894. (>= (match-end 0) pos))
  15895. (throw 'exit t)))
  15896. nil))))
  15897. (defun org-in-regexps-block-p (start-re end-re)
  15898. "Returns t if the current point is between matches of START-RE and END-RE.
  15899. This will also return to if point is on one of the two matches."
  15900. (interactive)
  15901. (let ((p (point)))
  15902. (save-excursion
  15903. (and (or (org-at-regexp-p start-re)
  15904. (re-search-backward start-re nil t))
  15905. (re-search-forward end-re nil t)
  15906. (>= (point) p)))))
  15907. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15908. "Call `multi-occur' with buffers for all agenda files."
  15909. (interactive "sOrg-files matching: \np")
  15910. (let* ((files (org-agenda-files))
  15911. (tnames (mapcar 'file-truename files))
  15912. (extra org-agenda-text-search-extra-files)
  15913. f)
  15914. (when (eq (car extra) 'agenda-archives)
  15915. (setq extra (cdr extra))
  15916. (setq files (org-add-archive-files files)))
  15917. (while (setq f (pop extra))
  15918. (unless (member (file-truename f) tnames)
  15919. (add-to-list 'files f 'append)
  15920. (add-to-list 'tnames (file-truename f) 'append)))
  15921. (multi-occur
  15922. (mapcar (lambda (x)
  15923. (with-current-buffer
  15924. (or (get-file-buffer x) (find-file-noselect x))
  15925. (widen)
  15926. (current-buffer)))
  15927. files)
  15928. regexp)))
  15929. (if (boundp 'occur-mode-find-occurrence-hook)
  15930. ;; Emacs 23
  15931. (add-hook 'occur-mode-find-occurrence-hook
  15932. (lambda ()
  15933. (when (org-mode-p)
  15934. (org-reveal))))
  15935. ;; Emacs 22
  15936. (defadvice occur-mode-goto-occurrence
  15937. (after org-occur-reveal activate)
  15938. (and (org-mode-p) (org-reveal)))
  15939. (defadvice occur-mode-goto-occurrence-other-window
  15940. (after org-occur-reveal activate)
  15941. (and (org-mode-p) (org-reveal)))
  15942. (defadvice occur-mode-display-occurrence
  15943. (after org-occur-reveal activate)
  15944. (when (org-mode-p)
  15945. (let ((pos (occur-mode-find-occurrence)))
  15946. (with-current-buffer (marker-buffer pos)
  15947. (save-excursion
  15948. (goto-char pos)
  15949. (org-reveal)))))))
  15950. (defun org-occur-link-in-agenda-files ()
  15951. "Create a link and search for it in the agendas.
  15952. The link is not stored in `org-stored-links', it is just created
  15953. for the search purpose."
  15954. (interactive)
  15955. (let ((link (condition-case nil
  15956. (org-store-link nil)
  15957. (error "Unable to create a link to here"))))
  15958. (org-occur-in-agenda-files (regexp-quote link))))
  15959. (defun org-uniquify (list)
  15960. "Remove duplicate elements from LIST."
  15961. (let (res)
  15962. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15963. res))
  15964. (defun org-delete-all (elts list)
  15965. "Remove all elements in ELTS from LIST."
  15966. (while elts
  15967. (setq list (delete (pop elts) list)))
  15968. list)
  15969. (defun org-back-over-empty-lines ()
  15970. "Move backwards over whitespace, to the beginning of the first empty line.
  15971. Returns the number of empty lines passed."
  15972. (let ((pos (point)))
  15973. (skip-chars-backward " \t\n\r")
  15974. (beginning-of-line 2)
  15975. (goto-char (min (point) pos))
  15976. (count-lines (point) pos)))
  15977. (defun org-skip-whitespace ()
  15978. (skip-chars-forward " \t\n\r"))
  15979. (defun org-point-in-group (point group &optional context)
  15980. "Check if POINT is in match-group GROUP.
  15981. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15982. match. If the match group does ot exist or point is not inside it,
  15983. return nil."
  15984. (and (match-beginning group)
  15985. (>= point (match-beginning group))
  15986. (<= point (match-end group))
  15987. (if context
  15988. (list context (match-beginning group) (match-end group))
  15989. t)))
  15990. (defun org-switch-to-buffer-other-window (&rest args)
  15991. "Switch to buffer in a second window on the current frame.
  15992. In particular, do not allow pop-up frames."
  15993. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15994. special-display-function)
  15995. (apply 'switch-to-buffer-other-window args)))
  15996. (defun org-combine-plists (&rest plists)
  15997. "Create a single property list from all plists in PLISTS.
  15998. The process starts by copying the first list, and then setting properties
  15999. from the other lists. Settings in the last list are the most significant
  16000. ones and overrule settings in the other lists."
  16001. (let ((rtn (copy-sequence (pop plists)))
  16002. p v ls)
  16003. (while plists
  16004. (setq ls (pop plists))
  16005. (while ls
  16006. (setq p (pop ls) v (pop ls))
  16007. (setq rtn (plist-put rtn p v))))
  16008. rtn))
  16009. (defun org-move-line-down (arg)
  16010. "Move the current line down. With prefix argument, move it past ARG lines."
  16011. (interactive "p")
  16012. (let ((col (current-column))
  16013. beg end pos)
  16014. (beginning-of-line 1) (setq beg (point))
  16015. (beginning-of-line 2) (setq end (point))
  16016. (beginning-of-line (+ 1 arg))
  16017. (setq pos (move-marker (make-marker) (point)))
  16018. (insert (delete-and-extract-region beg end))
  16019. (goto-char pos)
  16020. (org-move-to-column col)))
  16021. (defun org-move-line-up (arg)
  16022. "Move the current line up. With prefix argument, move it past ARG lines."
  16023. (interactive "p")
  16024. (let ((col (current-column))
  16025. beg end pos)
  16026. (beginning-of-line 1) (setq beg (point))
  16027. (beginning-of-line 2) (setq end (point))
  16028. (beginning-of-line (- arg))
  16029. (setq pos (move-marker (make-marker) (point)))
  16030. (insert (delete-and-extract-region beg end))
  16031. (goto-char pos)
  16032. (org-move-to-column col)))
  16033. (defun org-replace-escapes (string table)
  16034. "Replace %-escapes in STRING with values in TABLE.
  16035. TABLE is an association list with keys like \"%a\" and string values.
  16036. The sequences in STRING may contain normal field width and padding information,
  16037. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16038. so values can contain further %-escapes if they are define later in TABLE."
  16039. (let ((case-fold-search nil)
  16040. e re rpl)
  16041. (while (setq e (pop table))
  16042. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16043. (while (string-match re string)
  16044. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16045. (cdr e)))
  16046. (setq string (replace-match rpl t t string))))
  16047. string))
  16048. (defun org-sublist (list start end)
  16049. "Return a section of LIST, from START to END.
  16050. Counting starts at 1."
  16051. (let (rtn (c start))
  16052. (setq list (nthcdr (1- start) list))
  16053. (while (and list (<= c end))
  16054. (push (pop list) rtn)
  16055. (setq c (1+ c)))
  16056. (nreverse rtn)))
  16057. (defun org-find-base-buffer-visiting (file)
  16058. "Like `find-buffer-visiting' but always return the base buffer and
  16059. not an indirect buffer."
  16060. (let ((buf (or (get-file-buffer file)
  16061. (find-buffer-visiting file))))
  16062. (if buf
  16063. (or (buffer-base-buffer buf) buf)
  16064. nil)))
  16065. (defun org-image-file-name-regexp (&optional extensions)
  16066. "Return regexp matching the file names of images.
  16067. If EXTENSIONS is given, only match these."
  16068. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16069. (image-file-name-regexp)
  16070. (let ((image-file-name-extensions
  16071. (or extensions
  16072. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16073. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16074. (concat "\\."
  16075. (regexp-opt (nconc (mapcar 'upcase
  16076. image-file-name-extensions)
  16077. image-file-name-extensions)
  16078. t)
  16079. "\\'"))))
  16080. (defun org-file-image-p (file &optional extensions)
  16081. "Return non-nil if FILE is an image."
  16082. (save-match-data
  16083. (string-match (org-image-file-name-regexp extensions) file)))
  16084. (defun org-get-cursor-date ()
  16085. "Return the date at cursor in as a time.
  16086. This works in the calendar and in the agenda, anywhere else it just
  16087. returns the current time."
  16088. (let (date day defd)
  16089. (cond
  16090. ((eq major-mode 'calendar-mode)
  16091. (setq date (calendar-cursor-to-date)
  16092. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16093. ((eq major-mode 'org-agenda-mode)
  16094. (setq day (get-text-property (point) 'day))
  16095. (if day
  16096. (setq date (calendar-gregorian-from-absolute day)
  16097. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16098. (nth 2 date))))))
  16099. (or defd (current-time))))
  16100. (defvar org-agenda-action-marker (make-marker)
  16101. "Marker pointing to the entry for the next agenda action.")
  16102. (defun org-mark-entry-for-agenda-action ()
  16103. "Mark the current entry as target of an agenda action.
  16104. Agenda actions are actions executed from the agenda with the key `k',
  16105. which make use of the date at the cursor."
  16106. (interactive)
  16107. (move-marker org-agenda-action-marker
  16108. (save-excursion (org-back-to-heading t) (point))
  16109. (current-buffer))
  16110. (message
  16111. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16112. ;;; Paragraph filling stuff.
  16113. ;; We want this to be just right, so use the full arsenal.
  16114. (defun org-indent-line-function ()
  16115. "Indent line like previous, but further if previous was headline or item."
  16116. (interactive)
  16117. (let* ((pos (point))
  16118. (itemp (org-at-item-p))
  16119. (case-fold-search t)
  16120. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16121. column bpos bcol tpos tcol bullet btype bullet-type)
  16122. ;; Find the previous relevant line
  16123. (beginning-of-line 1)
  16124. (cond
  16125. ((looking-at "#") (setq column 0))
  16126. ((looking-at "\\*+ ") (setq column 0))
  16127. ((and (looking-at "[ \t]*:END:")
  16128. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16129. (save-excursion
  16130. (goto-char (1- (match-beginning 1)))
  16131. (setq column (current-column))))
  16132. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16133. (save-excursion
  16134. (re-search-backward
  16135. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16136. (setq column (org-get-indentation (match-string 0))))
  16137. (t
  16138. (beginning-of-line 0)
  16139. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16140. (not (looking-at "[ \t]*:END:"))
  16141. (not (looking-at org-drawer-regexp)))
  16142. (beginning-of-line 0))
  16143. (cond
  16144. ((looking-at "\\*+[ \t]+")
  16145. (if (not org-adapt-indentation)
  16146. (setq column 0)
  16147. (goto-char (match-end 0))
  16148. (setq column (current-column))))
  16149. ((looking-at org-drawer-regexp)
  16150. (goto-char (1- (match-beginning 1)))
  16151. (setq column (current-column)))
  16152. ((looking-at "\\([ \t]*\\):END:")
  16153. (goto-char (match-end 1))
  16154. (setq column (current-column)))
  16155. ((org-in-item-p)
  16156. (org-beginning-of-item)
  16157. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16158. (setq bpos (match-beginning 1) tpos (match-end 0)
  16159. bcol (progn (goto-char bpos) (current-column))
  16160. tcol (progn (goto-char tpos) (current-column))
  16161. bullet (match-string 1)
  16162. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16163. (if (> tcol (+ bcol org-description-max-indent))
  16164. (setq tcol (+ bcol 5)))
  16165. (if (not itemp)
  16166. (setq column tcol)
  16167. (goto-char pos)
  16168. (beginning-of-line 1)
  16169. (if (looking-at "\\S-")
  16170. (progn
  16171. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16172. (setq bullet (match-string 1)
  16173. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16174. (setq column (if (equal btype bullet-type) bcol tcol)))
  16175. (setq column (org-get-indentation)))))
  16176. (t (setq column (org-get-indentation))))))
  16177. (goto-char pos)
  16178. (if (<= (current-column) (current-indentation))
  16179. (org-indent-line-to column)
  16180. (save-excursion (org-indent-line-to column)))
  16181. (setq column (current-column))
  16182. (beginning-of-line 1)
  16183. (if (looking-at
  16184. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16185. (replace-match (concat (match-string 1)
  16186. (format org-property-format
  16187. (match-string 2) (match-string 3)))
  16188. t t))
  16189. (org-move-to-column column)))
  16190. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16191. "Variable to store copy of `adaptive-fill-regexp'.
  16192. Since `adaptive-fill-regexp' is set to never match, we need to
  16193. store a backup of its value before entering `org-mode' so that
  16194. the functionality can be provided as a fall-back.")
  16195. (defun org-set-autofill-regexps ()
  16196. (interactive)
  16197. ;; In the paragraph separator we include headlines, because filling
  16198. ;; text in a line directly attached to a headline would otherwise
  16199. ;; fill the headline as well.
  16200. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16201. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16202. ;; The paragraph starter includes hand-formatted lists.
  16203. (org-set-local
  16204. 'paragraph-start
  16205. (concat
  16206. "\f" "\\|"
  16207. "[ ]*$" "\\|"
  16208. "\\*+ " "\\|"
  16209. "[ \t]*#" "\\|"
  16210. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16211. "[ \t]*[:|]" "\\|"
  16212. "\\$\\$" "\\|"
  16213. "\\\\\\(begin\\|end\\|[][]\\)"))
  16214. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16215. ;; But only if the user has not turned off tables or fixed-width regions
  16216. (org-set-local
  16217. 'auto-fill-inhibit-regexp
  16218. (concat "\\*+ \\|#\\+"
  16219. "\\|[ \t]*" org-keyword-time-regexp
  16220. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16221. (concat
  16222. "\\|[ \t]*["
  16223. (if org-enable-table-editor "|" "")
  16224. (if org-enable-fixed-width-editor ":" "")
  16225. "]"))))
  16226. ;; We use our own fill-paragraph function, to make sure that tables
  16227. ;; and fixed-width regions are not wrapped. That function will pass
  16228. ;; through to `fill-paragraph' when appropriate.
  16229. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16230. ;; Adaptive filling: To get full control, first make sure that
  16231. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16232. (unless (local-variable-p 'adaptive-fill-regexp)
  16233. (org-set-local 'org-adaptive-fill-regexp-backup
  16234. adaptive-fill-regexp))
  16235. (org-set-local 'adaptive-fill-regexp "\000")
  16236. (org-set-local 'adaptive-fill-function
  16237. 'org-adaptive-fill-function)
  16238. (org-set-local
  16239. 'align-mode-rules-list
  16240. '((org-in-buffer-settings
  16241. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16242. (modes . '(org-mode))))))
  16243. (defun org-fill-paragraph (&optional justify)
  16244. "Re-align a table, pass through to fill-paragraph if no table."
  16245. (let ((table-p (org-at-table-p))
  16246. (table.el-p (org-at-table.el-p)))
  16247. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16248. (save-excursion (goto-char (point-at-bol))
  16249. (looking-at outline-regexp)))
  16250. t) ; skip headlines
  16251. (table.el-p t) ; skip table.el tables
  16252. (table-p (org-table-align) t) ; align org-mode tables
  16253. (t nil)))) ; call paragraph-fill
  16254. ;; For reference, this is the default value of adaptive-fill-regexp
  16255. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16256. (defun org-adaptive-fill-function ()
  16257. "Return a fill prefix for org-mode files.
  16258. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16259. work correctly."
  16260. (cond
  16261. ;; Comment line
  16262. ((looking-at "#[ \t]+")
  16263. (match-string-no-properties 0))
  16264. ;; Description list
  16265. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16266. (save-excursion
  16267. (if (> (match-end 1) (+ (match-beginning 1)
  16268. org-description-max-indent))
  16269. (goto-char (+ (match-beginning 1) 5))
  16270. (goto-char (match-end 0)))
  16271. (make-string (current-column) ?\ )))
  16272. ;; Ordered or unordered list
  16273. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16274. (save-excursion
  16275. (goto-char (match-end 0))
  16276. (make-string (current-column) ?\ )))
  16277. ;; Other text
  16278. ((looking-at org-adaptive-fill-regexp-backup)
  16279. (match-string-no-properties 0))))
  16280. ;;; Other stuff.
  16281. (defun org-toggle-fixed-width-section (arg)
  16282. "Toggle the fixed-width export.
  16283. If there is no active region, the QUOTE keyword at the current headline is
  16284. inserted or removed. When present, it causes the text between this headline
  16285. and the next to be exported as fixed-width text, and unmodified.
  16286. If there is an active region, this command adds or removes a colon as the
  16287. first character of this line. If the first character of a line is a colon,
  16288. this line is also exported in fixed-width font."
  16289. (interactive "P")
  16290. (let* ((cc 0)
  16291. (regionp (org-region-active-p))
  16292. (beg (if regionp (region-beginning) (point)))
  16293. (end (if regionp (region-end)))
  16294. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16295. (case-fold-search nil)
  16296. (re "[ \t]*\\(: \\)")
  16297. off)
  16298. (if regionp
  16299. (save-excursion
  16300. (goto-char beg)
  16301. (setq cc (current-column))
  16302. (beginning-of-line 1)
  16303. (setq off (looking-at re))
  16304. (while (> nlines 0)
  16305. (setq nlines (1- nlines))
  16306. (beginning-of-line 1)
  16307. (cond
  16308. (arg
  16309. (org-move-to-column cc t)
  16310. (insert ": \n")
  16311. (forward-line -1))
  16312. ((and off (looking-at re))
  16313. (replace-match "" t t nil 1))
  16314. ((not off) (org-move-to-column cc t) (insert ": ")))
  16315. (forward-line 1)))
  16316. (save-excursion
  16317. (org-back-to-heading)
  16318. (if (looking-at (concat outline-regexp
  16319. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16320. (replace-match "" t t nil 1)
  16321. (if (looking-at outline-regexp)
  16322. (progn
  16323. (goto-char (match-end 0))
  16324. (insert org-quote-string " "))))))))
  16325. (defun org-reftex-citation ()
  16326. "Use reftex-citation to insert a citation into the buffer.
  16327. This looks for a line like
  16328. #+BIBLIOGRAPHY: foo plain option:-d
  16329. and derives from it that foo.bib is the bibliography file relevant
  16330. for this document. It then installs the necessary environment for RefTeX
  16331. to work in this buffer and calls `reftex-citation' to insert a citation
  16332. into the buffer.
  16333. Export of such citations to both LaTeX and HTML is handled by the contributed
  16334. package org-exp-bibtex by Taru Karttunen."
  16335. (interactive)
  16336. (let ((reftex-docstruct-symbol 'rds)
  16337. (reftex-cite-format "\\cite{%l}")
  16338. rds bib)
  16339. (save-excursion
  16340. (save-restriction
  16341. (widen)
  16342. (let ((case-fold-search t)
  16343. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16344. (if (not (save-excursion
  16345. (or (re-search-forward re nil t)
  16346. (re-search-backward re nil t))))
  16347. (error "No bibliography defined in file")
  16348. (setq bib (concat (match-string 1) ".bib")
  16349. rds (list (list 'bib bib)))))))
  16350. (call-interactively 'reftex-citation)))
  16351. ;;;; Functions extending outline functionality
  16352. (defun org-beginning-of-line (&optional arg)
  16353. "Go to the beginning of the current line. If that is invisible, continue
  16354. to a visible line beginning. This makes the function of C-a more intuitive.
  16355. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16356. first attempt, and only move to after the tags when the cursor is already
  16357. beyond the end of the headline."
  16358. (interactive "P")
  16359. (let ((pos (point))
  16360. (special (if (consp org-special-ctrl-a/e)
  16361. (car org-special-ctrl-a/e)
  16362. org-special-ctrl-a/e))
  16363. refpos)
  16364. (if (org-bound-and-true-p line-move-visual)
  16365. (beginning-of-visual-line 1)
  16366. (beginning-of-line 1))
  16367. (if (and arg (fboundp 'move-beginning-of-line))
  16368. (call-interactively 'move-beginning-of-line)
  16369. (if (bobp)
  16370. nil
  16371. (backward-char 1)
  16372. (if (org-invisible-p)
  16373. (while (and (not (bobp)) (org-invisible-p))
  16374. (backward-char 1)
  16375. (beginning-of-line 1))
  16376. (forward-char 1))))
  16377. (when special
  16378. (cond
  16379. ((and (looking-at org-complex-heading-regexp)
  16380. (= (char-after (match-end 1)) ?\ ))
  16381. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16382. (point-at-eol)))
  16383. (goto-char
  16384. (if (eq special t)
  16385. (cond ((> pos refpos) refpos)
  16386. ((= pos (point)) refpos)
  16387. (t (point)))
  16388. (cond ((> pos (point)) (point))
  16389. ((not (eq last-command this-command)) (point))
  16390. (t refpos)))))
  16391. ((org-at-item-p)
  16392. (goto-char
  16393. (if (eq special t)
  16394. (cond ((> pos (match-end 4)) (match-end 4))
  16395. ((= pos (point)) (match-end 4))
  16396. (t (point)))
  16397. (cond ((> pos (point)) (point))
  16398. ((not (eq last-command this-command)) (point))
  16399. (t (match-end 4))))))))
  16400. (org-no-warnings
  16401. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16402. (defun org-end-of-line (&optional arg)
  16403. "Go to the end of the line.
  16404. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16405. first attempt, and only move to after the tags when the cursor is already
  16406. beyond the end of the headline."
  16407. (interactive "P")
  16408. (let ((special (if (consp org-special-ctrl-a/e)
  16409. (cdr org-special-ctrl-a/e)
  16410. org-special-ctrl-a/e)))
  16411. (if (or (not special)
  16412. (not (org-on-heading-p))
  16413. arg)
  16414. (call-interactively
  16415. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16416. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16417. (t 'end-of-line)))
  16418. (let ((pos (point)))
  16419. (beginning-of-line 1)
  16420. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16421. (if (eq special t)
  16422. (if (or (< pos (match-beginning 1))
  16423. (= pos (match-end 0)))
  16424. (goto-char (match-beginning 1))
  16425. (goto-char (match-end 0)))
  16426. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16427. (goto-char (match-end 0))
  16428. (goto-char (match-beginning 1))))
  16429. (call-interactively (if (fboundp 'move-end-of-line)
  16430. 'move-end-of-line
  16431. 'end-of-line)))))
  16432. (org-no-warnings
  16433. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16434. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16435. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16436. (define-key org-mode-map [home] 'org-beginning-of-line)
  16437. (define-key org-mode-map [end] 'org-end-of-line)
  16438. (defun org-backward-sentence (&optional arg)
  16439. "Go to beginning of sentence, or beginning of table field.
  16440. This will call `backward-sentence' or `org-table-beginning-of-field',
  16441. depending on context."
  16442. (interactive "P")
  16443. (cond
  16444. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16445. (t (call-interactively 'backward-sentence))))
  16446. (defun org-forward-sentence (&optional arg)
  16447. "Go to end of sentence, or end of table field.
  16448. This will call `forward-sentence' or `org-table-end-of-field',
  16449. depending on context."
  16450. (interactive "P")
  16451. (cond
  16452. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16453. (t (call-interactively 'forward-sentence))))
  16454. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16455. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16456. (defun org-kill-line (&optional arg)
  16457. "Kill line, to tags or end of line."
  16458. (interactive "P")
  16459. (cond
  16460. ((or (not org-special-ctrl-k)
  16461. (bolp)
  16462. (not (org-on-heading-p)))
  16463. (call-interactively 'kill-line))
  16464. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16465. (kill-region (point) (match-beginning 1))
  16466. (org-set-tags nil t))
  16467. (t (kill-region (point) (point-at-eol)))))
  16468. (define-key org-mode-map "\C-k" 'org-kill-line)
  16469. (defun org-yank (&optional arg)
  16470. "Yank. If the kill is a subtree, treat it specially.
  16471. This command will look at the current kill and check if is a single
  16472. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16473. cursor is at the beginning of a line or after the stars of a currently
  16474. empty headline, then the yank is handled specially. How exactly depends
  16475. on the value of the following variables, both set by default.
  16476. org-yank-folded-subtrees
  16477. When set, the subtree(s) will be folded after insertion, but only
  16478. if doing so would now swallow text after the yanked text.
  16479. org-yank-adjusted-subtrees
  16480. When set, the subtree will be promoted or demoted in order to
  16481. fit into the local outline tree structure, which means that the level
  16482. will be adjusted so that it becomes the smaller one of the two
  16483. *visible* surrounding headings.
  16484. Any prefix to this command will cause `yank' to be called directly with
  16485. no special treatment. In particular, a simple `C-u' prefix will just
  16486. plainly yank the text as it is.
  16487. \[1] The test checks if the first non-white line is a heading
  16488. and if there are no other headings with fewer stars."
  16489. (interactive "P")
  16490. (org-yank-generic 'yank arg))
  16491. (defun org-yank-generic (command arg)
  16492. "Perform some yank-like command.
  16493. This function implements the behavior described in the `org-yank'
  16494. documentation. However, it has been generalized to work for any
  16495. interactive command with similar behavior."
  16496. ;; pretend to be command COMMAND
  16497. (setq this-command command)
  16498. (if arg
  16499. (call-interactively command)
  16500. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16501. (and (org-kill-is-subtree-p)
  16502. (or (bolp)
  16503. (and (looking-at "[ \t]*$")
  16504. (string-match
  16505. "\\`\\*+\\'"
  16506. (buffer-substring (point-at-bol) (point)))))))
  16507. swallowp)
  16508. (cond
  16509. ((and subtreep org-yank-folded-subtrees)
  16510. (let ((beg (point))
  16511. end)
  16512. (if (and subtreep org-yank-adjusted-subtrees)
  16513. (org-paste-subtree nil nil 'for-yank)
  16514. (call-interactively command))
  16515. (setq end (point))
  16516. (goto-char beg)
  16517. (when (and (bolp) subtreep
  16518. (not (setq swallowp
  16519. (org-yank-folding-would-swallow-text beg end))))
  16520. (or (looking-at outline-regexp)
  16521. (re-search-forward (concat "^" outline-regexp) end t))
  16522. (while (and (< (point) end) (looking-at outline-regexp))
  16523. (hide-subtree)
  16524. (org-cycle-show-empty-lines 'folded)
  16525. (condition-case nil
  16526. (outline-forward-same-level 1)
  16527. (error (goto-char end)))))
  16528. (when swallowp
  16529. (message
  16530. "Inserted text not folded because that would swallow text"))
  16531. (goto-char end)
  16532. (skip-chars-forward " \t\n\r")
  16533. (beginning-of-line 1)
  16534. (push-mark beg 'nomsg)))
  16535. ((and subtreep org-yank-adjusted-subtrees)
  16536. (let ((beg (point-at-bol)))
  16537. (org-paste-subtree nil nil 'for-yank)
  16538. (push-mark beg 'nomsg)))
  16539. (t
  16540. (call-interactively command))))))
  16541. (defun org-yank-folding-would-swallow-text (beg end)
  16542. "Would hide-subtree at BEG swallow any text after END?"
  16543. (let (level)
  16544. (save-excursion
  16545. (goto-char beg)
  16546. (when (or (looking-at outline-regexp)
  16547. (re-search-forward (concat "^" outline-regexp) end t))
  16548. (setq level (org-outline-level)))
  16549. (goto-char end)
  16550. (skip-chars-forward " \t\r\n\v\f")
  16551. (if (or (eobp)
  16552. (and (bolp) (looking-at org-outline-regexp)
  16553. (<= (org-outline-level) level)))
  16554. nil ; Nothing would be swallowed
  16555. t)))) ; something would swallow
  16556. (define-key org-mode-map "\C-y" 'org-yank)
  16557. (defun org-invisible-p ()
  16558. "Check if point is at a character currently not visible."
  16559. ;; Early versions of noutline don't have `outline-invisible-p'.
  16560. (if (fboundp 'outline-invisible-p)
  16561. (outline-invisible-p)
  16562. (get-char-property (point) 'invisible)))
  16563. (defun org-invisible-p2 ()
  16564. "Check if point is at a character currently not visible."
  16565. (save-excursion
  16566. (if (and (eolp) (not (bobp))) (backward-char 1))
  16567. ;; Early versions of noutline don't have `outline-invisible-p'.
  16568. (if (fboundp 'outline-invisible-p)
  16569. (outline-invisible-p)
  16570. (get-char-property (point) 'invisible))))
  16571. (defun org-back-to-heading (&optional invisible-ok)
  16572. "Call `outline-back-to-heading', but provide a better error message."
  16573. (condition-case nil
  16574. (outline-back-to-heading invisible-ok)
  16575. (error (error "Before first headline at position %d in buffer %s"
  16576. (point) (current-buffer)))))
  16577. (defun org-before-first-heading-p ()
  16578. "Before first heading?"
  16579. (save-excursion
  16580. (null (re-search-backward "^\\*+ " nil t))))
  16581. (defun org-on-heading-p (&optional ignored)
  16582. (outline-on-heading-p t))
  16583. (defun org-at-heading-p (&optional ignored)
  16584. (outline-on-heading-p t))
  16585. (defun org-point-at-end-of-empty-headline ()
  16586. "If point is at the end of an empty headline, return t, else nil.
  16587. If the heading only contains a TODO keyword, it is still still considered
  16588. empty."
  16589. (and (looking-at "[ \t]*$")
  16590. (save-excursion
  16591. (beginning-of-line 1)
  16592. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16593. "\\)?[ \t]*$")))))
  16594. (defun org-at-heading-or-item-p ()
  16595. (or (org-on-heading-p) (org-at-item-p)))
  16596. (defun org-on-target-p ()
  16597. (or (org-in-regexp org-radio-target-regexp)
  16598. (org-in-regexp org-target-regexp)))
  16599. (defun org-up-heading-all (arg)
  16600. "Move to the heading line of which the present line is a subheading.
  16601. This function considers both visible and invisible heading lines.
  16602. With argument, move up ARG levels."
  16603. (if (fboundp 'outline-up-heading-all)
  16604. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16605. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16606. (defun org-up-heading-safe ()
  16607. "Move to the heading line of which the present line is a subheading.
  16608. This version will not throw an error. It will return the level of the
  16609. headline found, or nil if no higher level is found.
  16610. Also, this function will be a lot faster than `outline-up-heading',
  16611. because it relies on stars being the outline starters. This can really
  16612. make a significant difference in outlines with very many siblings."
  16613. (let (start-level re)
  16614. (org-back-to-heading t)
  16615. (setq start-level (funcall outline-level))
  16616. (if (equal start-level 1)
  16617. nil
  16618. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16619. (if (re-search-backward re nil t)
  16620. (funcall outline-level)))))
  16621. (defun org-first-sibling-p ()
  16622. "Is this heading the first child of its parents?"
  16623. (interactive)
  16624. (let ((re (concat "^" outline-regexp))
  16625. level l)
  16626. (unless (org-at-heading-p t)
  16627. (error "Not at a heading"))
  16628. (setq level (funcall outline-level))
  16629. (save-excursion
  16630. (if (not (re-search-backward re nil t))
  16631. t
  16632. (setq l (funcall outline-level))
  16633. (< l level)))))
  16634. (defun org-goto-sibling (&optional previous)
  16635. "Goto the next sibling, even if it is invisible.
  16636. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16637. when a sibling was found. When none is found, return nil and don't
  16638. move point."
  16639. (let ((fun (if previous 're-search-backward 're-search-forward))
  16640. (pos (point))
  16641. (re (concat "^" outline-regexp))
  16642. level l)
  16643. (when (condition-case nil (org-back-to-heading t) (error nil))
  16644. (setq level (funcall outline-level))
  16645. (catch 'exit
  16646. (or previous (forward-char 1))
  16647. (while (funcall fun re nil t)
  16648. (setq l (funcall outline-level))
  16649. (when (< l level) (goto-char pos) (throw 'exit nil))
  16650. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16651. (goto-char pos)
  16652. nil))))
  16653. (defun org-show-siblings ()
  16654. "Show all siblings of the current headline."
  16655. (save-excursion
  16656. (while (org-goto-sibling) (org-flag-heading nil)))
  16657. (save-excursion
  16658. (while (org-goto-sibling 'previous)
  16659. (org-flag-heading nil))))
  16660. (defun org-show-hidden-entry ()
  16661. "Show an entry where even the heading is hidden."
  16662. (save-excursion
  16663. (org-show-entry)))
  16664. (defun org-flag-heading (flag &optional entry)
  16665. "Flag the current heading. FLAG non-nil means make invisible.
  16666. When ENTRY is non-nil, show the entire entry."
  16667. (save-excursion
  16668. (org-back-to-heading t)
  16669. ;; Check if we should show the entire entry
  16670. (if entry
  16671. (progn
  16672. (org-show-entry)
  16673. (save-excursion
  16674. (and (outline-next-heading)
  16675. (org-flag-heading nil))))
  16676. (outline-flag-region (max (point-min) (1- (point)))
  16677. (save-excursion (outline-end-of-heading) (point))
  16678. flag))))
  16679. (defun org-get-next-sibling ()
  16680. "Move to next heading of the same level, and return point.
  16681. If there is no such heading, return nil.
  16682. This is like outline-next-sibling, but invisible headings are ok."
  16683. (let ((level (funcall outline-level)))
  16684. (outline-next-heading)
  16685. (while (and (not (eobp)) (> (funcall outline-level) level))
  16686. (outline-next-heading))
  16687. (if (or (eobp) (< (funcall outline-level) level))
  16688. nil
  16689. (point))))
  16690. (defun org-get-last-sibling ()
  16691. "Move to previous heading of the same level, and return point.
  16692. If there is no such heading, return nil."
  16693. (let ((opoint (point))
  16694. (level (funcall outline-level)))
  16695. (outline-previous-heading)
  16696. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16697. (while (and (> (funcall outline-level) level)
  16698. (not (bobp)))
  16699. (outline-previous-heading))
  16700. (if (< (funcall outline-level) level)
  16701. nil
  16702. (point)))))
  16703. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16704. ;; This contains an exact copy of the original function, but it uses
  16705. ;; `org-back-to-heading', to make it work also in invisible
  16706. ;; trees. And is uses an invisible-OK argument.
  16707. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16708. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16709. ;; with many children and grandchildren etc, this can be much faster
  16710. ;; than the outline version.
  16711. (org-back-to-heading invisible-OK)
  16712. (let ((first t)
  16713. (level (funcall outline-level)))
  16714. (if (and (org-mode-p) (< level 1000))
  16715. ;; A true heading (not a plain list item), in Org-mode
  16716. ;; This means we can easily find the end by looking
  16717. ;; only for the right number of stars. Using a regexp to do
  16718. ;; this is so much faster than using a Lisp loop.
  16719. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16720. (forward-char 1)
  16721. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16722. ;; something else, do it the slow way
  16723. (while (and (not (eobp))
  16724. (or first (> (funcall outline-level) level)))
  16725. (setq first nil)
  16726. (outline-next-heading)))
  16727. (unless to-heading
  16728. (if (memq (preceding-char) '(?\n ?\^M))
  16729. (progn
  16730. ;; Go to end of line before heading
  16731. (forward-char -1)
  16732. (if (memq (preceding-char) '(?\n ?\^M))
  16733. ;; leave blank line before heading
  16734. (forward-char -1))))))
  16735. (point))
  16736. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16737. "Use Org version in org-mode, for dramatic speed-up."
  16738. (if (eq major-mode 'org-mode)
  16739. (progn
  16740. (org-end-of-subtree nil t)
  16741. (unless (eobp) (backward-char 1)))
  16742. ad-do-it))
  16743. (defun org-forward-same-level (arg &optional invisible-ok)
  16744. "Move forward to the arg'th subheading at same level as this one.
  16745. Stop at the first and last subheadings of a superior heading."
  16746. (interactive "p")
  16747. (org-back-to-heading invisible-ok)
  16748. (org-on-heading-p)
  16749. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16750. (re (format "^\\*\\{1,%d\\} " level))
  16751. l)
  16752. (forward-char 1)
  16753. (while (> arg 0)
  16754. (while (and (re-search-forward re nil 'move)
  16755. (setq l (- (match-end 0) (match-beginning 0) 1))
  16756. (= l level)
  16757. (not invisible-ok)
  16758. (progn (backward-char 1) (org-invisible-p)))
  16759. (if (< l level) (setq arg 1)))
  16760. (setq arg (1- arg)))
  16761. (beginning-of-line 1)))
  16762. (defun org-backward-same-level (arg &optional invisible-ok)
  16763. "Move backward to the arg'th subheading at same level as this one.
  16764. Stop at the first and last subheadings of a superior heading."
  16765. (interactive "p")
  16766. (org-back-to-heading)
  16767. (org-on-heading-p)
  16768. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16769. (re (format "^\\*\\{1,%d\\} " level))
  16770. l)
  16771. (while (> arg 0)
  16772. (while (and (re-search-backward re nil 'move)
  16773. (setq l (- (match-end 0) (match-beginning 0) 1))
  16774. (= l level)
  16775. (not invisible-ok)
  16776. (org-invisible-p))
  16777. (if (< l level) (setq arg 1)))
  16778. (setq arg (1- arg)))))
  16779. (defun org-show-subtree ()
  16780. "Show everything after this heading at deeper levels."
  16781. (outline-flag-region
  16782. (point)
  16783. (save-excursion
  16784. (org-end-of-subtree t t))
  16785. nil))
  16786. (defun org-show-entry ()
  16787. "Show the body directly following this heading.
  16788. Show the heading too, if it is currently invisible."
  16789. (interactive)
  16790. (save-excursion
  16791. (condition-case nil
  16792. (progn
  16793. (org-back-to-heading t)
  16794. (outline-flag-region
  16795. (max (point-min) (1- (point)))
  16796. (save-excursion
  16797. (if (re-search-forward
  16798. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16799. (match-beginning 1)
  16800. (point-max)))
  16801. nil)
  16802. (org-cycle-hide-drawers 'children))
  16803. (error nil))))
  16804. (defun org-make-options-regexp (kwds &optional extra)
  16805. "Make a regular expression for keyword lines."
  16806. (concat
  16807. "^"
  16808. "#?[ \t]*\\+\\("
  16809. (mapconcat 'regexp-quote kwds "\\|")
  16810. (if extra (concat "\\|" extra))
  16811. "\\):[ \t]*"
  16812. "\\(.*\\)"))
  16813. ;; Make isearch reveal the necessary context
  16814. (defun org-isearch-end ()
  16815. "Reveal context after isearch exits."
  16816. (when isearch-success ; only if search was successful
  16817. (if (featurep 'xemacs)
  16818. ;; Under XEmacs, the hook is run in the correct place,
  16819. ;; we directly show the context.
  16820. (org-show-context 'isearch)
  16821. ;; In Emacs the hook runs *before* restoring the overlays.
  16822. ;; So we have to use a one-time post-command-hook to do this.
  16823. ;; (Emacs 22 has a special variable, see function `org-mode')
  16824. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16825. isearch-mode-end-hook-quit)
  16826. ;; Only when the isearch was not quitted.
  16827. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16828. 'append 'local)))))
  16829. (defun org-isearch-post-command ()
  16830. "Remove self from hook, and show context."
  16831. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16832. (org-show-context 'isearch))
  16833. ;;;; Integration with and fixes for other packages
  16834. ;;; Imenu support
  16835. (defvar org-imenu-markers nil
  16836. "All markers currently used by Imenu.")
  16837. (make-variable-buffer-local 'org-imenu-markers)
  16838. (defun org-imenu-new-marker (&optional pos)
  16839. "Return a new marker for use by Imenu, and remember the marker."
  16840. (let ((m (make-marker)))
  16841. (move-marker m (or pos (point)))
  16842. (push m org-imenu-markers)
  16843. m))
  16844. (defun org-imenu-get-tree ()
  16845. "Produce the index for Imenu."
  16846. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16847. (setq org-imenu-markers nil)
  16848. (let* ((n org-imenu-depth)
  16849. (re (concat "^" outline-regexp))
  16850. (subs (make-vector (1+ n) nil))
  16851. (last-level 0)
  16852. m level head)
  16853. (save-excursion
  16854. (save-restriction
  16855. (widen)
  16856. (goto-char (point-max))
  16857. (while (re-search-backward re nil t)
  16858. (setq level (org-reduced-level (funcall outline-level)))
  16859. (when (<= level n)
  16860. (looking-at org-complex-heading-regexp)
  16861. (setq head (org-link-display-format
  16862. (org-match-string-no-properties 4))
  16863. m (org-imenu-new-marker))
  16864. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16865. (if (>= level last-level)
  16866. (push (cons head m) (aref subs level))
  16867. (push (cons head (aref subs (1+ level))) (aref subs level))
  16868. (loop for i from (1+ level) to n do (aset subs i nil)))
  16869. (setq last-level level)))))
  16870. (aref subs 1)))
  16871. (eval-after-load "imenu"
  16872. '(progn
  16873. (add-hook 'imenu-after-jump-hook
  16874. (lambda ()
  16875. (if (eq major-mode 'org-mode)
  16876. (org-show-context 'org-goto))))))
  16877. (defun org-link-display-format (link)
  16878. "Replace a link with either the description, or the link target
  16879. if no description is present"
  16880. (save-match-data
  16881. (if (string-match org-bracket-link-analytic-regexp link)
  16882. (replace-match (if (match-end 5)
  16883. (match-string 5 link)
  16884. (concat (match-string 1 link)
  16885. (match-string 3 link)))
  16886. nil t link)
  16887. link)))
  16888. ;; Speedbar support
  16889. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16890. "Overlay marking the agenda restriction line in speedbar.")
  16891. (org-overlay-put org-speedbar-restriction-lock-overlay
  16892. 'face 'org-agenda-restriction-lock)
  16893. (org-overlay-put org-speedbar-restriction-lock-overlay
  16894. 'help-echo "Agendas are currently limited to this item.")
  16895. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16896. (defun org-speedbar-set-agenda-restriction ()
  16897. "Restrict future agenda commands to the location at point in speedbar.
  16898. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16899. (interactive)
  16900. (require 'org-agenda)
  16901. (let (p m tp np dir txt)
  16902. (cond
  16903. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16904. 'org-imenu t))
  16905. (setq m (get-text-property p 'org-imenu-marker))
  16906. (with-current-buffer (marker-buffer m)
  16907. (goto-char m)
  16908. (org-agenda-set-restriction-lock 'subtree)))
  16909. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16910. 'speedbar-function 'speedbar-find-file))
  16911. (setq tp (previous-single-property-change
  16912. (1+ p) 'speedbar-function)
  16913. np (next-single-property-change
  16914. tp 'speedbar-function)
  16915. dir (speedbar-line-directory)
  16916. txt (buffer-substring-no-properties (or tp (point-min))
  16917. (or np (point-max))))
  16918. (with-current-buffer (find-file-noselect
  16919. (let ((default-directory dir))
  16920. (expand-file-name txt)))
  16921. (unless (org-mode-p)
  16922. (error "Cannot restrict to non-Org-mode file"))
  16923. (org-agenda-set-restriction-lock 'file)))
  16924. (t (error "Don't know how to restrict Org-mode's agenda")))
  16925. (org-move-overlay org-speedbar-restriction-lock-overlay
  16926. (point-at-bol) (point-at-eol))
  16927. (setq current-prefix-arg nil)
  16928. (org-agenda-maybe-redo)))
  16929. (eval-after-load "speedbar"
  16930. '(progn
  16931. (speedbar-add-supported-extension ".org")
  16932. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16933. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16934. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16935. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16936. (add-hook 'speedbar-visiting-tag-hook
  16937. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16938. ;;; Fixes and Hacks for problems with other packages
  16939. ;; Make flyspell not check words in links, to not mess up our keymap
  16940. (defun org-mode-flyspell-verify ()
  16941. "Don't let flyspell put overlays at active buttons."
  16942. (and (not (get-text-property (point) 'keymap))
  16943. (not (get-text-property (point) 'org-no-flyspell))))
  16944. (defun org-remove-flyspell-overlays-in (beg end)
  16945. "Remove flyspell overlays in region."
  16946. (and (org-bound-and-true-p flyspell-mode)
  16947. (fboundp 'flyspell-delete-region-overlays)
  16948. (flyspell-delete-region-overlays beg end))
  16949. (add-text-properties beg end '(org-no-flyspell t)))
  16950. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16951. (eval-after-load "bookmark"
  16952. '(if (boundp 'bookmark-after-jump-hook)
  16953. ;; We can use the hook
  16954. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16955. ;; Hook not available, use advice
  16956. (defadvice bookmark-jump (after org-make-visible activate)
  16957. "Make the position visible."
  16958. (org-bookmark-jump-unhide))))
  16959. ;; Make sure saveplace shows the location if it was hidden
  16960. (eval-after-load "saveplace"
  16961. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16962. "Make the position visible."
  16963. (org-bookmark-jump-unhide)))
  16964. ;; Make sure ecb shows the location if it was hidden
  16965. (eval-after-load "ecb"
  16966. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16967. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16968. (if (eq major-mode 'org-mode)
  16969. (org-show-context))))
  16970. (defun org-bookmark-jump-unhide ()
  16971. "Unhide the current position, to show the bookmark location."
  16972. (and (org-mode-p)
  16973. (or (org-invisible-p)
  16974. (save-excursion (goto-char (max (point-min) (1- (point))))
  16975. (org-invisible-p)))
  16976. (org-show-context 'bookmark-jump)))
  16977. ;; Make session.el ignore our circular variable
  16978. (eval-after-load "session"
  16979. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16980. ;;;; Experimental code
  16981. (defun org-closed-in-range ()
  16982. "Sparse tree of items closed in a certain time range.
  16983. Still experimental, may disappear in the future."
  16984. (interactive)
  16985. ;; Get the time interval from the user.
  16986. (let* ((time1 (org-float-time
  16987. (org-read-date nil 'to-time nil "Starting date: ")))
  16988. (time2 (org-float-time
  16989. (org-read-date nil 'to-time nil "End date:")))
  16990. ;; callback function
  16991. (callback (lambda ()
  16992. (let ((time
  16993. (org-float-time
  16994. (apply 'encode-time
  16995. (org-parse-time-string
  16996. (match-string 1))))))
  16997. ;; check if time in interval
  16998. (and (>= time time1) (<= time time2))))))
  16999. ;; make tree, check each match with the callback
  17000. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17001. ;;;; Finish up
  17002. (provide 'org)
  17003. (run-hooks 'org-load-hook)
  17004. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17005. ;;; org.el ends here