org.el 694 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605
  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.35trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (unless (boundp 'calendar-view-holidays-initially-flag)
  74. (defvaralias 'calendar-view-holidays-initially-flag
  75. 'view-calendar-holidays-initially))
  76. (unless (boundp 'calendar-view-diary-initially-flag)
  77. (defvaralias 'calendar-view-diary-initially-flag
  78. 'view-diary-entries-initially))
  79. (unless (boundp 'diary-fancy-buffer)
  80. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  81. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  82. ;; the file noutline.el being loaded.
  83. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  84. ;; We require noutline, which might be provided in outline.el
  85. (require 'outline) (require 'noutline)
  86. ;; Other stuff we need.
  87. (require 'time-date)
  88. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  89. (require 'easymenu)
  90. (require 'overlay)
  91. (require 'org-macs)
  92. (require 'org-entities)
  93. (require 'org-compat)
  94. (require 'org-faces)
  95. (require 'org-list)
  96. (require 'org-src)
  97. (require 'org-footnote)
  98. ;;;; Customization variables
  99. ;;; Version
  100. (defconst org-version "6.35trans"
  101. "The version number of the file org.el.")
  102. (defun org-version (&optional here)
  103. "Show the org-mode version in the echo area.
  104. With prefix arg HERE, insert it at point."
  105. (interactive "P")
  106. (let* ((origin default-directory)
  107. (version org-version)
  108. (git-version)
  109. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  110. (when (and (file-exists-p (expand-file-name ".git" dir))
  111. (executable-find "git"))
  112. (unwind-protect
  113. (progn
  114. (cd dir)
  115. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  116. (with-current-buffer "*Shell Command Output*"
  117. (goto-char (point-min))
  118. (setq git-version (buffer-substring (point) (point-at-eol))))
  119. (subst-char-in-string ?- ?. git-version t)
  120. (when (string-match "\\S-"
  121. (shell-command-to-string
  122. "git diff-index --name-only HEAD --"))
  123. (setq git-version (concat git-version ".dirty")))
  124. (setq version (concat version " (" git-version ")"))))
  125. (cd origin)))
  126. (setq version (format "Org-mode version %s" version))
  127. (if here (insert version))
  128. (message version)))
  129. ;;; Compatibility constants
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'calendar)
  136. (defcustom org-mode-hook nil
  137. "Mode hook for Org-mode, run after the mode was turned on."
  138. :group 'org
  139. :type 'hook)
  140. (defcustom org-load-hook nil
  141. "Hook that is run after org.el has been loaded."
  142. :group 'org
  143. :type 'hook)
  144. (defvar org-modules) ; defined below
  145. (defvar org-modules-loaded nil
  146. "Have the modules been loaded already?")
  147. (defun org-load-modules-maybe (&optional force)
  148. "Load all extensions listed in `org-modules'."
  149. (when (or force (not org-modules-loaded))
  150. (mapc (lambda (ext)
  151. (condition-case nil (require ext)
  152. (error (message "Problems while trying to load feature `%s'" ext))))
  153. org-modules)
  154. (setq org-modules-loaded t)))
  155. (defun org-set-modules (var value)
  156. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  157. (set var value)
  158. (when (featurep 'org)
  159. (org-load-modules-maybe 'force)))
  160. (when (org-bound-and-true-p org-modules)
  161. (let ((a (member 'org-infojs org-modules)))
  162. (and a (setcar a 'org-jsinfo))))
  163. (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)
  164. "Modules that should always be loaded together with org.el.
  165. If a description starts with <C>, the file is not part of Emacs
  166. and loading it will require that you have downloaded and properly installed
  167. the org-mode distribution.
  168. You can also use this system to load external packages (i.e. neither Org
  169. core modules, nor modules from the CONTRIB directory). Just add symbols
  170. to the end of the list. If the package is called org-xyz.el, then you need
  171. to add the symbol `xyz', and the package must have a call to
  172. (provide 'org-xyz)"
  173. :group 'org
  174. :set 'org-set-modules
  175. :type
  176. '(set :greedy t
  177. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  178. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  179. (const :tag " crypt: Encryption of subtrees" org-crypt)
  180. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  181. (const :tag " docview: Links to doc-view buffers" org-docview)
  182. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  183. (const :tag " id: Global IDs for identifying entries" org-id)
  184. (const :tag " info: Links to Info nodes" org-info)
  185. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  186. (const :tag " habit: Track your consistency with habits" org-habit)
  187. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  188. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  189. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  190. (const :tag " mew Links to Mew folders/messages" org-mew)
  191. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  192. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  193. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  194. (const :tag " vm: Links to VM folders/messages" org-vm)
  195. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  196. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  197. (const :tag " mouse: Additional mouse support" org-mouse)
  198. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  199. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  200. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  201. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  202. (const :tag "C collector: Collect properties into tables" org-collector)
  203. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  204. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  205. (const :tag "C eval: Include command output as text" org-eval)
  206. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  207. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  208. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  209. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  210. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  211. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  212. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  213. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  214. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  215. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  216. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  217. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  218. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  219. (const :tag "C registry: A registry for Org-mode links" org-registry)
  220. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  221. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  222. (const :tag "C secretary: Team management with org-mode" org-secretary)
  223. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  224. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  225. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  226. (const :tag "C track: Keep up with Org-mode development" org-track)
  227. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  228. (defcustom org-support-shift-select nil
  229. "Non-nil means make shift-cursor commands select text when possible.
  230. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  231. selecting a region, or enlarge thusly regions started in this way.
  232. In Org-mode, in special contexts, these same keys are used for other
  233. purposes, important enough to compete with shift selection. Org tries
  234. to balance these needs by supporting `shift-select-mode' outside these
  235. special contexts, under control of this variable.
  236. The default of this variable is nil, to avoid confusing behavior. Shifted
  237. cursor keys will then execute Org commands in the following contexts:
  238. - on a headline, changing TODO state (left/right) and priority (up/down)
  239. - on a time stamp, changing the time
  240. - in a plain list item, changing the bullet type
  241. - in a property definition line, switching between allowed values
  242. - in the BEGIN line of a clock table (changing the time block).
  243. Outside these contexts, the commands will throw an error.
  244. When this variable is t and the cursor is not in a special context,
  245. Org-mode will support shift-selection for making and enlarging regions.
  246. To make this more effective, the bullet cycling will no longer happen
  247. anywhere in an item line, but only if the cursor is exactly on the bullet.
  248. If you set this variable to the symbol `always', then the keys
  249. will not be special in headlines, property lines, and item lines, to make
  250. shift selection work there as well. If this is what you want, you can
  251. use the following alternative commands: `C-c C-t' and `C-c ,' to
  252. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  253. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  254. edited by hand or in column view.
  255. However, when the cursor is on a timestamp, shift-cursor commands
  256. will still edit the time stamp - this is just too good to give up.
  257. XEmacs user should have this variable set to nil, because shift-select-mode
  258. is Emacs 23 only."
  259. :group 'org
  260. :type '(choice
  261. (const :tag "Never" nil)
  262. (const :tag "When outside special context" t)
  263. (const :tag "Everywhere except timestamps" always)))
  264. (defgroup org-startup nil
  265. "Options concerning startup of Org-mode."
  266. :tag "Org Startup"
  267. :group 'org)
  268. (defcustom org-startup-folded t
  269. "Non-nil means entering Org-mode will switch to OVERVIEW.
  270. This can also be configured on a per-file basis by adding one of
  271. the following lines anywhere in the buffer:
  272. #+STARTUP: fold (or `overview', this is equivalent)
  273. #+STARTUP: nofold (or `showall', this is equivalent)
  274. #+STARTUP: content
  275. #+STARTUP: showeverything"
  276. :group 'org-startup
  277. :type '(choice
  278. (const :tag "nofold: show all" nil)
  279. (const :tag "fold: overview" t)
  280. (const :tag "content: all headlines" content)
  281. (const :tag "show everything, even drawers" showeverything)))
  282. (defcustom org-startup-truncated t
  283. "Non-nil means entering Org-mode will set `truncate-lines'.
  284. This is useful since some lines containing links can be very long and
  285. uninteresting. Also tables look terrible when wrapped."
  286. :group 'org-startup
  287. :type 'boolean)
  288. (defcustom org-startup-indented nil
  289. "Non-nil means turn on `org-indent-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: indent
  293. #+STARTUP: noindent"
  294. :group 'org-structure
  295. :type '(choice
  296. (const :tag "Not" nil)
  297. (const :tag "Globally (slow on startup in large files)" t)))
  298. (defcustom org-startup-with-beamer-mode nil
  299. "Non-nil means turn on `org-beamer-mode' on startup.
  300. This can also be configured on a per-file basis by adding one of
  301. the following lines anywhere in the buffer:
  302. #+STARTUP: beamer"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-startup-align-all-tables nil
  306. "Non-nil means align all tables when visiting a file.
  307. This is useful when the column width in tables is forced with <N> cookies
  308. in table fields. Such tables will look correct only after the first re-align.
  309. This can also be configured on a per-file basis by adding one of
  310. the following lines anywhere in the buffer:
  311. #+STARTUP: align
  312. #+STARTUP: noalign"
  313. :group 'org-startup
  314. :type 'boolean)
  315. (defcustom org-insert-mode-line-in-empty-file nil
  316. "Non-nil means insert the first line setting Org-mode in empty files.
  317. When the function `org-mode' is called interactively in an empty file, this
  318. normally means that the file name does not automatically trigger Org-mode.
  319. To ensure that the file will always be in Org-mode in the future, a
  320. line enforcing Org-mode will be inserted into the buffer, if this option
  321. has been set."
  322. :group 'org-startup
  323. :type 'boolean)
  324. (defcustom org-replace-disputed-keys nil
  325. "Non-nil means use alternative key bindings for some keys.
  326. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  327. These keys are also used by other packages like shift-selection-mode'
  328. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  329. If you want to use Org-mode together with one of these other modes,
  330. or more generally if you would like to move some Org-mode commands to
  331. other keys, set this variable and configure the keys with the variable
  332. `org-disputed-keys'.
  333. This option is only relevant at load-time of Org-mode, and must be set
  334. *before* org.el is loaded. Changing it requires a restart of Emacs to
  335. become effective."
  336. :group 'org-startup
  337. :type 'boolean)
  338. (defcustom org-use-extra-keys nil
  339. "Non-nil means use extra key sequence definitions for certain
  340. commands. This happens automatically if you run XEmacs or if
  341. window-system is nil. This variable lets you do the same
  342. manually. You must set it before loading org.
  343. Example: on Carbon Emacs 22 running graphically, with an external
  344. keyboard on a Powerbook, the default way of setting M-left might
  345. not work for either Alt or ESC. Setting this variable will make
  346. it work for ESC."
  347. :group 'org-startup
  348. :type 'boolean)
  349. (if (fboundp 'defvaralias)
  350. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  351. (defcustom org-disputed-keys
  352. '(([(shift up)] . [(meta p)])
  353. ([(shift down)] . [(meta n)])
  354. ([(shift left)] . [(meta -)])
  355. ([(shift right)] . [(meta +)])
  356. ([(control shift right)] . [(meta shift +)])
  357. ([(control shift left)] . [(meta shift -)]))
  358. "Keys for which Org-mode and other modes compete.
  359. This is an alist, cars are the default keys, second element specifies
  360. the alternative to use when `org-replace-disputed-keys' is t.
  361. Keys can be specified in any syntax supported by `define-key'.
  362. The value of this option takes effect only at Org-mode's startup,
  363. therefore you'll have to restart Emacs to apply it after changing."
  364. :group 'org-startup
  365. :type 'alist)
  366. (defun org-key (key)
  367. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  368. Or return the original if not disputed."
  369. (if org-replace-disputed-keys
  370. (let* ((nkey (key-description key))
  371. (x (org-find-if (lambda (x)
  372. (equal (key-description (car x)) nkey))
  373. org-disputed-keys)))
  374. (if x (cdr x) key))
  375. key))
  376. (defun org-find-if (predicate seq)
  377. (catch 'exit
  378. (while seq
  379. (if (funcall predicate (car seq))
  380. (throw 'exit (car seq))
  381. (pop seq)))))
  382. (defun org-defkey (keymap key def)
  383. "Define a key, possibly translated, as returned by `org-key'."
  384. (define-key keymap (org-key key) def))
  385. (defcustom org-ellipsis nil
  386. "The ellipsis to use in the Org-mode outline.
  387. When nil, just use the standard three dots. When a string, use that instead,
  388. When a face, use the standard 3 dots, but with the specified face.
  389. The change affects only Org-mode (which will then use its own display table).
  390. Changing this requires executing `M-x org-mode' in a buffer to become
  391. effective."
  392. :group 'org-startup
  393. :type '(choice (const :tag "Default" nil)
  394. (face :tag "Face" :value org-warning)
  395. (string :tag "String" :value "...#")))
  396. (defvar org-display-table nil
  397. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  398. (defgroup org-keywords nil
  399. "Keywords in Org-mode."
  400. :tag "Org Keywords"
  401. :group 'org)
  402. (defcustom org-deadline-string "DEADLINE:"
  403. "String to mark deadline entries.
  404. A deadline is this string, followed by a time stamp. Should be a word,
  405. terminated by a colon. You can insert a schedule keyword and
  406. a timestamp with \\[org-deadline].
  407. Changes become only effective after restarting Emacs."
  408. :group 'org-keywords
  409. :type 'string)
  410. (defcustom org-scheduled-string "SCHEDULED:"
  411. "String to mark scheduled TODO entries.
  412. A schedule is this string, followed by a time stamp. Should be a word,
  413. terminated by a colon. You can insert a schedule keyword and
  414. a timestamp with \\[org-schedule].
  415. Changes become only effective after restarting Emacs."
  416. :group 'org-keywords
  417. :type 'string)
  418. (defcustom org-closed-string "CLOSED:"
  419. "String used as the prefix for timestamps logging closing a TODO entry."
  420. :group 'org-keywords
  421. :type 'string)
  422. (defcustom org-clock-string "CLOCK:"
  423. "String used as prefix for timestamps clocking work hours on an item."
  424. :group 'org-keywords
  425. :type 'string)
  426. (defcustom org-comment-string "COMMENT"
  427. "Entries starting with this keyword will never be exported.
  428. An entry can be toggled between COMMENT and normal with
  429. \\[org-toggle-comment].
  430. Changes become only effective after restarting Emacs."
  431. :group 'org-keywords
  432. :type 'string)
  433. (defcustom org-quote-string "QUOTE"
  434. "Entries starting with this keyword will be exported in fixed-width font.
  435. Quoting applies only to the text in the entry following the headline, and does
  436. not extend beyond the next headline, even if that is lower level.
  437. An entry can be toggled between QUOTE and normal with
  438. \\[org-toggle-fixed-width-section]."
  439. :group 'org-keywords
  440. :type 'string)
  441. (defconst org-repeat-re
  442. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  443. "Regular expression for specifying repeated events.
  444. After a match, group 1 contains the repeat expression.")
  445. (defgroup org-structure nil
  446. "Options concerning the general structure of Org-mode files."
  447. :tag "Org Structure"
  448. :group 'org)
  449. (defgroup org-reveal-location nil
  450. "Options about how to make context of a location visible."
  451. :tag "Org Reveal Location"
  452. :group 'org-structure)
  453. (defconst org-context-choice
  454. '(choice
  455. (const :tag "Always" t)
  456. (const :tag "Never" nil)
  457. (repeat :greedy t :tag "Individual contexts"
  458. (cons
  459. (choice :tag "Context"
  460. (const agenda)
  461. (const org-goto)
  462. (const occur-tree)
  463. (const tags-tree)
  464. (const link-search)
  465. (const mark-goto)
  466. (const bookmark-jump)
  467. (const isearch)
  468. (const default))
  469. (boolean))))
  470. "Contexts for the reveal options.")
  471. (defcustom org-show-hierarchy-above '((default . t))
  472. "Non-nil means show full hierarchy when revealing a location.
  473. Org-mode often shows locations in an org-mode file which might have
  474. been invisible before. When this is set, the hierarchy of headings
  475. above the exposed location is shown.
  476. Turning this off for example for sparse trees makes them very compact.
  477. Instead of t, this can also be an alist specifying this option for different
  478. contexts. Valid contexts are
  479. agenda when exposing an entry from the agenda
  480. org-goto when using the command `org-goto' on key C-c C-j
  481. occur-tree when using the command `org-occur' on key C-c /
  482. tags-tree when constructing a sparse tree based on tags matches
  483. link-search when exposing search matches associated with a link
  484. mark-goto when exposing the jump goal of a mark
  485. bookmark-jump when exposing a bookmark location
  486. isearch when exiting from an incremental search
  487. default default for all contexts not set explicitly"
  488. :group 'org-reveal-location
  489. :type org-context-choice)
  490. (defcustom org-show-following-heading '((default . nil))
  491. "Non-nil means show following heading when revealing a location.
  492. Org-mode often shows locations in an org-mode file which might have
  493. been invisible before. When this is set, the heading following the
  494. match is shown.
  495. Turning this off for example for sparse trees makes them very compact,
  496. but makes it harder to edit the location of the match. In such a case,
  497. use the command \\[org-reveal] to show more context.
  498. Instead of t, this can also be an alist specifying this option for different
  499. contexts. See `org-show-hierarchy-above' for valid contexts."
  500. :group 'org-reveal-location
  501. :type org-context-choice)
  502. (defcustom org-show-siblings '((default . nil) (isearch t))
  503. "Non-nil means show all sibling heading when revealing a location.
  504. Org-mode often shows locations in an org-mode file which might have
  505. been invisible before. When this is set, the sibling of the current entry
  506. heading are all made visible. If `org-show-hierarchy-above' is t,
  507. the same happens on each level of the hierarchy above the current entry.
  508. By default this is on for the isearch context, off for all other contexts.
  509. Turning this off for example for sparse trees makes them very compact,
  510. but makes it harder to edit the location of the match. In such a case,
  511. use the command \\[org-reveal] to show more context.
  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-show-entry-below '((default . nil))
  517. "Non-nil means show the entry below a headline when revealing a location.
  518. Org-mode often shows locations in an org-mode file which might have
  519. been invisible before. When this is set, the text below the headline that is
  520. exposed is also shown.
  521. By default this is off for all contexts.
  522. Instead of t, this can also be an alist specifying this option for different
  523. contexts. See `org-show-hierarchy-above' for valid contexts."
  524. :group 'org-reveal-location
  525. :type org-context-choice)
  526. (defcustom org-indirect-buffer-display 'other-window
  527. "How should indirect tree buffers be displayed?
  528. This applies to indirect buffers created with the commands
  529. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  530. Valid values are:
  531. current-window Display in the current window
  532. other-window Just display in another window.
  533. dedicated-frame Create one new frame, and re-use it each time.
  534. new-frame Make a new frame each time. Note that in this case
  535. previously-made indirect buffers are kept, and you need to
  536. kill these buffers yourself."
  537. :group 'org-structure
  538. :group 'org-agenda-windows
  539. :type '(choice
  540. (const :tag "In current window" current-window)
  541. (const :tag "In current frame, other window" other-window)
  542. (const :tag "Each time a new frame" new-frame)
  543. (const :tag "One dedicated frame" dedicated-frame)))
  544. (defcustom org-use-speed-commands nil
  545. "Non-nil means activate single letter commands at beginning of a headline.
  546. This may also be a function to test for appropriate locations where speed
  547. commands should be active."
  548. :group 'org-structure
  549. :type '(choice
  550. (const :tag "Never" nil)
  551. (const :tag "At beginning of headline stars" t)
  552. (function)))
  553. (defcustom org-speed-commands-user nil
  554. "Alist of additional speed commands.
  555. This list will be checked before `org-speed-commands-default'
  556. when the variable `org-use-speed-commands' is non-nil
  557. and when the cursor is at the beginning of a headline.
  558. The car if each entry is a string with a single letter, which must
  559. be assigned to `self-insert-command' in the global map.
  560. The cdr is either a command to be called interactively, a function
  561. to be called, or a form to be evaluated.
  562. An entry that is just a list with a single string will be interpreted
  563. as a descriptive headline that will be added when listing the speed
  564. copmmands in the Help buffer using the `?' speed command."
  565. :group 'org-structure
  566. :type '(repeat :value ("k" . ignore)
  567. (choice :value ("k" . ignore)
  568. (list :tag "Descriptive Headline" (string :tag "Headline"))
  569. (cons :tag "Letter and Command"
  570. (string :tag "Command letter")
  571. (choice
  572. (function)
  573. (sexp))))))
  574. (defgroup org-cycle nil
  575. "Options concerning visibility cycling in Org-mode."
  576. :tag "Org Cycle"
  577. :group 'org-structure)
  578. (defcustom org-cycle-skip-children-state-if-no-children t
  579. "Non-nil means skip CHILDREN state in entries that don't have any."
  580. :group 'org-cycle
  581. :type 'boolean)
  582. (defcustom org-cycle-max-level nil
  583. "Maximum level which should still be subject to visibility cycling.
  584. Levels higher than this will, for cycling, be treated as text, not a headline.
  585. When `org-odd-levels-only' is set, a value of N in this variable actually
  586. means 2N-1 stars as the limiting headline.
  587. When nil, cycle all levels.
  588. Note that the limiting level of cycling is also influenced by
  589. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  590. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  591. than its value."
  592. :group 'org-cycle
  593. :type '(choice
  594. (const :tag "No limit" nil)
  595. (integer :tag "Maximum level")))
  596. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  597. "Names of drawers. Drawers are not opened by cycling on the headline above.
  598. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  599. this:
  600. :DRAWERNAME:
  601. .....
  602. :END:
  603. The drawer \"PROPERTIES\" is special for capturing properties through
  604. the property API.
  605. Drawers can be defined on the per-file basis with a line like:
  606. #+DRAWERS: HIDDEN STATE PROPERTIES"
  607. :group 'org-structure
  608. :group 'org-cycle
  609. :type '(repeat (string :tag "Drawer Name")))
  610. (defcustom org-hide-block-startup nil
  611. "Non-nil means entering Org-mode will fold all blocks.
  612. This can also be set in on a per-file basis with
  613. #+STARTUP: hideblocks
  614. #+STARTUP: showblocks"
  615. :group 'org-startup
  616. :group 'org-cycle
  617. :type 'boolean)
  618. (defcustom org-cycle-global-at-bob nil
  619. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  620. This makes it possible to do global cycling without having to use S-TAB or
  621. C-u TAB. For this special case to work, the first line of the buffer
  622. must not be a headline - it may be empty or some other text. When used in
  623. this way, `org-cycle-hook' is disables temporarily, to make sure the
  624. cursor stays at the beginning of the buffer.
  625. When this option is nil, don't do anything special at the beginning
  626. of the buffer."
  627. :group 'org-cycle
  628. :type 'boolean)
  629. (defcustom org-cycle-level-after-item/entry-creation t
  630. "Non-nil means cycle entry level or item indentation in new empty entries.
  631. When the cursor is at the end of an empty headline, i.e with only stars
  632. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  633. and then all possible anchestor states, before returning to the original state.
  634. This makes data entry extremely fast: M-RET to create a new headline,
  635. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  636. When the cursor is at the end of an empty plain list item, one TAB will
  637. make it a subitem, two or more tabs will back up to make this an item
  638. higher up in the item hierarchy."
  639. :group 'org-cycle
  640. :type 'boolean)
  641. (defcustom org-cycle-emulate-tab t
  642. "Where should `org-cycle' emulate TAB.
  643. nil Never
  644. white Only in completely white lines
  645. whitestart Only at the beginning of lines, before the first non-white char
  646. t Everywhere except in headlines
  647. exc-hl-bol Everywhere except at the start of a headline
  648. If TAB is used in a place where it does not emulate TAB, the current subtree
  649. visibility is cycled."
  650. :group 'org-cycle
  651. :type '(choice (const :tag "Never" nil)
  652. (const :tag "Only in completely white lines" white)
  653. (const :tag "Before first char in a line" whitestart)
  654. (const :tag "Everywhere except in headlines" t)
  655. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  656. ))
  657. (defcustom org-cycle-separator-lines 2
  658. "Number of empty lines needed to keep an empty line between collapsed trees.
  659. If you leave an empty line between the end of a subtree and the following
  660. headline, this empty line is hidden when the subtree is folded.
  661. Org-mode will leave (exactly) one empty line visible if the number of
  662. empty lines is equal or larger to the number given in this variable.
  663. So the default 2 means at least 2 empty lines after the end of a subtree
  664. are needed to produce free space between a collapsed subtree and the
  665. following headline.
  666. If the number is negative, and the number of empty lines is at least -N,
  667. all empty lines are shown.
  668. Special case: when 0, never leave empty lines in collapsed view."
  669. :group 'org-cycle
  670. :type 'integer)
  671. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  672. (defcustom org-pre-cycle-hook nil
  673. "Hook that is run before visibility cycling is happening.
  674. The function(s) in this hook must accept a single argument which indicates
  675. the new state that will be set right after running this hook. The
  676. argument is a symbol. Before a global state change, it can have the values
  677. `overview', `content', or `all'. Before a local state change, it can have
  678. the values `folded', `children', or `subtree'."
  679. :group 'org-cycle
  680. :type 'hook)
  681. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  682. org-cycle-hide-drawers
  683. org-cycle-show-empty-lines
  684. org-optimize-window-after-visibility-change)
  685. "Hook that is run after `org-cycle' has changed the buffer visibility.
  686. The function(s) in this hook must accept a single argument which indicates
  687. the new state that was set by the most recent `org-cycle' command. The
  688. argument is a symbol. After a global state change, it can have the values
  689. `overview', `content', or `all'. After a local state change, it can have
  690. the values `folded', `children', or `subtree'."
  691. :group 'org-cycle
  692. :type 'hook)
  693. (defgroup org-edit-structure nil
  694. "Options concerning structure editing in Org-mode."
  695. :tag "Org Edit Structure"
  696. :group 'org-structure)
  697. (defcustom org-odd-levels-only nil
  698. "Non-nil means skip even levels and only use odd levels for the outline.
  699. This has the effect that two stars are being added/taken away in
  700. promotion/demotion commands. It also influences how levels are
  701. handled by the exporters.
  702. Changing it requires restart of `font-lock-mode' to become effective
  703. for fontification also in regions already fontified.
  704. You may also set this on a per-file basis by adding one of the following
  705. lines to the buffer:
  706. #+STARTUP: odd
  707. #+STARTUP: oddeven"
  708. :group 'org-edit-structure
  709. :group 'org-appearance
  710. :type 'boolean)
  711. (defcustom org-adapt-indentation t
  712. "Non-nil means adapt indentation to outline node level.
  713. When this variable is set, Org assumes that you write outlines by
  714. indenting text in each node to align with the headline (after the stars).
  715. The following issues are influenced by this variable:
  716. - When this is set and the *entire* text in an entry is indented, the
  717. indentation is increased by one space in a demotion command, and
  718. decreased by one in a promotion command. If any line in the entry
  719. body starts with text at column 0, indentation is not changed at all.
  720. - Property drawers and planning information is inserted indented when
  721. this variable s set. When nil, they will not be indented.
  722. - TAB indents a line relative to context. The lines below a headline
  723. will be indented when this variable is set.
  724. Note that this is all about true indentation, by adding and removing
  725. space characters. See also `org-indent.el' which does level-dependent
  726. indentation in a virtual way, i.e. at display time in Emacs."
  727. :group 'org-edit-structure
  728. :type 'boolean)
  729. (defcustom org-special-ctrl-a/e nil
  730. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  731. When t, `C-a' will bring back the cursor to the beginning of the
  732. headline text, i.e. after the stars and after a possible TODO keyword.
  733. In an item, this will be the position after the bullet.
  734. When the cursor is already at that position, another `C-a' will bring
  735. it to the beginning of the line.
  736. `C-e' will jump to the end of the headline, ignoring the presence of tags
  737. in the headline. A second `C-e' will then jump to the true end of the
  738. line, after any tags. This also means that, when this variable is
  739. non-nil, `C-e' also will never jump beyond the end of the heading of a
  740. folded section, i.e. not after the ellipses.
  741. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  742. going to the true line boundary first. Only a directly following, identical
  743. keypress will bring the cursor to the special positions.
  744. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  745. set separately."
  746. :group 'org-edit-structure
  747. :type '(choice
  748. (const :tag "off" nil)
  749. (const :tag "on: after stars/bullet and before tags first" t)
  750. (const :tag "reversed: true line boundary first" reversed)
  751. (cons :tag "Set C-a and C-e separately"
  752. (choice :tag "Special C-a"
  753. (const :tag "off" nil)
  754. (const :tag "on: after stars/bullet first" t)
  755. (const :tag "reversed: before stars/bullet first" reversed))
  756. (choice :tag "Special C-e"
  757. (const :tag "off" nil)
  758. (const :tag "on: before tags first" t)
  759. (const :tag "reversed: after tags first" reversed)))))
  760. (if (fboundp 'defvaralias)
  761. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  762. (defcustom org-special-ctrl-k nil
  763. "Non-nil means `C-k' will behave specially in headlines.
  764. When nil, `C-k' will call the default `kill-line' command.
  765. When t, the following will happen while the cursor is in the headline:
  766. - When the cursor is at the beginning of a headline, kill the entire
  767. line and possible the folded subtree below the line.
  768. - When in the middle of the headline text, kill the headline up to the tags.
  769. - When after the headline text, kill the tags."
  770. :group 'org-edit-structure
  771. :type 'boolean)
  772. (defcustom org-yank-folded-subtrees t
  773. "Non-nil means when yanking subtrees, fold them.
  774. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  775. it starts with a heading and all other headings in it are either children
  776. or siblings, then fold all the subtrees. However, do this only if no
  777. text after the yank would be swallowed into a folded tree by this action."
  778. :group 'org-edit-structure
  779. :type 'boolean)
  780. (defcustom org-yank-adjusted-subtrees nil
  781. "Non-nil means when yanking subtrees, adjust the level.
  782. With this setting, `org-paste-subtree' is used to insert the subtree, see
  783. this function for details."
  784. :group 'org-edit-structure
  785. :type 'boolean)
  786. (defcustom org-M-RET-may-split-line '((default . t))
  787. "Non-nil means M-RET will split the line at the cursor position.
  788. When nil, it will go to the end of the line before making a
  789. new line.
  790. You may also set this option in a different way for different
  791. contexts. Valid contexts are:
  792. headline when creating a new headline
  793. item when creating a new item
  794. table in a table field
  795. default the value to be used for all contexts not explicitly
  796. customized"
  797. :group 'org-structure
  798. :group 'org-table
  799. :type '(choice
  800. (const :tag "Always" t)
  801. (const :tag "Never" nil)
  802. (repeat :greedy t :tag "Individual contexts"
  803. (cons
  804. (choice :tag "Context"
  805. (const headline)
  806. (const item)
  807. (const table)
  808. (const default))
  809. (boolean)))))
  810. (defcustom org-insert-heading-respect-content nil
  811. "Non-nil means insert new headings after the current subtree.
  812. When nil, the new heading is created directly after the current line.
  813. The commands \\[org-insert-heading-respect-content] and
  814. \\[org-insert-todo-heading-respect-content] turn this variable on
  815. for the duration of the command."
  816. :group 'org-structure
  817. :type 'boolean)
  818. (defcustom org-blank-before-new-entry '((heading . auto)
  819. (plain-list-item . auto))
  820. "Should `org-insert-heading' leave a blank line before new heading/item?
  821. The value is an alist, with `heading' and `plain-list-item' as car,
  822. and a boolean flag as cdr. For plain lists, if the variable
  823. `org-empty-line-terminates-plain-lists' is set, the setting here
  824. is ignored and no empty line is inserted, to keep the list in tact."
  825. :group 'org-edit-structure
  826. :type '(list
  827. (cons (const heading)
  828. (choice (const :tag "Never" nil)
  829. (const :tag "Always" t)
  830. (const :tag "Auto" auto)))
  831. (cons (const plain-list-item)
  832. (choice (const :tag "Never" nil)
  833. (const :tag "Always" t)
  834. (const :tag "Auto" auto)))))
  835. (defcustom org-insert-heading-hook nil
  836. "Hook being run after inserting a new heading."
  837. :group 'org-edit-structure
  838. :type 'hook)
  839. (defcustom org-enable-fixed-width-editor t
  840. "Non-nil means lines starting with \":\" are treated as fixed-width.
  841. This currently only means they are never auto-wrapped.
  842. When nil, such lines will be treated like ordinary lines.
  843. See also the QUOTE keyword."
  844. :group 'org-edit-structure
  845. :type 'boolean)
  846. (defcustom org-goto-auto-isearch t
  847. "Non-nil means typing characters in org-goto starts incremental search."
  848. :group 'org-edit-structure
  849. :type 'boolean)
  850. (defgroup org-sparse-trees nil
  851. "Options concerning sparse trees in Org-mode."
  852. :tag "Org Sparse Trees"
  853. :group 'org-structure)
  854. (defcustom org-highlight-sparse-tree-matches t
  855. "Non-nil means highlight all matches that define a sparse tree.
  856. The highlights will automatically disappear the next time the buffer is
  857. changed by an edit command."
  858. :group 'org-sparse-trees
  859. :type 'boolean)
  860. (defcustom org-remove-highlights-with-change t
  861. "Non-nil means any change to the buffer will remove temporary highlights.
  862. Such highlights are created by `org-occur' and `org-clock-display'.
  863. When nil, `C-c C-c needs to be used to get rid of the highlights.
  864. The highlights created by `org-preview-latex-fragment' always need
  865. `C-c C-c' to be removed."
  866. :group 'org-sparse-trees
  867. :group 'org-time
  868. :type 'boolean)
  869. (defcustom org-occur-hook '(org-first-headline-recenter)
  870. "Hook that is run after `org-occur' has constructed a sparse tree.
  871. This can be used to recenter the window to show as much of the structure
  872. as possible."
  873. :group 'org-sparse-trees
  874. :type 'hook)
  875. (defgroup org-imenu-and-speedbar nil
  876. "Options concerning imenu and speedbar in Org-mode."
  877. :tag "Org Imenu and Speedbar"
  878. :group 'org-structure)
  879. (defcustom org-imenu-depth 2
  880. "The maximum level for Imenu access to Org-mode headlines.
  881. This also applied for speedbar access."
  882. :group 'org-imenu-and-speedbar
  883. :type 'integer)
  884. (defgroup org-table nil
  885. "Options concerning tables in Org-mode."
  886. :tag "Org Table"
  887. :group 'org)
  888. (defcustom org-enable-table-editor 'optimized
  889. "Non-nil means lines starting with \"|\" are handled by the table editor.
  890. When nil, such lines will be treated like ordinary lines.
  891. When equal to the symbol `optimized', the table editor will be optimized to
  892. do the following:
  893. - Automatic overwrite mode in front of whitespace in table fields.
  894. This makes the structure of the table stay in tact as long as the edited
  895. field does not exceed the column width.
  896. - Minimize the number of realigns. Normally, the table is aligned each time
  897. TAB or RET are pressed to move to another field. With optimization this
  898. happens only if changes to a field might have changed the column width.
  899. Optimization requires replacing the functions `self-insert-command',
  900. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  901. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  902. very good at guessing when a re-align will be necessary, but you can always
  903. force one with \\[org-ctrl-c-ctrl-c].
  904. If you would like to use the optimized version in Org-mode, but the
  905. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  906. This variable can be used to turn on and off the table editor during a session,
  907. but in order to toggle optimization, a restart is required.
  908. See also the variable `org-table-auto-blank-field'."
  909. :group 'org-table
  910. :type '(choice
  911. (const :tag "off" nil)
  912. (const :tag "on" t)
  913. (const :tag "on, optimized" optimized)))
  914. (defcustom org-self-insert-cluster-for-undo t
  915. "Non-nil means cluster self-insert commands for undo when possible.
  916. If this is set, then, like in the Emacs command loop, 20 consecutive
  917. characters will be undone together.
  918. This is configurable, because there is some impact on typing performance."
  919. :group 'org-table
  920. :type 'boolean)
  921. (defcustom org-table-tab-recognizes-table.el t
  922. "Non-nil means TAB will automatically notice a table.el table.
  923. When it sees such a table, it moves point into it and - if necessary -
  924. calls `table-recognize-table'."
  925. :group 'org-table-editing
  926. :type 'boolean)
  927. (defgroup org-link nil
  928. "Options concerning links in Org-mode."
  929. :tag "Org Link"
  930. :group 'org)
  931. (defvar org-link-abbrev-alist-local nil
  932. "Buffer-local version of `org-link-abbrev-alist', which see.
  933. The value of this is taken from the #+LINK lines.")
  934. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  935. (defcustom org-link-abbrev-alist nil
  936. "Alist of link abbreviations.
  937. The car of each element is a string, to be replaced at the start of a link.
  938. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  939. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  940. [[linkkey:tag][description]]
  941. The 'linkkey' must be a word word, starting with a letter, followed
  942. by letters, numbers, '-' or '_'.
  943. If REPLACE is a string, the tag will simply be appended to create the link.
  944. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  945. the placeholder \"%h\" will cause a url-encoded version of the tag to
  946. be inserted at that point (see the function `url-hexify-string').
  947. REPLACE may also be a function that will be called with the tag as the
  948. only argument to create the link, which should be returned as a string.
  949. See the manual for examples."
  950. :group 'org-link
  951. :type '(repeat
  952. (cons
  953. (string :tag "Protocol")
  954. (choice
  955. (string :tag "Format")
  956. (function)))))
  957. (defcustom org-descriptive-links t
  958. "Non-nil means hide link part and only show description of bracket links.
  959. Bracket links are like [[link][description]]. This variable sets the initial
  960. state in new org-mode buffers. The setting can then be toggled on a
  961. per-buffer basis from the Org->Hyperlinks menu."
  962. :group 'org-link
  963. :type 'boolean)
  964. (defcustom org-link-file-path-type 'adaptive
  965. "How the path name in file links should be stored.
  966. Valid values are:
  967. relative Relative to the current directory, i.e. the directory of the file
  968. into which the link is being inserted.
  969. absolute Absolute path, if possible with ~ for home directory.
  970. noabbrev Absolute path, no abbreviation of home directory.
  971. adaptive Use relative path for files in the current directory and sub-
  972. directories of it. For other files, use an absolute path."
  973. :group 'org-link
  974. :type '(choice
  975. (const relative)
  976. (const absolute)
  977. (const noabbrev)
  978. (const adaptive)))
  979. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  980. "Types of links that should be activated in Org-mode files.
  981. This is a list of symbols, each leading to the activation of a certain link
  982. type. In principle, it does not hurt to turn on most link types - there may
  983. be a small gain when turning off unused link types. The types are:
  984. bracket The recommended [[link][description]] or [[link]] links with hiding.
  985. angular Links in angular brackets that may contain whitespace like
  986. <bbdb:Carsten Dominik>.
  987. plain Plain links in normal text, no whitespace, like http://google.com.
  988. radio Text that is matched by a radio target, see manual for details.
  989. tag Tag settings in a headline (link to tag search).
  990. date Time stamps (link to calendar).
  991. footnote Footnote labels.
  992. Changing this variable requires a restart of Emacs to become effective."
  993. :group 'org-link
  994. :type '(set :greedy t
  995. (const :tag "Double bracket links (new style)" bracket)
  996. (const :tag "Angular bracket links (old style)" angular)
  997. (const :tag "Plain text links" plain)
  998. (const :tag "Radio target matches" radio)
  999. (const :tag "Tags" tag)
  1000. (const :tag "Timestamps" date)
  1001. (const :tag "Footnotes" footnote)))
  1002. (defcustom org-make-link-description-function nil
  1003. "Function to use to generate link descriptions from links. If
  1004. nil the link location will be used. This function must take two
  1005. parameters; the first is the link and the second the description
  1006. org-insert-link has generated, and should return the description
  1007. to use."
  1008. :group 'org-link
  1009. :type 'function)
  1010. (defgroup org-link-store nil
  1011. "Options concerning storing links in Org-mode."
  1012. :tag "Org Store Link"
  1013. :group 'org-link)
  1014. (defcustom org-email-link-description-format "Email %c: %.30s"
  1015. "Format of the description part of a link to an email or usenet message.
  1016. The following %-escapes will be replaced by corresponding information:
  1017. %F full \"From\" field
  1018. %f name, taken from \"From\" field, address if no name
  1019. %T full \"To\" field
  1020. %t first name in \"To\" field, address if no name
  1021. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1022. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1023. %s subject
  1024. %m message-id.
  1025. You may use normal field width specification between the % and the letter.
  1026. This is for example useful to limit the length of the subject.
  1027. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1028. :group 'org-link-store
  1029. :type 'string)
  1030. (defcustom org-from-is-user-regexp
  1031. (let (r1 r2)
  1032. (when (and user-mail-address (not (string= user-mail-address "")))
  1033. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1034. (when (and user-full-name (not (string= user-full-name "")))
  1035. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1036. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1037. "Regexp matched against the \"From:\" header of an email or usenet message.
  1038. It should match if the message is from the user him/herself."
  1039. :group 'org-link-store
  1040. :type 'regexp)
  1041. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1042. "Non-nil means storing a link to an Org file will use entry IDs.
  1043. Note that before this variable is even considered, org-id must be loaded,
  1044. so please customize `org-modules' and turn it on.
  1045. The variable can have the following values:
  1046. t Create an ID if needed to make a link to the current entry.
  1047. create-if-interactive
  1048. If `org-store-link' is called directly (interactively, as a user
  1049. command), do create an ID to support the link. But when doing the
  1050. job for remember, only use the ID if it already exists. The
  1051. purpose of this setting is to avoid proliferation of unwanted
  1052. IDs, just because you happen to be in an Org file when you
  1053. call `org-remember' that automatically and preemptively
  1054. creates a link. If you do want to get an ID link in a remember
  1055. template to an entry not having an ID, create it first by
  1056. explicitly creating a link to it, using `C-c C-l' first.
  1057. create-if-interactive-and-no-custom-id
  1058. Like create-if-interactive, but do not create an ID if there is
  1059. a CUSTOM_ID property defined in the entry. This is the default.
  1060. use-existing
  1061. Use existing ID, do not create one.
  1062. nil Never use an ID to make a link, instead link using a text search for
  1063. the headline text."
  1064. :group 'org-link-store
  1065. :type '(choice
  1066. (const :tag "Create ID to make link" t)
  1067. (const :tag "Create if storing link interactively"
  1068. create-if-interactive)
  1069. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1070. create-if-interactive-and-no-custom-id)
  1071. (const :tag "Only use existing" use-existing)
  1072. (const :tag "Do not use ID to create link" nil)))
  1073. (defcustom org-context-in-file-links t
  1074. "Non-nil means file links from `org-store-link' contain context.
  1075. A search string will be added to the file name with :: as separator and
  1076. used to find the context when the link is activated by the command
  1077. `org-open-at-point'.
  1078. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1079. negates this setting for the duration of the command."
  1080. :group 'org-link-store
  1081. :type 'boolean)
  1082. (defcustom org-keep-stored-link-after-insertion nil
  1083. "Non-nil means keep link in list for entire session.
  1084. The command `org-store-link' adds a link pointing to the current
  1085. location to an internal list. These links accumulate during a session.
  1086. The command `org-insert-link' can be used to insert links into any
  1087. Org-mode file (offering completion for all stored links). When this
  1088. option is nil, every link which has been inserted once using \\[org-insert-link]
  1089. will be removed from the list, to make completing the unused links
  1090. more efficient."
  1091. :group 'org-link-store
  1092. :type 'boolean)
  1093. (defgroup org-link-follow nil
  1094. "Options concerning following links in Org-mode."
  1095. :tag "Org Follow Link"
  1096. :group 'org-link)
  1097. (defcustom org-link-translation-function nil
  1098. "Function to translate links with different syntax to Org syntax.
  1099. This can be used to translate links created for example by the Planner
  1100. or emacs-wiki packages to Org syntax.
  1101. The function must accept two parameters, a TYPE containing the link
  1102. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1103. which is everything after the link protocol. It should return a cons
  1104. with possibly modified values of type and path.
  1105. Org contains a function for this, so if you set this variable to
  1106. `org-translate-link-from-planner', you should be able follow many
  1107. links created by planner."
  1108. :group 'org-link-follow
  1109. :type 'function)
  1110. (defcustom org-follow-link-hook nil
  1111. "Hook that is run after a link has been followed."
  1112. :group 'org-link-follow
  1113. :type 'hook)
  1114. (defcustom org-tab-follows-link nil
  1115. "Non-nil means on links TAB will follow the link.
  1116. Needs to be set before org.el is loaded.
  1117. This really should not be used, it does not make sense, and the
  1118. implementation is bad."
  1119. :group 'org-link-follow
  1120. :type 'boolean)
  1121. (defcustom org-return-follows-link nil
  1122. "Non-nil means on links RET will follow the link.
  1123. Needs to be set before org.el is loaded."
  1124. :group 'org-link-follow
  1125. :type 'boolean)
  1126. (defcustom org-mouse-1-follows-link
  1127. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1128. "Non-nil means mouse-1 on a link will follow the link.
  1129. A longer mouse click will still set point. Does not work on XEmacs.
  1130. Needs to be set before org.el is loaded."
  1131. :group 'org-link-follow
  1132. :type 'boolean)
  1133. (defcustom org-mark-ring-length 4
  1134. "Number of different positions to be recorded in the ring
  1135. Changing this requires a restart of Emacs to work correctly."
  1136. :group 'org-link-follow
  1137. :type 'integer)
  1138. (defcustom org-link-frame-setup
  1139. '((vm . vm-visit-folder-other-frame)
  1140. (gnus . gnus-other-frame)
  1141. (file . find-file-other-window))
  1142. "Setup the frame configuration for following links.
  1143. When following a link with Emacs, it may often be useful to display
  1144. this link in another window or frame. This variable can be used to
  1145. set this up for the different types of links.
  1146. For VM, use any of
  1147. `vm-visit-folder'
  1148. `vm-visit-folder-other-frame'
  1149. For Gnus, use any of
  1150. `gnus'
  1151. `gnus-other-frame'
  1152. `org-gnus-no-new-news'
  1153. For FILE, use any of
  1154. `find-file'
  1155. `find-file-other-window'
  1156. `find-file-other-frame'
  1157. For the calendar, use the variable `calendar-setup'.
  1158. For BBDB, it is currently only possible to display the matches in
  1159. another window."
  1160. :group 'org-link-follow
  1161. :type '(list
  1162. (cons (const vm)
  1163. (choice
  1164. (const vm-visit-folder)
  1165. (const vm-visit-folder-other-window)
  1166. (const vm-visit-folder-other-frame)))
  1167. (cons (const gnus)
  1168. (choice
  1169. (const gnus)
  1170. (const gnus-other-frame)
  1171. (const org-gnus-no-new-news)))
  1172. (cons (const file)
  1173. (choice
  1174. (const find-file)
  1175. (const find-file-other-window)
  1176. (const find-file-other-frame)))))
  1177. (defcustom org-display-internal-link-with-indirect-buffer nil
  1178. "Non-nil means use indirect buffer to display infile links.
  1179. Activating internal links (from one location in a file to another location
  1180. in the same file) normally just jumps to the location. When the link is
  1181. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1182. another window. When this option is set, the other window actually displays
  1183. an indirect buffer clone of the current buffer, to avoid any visibility
  1184. changes to the current buffer."
  1185. :group 'org-link-follow
  1186. :type 'boolean)
  1187. (defcustom org-open-non-existing-files nil
  1188. "Non-nil means `org-open-file' will open non-existing files.
  1189. When nil, an error will be generated.
  1190. This variable applies only to external applications because they
  1191. might choke on non-existing files. If the link is to a file that
  1192. will be opened in Emacs, the variable is ignored."
  1193. :group 'org-link-follow
  1194. :type 'boolean)
  1195. (defcustom org-open-directory-means-index-dot-org nil
  1196. "Non-nil means a link to a directory really means to index.org.
  1197. When nil, following a directory link will run dired or open a finder/explorer
  1198. window on that directory."
  1199. :group 'org-link-follow
  1200. :type 'boolean)
  1201. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1202. "Function and arguments to call for following mailto links.
  1203. This is a list with the first element being a lisp function, and the
  1204. remaining elements being arguments to the function. In string arguments,
  1205. %a will be replaced by the address, and %s will be replaced by the subject
  1206. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1207. :group 'org-link-follow
  1208. :type '(choice
  1209. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1210. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1211. (const :tag "message-mail" (message-mail "%a" "%s"))
  1212. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1213. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1214. "Non-nil means ask for confirmation before executing shell links.
  1215. Shell links can be dangerous: just think about a link
  1216. [[shell:rm -rf ~/*][Google Search]]
  1217. This link would show up in your Org-mode document as \"Google Search\",
  1218. but really it would remove your entire home directory.
  1219. Therefore we advise against setting this variable to nil.
  1220. Just change it to `y-or-n-p' if you want to confirm with a
  1221. single keystroke rather than having to type \"yes\"."
  1222. :group 'org-link-follow
  1223. :type '(choice
  1224. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1225. (const :tag "with y-or-n (faster)" y-or-n-p)
  1226. (const :tag "no confirmation (dangerous)" nil)))
  1227. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1228. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1229. Elisp links can be dangerous: just think about a link
  1230. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1231. This link would show up in your Org-mode document as \"Google Search\",
  1232. but really it would remove your entire home directory.
  1233. Therefore we advise against setting this variable to nil.
  1234. Just change it to `y-or-n-p' if you want to confirm with a
  1235. single keystroke rather than having to type \"yes\"."
  1236. :group 'org-link-follow
  1237. :type '(choice
  1238. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1239. (const :tag "with y-or-n (faster)" y-or-n-p)
  1240. (const :tag "no confirmation (dangerous)" nil)))
  1241. (defconst org-file-apps-defaults-gnu
  1242. '((remote . emacs)
  1243. (system . mailcap)
  1244. (t . mailcap))
  1245. "Default file applications on a UNIX or GNU/Linux system.
  1246. See `org-file-apps'.")
  1247. (defconst org-file-apps-defaults-macosx
  1248. '((remote . emacs)
  1249. (t . "open %s")
  1250. (system . "open %s")
  1251. ("ps.gz" . "gv %s")
  1252. ("eps.gz" . "gv %s")
  1253. ("dvi" . "xdvi %s")
  1254. ("fig" . "xfig %s"))
  1255. "Default file applications on a MacOS X system.
  1256. The system \"open\" is known as a default, but we use X11 applications
  1257. for some files for which the OS does not have a good default.
  1258. See `org-file-apps'.")
  1259. (defconst org-file-apps-defaults-windowsnt
  1260. (list
  1261. '(remote . emacs)
  1262. (cons t
  1263. (list (if (featurep 'xemacs)
  1264. 'mswindows-shell-execute
  1265. 'w32-shell-execute)
  1266. "open" 'file))
  1267. (cons 'system
  1268. (list (if (featurep 'xemacs)
  1269. 'mswindows-shell-execute
  1270. 'w32-shell-execute)
  1271. "open" 'file)))
  1272. "Default file applications on a Windows NT system.
  1273. The system \"open\" is used for most files.
  1274. See `org-file-apps'.")
  1275. (defcustom org-file-apps
  1276. '(
  1277. (auto-mode . emacs)
  1278. ("\\.mm\\'" . default)
  1279. ("\\.x?html?\\'" . default)
  1280. ("\\.pdf\\'" . default)
  1281. )
  1282. "External applications for opening `file:path' items in a document.
  1283. Org-mode uses system defaults for different file types, but
  1284. you can use this variable to set the application for a given file
  1285. extension. The entries in this list are cons cells where the car identifies
  1286. files and the cdr the corresponding command. Possible values for the
  1287. file identifier are
  1288. \"regex\" Regular expression matched against the file name. For backward
  1289. compatibility, this can also be a string with only alphanumeric
  1290. characters, which is then interpreted as an extension.
  1291. `directory' Matches a directory
  1292. `remote' Matches a remote file, accessible through tramp or efs.
  1293. Remote files most likely should be visited through Emacs
  1294. because external applications cannot handle such paths.
  1295. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1296. so all files Emacs knows how to handle. Using this with
  1297. command `emacs' will open most files in Emacs. Beware that this
  1298. will also open html files inside Emacs, unless you add
  1299. (\"html\" . default) to the list as well.
  1300. t Default for files not matched by any of the other options.
  1301. `system' The system command to open files, like `open' on Windows
  1302. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1303. that will be selected if you call `C-c C-o' with a double
  1304. `C-u C-u' prefix.
  1305. Possible values for the command are:
  1306. `emacs' The file will be visited by the current Emacs process.
  1307. `default' Use the default application for this file type, which is the
  1308. association for t in the list, most likely in the system-specific
  1309. part.
  1310. This can be used to overrule an unwanted setting in the
  1311. system-specific variable.
  1312. `system' Use the system command for opening files, like \"open\".
  1313. This command is specified by the entry whose car is `system'.
  1314. Most likely, the system-specific version of this variable
  1315. does define this command, but you can overrule/replace it
  1316. here.
  1317. string A command to be executed by a shell; %s will be replaced
  1318. by the path to the file.
  1319. sexp A Lisp form which will be evaluated. The file path will
  1320. be available in the Lisp variable `file'.
  1321. For more examples, see the system specific constants
  1322. `org-file-apps-defaults-macosx'
  1323. `org-file-apps-defaults-windowsnt'
  1324. `org-file-apps-defaults-gnu'."
  1325. :group 'org-link-follow
  1326. :type '(repeat
  1327. (cons (choice :value ""
  1328. (string :tag "Extension")
  1329. (const :tag "System command to open files" system)
  1330. (const :tag "Default for unrecognized files" t)
  1331. (const :tag "Remote file" remote)
  1332. (const :tag "Links to a directory" directory)
  1333. (const :tag "Any files that have Emacs modes"
  1334. auto-mode))
  1335. (choice :value ""
  1336. (const :tag "Visit with Emacs" emacs)
  1337. (const :tag "Use default" default)
  1338. (const :tag "Use the system command" system)
  1339. (string :tag "Command")
  1340. (sexp :tag "Lisp form")))))
  1341. (defgroup org-refile nil
  1342. "Options concerning refiling entries in Org-mode."
  1343. :tag "Org Refile"
  1344. :group 'org)
  1345. (defcustom org-directory "~/org"
  1346. "Directory with org files.
  1347. This is just a default location to look for Org files. There is no need
  1348. at all to put your files into this directory. It is only used in the
  1349. following situations:
  1350. 1. When a remember template specifies a target file that is not an
  1351. absolute path. The path will then be interpreted relative to
  1352. `org-directory'
  1353. 2. When a remember note is filed away in an interactive way (when exiting the
  1354. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1355. with `org-directory' as the default path."
  1356. :group 'org-refile
  1357. :group 'org-remember
  1358. :type 'directory)
  1359. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1360. "Default target for storing notes.
  1361. Used by the hooks for remember.el. This can be a string, or nil to mean
  1362. the value of `remember-data-file'.
  1363. You can set this on a per-template basis with the variable
  1364. `org-remember-templates'."
  1365. :group 'org-refile
  1366. :group 'org-remember
  1367. :type '(choice
  1368. (const :tag "Default from remember-data-file" nil)
  1369. file))
  1370. (defcustom org-goto-interface 'outline
  1371. "The default interface to be used for `org-goto'.
  1372. Allowed values are:
  1373. outline The interface shows an outline of the relevant file
  1374. and the correct heading is found by moving through
  1375. the outline or by searching with incremental search.
  1376. outline-path-completion Headlines in the current buffer are offered via
  1377. completion. This is the interface also used by
  1378. the refile command."
  1379. :group 'org-refile
  1380. :type '(choice
  1381. (const :tag "Outline" outline)
  1382. (const :tag "Outline-path-completion" outline-path-completion)))
  1383. (defcustom org-goto-max-level 5
  1384. "Maximum level to be considered when running org-goto with refile interface."
  1385. :group 'org-refile
  1386. :type 'integer)
  1387. (defcustom org-reverse-note-order nil
  1388. "Non-nil means store new notes at the beginning of a file or entry.
  1389. When nil, new notes will be filed to the end of a file or entry.
  1390. This can also be a list with cons cells of regular expressions that
  1391. are matched against file names, and values."
  1392. :group 'org-remember
  1393. :group 'org-refile
  1394. :type '(choice
  1395. (const :tag "Reverse always" t)
  1396. (const :tag "Reverse never" nil)
  1397. (repeat :tag "By file name regexp"
  1398. (cons regexp boolean))))
  1399. (defcustom org-log-refile nil
  1400. "Information to record when a task is refiled.
  1401. Possible values are:
  1402. nil Don't add anything
  1403. time Add a time stamp to the task
  1404. note Prompt for a note and add it with template `org-log-note-headings'
  1405. This option can also be set with on a per-file-basis with
  1406. #+STARTUP: nologrefile
  1407. #+STARTUP: logrefile
  1408. #+STARTUP: lognoterefile
  1409. You can have local logging settings for a subtree by setting the LOGGING
  1410. property to one or more of these keywords.
  1411. When bulk-refiling from the agenda, the value `note' is forbidden and
  1412. will temporarily be changed to `time'."
  1413. :group 'org-refile
  1414. :group 'org-progress
  1415. :type '(choice
  1416. (const :tag "No logging" nil)
  1417. (const :tag "Record timestamp" time)
  1418. (const :tag "Record timestamp with note." note)))
  1419. (defcustom org-refile-targets nil
  1420. "Targets for refiling entries with \\[org-refile].
  1421. This is list of cons cells. Each cell contains:
  1422. - a specification of the files to be considered, either a list of files,
  1423. or a symbol whose function or variable value will be used to retrieve
  1424. a file name or a list of file names. If you use `org-agenda-files' for
  1425. that, all agenda files will be scanned for targets. Nil means consider
  1426. headings in the current buffer.
  1427. - A specification of how to find candidate refile targets. This may be
  1428. any of:
  1429. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1430. This tag has to be present in all target headlines, inheritance will
  1431. not be considered.
  1432. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1433. todo keyword.
  1434. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1435. headlines that are refiling targets.
  1436. - a cons cell (:level . N). Any headline of level N is considered a target.
  1437. Note that, when `org-odd-levels-only' is set, level corresponds to
  1438. order in hierarchy, not to the number of stars.
  1439. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1440. Note that, when `org-odd-levels-only' is set, level corresponds to
  1441. order in hierarchy, not to the number of stars.
  1442. You can set the variable `org-refile-target-verify-function' to a function
  1443. to verify each headline found by the simple critery above.
  1444. When this variable is nil, all top-level headlines in the current buffer
  1445. are used, equivalent to the value `((nil . (:level . 1))'."
  1446. :group 'org-refile
  1447. :type '(repeat
  1448. (cons
  1449. (choice :value org-agenda-files
  1450. (const :tag "All agenda files" org-agenda-files)
  1451. (const :tag "Current buffer" nil)
  1452. (function) (variable) (file))
  1453. (choice :tag "Identify target headline by"
  1454. (cons :tag "Specific tag" (const :value :tag) (string))
  1455. (cons :tag "TODO keyword" (const :value :todo) (string))
  1456. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1457. (cons :tag "Level number" (const :value :level) (integer))
  1458. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1459. (defcustom org-refile-target-verify-function nil
  1460. "Function to verify if the headline at point should be a refile target.
  1461. The function will be called without arguments, with point at the
  1462. beginning of the headline. It should return t and leave point
  1463. where it is if the headline is a valid target for refiling.
  1464. If the target should not be selected, the function must return nil.
  1465. In addition to this, it may move point to a place from where the search
  1466. should be continued. For example, the function may decide that the entire
  1467. subtree of the current entry should be excluded and move point to the end
  1468. of the subtree."
  1469. :group 'org-refile
  1470. :type 'function)
  1471. (defcustom org-refile-use-outline-path nil
  1472. "Non-nil means provide refile targets as paths.
  1473. So a level 3 headline will be available as level1/level2/level3.
  1474. When the value is `file', also include the file name (without directory)
  1475. into the path. In this case, you can also stop the completion after
  1476. the file name, to get entries inserted as top level in the file.
  1477. When `full-file-path', include the full file path."
  1478. :group 'org-refile
  1479. :type '(choice
  1480. (const :tag "Not" nil)
  1481. (const :tag "Yes" t)
  1482. (const :tag "Start with file name" file)
  1483. (const :tag "Start with full file path" full-file-path)))
  1484. (defcustom org-outline-path-complete-in-steps t
  1485. "Non-nil means complete the outline path in hierarchical steps.
  1486. When Org-mode uses the refile interface to select an outline path
  1487. \(see variable `org-refile-use-outline-path'), the completion of
  1488. the path can be done is a single go, or if can be done in steps down
  1489. the headline hierarchy. Going in steps is probably the best if you
  1490. do not use a special completion package like `ido' or `icicles'.
  1491. However, when using these packages, going in one step can be very
  1492. fast, while still showing the whole path to the entry."
  1493. :group 'org-refile
  1494. :type 'boolean)
  1495. (defcustom org-refile-allow-creating-parent-nodes nil
  1496. "Non-nil means allow to create new nodes as refile targets.
  1497. New nodes are then created by adding \"/new node name\" to the completion
  1498. of an existing node. When the value of this variable is `confirm',
  1499. new node creation must be confirmed by the user (recommended)
  1500. When nil, the completion must match an existing entry.
  1501. Note that, if the new heading is not seen by the criteria
  1502. listed in `org-refile-targets', multiple instances of the same
  1503. heading would be created by trying again to file under the new
  1504. heading."
  1505. :group 'org-refile
  1506. :type '(choice
  1507. (const :tag "Never" nil)
  1508. (const :tag "Always" t)
  1509. (const :tag "Prompt for confirmation" confirm)))
  1510. (defgroup org-todo nil
  1511. "Options concerning TODO items in Org-mode."
  1512. :tag "Org TODO"
  1513. :group 'org)
  1514. (defgroup org-progress nil
  1515. "Options concerning Progress logging in Org-mode."
  1516. :tag "Org Progress"
  1517. :group 'org-time)
  1518. (defvar org-todo-interpretation-widgets
  1519. '(
  1520. (:tag "Sequence (cycling hits every state)" sequence)
  1521. (:tag "Type (cycling directly to DONE)" type))
  1522. "The available interpretation symbols for customizing
  1523. `org-todo-keywords'.
  1524. Interested libraries should add to this list.")
  1525. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1526. "List of TODO entry keyword sequences and their interpretation.
  1527. \\<org-mode-map>This is a list of sequences.
  1528. Each sequence starts with a symbol, either `sequence' or `type',
  1529. indicating if the keywords should be interpreted as a sequence of
  1530. action steps, or as different types of TODO items. The first
  1531. keywords are states requiring action - these states will select a headline
  1532. for inclusion into the global TODO list Org-mode produces. If one of
  1533. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1534. signify that no further action is necessary. If \"|\" is not found,
  1535. the last keyword is treated as the only DONE state of the sequence.
  1536. The command \\[org-todo] cycles an entry through these states, and one
  1537. additional state where no keyword is present. For details about this
  1538. cycling, see the manual.
  1539. TODO keywords and interpretation can also be set on a per-file basis with
  1540. the special #+SEQ_TODO and #+TYP_TODO lines.
  1541. Each keyword can optionally specify a character for fast state selection
  1542. \(in combination with the variable `org-use-fast-todo-selection')
  1543. and specifiers for state change logging, using the same syntax
  1544. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1545. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1546. indicates to record a time stamp each time this state is selected.
  1547. Each keyword may also specify if a timestamp or a note should be
  1548. recorded when entering or leaving the state, by adding additional
  1549. characters in the parenthesis after the keyword. This looks like this:
  1550. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1551. record only the time of the state change. With X and Y being either
  1552. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1553. Y when leaving the state if and only if the *target* state does not
  1554. define X. You may omit any of the fast-selection key or X or /Y,
  1555. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1556. For backward compatibility, this variable may also be just a list
  1557. of keywords - in this case the interpretation (sequence or type) will be
  1558. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1559. :group 'org-todo
  1560. :group 'org-keywords
  1561. :type '(choice
  1562. (repeat :tag "Old syntax, just keywords"
  1563. (string :tag "Keyword"))
  1564. (repeat :tag "New syntax"
  1565. (cons
  1566. (choice
  1567. :tag "Interpretation"
  1568. ;;Quick and dirty way to see
  1569. ;;`org-todo-interpretations'. This takes the
  1570. ;;place of item arguments
  1571. :convert-widget
  1572. (lambda (widget)
  1573. (widget-put widget
  1574. :args (mapcar
  1575. #'(lambda (x)
  1576. (widget-convert
  1577. (cons 'const x)))
  1578. org-todo-interpretation-widgets))
  1579. widget))
  1580. (repeat
  1581. (string :tag "Keyword"))))))
  1582. (defvar org-todo-keywords-1 nil
  1583. "All TODO and DONE keywords active in a buffer.")
  1584. (make-variable-buffer-local 'org-todo-keywords-1)
  1585. (defvar org-todo-keywords-for-agenda nil)
  1586. (defvar org-done-keywords-for-agenda nil)
  1587. (defvar org-drawers-for-agenda nil)
  1588. (defvar org-todo-keyword-alist-for-agenda nil)
  1589. (defvar org-tag-alist-for-agenda nil)
  1590. (defvar org-agenda-contributing-files nil)
  1591. (defvar org-not-done-keywords nil)
  1592. (make-variable-buffer-local 'org-not-done-keywords)
  1593. (defvar org-done-keywords nil)
  1594. (make-variable-buffer-local 'org-done-keywords)
  1595. (defvar org-todo-heads nil)
  1596. (make-variable-buffer-local 'org-todo-heads)
  1597. (defvar org-todo-sets nil)
  1598. (make-variable-buffer-local 'org-todo-sets)
  1599. (defvar org-todo-log-states nil)
  1600. (make-variable-buffer-local 'org-todo-log-states)
  1601. (defvar org-todo-kwd-alist nil)
  1602. (make-variable-buffer-local 'org-todo-kwd-alist)
  1603. (defvar org-todo-key-alist nil)
  1604. (make-variable-buffer-local 'org-todo-key-alist)
  1605. (defvar org-todo-key-trigger nil)
  1606. (make-variable-buffer-local 'org-todo-key-trigger)
  1607. (defcustom org-todo-interpretation 'sequence
  1608. "Controls how TODO keywords are interpreted.
  1609. This variable is in principle obsolete and is only used for
  1610. backward compatibility, if the interpretation of todo keywords is
  1611. not given already in `org-todo-keywords'. See that variable for
  1612. more information."
  1613. :group 'org-todo
  1614. :group 'org-keywords
  1615. :type '(choice (const sequence)
  1616. (const type)))
  1617. (defcustom org-use-fast-todo-selection t
  1618. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1619. This variable describes if and under what circumstances the cycling
  1620. mechanism for TODO keywords will be replaced by a single-key, direct
  1621. selection scheme.
  1622. When nil, fast selection is never used.
  1623. When the symbol `prefix', it will be used when `org-todo' is called with
  1624. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1625. in an agenda buffer.
  1626. When t, fast selection is used by default. In this case, the prefix
  1627. argument forces cycling instead.
  1628. In all cases, the special interface is only used if access keys have actually
  1629. been assigned by the user, i.e. if keywords in the configuration are followed
  1630. by a letter in parenthesis, like TODO(t)."
  1631. :group 'org-todo
  1632. :type '(choice
  1633. (const :tag "Never" nil)
  1634. (const :tag "By default" t)
  1635. (const :tag "Only with C-u C-c C-t" prefix)))
  1636. (defcustom org-provide-todo-statistics t
  1637. "Non-nil means update todo statistics after insert and toggle.
  1638. ALL-HEADLINES means update todo statistics by including headlines
  1639. with no TODO keyword as well, counting them as not done.
  1640. A list of TODO keywords means the same, but skip keywords that are
  1641. not in this list.
  1642. When this is set, todo statistics is updated in the parent of the
  1643. current entry each time a todo state is changed."
  1644. :group 'org-todo
  1645. :type '(choice
  1646. (const :tag "Yes, only for TODO entries" t)
  1647. (const :tag "Yes, including all entries" 'all-headlines)
  1648. (repeat :tag "Yes, for TODOs in this list"
  1649. (string :tag "TODO keyword"))
  1650. (other :tag "No TODO statistics" nil)))
  1651. (defcustom org-hierarchical-todo-statistics t
  1652. "Non-nil means TODO statistics covers just direct children.
  1653. When nil, all entries in the subtree are considered.
  1654. This has only an effect if `org-provide-todo-statistics' is set.
  1655. To set this to nil for only a single subtree, use a COOKIE_DATA
  1656. property and include the word \"recursive\" into the value."
  1657. :group 'org-todo
  1658. :type 'boolean)
  1659. (defcustom org-after-todo-state-change-hook nil
  1660. "Hook which is run after the state of a TODO item was changed.
  1661. The new state (a string with a TODO keyword, or nil) is available in the
  1662. Lisp variable `state'."
  1663. :group 'org-todo
  1664. :type 'hook)
  1665. (defvar org-blocker-hook nil
  1666. "Hook for functions that are allowed to block a state change.
  1667. Each function gets as its single argument a property list, see
  1668. `org-trigger-hook' for more information about this list.
  1669. If any of the functions in this hook returns nil, the state change
  1670. is blocked.")
  1671. (defvar org-trigger-hook nil
  1672. "Hook for functions that are triggered by a state change.
  1673. Each function gets as its single argument a property list with at least
  1674. the following elements:
  1675. (:type type-of-change :position pos-at-entry-start
  1676. :from old-state :to new-state)
  1677. Depending on the type, more properties may be present.
  1678. This mechanism is currently implemented for:
  1679. TODO state changes
  1680. ------------------
  1681. :type todo-state-change
  1682. :from previous state (keyword as a string), or nil, or a symbol
  1683. 'todo' or 'done', to indicate the general type of state.
  1684. :to new state, like in :from")
  1685. (defcustom org-enforce-todo-dependencies nil
  1686. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1687. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1688. be blocked if any prior sibling is not yet done.
  1689. Finally, if the parent is blocked because of ordered siblings of its own,
  1690. the child will also be blocked.
  1691. This variable needs to be set before org.el is loaded, and you need to
  1692. restart Emacs after a change to make the change effective. The only way
  1693. to change is while Emacs is running is through the customize interface."
  1694. :set (lambda (var val)
  1695. (set var val)
  1696. (if val
  1697. (add-hook 'org-blocker-hook
  1698. 'org-block-todo-from-children-or-siblings-or-parent)
  1699. (remove-hook 'org-blocker-hook
  1700. 'org-block-todo-from-children-or-siblings-or-parent)))
  1701. :group 'org-todo
  1702. :type 'boolean)
  1703. (defcustom org-enforce-todo-checkbox-dependencies nil
  1704. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1705. When this is nil, checkboxes have no influence on switching TODO states.
  1706. When non-nil, you first need to check off all check boxes before the TODO
  1707. entry can be switched to DONE.
  1708. This variable needs to be set before org.el is loaded, and you need to
  1709. restart Emacs after a change to make the change effective. The only way
  1710. to change is while Emacs is running is through the customize interface."
  1711. :set (lambda (var val)
  1712. (set var val)
  1713. (if val
  1714. (add-hook 'org-blocker-hook
  1715. 'org-block-todo-from-checkboxes)
  1716. (remove-hook 'org-blocker-hook
  1717. 'org-block-todo-from-checkboxes)))
  1718. :group 'org-todo
  1719. :type 'boolean)
  1720. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1721. "Non-nil means inserting a TODO heading is treated as state change.
  1722. So when the command \\[org-insert-todo-heading] is used, state change
  1723. logging will apply if appropriate. When nil, the new TODO item will
  1724. be inserted directly, and no logging will take place."
  1725. :group 'org-todo
  1726. :type 'boolean)
  1727. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1728. "Non-nil means switching TODO states with S-cursor counts as state change.
  1729. This is the default behavior. However, setting this to nil allows a
  1730. convenient way to select a TODO state and bypass any logging associated
  1731. with that."
  1732. :group 'org-todo
  1733. :type 'boolean)
  1734. (defcustom org-todo-state-tags-triggers nil
  1735. "Tag changes that should be triggered by TODO state changes.
  1736. This is a list. Each entry is
  1737. (state-change (tag . flag) .......)
  1738. State-change can be a string with a state, and empty string to indicate the
  1739. state that has no TODO keyword, or it can be one of the symbols `todo'
  1740. or `done', meaning any not-done or done state, respectively."
  1741. :group 'org-todo
  1742. :group 'org-tags
  1743. :type '(repeat
  1744. (cons (choice :tag "When changing to"
  1745. (const :tag "Not-done state" todo)
  1746. (const :tag "Done state" done)
  1747. (string :tag "State"))
  1748. (repeat
  1749. (cons :tag "Tag action"
  1750. (string :tag "Tag")
  1751. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1752. (defcustom org-log-done nil
  1753. "Information to record when a task moves to the DONE state.
  1754. Possible values are:
  1755. nil Don't add anything, just change the keyword
  1756. time Add a time stamp to the task
  1757. note Prompt for a note and add it with template `org-log-note-headings'
  1758. This option can also be set with on a per-file-basis with
  1759. #+STARTUP: nologdone
  1760. #+STARTUP: logdone
  1761. #+STARTUP: lognotedone
  1762. You can have local logging settings for a subtree by setting the LOGGING
  1763. property to one or more of these keywords."
  1764. :group 'org-todo
  1765. :group 'org-progress
  1766. :type '(choice
  1767. (const :tag "No logging" nil)
  1768. (const :tag "Record CLOSED timestamp" time)
  1769. (const :tag "Record CLOSED timestamp with note." note)))
  1770. ;; Normalize old uses of org-log-done.
  1771. (cond
  1772. ((eq org-log-done t) (setq org-log-done 'time))
  1773. ((and (listp org-log-done) (memq 'done org-log-done))
  1774. (setq org-log-done 'note)))
  1775. (defcustom org-log-reschedule nil
  1776. "Information to record when the scheduling date of a tasks is modified.
  1777. Possible values are:
  1778. nil Don't add anything, just change the date
  1779. time Add a time stamp to the task
  1780. note Prompt for a note and add it with template `org-log-note-headings'
  1781. This option can also be set with on a per-file-basis with
  1782. #+STARTUP: nologreschedule
  1783. #+STARTUP: logreschedule
  1784. #+STARTUP: lognotereschedule"
  1785. :group 'org-todo
  1786. :group 'org-progress
  1787. :type '(choice
  1788. (const :tag "No logging" nil)
  1789. (const :tag "Record timestamp" time)
  1790. (const :tag "Record timestamp with note." note)))
  1791. (defcustom org-log-redeadline nil
  1792. "Information to record when the deadline date of a tasks is modified.
  1793. Possible values are:
  1794. nil Don't add anything, just change the date
  1795. time Add a time stamp to the task
  1796. note Prompt for a note and add it with template `org-log-note-headings'
  1797. This option can also be set with on a per-file-basis with
  1798. #+STARTUP: nologredeadline
  1799. #+STARTUP: logredeadline
  1800. #+STARTUP: lognoteredeadline
  1801. You can have local logging settings for a subtree by setting the LOGGING
  1802. property to one or more of these keywords."
  1803. :group 'org-todo
  1804. :group 'org-progress
  1805. :type '(choice
  1806. (const :tag "No logging" nil)
  1807. (const :tag "Record timestamp" time)
  1808. (const :tag "Record timestamp with note." note)))
  1809. (defcustom org-log-note-clock-out nil
  1810. "Non-nil means record a note when clocking out of an item.
  1811. This can also be configured on a per-file basis by adding one of
  1812. the following lines anywhere in the buffer:
  1813. #+STARTUP: lognoteclock-out
  1814. #+STARTUP: nolognoteclock-out"
  1815. :group 'org-todo
  1816. :group 'org-progress
  1817. :type 'boolean)
  1818. (defcustom org-log-done-with-time t
  1819. "Non-nil means the CLOSED time stamp will contain date and time.
  1820. When nil, only the date will be recorded."
  1821. :group 'org-progress
  1822. :type 'boolean)
  1823. (defcustom org-log-note-headings
  1824. '((done . "CLOSING NOTE %t")
  1825. (state . "State %-12s from %-12S %t")
  1826. (note . "Note taken on %t")
  1827. (reschedule . "Rescheduled from %S on %t")
  1828. (delschedule . "Not scheduled, was %S on %t")
  1829. (redeadline . "New deadline from %S on %t")
  1830. (deldeadline . "Removed deadline, was %S on %t")
  1831. (refile . "Refiled on %t")
  1832. (clock-out . ""))
  1833. "Headings for notes added to entries.
  1834. The value is an alist, with the car being a symbol indicating the note
  1835. context, and the cdr is the heading to be used. The heading may also be the
  1836. empty string.
  1837. %t in the heading will be replaced by a time stamp.
  1838. %s will be replaced by the new TODO state, in double quotes.
  1839. %S will be replaced by the old TODO state, in double quotes.
  1840. %u will be replaced by the user name.
  1841. %U will be replaced by the full user name.
  1842. In fact, it is not a good idea to change the `state' entry, because
  1843. agenda log mode depends on the format of these entries."
  1844. :group 'org-todo
  1845. :group 'org-progress
  1846. :type '(list :greedy t
  1847. (cons (const :tag "Heading when closing an item" done) string)
  1848. (cons (const :tag
  1849. "Heading when changing todo state (todo sequence only)"
  1850. state) string)
  1851. (cons (const :tag "Heading when just taking a note" note) string)
  1852. (cons (const :tag "Heading when clocking out" clock-out) string)
  1853. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1854. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1855. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1856. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1857. (cons (const :tag "Heading when refiling" refile) string)))
  1858. (unless (assq 'note org-log-note-headings)
  1859. (push '(note . "%t") org-log-note-headings))
  1860. (defcustom org-log-into-drawer nil
  1861. "Non-nil means insert state change notes and time stamps into a drawer.
  1862. When nil, state changes notes will be inserted after the headline and
  1863. any scheduling and clock lines, but not inside a drawer.
  1864. The value of this variable should be the name of the drawer to use.
  1865. LOGBOOK is proposed at the default drawer for this purpose, you can
  1866. also set this to a string to define the drawer of your choice.
  1867. A value of t is also allowed, representing \"LOGBOOK\".
  1868. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1869. will be ignored.
  1870. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1871. a subtree."
  1872. :group 'org-todo
  1873. :group 'org-progress
  1874. :type '(choice
  1875. (const :tag "Not into a drawer" nil)
  1876. (const :tag "LOGBOOK" t)
  1877. (string :tag "Other")))
  1878. (if (fboundp 'defvaralias)
  1879. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1880. (defun org-log-into-drawer ()
  1881. "Return the value of `org-log-into-drawer', but let properties overrule.
  1882. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1883. used instead of the default value."
  1884. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1885. (cond
  1886. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1887. ((equal p "t") "LOGBOOK")
  1888. (t p))))
  1889. (defcustom org-log-state-notes-insert-after-drawers nil
  1890. "Non-nil means insert state change notes after any drawers in entry.
  1891. Only the drawers that *immediately* follow the headline and the
  1892. deadline/scheduled line are skipped.
  1893. When nil, insert notes right after the heading and perhaps the line
  1894. with deadline/scheduling if present.
  1895. This variable will have no effect if `org-log-into-drawer' is
  1896. set."
  1897. :group 'org-todo
  1898. :group 'org-progress
  1899. :type 'boolean)
  1900. (defcustom org-log-states-order-reversed t
  1901. "Non-nil means the latest state note will be directly after heading.
  1902. When nil, the state change notes will be ordered according to time."
  1903. :group 'org-todo
  1904. :group 'org-progress
  1905. :type 'boolean)
  1906. (defcustom org-todo-repeat-to-state nil
  1907. "The TODO state to which a repeater should return the repeating task.
  1908. By default this is the first task in a TODO sequence, or the previous state
  1909. in a TODO_TYP set. But you can specify another task here.
  1910. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1911. :group 'org-todo
  1912. :type '(choice (const :tag "Head of sequence" nil)
  1913. (string :tag "Specific state")))
  1914. (defcustom org-log-repeat 'time
  1915. "Non-nil means record moving through the DONE state when triggering repeat.
  1916. An auto-repeating task is immediately switched back to TODO when
  1917. marked DONE. If you are not logging state changes (by adding \"@\"
  1918. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1919. record a closing note, there will be no record of the task moving
  1920. through DONE. This variable forces taking a note anyway.
  1921. nil Don't force a record
  1922. time Record a time stamp
  1923. note Record a note
  1924. This option can also be set with on a per-file-basis with
  1925. #+STARTUP: logrepeat
  1926. #+STARTUP: lognoterepeat
  1927. #+STARTUP: nologrepeat
  1928. You can have local logging settings for a subtree by setting the LOGGING
  1929. property to one or more of these keywords."
  1930. :group 'org-todo
  1931. :group 'org-progress
  1932. :type '(choice
  1933. (const :tag "Don't force a record" nil)
  1934. (const :tag "Force recording the DONE state" time)
  1935. (const :tag "Force recording a note with the DONE state" note)))
  1936. (defgroup org-priorities nil
  1937. "Priorities in Org-mode."
  1938. :tag "Org Priorities"
  1939. :group 'org-todo)
  1940. (defcustom org-enable-priority-commands t
  1941. "Non-nil means priority commands are active.
  1942. When nil, these commands will be disabled, so that you never accidentally
  1943. set a priority."
  1944. :group 'org-priorities
  1945. :type 'boolean)
  1946. (defcustom org-highest-priority ?A
  1947. "The highest priority of TODO items. A character like ?A, ?B etc.
  1948. Must have a smaller ASCII number than `org-lowest-priority'."
  1949. :group 'org-priorities
  1950. :type 'character)
  1951. (defcustom org-lowest-priority ?C
  1952. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1953. Must have a larger ASCII number than `org-highest-priority'."
  1954. :group 'org-priorities
  1955. :type 'character)
  1956. (defcustom org-default-priority ?B
  1957. "The default priority of TODO items.
  1958. This is the priority an item get if no explicit priority is given."
  1959. :group 'org-priorities
  1960. :type 'character)
  1961. (defcustom org-priority-start-cycle-with-default t
  1962. "Non-nil means start with default priority when starting to cycle.
  1963. When this is nil, the first step in the cycle will be (depending on the
  1964. command used) one higher or lower that the default priority."
  1965. :group 'org-priorities
  1966. :type 'boolean)
  1967. (defgroup org-time nil
  1968. "Options concerning time stamps and deadlines in Org-mode."
  1969. :tag "Org Time"
  1970. :group 'org)
  1971. (defcustom org-insert-labeled-timestamps-at-point nil
  1972. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1973. When nil, these labeled time stamps are forces into the second line of an
  1974. entry, just after the headline. When scheduling from the global TODO list,
  1975. the time stamp will always be forced into the second line."
  1976. :group 'org-time
  1977. :type 'boolean)
  1978. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1979. "Formats for `format-time-string' which are used for time stamps.
  1980. It is not recommended to change this constant.")
  1981. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1982. "Number of minutes to round time stamps to.
  1983. These are two values, the first applies when first creating a time stamp.
  1984. The second applies when changing it with the commands `S-up' and `S-down'.
  1985. When changing the time stamp, this means that it will change in steps
  1986. of N minutes, as given by the second value.
  1987. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1988. numbers should be factors of 60, so for example 5, 10, 15.
  1989. When this is larger than 1, you can still force an exact time-stamp by using
  1990. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1991. and by using a prefix arg to `S-up/down' to specify the exact number
  1992. of minutes to shift."
  1993. :group 'org-time
  1994. :get '(lambda (var) ; Make sure all entries have 5 elements
  1995. (if (integerp (default-value var))
  1996. (list (default-value var) 5)
  1997. (default-value var)))
  1998. :type '(list
  1999. (integer :tag "when inserting times")
  2000. (integer :tag "when modifying times")))
  2001. ;; Normalize old customizations of this variable.
  2002. (when (integerp org-time-stamp-rounding-minutes)
  2003. (setq org-time-stamp-rounding-minutes
  2004. (list org-time-stamp-rounding-minutes
  2005. org-time-stamp-rounding-minutes)))
  2006. (defcustom org-display-custom-times nil
  2007. "Non-nil means overlay custom formats over all time stamps.
  2008. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2009. To turn this on on a per-file basis, insert anywhere in the file:
  2010. #+STARTUP: customtime"
  2011. :group 'org-time
  2012. :set 'set-default
  2013. :type 'sexp)
  2014. (make-variable-buffer-local 'org-display-custom-times)
  2015. (defcustom org-time-stamp-custom-formats
  2016. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2017. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2018. These are overlayed over the default ISO format if the variable
  2019. `org-display-custom-times' is set. Time like %H:%M should be at the
  2020. end of the second format. The custom formats are also honored by export
  2021. commands, if custom time display is turned on at the time of export."
  2022. :group 'org-time
  2023. :type 'sexp)
  2024. (defun org-time-stamp-format (&optional long inactive)
  2025. "Get the right format for a time string."
  2026. (let ((f (if long (cdr org-time-stamp-formats)
  2027. (car org-time-stamp-formats))))
  2028. (if inactive
  2029. (concat "[" (substring f 1 -1) "]")
  2030. f)))
  2031. (defcustom org-time-clocksum-format "%d:%02d"
  2032. "The format string used when creating CLOCKSUM lines, or when
  2033. org-mode generates a time duration."
  2034. :group 'org-time
  2035. :type 'string)
  2036. (defcustom org-time-clocksum-use-fractional nil
  2037. "If non-nil, \\[org-clock-display] uses fractional times.
  2038. org-mode generates a time duration."
  2039. :group 'org-time
  2040. :type 'boolean)
  2041. (defcustom org-time-clocksum-fractional-format "%.2f"
  2042. "The format string used when creating CLOCKSUM lines, or when
  2043. org-mode generates a time duration."
  2044. :group 'org-time
  2045. :type 'string)
  2046. (defcustom org-deadline-warning-days 14
  2047. "No. of days before expiration during which a deadline becomes active.
  2048. This variable governs the display in sparse trees and in the agenda.
  2049. When 0 or negative, it means use this number (the absolute value of it)
  2050. even if a deadline has a different individual lead time specified.
  2051. Custom commands can set this variable in the options section."
  2052. :group 'org-time
  2053. :group 'org-agenda-daily/weekly
  2054. :type 'integer)
  2055. (defcustom org-read-date-prefer-future t
  2056. "Non-nil means assume future for incomplete date input from user.
  2057. This affects the following situations:
  2058. 1. The user gives a month but not a year.
  2059. For example, if it is april and you enter \"feb 2\", this will be read
  2060. as feb 2, *next* year. \"May 5\", however, will be this year.
  2061. 2. The user gives a day, but no month.
  2062. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2063. read this as the third of *next* month. However, if you enter \"17\",
  2064. it will be considered as *this* month.
  2065. If you set this variable to the symbol `time', then also the following
  2066. will work:
  2067. 3. If the user gives a time, but no day. If the time is before now,
  2068. to will be interpreted as tomorrow.
  2069. Currently none of this works for ISO week specifications.
  2070. When this option is nil, the current day, month and year will always be
  2071. used as defaults."
  2072. :group 'org-time
  2073. :type '(choice
  2074. (const :tag "Never" nil)
  2075. (const :tag "Check month and day" t)
  2076. (const :tag "Check month, day, and time" time)))
  2077. (defcustom org-read-date-display-live t
  2078. "Non-nil means display current interpretation of date prompt live.
  2079. This display will be in an overlay, in the minibuffer."
  2080. :group 'org-time
  2081. :type 'boolean)
  2082. (defcustom org-read-date-popup-calendar t
  2083. "Non-nil means pop up a calendar when prompting for a date.
  2084. In the calendar, the date can be selected with mouse-1. However, the
  2085. minibuffer will also be active, and you can simply enter the date as well.
  2086. When nil, only the minibuffer will be available."
  2087. :group 'org-time
  2088. :type 'boolean)
  2089. (if (fboundp 'defvaralias)
  2090. (defvaralias 'org-popup-calendar-for-date-prompt
  2091. 'org-read-date-popup-calendar))
  2092. (defcustom org-read-date-minibuffer-setup-hook nil
  2093. "Hook to be used to set up keys for the date/time interface.
  2094. Add key definitions to `minibuffer-local-map', which will be a temporary
  2095. copy."
  2096. :group 'org-time
  2097. :type 'hook)
  2098. (defcustom org-extend-today-until 0
  2099. "The hour when your day really ends. Must be an integer.
  2100. This has influence for the following applications:
  2101. - When switching the agenda to \"today\". It it is still earlier than
  2102. the time given here, the day recognized as TODAY is actually yesterday.
  2103. - When a date is read from the user and it is still before the time given
  2104. here, the current date and time will be assumed to be yesterday, 23:59.
  2105. Also, timestamps inserted in remember templates follow this rule.
  2106. IMPORTANT: This is a feature whose implementation is and likely will
  2107. remain incomplete. Really, it is only here because past midnight seems to
  2108. be the favorite working time of John Wiegley :-)"
  2109. :group 'org-time
  2110. :type 'integer)
  2111. (defcustom org-edit-timestamp-down-means-later nil
  2112. "Non-nil means S-down will increase the time in a time stamp.
  2113. When nil, S-up will increase."
  2114. :group 'org-time
  2115. :type 'boolean)
  2116. (defcustom org-calendar-follow-timestamp-change t
  2117. "Non-nil means make the calendar window follow timestamp changes.
  2118. When a timestamp is modified and the calendar window is visible, it will be
  2119. moved to the new date."
  2120. :group 'org-time
  2121. :type 'boolean)
  2122. (defgroup org-tags nil
  2123. "Options concerning tags in Org-mode."
  2124. :tag "Org Tags"
  2125. :group 'org)
  2126. (defcustom org-tag-alist nil
  2127. "List of tags allowed in Org-mode files.
  2128. When this list is nil, Org-mode will base TAG input on what is already in the
  2129. buffer.
  2130. The value of this variable is an alist, the car of each entry must be a
  2131. keyword as a string, the cdr may be a character that is used to select
  2132. that tag through the fast-tag-selection interface.
  2133. See the manual for details."
  2134. :group 'org-tags
  2135. :type '(repeat
  2136. (choice
  2137. (cons (string :tag "Tag name")
  2138. (character :tag "Access char"))
  2139. (list :tag "Start radio group"
  2140. (const :startgroup)
  2141. (option (string :tag "Group description")))
  2142. (list :tag "End radio group"
  2143. (const :endgroup)
  2144. (option (string :tag "Group description")))
  2145. (const :tag "New line" (:newline)))))
  2146. (defcustom org-tag-persistent-alist nil
  2147. "List of tags that will always appear in all Org-mode files.
  2148. This is in addition to any in buffer settings or customizations
  2149. of `org-tag-alist'.
  2150. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2151. The value of this variable is an alist, the car of each entry must be a
  2152. keyword as a string, the cdr may be a character that is used to select
  2153. that tag through the fast-tag-selection interface.
  2154. See the manual for details.
  2155. To disable these tags on a per-file basis, insert anywhere in the file:
  2156. #+STARTUP: noptag"
  2157. :group 'org-tags
  2158. :type '(repeat
  2159. (choice
  2160. (cons (string :tag "Tag name")
  2161. (character :tag "Access char"))
  2162. (const :tag "Start radio group" (:startgroup))
  2163. (const :tag "End radio group" (:endgroup))
  2164. (const :tag "New line" (:newline)))))
  2165. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2166. "If non-nil, always offer completion for all tags of all agenda files.
  2167. Instead of customizing this variable directly, you might want to
  2168. set it locally for remember buffers, because there no list of
  2169. tags in that file can be created dynamically (there are none).
  2170. (add-hook 'org-remember-mode-hook
  2171. (lambda ()
  2172. (set (make-local-variable
  2173. 'org-complete-tags-always-offer-all-agenda-tags)
  2174. t)))"
  2175. :group 'org-tags
  2176. :type 'boolean)
  2177. (defvar org-file-tags nil
  2178. "List of tags that can be inherited by all entries in the file.
  2179. The tags will be inherited if the variable `org-use-tag-inheritance'
  2180. says they should be.
  2181. This variable is populated from #+FILETAGS lines.")
  2182. (defcustom org-use-fast-tag-selection 'auto
  2183. "Non-nil means use fast tag selection scheme.
  2184. This is a special interface to select and deselect tags with single keys.
  2185. When nil, fast selection is never used.
  2186. When the symbol `auto', fast selection is used if and only if selection
  2187. characters for tags have been configured, either through the variable
  2188. `org-tag-alist' or through a #+TAGS line in the buffer.
  2189. When t, fast selection is always used and selection keys are assigned
  2190. automatically if necessary."
  2191. :group 'org-tags
  2192. :type '(choice
  2193. (const :tag "Always" t)
  2194. (const :tag "Never" nil)
  2195. (const :tag "When selection characters are configured" 'auto)))
  2196. (defcustom org-fast-tag-selection-single-key nil
  2197. "Non-nil means fast tag selection exits after first change.
  2198. When nil, you have to press RET to exit it.
  2199. During fast tag selection, you can toggle this flag with `C-c'.
  2200. This variable can also have the value `expert'. In this case, the window
  2201. displaying the tags menu is not even shown, until you press C-c again."
  2202. :group 'org-tags
  2203. :type '(choice
  2204. (const :tag "No" nil)
  2205. (const :tag "Yes" t)
  2206. (const :tag "Expert" expert)))
  2207. (defvar org-fast-tag-selection-include-todo nil
  2208. "Non-nil means fast tags selection interface will also offer TODO states.
  2209. This is an undocumented feature, you should not rely on it.")
  2210. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2211. "The column to which tags should be indented in a headline.
  2212. If this number is positive, it specifies the column. If it is negative,
  2213. it means that the tags should be flushright to that column. For example,
  2214. -80 works well for a normal 80 character screen."
  2215. :group 'org-tags
  2216. :type 'integer)
  2217. (defcustom org-auto-align-tags t
  2218. "Non-nil means realign tags after pro/demotion of TODO state change.
  2219. These operations change the length of a headline and therefore shift
  2220. the tags around. With this options turned on, after each such operation
  2221. the tags are again aligned to `org-tags-column'."
  2222. :group 'org-tags
  2223. :type 'boolean)
  2224. (defcustom org-use-tag-inheritance t
  2225. "Non-nil means tags in levels apply also for sublevels.
  2226. When nil, only the tags directly given in a specific line apply there.
  2227. This may also be a list of tags that should be inherited, or a regexp that
  2228. matches tags that should be inherited. Additional control is possible
  2229. with the variable `org-tags-exclude-from-inheritance' which gives an
  2230. explicit list of tags to be excluded from inheritance., even if the value of
  2231. `org-use-tag-inheritance' would select it for inheritance.
  2232. If this option is t, a match early-on in a tree can lead to a large
  2233. number of matches in the subtree when constructing the agenda or creating
  2234. a sparse tree. If you only want to see the first match in a tree during
  2235. a search, check out the variable `org-tags-match-list-sublevels'."
  2236. :group 'org-tags
  2237. :type '(choice
  2238. (const :tag "Not" nil)
  2239. (const :tag "Always" t)
  2240. (repeat :tag "Specific tags" (string :tag "Tag"))
  2241. (regexp :tag "Tags matched by regexp")))
  2242. (defcustom org-tags-exclude-from-inheritance nil
  2243. "List of tags that should never be inherited.
  2244. This is a way to exclude a few tags from inheritance. For way to do
  2245. the opposite, to actively allow inheritance for selected tags,
  2246. see the variable `org-use-tag-inheritance'."
  2247. :group 'org-tags
  2248. :type '(repeat (string :tag "Tag")))
  2249. (defun org-tag-inherit-p (tag)
  2250. "Check if TAG is one that should be inherited."
  2251. (cond
  2252. ((member tag org-tags-exclude-from-inheritance) nil)
  2253. ((eq org-use-tag-inheritance t) t)
  2254. ((not org-use-tag-inheritance) nil)
  2255. ((stringp org-use-tag-inheritance)
  2256. (string-match org-use-tag-inheritance tag))
  2257. ((listp org-use-tag-inheritance)
  2258. (member tag org-use-tag-inheritance))
  2259. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2260. (defcustom org-tags-match-list-sublevels t
  2261. "Non-nil means list also sublevels of headlines matching a search.
  2262. This variable applies to tags/property searches, and also to stuck
  2263. projects because this search is based on a tags match as well.
  2264. When set to the symbol `indented', sublevels are indented with
  2265. leading dots.
  2266. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2267. the sublevels of a headline matching a tag search often also match
  2268. the same search. Listing all of them can create very long lists.
  2269. Setting this variable to nil causes subtrees of a match to be skipped.
  2270. This variable is semi-obsolete and probably should always be true. It
  2271. is better to limit inheritance to certain tags using the variables
  2272. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2273. :group 'org-tags
  2274. :type '(choice
  2275. (const :tag "No, don't list them" nil)
  2276. (const :tag "Yes, do list them" t)
  2277. (const :tag "List them, indented with leading dots" indented)))
  2278. (defcustom org-tags-sort-function nil
  2279. "When set, tags are sorted using this function as a comparator"
  2280. :group 'org-tags
  2281. :type '(choice
  2282. (const :tag "No sorting" nil)
  2283. (const :tag "Alphabetical" string<)
  2284. (const :tag "Reverse alphabetical" string>)
  2285. (function :tag "Custom function" nil)))
  2286. (defvar org-tags-history nil
  2287. "History of minibuffer reads for tags.")
  2288. (defvar org-last-tags-completion-table nil
  2289. "The last used completion table for tags.")
  2290. (defvar org-after-tags-change-hook nil
  2291. "Hook that is run after the tags in a line have changed.")
  2292. (defgroup org-properties nil
  2293. "Options concerning properties in Org-mode."
  2294. :tag "Org Properties"
  2295. :group 'org)
  2296. (defcustom org-property-format "%-10s %s"
  2297. "How property key/value pairs should be formatted by `indent-line'.
  2298. When `indent-line' hits a property definition, it will format the line
  2299. according to this format, mainly to make sure that the values are
  2300. lined-up with respect to each other."
  2301. :group 'org-properties
  2302. :type 'string)
  2303. (defcustom org-use-property-inheritance nil
  2304. "Non-nil means properties apply also for sublevels.
  2305. This setting is chiefly used during property searches. Turning it on can
  2306. cause significant overhead when doing a search, which is why it is not
  2307. on by default.
  2308. When nil, only the properties directly given in the current entry count.
  2309. When t, every property is inherited. The value may also be a list of
  2310. properties that should have inheritance, or a regular expression matching
  2311. properties that should be inherited.
  2312. However, note that some special properties use inheritance under special
  2313. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2314. and the properties ending in \"_ALL\" when they are used as descriptor
  2315. for valid values of a property.
  2316. Note for programmers:
  2317. When querying an entry with `org-entry-get', you can control if inheritance
  2318. should be used. By default, `org-entry-get' looks only at the local
  2319. properties. You can request inheritance by setting the inherit argument
  2320. to t (to force inheritance) or to `selective' (to respect the setting
  2321. in this variable)."
  2322. :group 'org-properties
  2323. :type '(choice
  2324. (const :tag "Not" nil)
  2325. (const :tag "Always" t)
  2326. (repeat :tag "Specific properties" (string :tag "Property"))
  2327. (regexp :tag "Properties matched by regexp")))
  2328. (defun org-property-inherit-p (property)
  2329. "Check if PROPERTY is one that should be inherited."
  2330. (cond
  2331. ((eq org-use-property-inheritance t) t)
  2332. ((not org-use-property-inheritance) nil)
  2333. ((stringp org-use-property-inheritance)
  2334. (string-match org-use-property-inheritance property))
  2335. ((listp org-use-property-inheritance)
  2336. (member property org-use-property-inheritance))
  2337. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2338. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2339. "The default column format, if no other format has been defined.
  2340. This variable can be set on the per-file basis by inserting a line
  2341. #+COLUMNS: %25ITEM ....."
  2342. :group 'org-properties
  2343. :type 'string)
  2344. (defcustom org-columns-ellipses ".."
  2345. "The ellipses to be used when a field in column view is truncated.
  2346. When this is the empty string, as many characters as possible are shown,
  2347. but then there will be no visual indication that the field has been truncated.
  2348. When this is a string of length N, the last N characters of a truncated
  2349. field are replaced by this string. If the column is narrower than the
  2350. ellipses string, only part of the ellipses string will be shown."
  2351. :group 'org-properties
  2352. :type 'string)
  2353. (defcustom org-columns-modify-value-for-display-function nil
  2354. "Function that modifies values for display in column view.
  2355. For example, it can be used to cut out a certain part from a time stamp.
  2356. The function must take 2 arguments:
  2357. column-title The title of the column (*not* the property name)
  2358. value The value that should be modified.
  2359. The function should return the value that should be displayed,
  2360. or nil if the normal value should be used."
  2361. :group 'org-properties
  2362. :type 'function)
  2363. (defcustom org-effort-property "Effort"
  2364. "The property that is being used to keep track of effort estimates.
  2365. Effort estimates given in this property need to have the format H:MM."
  2366. :group 'org-properties
  2367. :group 'org-progress
  2368. :type '(string :tag "Property"))
  2369. (defconst org-global-properties-fixed
  2370. '(("VISIBILITY_ALL" . "folded children content all")
  2371. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2372. "List of property/value pairs that can be inherited by any entry.
  2373. These are fixed values, for the preset properties. The user variable
  2374. that can be used to add to this list is `org-global-properties'.
  2375. The entries in this list are cons cells where the car is a property
  2376. name and cdr is a string with the value. If the value represents
  2377. multiple items like an \"_ALL\" property, separate the items by
  2378. spaces.")
  2379. (defcustom org-global-properties nil
  2380. "List of property/value pairs that can be inherited by any entry.
  2381. This list will be combined with the constant `org-global-properties-fixed'.
  2382. The entries in this list are cons cells where the car is a property
  2383. name and cdr is a string with the value.
  2384. You can set buffer-local values for the same purpose in the variable
  2385. `org-file-properties' this by adding lines like
  2386. #+PROPERTY: NAME VALUE"
  2387. :group 'org-properties
  2388. :type '(repeat
  2389. (cons (string :tag "Property")
  2390. (string :tag "Value"))))
  2391. (defvar org-file-properties nil
  2392. "List of property/value pairs that can be inherited by any entry.
  2393. Valid for the current buffer.
  2394. This variable is populated from #+PROPERTY lines.")
  2395. (make-variable-buffer-local 'org-file-properties)
  2396. (defgroup org-agenda nil
  2397. "Options concerning agenda views in Org-mode."
  2398. :tag "Org Agenda"
  2399. :group 'org)
  2400. (defvar org-category nil
  2401. "Variable used by org files to set a category for agenda display.
  2402. Such files should use a file variable to set it, for example
  2403. # -*- mode: org; org-category: \"ELisp\"
  2404. or contain a special line
  2405. #+CATEGORY: ELisp
  2406. If the file does not specify a category, then file's base name
  2407. is used instead.")
  2408. (make-variable-buffer-local 'org-category)
  2409. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2410. (defcustom org-agenda-files nil
  2411. "The files to be used for agenda display.
  2412. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2413. \\[org-remove-file]. You can also use customize to edit the list.
  2414. If an entry is a directory, all files in that directory that are matched by
  2415. `org-agenda-file-regexp' will be part of the file list.
  2416. If the value of the variable is not a list but a single file name, then
  2417. the list of agenda files is actually stored and maintained in that file, one
  2418. agenda file per line. In this file paths can be given relative to
  2419. `org-directory'. Tilde expansion and environment variable substitution
  2420. are also made."
  2421. :group 'org-agenda
  2422. :type '(choice
  2423. (repeat :tag "List of files and directories" file)
  2424. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2425. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2426. "Regular expression to match files for `org-agenda-files'.
  2427. If any element in the list in that variable contains a directory instead
  2428. of a normal file, all files in that directory that are matched by this
  2429. regular expression will be included."
  2430. :group 'org-agenda
  2431. :type 'regexp)
  2432. (defcustom org-agenda-text-search-extra-files nil
  2433. "List of extra files to be searched by text search commands.
  2434. These files will be search in addition to the agenda files by the
  2435. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2436. Note that these files will only be searched for text search commands,
  2437. not for the other agenda views like todo lists, tag searches or the weekly
  2438. agenda. This variable is intended to list notes and possibly archive files
  2439. that should also be searched by these two commands.
  2440. In fact, if the first element in the list is the symbol `agenda-archives',
  2441. than all archive files of all agenda files will be added to the search
  2442. scope."
  2443. :group 'org-agenda
  2444. :type '(set :greedy t
  2445. (const :tag "Agenda Archives" agenda-archives)
  2446. (repeat :inline t (file))))
  2447. (if (fboundp 'defvaralias)
  2448. (defvaralias 'org-agenda-multi-occur-extra-files
  2449. 'org-agenda-text-search-extra-files))
  2450. (defcustom org-agenda-skip-unavailable-files nil
  2451. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2452. A nil value means to remove them, after a query, from the list."
  2453. :group 'org-agenda
  2454. :type 'boolean)
  2455. (defcustom org-calendar-to-agenda-key [?c]
  2456. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2457. The command `org-calendar-goto-agenda' will be bound to this key. The
  2458. default is the character `c' because then `c' can be used to switch back and
  2459. forth between agenda and calendar."
  2460. :group 'org-agenda
  2461. :type 'sexp)
  2462. (defcustom org-calendar-agenda-action-key [?k]
  2463. "The key to be installed in `calendar-mode-map' for agenda-action.
  2464. The command `org-agenda-action' will be bound to this key. The
  2465. default is the character `k' because we use the same key in the agenda."
  2466. :group 'org-agenda
  2467. :type 'sexp)
  2468. (defcustom org-calendar-insert-diary-entry-key [?i]
  2469. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2470. This option is irrelevant until `org-agenda-diary-file' has been configured
  2471. to point to an Org-mode file. When that is the case, the command
  2472. `org-agenda-diary-entry' will be bound to the key given here, by default
  2473. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2474. if you want to continue doing this, you need to change this to a different
  2475. key."
  2476. :group 'org-agenda
  2477. :type 'sexp)
  2478. (defcustom org-agenda-diary-file 'diary-file
  2479. "File to which to add new entries with the `i' key in agenda and calendar.
  2480. When this is the symbol `diary-file', the functionality in the Emacs
  2481. calendar will be used to add entries to the `diary-file'. But when this
  2482. points to a file, `org-agenda-diary-entry' will be used instead."
  2483. :group 'org-agenda
  2484. :type '(choice
  2485. (const :tag "The standard Emacs diary file" diary-file)
  2486. (file :tag "Special Org file diary entries")))
  2487. (eval-after-load "calendar"
  2488. '(progn
  2489. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2490. 'org-calendar-goto-agenda)
  2491. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2492. 'org-agenda-action)
  2493. (add-hook 'calendar-mode-hook
  2494. (lambda ()
  2495. (unless (eq org-agenda-diary-file 'diary-file)
  2496. (define-key calendar-mode-map
  2497. org-calendar-insert-diary-entry-key
  2498. 'org-agenda-diary-entry))))))
  2499. (defgroup org-latex nil
  2500. "Options for embedding LaTeX code into Org-mode."
  2501. :tag "Org LaTeX"
  2502. :group 'org)
  2503. (defcustom org-format-latex-options
  2504. '(:foreground default :background default :scale 1.0
  2505. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2506. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2507. "Options for creating images from LaTeX fragments.
  2508. This is a property list with the following properties:
  2509. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2510. `default' means use the foreground of the default face.
  2511. :background the background color, or \"Transparent\".
  2512. `default' means use the background of the default face.
  2513. :scale a scaling factor for the size of the images.
  2514. :html-foreground, :html-background, :html-scale
  2515. the same numbers for HTML export.
  2516. :matchers a list indicating which matchers should be used to
  2517. find LaTeX fragments. Valid members of this list are:
  2518. \"begin\" find environments
  2519. \"$1\" find single characters surrounded by $.$
  2520. \"$\" find math expressions surrounded by $...$
  2521. \"$$\" find math expressions surrounded by $$....$$
  2522. \"\\(\" find math expressions surrounded by \\(...\\)
  2523. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2524. :group 'org-latex
  2525. :type 'plist)
  2526. (defcustom org-format-latex-signal-error t
  2527. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2528. When nil, just push out a message."
  2529. :group 'org-latex
  2530. :type 'boolean)
  2531. (defcustom org-format-latex-header "\\documentclass{article}
  2532. \\usepackage[usenames]{color}
  2533. \\usepackage{amsmath}
  2534. \\usepackage[mathscr]{eucal}
  2535. \\pagestyle{empty} % do not remove
  2536. \[PACKAGES]
  2537. \[DEFAULT-PACKAGES]
  2538. % The settings below are copied from fullpage.sty
  2539. \\setlength{\\textwidth}{\\paperwidth}
  2540. \\addtolength{\\textwidth}{-3cm}
  2541. \\setlength{\\oddsidemargin}{1.5cm}
  2542. \\addtolength{\\oddsidemargin}{-2.54cm}
  2543. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2544. \\setlength{\\textheight}{\\paperheight}
  2545. \\addtolength{\\textheight}{-\\headheight}
  2546. \\addtolength{\\textheight}{-\\headsep}
  2547. \\addtolength{\\textheight}{-\\footskip}
  2548. \\addtolength{\\textheight}{-3cm}
  2549. \\setlength{\\topmargin}{1.5cm}
  2550. \\addtolength{\\topmargin}{-2.54cm}"
  2551. "The document header used for processing LaTeX fragments.
  2552. It is imperative that this header make sure that no page number
  2553. appears on the page. The package defined in the variables
  2554. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2555. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2556. will be appended."
  2557. :group 'org-latex
  2558. :type 'string)
  2559. (defvar org-format-latex-header-extra nil)
  2560. ;; The following variables are defined here because is it also used
  2561. ;; when formatting latex fragments. Originally it was part of the
  2562. ;; LaTeX exporter, which is why the name includes "export".
  2563. (defcustom org-export-latex-default-packages-alist
  2564. '(("AUTO" "inputenc")
  2565. ("T1" "fontenc")
  2566. ("" "fixltx2e")
  2567. ("" "graphicx")
  2568. ("" "longtable")
  2569. ("" "float")
  2570. ("" "wrapfig")
  2571. ("" "soul")
  2572. ("" "t1enc")
  2573. ("" "textcomp")
  2574. ("" "marvosym")
  2575. ("" "wasysym")
  2576. ("" "latexsym")
  2577. ("" "amssymb")
  2578. ("" "hyperref")
  2579. "\\tolerance=1000"
  2580. )
  2581. "Alist of default packages to be inserted in the header.
  2582. Change this only if one of the packages here causes an incompatibility
  2583. with another package you are using.
  2584. The packages in this list are needed by one part or another of Org-mode
  2585. to function properly.
  2586. - inputenc, fontenc, t1enc: for basic font and character selection
  2587. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2588. for interpreting the entities in `org-entities'. You can skip some of these
  2589. packages if you don't use any of the symbols in it.
  2590. - graphicx: for including images
  2591. - float, wrapfig: for figure placement
  2592. - longtable: for long tables
  2593. - hyperref: for cross references
  2594. Therefore you should not modify this variable unless you know what you
  2595. are doing. The one reason to change it anyway is that you might be loading
  2596. some other package that conflicts with one of the default packages.
  2597. Each cell is of the format \( \"options\" \"package\" \)."
  2598. :group 'org-export-latex
  2599. :type '(repeat
  2600. (choice
  2601. (string :tag "A line of LaTeX")
  2602. (list :tag "options/package pair"
  2603. (string :tag "options")
  2604. (string :tag "package")))))
  2605. (defcustom org-export-latex-packages-alist nil
  2606. "Alist of packages to be inserted in every LaTeX the header.
  2607. These will be inserted after `org-export-latex-default-packages-alist'.
  2608. Each cell is of the format \( \"options\" \"package\" \).
  2609. Make sure that you only lis packages here which:
  2610. - you want in every file
  2611. - do not conflict with the default packages in
  2612. `org-export-latex-default-packages-alist'
  2613. - do not conflict with the setup in `org-format-latex-header'."
  2614. :group 'org-export-latex
  2615. :type '(repeat
  2616. (choice
  2617. (string :tag "A line of LaTeX")
  2618. (list :tag "options/package pair"
  2619. (string :tag "options")
  2620. (string :tag "package")))))
  2621. (defgroup org-appearance nil
  2622. "Settings for Org-mode appearance."
  2623. :tag "Org Appearance"
  2624. :group 'org)
  2625. (defcustom org-level-color-stars-only nil
  2626. "Non-nil means fontify only the stars in each headline.
  2627. When nil, the entire headline is fontified.
  2628. Changing it requires restart of `font-lock-mode' to become effective
  2629. also in regions already fontified."
  2630. :group 'org-appearance
  2631. :type 'boolean)
  2632. (defcustom org-hide-leading-stars nil
  2633. "Non-nil means hide the first N-1 stars in a headline.
  2634. This works by using the face `org-hide' for these stars. This
  2635. face is white for a light background, and black for a dark
  2636. background. You may have to customize the face `org-hide' to
  2637. make this work.
  2638. Changing it requires restart of `font-lock-mode' to become effective
  2639. also in regions already fontified.
  2640. You may also set this on a per-file basis by adding one of the following
  2641. lines to the buffer:
  2642. #+STARTUP: hidestars
  2643. #+STARTUP: showstars"
  2644. :group 'org-appearance
  2645. :type 'boolean)
  2646. (defcustom org-hidden-keywords nil
  2647. "List of keywords that should be hidden when typed in the org buffer.
  2648. For example, add #+TITLE to this list in order to make the
  2649. document title appear in the buffer without the initial #+TITLE:
  2650. keyword."
  2651. :group 'org-appearance
  2652. :type '(set (const :tag "#+AUTHOR" author)
  2653. (const :tag "#+DATE" date)
  2654. (const :tag "#+EMAIL" email)
  2655. (const :tag "#+TITLE" title)))
  2656. (defcustom org-fontify-done-headline nil
  2657. "Non-nil means change the face of a headline if it is marked DONE.
  2658. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2659. When this is non-nil, the headline after the keyword is set to the
  2660. `org-headline-done' as an additional indication."
  2661. :group 'org-appearance
  2662. :type 'boolean)
  2663. (defcustom org-fontify-emphasized-text t
  2664. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2665. Changing this variable requires a restart of Emacs to take effect."
  2666. :group 'org-appearance
  2667. :type 'boolean)
  2668. (defcustom org-fontify-whole-heading-line nil
  2669. "Non-nil means fontify the whole line for headings.
  2670. This is useful when setting a background color for the
  2671. org-level-* faces."
  2672. :group 'org-appearance
  2673. :type 'boolean)
  2674. (defcustom org-highlight-latex-fragments-and-specials nil
  2675. "Non-nil means fontify what is treated specially by the exporters."
  2676. :group 'org-appearance
  2677. :type 'boolean)
  2678. (defcustom org-hide-emphasis-markers nil
  2679. "Non-nil mean font-lock should hide the emphasis marker characters."
  2680. :group 'org-appearance
  2681. :type 'boolean)
  2682. (defvar org-emph-re nil
  2683. "Regular expression for matching emphasis.")
  2684. (defvar org-verbatim-re nil
  2685. "Regular expression for matching verbatim text.")
  2686. (defvar org-emphasis-regexp-components) ; defined just below
  2687. (defvar org-emphasis-alist) ; defined just below
  2688. (defun org-set-emph-re (var val)
  2689. "Set variable and compute the emphasis regular expression."
  2690. (set var val)
  2691. (when (and (boundp 'org-emphasis-alist)
  2692. (boundp 'org-emphasis-regexp-components)
  2693. org-emphasis-alist org-emphasis-regexp-components)
  2694. (let* ((e org-emphasis-regexp-components)
  2695. (pre (car e))
  2696. (post (nth 1 e))
  2697. (border (nth 2 e))
  2698. (body (nth 3 e))
  2699. (nl (nth 4 e))
  2700. (body1 (concat body "*?"))
  2701. (markers (mapconcat 'car org-emphasis-alist ""))
  2702. (vmarkers (mapconcat
  2703. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2704. org-emphasis-alist "")))
  2705. ;; make sure special characters appear at the right position in the class
  2706. (if (string-match "\\^" markers)
  2707. (setq markers (concat (replace-match "" t t markers) "^")))
  2708. (if (string-match "-" markers)
  2709. (setq markers (concat (replace-match "" t t markers) "-")))
  2710. (if (string-match "\\^" vmarkers)
  2711. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2712. (if (string-match "-" vmarkers)
  2713. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2714. (if (> nl 0)
  2715. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2716. (int-to-string nl) "\\}")))
  2717. ;; Make the regexp
  2718. (setq org-emph-re
  2719. (concat "\\([" pre "]\\|^\\)"
  2720. "\\("
  2721. "\\([" markers "]\\)"
  2722. "\\("
  2723. "[^" border "]\\|"
  2724. "[^" border "]"
  2725. body1
  2726. "[^" border "]"
  2727. "\\)"
  2728. "\\3\\)"
  2729. "\\([" post "]\\|$\\)"))
  2730. (setq org-verbatim-re
  2731. (concat "\\([" pre "]\\|^\\)"
  2732. "\\("
  2733. "\\([" vmarkers "]\\)"
  2734. "\\("
  2735. "[^" border "]\\|"
  2736. "[^" border "]"
  2737. body1
  2738. "[^" border "]"
  2739. "\\)"
  2740. "\\3\\)"
  2741. "\\([" post "]\\|$\\)")))))
  2742. (defcustom org-emphasis-regexp-components
  2743. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2744. "Components used to build the regular expression for emphasis.
  2745. This is a list with 6 entries. Terminology: In an emphasis string
  2746. like \" *strong word* \", we call the initial space PREMATCH, the final
  2747. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2748. and \"trong wor\" is the body. The different components in this variable
  2749. specify what is allowed/forbidden in each part:
  2750. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2751. post Chars allowed as postmatch. End of line will be allowed too.
  2752. border The chars *forbidden* as border characters.
  2753. body-regexp A regexp like \".\" to match a body character. Don't use
  2754. non-shy groups here, and don't allow newline here.
  2755. newline The maximum number of newlines allowed in an emphasis exp.
  2756. Use customize to modify this, or restart Emacs after changing it."
  2757. :group 'org-appearance
  2758. :set 'org-set-emph-re
  2759. :type '(list
  2760. (sexp :tag "Allowed chars in pre ")
  2761. (sexp :tag "Allowed chars in post ")
  2762. (sexp :tag "Forbidden chars in border ")
  2763. (sexp :tag "Regexp for body ")
  2764. (integer :tag "number of newlines allowed")
  2765. (option (boolean :tag "Please ignore this button"))))
  2766. (defcustom org-emphasis-alist
  2767. `(("*" bold "<b>" "</b>")
  2768. ("/" italic "<i>" "</i>")
  2769. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2770. ("=" org-code "<code>" "</code>" verbatim)
  2771. ("~" org-verbatim "<code>" "</code>" verbatim)
  2772. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2773. "<del>" "</del>")
  2774. )
  2775. "Special syntax for emphasized text.
  2776. Text starting and ending with a special character will be emphasized, for
  2777. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2778. characters, the face to be used by font-lock for highlighting in Org-mode
  2779. Emacs buffers, and the HTML tags to be used for this.
  2780. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2781. Use customize to modify this, or restart Emacs after changing it."
  2782. :group 'org-appearance
  2783. :set 'org-set-emph-re
  2784. :type '(repeat
  2785. (list
  2786. (string :tag "Marker character")
  2787. (choice
  2788. (face :tag "Font-lock-face")
  2789. (plist :tag "Face property list"))
  2790. (string :tag "HTML start tag")
  2791. (string :tag "HTML end tag")
  2792. (option (const verbatim)))))
  2793. (defvar org-protecting-blocks
  2794. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2795. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2796. This is needed for font-lock setup.")
  2797. ;;; Miscellaneous options
  2798. (defgroup org-completion nil
  2799. "Completion in Org-mode."
  2800. :tag "Org Completion"
  2801. :group 'org)
  2802. (defcustom org-completion-use-ido nil
  2803. "Non-nil means use ido completion wherever possible.
  2804. Note that `ido-mode' must be active for this variable to be relevant.
  2805. If you decide to turn this variable on, you might well want to turn off
  2806. `org-outline-path-complete-in-steps'.
  2807. See also `org-completion-use-iswitchb'."
  2808. :group 'org-completion
  2809. :type 'boolean)
  2810. (defcustom org-completion-use-iswitchb nil
  2811. "Non-nil means use iswitchb completion wherever possible.
  2812. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2813. If you decide to turn this variable on, you might well want to turn off
  2814. `org-outline-path-complete-in-steps'.
  2815. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2816. :group 'org-completion
  2817. :type 'boolean)
  2818. (defcustom org-completion-fallback-command 'hippie-expand
  2819. "The expansion command called by \\[org-complete] in normal context.
  2820. Normal means no org-mode-specific context."
  2821. :group 'org-completion
  2822. :type 'function)
  2823. ;;; Functions and variables from their packages
  2824. ;; Declared here to avoid compiler warnings
  2825. ;; XEmacs only
  2826. (defvar outline-mode-menu-heading)
  2827. (defvar outline-mode-menu-show)
  2828. (defvar outline-mode-menu-hide)
  2829. (defvar zmacs-regions) ; XEmacs regions
  2830. ;; Emacs only
  2831. (defvar mark-active)
  2832. ;; Various packages
  2833. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2834. (declare-function calendar-forward-day "cal-move" (arg))
  2835. (declare-function calendar-goto-date "cal-move" (date))
  2836. (declare-function calendar-goto-today "cal-move" ())
  2837. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2838. (defvar calc-embedded-close-formula)
  2839. (defvar calc-embedded-open-formula)
  2840. (declare-function cdlatex-tab "ext:cdlatex" ())
  2841. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2842. (defvar font-lock-unfontify-region-function)
  2843. (declare-function iswitchb-read-buffer "iswitchb"
  2844. (prompt &optional default require-match start matches-set))
  2845. (defvar iswitchb-temp-buflist)
  2846. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2847. (defvar org-agenda-tags-todo-honor-ignore-options)
  2848. (declare-function org-agenda-skip "org-agenda" ())
  2849. (declare-function
  2850. org-format-agenda-item "org-agenda"
  2851. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2852. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2853. (declare-function org-agenda-change-all-lines "org-agenda"
  2854. (newhead hdmarker &optional fixface just-this))
  2855. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2856. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2857. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2858. (beg end))
  2859. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2860. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2861. "org-agenda" (&optional end))
  2862. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2863. (declare-function org-indent-mode "org-indent" (&optional arg))
  2864. (declare-function parse-time-string "parse-time" (string))
  2865. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2866. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  2867. (defvar remember-data-file)
  2868. (defvar texmathp-why)
  2869. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2870. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2871. (defvar w3m-current-url)
  2872. (defvar w3m-current-title)
  2873. (defvar org-latex-regexps)
  2874. ;;; Autoload and prepare some org modules
  2875. ;; Some table stuff that needs to be defined here, because it is used
  2876. ;; by the functions setting up org-mode or checking for table context.
  2877. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2878. "Detects an org-type or table-type table.")
  2879. (defconst org-table-line-regexp "^[ \t]*|"
  2880. "Detects an org-type table line.")
  2881. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2882. "Detects an org-type table line.")
  2883. (defconst org-table-hline-regexp "^[ \t]*|-"
  2884. "Detects an org-type table hline.")
  2885. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2886. "Detects a table-type table hline.")
  2887. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2888. "Searching from within a table (any type) this finds the first line
  2889. outside the table.")
  2890. ;; Autoload the functions in org-table.el that are needed by functions here.
  2891. (eval-and-compile
  2892. (org-autoload "org-table"
  2893. '(org-table-align org-table-begin org-table-blank-field
  2894. org-table-convert org-table-convert-region org-table-copy-down
  2895. org-table-copy-region org-table-create
  2896. org-table-create-or-convert-from-region
  2897. org-table-create-with-table.el org-table-current-dline
  2898. org-table-cut-region org-table-delete-column org-table-edit-field
  2899. org-table-edit-formulas org-table-end org-table-eval-formula
  2900. org-table-export org-table-field-info
  2901. org-table-get-stored-formulas org-table-goto-column
  2902. org-table-hline-and-move org-table-import org-table-insert-column
  2903. org-table-insert-hline org-table-insert-row org-table-iterate
  2904. org-table-justify-field-maybe org-table-kill-row
  2905. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2906. org-table-move-column org-table-move-column-left
  2907. org-table-move-column-right org-table-move-row
  2908. org-table-move-row-down org-table-move-row-up
  2909. org-table-next-field org-table-next-row org-table-paste-rectangle
  2910. org-table-previous-field org-table-recalculate
  2911. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2912. org-table-toggle-coordinate-overlays
  2913. org-table-toggle-formula-debugger org-table-wrap-region
  2914. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2915. (defun org-at-table-p (&optional table-type)
  2916. "Return t if the cursor is inside an org-type table.
  2917. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2918. (if org-enable-table-editor
  2919. (save-excursion
  2920. (beginning-of-line 1)
  2921. (looking-at (if table-type org-table-any-line-regexp
  2922. org-table-line-regexp)))
  2923. nil))
  2924. (defsubst org-table-p () (org-at-table-p))
  2925. (defun org-at-table.el-p ()
  2926. "Return t if and only if we are at a table.el table."
  2927. (and (org-at-table-p 'any)
  2928. (save-excursion
  2929. (goto-char (org-table-begin 'any))
  2930. (looking-at org-table1-hline-regexp))))
  2931. (defun org-table-recognize-table.el ()
  2932. "If there is a table.el table nearby, recognize it and move into it."
  2933. (if org-table-tab-recognizes-table.el
  2934. (if (org-at-table.el-p)
  2935. (progn
  2936. (beginning-of-line 1)
  2937. (if (looking-at org-table-dataline-regexp)
  2938. nil
  2939. (if (looking-at org-table1-hline-regexp)
  2940. (progn
  2941. (beginning-of-line 2)
  2942. (if (looking-at org-table-any-border-regexp)
  2943. (beginning-of-line -1)))))
  2944. (if (re-search-forward "|" (org-table-end t) t)
  2945. (progn
  2946. (require 'table)
  2947. (if (table--at-cell-p (point))
  2948. t
  2949. (message "recognizing table.el table...")
  2950. (table-recognize-table)
  2951. (message "recognizing table.el table...done")))
  2952. (error "This should not happen..."))
  2953. t)
  2954. nil)
  2955. nil))
  2956. (defun org-at-table-hline-p ()
  2957. "Return t if the cursor is inside a hline in a table."
  2958. (if org-enable-table-editor
  2959. (save-excursion
  2960. (beginning-of-line 1)
  2961. (looking-at org-table-hline-regexp))
  2962. nil))
  2963. (defvar org-table-clean-did-remove-column nil)
  2964. (defun org-table-map-tables (function)
  2965. "Apply FUNCTION to the start of all tables in the buffer."
  2966. (save-excursion
  2967. (save-restriction
  2968. (widen)
  2969. (goto-char (point-min))
  2970. (while (re-search-forward org-table-any-line-regexp nil t)
  2971. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2972. (beginning-of-line 1)
  2973. (when (looking-at org-table-line-regexp)
  2974. (save-excursion (funcall function))
  2975. (or (looking-at org-table-line-regexp)
  2976. (forward-char 1)))
  2977. (re-search-forward org-table-any-border-regexp nil 1))))
  2978. (message "Mapping tables: done"))
  2979. ;; Declare and autoload functions from org-exp.el & Co
  2980. (declare-function org-default-export-plist "org-exp")
  2981. (declare-function org-infile-export-plist "org-exp")
  2982. (declare-function org-get-current-options "org-exp")
  2983. (eval-and-compile
  2984. (org-autoload "org-exp"
  2985. '(org-export org-export-visible
  2986. org-insert-export-options-template
  2987. org-table-clean-before-export))
  2988. (org-autoload "org-ascii"
  2989. '(org-export-as-ascii org-export-ascii-preprocess
  2990. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2991. org-export-region-as-ascii))
  2992. (org-autoload "org-latex"
  2993. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2994. org-replace-region-by-latex org-export-region-as-latex
  2995. org-export-as-latex org-export-as-pdf
  2996. org-export-as-pdf-and-open))
  2997. (org-autoload "org-html"
  2998. '(org-export-as-html-and-open
  2999. org-export-as-html-batch org-export-as-html-to-buffer
  3000. org-replace-region-by-html org-export-region-as-html
  3001. org-export-as-html))
  3002. (org-autoload "org-docbook"
  3003. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3004. org-replace-region-by-docbook org-export-region-as-docbook
  3005. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3006. org-export-as-docbook))
  3007. (org-autoload "org-icalendar"
  3008. '(org-export-icalendar-this-file
  3009. org-export-icalendar-all-agenda-files
  3010. org-export-icalendar-combine-agenda-files))
  3011. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3012. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3013. ;; Declare and autoload functions from org-agenda.el
  3014. (eval-and-compile
  3015. (org-autoload "org-agenda"
  3016. '(org-agenda org-agenda-list org-search-view
  3017. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3018. org-diary org-agenda-to-appt
  3019. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3020. ;; Autoload org-remember
  3021. (eval-and-compile
  3022. (org-autoload "org-remember"
  3023. '(org-remember-insinuate org-remember-annotation
  3024. org-remember-apply-template org-remember org-remember-handler)))
  3025. ;; Autoload org-clock.el
  3026. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3027. (beg end))
  3028. (declare-function org-clock-update-mode-line "org-clock" ())
  3029. (declare-function org-resolve-clocks "org-clock"
  3030. (&optional also-non-dangling-p prompt last-valid))
  3031. (defvar org-clock-start-time)
  3032. (defvar org-clock-marker (make-marker)
  3033. "Marker recording the last clock-in.")
  3034. (defvar org-clock-hd-marker (make-marker)
  3035. "Marker recording the last clock-in, but the headline position.")
  3036. (defvar org-clock-heading ""
  3037. "The heading of the current clock entry.")
  3038. (defun org-clock-is-active ()
  3039. "Return non-nil if clock is currently running.
  3040. The return value is actually the clock marker."
  3041. (marker-buffer org-clock-marker))
  3042. (eval-and-compile
  3043. (org-autoload
  3044. "org-clock"
  3045. '(org-clock-in org-clock-out org-clock-cancel
  3046. org-clock-goto org-clock-sum org-clock-display
  3047. org-clock-remove-overlays org-clock-report
  3048. org-clocktable-shift org-dblock-write:clocktable
  3049. org-get-clocktable org-resolve-clocks)))
  3050. (defun org-clock-update-time-maybe ()
  3051. "If this is a CLOCK line, update it and return t.
  3052. Otherwise, return nil."
  3053. (interactive)
  3054. (save-excursion
  3055. (beginning-of-line 1)
  3056. (skip-chars-forward " \t")
  3057. (when (looking-at org-clock-string)
  3058. (let ((re (concat "[ \t]*" org-clock-string
  3059. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3060. "\\([ \t]*=>.*\\)?\\)?"))
  3061. ts te h m s neg)
  3062. (cond
  3063. ((not (looking-at re))
  3064. nil)
  3065. ((not (match-end 2))
  3066. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3067. (> org-clock-marker (point))
  3068. (<= org-clock-marker (point-at-eol)))
  3069. ;; The clock is running here
  3070. (setq org-clock-start-time
  3071. (apply 'encode-time
  3072. (org-parse-time-string (match-string 1))))
  3073. (org-clock-update-mode-line)))
  3074. (t
  3075. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3076. (end-of-line 1)
  3077. (setq ts (match-string 1)
  3078. te (match-string 3))
  3079. (setq s (- (org-float-time
  3080. (apply 'encode-time (org-parse-time-string te)))
  3081. (org-float-time
  3082. (apply 'encode-time (org-parse-time-string ts))))
  3083. neg (< s 0)
  3084. s (abs s)
  3085. h (floor (/ s 3600))
  3086. s (- s (* 3600 h))
  3087. m (floor (/ s 60))
  3088. s (- s (* 60 s)))
  3089. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3090. t))))))
  3091. (defun org-check-running-clock ()
  3092. "Check if the current buffer contains the running clock.
  3093. If yes, offer to stop it and to save the buffer with the changes."
  3094. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3095. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3096. (buffer-name))))
  3097. (org-clock-out)
  3098. (when (y-or-n-p "Save changed buffer?")
  3099. (save-buffer))))
  3100. (defun org-clocktable-try-shift (dir n)
  3101. "Check if this line starts a clock table, if yes, shift the time block."
  3102. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3103. (org-clocktable-shift dir n)))
  3104. ;; Autoload org-timer.el
  3105. (eval-and-compile
  3106. (org-autoload
  3107. "org-timer"
  3108. '(org-timer-start org-timer org-timer-item
  3109. org-timer-change-times-in-region
  3110. org-timer-set-timer
  3111. org-timer-reset-timers
  3112. org-timer-show-remaining-time)))
  3113. ;; Autoload org-feed.el
  3114. (eval-and-compile
  3115. (org-autoload
  3116. "org-feed"
  3117. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3118. ;; Autoload org-indent.el
  3119. ;; Define the variable already here, to make sure we have it.
  3120. (defvar org-indent-mode nil
  3121. "Non-nil if Org-Indent mode is enabled.
  3122. Use the command `org-indent-mode' to change this variable.")
  3123. (eval-and-compile
  3124. (org-autoload
  3125. "org-indent"
  3126. '(org-indent-mode)))
  3127. ;; Autoload org-mobile.el
  3128. (eval-and-compile
  3129. (org-autoload
  3130. "org-mobile"
  3131. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3132. ;; Autoload archiving code
  3133. ;; The stuff that is needed for cycling and tags has to be defined here.
  3134. (defgroup org-archive nil
  3135. "Options concerning archiving in Org-mode."
  3136. :tag "Org Archive"
  3137. :group 'org-structure)
  3138. (defcustom org-archive-location "%s_archive::"
  3139. "The location where subtrees should be archived.
  3140. The value of this variable is a string, consisting of two parts,
  3141. separated by a double-colon. The first part is a filename and
  3142. the second part is a headline.
  3143. When the filename is omitted, archiving happens in the same file.
  3144. %s in the filename will be replaced by the current file
  3145. name (without the directory part). Archiving to a different file
  3146. is useful to keep archived entries from contributing to the
  3147. Org-mode Agenda.
  3148. The archived entries will be filed as subtrees of the specified
  3149. headline. When the headline is omitted, the subtrees are simply
  3150. filed away at the end of the file, as top-level entries. Also in
  3151. the heading you can use %s to represent the file name, this can be
  3152. useful when using the same archive for a number of different files.
  3153. Here are a few examples:
  3154. \"%s_archive::\"
  3155. If the current file is Projects.org, archive in file
  3156. Projects.org_archive, as top-level trees. This is the default.
  3157. \"::* Archived Tasks\"
  3158. Archive in the current file, under the top-level headline
  3159. \"* Archived Tasks\".
  3160. \"~/org/archive.org::\"
  3161. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3162. \"~/org/archive.org::From %s\"
  3163. Archive in file ~/org/archive.org (absolute path), under headlines
  3164. \"From FILENAME\" where file name is the current file name.
  3165. \"basement::** Finished Tasks\"
  3166. Archive in file ./basement (relative path), as level 3 trees
  3167. below the level 2 heading \"** Finished Tasks\".
  3168. You may set this option on a per-file basis by adding to the buffer a
  3169. line like
  3170. #+ARCHIVE: basement::** Finished Tasks
  3171. You may also define it locally for a subtree by setting an ARCHIVE property
  3172. in the entry. If such a property is found in an entry, or anywhere up
  3173. the hierarchy, it will be used."
  3174. :group 'org-archive
  3175. :type 'string)
  3176. (defcustom org-archive-tag "ARCHIVE"
  3177. "The tag that marks a subtree as archived.
  3178. An archived subtree does not open during visibility cycling, and does
  3179. not contribute to the agenda listings.
  3180. After changing this, font-lock must be restarted in the relevant buffers to
  3181. get the proper fontification."
  3182. :group 'org-archive
  3183. :group 'org-keywords
  3184. :type 'string)
  3185. (defcustom org-agenda-skip-archived-trees t
  3186. "Non-nil means the agenda will skip any items located in archived trees.
  3187. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3188. variable is no longer recommended, you should leave it at the value t.
  3189. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3190. :group 'org-archive
  3191. :group 'org-agenda-skip
  3192. :type 'boolean)
  3193. (defcustom org-columns-skip-archived-trees t
  3194. "Non-nil means ignore archived trees when creating column view."
  3195. :group 'org-archive
  3196. :group 'org-properties
  3197. :type 'boolean)
  3198. (defcustom org-cycle-open-archived-trees nil
  3199. "Non-nil means `org-cycle' will open archived trees.
  3200. An archived tree is a tree marked with the tag ARCHIVE.
  3201. When nil, archived trees will stay folded. You can still open them with
  3202. normal outline commands like `show-all', but not with the cycling commands."
  3203. :group 'org-archive
  3204. :group 'org-cycle
  3205. :type 'boolean)
  3206. (defcustom org-sparse-tree-open-archived-trees nil
  3207. "Non-nil means sparse tree construction shows matches in archived trees.
  3208. When nil, matches in these trees are highlighted, but the trees are kept in
  3209. collapsed state."
  3210. :group 'org-archive
  3211. :group 'org-sparse-trees
  3212. :type 'boolean)
  3213. (defun org-cycle-hide-archived-subtrees (state)
  3214. "Re-hide all archived subtrees after a visibility state change."
  3215. (when (and (not org-cycle-open-archived-trees)
  3216. (not (memq state '(overview folded))))
  3217. (save-excursion
  3218. (let* ((globalp (memq state '(contents all)))
  3219. (beg (if globalp (point-min) (point)))
  3220. (end (if globalp (point-max) (org-end-of-subtree t))))
  3221. (org-hide-archived-subtrees beg end)
  3222. (goto-char beg)
  3223. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3224. (message "%s" (substitute-command-keys
  3225. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3226. (defun org-force-cycle-archived ()
  3227. "Cycle subtree even if it is archived."
  3228. (interactive)
  3229. (setq this-command 'org-cycle)
  3230. (let ((org-cycle-open-archived-trees t))
  3231. (call-interactively 'org-cycle)))
  3232. (defun org-hide-archived-subtrees (beg end)
  3233. "Re-hide all archived subtrees after a visibility state change."
  3234. (save-excursion
  3235. (let* ((re (concat ":" org-archive-tag ":")))
  3236. (goto-char beg)
  3237. (while (re-search-forward re end t)
  3238. (when (org-on-heading-p)
  3239. (org-flag-subtree t)
  3240. (org-end-of-subtree t))))))
  3241. (defun org-flag-subtree (flag)
  3242. (save-excursion
  3243. (org-back-to-heading t)
  3244. (outline-end-of-heading)
  3245. (outline-flag-region (point)
  3246. (progn (org-end-of-subtree t) (point))
  3247. flag)))
  3248. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3249. (eval-and-compile
  3250. (org-autoload "org-archive"
  3251. '(org-add-archive-files org-archive-subtree
  3252. org-archive-to-archive-sibling org-toggle-archive-tag
  3253. org-archive-subtree-default
  3254. org-archive-subtree-default-with-confirmation)))
  3255. ;; Autoload Column View Code
  3256. (declare-function org-columns-number-to-string "org-colview")
  3257. (declare-function org-columns-get-format-and-top-level "org-colview")
  3258. (declare-function org-columns-compute "org-colview")
  3259. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3260. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3261. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3262. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3263. ;; Autoload ID code
  3264. (declare-function org-id-store-link "org-id")
  3265. (declare-function org-id-locations-load "org-id")
  3266. (declare-function org-id-locations-save "org-id")
  3267. (defvar org-id-track-globally)
  3268. (org-autoload "org-id"
  3269. '(org-id-get-create org-id-new org-id-copy org-id-get
  3270. org-id-get-with-outline-path-completion
  3271. org-id-get-with-outline-drilling
  3272. org-id-goto org-id-find org-id-store-link))
  3273. ;; Autoload Plotting Code
  3274. (org-autoload "org-plot"
  3275. '(org-plot/gnuplot))
  3276. ;;; Variables for pre-computed regular expressions, all buffer local
  3277. (defvar org-drawer-regexp nil
  3278. "Matches first line of a hidden block.")
  3279. (make-variable-buffer-local 'org-drawer-regexp)
  3280. (defvar org-todo-regexp nil
  3281. "Matches any of the TODO state keywords.")
  3282. (make-variable-buffer-local 'org-todo-regexp)
  3283. (defvar org-not-done-regexp nil
  3284. "Matches any of the TODO state keywords except the last one.")
  3285. (make-variable-buffer-local 'org-not-done-regexp)
  3286. (defvar org-not-done-heading-regexp nil
  3287. "Matches a TODO headline that is not done.")
  3288. (make-variable-buffer-local 'org-not-done-regexp)
  3289. (defvar org-todo-line-regexp nil
  3290. "Matches a headline and puts TODO state into group 2 if present.")
  3291. (make-variable-buffer-local 'org-todo-line-regexp)
  3292. (defvar org-complex-heading-regexp nil
  3293. "Matches a headline and puts everything into groups:
  3294. group 1: the stars
  3295. group 2: The todo keyword, maybe
  3296. group 3: Priority cookie
  3297. group 4: True headline
  3298. group 5: Tags")
  3299. (make-variable-buffer-local 'org-complex-heading-regexp)
  3300. (defvar org-complex-heading-regexp-format nil)
  3301. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3302. (defvar org-todo-line-tags-regexp nil
  3303. "Matches a headline and puts TODO state into group 2 if present.
  3304. Also put tags into group 4 if tags are present.")
  3305. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3306. (defvar org-nl-done-regexp nil
  3307. "Matches newline followed by a headline with the DONE keyword.")
  3308. (make-variable-buffer-local 'org-nl-done-regexp)
  3309. (defvar org-looking-at-done-regexp nil
  3310. "Matches the DONE keyword a point.")
  3311. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3312. (defvar org-ds-keyword-length 12
  3313. "Maximum length of the Deadline and SCHEDULED keywords.")
  3314. (make-variable-buffer-local 'org-ds-keyword-length)
  3315. (defvar org-deadline-regexp nil
  3316. "Matches the DEADLINE keyword.")
  3317. (make-variable-buffer-local 'org-deadline-regexp)
  3318. (defvar org-deadline-time-regexp nil
  3319. "Matches the DEADLINE keyword together with a time stamp.")
  3320. (make-variable-buffer-local 'org-deadline-time-regexp)
  3321. (defvar org-deadline-line-regexp nil
  3322. "Matches the DEADLINE keyword and the rest of the line.")
  3323. (make-variable-buffer-local 'org-deadline-line-regexp)
  3324. (defvar org-scheduled-regexp nil
  3325. "Matches the SCHEDULED keyword.")
  3326. (make-variable-buffer-local 'org-scheduled-regexp)
  3327. (defvar org-scheduled-time-regexp nil
  3328. "Matches the SCHEDULED keyword together with a time stamp.")
  3329. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3330. (defvar org-closed-time-regexp nil
  3331. "Matches the CLOSED keyword together with a time stamp.")
  3332. (make-variable-buffer-local 'org-closed-time-regexp)
  3333. (defvar org-keyword-time-regexp nil
  3334. "Matches any of the 4 keywords, together with the time stamp.")
  3335. (make-variable-buffer-local 'org-keyword-time-regexp)
  3336. (defvar org-keyword-time-not-clock-regexp nil
  3337. "Matches any of the 3 keywords, together with the time stamp.")
  3338. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3339. (defvar org-maybe-keyword-time-regexp nil
  3340. "Matches a timestamp, possibly preceeded by a keyword.")
  3341. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3342. (defvar org-planning-or-clock-line-re nil
  3343. "Matches a line with planning or clock info.")
  3344. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3345. (defvar org-all-time-keywords nil
  3346. "List of time keywords.")
  3347. (make-variable-buffer-local 'org-all-time-keywords)
  3348. (defconst org-plain-time-of-day-regexp
  3349. (concat
  3350. "\\(\\<[012]?[0-9]"
  3351. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3352. "\\(--?"
  3353. "\\(\\<[012]?[0-9]"
  3354. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3355. "\\)?")
  3356. "Regular expression to match a plain time or time range.
  3357. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3358. groups carry important information:
  3359. 0 the full match
  3360. 1 the first time, range or not
  3361. 8 the second time, if it is a range.")
  3362. (defconst org-plain-time-extension-regexp
  3363. (concat
  3364. "\\(\\<[012]?[0-9]"
  3365. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3366. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3367. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3368. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3369. groups carry important information:
  3370. 0 the full match
  3371. 7 hours of duration
  3372. 9 minutes of duration")
  3373. (defconst org-stamp-time-of-day-regexp
  3374. (concat
  3375. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3376. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3377. "\\(--?"
  3378. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3379. "Regular expression to match a timestamp time or time range.
  3380. After a match, the following groups carry important information:
  3381. 0 the full match
  3382. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3383. 2 the first time, range or not
  3384. 4 the second time, if it is a range.")
  3385. (defconst org-startup-options
  3386. '(("fold" org-startup-folded t)
  3387. ("overview" org-startup-folded t)
  3388. ("nofold" org-startup-folded nil)
  3389. ("showall" org-startup-folded nil)
  3390. ("showeverything" org-startup-folded showeverything)
  3391. ("content" org-startup-folded content)
  3392. ("indent" org-startup-indented t)
  3393. ("noindent" org-startup-indented nil)
  3394. ("hidestars" org-hide-leading-stars t)
  3395. ("showstars" org-hide-leading-stars nil)
  3396. ("odd" org-odd-levels-only t)
  3397. ("oddeven" org-odd-levels-only nil)
  3398. ("align" org-startup-align-all-tables t)
  3399. ("noalign" org-startup-align-all-tables nil)
  3400. ("customtime" org-display-custom-times t)
  3401. ("logdone" org-log-done time)
  3402. ("lognotedone" org-log-done note)
  3403. ("nologdone" org-log-done nil)
  3404. ("lognoteclock-out" org-log-note-clock-out t)
  3405. ("nolognoteclock-out" org-log-note-clock-out nil)
  3406. ("logrepeat" org-log-repeat state)
  3407. ("lognoterepeat" org-log-repeat note)
  3408. ("nologrepeat" org-log-repeat nil)
  3409. ("logreschedule" org-log-reschedule time)
  3410. ("lognotereschedule" org-log-reschedule note)
  3411. ("nologreschedule" org-log-reschedule nil)
  3412. ("logredeadline" org-log-redeadline time)
  3413. ("lognoteredeadline" org-log-redeadline note)
  3414. ("nologredeadline" org-log-redeadline nil)
  3415. ("logrefile" org-log-refile time)
  3416. ("lognoterefile" org-log-refile note)
  3417. ("nologrefile" org-log-refile nil)
  3418. ("fninline" org-footnote-define-inline t)
  3419. ("nofninline" org-footnote-define-inline nil)
  3420. ("fnlocal" org-footnote-section nil)
  3421. ("fnauto" org-footnote-auto-label t)
  3422. ("fnprompt" org-footnote-auto-label nil)
  3423. ("fnconfirm" org-footnote-auto-label confirm)
  3424. ("fnplain" org-footnote-auto-label plain)
  3425. ("fnadjust" org-footnote-auto-adjust t)
  3426. ("nofnadjust" org-footnote-auto-adjust nil)
  3427. ("constcgs" constants-unit-system cgs)
  3428. ("constSI" constants-unit-system SI)
  3429. ("noptag" org-tag-persistent-alist nil)
  3430. ("hideblocks" org-hide-block-startup t)
  3431. ("nohideblocks" org-hide-block-startup nil)
  3432. ("beamer" org-startup-with-beamer-mode t))
  3433. "Variable associated with STARTUP options for org-mode.
  3434. Each element is a list of three items: The startup options as written
  3435. in the #+STARTUP line, the corresponding variable, and the value to
  3436. set this variable to if the option is found. An optional forth element PUSH
  3437. means to push this value onto the list in the variable.")
  3438. (defun org-set-regexps-and-options ()
  3439. "Precompute regular expressions for current buffer."
  3440. (when (org-mode-p)
  3441. (org-set-local 'org-todo-kwd-alist nil)
  3442. (org-set-local 'org-todo-key-alist nil)
  3443. (org-set-local 'org-todo-key-trigger nil)
  3444. (org-set-local 'org-todo-keywords-1 nil)
  3445. (org-set-local 'org-done-keywords nil)
  3446. (org-set-local 'org-todo-heads nil)
  3447. (org-set-local 'org-todo-sets nil)
  3448. (org-set-local 'org-todo-log-states nil)
  3449. (org-set-local 'org-file-properties nil)
  3450. (org-set-local 'org-file-tags nil)
  3451. (let ((re (org-make-options-regexp
  3452. '("CATEGORY" "TODO" "COLUMNS"
  3453. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3454. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3455. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3456. (splitre "[ \t]+")
  3457. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3458. tail sep kws1 prio props ftags drawers beamer-p
  3459. ext-setup-or-nil setup-contents (start 0))
  3460. (save-excursion
  3461. (save-restriction
  3462. (widen)
  3463. (goto-char (point-min))
  3464. (while (or (and ext-setup-or-nil
  3465. (string-match re ext-setup-or-nil start)
  3466. (setq start (match-end 0)))
  3467. (and (setq ext-setup-or-nil nil start 0)
  3468. (re-search-forward re nil t)))
  3469. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3470. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3471. (cond
  3472. ((equal key "CATEGORY")
  3473. (if (string-match "[ \t]+$" value)
  3474. (setq value (replace-match "" t t value)))
  3475. (setq cat value))
  3476. ((member key '("SEQ_TODO" "TODO"))
  3477. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3478. ((equal key "TYP_TODO")
  3479. (push (cons 'type (org-split-string value splitre)) kwds))
  3480. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3481. ;; general TODO-like setup
  3482. (push (cons (intern (downcase (match-string 1 key)))
  3483. (org-split-string value splitre)) kwds))
  3484. ((equal key "TAGS")
  3485. (setq tags (append tags (if tags '("\\n") nil)
  3486. (org-split-string value splitre))))
  3487. ((equal key "COLUMNS")
  3488. (org-set-local 'org-columns-default-format value))
  3489. ((equal key "LINK")
  3490. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3491. (push (cons (match-string 1 value)
  3492. (org-trim (match-string 2 value)))
  3493. links)))
  3494. ((equal key "PRIORITIES")
  3495. (setq prio (org-split-string value " +")))
  3496. ((equal key "PROPERTY")
  3497. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3498. (push (cons (match-string 1 value) (match-string 2 value))
  3499. props)))
  3500. ((equal key "FILETAGS")
  3501. (when (string-match "\\S-" value)
  3502. (setq ftags
  3503. (append
  3504. ftags
  3505. (apply 'append
  3506. (mapcar (lambda (x) (org-split-string x ":"))
  3507. (org-split-string value)))))))
  3508. ((equal key "DRAWERS")
  3509. (setq drawers (org-split-string value splitre)))
  3510. ((equal key "CONSTANTS")
  3511. (setq const (append const (org-split-string value splitre))))
  3512. ((equal key "STARTUP")
  3513. (let ((opts (org-split-string value splitre))
  3514. l var val)
  3515. (while (setq l (pop opts))
  3516. (when (setq l (assoc l org-startup-options))
  3517. (setq var (nth 1 l) val (nth 2 l))
  3518. (if (not (nth 3 l))
  3519. (set (make-local-variable var) val)
  3520. (if (not (listp (symbol-value var)))
  3521. (set (make-local-variable var) nil))
  3522. (set (make-local-variable var) (symbol-value var))
  3523. (add-to-list var val))))))
  3524. ((equal key "ARCHIVE")
  3525. (string-match " *$" value)
  3526. (setq arch (replace-match "" t t value))
  3527. (remove-text-properties 0 (length arch)
  3528. '(face t fontified t) arch))
  3529. ((equal key "LATEX_CLASS")
  3530. (setq beamer-p (equal value "beamer")))
  3531. ((equal key "SETUPFILE")
  3532. (setq setup-contents (org-file-contents
  3533. (expand-file-name
  3534. (org-remove-double-quotes value))
  3535. 'noerror))
  3536. (if (not ext-setup-or-nil)
  3537. (setq ext-setup-or-nil setup-contents start 0)
  3538. (setq ext-setup-or-nil
  3539. (concat (substring ext-setup-or-nil 0 start)
  3540. "\n" setup-contents "\n"
  3541. (substring ext-setup-or-nil start)))))
  3542. ))))
  3543. (when cat
  3544. (org-set-local 'org-category (intern cat))
  3545. (push (cons "CATEGORY" cat) props))
  3546. (when prio
  3547. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3548. (setq prio (mapcar 'string-to-char prio))
  3549. (org-set-local 'org-highest-priority (nth 0 prio))
  3550. (org-set-local 'org-lowest-priority (nth 1 prio))
  3551. (org-set-local 'org-default-priority (nth 2 prio)))
  3552. (and props (org-set-local 'org-file-properties (nreverse props)))
  3553. (and ftags (org-set-local 'org-file-tags
  3554. (mapcar 'org-add-prop-inherited ftags)))
  3555. (and drawers (org-set-local 'org-drawers drawers))
  3556. (and arch (org-set-local 'org-archive-location arch))
  3557. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3558. ;; Process the TODO keywords
  3559. (unless kwds
  3560. ;; Use the global values as if they had been given locally.
  3561. (setq kwds (default-value 'org-todo-keywords))
  3562. (if (stringp (car kwds))
  3563. (setq kwds (list (cons org-todo-interpretation
  3564. (default-value 'org-todo-keywords)))))
  3565. (setq kwds (reverse kwds)))
  3566. (setq kwds (nreverse kwds))
  3567. (let (inter kws kw)
  3568. (while (setq kws (pop kwds))
  3569. (let ((kws (or
  3570. (run-hook-with-args-until-success
  3571. 'org-todo-setup-filter-hook kws)
  3572. kws)))
  3573. (setq inter (pop kws) sep (member "|" kws)
  3574. kws0 (delete "|" (copy-sequence kws))
  3575. kwsa nil
  3576. kws1 (mapcar
  3577. (lambda (x)
  3578. ;; 1 2
  3579. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3580. (progn
  3581. (setq kw (match-string 1 x)
  3582. key (and (match-end 2) (match-string 2 x))
  3583. log (org-extract-log-state-settings x))
  3584. (push (cons kw (and key (string-to-char key))) kwsa)
  3585. (and log (push log org-todo-log-states))
  3586. kw)
  3587. (error "Invalid TODO keyword %s" x)))
  3588. kws0)
  3589. kwsa (if kwsa (append '((:startgroup))
  3590. (nreverse kwsa)
  3591. '((:endgroup))))
  3592. hw (car kws1)
  3593. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3594. tail (list inter hw (car dws) (org-last dws))))
  3595. (add-to-list 'org-todo-heads hw 'append)
  3596. (push kws1 org-todo-sets)
  3597. (setq org-done-keywords (append org-done-keywords dws nil))
  3598. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3599. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3600. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3601. (setq org-todo-sets (nreverse org-todo-sets)
  3602. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3603. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3604. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3605. ;; Process the constants
  3606. (when const
  3607. (let (e cst)
  3608. (while (setq e (pop const))
  3609. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3610. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3611. (setq org-table-formula-constants-local cst)))
  3612. ;; Process the tags.
  3613. (when tags
  3614. (let (e tgs)
  3615. (while (setq e (pop tags))
  3616. (cond
  3617. ((equal e "{") (push '(:startgroup) tgs))
  3618. ((equal e "}") (push '(:endgroup) tgs))
  3619. ((equal e "\\n") (push '(:newline) tgs))
  3620. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3621. (push (cons (match-string 1 e)
  3622. (string-to-char (match-string 2 e)))
  3623. tgs))
  3624. (t (push (list e) tgs))))
  3625. (org-set-local 'org-tag-alist nil)
  3626. (while (setq e (pop tgs))
  3627. (or (and (stringp (car e))
  3628. (assoc (car e) org-tag-alist))
  3629. (push e org-tag-alist)))))
  3630. ;; Compute the regular expressions and other local variables
  3631. (if (not org-done-keywords)
  3632. (setq org-done-keywords (and org-todo-keywords-1
  3633. (list (org-last org-todo-keywords-1)))))
  3634. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3635. (length org-scheduled-string)
  3636. (length org-clock-string)
  3637. (length org-closed-string)))
  3638. org-drawer-regexp
  3639. (concat "^[ \t]*:\\("
  3640. (mapconcat 'regexp-quote org-drawers "\\|")
  3641. "\\):[ \t]*$")
  3642. org-not-done-keywords
  3643. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3644. org-todo-regexp
  3645. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3646. "\\|") "\\)\\>")
  3647. org-not-done-regexp
  3648. (concat "\\<\\("
  3649. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3650. "\\)\\>")
  3651. org-not-done-heading-regexp
  3652. (concat "^\\(\\*+\\)[ \t]+\\("
  3653. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3654. "\\)\\>")
  3655. org-todo-line-regexp
  3656. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3657. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3658. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3659. org-complex-heading-regexp
  3660. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3661. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3662. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3663. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3664. org-complex-heading-regexp-format
  3665. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3666. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3667. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3668. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3669. org-nl-done-regexp
  3670. (concat "\n\\*+[ \t]+"
  3671. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3672. "\\)" "\\>")
  3673. org-todo-line-tags-regexp
  3674. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3675. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3676. (org-re
  3677. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3678. org-looking-at-done-regexp
  3679. (concat "^" "\\(?:"
  3680. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3681. "\\>")
  3682. org-deadline-regexp (concat "\\<" org-deadline-string)
  3683. org-deadline-time-regexp
  3684. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3685. org-deadline-line-regexp
  3686. (concat "\\<\\(" org-deadline-string "\\).*")
  3687. org-scheduled-regexp
  3688. (concat "\\<" org-scheduled-string)
  3689. org-scheduled-time-regexp
  3690. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3691. org-closed-time-regexp
  3692. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3693. org-keyword-time-regexp
  3694. (concat "\\<\\(" org-scheduled-string
  3695. "\\|" org-deadline-string
  3696. "\\|" org-closed-string
  3697. "\\|" org-clock-string "\\)"
  3698. " *[[<]\\([^]>]+\\)[]>]")
  3699. org-keyword-time-not-clock-regexp
  3700. (concat "\\<\\(" org-scheduled-string
  3701. "\\|" org-deadline-string
  3702. "\\|" org-closed-string
  3703. "\\)"
  3704. " *[[<]\\([^]>]+\\)[]>]")
  3705. org-maybe-keyword-time-regexp
  3706. (concat "\\(\\<\\(" org-scheduled-string
  3707. "\\|" org-deadline-string
  3708. "\\|" org-closed-string
  3709. "\\|" org-clock-string "\\)\\)?"
  3710. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3711. org-planning-or-clock-line-re
  3712. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3713. "\\|" org-deadline-string
  3714. "\\|" org-closed-string "\\|" org-clock-string
  3715. "\\)\\>\\)")
  3716. org-all-time-keywords
  3717. (mapcar (lambda (w) (substring w 0 -1))
  3718. (list org-scheduled-string org-deadline-string
  3719. org-clock-string org-closed-string))
  3720. )
  3721. (org-compute-latex-and-specials-regexp)
  3722. (org-set-font-lock-defaults))))
  3723. (defun org-file-contents (file &optional noerror)
  3724. "Return the contents of FILE, as a string."
  3725. (if (or (not file)
  3726. (not (file-readable-p file)))
  3727. (if noerror
  3728. (progn
  3729. (message "Cannot read file %s" file)
  3730. (ding) (sit-for 2)
  3731. "")
  3732. (error "Cannot read file %s" file))
  3733. (with-temp-buffer
  3734. (insert-file-contents file)
  3735. (buffer-string))))
  3736. (defun org-extract-log-state-settings (x)
  3737. "Extract the log state setting from a TODO keyword string.
  3738. This will extract info from a string like \"WAIT(w@/!)\"."
  3739. (let (kw key log1 log2)
  3740. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3741. (setq kw (match-string 1 x)
  3742. key (and (match-end 2) (match-string 2 x))
  3743. log1 (and (match-end 3) (match-string 3 x))
  3744. log2 (and (match-end 4) (match-string 4 x)))
  3745. (and (or log1 log2)
  3746. (list kw
  3747. (and log1 (if (equal log1 "!") 'time 'note))
  3748. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3749. (defun org-remove-keyword-keys (list)
  3750. "Remove a pair of parenthesis at the end of each string in LIST."
  3751. (mapcar (lambda (x)
  3752. (if (string-match "(.*)$" x)
  3753. (substring x 0 (match-beginning 0))
  3754. x))
  3755. list))
  3756. (defun org-assign-fast-keys (alist)
  3757. "Assign fast keys to a keyword-key alist.
  3758. Respect keys that are already there."
  3759. (let (new e (alt ?0))
  3760. (while (setq e (pop alist))
  3761. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3762. (cdr e)) ;; Key already assigned.
  3763. (push e new)
  3764. (let ((clist (string-to-list (downcase (car e))))
  3765. (used (append new alist)))
  3766. (when (= (car clist) ?@)
  3767. (pop clist))
  3768. (while (and clist (rassoc (car clist) used))
  3769. (pop clist))
  3770. (unless clist
  3771. (while (rassoc alt used)
  3772. (incf alt)))
  3773. (push (cons (car e) (or (car clist) alt)) new))))
  3774. (nreverse new)))
  3775. ;;; Some variables used in various places
  3776. (defvar org-window-configuration nil
  3777. "Used in various places to store a window configuration.")
  3778. (defvar org-selected-window nil
  3779. "Used in various places to store a window configuration.")
  3780. (defvar org-finish-function nil
  3781. "Function to be called when `C-c C-c' is used.
  3782. This is for getting out of special buffers like remember.")
  3783. ;; FIXME: Occasionally check by commenting these, to make sure
  3784. ;; no other functions uses these, forgetting to let-bind them.
  3785. (defvar entry)
  3786. (defvar last-state)
  3787. (defvar date)
  3788. ;; Defined somewhere in this file, but used before definition.
  3789. (defvar org-entities) ;; defined in org-entities.el
  3790. (defvar org-struct-menu)
  3791. (defvar org-org-menu)
  3792. (defvar org-tbl-menu)
  3793. ;;;; Define the Org-mode
  3794. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3795. (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."))
  3796. ;; We use a before-change function to check if a table might need
  3797. ;; an update.
  3798. (defvar org-table-may-need-update t
  3799. "Indicates that a table might need an update.
  3800. This variable is set by `org-before-change-function'.
  3801. `org-table-align' sets it back to nil.")
  3802. (defun org-before-change-function (beg end)
  3803. "Every change indicates that a table might need an update."
  3804. (setq org-table-may-need-update t))
  3805. (defvar org-mode-map)
  3806. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3807. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3808. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3809. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3810. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3811. (defvar org-table-buffer-is-an nil)
  3812. (defconst org-outline-regexp "\\*+ ")
  3813. ;;;###autoload
  3814. (define-derived-mode org-mode outline-mode "Org"
  3815. "Outline-based notes management and organizer, alias
  3816. \"Carsten's outline-mode for keeping track of everything.\"
  3817. Org-mode develops organizational tasks around a NOTES file which
  3818. contains information about projects as plain text. Org-mode is
  3819. implemented on top of outline-mode, which is ideal to keep the content
  3820. of large files well structured. It supports ToDo items, deadlines and
  3821. time stamps, which magically appear in the diary listing of the Emacs
  3822. calendar. Tables are easily created with a built-in table editor.
  3823. Plain text URL-like links connect to websites, emails (VM), Usenet
  3824. messages (Gnus), BBDB entries, and any files related to the project.
  3825. For printing and sharing of notes, an Org-mode file (or a part of it)
  3826. can be exported as a structured ASCII or HTML file.
  3827. The following commands are available:
  3828. \\{org-mode-map}"
  3829. ;; Get rid of Outline menus, they are not needed
  3830. ;; Need to do this here because define-derived-mode sets up
  3831. ;; the keymap so late. Still, it is a waste to call this each time
  3832. ;; we switch another buffer into org-mode.
  3833. (if (featurep 'xemacs)
  3834. (when (boundp 'outline-mode-menu-heading)
  3835. ;; Assume this is Greg's port, it used easymenu
  3836. (easy-menu-remove outline-mode-menu-heading)
  3837. (easy-menu-remove outline-mode-menu-show)
  3838. (easy-menu-remove outline-mode-menu-hide))
  3839. (define-key org-mode-map [menu-bar headings] 'undefined)
  3840. (define-key org-mode-map [menu-bar hide] 'undefined)
  3841. (define-key org-mode-map [menu-bar show] 'undefined))
  3842. (org-load-modules-maybe)
  3843. (easy-menu-add org-org-menu)
  3844. (easy-menu-add org-tbl-menu)
  3845. (org-install-agenda-files-menu)
  3846. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  3847. (add-to-invisibility-spec '(org-cwidth))
  3848. (add-to-invisibility-spec '(org-hide-block . t))
  3849. (when (featurep 'xemacs)
  3850. (org-set-local 'line-move-ignore-invisible t))
  3851. (org-set-local 'outline-regexp org-outline-regexp)
  3852. (org-set-local 'outline-level 'org-outline-level)
  3853. (when (and org-ellipsis
  3854. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3855. (fboundp 'make-glyph-code))
  3856. (unless org-display-table
  3857. (setq org-display-table (make-display-table)))
  3858. (set-display-table-slot
  3859. org-display-table 4
  3860. (vconcat (mapcar
  3861. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3862. org-ellipsis)))
  3863. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3864. (setq buffer-display-table org-display-table))
  3865. (org-set-regexps-and-options)
  3866. (when (and org-tag-faces (not org-tags-special-faces-re))
  3867. ;; tag faces set outside customize.... force initialization.
  3868. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3869. ;; Calc embedded
  3870. (org-set-local 'calc-embedded-open-mode "# ")
  3871. (modify-syntax-entry ?# "<")
  3872. (modify-syntax-entry ?@ "w")
  3873. (if org-startup-truncated (setq truncate-lines t))
  3874. (org-set-local 'font-lock-unfontify-region-function
  3875. 'org-unfontify-region)
  3876. ;; Activate before-change-function
  3877. (org-set-local 'org-table-may-need-update t)
  3878. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3879. 'local)
  3880. ;; Check for running clock before killing a buffer
  3881. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3882. ;; Paragraphs and auto-filling
  3883. (org-set-autofill-regexps)
  3884. (setq indent-line-function 'org-indent-line-function)
  3885. (org-update-radio-target-regexp)
  3886. ;; Make sure dependence stuff works reliably, even for users who set it
  3887. ;; too late :-(
  3888. (if org-enforce-todo-dependencies
  3889. (add-hook 'org-blocker-hook
  3890. 'org-block-todo-from-children-or-siblings-or-parent)
  3891. (remove-hook 'org-blocker-hook
  3892. 'org-block-todo-from-children-or-siblings-or-parent))
  3893. (if org-enforce-todo-checkbox-dependencies
  3894. (add-hook 'org-blocker-hook
  3895. 'org-block-todo-from-checkboxes)
  3896. (remove-hook 'org-blocker-hook
  3897. 'org-block-todo-from-checkboxes))
  3898. ;; Comment characters
  3899. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3900. (org-set-local 'comment-padding " ")
  3901. ;; Align options lines
  3902. (org-set-local
  3903. 'align-mode-rules-list
  3904. '((org-in-buffer-settings
  3905. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3906. (modes . '(org-mode)))))
  3907. ;; Imenu
  3908. (org-set-local 'imenu-create-index-function
  3909. 'org-imenu-get-tree)
  3910. ;; Make isearch reveal context
  3911. (if (or (featurep 'xemacs)
  3912. (not (boundp 'outline-isearch-open-invisible-function)))
  3913. ;; Emacs 21 and XEmacs make use of the hook
  3914. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3915. ;; Emacs 22 deals with this through a special variable
  3916. (org-set-local 'outline-isearch-open-invisible-function
  3917. (lambda (&rest ignore) (org-show-context 'isearch))))
  3918. ;; Turn on org-beamer-mode?
  3919. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3920. ;; If empty file that did not turn on org-mode automatically, make it to.
  3921. (if (and org-insert-mode-line-in-empty-file
  3922. (interactive-p)
  3923. (= (point-min) (point-max)))
  3924. (insert "# -*- mode: org -*-\n\n"))
  3925. (unless org-inhibit-startup
  3926. (when org-startup-align-all-tables
  3927. (let ((bmp (buffer-modified-p)))
  3928. (org-table-map-tables 'org-table-align)
  3929. (set-buffer-modified-p bmp)))
  3930. (when org-startup-indented
  3931. (require 'org-indent)
  3932. (org-indent-mode 1))
  3933. (unless org-inhibit-startup-visibility-stuff
  3934. (org-set-startup-visibility))))
  3935. (when (fboundp 'abbrev-table-put)
  3936. (abbrev-table-put org-mode-abbrev-table
  3937. :parents (list text-mode-abbrev-table)))
  3938. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3939. (defun org-current-time ()
  3940. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3941. (if (> (car org-time-stamp-rounding-minutes) 1)
  3942. (let ((r (car org-time-stamp-rounding-minutes))
  3943. (time (decode-time)))
  3944. (apply 'encode-time
  3945. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3946. (nthcdr 2 time))))
  3947. (current-time)))
  3948. ;;;; Font-Lock stuff, including the activators
  3949. (defvar org-mouse-map (make-sparse-keymap))
  3950. (org-defkey org-mouse-map
  3951. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3952. (org-defkey org-mouse-map
  3953. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3954. (when org-mouse-1-follows-link
  3955. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3956. (when org-tab-follows-link
  3957. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3958. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3959. (require 'font-lock)
  3960. (defconst org-non-link-chars "]\t\n\r<>")
  3961. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3962. "shell" "elisp"))
  3963. (defvar org-link-types-re nil
  3964. "Matches a link that has a url-like prefix like \"http:\"")
  3965. (defvar org-link-re-with-space nil
  3966. "Matches a link with spaces, optional angular brackets around it.")
  3967. (defvar org-link-re-with-space2 nil
  3968. "Matches a link with spaces, optional angular brackets around it.")
  3969. (defvar org-link-re-with-space3 nil
  3970. "Matches a link with spaces, only for internal part in bracket links.")
  3971. (defvar org-angle-link-re nil
  3972. "Matches link with angular brackets, spaces are allowed.")
  3973. (defvar org-plain-link-re nil
  3974. "Matches plain link, without spaces.")
  3975. (defvar org-bracket-link-regexp nil
  3976. "Matches a link in double brackets.")
  3977. (defvar org-bracket-link-analytic-regexp nil
  3978. "Regular expression used to analyze links.
  3979. Here is what the match groups contain after a match:
  3980. 1: http:
  3981. 2: http
  3982. 3: path
  3983. 4: [desc]
  3984. 5: desc")
  3985. (defvar org-bracket-link-analytic-regexp++ nil
  3986. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3987. (defvar org-any-link-re nil
  3988. "Regular expression matching any link.")
  3989. (defun org-make-link-regexps ()
  3990. "Update the link regular expressions.
  3991. This should be called after the variable `org-link-types' has changed."
  3992. (setq org-link-types-re
  3993. (concat
  3994. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3995. org-link-re-with-space
  3996. (concat
  3997. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3998. "\\([^" org-non-link-chars " ]"
  3999. "[^" org-non-link-chars "]*"
  4000. "[^" org-non-link-chars " ]\\)>?")
  4001. org-link-re-with-space2
  4002. (concat
  4003. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4004. "\\([^" org-non-link-chars " ]"
  4005. "[^\t\n\r]*"
  4006. "[^" org-non-link-chars " ]\\)>?")
  4007. org-link-re-with-space3
  4008. (concat
  4009. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4010. "\\([^" org-non-link-chars " ]"
  4011. "[^\t\n\r]*\\)")
  4012. org-angle-link-re
  4013. (concat
  4014. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4015. "\\([^" org-non-link-chars " ]"
  4016. "[^" org-non-link-chars "]*"
  4017. "\\)>")
  4018. org-plain-link-re
  4019. (concat
  4020. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4021. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4022. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4023. org-bracket-link-regexp
  4024. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4025. org-bracket-link-analytic-regexp
  4026. (concat
  4027. "\\[\\["
  4028. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4029. "\\([^]]+\\)"
  4030. "\\]"
  4031. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4032. "\\]")
  4033. org-bracket-link-analytic-regexp++
  4034. (concat
  4035. "\\[\\["
  4036. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4037. "\\([^]]+\\)"
  4038. "\\]"
  4039. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4040. "\\]")
  4041. org-any-link-re
  4042. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4043. org-angle-link-re "\\)\\|\\("
  4044. org-plain-link-re "\\)")))
  4045. (org-make-link-regexps)
  4046. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4047. "Regular expression for fast time stamp matching.")
  4048. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4049. "Regular expression for fast time stamp matching.")
  4050. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4051. "Regular expression matching time strings for analysis.
  4052. This one does not require the space after the date, so it can be used
  4053. on a string that terminates immediately after the date.")
  4054. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4055. "Regular expression matching time strings for analysis.")
  4056. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4057. "Regular expression matching time stamps, with groups.")
  4058. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4059. "Regular expression matching time stamps (also [..]), with groups.")
  4060. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4061. "Regular expression matching a time stamp range.")
  4062. (defconst org-tr-regexp-both
  4063. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4064. "Regular expression matching a time stamp range.")
  4065. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4066. org-ts-regexp "\\)?")
  4067. "Regular expression matching a time stamp or time stamp range.")
  4068. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4069. org-ts-regexp-both "\\)?")
  4070. "Regular expression matching a time stamp or time stamp range.
  4071. The time stamps may be either active or inactive.")
  4072. (defvar org-emph-face nil)
  4073. (defun org-do-emphasis-faces (limit)
  4074. "Run through the buffer and add overlays to links."
  4075. (let (rtn a)
  4076. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4077. (if (not (= (char-after (match-beginning 3))
  4078. (char-after (match-beginning 4))))
  4079. (progn
  4080. (setq rtn t)
  4081. (setq a (assoc (match-string 3) org-emphasis-alist))
  4082. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4083. 'face
  4084. (nth 1 a))
  4085. (and (nth 4 a)
  4086. (org-remove-flyspell-overlays-in
  4087. (match-beginning 0) (match-end 0)))
  4088. (add-text-properties (match-beginning 2) (match-end 2)
  4089. '(font-lock-multiline t))
  4090. (when org-hide-emphasis-markers
  4091. (add-text-properties (match-end 4) (match-beginning 5)
  4092. '(invisible org-link))
  4093. (add-text-properties (match-beginning 3) (match-end 3)
  4094. '(invisible org-link)))))
  4095. (backward-char 1))
  4096. rtn))
  4097. (defun org-emphasize (&optional char)
  4098. "Insert or change an emphasis, i.e. a font like bold or italic.
  4099. If there is an active region, change that region to a new emphasis.
  4100. If there is no region, just insert the marker characters and position
  4101. the cursor between them.
  4102. CHAR should be either the marker character, or the first character of the
  4103. HTML tag associated with that emphasis. If CHAR is a space, the means
  4104. to remove the emphasis of the selected region.
  4105. If char is not given (for example in an interactive call) it
  4106. will be prompted for."
  4107. (interactive)
  4108. (let ((eal org-emphasis-alist) e det
  4109. (erc org-emphasis-regexp-components)
  4110. (prompt "")
  4111. (string "") beg end move tag c s)
  4112. (if (org-region-active-p)
  4113. (setq beg (region-beginning) end (region-end)
  4114. string (buffer-substring beg end))
  4115. (setq move t))
  4116. (while (setq e (pop eal))
  4117. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4118. c (aref tag 0))
  4119. (push (cons c (string-to-char (car e))) det)
  4120. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4121. (substring tag 1)))))
  4122. (setq det (nreverse det))
  4123. (unless char
  4124. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4125. (setq char (read-char-exclusive)))
  4126. (setq char (or (cdr (assoc char det)) char))
  4127. (if (equal char ?\ )
  4128. (setq s "" move nil)
  4129. (unless (assoc (char-to-string char) org-emphasis-alist)
  4130. (error "No such emphasis marker: \"%c\"" char))
  4131. (setq s (char-to-string char)))
  4132. (while (and (> (length string) 1)
  4133. (equal (substring string 0 1) (substring string -1))
  4134. (assoc (substring string 0 1) org-emphasis-alist))
  4135. (setq string (substring string 1 -1)))
  4136. (setq string (concat s string s))
  4137. (if beg (delete-region beg end))
  4138. (unless (or (bolp)
  4139. (string-match (concat "[" (nth 0 erc) "\n]")
  4140. (char-to-string (char-before (point)))))
  4141. (insert " "))
  4142. (unless (or (eobp)
  4143. (string-match (concat "[" (nth 1 erc) "\n]")
  4144. (char-to-string (char-after (point)))))
  4145. (insert " ") (backward-char 1))
  4146. (insert string)
  4147. (and move (backward-char 1))))
  4148. (defconst org-nonsticky-props
  4149. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4150. (defsubst org-rear-nonsticky-at (pos)
  4151. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4152. (defun org-activate-plain-links (limit)
  4153. "Run through the buffer and add overlays to links."
  4154. (catch 'exit
  4155. (let (f)
  4156. (if (re-search-forward org-plain-link-re limit t)
  4157. (progn
  4158. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4159. (setq f (get-text-property (match-beginning 0) 'face))
  4160. (if (or (eq f 'org-tag)
  4161. (and (listp f) (memq 'org-tag f)))
  4162. nil
  4163. (add-text-properties (match-beginning 0) (match-end 0)
  4164. (list 'mouse-face 'highlight
  4165. 'face 'org-link
  4166. 'keymap org-mouse-map))
  4167. (org-rear-nonsticky-at (match-end 0)))
  4168. t)))))
  4169. (defun org-activate-code (limit)
  4170. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4171. (progn
  4172. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4173. (remove-text-properties (match-beginning 0) (match-end 0)
  4174. '(display t invisible t intangible t))
  4175. t)))
  4176. (defun org-fontify-meta-lines-and-blocks (limit)
  4177. "Fontify #+ lines and blocks, in the correct ways."
  4178. (let ((case-fold-search t))
  4179. (if (re-search-forward
  4180. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4181. limit t)
  4182. (let ((beg (match-beginning 0))
  4183. (beg1 (line-beginning-position 2))
  4184. (dc1 (downcase (match-string 2)))
  4185. (dc3 (downcase (match-string 3)))
  4186. end end1 quoting block-type)
  4187. (cond
  4188. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4189. ;; a single line of backend-specific content
  4190. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4191. (remove-text-properties (match-beginning 0) (match-end 0)
  4192. '(display t invisible t intangible t))
  4193. (add-text-properties (match-beginning 1) (match-end 3)
  4194. '(font-lock-fontified t face org-meta-line))
  4195. (add-text-properties (match-beginning 6) (match-end 6)
  4196. '(font-lock-fontified t face org-block))
  4197. t)
  4198. ((and (match-end 4) (equal dc3 "begin"))
  4199. ;; Truely a block
  4200. (setq block-type (downcase (match-string 5))
  4201. quoting (member block-type org-protecting-blocks))
  4202. (when (re-search-forward
  4203. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4204. nil t) ;; on purpose, we look further than LIMIT
  4205. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4206. (when quoting
  4207. (remove-text-properties beg end
  4208. '(display t invisible t intangible t)))
  4209. (add-text-properties
  4210. beg end
  4211. '(font-lock-fontified t font-lock-multiline t))
  4212. (add-text-properties beg beg1 '(face org-meta-line))
  4213. (add-text-properties end1 end '(face org-meta-line))
  4214. (cond
  4215. (quoting
  4216. (add-text-properties beg1 end1 '(face org-block)))
  4217. ((not org-fontify-quote-and-verse-blocks))
  4218. ((string= block-type "quote")
  4219. (add-text-properties beg1 end1 '(face org-quote)))
  4220. ((string= block-type "verse")
  4221. (add-text-properties beg1 end1 '(face org-verse))))
  4222. t))
  4223. ((member dc1 '("title:" "author:" "email:" "date:"))
  4224. (add-text-properties
  4225. beg (match-end 3)
  4226. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4227. '(font-lock-fontified t invisible t)
  4228. '(font-lock-fontified t face org-document-info-keyword)))
  4229. (add-text-properties
  4230. (match-beginning 6) (match-end 6)
  4231. (if (string-equal dc1 "title:")
  4232. '(font-lock-fontified t face org-document-title)
  4233. '(font-lock-fontified t face org-document-info))))
  4234. ((not (member (char-after beg) '(?\ ?\t)))
  4235. ;; just any other in-buffer setting, but not indented
  4236. (add-text-properties
  4237. beg (match-end 0)
  4238. '(font-lock-fontified t face org-meta-line))
  4239. t)
  4240. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4241. "orgtbl:" "tblfm:" "tblname:"))
  4242. (and (match-end 4) (equal dc3 "attr")))
  4243. (add-text-properties
  4244. beg (match-end 0)
  4245. '(font-lock-fontified t face org-meta-line))
  4246. t)
  4247. ((member dc3 '(" " ""))
  4248. (add-text-properties
  4249. beg (match-end 0)
  4250. '(font-lock-fontified t face font-lock-comment-face)))
  4251. (t nil))))))
  4252. (defun org-activate-angle-links (limit)
  4253. "Run through the buffer and add overlays to links."
  4254. (if (re-search-forward org-angle-link-re limit t)
  4255. (progn
  4256. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4257. (add-text-properties (match-beginning 0) (match-end 0)
  4258. (list 'mouse-face 'highlight
  4259. 'keymap org-mouse-map))
  4260. (org-rear-nonsticky-at (match-end 0))
  4261. t)))
  4262. (defun org-activate-footnote-links (limit)
  4263. "Run through the buffer and add overlays to links."
  4264. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4265. limit t)
  4266. (progn
  4267. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4268. (add-text-properties (match-beginning 2) (match-end 2)
  4269. (list 'mouse-face 'highlight
  4270. 'keymap org-mouse-map
  4271. 'help-echo
  4272. (if (= (point-at-bol) (match-beginning 2))
  4273. "Footnote definition"
  4274. "Footnote reference")
  4275. ))
  4276. (org-rear-nonsticky-at (match-end 2))
  4277. t)))
  4278. (defun org-activate-bracket-links (limit)
  4279. "Run through the buffer and add overlays to bracketed links."
  4280. (if (re-search-forward org-bracket-link-regexp limit t)
  4281. (let* ((help (concat "LINK: "
  4282. (org-match-string-no-properties 1)))
  4283. ;; FIXME: above we should remove the escapes.
  4284. ;; but that requires another match, protecting match data,
  4285. ;; a lot of overhead for font-lock.
  4286. (ip (org-maybe-intangible
  4287. (list 'invisible 'org-link
  4288. 'keymap org-mouse-map 'mouse-face 'highlight
  4289. 'font-lock-multiline t 'help-echo help)))
  4290. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4291. 'font-lock-multiline t 'help-echo help)))
  4292. ;; We need to remove the invisible property here. Table narrowing
  4293. ;; may have made some of this invisible.
  4294. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4295. (remove-text-properties (match-beginning 0) (match-end 0)
  4296. '(invisible nil))
  4297. (if (match-end 3)
  4298. (progn
  4299. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4300. (org-rear-nonsticky-at (match-beginning 3))
  4301. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4302. (org-rear-nonsticky-at (match-end 3))
  4303. (add-text-properties (match-end 3) (match-end 0) ip)
  4304. (org-rear-nonsticky-at (match-end 0)))
  4305. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4306. (org-rear-nonsticky-at (match-beginning 1))
  4307. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4308. (org-rear-nonsticky-at (match-end 1))
  4309. (add-text-properties (match-end 1) (match-end 0) ip)
  4310. (org-rear-nonsticky-at (match-end 0)))
  4311. t)))
  4312. (defun org-activate-dates (limit)
  4313. "Run through the buffer and add overlays to dates."
  4314. (if (re-search-forward org-tsr-regexp-both limit t)
  4315. (progn
  4316. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4317. (add-text-properties (match-beginning 0) (match-end 0)
  4318. (list 'mouse-face 'highlight
  4319. 'keymap org-mouse-map))
  4320. (org-rear-nonsticky-at (match-end 0))
  4321. (when org-display-custom-times
  4322. (if (match-end 3)
  4323. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4324. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4325. t)))
  4326. (defvar org-target-link-regexp nil
  4327. "Regular expression matching radio targets in plain text.")
  4328. (make-variable-buffer-local 'org-target-link-regexp)
  4329. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4330. "Regular expression matching a link target.")
  4331. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4332. "Regular expression matching a radio target.")
  4333. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4334. "Regular expression matching any target.")
  4335. (defun org-activate-target-links (limit)
  4336. "Run through the buffer and add overlays to target matches."
  4337. (when org-target-link-regexp
  4338. (let ((case-fold-search t))
  4339. (if (re-search-forward org-target-link-regexp limit t)
  4340. (progn
  4341. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4342. (add-text-properties (match-beginning 0) (match-end 0)
  4343. (list 'mouse-face 'highlight
  4344. 'keymap org-mouse-map
  4345. 'help-echo "Radio target link"
  4346. 'org-linked-text t))
  4347. (org-rear-nonsticky-at (match-end 0))
  4348. t)))))
  4349. (defun org-update-radio-target-regexp ()
  4350. "Find all radio targets in this file and update the regular expression."
  4351. (interactive)
  4352. (when (memq 'radio org-activate-links)
  4353. (setq org-target-link-regexp
  4354. (org-make-target-link-regexp (org-all-targets 'radio)))
  4355. (org-restart-font-lock)))
  4356. (defun org-hide-wide-columns (limit)
  4357. (let (s e)
  4358. (setq s (text-property-any (point) (or limit (point-max))
  4359. 'org-cwidth t))
  4360. (when s
  4361. (setq e (next-single-property-change s 'org-cwidth))
  4362. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4363. (goto-char e)
  4364. t)))
  4365. (defvar org-latex-and-specials-regexp nil
  4366. "Regular expression for highlighting export special stuff.")
  4367. (defvar org-match-substring-regexp)
  4368. (defvar org-match-substring-with-braces-regexp)
  4369. ;; This should be with the exporter code, but we also use if for font-locking
  4370. (defconst org-export-html-special-string-regexps
  4371. '(("\\\\-" . "&shy;")
  4372. ("---\\([^-]\\)" . "&mdash;\\1")
  4373. ("--\\([^-]\\)" . "&ndash;\\1")
  4374. ("\\.\\.\\." . "&hellip;"))
  4375. "Regular expressions for special string conversion.")
  4376. (defun org-compute-latex-and-specials-regexp ()
  4377. "Compute regular expression for stuff treated specially by exporters."
  4378. (if (not org-highlight-latex-fragments-and-specials)
  4379. (org-set-local 'org-latex-and-specials-regexp nil)
  4380. (require 'org-exp)
  4381. (let*
  4382. ((matchers (plist-get org-format-latex-options :matchers))
  4383. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4384. org-latex-regexps)))
  4385. (org-export-allow-BIND nil)
  4386. (options (org-combine-plists (org-default-export-plist)
  4387. (org-infile-export-plist)))
  4388. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4389. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4390. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4391. (org-export-html-expand (plist-get options :expand-quoted-html))
  4392. (org-export-with-special-strings (plist-get options :special-strings))
  4393. (re-sub
  4394. (cond
  4395. ((equal org-export-with-sub-superscripts '{})
  4396. (list org-match-substring-with-braces-regexp))
  4397. (org-export-with-sub-superscripts
  4398. (list org-match-substring-regexp))
  4399. (t nil)))
  4400. (re-latex
  4401. (if org-export-with-LaTeX-fragments
  4402. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4403. (re-macros
  4404. (if org-export-with-TeX-macros
  4405. (list (concat "\\\\"
  4406. (regexp-opt
  4407. (append (mapcar 'car (append org-entities-user
  4408. org-entities))
  4409. (if (boundp 'org-latex-entities)
  4410. (mapcar (lambda (x)
  4411. (or (car-safe x) x))
  4412. org-latex-entities)
  4413. nil))
  4414. 'words))) ; FIXME
  4415. ))
  4416. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4417. (re-special (if org-export-with-special-strings
  4418. (mapcar (lambda (x) (car x))
  4419. org-export-html-special-string-regexps)))
  4420. (re-rest
  4421. (delq nil
  4422. (list
  4423. (if org-export-html-expand "@<[^>\n]+>")
  4424. ))))
  4425. (org-set-local
  4426. 'org-latex-and-specials-regexp
  4427. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4428. re-rest) "\\|")))))
  4429. (defun org-do-latex-and-special-faces (limit)
  4430. "Run through the buffer and add overlays to links."
  4431. (when org-latex-and-specials-regexp
  4432. (let (rtn d)
  4433. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4434. limit t))
  4435. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4436. 'face))
  4437. '(org-code org-verbatim underline)))
  4438. (progn
  4439. (setq rtn t
  4440. d (cond ((member (char-after (1+ (match-beginning 0)))
  4441. '(?_ ?^)) 1)
  4442. (t 0)))
  4443. (font-lock-prepend-text-property
  4444. (+ d (match-beginning 0)) (match-end 0)
  4445. 'face 'org-latex-and-export-specials)
  4446. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4447. '(font-lock-multiline t)))))
  4448. rtn)))
  4449. (defun org-restart-font-lock ()
  4450. "Restart font-lock-mode, to force refontification."
  4451. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4452. (font-lock-mode -1)
  4453. (font-lock-mode 1)))
  4454. (defun org-all-targets (&optional radio)
  4455. "Return a list of all targets in this file.
  4456. With optional argument RADIO, only find radio targets."
  4457. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4458. rtn)
  4459. (save-excursion
  4460. (goto-char (point-min))
  4461. (while (re-search-forward re nil t)
  4462. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4463. rtn)))
  4464. (defun org-make-target-link-regexp (targets)
  4465. "Make regular expression matching all strings in TARGETS.
  4466. The regular expression finds the targets also if there is a line break
  4467. between words."
  4468. (and targets
  4469. (concat
  4470. "\\<\\("
  4471. (mapconcat
  4472. (lambda (x)
  4473. (while (string-match " +" x)
  4474. (setq x (replace-match "\\s-+" t t x)))
  4475. x)
  4476. targets
  4477. "\\|")
  4478. "\\)\\>")))
  4479. (defun org-activate-tags (limit)
  4480. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4481. (progn
  4482. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4483. (add-text-properties (match-beginning 1) (match-end 1)
  4484. (list 'mouse-face 'highlight
  4485. 'keymap org-mouse-map))
  4486. (org-rear-nonsticky-at (match-end 1))
  4487. t)))
  4488. (defun org-outline-level ()
  4489. "Compute the outline level of the heading at point.
  4490. This function assumes that the cursor is at the beginning of a line matched
  4491. by outline-regexp. Otherwise it returns garbage.
  4492. If this is called at a normal headline, the level is the number of stars.
  4493. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4494. For plain list items, if they are matched by `outline-regexp', this returns
  4495. 1000 plus the line indentation."
  4496. (save-excursion
  4497. (looking-at outline-regexp)
  4498. (if (match-beginning 1)
  4499. (+ (org-get-string-indentation (match-string 1)) 1000)
  4500. (1- (- (match-end 0) (match-beginning 0))))))
  4501. (defvar org-font-lock-keywords nil)
  4502. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4503. "Regular expression matching a property line.")
  4504. (defvar org-font-lock-hook nil
  4505. "Functions to be called for special font lock stuff.")
  4506. (defun org-font-lock-hook (limit)
  4507. (run-hook-with-args 'org-font-lock-hook limit))
  4508. (defun org-set-font-lock-defaults ()
  4509. (let* ((em org-fontify-emphasized-text)
  4510. (lk org-activate-links)
  4511. (org-font-lock-extra-keywords
  4512. (list
  4513. ;; Call the hook
  4514. '(org-font-lock-hook)
  4515. ;; Headlines
  4516. `(,(if org-fontify-whole-heading-line
  4517. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4518. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4519. (1 (org-get-level-face 1))
  4520. (2 (org-get-level-face 2))
  4521. (3 (org-get-level-face 3)))
  4522. ;; Table lines
  4523. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4524. (1 'org-table t))
  4525. ;; Table internals
  4526. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4527. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4528. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4529. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4530. ;; Drawers
  4531. (list org-drawer-regexp '(0 'org-special-keyword t))
  4532. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4533. ;; Properties
  4534. (list org-property-re
  4535. '(1 'org-special-keyword t)
  4536. '(3 'org-property-value t))
  4537. ;; Links
  4538. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4539. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4540. (if (memq 'plain lk) '(org-activate-plain-links))
  4541. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4542. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4543. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4544. (if (memq 'footnote lk) '(org-activate-footnote-links
  4545. (2 'org-footnote t)))
  4546. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4547. '(org-hide-wide-columns (0 nil append))
  4548. ;; TODO lines
  4549. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4550. '(1 (org-get-todo-face 1) t))
  4551. ;; DONE
  4552. (if org-fontify-done-headline
  4553. (list (concat "^[*]+ +\\<\\("
  4554. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4555. "\\)\\(.*\\)")
  4556. '(2 'org-headline-done t))
  4557. nil)
  4558. ;; Priorities
  4559. '(org-font-lock-add-priority-faces)
  4560. ;; Tags
  4561. '(org-font-lock-add-tag-faces)
  4562. ;; Special keywords
  4563. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4564. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4565. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4566. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4567. ;; Emphasis
  4568. (if em
  4569. (if (featurep 'xemacs)
  4570. '(org-do-emphasis-faces (0 nil append))
  4571. '(org-do-emphasis-faces)))
  4572. ;; Checkboxes
  4573. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4574. 2 'org-checkbox prepend)
  4575. (if org-provide-checkbox-statistics
  4576. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4577. (0 (org-get-checkbox-statistics-face) t)))
  4578. ;; Description list items
  4579. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4580. 2 'bold prepend)
  4581. ;; ARCHIVEd headings
  4582. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4583. '(1 'org-archived prepend))
  4584. ;; Specials
  4585. '(org-do-latex-and-special-faces)
  4586. ;; Code
  4587. '(org-activate-code (1 'org-code t))
  4588. ;; COMMENT
  4589. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4590. "\\|" org-quote-string "\\)\\>")
  4591. '(1 'org-special-keyword t))
  4592. '("^#.*" (0 'font-lock-comment-face t))
  4593. ;; Blocks and meta lines
  4594. '(org-fontify-meta-lines-and-blocks)
  4595. )))
  4596. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4597. ;; Now set the full font-lock-keywords
  4598. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4599. (org-set-local 'font-lock-defaults
  4600. '(org-font-lock-keywords t nil nil backward-paragraph))
  4601. (kill-local-variable 'font-lock-keywords) nil))
  4602. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4603. "Fontify string S like in Org-mode"
  4604. (with-temp-buffer
  4605. (insert s)
  4606. (let ((org-odd-levels-only odd-levels))
  4607. (org-mode)
  4608. (font-lock-fontify-buffer)
  4609. (buffer-string))))
  4610. (defvar org-m nil)
  4611. (defvar org-l nil)
  4612. (defvar org-f nil)
  4613. (defun org-get-level-face (n)
  4614. "Get the right face for match N in font-lock matching of headlines."
  4615. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4616. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4617. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4618. (cond
  4619. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4620. ((eq n 2) org-f)
  4621. (t (if org-level-color-stars-only nil org-f))))
  4622. (defun org-get-todo-face (kwd)
  4623. "Get the right face for a TODO keyword KWD.
  4624. If KWD is a number, get the corresponding match group."
  4625. (if (numberp kwd) (setq kwd (match-string kwd)))
  4626. (or (org-face-from-face-or-color
  4627. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4628. (and (member kwd org-done-keywords) 'org-done)
  4629. 'org-todo))
  4630. (defun org-face-from-face-or-color (context inherit face-or-color)
  4631. "Create a face list that inherits INHERIT, but sets the foreground color.
  4632. When FACE-OR-COLOR is not a string, just return it."
  4633. (if (stringp face-or-color)
  4634. (list :inherit inherit
  4635. (cdr (assoc context org-faces-easy-properties))
  4636. face-or-color)
  4637. face-or-color))
  4638. (defun org-font-lock-add-tag-faces (limit)
  4639. "Add the special tag faces."
  4640. (when (and org-tag-faces org-tags-special-faces-re)
  4641. (while (re-search-forward org-tags-special-faces-re limit t)
  4642. (add-text-properties (match-beginning 1) (match-end 1)
  4643. (list 'face (org-get-tag-face 1)
  4644. 'font-lock-fontified t))
  4645. (backward-char 1))))
  4646. (defun org-font-lock-add-priority-faces (limit)
  4647. "Add the special priority faces."
  4648. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4649. (add-text-properties
  4650. (match-beginning 0) (match-end 0)
  4651. (list 'face (or (org-face-from-face-or-color
  4652. 'priority 'org-special-keyword
  4653. (cdr (assoc (char-after (match-beginning 1))
  4654. org-priority-faces)))
  4655. 'org-special-keyword)
  4656. 'font-lock-fontified t))))
  4657. (defun org-get-tag-face (kwd)
  4658. "Get the right face for a TODO keyword KWD.
  4659. If KWD is a number, get the corresponding match group."
  4660. (if (numberp kwd) (setq kwd (match-string kwd)))
  4661. (or (org-face-from-face-or-color
  4662. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4663. 'org-tag))
  4664. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4665. "Remove fontification and activation overlays from links."
  4666. (font-lock-default-unfontify-region beg end)
  4667. (let* ((buffer-undo-list t)
  4668. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4669. (inhibit-modification-hooks t)
  4670. deactivate-mark buffer-file-name buffer-file-truename)
  4671. (remove-text-properties
  4672. beg end
  4673. (if org-indent-mode
  4674. ;; also remove line-prefix and wrap-prefix properties
  4675. '(mouse-face t keymap t org-linked-text t
  4676. invisible t intangible t
  4677. line-prefix t wrap-prefix t
  4678. org-no-flyspell t)
  4679. '(mouse-face t keymap t org-linked-text t
  4680. invisible t intangible t
  4681. org-no-flyspell t)))))
  4682. ;;;; Visibility cycling, including org-goto and indirect buffer
  4683. ;;; Cycling
  4684. (defvar org-cycle-global-status nil)
  4685. (make-variable-buffer-local 'org-cycle-global-status)
  4686. (defvar org-cycle-subtree-status nil)
  4687. (make-variable-buffer-local 'org-cycle-subtree-status)
  4688. ;;;###autoload
  4689. (defvar org-inlinetask-min-level)
  4690. (defun org-cycle (&optional arg)
  4691. "TAB-action and visibility cycling for Org-mode.
  4692. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4693. is outline visibility cycling, but it also invokes other actions
  4694. in special contexts.
  4695. - When this function is called with a prefix argument, rotate the entire
  4696. buffer through 3 states (global cycling)
  4697. 1. OVERVIEW: Show only top-level headlines.
  4698. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4699. 3. SHOW ALL: Show everything.
  4700. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4701. determined by the variable `org-startup-folded', and by any VISIBILITY
  4702. properties in the buffer.
  4703. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4704. including any drawers.
  4705. - When inside a table, re-align the table and move to the next field.
  4706. - When point is at the beginning of a headline, rotate the subtree started
  4707. by this line through 3 different states (local cycling)
  4708. 1. FOLDED: Only the main headline is shown.
  4709. 2. CHILDREN: The main headline and the direct children are shown.
  4710. From this state, you can move to one of the children
  4711. and zoom in further.
  4712. 3. SUBTREE: Show the entire subtree, including body text.
  4713. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4714. - When point is at the beginning of an empty headline and the variable
  4715. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4716. of the headline by demoting and promoting it to likely levels. This
  4717. speeds up creation document structure by presing TAB once or several
  4718. times right after creating a new headline.
  4719. - When there is a numeric prefix, go up to a heading with level ARG, do
  4720. a `show-subtree' and return to the previous cursor position. If ARG
  4721. is negative, go up that many levels.
  4722. - When point is not at the beginning of a headline, execute the global
  4723. binding for TAB, which is re-indenting the line. See the option
  4724. `org-cycle-emulate-tab' for details.
  4725. - Special case: if point is at the beginning of the buffer and there is
  4726. no headline in line 1, this function will act as if called with prefix arg.
  4727. But only if also the variable `org-cycle-global-at-bob' is t."
  4728. (interactive "P")
  4729. (org-load-modules-maybe)
  4730. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4731. (and org-cycle-level-after-item/entry-creation
  4732. (or (org-cycle-level)
  4733. (org-cycle-item-indentation))))
  4734. (let* ((limit-level
  4735. (or org-cycle-max-level
  4736. (and (boundp 'org-inlinetask-min-level)
  4737. org-inlinetask-min-level
  4738. (1- org-inlinetask-min-level))))
  4739. (nstars (and limit-level
  4740. (if org-odd-levels-only
  4741. (and limit-level (1- (* limit-level 2)))
  4742. limit-level)))
  4743. (outline-regexp
  4744. (cond
  4745. ((not (org-mode-p)) outline-regexp)
  4746. ((or (eq org-cycle-include-plain-lists 'integrate)
  4747. (and org-cycle-include-plain-lists (org-at-item-p)))
  4748. (concat "\\(?:\\*"
  4749. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4750. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4751. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4752. (bob-special (and org-cycle-global-at-bob (bobp)
  4753. (not (looking-at outline-regexp))))
  4754. (org-cycle-hook
  4755. (if bob-special
  4756. (delq 'org-optimize-window-after-visibility-change
  4757. (copy-sequence org-cycle-hook))
  4758. org-cycle-hook))
  4759. (pos (point)))
  4760. (if (or bob-special (equal arg '(4)))
  4761. ;; special case: use global cycling
  4762. (setq arg t))
  4763. (cond
  4764. ((equal arg '(16))
  4765. (org-set-startup-visibility)
  4766. (message "Startup visibility, plus VISIBILITY properties"))
  4767. ((equal arg '(64))
  4768. (show-all)
  4769. (message "Entire buffer visible, including drawers"))
  4770. ((org-at-table-p 'any)
  4771. ;; Enter the table or move to the next field in the table
  4772. (if (org-at-table.el-p)
  4773. (message "Use C-c ' to edit table.el tables")
  4774. (if arg (org-table-edit-field t)
  4775. (org-table-justify-field-maybe)
  4776. (call-interactively 'org-table-next-field))))
  4777. ((run-hook-with-args-until-success
  4778. 'org-tab-after-check-for-table-hook))
  4779. ((eq arg t) ;; Global cycling
  4780. (org-cycle-internal-global))
  4781. ((and org-drawers org-drawer-regexp
  4782. (save-excursion
  4783. (beginning-of-line 1)
  4784. (looking-at org-drawer-regexp)))
  4785. ;; Toggle block visibility
  4786. (org-flag-drawer
  4787. (not (get-char-property (match-end 0) 'invisible))))
  4788. ((integerp arg)
  4789. ;; Show-subtree, ARG levels up from here.
  4790. (save-excursion
  4791. (org-back-to-heading)
  4792. (outline-up-heading (if (< arg 0) (- arg)
  4793. (- (funcall outline-level) arg)))
  4794. (org-show-subtree)))
  4795. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4796. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4797. (org-cycle-internal-local))
  4798. ;; TAB emulation and template completion
  4799. (buffer-read-only (org-back-to-heading))
  4800. ((run-hook-with-args-until-success
  4801. 'org-tab-after-check-for-cycling-hook))
  4802. ((org-try-structure-completion))
  4803. ((org-try-cdlatex-tab))
  4804. ((run-hook-with-args-until-success
  4805. 'org-tab-before-tab-emulation-hook))
  4806. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4807. (or (not (bolp))
  4808. (not (looking-at outline-regexp))))
  4809. (call-interactively (global-key-binding "\t")))
  4810. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4811. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4812. (or (and (eq org-cycle-emulate-tab 'white)
  4813. (= (match-end 0) (point-at-eol)))
  4814. (and (eq org-cycle-emulate-tab 'whitestart)
  4815. (>= (match-end 0) pos))))
  4816. t
  4817. (eq org-cycle-emulate-tab t))
  4818. (call-interactively (global-key-binding "\t")))
  4819. (t (save-excursion
  4820. (org-back-to-heading)
  4821. (org-cycle)))))))
  4822. (defun org-cycle-internal-global ()
  4823. "Do the global cycling action."
  4824. (cond
  4825. ((and (eq last-command this-command)
  4826. (eq org-cycle-global-status 'overview))
  4827. ;; We just created the overview - now do table of contents
  4828. ;; This can be slow in very large buffers, so indicate action
  4829. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4830. (message "CONTENTS...")
  4831. (org-content)
  4832. (message "CONTENTS...done")
  4833. (setq org-cycle-global-status 'contents)
  4834. (run-hook-with-args 'org-cycle-hook 'contents))
  4835. ((and (eq last-command this-command)
  4836. (eq org-cycle-global-status 'contents))
  4837. ;; We just showed the table of contents - now show everything
  4838. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4839. (show-all)
  4840. (message "SHOW ALL")
  4841. (setq org-cycle-global-status 'all)
  4842. (run-hook-with-args 'org-cycle-hook 'all))
  4843. (t
  4844. ;; Default action: go to overview
  4845. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4846. (org-overview)
  4847. (message "OVERVIEW")
  4848. (setq org-cycle-global-status 'overview)
  4849. (run-hook-with-args 'org-cycle-hook 'overview))))
  4850. (defun org-cycle-internal-local ()
  4851. "Do the local cycling action."
  4852. (org-back-to-heading)
  4853. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4854. ;; First, some boundaries
  4855. (save-excursion
  4856. (org-back-to-heading)
  4857. (setq level (funcall outline-level))
  4858. (save-excursion
  4859. (beginning-of-line 2)
  4860. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4861. ; XEmacs does not have `next-single-char-property-change'
  4862. ; I'm not sure about Emacs 21.
  4863. (while (and (not (eobp)) ;; this is like `next-line'
  4864. (get-char-property (1- (point)) 'invisible))
  4865. (beginning-of-line 2))
  4866. (while (and (not (eobp)) ;; this is like `next-line'
  4867. (get-char-property (1- (point)) 'invisible))
  4868. (goto-char (next-single-char-property-change (point) 'invisible))
  4869. ;;;??? (or (bolp) (beginning-of-line 2))))
  4870. (and (eolp) (beginning-of-line 2))))
  4871. (setq eol (point)))
  4872. (outline-end-of-heading) (setq eoh (point))
  4873. (save-excursion
  4874. (outline-next-heading)
  4875. (setq has-children (and (org-at-heading-p t)
  4876. (> (funcall outline-level) level))))
  4877. (org-end-of-subtree t)
  4878. (unless (eobp)
  4879. (skip-chars-forward " \t\n")
  4880. (beginning-of-line 1) ; in case this is an item
  4881. )
  4882. (setq eos (if (eobp) (point) (1- (point)))))
  4883. ;; Find out what to do next and set `this-command'
  4884. (cond
  4885. ((= eos eoh)
  4886. ;; Nothing is hidden behind this heading
  4887. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4888. (message "EMPTY ENTRY")
  4889. (setq org-cycle-subtree-status nil)
  4890. (save-excursion
  4891. (goto-char eos)
  4892. (outline-next-heading)
  4893. (if (org-invisible-p) (org-flag-heading nil))))
  4894. ((and (or (>= eol eos)
  4895. (not (string-match "\\S-" (buffer-substring eol eos))))
  4896. (or has-children
  4897. (not (setq children-skipped
  4898. org-cycle-skip-children-state-if-no-children))))
  4899. ;; Entire subtree is hidden in one line: children view
  4900. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4901. (org-show-entry)
  4902. (show-children)
  4903. (message "CHILDREN")
  4904. (save-excursion
  4905. (goto-char eos)
  4906. (outline-next-heading)
  4907. (if (org-invisible-p) (org-flag-heading nil)))
  4908. (setq org-cycle-subtree-status 'children)
  4909. (run-hook-with-args 'org-cycle-hook 'children))
  4910. ((or children-skipped
  4911. (and (eq last-command this-command)
  4912. (eq org-cycle-subtree-status 'children)))
  4913. ;; We just showed the children, or no children are there,
  4914. ;; now show everything.
  4915. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4916. (org-show-subtree)
  4917. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4918. (setq org-cycle-subtree-status 'subtree)
  4919. (run-hook-with-args 'org-cycle-hook 'subtree))
  4920. (t
  4921. ;; Default action: hide the subtree.
  4922. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4923. (hide-subtree)
  4924. (message "FOLDED")
  4925. (setq org-cycle-subtree-status 'folded)
  4926. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4927. ;;;###autoload
  4928. (defun org-global-cycle (&optional arg)
  4929. "Cycle the global visibility. For details see `org-cycle'.
  4930. With C-u prefix arg, switch to startup visibility.
  4931. With a numeric prefix, show all headlines up to that level."
  4932. (interactive "P")
  4933. (let ((org-cycle-include-plain-lists
  4934. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4935. (cond
  4936. ((integerp arg)
  4937. (show-all)
  4938. (hide-sublevels arg)
  4939. (setq org-cycle-global-status 'contents))
  4940. ((equal arg '(4))
  4941. (org-set-startup-visibility)
  4942. (message "Startup visibility, plus VISIBILITY properties."))
  4943. (t
  4944. (org-cycle '(4))))))
  4945. (defun org-set-startup-visibility ()
  4946. "Set the visibility required by startup options and properties."
  4947. (cond
  4948. ((eq org-startup-folded t)
  4949. (org-cycle '(4)))
  4950. ((eq org-startup-folded 'content)
  4951. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4952. (org-cycle '(4)) (org-cycle '(4)))))
  4953. (unless (eq org-startup-folded 'showeverything)
  4954. (if org-hide-block-startup (org-hide-block-all))
  4955. (org-set-visibility-according-to-property 'no-cleanup)
  4956. (org-cycle-hide-archived-subtrees 'all)
  4957. (org-cycle-hide-drawers 'all)
  4958. (org-cycle-show-empty-lines 'all)))
  4959. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4960. "Switch subtree visibilities according to :VISIBILITY: property."
  4961. (interactive)
  4962. (let (org-show-entry-below state)
  4963. (save-excursion
  4964. (goto-char (point-min))
  4965. (while (re-search-forward
  4966. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4967. nil t)
  4968. (setq state (match-string 1))
  4969. (save-excursion
  4970. (org-back-to-heading t)
  4971. (hide-subtree)
  4972. (org-reveal)
  4973. (cond
  4974. ((equal state '("fold" "folded"))
  4975. (hide-subtree))
  4976. ((equal state "children")
  4977. (org-show-hidden-entry)
  4978. (show-children))
  4979. ((equal state "content")
  4980. (save-excursion
  4981. (save-restriction
  4982. (org-narrow-to-subtree)
  4983. (org-content))))
  4984. ((member state '("all" "showall"))
  4985. (show-subtree)))))
  4986. (unless no-cleanup
  4987. (org-cycle-hide-archived-subtrees 'all)
  4988. (org-cycle-hide-drawers 'all)
  4989. (org-cycle-show-empty-lines 'all)))))
  4990. (defun org-overview ()
  4991. "Switch to overview mode, showing only top-level headlines.
  4992. Really, this shows all headlines with level equal or greater than the level
  4993. of the first headline in the buffer. This is important, because if the
  4994. first headline is not level one, then (hide-sublevels 1) gives confusing
  4995. results."
  4996. (interactive)
  4997. (let ((level (save-excursion
  4998. (goto-char (point-min))
  4999. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5000. (progn
  5001. (goto-char (match-beginning 0))
  5002. (funcall outline-level))))))
  5003. (and level (hide-sublevels level))))
  5004. (defun org-content (&optional arg)
  5005. "Show all headlines in the buffer, like a table of contents.
  5006. With numerical argument N, show content up to level N."
  5007. (interactive "P")
  5008. (save-excursion
  5009. ;; Visit all headings and show their offspring
  5010. (and (integerp arg) (org-overview))
  5011. (goto-char (point-max))
  5012. (catch 'exit
  5013. (while (and (progn (condition-case nil
  5014. (outline-previous-visible-heading 1)
  5015. (error (goto-char (point-min))))
  5016. t)
  5017. (looking-at outline-regexp))
  5018. (if (integerp arg)
  5019. (show-children (1- arg))
  5020. (show-branches))
  5021. (if (bobp) (throw 'exit nil))))))
  5022. (defun org-optimize-window-after-visibility-change (state)
  5023. "Adjust the window after a change in outline visibility.
  5024. This function is the default value of the hook `org-cycle-hook'."
  5025. (when (get-buffer-window (current-buffer))
  5026. (cond
  5027. ((eq state 'content) nil)
  5028. ((eq state 'all) nil)
  5029. ((eq state 'folded) nil)
  5030. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5031. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5032. (defun org-remove-empty-overlays-at (pos)
  5033. "Remove outline overlays that do not contain non-white stuff."
  5034. (mapc
  5035. (lambda (o)
  5036. (and (eq 'outline (overlay-get o 'invisible))
  5037. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5038. (overlay-end o))))
  5039. (delete-overlay o)))
  5040. (overlays-at pos)))
  5041. (defun org-clean-visibility-after-subtree-move ()
  5042. "Fix visibility issues after moving a subtree."
  5043. ;; First, find a reasonable region to look at:
  5044. ;; Start two siblings above, end three below
  5045. (let* ((beg (save-excursion
  5046. (and (org-get-last-sibling)
  5047. (org-get-last-sibling))
  5048. (point)))
  5049. (end (save-excursion
  5050. (and (org-get-next-sibling)
  5051. (org-get-next-sibling)
  5052. (org-get-next-sibling))
  5053. (if (org-at-heading-p)
  5054. (point-at-eol)
  5055. (point))))
  5056. (level (looking-at "\\*+"))
  5057. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5058. (save-excursion
  5059. (save-restriction
  5060. (narrow-to-region beg end)
  5061. (when re
  5062. ;; Properly fold already folded siblings
  5063. (goto-char (point-min))
  5064. (while (re-search-forward re nil t)
  5065. (if (and (not (org-invisible-p))
  5066. (save-excursion
  5067. (goto-char (point-at-eol)) (org-invisible-p)))
  5068. (hide-entry))))
  5069. (org-cycle-show-empty-lines 'overview)
  5070. (org-cycle-hide-drawers 'overview)))))
  5071. (defun org-cycle-show-empty-lines (state)
  5072. "Show empty lines above all visible headlines.
  5073. The region to be covered depends on STATE when called through
  5074. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5075. entire buffer covered. Note that an empty line is only shown if there
  5076. are at least `org-cycle-separator-lines' empty lines before the headline."
  5077. (when (not (= org-cycle-separator-lines 0))
  5078. (save-excursion
  5079. (let* ((n (abs org-cycle-separator-lines))
  5080. (re (cond
  5081. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5082. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5083. (t (let ((ns (number-to-string (- n 2))))
  5084. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5085. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5086. beg end b e)
  5087. (cond
  5088. ((memq state '(overview contents t))
  5089. (setq beg (point-min) end (point-max)))
  5090. ((memq state '(children folded))
  5091. (setq beg (point) end (progn (org-end-of-subtree t t)
  5092. (beginning-of-line 2)
  5093. (point)))))
  5094. (when beg
  5095. (goto-char beg)
  5096. (while (re-search-forward re end t)
  5097. (unless (get-char-property (match-end 1) 'invisible)
  5098. (setq e (match-end 1))
  5099. (if (< org-cycle-separator-lines 0)
  5100. (setq b (save-excursion
  5101. (goto-char (match-beginning 0))
  5102. (org-back-over-empty-lines)
  5103. (if (save-excursion
  5104. (goto-char (max (point-min) (1- (point))))
  5105. (org-on-heading-p))
  5106. (1- (point))
  5107. (point))))
  5108. (setq b (match-beginning 1)))
  5109. (outline-flag-region b e nil)))))))
  5110. ;; Never hide empty lines at the end of the file.
  5111. (save-excursion
  5112. (goto-char (point-max))
  5113. (outline-previous-heading)
  5114. (outline-end-of-heading)
  5115. (if (and (looking-at "[ \t\n]+")
  5116. (= (match-end 0) (point-max)))
  5117. (outline-flag-region (point) (match-end 0) nil))))
  5118. (defun org-show-empty-lines-in-parent ()
  5119. "Move to the parent and re-show empty lines before visible headlines."
  5120. (save-excursion
  5121. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5122. (org-cycle-show-empty-lines context))))
  5123. (defun org-files-list ()
  5124. "Return `org-agenda-files' list, plus all open org-mode files.
  5125. This is useful for operations that need to scan all of a user's
  5126. open and agenda-wise Org files."
  5127. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5128. (dolist (buf (buffer-list))
  5129. (with-current-buffer buf
  5130. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5131. (let ((file (expand-file-name (buffer-file-name))))
  5132. (unless (member file files)
  5133. (push file files))))))
  5134. files))
  5135. (defsubst org-entry-beginning-position ()
  5136. "Return the beginning position of the current entry."
  5137. (save-excursion (outline-back-to-heading t) (point)))
  5138. (defsubst org-entry-end-position ()
  5139. "Return the end position of the current entry."
  5140. (save-excursion (outline-next-heading) (point)))
  5141. (defun org-cycle-hide-drawers (state)
  5142. "Re-hide all drawers after a visibility state change."
  5143. (when (and (org-mode-p)
  5144. (not (memq state '(overview folded contents))))
  5145. (save-excursion
  5146. (let* ((globalp (memq state '(contents all)))
  5147. (beg (if globalp (point-min) (point)))
  5148. (end (if globalp (point-max)
  5149. (if (eq state 'children)
  5150. (save-excursion (outline-next-heading) (point))
  5151. (org-end-of-subtree t)))))
  5152. (goto-char beg)
  5153. (while (re-search-forward org-drawer-regexp end t)
  5154. (org-flag-drawer t))))))
  5155. (defun org-flag-drawer (flag)
  5156. (save-excursion
  5157. (beginning-of-line 1)
  5158. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5159. (let ((b (match-end 0))
  5160. (outline-regexp org-outline-regexp))
  5161. (if (re-search-forward
  5162. "^[ \t]*:END:"
  5163. (save-excursion (outline-next-heading) (point)) t)
  5164. (outline-flag-region b (point-at-eol) flag)
  5165. (error ":END: line missing at position %s" b))))))
  5166. (defun org-subtree-end-visible-p ()
  5167. "Is the end of the current subtree visible?"
  5168. (pos-visible-in-window-p
  5169. (save-excursion (org-end-of-subtree t) (point))))
  5170. (defun org-first-headline-recenter (&optional N)
  5171. "Move cursor to the first headline and recenter the headline.
  5172. Optional argument N means put the headline into the Nth line of the window."
  5173. (goto-char (point-min))
  5174. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5175. (beginning-of-line)
  5176. (recenter (prefix-numeric-value N))))
  5177. ;;; Saving and restoring visibility
  5178. (defun org-outline-overlay-data (&optional use-markers)
  5179. "Return a list of the locations of all outline overlays.
  5180. The are overlays with the `invisible' property value `outline'.
  5181. The return valus is a list of cons cells, with start and stop
  5182. positions for each overlay.
  5183. If USE-MARKERS is set, return the positions as markers."
  5184. (let (beg end)
  5185. (save-excursion
  5186. (save-restriction
  5187. (widen)
  5188. (delq nil
  5189. (mapcar (lambda (o)
  5190. (when (eq (overlay-get o 'invisible) 'outline)
  5191. (setq beg (overlay-start o)
  5192. end (overlay-end o))
  5193. (and beg end (> end beg)
  5194. (if use-markers
  5195. (cons (move-marker (make-marker) beg)
  5196. (move-marker (make-marker) end))
  5197. (cons beg end)))))
  5198. (overlays-in (point-min) (point-max))))))))
  5199. (defun org-set-outline-overlay-data (data)
  5200. "Create visibility overlays for all positions in DATA.
  5201. DATA should have been made by `org-outline-overlay-data'."
  5202. (let (o)
  5203. (save-excursion
  5204. (save-restriction
  5205. (widen)
  5206. (show-all)
  5207. (mapc (lambda (c)
  5208. (setq o (make-overlay (car c) (cdr c)))
  5209. (overlay-put o 'invisible 'outline))
  5210. data)))))
  5211. (defmacro org-save-outline-visibility (use-markers &rest body)
  5212. "Save and restore outline visibility around BODY.
  5213. If USE-MARKERS is non-nil, use markers for the positions.
  5214. This means that the buffer may change while running BODY,
  5215. but it also means that the buffer should stay alive
  5216. during the operation, because otherwise all these markers will
  5217. point nowhere."
  5218. `(let ((data (org-outline-overlay-data ,use-markers)))
  5219. (unwind-protect
  5220. (progn
  5221. ,@body
  5222. (org-set-outline-overlay-data data))
  5223. (when ,use-markers
  5224. (mapc (lambda (c)
  5225. (and (markerp (car c)) (move-marker (car c) nil))
  5226. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5227. data)))))
  5228. ;;; Folding of blocks
  5229. (defconst org-block-regexp
  5230. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5231. "Regular expression for hiding blocks.")
  5232. (defvar org-hide-block-overlays nil
  5233. "Overlays hiding blocks.")
  5234. (make-variable-buffer-local 'org-hide-block-overlays)
  5235. (defun org-block-map (function &optional start end)
  5236. "Call func at the head of all source blocks in the current
  5237. buffer. Optional arguments START and END can be used to limit
  5238. the range."
  5239. (let ((start (or start (point-min)))
  5240. (end (or end (point-max))))
  5241. (save-excursion
  5242. (goto-char start)
  5243. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5244. (save-excursion
  5245. (save-match-data
  5246. (goto-char (match-beginning 0))
  5247. (funcall function)))))))
  5248. (defun org-hide-block-toggle-all ()
  5249. "Toggle the visibility of all blocks in the current buffer."
  5250. (org-block-map #'org-hide-block-toggle))
  5251. (defun org-hide-block-all ()
  5252. "Fold all blocks in the current buffer."
  5253. (interactive)
  5254. (org-show-block-all)
  5255. (org-block-map #'org-hide-block-toggle-maybe))
  5256. (defun org-show-block-all ()
  5257. "Unfold all blocks in the current buffer."
  5258. (mapc 'delete-overlay org-hide-block-overlays)
  5259. (setq org-hide-block-overlays nil))
  5260. (defun org-hide-block-toggle-maybe ()
  5261. "Toggle visibility of block at point."
  5262. (interactive)
  5263. (let ((case-fold-search t))
  5264. (if (save-excursion
  5265. (beginning-of-line 1)
  5266. (looking-at org-block-regexp))
  5267. (progn (org-hide-block-toggle)
  5268. t) ;; to signal that we took action
  5269. nil))) ;; to signal that we did not
  5270. (defun org-hide-block-toggle (&optional force)
  5271. "Toggle the visibility of the current block."
  5272. (interactive)
  5273. (save-excursion
  5274. (beginning-of-line)
  5275. (if (re-search-forward org-block-regexp nil t)
  5276. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5277. (end (match-end 0)) ;; end of entire body
  5278. ov)
  5279. (if (memq t (mapcar (lambda (overlay)
  5280. (eq (overlay-get overlay 'invisible)
  5281. 'org-hide-block))
  5282. (overlays-at start)))
  5283. (if (or (not force) (eq force 'off))
  5284. (mapc (lambda (ov)
  5285. (when (member ov org-hide-block-overlays)
  5286. (setq org-hide-block-overlays
  5287. (delq ov org-hide-block-overlays)))
  5288. (when (eq (overlay-get ov 'invisible)
  5289. 'org-hide-block)
  5290. (delete-overlay ov)))
  5291. (overlays-at start)))
  5292. (setq ov (make-overlay start end))
  5293. (overlay-put ov 'invisible 'org-hide-block)
  5294. ;; make the block accessible to isearch
  5295. (overlay-put
  5296. ov 'isearch-open-invisible
  5297. (lambda (ov)
  5298. (when (member ov org-hide-block-overlays)
  5299. (setq org-hide-block-overlays
  5300. (delq ov org-hide-block-overlays)))
  5301. (when (eq (overlay-get ov 'invisible)
  5302. 'org-hide-block)
  5303. (delete-overlay ov))))
  5304. (push ov org-hide-block-overlays)))
  5305. (error "Not looking at a source block"))))
  5306. ;; org-tab-after-check-for-cycling-hook
  5307. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5308. ;; Remove overlays when changing major mode
  5309. (add-hook 'org-mode-hook
  5310. (lambda () (org-add-hook 'change-major-mode-hook
  5311. 'org-show-block-all 'append 'local)))
  5312. ;;; Org-goto
  5313. (defvar org-goto-window-configuration nil)
  5314. (defvar org-goto-marker nil)
  5315. (defvar org-goto-map
  5316. (let ((map (make-sparse-keymap)))
  5317. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5318. (while (setq cmd (pop cmds))
  5319. (substitute-key-definition cmd cmd map global-map)))
  5320. (suppress-keymap map)
  5321. (org-defkey map "\C-m" 'org-goto-ret)
  5322. (org-defkey map [(return)] 'org-goto-ret)
  5323. (org-defkey map [(left)] 'org-goto-left)
  5324. (org-defkey map [(right)] 'org-goto-right)
  5325. (org-defkey map [(control ?g)] 'org-goto-quit)
  5326. (org-defkey map "\C-i" 'org-cycle)
  5327. (org-defkey map [(tab)] 'org-cycle)
  5328. (org-defkey map [(down)] 'outline-next-visible-heading)
  5329. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5330. (if org-goto-auto-isearch
  5331. (if (fboundp 'define-key-after)
  5332. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5333. nil)
  5334. (org-defkey map "q" 'org-goto-quit)
  5335. (org-defkey map "n" 'outline-next-visible-heading)
  5336. (org-defkey map "p" 'outline-previous-visible-heading)
  5337. (org-defkey map "f" 'outline-forward-same-level)
  5338. (org-defkey map "b" 'outline-backward-same-level)
  5339. (org-defkey map "u" 'outline-up-heading))
  5340. (org-defkey map "/" 'org-occur)
  5341. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5342. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5343. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5344. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5345. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5346. map))
  5347. (defconst org-goto-help
  5348. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5349. RET=jump to location [Q]uit and return to previous location
  5350. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5351. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5352. ;; FIXME: Docstring does not mention both interfaces
  5353. (defun org-goto (&optional alternative-interface)
  5354. "Look up a different location in the current file, keeping current visibility.
  5355. When you want look-up or go to a different location in a document, the
  5356. fastest way is often to fold the entire buffer and then dive into the tree.
  5357. This method has the disadvantage, that the previous location will be folded,
  5358. which may not be what you want.
  5359. This command works around this by showing a copy of the current buffer
  5360. in an indirect buffer, in overview mode. You can dive into the tree in
  5361. that copy, use org-occur and incremental search to find a location.
  5362. When pressing RET or `Q', the command returns to the original buffer in
  5363. which the visibility is still unchanged. After RET is will also jump to
  5364. the location selected in the indirect buffer and expose the
  5365. the headline hierarchy above."
  5366. (interactive "P")
  5367. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5368. (org-refile-use-outline-path t)
  5369. (org-refile-target-verify-function nil)
  5370. (interface
  5371. (if (not alternative-interface)
  5372. org-goto-interface
  5373. (if (eq org-goto-interface 'outline)
  5374. 'outline-path-completion
  5375. 'outline)))
  5376. (org-goto-start-pos (point))
  5377. (selected-point
  5378. (if (eq interface 'outline)
  5379. (car (org-get-location (current-buffer) org-goto-help))
  5380. (nth 3 (org-refile-get-location "Goto: ")))))
  5381. (if selected-point
  5382. (progn
  5383. (org-mark-ring-push org-goto-start-pos)
  5384. (goto-char selected-point)
  5385. (if (or (org-invisible-p) (org-invisible-p2))
  5386. (org-show-context 'org-goto)))
  5387. (message "Quit"))))
  5388. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5389. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5390. (defvar org-goto-local-auto-isearch-map) ; defined below
  5391. (defun org-get-location (buf help)
  5392. "Let the user select a location in the Org-mode buffer BUF.
  5393. This function uses a recursive edit. It returns the selected position
  5394. or nil."
  5395. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5396. (isearch-hide-immediately nil)
  5397. (isearch-search-fun-function
  5398. (lambda () 'org-goto-local-search-headings))
  5399. (org-goto-selected-point org-goto-exit-command)
  5400. (pop-up-frames nil)
  5401. (special-display-buffer-names nil)
  5402. (special-display-regexps nil)
  5403. (special-display-function nil))
  5404. (save-excursion
  5405. (save-window-excursion
  5406. (delete-other-windows)
  5407. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5408. (switch-to-buffer
  5409. (condition-case nil
  5410. (make-indirect-buffer (current-buffer) "*org-goto*")
  5411. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5412. (with-output-to-temp-buffer "*Help*"
  5413. (princ help))
  5414. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5415. (setq buffer-read-only nil)
  5416. (let ((org-startup-truncated t)
  5417. (org-startup-folded nil)
  5418. (org-startup-align-all-tables nil))
  5419. (org-mode)
  5420. (org-overview))
  5421. (setq buffer-read-only t)
  5422. (if (and (boundp 'org-goto-start-pos)
  5423. (integer-or-marker-p org-goto-start-pos))
  5424. (let ((org-show-hierarchy-above t)
  5425. (org-show-siblings t)
  5426. (org-show-following-heading t))
  5427. (goto-char org-goto-start-pos)
  5428. (and (org-invisible-p) (org-show-context)))
  5429. (goto-char (point-min)))
  5430. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5431. (message "Select location and press RET")
  5432. (use-local-map org-goto-map)
  5433. (recursive-edit)
  5434. ))
  5435. (kill-buffer "*org-goto*")
  5436. (cons org-goto-selected-point org-goto-exit-command)))
  5437. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5438. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5439. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5440. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5441. (defun org-goto-local-search-headings (string bound noerror)
  5442. "Search and make sure that any matches are in headlines."
  5443. (catch 'return
  5444. (while (if isearch-forward
  5445. (search-forward string bound noerror)
  5446. (search-backward string bound noerror))
  5447. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5448. (and (member :headline context)
  5449. (not (member :tags context))))
  5450. (throw 'return (point))))))
  5451. (defun org-goto-local-auto-isearch ()
  5452. "Start isearch."
  5453. (interactive)
  5454. (goto-char (point-min))
  5455. (let ((keys (this-command-keys)))
  5456. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5457. (isearch-mode t)
  5458. (isearch-process-search-char (string-to-char keys)))))
  5459. (defun org-goto-ret (&optional arg)
  5460. "Finish `org-goto' by going to the new location."
  5461. (interactive "P")
  5462. (setq org-goto-selected-point (point)
  5463. org-goto-exit-command 'return)
  5464. (throw 'exit nil))
  5465. (defun org-goto-left ()
  5466. "Finish `org-goto' by going to the new location."
  5467. (interactive)
  5468. (if (org-on-heading-p)
  5469. (progn
  5470. (beginning-of-line 1)
  5471. (setq org-goto-selected-point (point)
  5472. org-goto-exit-command 'left)
  5473. (throw 'exit nil))
  5474. (error "Not on a heading")))
  5475. (defun org-goto-right ()
  5476. "Finish `org-goto' by going to the new location."
  5477. (interactive)
  5478. (if (org-on-heading-p)
  5479. (progn
  5480. (setq org-goto-selected-point (point)
  5481. org-goto-exit-command 'right)
  5482. (throw 'exit nil))
  5483. (error "Not on a heading")))
  5484. (defun org-goto-quit ()
  5485. "Finish `org-goto' without cursor motion."
  5486. (interactive)
  5487. (setq org-goto-selected-point nil)
  5488. (setq org-goto-exit-command 'quit)
  5489. (throw 'exit nil))
  5490. ;;; Indirect buffer display of subtrees
  5491. (defvar org-indirect-dedicated-frame nil
  5492. "This is the frame being used for indirect tree display.")
  5493. (defvar org-last-indirect-buffer nil)
  5494. (defun org-tree-to-indirect-buffer (&optional arg)
  5495. "Create indirect buffer and narrow it to current subtree.
  5496. With numerical prefix ARG, go up to this level and then take that tree.
  5497. If ARG is negative, go up that many levels.
  5498. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5499. indirect buffer previously made with this command, to avoid proliferation of
  5500. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5501. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5502. is kept so that you can work with several indirect buffers at the same time.
  5503. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5504. requests that a new frame be made for the new buffer, so that the dedicated
  5505. frame is not changed."
  5506. (interactive "P")
  5507. (let ((cbuf (current-buffer))
  5508. (cwin (selected-window))
  5509. (pos (point))
  5510. beg end level heading ibuf)
  5511. (save-excursion
  5512. (org-back-to-heading t)
  5513. (when (numberp arg)
  5514. (setq level (org-outline-level))
  5515. (if (< arg 0) (setq arg (+ level arg)))
  5516. (while (> (setq level (org-outline-level)) arg)
  5517. (outline-up-heading 1 t)))
  5518. (setq beg (point)
  5519. heading (org-get-heading))
  5520. (org-end-of-subtree t t)
  5521. (if (org-on-heading-p) (backward-char 1))
  5522. (setq end (point)))
  5523. (if (and (buffer-live-p org-last-indirect-buffer)
  5524. (not (eq org-indirect-buffer-display 'new-frame))
  5525. (not arg))
  5526. (kill-buffer org-last-indirect-buffer))
  5527. (setq ibuf (org-get-indirect-buffer cbuf)
  5528. org-last-indirect-buffer ibuf)
  5529. (cond
  5530. ((or (eq org-indirect-buffer-display 'new-frame)
  5531. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5532. (select-frame (make-frame))
  5533. (delete-other-windows)
  5534. (switch-to-buffer ibuf)
  5535. (org-set-frame-title heading))
  5536. ((eq org-indirect-buffer-display 'dedicated-frame)
  5537. (raise-frame
  5538. (select-frame (or (and org-indirect-dedicated-frame
  5539. (frame-live-p org-indirect-dedicated-frame)
  5540. org-indirect-dedicated-frame)
  5541. (setq org-indirect-dedicated-frame (make-frame)))))
  5542. (delete-other-windows)
  5543. (switch-to-buffer ibuf)
  5544. (org-set-frame-title (concat "Indirect: " heading)))
  5545. ((eq org-indirect-buffer-display 'current-window)
  5546. (switch-to-buffer ibuf))
  5547. ((eq org-indirect-buffer-display 'other-window)
  5548. (pop-to-buffer ibuf))
  5549. (t (error "Invalid value")))
  5550. (if (featurep 'xemacs)
  5551. (save-excursion (org-mode) (turn-on-font-lock)))
  5552. (narrow-to-region beg end)
  5553. (show-all)
  5554. (goto-char pos)
  5555. (and (window-live-p cwin) (select-window cwin))))
  5556. (defun org-get-indirect-buffer (&optional buffer)
  5557. (setq buffer (or buffer (current-buffer)))
  5558. (let ((n 1) (base (buffer-name buffer)) bname)
  5559. (while (buffer-live-p
  5560. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5561. (setq n (1+ n)))
  5562. (condition-case nil
  5563. (make-indirect-buffer buffer bname 'clone)
  5564. (error (make-indirect-buffer buffer bname)))))
  5565. (defun org-set-frame-title (title)
  5566. "Set the title of the current frame to the string TITLE."
  5567. ;; FIXME: how to name a single frame in XEmacs???
  5568. (unless (featurep 'xemacs)
  5569. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5570. ;;;; Structure editing
  5571. ;;; Inserting headlines
  5572. (defun org-previous-line-empty-p ()
  5573. (save-excursion
  5574. (and (not (bobp))
  5575. (or (beginning-of-line 0) t)
  5576. (save-match-data
  5577. (looking-at "[ \t]*$")))))
  5578. (defun org-insert-heading (&optional force-heading invisible-ok)
  5579. "Insert a new heading or item with same depth at point.
  5580. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5581. If point is at the beginning of a headline, insert a sibling before the
  5582. current headline. If point is not at the beginning, do not split the line,
  5583. but create the new headline after the current line.
  5584. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5585. This is important for non-interactive uses of the command."
  5586. (interactive "P")
  5587. (if (or (= (buffer-size) 0)
  5588. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5589. (org-on-heading-p))))
  5590. (not (org-in-item-p))))
  5591. (insert "\n* ")
  5592. (when (or force-heading (not (org-insert-item)))
  5593. (let* ((empty-line-p nil)
  5594. (head (save-excursion
  5595. (condition-case nil
  5596. (progn
  5597. (org-back-to-heading invisible-ok)
  5598. (setq empty-line-p (org-previous-line-empty-p))
  5599. (match-string 0))
  5600. (error "*"))))
  5601. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5602. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5603. pos hide-previous previous-pos)
  5604. (cond
  5605. ((and (org-on-heading-p) (bolp)
  5606. (or (bobp)
  5607. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5608. ;; insert before the current line
  5609. (open-line (if blank 2 1)))
  5610. ((and (bolp)
  5611. (not org-insert-heading-respect-content)
  5612. (or (bobp)
  5613. (save-excursion
  5614. (backward-char 1) (not (org-invisible-p)))))
  5615. ;; insert right here
  5616. nil)
  5617. (t
  5618. ;; somewhere in the line
  5619. (save-excursion
  5620. (setq previous-pos (point-at-bol))
  5621. (end-of-line)
  5622. (setq hide-previous (org-invisible-p)))
  5623. (and org-insert-heading-respect-content (org-show-subtree))
  5624. (let ((split
  5625. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5626. (save-excursion
  5627. (let ((p (point)))
  5628. (goto-char (point-at-bol))
  5629. (and (looking-at org-complex-heading-regexp)
  5630. (> p (match-beginning 4)))))))
  5631. tags pos)
  5632. (cond
  5633. (org-insert-heading-respect-content
  5634. (org-end-of-subtree nil t)
  5635. (or (bolp) (newline))
  5636. (or (org-previous-line-empty-p)
  5637. (and blank (newline)))
  5638. (open-line 1))
  5639. ((org-on-heading-p)
  5640. (when hide-previous
  5641. (show-children)
  5642. (org-show-entry))
  5643. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5644. (setq tags (and (match-end 2) (match-string 2)))
  5645. (and (match-end 1)
  5646. (delete-region (match-beginning 1) (match-end 1)))
  5647. (setq pos (point-at-bol))
  5648. (or split (end-of-line 1))
  5649. (delete-horizontal-space)
  5650. (if (string-match "\\`\\*+\\'"
  5651. (buffer-substring (point-at-bol) (point)))
  5652. (insert " "))
  5653. (newline (if blank 2 1))
  5654. (when tags
  5655. (save-excursion
  5656. (goto-char pos)
  5657. (end-of-line 1)
  5658. (insert " " tags)
  5659. (org-set-tags nil 'align))))
  5660. (t
  5661. (or split (end-of-line 1))
  5662. (newline (if blank 2 1)))))))
  5663. (insert head) (just-one-space)
  5664. (setq pos (point))
  5665. (end-of-line 1)
  5666. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5667. (when (and org-insert-heading-respect-content hide-previous)
  5668. (save-excursion
  5669. (goto-char previous-pos)
  5670. (hide-subtree)))
  5671. (run-hooks 'org-insert-heading-hook)))))
  5672. (defun org-get-heading (&optional no-tags)
  5673. "Return the heading of the current entry, without the stars."
  5674. (save-excursion
  5675. (org-back-to-heading t)
  5676. (if (looking-at
  5677. (if no-tags
  5678. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5679. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5680. (match-string 1) "")))
  5681. (defun org-heading-components ()
  5682. "Return the components of the current heading.
  5683. This is a list with the following elements:
  5684. - the level as an integer
  5685. - the reduced level, different if `org-odd-levels-only' is set.
  5686. - the TODO keyword, or nil
  5687. - the priority character, like ?A, or nil if no priority is given
  5688. - the headline text itself, or the tags string if no headline text
  5689. - the tags string, or nil."
  5690. (save-excursion
  5691. (org-back-to-heading t)
  5692. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5693. (list (length (match-string 1))
  5694. (org-reduced-level (length (match-string 1)))
  5695. (org-match-string-no-properties 2)
  5696. (and (match-end 3) (aref (match-string 3) 2))
  5697. (org-match-string-no-properties 4)
  5698. (org-match-string-no-properties 5)))))
  5699. (defun org-get-entry ()
  5700. "Get the entry text, after heading, entire subtree."
  5701. (save-excursion
  5702. (org-back-to-heading t)
  5703. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5704. (defun org-insert-heading-after-current ()
  5705. "Insert a new heading with same level as current, after current subtree."
  5706. (interactive)
  5707. (org-back-to-heading)
  5708. (org-insert-heading)
  5709. (org-move-subtree-down)
  5710. (end-of-line 1))
  5711. (defun org-insert-heading-respect-content ()
  5712. (interactive)
  5713. (let ((org-insert-heading-respect-content t))
  5714. (org-insert-heading t)))
  5715. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5716. (interactive "P")
  5717. (let ((org-insert-heading-respect-content t))
  5718. (org-insert-todo-heading force-state t)))
  5719. (defun org-insert-todo-heading (arg &optional force-heading)
  5720. "Insert a new heading with the same level and TODO state as current heading.
  5721. If the heading has no TODO state, or if the state is DONE, use the first
  5722. state (TODO by default). Also with prefix arg, force first state."
  5723. (interactive "P")
  5724. (when (or force-heading (not (org-insert-item 'checkbox)))
  5725. (org-insert-heading force-heading)
  5726. (save-excursion
  5727. (org-back-to-heading)
  5728. (outline-previous-heading)
  5729. (looking-at org-todo-line-regexp))
  5730. (let*
  5731. ((new-mark-x
  5732. (if (or arg
  5733. (not (match-beginning 2))
  5734. (member (match-string 2) org-done-keywords))
  5735. (car org-todo-keywords-1)
  5736. (match-string 2)))
  5737. (new-mark
  5738. (or
  5739. (run-hook-with-args-until-success
  5740. 'org-todo-get-default-hook new-mark-x nil)
  5741. new-mark-x)))
  5742. (beginning-of-line 1)
  5743. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5744. (if org-treat-insert-todo-heading-as-state-change
  5745. (org-todo new-mark)
  5746. (insert new-mark " "))))
  5747. (when org-provide-todo-statistics
  5748. (org-update-parent-todo-statistics))))
  5749. (defun org-insert-subheading (arg)
  5750. "Insert a new subheading and demote it.
  5751. Works for outline headings and for plain lists alike."
  5752. (interactive "P")
  5753. (org-insert-heading arg)
  5754. (cond
  5755. ((org-on-heading-p) (org-do-demote))
  5756. ((org-at-item-p) (org-indent-item 1))))
  5757. (defun org-insert-todo-subheading (arg)
  5758. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5759. Works for outline headings and for plain lists alike."
  5760. (interactive "P")
  5761. (org-insert-todo-heading arg)
  5762. (cond
  5763. ((org-on-heading-p) (org-do-demote))
  5764. ((org-at-item-p) (org-indent-item 1))))
  5765. ;;; Promotion and Demotion
  5766. (defvar org-after-demote-entry-hook nil
  5767. "Hook run after an entry has been demoted.
  5768. The cursor will be at the beginning of the entry.
  5769. When a subtree is being demoted, the hook will be called for each node.")
  5770. (defvar org-after-promote-entry-hook nil
  5771. "Hook run after an entry has been promoted.
  5772. The cursor will be at the beginning of the entry.
  5773. When a subtree is being promoted, the hook will be called for each node.")
  5774. (defun org-promote-subtree ()
  5775. "Promote the entire subtree.
  5776. See also `org-promote'."
  5777. (interactive)
  5778. (save-excursion
  5779. (org-map-tree 'org-promote))
  5780. (org-fix-position-after-promote))
  5781. (defun org-demote-subtree ()
  5782. "Demote the entire subtree. See `org-demote'.
  5783. See also `org-promote'."
  5784. (interactive)
  5785. (save-excursion
  5786. (org-map-tree 'org-demote))
  5787. (org-fix-position-after-promote))
  5788. (defun org-do-promote ()
  5789. "Promote the current heading higher up the tree.
  5790. If the region is active in `transient-mark-mode', promote all headings
  5791. in the region."
  5792. (interactive)
  5793. (save-excursion
  5794. (if (org-region-active-p)
  5795. (org-map-region 'org-promote (region-beginning) (region-end))
  5796. (org-promote)))
  5797. (org-fix-position-after-promote))
  5798. (defun org-do-demote ()
  5799. "Demote the current heading lower down the tree.
  5800. If the region is active in `transient-mark-mode', demote all headings
  5801. in the region."
  5802. (interactive)
  5803. (save-excursion
  5804. (if (org-region-active-p)
  5805. (org-map-region 'org-demote (region-beginning) (region-end))
  5806. (org-demote)))
  5807. (org-fix-position-after-promote))
  5808. (defun org-fix-position-after-promote ()
  5809. "Make sure that after pro/demotion cursor position is right."
  5810. (let ((pos (point)))
  5811. (when (save-excursion
  5812. (beginning-of-line 1)
  5813. (looking-at org-todo-line-regexp)
  5814. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5815. (cond ((eobp) (insert " "))
  5816. ((eolp) (insert " "))
  5817. ((equal (char-after) ?\ ) (forward-char 1))))))
  5818. (defun org-current-level ()
  5819. "Return the level of the current entry, or nil if before the first headline.
  5820. The level is the number of stars at the beginning of the headline."
  5821. (save-excursion
  5822. (condition-case nil
  5823. (progn
  5824. (org-back-to-heading t)
  5825. (funcall outline-level))
  5826. (error nil))))
  5827. (defun org-get-previous-line-level ()
  5828. "Return the outline depth of the last headline before the current line.
  5829. Returns 0 for the first headline in the buffer, and nil if before the
  5830. first headline."
  5831. (let ((current-level (org-current-level))
  5832. (prev-level (when (> (line-number-at-pos) 1)
  5833. (save-excursion
  5834. (beginning-of-line 0)
  5835. (org-current-level)))))
  5836. (cond ((null current-level) nil) ; Before first headline
  5837. ((null prev-level) 0) ; At first headline
  5838. (prev-level))))
  5839. (defun org-reduced-level (l)
  5840. "Compute the effective level of a heading.
  5841. This takes into account the setting of `org-odd-levels-only'."
  5842. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5843. (defun org-level-increment ()
  5844. "Return the number of stars that will be added or removed at a
  5845. time to headlines when structure editing, based on the value of
  5846. `org-odd-levels-only'."
  5847. (if org-odd-levels-only 2 1))
  5848. (defun org-get-valid-level (level &optional change)
  5849. "Rectify a level change under the influence of `org-odd-levels-only'
  5850. LEVEL is a current level, CHANGE is by how much the level should be
  5851. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5852. even level numbers will become the next higher odd number."
  5853. (if org-odd-levels-only
  5854. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5855. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5856. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5857. (max 1 (+ level (or change 0)))))
  5858. (if (boundp 'define-obsolete-function-alias)
  5859. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5860. (define-obsolete-function-alias 'org-get-legal-level
  5861. 'org-get-valid-level)
  5862. (define-obsolete-function-alias 'org-get-legal-level
  5863. 'org-get-valid-level "23.1")))
  5864. (defun org-promote ()
  5865. "Promote the current heading higher up the tree.
  5866. If the region is active in `transient-mark-mode', promote all headings
  5867. in the region."
  5868. (org-back-to-heading t)
  5869. (let* ((level (save-match-data (funcall outline-level)))
  5870. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5871. (diff (abs (- level (length up-head) -1))))
  5872. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5873. (replace-match up-head nil t)
  5874. ;; Fixup tag positioning
  5875. (and org-auto-align-tags (org-set-tags nil t))
  5876. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5877. (run-hooks 'org-after-promote-entry-hook)))
  5878. (defun org-demote ()
  5879. "Demote the current heading lower down the tree.
  5880. If the region is active in `transient-mark-mode', demote all headings
  5881. in the region."
  5882. (org-back-to-heading t)
  5883. (let* ((level (save-match-data (funcall outline-level)))
  5884. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5885. (diff (abs (- level (length down-head) -1))))
  5886. (replace-match down-head nil t)
  5887. ;; Fixup tag positioning
  5888. (and org-auto-align-tags (org-set-tags nil t))
  5889. (if org-adapt-indentation (org-fixup-indentation diff))
  5890. (run-hooks 'org-after-demote-entry-hook)))
  5891. (defun org-cycle-level ()
  5892. "Cycle the level of an empty headline through possible states.
  5893. This goes first to child, then to parent, level, then up the hierarchy.
  5894. After top level, it switches back to sibling level."
  5895. (interactive)
  5896. (let ((org-adapt-indentation nil))
  5897. (when (org-point-at-end-of-empty-headline)
  5898. (setq this-command 'org-cycle-level) ; Only needed for caching
  5899. (let ((cur-level (org-current-level))
  5900. (prev-level (org-get-previous-line-level)))
  5901. (cond
  5902. ;; If first headline in file, promote to top-level.
  5903. ((= prev-level 0)
  5904. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5905. do (org-do-promote)))
  5906. ;; If same level as prev, demote one.
  5907. ((= prev-level cur-level)
  5908. (org-do-demote))
  5909. ;; If parent is top-level, promote to top level if not already.
  5910. ((= prev-level 1)
  5911. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5912. do (org-do-promote)))
  5913. ;; If top-level, return to prev-level.
  5914. ((= cur-level 1)
  5915. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5916. do (org-do-demote)))
  5917. ;; If less than prev-level, promote one.
  5918. ((< cur-level prev-level)
  5919. (org-do-promote))
  5920. ;; If deeper than prev-level, promote until higher than
  5921. ;; prev-level.
  5922. ((> cur-level prev-level)
  5923. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5924. do (org-do-promote))))
  5925. t))))
  5926. (defun org-map-tree (fun)
  5927. "Call FUN for every heading underneath the current one."
  5928. (org-back-to-heading)
  5929. (let ((level (funcall outline-level)))
  5930. (save-excursion
  5931. (funcall fun)
  5932. (while (and (progn
  5933. (outline-next-heading)
  5934. (> (funcall outline-level) level))
  5935. (not (eobp)))
  5936. (funcall fun)))))
  5937. (defun org-map-region (fun beg end)
  5938. "Call FUN for every heading between BEG and END."
  5939. (let ((org-ignore-region t))
  5940. (save-excursion
  5941. (setq end (copy-marker end))
  5942. (goto-char beg)
  5943. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5944. (< (point) end))
  5945. (funcall fun))
  5946. (while (and (progn
  5947. (outline-next-heading)
  5948. (< (point) end))
  5949. (not (eobp)))
  5950. (funcall fun)))))
  5951. (defun org-fixup-indentation (diff)
  5952. "Change the indentation in the current entry by DIFF
  5953. However, if any line in the current entry has no indentation, or if it
  5954. would end up with no indentation after the change, nothing at all is done."
  5955. (save-excursion
  5956. (let ((end (save-excursion (outline-next-heading)
  5957. (point-marker)))
  5958. (prohibit (if (> diff 0)
  5959. "^\\S-"
  5960. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5961. col)
  5962. (unless (save-excursion (end-of-line 1)
  5963. (re-search-forward prohibit end t))
  5964. (while (and (< (point) end)
  5965. (re-search-forward "^[ \t]+" end t))
  5966. (goto-char (match-end 0))
  5967. (setq col (current-column))
  5968. (if (< diff 0) (replace-match ""))
  5969. (org-indent-to-column (+ diff col))))
  5970. (move-marker end nil))))
  5971. (defun org-convert-to-odd-levels ()
  5972. "Convert an org-mode file with all levels allowed to one with odd levels.
  5973. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5974. level 5 etc."
  5975. (interactive)
  5976. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5977. (let ((outline-regexp org-outline-regexp)
  5978. (outline-level 'org-outline-level)
  5979. (org-odd-levels-only nil) n)
  5980. (save-excursion
  5981. (goto-char (point-min))
  5982. (while (re-search-forward "^\\*\\*+ " nil t)
  5983. (setq n (- (length (match-string 0)) 2))
  5984. (while (>= (setq n (1- n)) 0)
  5985. (org-demote))
  5986. (end-of-line 1))))))
  5987. (defun org-convert-to-oddeven-levels ()
  5988. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5989. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5990. section with an even level, conversion would destroy the structure of the file. An error
  5991. is signaled in this case."
  5992. (interactive)
  5993. (goto-char (point-min))
  5994. ;; First check if there are no even levels
  5995. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5996. (org-show-context t)
  5997. (error "Not all levels are odd in this file. Conversion not possible"))
  5998. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5999. (let ((outline-regexp org-outline-regexp)
  6000. (outline-level 'org-outline-level)
  6001. (org-odd-levels-only nil) n)
  6002. (save-excursion
  6003. (goto-char (point-min))
  6004. (while (re-search-forward "^\\*\\*+ " nil t)
  6005. (setq n (/ (1- (length (match-string 0))) 2))
  6006. (while (>= (setq n (1- n)) 0)
  6007. (org-promote))
  6008. (end-of-line 1))))))
  6009. (defun org-tr-level (n)
  6010. "Make N odd if required."
  6011. (if org-odd-levels-only (1+ (/ n 2)) n))
  6012. ;;; Vertical tree motion, cutting and pasting of subtrees
  6013. (defun org-move-subtree-up (&optional arg)
  6014. "Move the current subtree up past ARG headlines of the same level."
  6015. (interactive "p")
  6016. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6017. (defun org-move-subtree-down (&optional arg)
  6018. "Move the current subtree down past ARG headlines of the same level."
  6019. (interactive "p")
  6020. (setq arg (prefix-numeric-value arg))
  6021. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6022. 'org-get-last-sibling))
  6023. (ins-point (make-marker))
  6024. (cnt (abs arg))
  6025. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6026. ;; Select the tree
  6027. (org-back-to-heading)
  6028. (setq beg0 (point))
  6029. (save-excursion
  6030. (setq ne-beg (org-back-over-empty-lines))
  6031. (setq beg (point)))
  6032. (save-match-data
  6033. (save-excursion (outline-end-of-heading)
  6034. (setq folded (org-invisible-p)))
  6035. (outline-end-of-subtree))
  6036. (outline-next-heading)
  6037. (setq ne-end (org-back-over-empty-lines))
  6038. (setq end (point))
  6039. (goto-char beg0)
  6040. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6041. ;; include less whitespace
  6042. (save-excursion
  6043. (goto-char beg)
  6044. (forward-line (- ne-beg ne-end))
  6045. (setq beg (point))))
  6046. ;; Find insertion point, with error handling
  6047. (while (> cnt 0)
  6048. (or (and (funcall movfunc) (looking-at outline-regexp))
  6049. (progn (goto-char beg0)
  6050. (error "Cannot move past superior level or buffer limit")))
  6051. (setq cnt (1- cnt)))
  6052. (if (> arg 0)
  6053. ;; Moving forward - still need to move over subtree
  6054. (progn (org-end-of-subtree t t)
  6055. (save-excursion
  6056. (org-back-over-empty-lines)
  6057. (or (bolp) (newline)))))
  6058. (setq ne-ins (org-back-over-empty-lines))
  6059. (move-marker ins-point (point))
  6060. (setq txt (buffer-substring beg end))
  6061. (org-save-markers-in-region beg end)
  6062. (delete-region beg end)
  6063. (org-remove-empty-overlays-at beg)
  6064. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6065. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6066. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6067. (let ((bbb (point)))
  6068. (insert-before-markers txt)
  6069. (org-reinstall-markers-in-region bbb)
  6070. (move-marker ins-point bbb))
  6071. (or (bolp) (insert "\n"))
  6072. (setq ins-end (point))
  6073. (goto-char ins-point)
  6074. (org-skip-whitespace)
  6075. (when (and (< arg 0)
  6076. (org-first-sibling-p)
  6077. (> ne-ins ne-beg))
  6078. ;; Move whitespace back to beginning
  6079. (save-excursion
  6080. (goto-char ins-end)
  6081. (let ((kill-whole-line t))
  6082. (kill-line (- ne-ins ne-beg)) (point)))
  6083. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6084. (move-marker ins-point nil)
  6085. (if folded
  6086. (hide-subtree)
  6087. (org-show-entry)
  6088. (show-children)
  6089. (org-cycle-hide-drawers 'children))
  6090. (org-clean-visibility-after-subtree-move)))
  6091. (defvar org-subtree-clip ""
  6092. "Clipboard for cut and paste of subtrees.
  6093. This is actually only a copy of the kill, because we use the normal kill
  6094. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6095. (defvar org-subtree-clip-folded nil
  6096. "Was the last copied subtree folded?
  6097. This is used to fold the tree back after pasting.")
  6098. (defun org-cut-subtree (&optional n)
  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 cutting it."
  6102. (interactive "p")
  6103. (org-copy-subtree n 'cut))
  6104. (defun org-copy-subtree (&optional n cut force-store-markers)
  6105. "Cut the current subtree into the clipboard.
  6106. With prefix arg N, cut this many sequential subtrees.
  6107. This is a short-hand for marking the subtree and then copying it.
  6108. If CUT is non-nil, actually cut the subtree.
  6109. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6110. of some markers in the region, even if CUT is non-nil. This is
  6111. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6112. (interactive "p")
  6113. (let (beg end folded (beg0 (point)))
  6114. (if (interactive-p)
  6115. (org-back-to-heading nil) ; take what looks like a subtree
  6116. (org-back-to-heading t)) ; take what is really there
  6117. (org-back-over-empty-lines)
  6118. (setq beg (point))
  6119. (skip-chars-forward " \t\r\n")
  6120. (save-match-data
  6121. (save-excursion (outline-end-of-heading)
  6122. (setq folded (org-invisible-p)))
  6123. (condition-case nil
  6124. (org-forward-same-level (1- n) t)
  6125. (error nil))
  6126. (org-end-of-subtree t t))
  6127. (org-back-over-empty-lines)
  6128. (setq end (point))
  6129. (goto-char beg0)
  6130. (when (> end beg)
  6131. (setq org-subtree-clip-folded folded)
  6132. (when (or cut force-store-markers)
  6133. (org-save-markers-in-region beg end))
  6134. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6135. (setq org-subtree-clip (current-kill 0))
  6136. (message "%s: Subtree(s) with %d characters"
  6137. (if cut "Cut" "Copied")
  6138. (length org-subtree-clip)))))
  6139. (defun org-paste-subtree (&optional level tree for-yank)
  6140. "Paste the clipboard as a subtree, with modification of headline level.
  6141. The entire subtree is promoted or demoted in order to match a new headline
  6142. level.
  6143. If the cursor is at the beginning of a headline, the same level as
  6144. that headline is used to paste the tree
  6145. If not, the new level is derived from the *visible* headings
  6146. before and after the insertion point, and taken to be the inferior headline
  6147. level of the two. So if the previous visible heading is level 3 and the
  6148. next is level 4 (or vice versa), level 4 will be used for insertion.
  6149. This makes sure that the subtree remains an independent subtree and does
  6150. not swallow low level entries.
  6151. You can also force a different level, either by using a numeric prefix
  6152. argument, or by inserting the heading marker by hand. For example, if the
  6153. cursor is after \"*****\", then the tree will be shifted to level 5.
  6154. If optional TREE is given, use this text instead of the kill ring.
  6155. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6156. move back over whitespace before inserting, and move point to the end of
  6157. the inserted text when done."
  6158. (interactive "P")
  6159. (setq tree (or tree (and kill-ring (current-kill 0))))
  6160. (unless (org-kill-is-subtree-p tree)
  6161. (error "%s"
  6162. (substitute-command-keys
  6163. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6164. (let* ((visp (not (org-invisible-p)))
  6165. (txt tree)
  6166. (^re (concat "^\\(" outline-regexp "\\)"))
  6167. (re (concat "\\(" outline-regexp "\\)"))
  6168. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6169. (old-level (if (string-match ^re txt)
  6170. (- (match-end 0) (match-beginning 0) 1)
  6171. -1))
  6172. (force-level (cond (level (prefix-numeric-value level))
  6173. ((and (looking-at "[ \t]*$")
  6174. (string-match
  6175. ^re_ (buffer-substring
  6176. (point-at-bol) (point))))
  6177. (- (match-end 1) (match-beginning 1)))
  6178. ((and (bolp)
  6179. (looking-at org-outline-regexp))
  6180. (- (match-end 0) (point) 1))
  6181. (t nil)))
  6182. (previous-level (save-excursion
  6183. (condition-case nil
  6184. (progn
  6185. (outline-previous-visible-heading 1)
  6186. (if (looking-at re)
  6187. (- (match-end 0) (match-beginning 0) 1)
  6188. 1))
  6189. (error 1))))
  6190. (next-level (save-excursion
  6191. (condition-case nil
  6192. (progn
  6193. (or (looking-at outline-regexp)
  6194. (outline-next-visible-heading 1))
  6195. (if (looking-at re)
  6196. (- (match-end 0) (match-beginning 0) 1)
  6197. 1))
  6198. (error 1))))
  6199. (new-level (or force-level (max previous-level next-level)))
  6200. (shift (if (or (= old-level -1)
  6201. (= new-level -1)
  6202. (= old-level new-level))
  6203. 0
  6204. (- new-level old-level)))
  6205. (delta (if (> shift 0) -1 1))
  6206. (func (if (> shift 0) 'org-demote 'org-promote))
  6207. (org-odd-levels-only nil)
  6208. beg end newend)
  6209. ;; Remove the forced level indicator
  6210. (if force-level
  6211. (delete-region (point-at-bol) (point)))
  6212. ;; Paste
  6213. (beginning-of-line 1)
  6214. (unless for-yank (org-back-over-empty-lines))
  6215. (setq beg (point))
  6216. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6217. (insert-before-markers txt)
  6218. (unless (string-match "\n\\'" txt) (insert "\n"))
  6219. (setq newend (point))
  6220. (org-reinstall-markers-in-region beg)
  6221. (setq end (point))
  6222. (goto-char beg)
  6223. (skip-chars-forward " \t\n\r")
  6224. (setq beg (point))
  6225. (if (and (org-invisible-p) visp)
  6226. (save-excursion (outline-show-heading)))
  6227. ;; Shift if necessary
  6228. (unless (= shift 0)
  6229. (save-restriction
  6230. (narrow-to-region beg end)
  6231. (while (not (= shift 0))
  6232. (org-map-region func (point-min) (point-max))
  6233. (setq shift (+ delta shift)))
  6234. (goto-char (point-min))
  6235. (setq newend (point-max))))
  6236. (when (or (interactive-p) for-yank)
  6237. (message "Clipboard pasted as level %d subtree" new-level))
  6238. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6239. kill-ring
  6240. (eq org-subtree-clip (current-kill 0))
  6241. org-subtree-clip-folded)
  6242. ;; The tree was folded before it was killed/copied
  6243. (hide-subtree))
  6244. (and for-yank (goto-char newend))))
  6245. (defun org-kill-is-subtree-p (&optional txt)
  6246. "Check if the current kill is an outline subtree, or a set of trees.
  6247. Returns nil if kill does not start with a headline, or if the first
  6248. headline level is not the largest headline level in the tree.
  6249. So this will actually accept several entries of equal levels as well,
  6250. which is OK for `org-paste-subtree'.
  6251. If optional TXT is given, check this string instead of the current kill."
  6252. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6253. (start-level (and kill
  6254. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6255. org-outline-regexp "\\)")
  6256. kill)
  6257. (- (match-end 2) (match-beginning 2) 1)))
  6258. (re (concat "^" org-outline-regexp))
  6259. (start (1+ (or (match-beginning 2) -1))))
  6260. (if (not start-level)
  6261. (progn
  6262. nil) ;; does not even start with a heading
  6263. (catch 'exit
  6264. (while (setq start (string-match re kill (1+ start)))
  6265. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6266. (throw 'exit nil)))
  6267. t))))
  6268. (defvar org-markers-to-move nil
  6269. "Markers that should be moved with a cut-and-paste operation.
  6270. Those markers are stored together with their positions relative to
  6271. the start of the region.")
  6272. (defun org-save-markers-in-region (beg end)
  6273. "Check markers in region.
  6274. If these markers are between BEG and END, record their position relative
  6275. to BEG, so that after moving the block of text, we can put the markers back
  6276. into place.
  6277. This function gets called just before an entry or tree gets cut from the
  6278. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6279. called immediately, to move the markers with the entries."
  6280. (setq org-markers-to-move nil)
  6281. (when (featurep 'org-clock)
  6282. (org-clock-save-markers-for-cut-and-paste beg end))
  6283. (when (featurep 'org-agenda)
  6284. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6285. (defun org-check-and-save-marker (marker beg end)
  6286. "Check if MARKER is between BEG and END.
  6287. If yes, remember the marker and the distance to BEG."
  6288. (when (and (marker-buffer marker)
  6289. (equal (marker-buffer marker) (current-buffer)))
  6290. (if (and (>= marker beg) (< marker end))
  6291. (push (cons marker (- marker beg)) org-markers-to-move))))
  6292. (defun org-reinstall-markers-in-region (beg)
  6293. "Move all remembered markers to their position relative to BEG."
  6294. (mapc (lambda (x)
  6295. (move-marker (car x) (+ beg (cdr x))))
  6296. org-markers-to-move)
  6297. (setq org-markers-to-move nil))
  6298. (defun org-narrow-to-subtree ()
  6299. "Narrow buffer to the current subtree."
  6300. (interactive)
  6301. (save-excursion
  6302. (save-match-data
  6303. (narrow-to-region
  6304. (progn (org-back-to-heading t) (point))
  6305. (progn (org-end-of-subtree t t)
  6306. (if (org-on-heading-p) (backward-char 1))
  6307. (point))))))
  6308. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6309. "Clone the task (subtree) at point N times.
  6310. The clones will be inserted as siblings.
  6311. In interactive use, the user will be prompted for the number of clones
  6312. to be produced, and for a time SHIFT, which may be a repeater as used
  6313. in time stamps, for example `+3d'.
  6314. When a valid repeater is given and the entry contains any time stamps,
  6315. the clones will become a sequence in time, with time stamps in the
  6316. subtree shifted for each clone produced. If SHIFT is nil or the
  6317. empty string, time stamps will be left alone.
  6318. If the original subtree did contain time stamps with a repeater,
  6319. the following will happen:
  6320. - the repeater will be removed in each clone
  6321. - an additional clone will be produced, with the current, unshifted
  6322. date(s) in the entry.
  6323. - the original entry will be placed *after* all the clones, with
  6324. repeater intact.
  6325. - the start days in the repeater in the original entry will be shifted
  6326. to past the last clone.
  6327. I this way you can spell out a number of instances of a repeating task,
  6328. and still retain the repeater to cover future instances of the task."
  6329. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6330. (let (beg end template task
  6331. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6332. (if (not (and (integerp n) (> n 0)))
  6333. (error "Invalid number of replications %s" n))
  6334. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6335. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6336. shift)))
  6337. (error "Invalid shift specification %s" shift))
  6338. (when doshift
  6339. (setq shift-n (string-to-number (match-string 1 shift))
  6340. shift-what (cdr (assoc (match-string 2 shift)
  6341. '(("d" . day) ("w" . week)
  6342. ("m" . month) ("y" . year))))))
  6343. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6344. (setq nmin 1 nmax n)
  6345. (org-back-to-heading t)
  6346. (setq beg (point))
  6347. (org-end-of-subtree t t)
  6348. (or (bolp) (insert "\n"))
  6349. (setq end (point))
  6350. (setq template (buffer-substring beg end))
  6351. (when (and doshift
  6352. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6353. (delete-region beg end)
  6354. (setq end beg)
  6355. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6356. (goto-char end)
  6357. (loop for n from nmin to nmax do
  6358. (if (not doshift)
  6359. (setq task template)
  6360. (with-temp-buffer
  6361. (insert template)
  6362. (org-mode)
  6363. (goto-char (point-min))
  6364. (while (re-search-forward org-ts-regexp-both nil t)
  6365. (org-timestamp-change (* n shift-n) shift-what))
  6366. (unless (= n n-no-remove)
  6367. (goto-char (point-min))
  6368. (while (re-search-forward org-ts-regexp nil t)
  6369. (save-excursion
  6370. (goto-char (match-beginning 0))
  6371. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6372. (delete-region (match-beginning 1) (match-end 1))))))
  6373. (setq task (buffer-string))))
  6374. (insert task))
  6375. (goto-char beg)))
  6376. ;;; Outline Sorting
  6377. (defun org-sort (with-case)
  6378. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6379. Optional argument WITH-CASE means sort case-sensitively.
  6380. With a double prefix argument, also remove duplicate entries."
  6381. (interactive "P")
  6382. (if (org-at-table-p)
  6383. (org-call-with-arg 'org-table-sort-lines with-case)
  6384. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6385. (defun org-sort-remove-invisible (s)
  6386. (remove-text-properties 0 (length s) org-rm-props s)
  6387. (while (string-match org-bracket-link-regexp s)
  6388. (setq s (replace-match (if (match-end 2)
  6389. (match-string 3 s)
  6390. (match-string 1 s)) t t s)))
  6391. s)
  6392. (defvar org-priority-regexp) ; defined later in the file
  6393. (defvar org-after-sorting-entries-or-items-hook nil
  6394. "Hook that is run after a bunch of entries or items have been sorted.
  6395. When children are sorted, the cursor is in the parent line when this
  6396. hook gets called. When a region or a plain list is sorted, the cursor
  6397. will be in the first entry of the sorted region/list.")
  6398. (defun org-sort-entries-or-items
  6399. (&optional with-case sorting-type getkey-func compare-func property)
  6400. "Sort entries on a certain level of an outline tree, or plain list items.
  6401. If there is an active region, the entries in the region are sorted.
  6402. Else, if the cursor is before the first entry, sort the top-level items.
  6403. Else, the children of the entry at point are sorted.
  6404. If the cursor is at the first item in a plain list, the list items will be
  6405. sorted.
  6406. Sorting can be alphabetically, numerically, by date/time as given by
  6407. a time stamp, by a property or by priority.
  6408. The command prompts for the sorting type unless it has been given to the
  6409. function through the SORTING-TYPE argument, which needs to a character,
  6410. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6411. precise meaning of each character:
  6412. n Numerically, by converting the beginning of the entry/item to a number.
  6413. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6414. t By date/time, either the first active time stamp in the entry, or, if
  6415. none exist, by the first inactive one.
  6416. In items, only the first line will be checked.
  6417. s By the scheduled date/time.
  6418. d By deadline date/time.
  6419. c By creation time, which is assumed to be the first inactive time stamp
  6420. at the beginning of a line.
  6421. p By priority according to the cookie.
  6422. r By the value of a property.
  6423. Capital letters will reverse the sort order.
  6424. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6425. called with point at the beginning of the record. It must return either
  6426. a string or a number that should serve as the sorting key for that record.
  6427. Comparing entries ignores case by default. However, with an optional argument
  6428. WITH-CASE, the sorting considers case as well."
  6429. (interactive "P")
  6430. (let ((case-func (if with-case 'identity 'downcase))
  6431. start beg end stars re re2
  6432. txt what tmp plain-list-p)
  6433. ;; Find beginning and end of region to sort
  6434. (cond
  6435. ((org-region-active-p)
  6436. ;; we will sort the region
  6437. (setq end (region-end)
  6438. what "region")
  6439. (goto-char (region-beginning))
  6440. (if (not (org-on-heading-p)) (outline-next-heading))
  6441. (setq start (point)))
  6442. ((org-at-item-p)
  6443. ;; we will sort this plain list
  6444. (org-beginning-of-item-list) (setq start (point))
  6445. (org-end-of-item-list)
  6446. (or (bolp) (insert "\n"))
  6447. (setq end (point))
  6448. (goto-char start)
  6449. (setq plain-list-p t
  6450. what "plain list"))
  6451. ((or (org-on-heading-p)
  6452. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6453. ;; we will sort the children of the current headline
  6454. (org-back-to-heading)
  6455. (setq start (point)
  6456. end (progn (org-end-of-subtree t t)
  6457. (or (bolp) (insert "\n"))
  6458. (org-back-over-empty-lines)
  6459. (point))
  6460. what "children")
  6461. (goto-char start)
  6462. (show-subtree)
  6463. (outline-next-heading))
  6464. (t
  6465. ;; we will sort the top-level entries in this file
  6466. (goto-char (point-min))
  6467. (or (org-on-heading-p) (outline-next-heading))
  6468. (setq start (point))
  6469. (goto-char (point-max))
  6470. (beginning-of-line 1)
  6471. (when (looking-at ".*?\\S-")
  6472. ;; File ends in a non-white line
  6473. (end-of-line 1)
  6474. (insert "\n"))
  6475. (setq end (point-max))
  6476. (setq what "top-level")
  6477. (goto-char start)
  6478. (show-all)))
  6479. (setq beg (point))
  6480. (if (>= beg end) (error "Nothing to sort"))
  6481. (unless plain-list-p
  6482. (looking-at "\\(\\*+\\)")
  6483. (setq stars (match-string 1)
  6484. re (concat "^" (regexp-quote stars) " +")
  6485. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6486. txt (buffer-substring beg end))
  6487. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6488. (if (and (not (equal stars "*")) (string-match re2 txt))
  6489. (error "Region to sort contains a level above the first entry")))
  6490. (unless sorting-type
  6491. (message
  6492. (if plain-list-p
  6493. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6494. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6495. [t]ime [s]cheduled [d]eadline [c]reated
  6496. A/N/T/S/D/C/P/O/F means reversed:")
  6497. what)
  6498. (setq sorting-type (read-char-exclusive))
  6499. (and (= (downcase sorting-type) ?f)
  6500. (setq getkey-func
  6501. (org-icompleting-read "Sort using function: "
  6502. obarray 'fboundp t nil nil))
  6503. (setq getkey-func (intern getkey-func)))
  6504. (and (= (downcase sorting-type) ?r)
  6505. (setq property
  6506. (org-icompleting-read "Property: "
  6507. (mapcar 'list (org-buffer-property-keys t))
  6508. nil t))))
  6509. (message "Sorting entries...")
  6510. (save-restriction
  6511. (narrow-to-region start end)
  6512. (let ((dcst (downcase sorting-type))
  6513. (case-fold-search nil)
  6514. (now (current-time)))
  6515. (sort-subr
  6516. (/= dcst sorting-type)
  6517. ;; This function moves to the beginning character of the "record" to
  6518. ;; be sorted.
  6519. (if plain-list-p
  6520. (lambda nil
  6521. (if (org-at-item-p) t (goto-char (point-max))))
  6522. (lambda nil
  6523. (if (re-search-forward re nil t)
  6524. (goto-char (match-beginning 0))
  6525. (goto-char (point-max)))))
  6526. ;; This function moves to the last character of the "record" being
  6527. ;; sorted.
  6528. (if plain-list-p
  6529. 'org-end-of-item
  6530. (lambda nil
  6531. (save-match-data
  6532. (condition-case nil
  6533. (outline-forward-same-level 1)
  6534. (error
  6535. (goto-char (point-max)))))))
  6536. ;; This function returns the value that gets sorted against.
  6537. (if plain-list-p
  6538. (lambda nil
  6539. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6540. (cond
  6541. ((= dcst ?n)
  6542. (string-to-number (buffer-substring (match-end 0)
  6543. (point-at-eol))))
  6544. ((= dcst ?a)
  6545. (buffer-substring (match-end 0) (point-at-eol)))
  6546. ((= dcst ?t)
  6547. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6548. (re-search-forward org-ts-regexp-both
  6549. (point-at-eol) t))
  6550. (org-time-string-to-seconds (match-string 0))
  6551. (org-float-time now)))
  6552. ((= dcst ?f)
  6553. (if getkey-func
  6554. (progn
  6555. (setq tmp (funcall getkey-func))
  6556. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6557. tmp)
  6558. (error "Invalid key function `%s'" getkey-func)))
  6559. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6560. (lambda nil
  6561. (cond
  6562. ((= dcst ?n)
  6563. (if (looking-at org-complex-heading-regexp)
  6564. (string-to-number (match-string 4))
  6565. nil))
  6566. ((= dcst ?a)
  6567. (if (looking-at org-complex-heading-regexp)
  6568. (funcall case-func (match-string 4))
  6569. nil))
  6570. ((= dcst ?t)
  6571. (let ((end (save-excursion (outline-next-heading) (point))))
  6572. (if (or (re-search-forward org-ts-regexp end t)
  6573. (re-search-forward org-ts-regexp-both end t))
  6574. (org-time-string-to-seconds (match-string 0))
  6575. (org-float-time now))))
  6576. ((= dcst ?c)
  6577. (let ((end (save-excursion (outline-next-heading) (point))))
  6578. (if (re-search-forward
  6579. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6580. end t)
  6581. (org-time-string-to-seconds (match-string 0))
  6582. (org-float-time now))))
  6583. ((= dcst ?s)
  6584. (let ((end (save-excursion (outline-next-heading) (point))))
  6585. (if (re-search-forward org-scheduled-time-regexp end t)
  6586. (org-time-string-to-seconds (match-string 1))
  6587. (org-float-time now))))
  6588. ((= dcst ?d)
  6589. (let ((end (save-excursion (outline-next-heading) (point))))
  6590. (if (re-search-forward org-deadline-time-regexp end t)
  6591. (org-time-string-to-seconds (match-string 1))
  6592. (org-float-time now))))
  6593. ((= dcst ?p)
  6594. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6595. (string-to-char (match-string 2))
  6596. org-default-priority))
  6597. ((= dcst ?r)
  6598. (or (org-entry-get nil property) ""))
  6599. ((= dcst ?o)
  6600. (if (looking-at org-complex-heading-regexp)
  6601. (- 9999 (length (member (match-string 2)
  6602. org-todo-keywords-1)))))
  6603. ((= dcst ?f)
  6604. (if getkey-func
  6605. (progn
  6606. (setq tmp (funcall getkey-func))
  6607. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6608. tmp)
  6609. (error "Invalid key function `%s'" getkey-func)))
  6610. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6611. nil
  6612. (cond
  6613. ((= dcst ?a) 'string<)
  6614. ((= dcst ?f) compare-func)
  6615. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6616. (t nil)))))
  6617. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6618. (message "Sorting entries...done")))
  6619. (defun org-do-sort (table what &optional with-case sorting-type)
  6620. "Sort TABLE of WHAT according to SORTING-TYPE.
  6621. The user will be prompted for the SORTING-TYPE if the call to this
  6622. function does not specify it. WHAT is only for the prompt, to indicate
  6623. what is being sorted. The sorting key will be extracted from
  6624. the car of the elements of the table.
  6625. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6626. (unless sorting-type
  6627. (message
  6628. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6629. what)
  6630. (setq sorting-type (read-char-exclusive)))
  6631. (let ((dcst (downcase sorting-type))
  6632. extractfun comparefun)
  6633. ;; Define the appropriate functions
  6634. (cond
  6635. ((= dcst ?n)
  6636. (setq extractfun 'string-to-number
  6637. comparefun (if (= dcst sorting-type) '< '>)))
  6638. ((= dcst ?a)
  6639. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6640. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6641. comparefun (if (= dcst sorting-type)
  6642. 'string<
  6643. (lambda (a b) (and (not (string< a b))
  6644. (not (string= a b)))))))
  6645. ((= dcst ?t)
  6646. (setq extractfun
  6647. (lambda (x)
  6648. (if (or (string-match org-ts-regexp x)
  6649. (string-match org-ts-regexp-both x))
  6650. (org-float-time
  6651. (org-time-string-to-time (match-string 0 x)))
  6652. 0))
  6653. comparefun (if (= dcst sorting-type) '< '>)))
  6654. (t (error "Invalid sorting type `%c'" sorting-type)))
  6655. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6656. table)
  6657. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6658. ;;; The orgstruct minor mode
  6659. ;; Define a minor mode which can be used in other modes in order to
  6660. ;; integrate the org-mode structure editing commands.
  6661. ;; This is really a hack, because the org-mode structure commands use
  6662. ;; keys which normally belong to the major mode. Here is how it
  6663. ;; works: The minor mode defines all the keys necessary to operate the
  6664. ;; structure commands, but wraps the commands into a function which
  6665. ;; tests if the cursor is currently at a headline or a plain list
  6666. ;; item. If that is the case, the structure command is used,
  6667. ;; temporarily setting many Org-mode variables like regular
  6668. ;; expressions for filling etc. However, when any of those keys is
  6669. ;; used at a different location, function uses `key-binding' to look
  6670. ;; up if the key has an associated command in another currently active
  6671. ;; keymap (minor modes, major mode, global), and executes that
  6672. ;; command. There might be problems if any of the keys is otherwise
  6673. ;; used as a prefix key.
  6674. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6675. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6676. ;; addresses this by checking explicitly for both bindings.
  6677. (defvar orgstruct-mode-map (make-sparse-keymap)
  6678. "Keymap for the minor `orgstruct-mode'.")
  6679. (defvar org-local-vars nil
  6680. "List of local variables, for use by `orgstruct-mode'")
  6681. ;;;###autoload
  6682. (define-minor-mode orgstruct-mode
  6683. "Toggle the minor more `orgstruct-mode'.
  6684. This mode is for using Org-mode structure commands in other modes.
  6685. The following key behave as if Org-mode was active, if the cursor
  6686. is on a headline, or on a plain list item (both in the definition
  6687. of Org-mode).
  6688. M-up Move entry/item up
  6689. M-down Move entry/item down
  6690. M-left Promote
  6691. M-right Demote
  6692. M-S-up Move entry/item up
  6693. M-S-down Move entry/item down
  6694. M-S-left Promote subtree
  6695. M-S-right Demote subtree
  6696. M-q Fill paragraph and items like in Org-mode
  6697. C-c ^ Sort entries
  6698. C-c - Cycle list bullet
  6699. TAB Cycle item visibility
  6700. M-RET Insert new heading/item
  6701. S-M-RET Insert new TODO heading / Checkbox item
  6702. C-c C-c Set tags / toggle checkbox"
  6703. nil " OrgStruct" nil
  6704. (org-load-modules-maybe)
  6705. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6706. ;;;###autoload
  6707. (defun turn-on-orgstruct ()
  6708. "Unconditionally turn on `orgstruct-mode'."
  6709. (orgstruct-mode 1))
  6710. (defun orgstruct++-mode (&optional arg)
  6711. "Toggle `orgstruct-mode', the enhanced version of it.
  6712. In addition to setting orgstruct-mode, this also exports all indentation
  6713. and autofilling variables from org-mode into the buffer. It will also
  6714. recognize item context in multiline items.
  6715. Note that turning off orgstruct-mode will *not* remove the
  6716. indentation/paragraph settings. This can only be done by refreshing the
  6717. major mode, for example with \\[normal-mode]."
  6718. (interactive "P")
  6719. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6720. (if (< arg 1)
  6721. (orgstruct-mode -1)
  6722. (orgstruct-mode 1)
  6723. (let (var val)
  6724. (mapc
  6725. (lambda (x)
  6726. (when (string-match
  6727. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6728. (symbol-name (car x)))
  6729. (setq var (car x) val (nth 1 x))
  6730. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6731. org-local-vars)
  6732. (org-set-local 'orgstruct-is-++ t))))
  6733. (defvar orgstruct-is-++ nil
  6734. "Is orgstruct-mode in ++ version in the current-buffer?")
  6735. (make-variable-buffer-local 'orgstruct-is-++)
  6736. ;;;###autoload
  6737. (defun turn-on-orgstruct++ ()
  6738. "Unconditionally turn on `orgstruct++-mode'."
  6739. (orgstruct++-mode 1))
  6740. (defun orgstruct-error ()
  6741. "Error when there is no default binding for a structure key."
  6742. (interactive)
  6743. (error "This key has no function outside structure elements"))
  6744. (defun orgstruct-setup ()
  6745. "Setup orgstruct keymaps."
  6746. (let ((nfunc 0)
  6747. (bindings
  6748. (list
  6749. '([(meta up)] org-metaup)
  6750. '([(meta down)] org-metadown)
  6751. '([(meta left)] org-metaleft)
  6752. '([(meta right)] org-metaright)
  6753. '([(meta shift up)] org-shiftmetaup)
  6754. '([(meta shift down)] org-shiftmetadown)
  6755. '([(meta shift left)] org-shiftmetaleft)
  6756. '([(meta shift right)] org-shiftmetaright)
  6757. '([?\e (up)] org-metaup)
  6758. '([?\e (down)] org-metadown)
  6759. '([?\e (left)] org-metaleft)
  6760. '([?\e (right)] org-metaright)
  6761. '([?\e (shift up)] org-shiftmetaup)
  6762. '([?\e (shift down)] org-shiftmetadown)
  6763. '([?\e (shift left)] org-shiftmetaleft)
  6764. '([?\e (shift right)] org-shiftmetaright)
  6765. '([(shift up)] org-shiftup)
  6766. '([(shift down)] org-shiftdown)
  6767. '([(shift left)] org-shiftleft)
  6768. '([(shift right)] org-shiftright)
  6769. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6770. '("\M-q" fill-paragraph)
  6771. '("\C-c^" org-sort)
  6772. '("\C-c-" org-cycle-list-bullet)))
  6773. elt key fun cmd)
  6774. (while (setq elt (pop bindings))
  6775. (setq nfunc (1+ nfunc))
  6776. (setq key (org-key (car elt))
  6777. fun (nth 1 elt)
  6778. cmd (orgstruct-make-binding fun nfunc key))
  6779. (org-defkey orgstruct-mode-map key cmd))
  6780. ;; Special treatment needed for TAB and RET
  6781. (org-defkey orgstruct-mode-map [(tab)]
  6782. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6783. (org-defkey orgstruct-mode-map "\C-i"
  6784. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6785. (org-defkey orgstruct-mode-map "\M-\C-m"
  6786. (orgstruct-make-binding 'org-insert-heading 105
  6787. "\M-\C-m" [(meta return)]))
  6788. (org-defkey orgstruct-mode-map [(meta return)]
  6789. (orgstruct-make-binding 'org-insert-heading 106
  6790. [(meta return)] "\M-\C-m"))
  6791. (org-defkey orgstruct-mode-map [(shift meta return)]
  6792. (orgstruct-make-binding 'org-insert-todo-heading 107
  6793. [(meta return)] "\M-\C-m"))
  6794. (org-defkey orgstruct-mode-map "\e\C-m"
  6795. (orgstruct-make-binding 'org-insert-heading 108
  6796. "\e\C-m" [?\e (return)]))
  6797. (org-defkey orgstruct-mode-map [?\e (return)]
  6798. (orgstruct-make-binding 'org-insert-heading 109
  6799. [?\e (return)] "\e\C-m"))
  6800. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6801. (orgstruct-make-binding 'org-insert-todo-heading 110
  6802. [?\e (return)] "\e\C-m"))
  6803. (unless org-local-vars
  6804. (setq org-local-vars (org-get-local-variables)))
  6805. t))
  6806. (defun orgstruct-make-binding (fun n &rest keys)
  6807. "Create a function for binding in the structure minor mode.
  6808. FUN is the command to call inside a table. N is used to create a unique
  6809. command name. KEYS are keys that should be checked in for a command
  6810. to execute outside of tables."
  6811. (eval
  6812. (list 'defun
  6813. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6814. '(arg)
  6815. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6816. "Outside of structure, run the binding of `"
  6817. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6818. "'.")
  6819. '(interactive "p")
  6820. (list 'if
  6821. `(org-context-p 'headline 'item
  6822. (and orgstruct-is-++
  6823. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6824. 'item-body))
  6825. (list 'org-run-like-in-org-mode (list 'quote fun))
  6826. (list 'let '(orgstruct-mode)
  6827. (list 'call-interactively
  6828. (append '(or)
  6829. (mapcar (lambda (k)
  6830. (list 'key-binding k))
  6831. keys)
  6832. '('orgstruct-error))))))))
  6833. (defun org-context-p (&rest contexts)
  6834. "Check if local context is any of CONTEXTS.
  6835. Possible values in the list of contexts are `table', `headline', and `item'."
  6836. (let ((pos (point)))
  6837. (goto-char (point-at-bol))
  6838. (prog1 (or (and (memq 'table contexts)
  6839. (looking-at "[ \t]*|"))
  6840. (and (memq 'headline contexts)
  6841. ;;????????? (looking-at "\\*+"))
  6842. (looking-at outline-regexp))
  6843. (and (memq 'item contexts)
  6844. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6845. (and (memq 'item-body contexts)
  6846. (org-in-item-p)))
  6847. (goto-char pos))))
  6848. (defun org-get-local-variables ()
  6849. "Return a list of all local variables in an org-mode buffer."
  6850. (let (varlist)
  6851. (with-current-buffer (get-buffer-create "*Org tmp*")
  6852. (erase-buffer)
  6853. (org-mode)
  6854. (setq varlist (buffer-local-variables)))
  6855. (kill-buffer "*Org tmp*")
  6856. (delq nil
  6857. (mapcar
  6858. (lambda (x)
  6859. (setq x
  6860. (if (symbolp x)
  6861. (list x)
  6862. (list (car x) (list 'quote (cdr x)))))
  6863. (if (string-match
  6864. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6865. (symbol-name (car x)))
  6866. x nil))
  6867. varlist))))
  6868. ;;;###autoload
  6869. (defun org-run-like-in-org-mode (cmd)
  6870. "Run a command, pretending that the current buffer is in Org-mode.
  6871. This will temporarily bind local variables that are typically bound in
  6872. Org-mode to the values they have in Org-mode, and then interactively
  6873. call CMD."
  6874. (org-load-modules-maybe)
  6875. (unless org-local-vars
  6876. (setq org-local-vars (org-get-local-variables)))
  6877. (eval (list 'let org-local-vars
  6878. (list 'call-interactively (list 'quote cmd)))))
  6879. ;;;; Archiving
  6880. (defun org-get-category (&optional pos)
  6881. "Get the category applying to position POS."
  6882. (get-text-property (or pos (point)) 'org-category))
  6883. (defun org-refresh-category-properties ()
  6884. "Refresh category text properties in the buffer."
  6885. (let ((def-cat (cond
  6886. ((null org-category)
  6887. (if buffer-file-name
  6888. (file-name-sans-extension
  6889. (file-name-nondirectory buffer-file-name))
  6890. "???"))
  6891. ((symbolp org-category) (symbol-name org-category))
  6892. (t org-category)))
  6893. beg end cat pos optionp)
  6894. (org-unmodified
  6895. (save-excursion
  6896. (save-restriction
  6897. (widen)
  6898. (goto-char (point-min))
  6899. (put-text-property (point) (point-max) 'org-category def-cat)
  6900. (while (re-search-forward
  6901. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6902. (setq pos (match-end 0)
  6903. optionp (equal (char-after (match-beginning 0)) ?#)
  6904. cat (org-trim (match-string 2)))
  6905. (if optionp
  6906. (setq beg (point-at-bol) end (point-max))
  6907. (org-back-to-heading t)
  6908. (setq beg (point) end (org-end-of-subtree t t)))
  6909. (put-text-property beg end 'org-category cat)
  6910. (goto-char pos)))))))
  6911. ;;;; Link Stuff
  6912. ;;; Link abbreviations
  6913. (defun org-link-expand-abbrev (link)
  6914. "Apply replacements as defined in `org-link-abbrev-alist."
  6915. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6916. (let* ((key (match-string 1 link))
  6917. (as (or (assoc key org-link-abbrev-alist-local)
  6918. (assoc key org-link-abbrev-alist)))
  6919. (tag (and (match-end 2) (match-string 3 link)))
  6920. rpl)
  6921. (if (not as)
  6922. link
  6923. (setq rpl (cdr as))
  6924. (cond
  6925. ((symbolp rpl) (funcall rpl tag))
  6926. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6927. ((string-match "%h" rpl)
  6928. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6929. (t (concat rpl tag)))))
  6930. link))
  6931. ;;; Storing and inserting links
  6932. (defvar org-insert-link-history nil
  6933. "Minibuffer history for links inserted with `org-insert-link'.")
  6934. (defvar org-stored-links nil
  6935. "Contains the links stored with `org-store-link'.")
  6936. (defvar org-store-link-plist nil
  6937. "Plist with info about the most recently link created with `org-store-link'.")
  6938. (defvar org-link-protocols nil
  6939. "Link protocols added to Org-mode using `org-add-link-type'.")
  6940. (defvar org-store-link-functions nil
  6941. "List of functions that are called to create and store a link.
  6942. Each function will be called in turn until one returns a non-nil
  6943. value. Each function should check if it is responsible for creating
  6944. this link (for example by looking at the major mode).
  6945. If not, it must exit and return nil.
  6946. If yes, it should return a non-nil value after a calling
  6947. `org-store-link-props' with a list of properties and values.
  6948. Special properties are:
  6949. :type The link prefix. like \"http\". This must be given.
  6950. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6951. This is obligatory as well.
  6952. :description Optional default description for the second pair
  6953. of brackets in an Org-mode link. The user can still change
  6954. this when inserting this link into an Org-mode buffer.
  6955. In addition to these, any additional properties can be specified
  6956. and then used in remember templates.")
  6957. (defun org-add-link-type (type &optional follow export)
  6958. "Add TYPE to the list of `org-link-types'.
  6959. Re-compute all regular expressions depending on `org-link-types'
  6960. FOLLOW and EXPORT are two functions.
  6961. FOLLOW should take the link path as the single argument and do whatever
  6962. is necessary to follow the link, for example find a file or display
  6963. a mail message.
  6964. EXPORT should format the link path for export to one of the export formats.
  6965. It should be a function accepting three arguments:
  6966. path the path of the link, the text after the prefix (like \"http:\")
  6967. desc the description of the link, if any, nil if there was no description
  6968. format the export format, a symbol like `html' or `latex'.
  6969. The function may use the FORMAT information to return different values
  6970. depending on the format. The return value will be put literally into
  6971. the exported file.
  6972. Org-mode has a built-in default for exporting links. If you are happy with
  6973. this default, there is no need to define an export function for the link
  6974. type. For a simple example of an export function, see `org-bbdb.el'."
  6975. (add-to-list 'org-link-types type t)
  6976. (org-make-link-regexps)
  6977. (if (assoc type org-link-protocols)
  6978. (setcdr (assoc type org-link-protocols) (list follow export))
  6979. (push (list type follow export) org-link-protocols)))
  6980. (defvar org-agenda-buffer-name)
  6981. ;;;###autoload
  6982. (defun org-store-link (arg)
  6983. "\\<org-mode-map>Store an org-link to the current location.
  6984. This link is added to `org-stored-links' and can later be inserted
  6985. into an org-buffer with \\[org-insert-link].
  6986. For some link types, a prefix arg is interpreted:
  6987. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6988. For file links, arg negates `org-context-in-file-links'."
  6989. (interactive "P")
  6990. (org-load-modules-maybe)
  6991. (setq org-store-link-plist nil) ; reset
  6992. (let ((outline-regexp (org-get-limited-outline-regexp))
  6993. link cpltxt desc description search txt custom-id)
  6994. (cond
  6995. ((run-hook-with-args-until-success 'org-store-link-functions)
  6996. (setq link (plist-get org-store-link-plist :link)
  6997. desc (or (plist-get org-store-link-plist :description) link)))
  6998. ((equal (buffer-name) "*Org Edit Src Example*")
  6999. (let (label gc)
  7000. (while (or (not label)
  7001. (save-excursion
  7002. (save-restriction
  7003. (widen)
  7004. (goto-char (point-min))
  7005. (re-search-forward
  7006. (regexp-quote (format org-coderef-label-format label))
  7007. nil t))))
  7008. (when label (message "Label exists already") (sit-for 2))
  7009. (setq label (read-string "Code line label: " label)))
  7010. (end-of-line 1)
  7011. (setq link (format org-coderef-label-format label))
  7012. (setq gc (- 79 (length link)))
  7013. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7014. (insert link)
  7015. (setq link (concat "(" label ")") desc nil)))
  7016. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7017. ;; We are in the agenda, link to referenced location
  7018. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7019. (get-text-property (point) 'org-marker))))
  7020. (when m
  7021. (org-with-point-at m
  7022. (call-interactively 'org-store-link)))))
  7023. ((eq major-mode 'calendar-mode)
  7024. (let ((cd (calendar-cursor-to-date)))
  7025. (setq link
  7026. (format-time-string
  7027. (car org-time-stamp-formats)
  7028. (apply 'encode-time
  7029. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7030. nil nil nil))))
  7031. (org-store-link-props :type "calendar" :date cd)))
  7032. ((eq major-mode 'w3-mode)
  7033. (setq cpltxt (if (and (buffer-name)
  7034. (not (string-match "Untitled" (buffer-name))))
  7035. (buffer-name)
  7036. (url-view-url t))
  7037. link (org-make-link (url-view-url t)))
  7038. (org-store-link-props :type "w3" :url (url-view-url t)))
  7039. ((eq major-mode 'w3m-mode)
  7040. (setq cpltxt (or w3m-current-title w3m-current-url)
  7041. link (org-make-link w3m-current-url))
  7042. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7043. ((setq search (run-hook-with-args-until-success
  7044. 'org-create-file-search-functions))
  7045. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7046. "::" search))
  7047. (setq cpltxt (or description link)))
  7048. ((eq major-mode 'image-mode)
  7049. (setq cpltxt (concat "file:"
  7050. (abbreviate-file-name buffer-file-name))
  7051. link (org-make-link cpltxt))
  7052. (org-store-link-props :type "image" :file buffer-file-name))
  7053. ((eq major-mode 'dired-mode)
  7054. ;; link to the file in the current line
  7055. (let ((file (dired-get-filename nil t)))
  7056. (setq file (if file
  7057. (abbreviate-file-name
  7058. (expand-file-name (dired-get-filename nil t)))
  7059. ;; otherwise, no file so use current directory.
  7060. default-directory))
  7061. (setq cpltxt (concat "file:" file)
  7062. link (org-make-link cpltxt))))
  7063. ((and buffer-file-name (org-mode-p))
  7064. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7065. (cond
  7066. ((org-in-regexp "<<\\(.*?\\)>>")
  7067. (setq cpltxt
  7068. (concat "file:"
  7069. (abbreviate-file-name buffer-file-name)
  7070. "::" (match-string 1))
  7071. link (org-make-link cpltxt)))
  7072. ((and (featurep 'org-id)
  7073. (or (eq org-link-to-org-use-id t)
  7074. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7075. (interactive-p))
  7076. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7077. (interactive-p)
  7078. (not custom-id))
  7079. (and org-link-to-org-use-id
  7080. (condition-case nil
  7081. (org-entry-get nil "ID")
  7082. (error nil)))))
  7083. ;; We can make a link using the ID.
  7084. (setq link (condition-case nil
  7085. (prog1 (org-id-store-link)
  7086. (setq desc (plist-get org-store-link-plist
  7087. :description)))
  7088. (error
  7089. ;; probably before first headline, link to file only
  7090. (concat "file:"
  7091. (abbreviate-file-name buffer-file-name))))))
  7092. (t
  7093. ;; Just link to current headline
  7094. (setq cpltxt (concat "file:"
  7095. (abbreviate-file-name buffer-file-name)))
  7096. ;; Add a context search string
  7097. (when (org-xor org-context-in-file-links arg)
  7098. (setq txt (cond
  7099. ((org-on-heading-p) nil)
  7100. ((org-region-active-p)
  7101. (buffer-substring (region-beginning) (region-end)))
  7102. (t nil)))
  7103. (when (or (null txt) (string-match "\\S-" txt))
  7104. (setq cpltxt
  7105. (concat cpltxt "::"
  7106. (condition-case nil
  7107. (org-make-org-heading-search-string txt)
  7108. (error "")))
  7109. desc (or (nth 4 (ignore-errors
  7110. (org-heading-components))) "NONE"))))
  7111. (if (string-match "::\\'" cpltxt)
  7112. (setq cpltxt (substring cpltxt 0 -2)))
  7113. (setq link (org-make-link cpltxt)))))
  7114. ((buffer-file-name (buffer-base-buffer))
  7115. ;; Just link to this file here.
  7116. (setq cpltxt (concat "file:"
  7117. (abbreviate-file-name
  7118. (buffer-file-name (buffer-base-buffer)))))
  7119. ;; Add a context string
  7120. (when (org-xor org-context-in-file-links arg)
  7121. (setq txt (if (org-region-active-p)
  7122. (buffer-substring (region-beginning) (region-end))
  7123. (buffer-substring (point-at-bol) (point-at-eol))))
  7124. ;; Only use search option if there is some text.
  7125. (when (string-match "\\S-" txt)
  7126. (setq cpltxt
  7127. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7128. desc "NONE")))
  7129. (setq link (org-make-link cpltxt)))
  7130. ((interactive-p)
  7131. (error "Cannot link to a buffer which is not visiting a file"))
  7132. (t (setq link nil)))
  7133. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7134. (setq link (or link cpltxt)
  7135. desc (or desc cpltxt))
  7136. (if (equal desc "NONE") (setq desc nil))
  7137. (if (and (or (interactive-p) executing-kbd-macro) link)
  7138. (progn
  7139. (setq org-stored-links
  7140. (cons (list link desc) org-stored-links))
  7141. (message "Stored: %s" (or desc link))
  7142. (when custom-id
  7143. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7144. "::#" custom-id))
  7145. (setq org-stored-links
  7146. (cons (list link desc) org-stored-links))))
  7147. (and link (org-make-link-string link desc)))))
  7148. (defun org-store-link-props (&rest plist)
  7149. "Store link properties, extract names and addresses."
  7150. (let (x adr)
  7151. (when (setq x (plist-get plist :from))
  7152. (setq adr (mail-extract-address-components x))
  7153. (setq plist (plist-put plist :fromname (car adr)))
  7154. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7155. (when (setq x (plist-get plist :to))
  7156. (setq adr (mail-extract-address-components x))
  7157. (setq plist (plist-put plist :toname (car adr)))
  7158. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7159. (let ((from (plist-get plist :from))
  7160. (to (plist-get plist :to)))
  7161. (when (and from to org-from-is-user-regexp)
  7162. (setq plist
  7163. (plist-put plist :fromto
  7164. (if (string-match org-from-is-user-regexp from)
  7165. (concat "to %t")
  7166. (concat "from %f"))))))
  7167. (setq org-store-link-plist plist))
  7168. (defun org-add-link-props (&rest plist)
  7169. "Add these properties to the link property list."
  7170. (let (key value)
  7171. (while plist
  7172. (setq key (pop plist) value (pop plist))
  7173. (setq org-store-link-plist
  7174. (plist-put org-store-link-plist key value)))))
  7175. (defun org-email-link-description (&optional fmt)
  7176. "Return the description part of an email link.
  7177. This takes information from `org-store-link-plist' and formats it
  7178. according to FMT (default from `org-email-link-description-format')."
  7179. (setq fmt (or fmt org-email-link-description-format))
  7180. (let* ((p org-store-link-plist)
  7181. (to (plist-get p :toaddress))
  7182. (from (plist-get p :fromaddress))
  7183. (table
  7184. (list
  7185. (cons "%c" (plist-get p :fromto))
  7186. (cons "%F" (plist-get p :from))
  7187. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7188. (cons "%T" (plist-get p :to))
  7189. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7190. (cons "%s" (plist-get p :subject))
  7191. (cons "%m" (plist-get p :message-id)))))
  7192. (when (string-match "%c" fmt)
  7193. ;; Check if the user wrote this message
  7194. (if (and org-from-is-user-regexp from to
  7195. (save-match-data (string-match org-from-is-user-regexp from)))
  7196. (setq fmt (replace-match "to %t" t t fmt))
  7197. (setq fmt (replace-match "from %f" t t fmt))))
  7198. (org-replace-escapes fmt table)))
  7199. (defun org-make-org-heading-search-string (&optional string heading)
  7200. "Make search string for STRING or current headline."
  7201. (interactive)
  7202. (let ((s (or string (org-get-heading))))
  7203. (unless (and string (not heading))
  7204. ;; We are using a headline, clean up garbage in there.
  7205. (if (string-match org-todo-regexp s)
  7206. (setq s (replace-match "" t t s)))
  7207. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7208. (setq s (replace-match "" t t s)))
  7209. (setq s (org-trim s))
  7210. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7211. org-comment-string "\\)") s)
  7212. (setq s (replace-match "" t t s)))
  7213. (while (string-match org-ts-regexp s)
  7214. (setq s (replace-match "" t t s))))
  7215. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7216. (setq s (replace-match " " t t s)))
  7217. (or string (setq s (concat "*" s))) ; Add * for headlines
  7218. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7219. (defun org-make-link (&rest strings)
  7220. "Concatenate STRINGS."
  7221. (apply 'concat strings))
  7222. (defun org-make-link-string (link &optional description)
  7223. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7224. (unless (string-match "\\S-" link)
  7225. (error "Empty link"))
  7226. (when (and description
  7227. (stringp description)
  7228. (not (string-match "\\S-" description)))
  7229. (setq description nil))
  7230. (when (stringp description)
  7231. ;; Remove brackets from the description, they are fatal.
  7232. (while (string-match "\\[" description)
  7233. (setq description (replace-match "{" t t description)))
  7234. (while (string-match "\\]" description)
  7235. (setq description (replace-match "}" t t description))))
  7236. (when (equal (org-link-escape link) description)
  7237. ;; No description needed, it is identical
  7238. (setq description nil))
  7239. (when (and (not description)
  7240. (not (equal link (org-link-escape link))))
  7241. (setq description (org-extract-attributes link)))
  7242. (concat "[[" (org-link-escape link) "]"
  7243. (if description (concat "[" description "]") "")
  7244. "]"))
  7245. (defconst org-link-escape-chars
  7246. '((?\ . "%20")
  7247. (?\[ . "%5B")
  7248. (?\] . "%5D")
  7249. (?\340 . "%E0") ; `a
  7250. (?\342 . "%E2") ; ^a
  7251. (?\347 . "%E7") ; ,c
  7252. (?\350 . "%E8") ; `e
  7253. (?\351 . "%E9") ; 'e
  7254. (?\352 . "%EA") ; ^e
  7255. (?\356 . "%EE") ; ^i
  7256. (?\364 . "%F4") ; ^o
  7257. (?\371 . "%F9") ; `u
  7258. (?\373 . "%FB") ; ^u
  7259. (?\; . "%3B")
  7260. ;; (?? . "%3F")
  7261. (?= . "%3D")
  7262. (?+ . "%2B")
  7263. )
  7264. "Association list of escapes for some characters problematic in links.
  7265. This is the list that is used for internal purposes.")
  7266. (defvar org-url-encoding-use-url-hexify nil)
  7267. (defconst org-link-escape-chars-browser
  7268. '((?\ . "%20")) ; 32 for the SPC char
  7269. "Association list of escapes for some characters problematic in links.
  7270. This is the list that is used before handing over to the browser.")
  7271. (defun org-link-escape (text &optional table)
  7272. "Escape characters in TEXT that are problematic for links."
  7273. (if (and org-url-encoding-use-url-hexify (not table))
  7274. (url-hexify-string text)
  7275. (setq table (or table org-link-escape-chars))
  7276. (when text
  7277. (let ((re (mapconcat (lambda (x) (regexp-quote
  7278. (char-to-string (car x))))
  7279. table "\\|")))
  7280. (while (string-match re text)
  7281. (setq text
  7282. (replace-match
  7283. (cdr (assoc (string-to-char (match-string 0 text))
  7284. table))
  7285. t t text)))
  7286. text))))
  7287. (defun org-link-unescape (text &optional table)
  7288. "Reverse the action of `org-link-escape'."
  7289. (if (and org-url-encoding-use-url-hexify (not table))
  7290. (url-unhex-string text)
  7291. (setq table (or table org-link-escape-chars))
  7292. (when text
  7293. (let ((case-fold-search t)
  7294. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7295. table "\\|")))
  7296. (while (string-match re text)
  7297. (setq text
  7298. (replace-match
  7299. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7300. table)))
  7301. t t text)))
  7302. text))))
  7303. (defun org-xor (a b)
  7304. "Exclusive or."
  7305. (if a (not b) b))
  7306. (defun org-fixup-message-id-for-http (s)
  7307. "Replace special characters in a message id, so it can be used in an http query."
  7308. (while (string-match "<" s)
  7309. (setq s (replace-match "%3C" t t s)))
  7310. (while (string-match ">" s)
  7311. (setq s (replace-match "%3E" t t s)))
  7312. (while (string-match "@" s)
  7313. (setq s (replace-match "%40" t t s)))
  7314. s)
  7315. ;;;###autoload
  7316. (defun org-insert-link-global ()
  7317. "Insert a link like Org-mode does.
  7318. This command can be called in any mode to insert a link in Org-mode syntax."
  7319. (interactive)
  7320. (org-load-modules-maybe)
  7321. (org-run-like-in-org-mode 'org-insert-link))
  7322. (defun org-insert-link (&optional complete-file link-location)
  7323. "Insert a link. At the prompt, enter the link.
  7324. Completion can be used to insert any of the link protocol prefixes like
  7325. http or ftp in use.
  7326. The history can be used to select a link previously stored with
  7327. `org-store-link'. When the empty string is entered (i.e. if you just
  7328. press RET at the prompt), the link defaults to the most recently
  7329. stored link. As SPC triggers completion in the minibuffer, you need to
  7330. use M-SPC or C-q SPC to force the insertion of a space character.
  7331. You will also be prompted for a description, and if one is given, it will
  7332. be displayed in the buffer instead of the link.
  7333. If there is already a link at point, this command will allow you to edit link
  7334. and description parts.
  7335. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7336. be selected using completion. The path to the file will be relative to the
  7337. current directory if the file is in the current directory or a subdirectory.
  7338. Otherwise, the link will be the absolute path as completed in the minibuffer
  7339. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7340. option `org-link-file-path-type'.
  7341. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7342. the current directory or below.
  7343. With three \\[universal-argument] prefixes, negate the meaning of
  7344. `org-keep-stored-link-after-insertion'.
  7345. If `org-make-link-description-function' is non-nil, this function will be
  7346. called with the link target, and the result will be the default
  7347. link description.
  7348. If the LINK-LOCATION parameter is non-nil, this value will be
  7349. used as the link location instead of reading one interactively."
  7350. (interactive "P")
  7351. (let* ((wcf (current-window-configuration))
  7352. (region (if (org-region-active-p)
  7353. (buffer-substring (region-beginning) (region-end))))
  7354. (remove (and region (list (region-beginning) (region-end))))
  7355. (desc region)
  7356. tmphist ; byte-compile incorrectly complains about this
  7357. (link link-location)
  7358. entry file all-prefixes)
  7359. (cond
  7360. (link-location) ; specified by arg, just use it.
  7361. ((org-in-regexp org-bracket-link-regexp 1)
  7362. ;; We do have a link at point, and we are going to edit it.
  7363. (setq remove (list (match-beginning 0) (match-end 0)))
  7364. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7365. (setq link (read-string "Link: "
  7366. (org-link-unescape
  7367. (org-match-string-no-properties 1)))))
  7368. ((or (org-in-regexp org-angle-link-re)
  7369. (org-in-regexp org-plain-link-re))
  7370. ;; Convert to bracket link
  7371. (setq remove (list (match-beginning 0) (match-end 0))
  7372. link (read-string "Link: "
  7373. (org-remove-angle-brackets (match-string 0)))))
  7374. ((member complete-file '((4) (16)))
  7375. ;; Completing read for file names.
  7376. (setq link (org-file-complete-link complete-file)))
  7377. (t
  7378. ;; Read link, with completion for stored links.
  7379. (with-output-to-temp-buffer "*Org Links*"
  7380. (princ "Insert a link.
  7381. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7382. (when org-stored-links
  7383. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7384. (princ (mapconcat
  7385. (lambda (x)
  7386. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7387. (reverse org-stored-links) "\n"))))
  7388. (let ((cw (selected-window)))
  7389. (select-window (get-buffer-window "*Org Links*" 'visible))
  7390. (setq truncate-lines t)
  7391. (unless (pos-visible-in-window-p (point-max))
  7392. (org-fit-window-to-buffer))
  7393. (and (window-live-p cw) (select-window cw)))
  7394. ;; Fake a link history, containing the stored links.
  7395. (setq tmphist (append (mapcar 'car org-stored-links)
  7396. org-insert-link-history))
  7397. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7398. (mapcar 'car org-link-abbrev-alist)
  7399. org-link-types))
  7400. (unwind-protect
  7401. (progn
  7402. (setq link
  7403. (let ((org-completion-use-ido nil)
  7404. (org-completion-use-iswitchb nil))
  7405. (org-completing-read
  7406. "Link: "
  7407. (append
  7408. (mapcar (lambda (x) (list (concat x ":")))
  7409. all-prefixes)
  7410. (mapcar 'car org-stored-links))
  7411. nil nil nil
  7412. 'tmphist
  7413. (car (car org-stored-links)))))
  7414. (if (not (string-match "\\S-" link))
  7415. (error "No link selected"))
  7416. (if (or (member link all-prefixes)
  7417. (and (equal ":" (substring link -1))
  7418. (member (substring link 0 -1) all-prefixes)
  7419. (setq link (substring link 0 -1))))
  7420. (setq link (org-link-try-special-completion link))))
  7421. (set-window-configuration wcf)
  7422. (kill-buffer "*Org Links*"))
  7423. (setq entry (assoc link org-stored-links))
  7424. (or entry (push link org-insert-link-history))
  7425. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7426. (not org-keep-stored-link-after-insertion))
  7427. (setq org-stored-links (delq (assoc link org-stored-links)
  7428. org-stored-links)))
  7429. (setq desc (or desc (nth 1 entry)))))
  7430. (if (string-match org-plain-link-re link)
  7431. ;; URL-like link, normalize the use of angular brackets.
  7432. (setq link (org-make-link (org-remove-angle-brackets link))))
  7433. ;; Check if we are linking to the current file with a search option
  7434. ;; If yes, simplify the link by using only the search option.
  7435. (when (and buffer-file-name
  7436. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7437. (let* ((path (match-string 1 link))
  7438. (case-fold-search nil)
  7439. (search (match-string 2 link)))
  7440. (save-match-data
  7441. (if (equal (file-truename buffer-file-name) (file-truename path))
  7442. ;; We are linking to this same file, with a search option
  7443. (setq link search)))))
  7444. ;; Check if we can/should use a relative path. If yes, simplify the link
  7445. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7446. (let* ((type (match-string 1 link))
  7447. (path (match-string 2 link))
  7448. (origpath path)
  7449. (case-fold-search nil))
  7450. (cond
  7451. ((or (eq org-link-file-path-type 'absolute)
  7452. (equal complete-file '(16)))
  7453. (setq path (abbreviate-file-name (expand-file-name path))))
  7454. ((eq org-link-file-path-type 'noabbrev)
  7455. (setq path (expand-file-name path)))
  7456. ((eq org-link-file-path-type 'relative)
  7457. (setq path (file-relative-name path)))
  7458. (t
  7459. (save-match-data
  7460. (if (string-match (concat "^" (regexp-quote
  7461. (file-name-as-directory
  7462. (expand-file-name "."))))
  7463. (expand-file-name path))
  7464. ;; We are linking a file with relative path name.
  7465. (setq path (substring (expand-file-name path)
  7466. (match-end 0)))
  7467. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7468. (setq link (concat type path))
  7469. (if (equal desc origpath)
  7470. (setq desc path))))
  7471. (if org-make-link-description-function
  7472. (setq desc (funcall org-make-link-description-function link desc)))
  7473. (setq desc (read-string "Description: " desc))
  7474. (unless (string-match "\\S-" desc) (setq desc nil))
  7475. (if remove (apply 'delete-region remove))
  7476. (insert (org-make-link-string link desc))))
  7477. (defun org-link-try-special-completion (type)
  7478. "If there is completion support for link type TYPE, offer it."
  7479. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7480. (if (functionp fun)
  7481. (funcall fun)
  7482. (read-string "Link (no completion support): " (concat type ":")))))
  7483. (defun org-file-complete-link (&optional arg)
  7484. "Create a file link using completion."
  7485. (let (file link)
  7486. (setq file (read-file-name "File: "))
  7487. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7488. (pwd1 (file-name-as-directory (abbreviate-file-name
  7489. (expand-file-name ".")))))
  7490. (cond
  7491. ((equal arg '(16))
  7492. (setq link (org-make-link
  7493. "file:"
  7494. (abbreviate-file-name (expand-file-name file)))))
  7495. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7496. (setq link (org-make-link "file:" (match-string 1 file))))
  7497. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7498. (expand-file-name file))
  7499. (setq link (org-make-link
  7500. "file:" (match-string 1 (expand-file-name file)))))
  7501. (t (setq link (org-make-link "file:" file)))))
  7502. link))
  7503. (defun org-completing-read (&rest args)
  7504. "Completing-read with SPACE being a normal character."
  7505. (let ((minibuffer-local-completion-map
  7506. (copy-keymap minibuffer-local-completion-map)))
  7507. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7508. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7509. (apply 'org-icompleting-read args)))
  7510. (defun org-completing-read-no-i (&rest args)
  7511. (let (org-completion-use-ido org-completion-use-iswitchb)
  7512. (apply 'org-completing-read args)))
  7513. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7514. "Use iswitch as a completing-read replacement to choose from choices.
  7515. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7516. from."
  7517. (let* ((iswitchb-use-virtual-buffers nil)
  7518. (iswitchb-make-buflist-hook
  7519. (lambda ()
  7520. (setq iswitchb-temp-buflist choices))))
  7521. (iswitchb-read-buffer prompt)))
  7522. (defun org-icompleting-read (&rest args)
  7523. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7524. (org-without-partial-completion
  7525. (if (and org-completion-use-ido
  7526. (fboundp 'ido-completing-read)
  7527. (boundp 'ido-mode) ido-mode
  7528. (listp (second args)))
  7529. (let ((ido-enter-matching-directory nil))
  7530. (apply 'ido-completing-read (concat (car args))
  7531. (if (consp (car (nth 1 args)))
  7532. (mapcar (lambda (x) (car x)) (nth 1 args))
  7533. (nth 1 args))
  7534. (cddr args)))
  7535. (if (and org-completion-use-iswitchb
  7536. (boundp 'iswitchb-mode) iswitchb-mode
  7537. (listp (second args)))
  7538. (apply 'org-iswitchb-completing-read (concat (car args))
  7539. (if (consp (car (nth 1 args)))
  7540. (mapcar (lambda (x) (car x)) (nth 1 args))
  7541. (nth 1 args))
  7542. (cddr args))
  7543. (apply 'completing-read args)))))
  7544. (defun org-extract-attributes (s)
  7545. "Extract the attributes cookie from a string and set as text property."
  7546. (let (a attr (start 0) key value)
  7547. (save-match-data
  7548. (when (string-match "{{\\([^}]+\\)}}$" s)
  7549. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7550. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7551. (setq key (match-string 1 a) value (match-string 2 a)
  7552. start (match-end 0)
  7553. attr (plist-put attr (intern key) value))))
  7554. (org-add-props s nil 'org-attr attr))
  7555. s))
  7556. (defun org-extract-attributes-from-string (tag)
  7557. (let (key value attr)
  7558. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7559. (setq key (match-string 1 tag) value (match-string 2 tag)
  7560. tag (replace-match "" t t tag)
  7561. attr (plist-put attr (intern key) value)))
  7562. (cons tag attr)))
  7563. (defun org-attributes-to-string (plist)
  7564. "Format a property list into an HTML attribute list."
  7565. (let ((s "") key value)
  7566. (while plist
  7567. (setq key (pop plist) value (pop plist))
  7568. (and value
  7569. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7570. s))
  7571. ;;; Opening/following a link
  7572. (defvar org-link-search-failed nil)
  7573. (defvar org-open-link-functions nil
  7574. "Hook for functions finding a plain text link.
  7575. These functions must take a single argument, the link content.
  7576. They will be called for links that look like [[link text][description]]
  7577. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7578. like a filename (e.g. \"./blue.png\").
  7579. These functions will be called *before* Org attempts to resolve the
  7580. link by doing text searches in the current buffer - so if you want a
  7581. link \"[[target]]\" to still find \"<<target>>\", your function should
  7582. handle this as a special case.
  7583. When the function does handle the link, it must return a non-nil value.
  7584. If it decides that it is not responsible for this link, it must return
  7585. nil to indicate that that Org-mode can continue with other options
  7586. like exact and fuzzy text search.")
  7587. (defun org-next-link ()
  7588. "Move forward to the next link.
  7589. If the link is in hidden text, expose it."
  7590. (interactive)
  7591. (when (and org-link-search-failed (eq this-command last-command))
  7592. (goto-char (point-min))
  7593. (message "Link search wrapped back to beginning of buffer"))
  7594. (setq org-link-search-failed nil)
  7595. (let* ((pos (point))
  7596. (ct (org-context))
  7597. (a (assoc :link ct)))
  7598. (if a (goto-char (nth 2 a)))
  7599. (if (re-search-forward org-any-link-re nil t)
  7600. (progn
  7601. (goto-char (match-beginning 0))
  7602. (if (org-invisible-p) (org-show-context)))
  7603. (goto-char pos)
  7604. (setq org-link-search-failed t)
  7605. (error "No further link found"))))
  7606. (defun org-previous-link ()
  7607. "Move backward to the previous link.
  7608. If the link is in hidden text, expose it."
  7609. (interactive)
  7610. (when (and org-link-search-failed (eq this-command last-command))
  7611. (goto-char (point-max))
  7612. (message "Link search wrapped back to end of buffer"))
  7613. (setq org-link-search-failed nil)
  7614. (let* ((pos (point))
  7615. (ct (org-context))
  7616. (a (assoc :link ct)))
  7617. (if a (goto-char (nth 1 a)))
  7618. (if (re-search-backward org-any-link-re nil t)
  7619. (progn
  7620. (goto-char (match-beginning 0))
  7621. (if (org-invisible-p) (org-show-context)))
  7622. (goto-char pos)
  7623. (setq org-link-search-failed t)
  7624. (error "No further link found"))))
  7625. (defun org-translate-link (s)
  7626. "Translate a link string if a translation function has been defined."
  7627. (if (and org-link-translation-function
  7628. (fboundp org-link-translation-function)
  7629. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7630. (progn
  7631. (setq s (funcall org-link-translation-function
  7632. (match-string 1) (match-string 2)))
  7633. (concat (car s) ":" (cdr s)))
  7634. s))
  7635. (defun org-translate-link-from-planner (type path)
  7636. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7637. This is still an experimental function, your mileage may vary."
  7638. (cond
  7639. ((member type '("http" "https" "news" "ftp"))
  7640. ;; standard Internet links are the same.
  7641. nil)
  7642. ((and (equal type "irc") (string-match "^//" path))
  7643. ;; Planner has two / at the beginning of an irc link, we have 1.
  7644. ;; We should have zero, actually....
  7645. (setq path (substring path 1)))
  7646. ((and (equal type "lisp") (string-match "^/" path))
  7647. ;; Planner has a slash, we do not.
  7648. (setq type "elisp" path (substring path 1)))
  7649. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7650. ;; A typical message link. Planner has the id after the final slash,
  7651. ;; we separate it with a hash mark
  7652. (setq path (concat (match-string 1 path) "#"
  7653. (org-remove-angle-brackets (match-string 2 path)))))
  7654. )
  7655. (cons type path))
  7656. (defun org-find-file-at-mouse (ev)
  7657. "Open file link or URL at mouse."
  7658. (interactive "e")
  7659. (mouse-set-point ev)
  7660. (org-open-at-point 'in-emacs))
  7661. (defun org-open-at-mouse (ev)
  7662. "Open file link or URL at mouse."
  7663. (interactive "e")
  7664. (mouse-set-point ev)
  7665. (if (eq major-mode 'org-agenda-mode)
  7666. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7667. (org-open-at-point))
  7668. (defvar org-window-config-before-follow-link nil
  7669. "The window configuration before following a link.
  7670. This is saved in case the need arises to restore it.")
  7671. (defvar org-open-link-marker (make-marker)
  7672. "Marker pointing to the location where `org-open-at-point; was called.")
  7673. ;;;###autoload
  7674. (defun org-open-at-point-global ()
  7675. "Follow a link like Org-mode does.
  7676. This command can be called in any mode to follow a link that has
  7677. Org-mode syntax."
  7678. (interactive)
  7679. (org-run-like-in-org-mode 'org-open-at-point))
  7680. ;;;###autoload
  7681. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7682. "Open a link in the string S, as if it was in Org-mode."
  7683. (interactive "sLink: \nP")
  7684. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7685. (with-temp-buffer
  7686. (let ((org-inhibit-startup t))
  7687. (org-mode)
  7688. (insert s)
  7689. (goto-char (point-min))
  7690. (when reference-buffer
  7691. (setq org-link-abbrev-alist-local
  7692. (with-current-buffer reference-buffer
  7693. org-link-abbrev-alist-local)))
  7694. (org-open-at-point arg reference-buffer)))))
  7695. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7696. "Open link at or after point.
  7697. If there is no link at point, this function will search forward up to
  7698. the end of the current line.
  7699. Normally, files will be opened by an appropriate application. If the
  7700. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7701. With a double prefix argument, try to open outside of Emacs, in the
  7702. application the system uses for this file type."
  7703. (interactive "P")
  7704. (org-load-modules-maybe)
  7705. (move-marker org-open-link-marker (point))
  7706. (setq org-window-config-before-follow-link (current-window-configuration))
  7707. (org-remove-occur-highlights nil nil t)
  7708. (cond
  7709. ((and (org-on-heading-p)
  7710. (not (org-in-regexp
  7711. (concat org-plain-link-re "\\|"
  7712. org-bracket-link-regexp "\\|"
  7713. org-angle-link-re "\\|"
  7714. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7715. (not (get-text-property (point) 'org-linked-text)))
  7716. (or (org-offer-links-in-entry in-emacs)
  7717. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7718. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7719. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7720. (org-footnote-action))
  7721. (t
  7722. (let (type path link line search (pos (point)))
  7723. (catch 'match
  7724. (save-excursion
  7725. (skip-chars-forward "^]\n\r")
  7726. (when (org-in-regexp org-bracket-link-regexp 1)
  7727. (setq link (org-extract-attributes
  7728. (org-link-unescape (org-match-string-no-properties 1))))
  7729. (while (string-match " *\n *" link)
  7730. (setq link (replace-match " " t t link)))
  7731. (setq link (org-link-expand-abbrev link))
  7732. (cond
  7733. ((or (file-name-absolute-p link)
  7734. (string-match "^\\.\\.?/" link))
  7735. (setq type "file" path link))
  7736. ((string-match org-link-re-with-space3 link)
  7737. (setq type (match-string 1 link) path (match-string 2 link)))
  7738. (t (setq type "thisfile" path link)))
  7739. (throw 'match t)))
  7740. (when (get-text-property (point) 'org-linked-text)
  7741. (setq type "thisfile"
  7742. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7743. (1+ (point)) (point))
  7744. path (buffer-substring
  7745. (previous-single-property-change pos 'org-linked-text)
  7746. (next-single-property-change pos 'org-linked-text)))
  7747. (throw 'match t))
  7748. (save-excursion
  7749. (when (or (org-in-regexp org-angle-link-re)
  7750. (org-in-regexp org-plain-link-re))
  7751. (setq type (match-string 1) path (match-string 2))
  7752. (throw 'match t)))
  7753. (save-excursion
  7754. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7755. (setq type "tags"
  7756. path (match-string 1))
  7757. (while (string-match ":" path)
  7758. (setq path (replace-match "+" t t path)))
  7759. (throw 'match t)))
  7760. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7761. (setq type "tree-match"
  7762. path (match-string 1))
  7763. (throw 'match t)))
  7764. (unless path
  7765. (error "No link found"))
  7766. ;; switch back to reference buffer
  7767. ;; needed when if called in a temporary buffer through
  7768. ;; org-open-link-from-string
  7769. (with-current-buffer (or reference-buffer (current-buffer))
  7770. ;; Remove any trailing spaces in path
  7771. (if (string-match " +\\'" path)
  7772. (setq path (replace-match "" t t path)))
  7773. (if (and org-link-translation-function
  7774. (fboundp org-link-translation-function))
  7775. ;; Check if we need to translate the link
  7776. (let ((tmp (funcall org-link-translation-function type path)))
  7777. (setq type (car tmp) path (cdr tmp))))
  7778. (cond
  7779. ((assoc type org-link-protocols)
  7780. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7781. ((equal type "mailto")
  7782. (let ((cmd (car org-link-mailto-program))
  7783. (args (cdr org-link-mailto-program)) args1
  7784. (address path) (subject "") a)
  7785. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7786. (setq address (match-string 1 path)
  7787. subject (org-link-escape (match-string 2 path))))
  7788. (while args
  7789. (cond
  7790. ((not (stringp (car args))) (push (pop args) args1))
  7791. (t (setq a (pop args))
  7792. (if (string-match "%a" a)
  7793. (setq a (replace-match address t t a)))
  7794. (if (string-match "%s" a)
  7795. (setq a (replace-match subject t t a)))
  7796. (push a args1))))
  7797. (apply cmd (nreverse args1))))
  7798. ((member type '("http" "https" "ftp" "news"))
  7799. (browse-url (concat type ":" (org-link-escape
  7800. path org-link-escape-chars-browser))))
  7801. ((member type '("message"))
  7802. (browse-url (concat type ":" path)))
  7803. ((string= type "tags")
  7804. (org-tags-view in-emacs path))
  7805. ((string= type "tree-match")
  7806. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7807. ((string= type "file")
  7808. (if (string-match "::\\([0-9]+\\)\\'" path)
  7809. (setq line (string-to-number (match-string 1 path))
  7810. path (substring path 0 (match-beginning 0)))
  7811. (if (string-match "::\\(.+\\)\\'" path)
  7812. (setq search (match-string 1 path)
  7813. path (substring path 0 (match-beginning 0)))))
  7814. (if (string-match "[*?{]" (file-name-nondirectory path))
  7815. (dired path)
  7816. (org-open-file path in-emacs line search)))
  7817. ((string= type "news")
  7818. (require 'org-gnus)
  7819. (org-gnus-follow-link path))
  7820. ((string= type "shell")
  7821. (let ((cmd path))
  7822. (if (or (not org-confirm-shell-link-function)
  7823. (funcall org-confirm-shell-link-function
  7824. (format "Execute \"%s\" in shell? "
  7825. (org-add-props cmd nil
  7826. 'face 'org-warning))))
  7827. (progn
  7828. (message "Executing %s" cmd)
  7829. (shell-command cmd))
  7830. (error "Abort"))))
  7831. ((string= type "elisp")
  7832. (let ((cmd path))
  7833. (if (or (not org-confirm-elisp-link-function)
  7834. (funcall org-confirm-elisp-link-function
  7835. (format "Execute \"%s\" as elisp? "
  7836. (org-add-props cmd nil
  7837. 'face 'org-warning))))
  7838. (message "%s => %s" cmd
  7839. (if (equal (string-to-char cmd) ?\()
  7840. (eval (read cmd))
  7841. (call-interactively (read cmd))))
  7842. (error "Abort"))))
  7843. ((and (string= type "thisfile")
  7844. (run-hook-with-args-until-success
  7845. 'org-open-link-functions path)))
  7846. ((string= type "thisfile")
  7847. (if in-emacs
  7848. (switch-to-buffer-other-window
  7849. (org-get-buffer-for-internal-link (current-buffer)))
  7850. (org-mark-ring-push))
  7851. (let ((cmd `(org-link-search
  7852. ,path
  7853. ,(cond ((equal in-emacs '(4)) 'occur)
  7854. ((equal in-emacs '(16)) 'org-occur)
  7855. (t nil))
  7856. ,pos)))
  7857. (condition-case nil (eval cmd)
  7858. (error (progn (widen) (eval cmd))))))
  7859. (t
  7860. (browse-url-at-point)))))))
  7861. (move-marker org-open-link-marker nil)
  7862. (run-hook-with-args 'org-follow-link-hook))
  7863. (defun org-offer-links-in-entry (&optional nth zero)
  7864. "Offer links in the current entry and follow the selected link.
  7865. If there is only one link, follow it immediately as well.
  7866. If NTH is an integer, immediately pick the NTH link found.
  7867. If ZERO is a string, check also this string for a link, and if
  7868. there is one, offer it as link number zero."
  7869. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7870. "\\(" org-angle-link-re "\\)\\|"
  7871. "\\(" org-plain-link-re "\\)"))
  7872. (cnt ?0)
  7873. (in-emacs (if (integerp nth) nil nth))
  7874. have-zero end links link c)
  7875. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7876. (push (match-string 0 zero) links)
  7877. (setq cnt (1- cnt) have-zero t))
  7878. (save-excursion
  7879. (org-back-to-heading t)
  7880. (setq end (save-excursion (outline-next-heading) (point)))
  7881. (while (re-search-forward re end t)
  7882. (push (match-string 0) links))
  7883. (setq links (org-uniquify (reverse links))))
  7884. (cond
  7885. ((null links)
  7886. (message "No links"))
  7887. ((equal (length links) 1)
  7888. (setq link (list (car links))))
  7889. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7890. (setq link (nth (if have-zero nth (1- nth)) links)))
  7891. (t ; we have to select a link
  7892. (save-excursion
  7893. (save-window-excursion
  7894. (delete-other-windows)
  7895. (with-output-to-temp-buffer "*Select Link*"
  7896. (mapc (lambda (l)
  7897. (if (not (string-match org-bracket-link-regexp l))
  7898. (princ (format "[%c] %s\n" (incf cnt)
  7899. (org-remove-angle-brackets l)))
  7900. (if (match-end 3)
  7901. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7902. (match-string 3 l) (match-string 1 l)))
  7903. (princ (format "[%c] %s\n" (incf cnt)
  7904. (match-string 1 l))))))
  7905. links))
  7906. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7907. (message "Select link to open, RET to open all:")
  7908. (setq c (read-char-exclusive))
  7909. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7910. (when (equal c ?q) (error "Abort"))
  7911. (if (equal c ?\C-m)
  7912. (setq link links)
  7913. (setq nth (- c ?0))
  7914. (if have-zero (setq nth (1+ nth)))
  7915. (unless (and (integerp nth) (>= (length links) nth))
  7916. (error "Invalid link selection"))
  7917. (setq link (list (nth (1- nth) links))))))
  7918. (if link
  7919. (let ((buf (current-buffer)))
  7920. (dolist (l link)
  7921. (org-open-link-from-string l in-emacs buf))
  7922. t)
  7923. nil)))
  7924. ;; Add special file links that specify the way of opening
  7925. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7926. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7927. (defun org-open-file-with-system (path)
  7928. "Open file at PATH using the system way of opeing it."
  7929. (org-open-file path 'system))
  7930. (defun org-open-file-with-emacs (path)
  7931. "Open file at PATH in emacs."
  7932. (org-open-file path 'emacs))
  7933. (defun org-remove-file-link-modifiers ()
  7934. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7935. (goto-char (point-min))
  7936. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7937. (org-if-unprotected
  7938. (replace-match "file:" t t))))
  7939. (eval-after-load "org-exp"
  7940. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7941. 'org-remove-file-link-modifiers))
  7942. ;;;; Time estimates
  7943. (defun org-get-effort (&optional pom)
  7944. "Get the effort estimate for the current entry."
  7945. (org-entry-get pom org-effort-property))
  7946. ;;; File search
  7947. (defvar org-create-file-search-functions nil
  7948. "List of functions to construct the right search string for a file link.
  7949. These functions are called in turn with point at the location to
  7950. which the link should point.
  7951. A function in the hook should first test if it would like to
  7952. handle this file type, for example by checking the major-mode or
  7953. the file extension. If it decides not to handle this file, it
  7954. should just return nil to give other functions a chance. If it
  7955. does handle the file, it must return the search string to be used
  7956. when following the link. The search string will be part of the
  7957. file link, given after a double colon, and `org-open-at-point'
  7958. will automatically search for it. If special measures must be
  7959. taken to make the search successful, another function should be
  7960. added to the companion hook `org-execute-file-search-functions',
  7961. which see.
  7962. A function in this hook may also use `setq' to set the variable
  7963. `description' to provide a suggestion for the descriptive text to
  7964. be used for this link when it gets inserted into an Org-mode
  7965. buffer with \\[org-insert-link].")
  7966. (defvar org-execute-file-search-functions nil
  7967. "List of functions to execute a file search triggered by a link.
  7968. Functions added to this hook must accept a single argument, the
  7969. search string that was part of the file link, the part after the
  7970. double colon. The function must first check if it would like to
  7971. handle this search, for example by checking the major-mode or the
  7972. file extension. If it decides not to handle this search, it
  7973. should just return nil to give other functions a chance. If it
  7974. does handle the search, it must return a non-nil value to keep
  7975. other functions from trying.
  7976. Each function can access the current prefix argument through the
  7977. variable `current-prefix-argument'. Note that a single prefix is
  7978. used to force opening a link in Emacs, so it may be good to only
  7979. use a numeric or double prefix to guide the search function.
  7980. In case this is needed, a function in this hook can also restore
  7981. the window configuration before `org-open-at-point' was called using:
  7982. (set-window-configuration org-window-config-before-follow-link)")
  7983. (defun org-link-search (s &optional type avoid-pos)
  7984. "Search for a link search option.
  7985. If S is surrounded by forward slashes, it is interpreted as a
  7986. regular expression. In org-mode files, this will create an `org-occur'
  7987. sparse tree. In ordinary files, `occur' will be used to list matches.
  7988. If the current buffer is in `dired-mode', grep will be used to search
  7989. in all files. If AVOID-POS is given, ignore matches near that position."
  7990. (let ((case-fold-search t)
  7991. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7992. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7993. (append '(("") (" ") ("\t") ("\n"))
  7994. org-emphasis-alist)
  7995. "\\|") "\\)"))
  7996. (pos (point))
  7997. (pre nil) (post nil)
  7998. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7999. (cond
  8000. ;; First check if there are any special
  8001. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8002. ;; Now try the builtin stuff
  8003. ((and (equal (string-to-char s0) ?#)
  8004. (> (length s0) 1)
  8005. (save-excursion
  8006. (goto-char (point-min))
  8007. (and
  8008. (re-search-forward
  8009. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8010. (setq type 'dedicated
  8011. pos (match-beginning 0))))
  8012. ;; There is an exact target for this
  8013. (goto-char pos)
  8014. (org-back-to-heading t)))
  8015. ((save-excursion
  8016. (goto-char (point-min))
  8017. (and
  8018. (re-search-forward
  8019. (concat "<<" (regexp-quote s0) ">>") nil t)
  8020. (setq type 'dedicated
  8021. pos (match-beginning 0))))
  8022. ;; There is an exact target for this
  8023. (goto-char pos))
  8024. ((and (string-match "^(\\(.*\\))$" s0)
  8025. (save-excursion
  8026. (goto-char (point-min))
  8027. (and
  8028. (re-search-forward
  8029. (concat "[^[]" (regexp-quote
  8030. (format org-coderef-label-format
  8031. (match-string 1 s0))))
  8032. nil t)
  8033. (setq type 'dedicated
  8034. pos (1+ (match-beginning 0))))))
  8035. ;; There is a coderef target for this
  8036. (goto-char pos))
  8037. ((string-match "^/\\(.*\\)/$" s)
  8038. ;; A regular expression
  8039. (cond
  8040. ((org-mode-p)
  8041. (org-occur (match-string 1 s)))
  8042. ;;((eq major-mode 'dired-mode)
  8043. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8044. (t (org-do-occur (match-string 1 s)))))
  8045. (t
  8046. ;; A normal search strings
  8047. (when (equal (string-to-char s) ?*)
  8048. ;; Anchor on headlines, post may include tags.
  8049. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8050. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8051. s (substring s 1)))
  8052. (remove-text-properties
  8053. 0 (length s)
  8054. '(face nil mouse-face nil keymap nil fontified nil) s)
  8055. ;; Make a series of regular expressions to find a match
  8056. (setq words (org-split-string s "[ \n\r\t]+")
  8057. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8058. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8059. "\\)" markers)
  8060. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8061. re2a (concat "[ \t\r\n]" re2a_)
  8062. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8063. re4 (concat "[^a-zA-Z_]" re4_)
  8064. re1 (concat pre re2 post)
  8065. re3 (concat pre (if pre re4_ re4) post)
  8066. re5 (concat pre ".*" re4)
  8067. re2 (concat pre re2)
  8068. re2a (concat pre (if pre re2a_ re2a))
  8069. re4 (concat pre (if pre re4_ re4))
  8070. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8071. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8072. re5 "\\)"
  8073. ))
  8074. (cond
  8075. ((eq type 'org-occur) (org-occur reall))
  8076. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8077. (t (goto-char (point-min))
  8078. (setq type 'fuzzy)
  8079. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8080. (org-search-not-self 1 re1 nil t)
  8081. (org-search-not-self 1 re2 nil t)
  8082. (org-search-not-self 1 re2a nil t)
  8083. (org-search-not-self 1 re3 nil t)
  8084. (org-search-not-self 1 re4 nil t)
  8085. (org-search-not-self 1 re5 nil t)
  8086. )
  8087. (goto-char (match-beginning 1))
  8088. (goto-char pos)
  8089. (error "No match")))))
  8090. (t
  8091. ;; Normal string-search
  8092. (goto-char (point-min))
  8093. (if (search-forward s nil t)
  8094. (goto-char (match-beginning 0))
  8095. (error "No match"))))
  8096. (and (org-mode-p) (org-show-context 'link-search))
  8097. type))
  8098. (defun org-search-not-self (group &rest args)
  8099. "Execute `re-search-forward', but only accept matches that do not
  8100. enclose the position of `org-open-link-marker'."
  8101. (let ((m org-open-link-marker))
  8102. (catch 'exit
  8103. (while (apply 're-search-forward args)
  8104. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8105. (goto-char (match-end group))
  8106. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8107. (> (match-beginning 0) (marker-position m))
  8108. (< (match-end 0) (marker-position m)))
  8109. (save-match-data
  8110. (or (not (org-in-regexp
  8111. org-bracket-link-analytic-regexp 1))
  8112. (not (match-end 4)) ; no description
  8113. (and (<= (match-beginning 4) (point))
  8114. (>= (match-end 4) (point))))))
  8115. (throw 'exit (point))))))))
  8116. (defun org-get-buffer-for-internal-link (buffer)
  8117. "Return a buffer to be used for displaying the link target of internal links."
  8118. (cond
  8119. ((not org-display-internal-link-with-indirect-buffer)
  8120. buffer)
  8121. ((string-match "(Clone)$" (buffer-name buffer))
  8122. (message "Buffer is already a clone, not making another one")
  8123. ;; we also do not modify visibility in this case
  8124. buffer)
  8125. (t ; make a new indirect buffer for displaying the link
  8126. (let* ((bn (buffer-name buffer))
  8127. (ibn (concat bn "(Clone)"))
  8128. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8129. (with-current-buffer ib (org-overview))
  8130. ib))))
  8131. (defun org-do-occur (regexp &optional cleanup)
  8132. "Call the Emacs command `occur'.
  8133. If CLEANUP is non-nil, remove the printout of the regular expression
  8134. in the *Occur* buffer. This is useful if the regex is long and not useful
  8135. to read."
  8136. (occur regexp)
  8137. (when cleanup
  8138. (let ((cwin (selected-window)) win beg end)
  8139. (when (setq win (get-buffer-window "*Occur*"))
  8140. (select-window win))
  8141. (goto-char (point-min))
  8142. (when (re-search-forward "match[a-z]+" nil t)
  8143. (setq beg (match-end 0))
  8144. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8145. (setq end (1- (match-beginning 0)))))
  8146. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8147. (goto-char (point-min))
  8148. (select-window cwin))))
  8149. ;;; The mark ring for links jumps
  8150. (defvar org-mark-ring nil
  8151. "Mark ring for positions before jumps in Org-mode.")
  8152. (defvar org-mark-ring-last-goto nil
  8153. "Last position in the mark ring used to go back.")
  8154. ;; Fill and close the ring
  8155. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8156. (loop for i from 1 to org-mark-ring-length do
  8157. (push (make-marker) org-mark-ring))
  8158. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8159. org-mark-ring)
  8160. (defun org-mark-ring-push (&optional pos buffer)
  8161. "Put the current position or POS into the mark ring and rotate it."
  8162. (interactive)
  8163. (setq pos (or pos (point)))
  8164. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8165. (move-marker (car org-mark-ring)
  8166. (or pos (point))
  8167. (or buffer (current-buffer)))
  8168. (message "%s"
  8169. (substitute-command-keys
  8170. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8171. (defun org-mark-ring-goto (&optional n)
  8172. "Jump to the previous position in the mark ring.
  8173. With prefix arg N, jump back that many stored positions. When
  8174. called several times in succession, walk through the entire ring.
  8175. Org-mode commands jumping to a different position in the current file,
  8176. or to another Org-mode file, automatically push the old position
  8177. onto the ring."
  8178. (interactive "p")
  8179. (let (p m)
  8180. (if (eq last-command this-command)
  8181. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8182. (setq p org-mark-ring))
  8183. (setq org-mark-ring-last-goto p)
  8184. (setq m (car p))
  8185. (switch-to-buffer (marker-buffer m))
  8186. (goto-char m)
  8187. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8188. (defun org-remove-angle-brackets (s)
  8189. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8190. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8191. s)
  8192. (defun org-add-angle-brackets (s)
  8193. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8194. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8195. s)
  8196. (defun org-remove-double-quotes (s)
  8197. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8198. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8199. s)
  8200. ;;; Following specific links
  8201. (defun org-follow-timestamp-link ()
  8202. (cond
  8203. ((org-at-date-range-p t)
  8204. (let ((org-agenda-start-on-weekday)
  8205. (t1 (match-string 1))
  8206. (t2 (match-string 2)))
  8207. (setq t1 (time-to-days (org-time-string-to-time t1))
  8208. t2 (time-to-days (org-time-string-to-time t2)))
  8209. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8210. ((org-at-timestamp-p t)
  8211. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8212. (substring (match-string 1) 0 10)))
  8213. 1))
  8214. (t (error "This should not happen"))))
  8215. ;;; Following file links
  8216. (defvar org-wait nil)
  8217. (defun org-open-file (path &optional in-emacs line search)
  8218. "Open the file at PATH.
  8219. First, this expands any special file name abbreviations. Then the
  8220. configuration variable `org-file-apps' is checked if it contains an
  8221. entry for this file type, and if yes, the corresponding command is launched.
  8222. If no application is found, Emacs simply visits the file.
  8223. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8224. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8225. and to use an external application to visit the file.
  8226. Optional LINE specifies a line to go to, optional SEARCH a string
  8227. to search for. If LINE or SEARCH is given, the file will be
  8228. opened in Emacs, unless an entry from org-file-apps that makes
  8229. use of groups in a regexp matches.
  8230. If the file does not exist, an error is thrown."
  8231. (let* ((file (if (equal path "")
  8232. buffer-file-name
  8233. (substitute-in-file-name (expand-file-name path))))
  8234. (file-apps (append org-file-apps (org-default-apps)))
  8235. (apps (org-remove-if
  8236. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8237. (apps-dlink (org-remove-if-not
  8238. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8239. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8240. (dirp (if remp nil (file-directory-p file)))
  8241. (file (if (and dirp org-open-directory-means-index-dot-org)
  8242. (concat (file-name-as-directory file) "index.org")
  8243. file))
  8244. (a-m-a-p (assq 'auto-mode apps))
  8245. (dfile (downcase file))
  8246. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8247. (link (cond ((and (eq line nil)
  8248. (eq search nil))
  8249. file)
  8250. (line
  8251. (concat file "::" (number-to-string line)))
  8252. (search
  8253. (concat file "::" search))))
  8254. (dlink (downcase link))
  8255. (old-buffer (current-buffer))
  8256. (old-pos (point))
  8257. (old-mode major-mode)
  8258. ext cmd link-match-data)
  8259. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8260. (setq ext (match-string 1 dfile))
  8261. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8262. (setq ext (match-string 1 dfile))))
  8263. (cond
  8264. ((member in-emacs '((16) system))
  8265. (setq cmd (cdr (assoc 'system apps))))
  8266. (in-emacs (setq cmd 'emacs))
  8267. (t
  8268. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8269. (and dirp (cdr (assoc 'directory apps)))
  8270. ; first, try matching against apps-dlink
  8271. ; if we get a match here, store the match data for later
  8272. (let ((match (assoc-default dlink apps-dlink
  8273. 'string-match)))
  8274. (if match
  8275. (progn (setq link-match-data (match-data))
  8276. match)
  8277. (progn (setq in-emacs (or in-emacs line search))
  8278. nil))) ; if we have no match in apps-dlink,
  8279. ; always open the file in emacs if line or search
  8280. ; is given (for backwards compatibility)
  8281. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8282. 'string-match)
  8283. (cdr (assoc ext apps))
  8284. (cdr (assoc t apps))))))
  8285. (when (eq cmd 'system)
  8286. (setq cmd (cdr (assoc 'system apps))))
  8287. (when (eq cmd 'default)
  8288. (setq cmd (cdr (assoc t apps))))
  8289. (when (eq cmd 'mailcap)
  8290. (require 'mailcap)
  8291. (mailcap-parse-mailcaps)
  8292. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8293. (command (mailcap-mime-info mime-type)))
  8294. (if (stringp command)
  8295. (setq cmd command)
  8296. (setq cmd 'emacs))))
  8297. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8298. (not (file-exists-p file))
  8299. (not org-open-non-existing-files))
  8300. (error "No such file: %s" file))
  8301. (cond
  8302. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8303. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8304. (while (string-match "['\"]%s['\"]" cmd)
  8305. (setq cmd (replace-match "%s" t t cmd)))
  8306. (while (string-match "%s" cmd)
  8307. (setq cmd (replace-match
  8308. (save-match-data
  8309. (shell-quote-argument
  8310. (convert-standard-filename file)))
  8311. t t cmd)))
  8312. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8313. (save-match-data
  8314. (let ((match-index 1)
  8315. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8316. (set-match-data link-match-data)
  8317. (while (<= match-index number-of-groups)
  8318. (let ((regex (concat "%" (number-to-string match-index)))
  8319. (replace-with (match-string match-index dlink)))
  8320. (while (string-match regex cmd)
  8321. (setq cmd (replace-match replace-with t t cmd))))
  8322. (setq match-index (+ match-index 1)))))
  8323. (save-window-excursion
  8324. (start-process-shell-command cmd nil cmd)
  8325. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8326. ))
  8327. ((or (stringp cmd)
  8328. (eq cmd 'emacs))
  8329. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8330. (widen)
  8331. (if line (org-goto-line line)
  8332. (if search (org-link-search search))))
  8333. ((consp cmd)
  8334. (let ((file (convert-standard-filename file)))
  8335. (save-match-data
  8336. (set-match-data link-match-data)
  8337. (eval cmd))))
  8338. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8339. (and (org-mode-p) (eq old-mode 'org-mode)
  8340. (or (not (equal old-buffer (current-buffer)))
  8341. (not (equal old-pos (point))))
  8342. (org-mark-ring-push old-pos old-buffer))))
  8343. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8344. "This function returns non-nil if `entry' uses a regular
  8345. expression which should be matched against the whole link by
  8346. org-open-file.
  8347. It assumes that is the case when the entry uses a regular
  8348. expression which has at least one grouping construct and the
  8349. action is either a lisp form or a command string containing
  8350. '%1', i.e. using at least one subexpression match as a
  8351. parameter."
  8352. (let ((selector (car entry))
  8353. (action (cdr entry)))
  8354. (if (stringp selector)
  8355. (and (> (regexp-opt-depth selector) 0)
  8356. (or (and (stringp action)
  8357. (string-match "%[0-9]" action))
  8358. (consp action)))
  8359. nil)))
  8360. (defun org-default-apps ()
  8361. "Return the default applications for this operating system."
  8362. (cond
  8363. ((eq system-type 'darwin)
  8364. org-file-apps-defaults-macosx)
  8365. ((eq system-type 'windows-nt)
  8366. org-file-apps-defaults-windowsnt)
  8367. (t org-file-apps-defaults-gnu)))
  8368. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8369. "Convert extensions to regular expressions in the cars of LIST.
  8370. Also, weed out any non-string entries, because the return value is used
  8371. only for regexp matching.
  8372. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8373. point to the symbol `emacs', indicating that the file should
  8374. be opened in Emacs."
  8375. (append
  8376. (delq nil
  8377. (mapcar (lambda (x)
  8378. (if (not (stringp (car x)))
  8379. nil
  8380. (if (string-match "\\W" (car x))
  8381. x
  8382. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8383. list))
  8384. (if add-auto-mode
  8385. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8386. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8387. (defun org-file-remote-p (file)
  8388. "Test whether FILE specifies a location on a remote system.
  8389. Return non-nil if the location is indeed remote.
  8390. For example, the filename \"/user@host:/foo\" specifies a location
  8391. on the system \"/user@host:\"."
  8392. (cond ((fboundp 'file-remote-p)
  8393. (file-remote-p file))
  8394. ((fboundp 'tramp-handle-file-remote-p)
  8395. (tramp-handle-file-remote-p file))
  8396. ((and (boundp 'ange-ftp-name-format)
  8397. (string-match (car ange-ftp-name-format) file))
  8398. t)
  8399. (t nil)))
  8400. ;;;; Refiling
  8401. (defun org-get-org-file ()
  8402. "Read a filename, with default directory `org-directory'."
  8403. (let ((default (or org-default-notes-file remember-data-file)))
  8404. (read-file-name (format "File name [%s]: " default)
  8405. (file-name-as-directory org-directory)
  8406. default)))
  8407. (defun org-notes-order-reversed-p ()
  8408. "Check if the current file should receive notes in reversed order."
  8409. (cond
  8410. ((not org-reverse-note-order) nil)
  8411. ((eq t org-reverse-note-order) t)
  8412. ((not (listp org-reverse-note-order)) nil)
  8413. (t (catch 'exit
  8414. (let ((all org-reverse-note-order)
  8415. entry)
  8416. (while (setq entry (pop all))
  8417. (if (string-match (car entry) buffer-file-name)
  8418. (throw 'exit (cdr entry))))
  8419. nil)))))
  8420. (defvar org-refile-target-table nil
  8421. "The list of refile targets, created by `org-refile'.")
  8422. (defvar org-agenda-new-buffers nil
  8423. "Buffers created to visit agenda files.")
  8424. (defun org-get-refile-targets (&optional default-buffer)
  8425. "Produce a table with refile targets."
  8426. (let ((case-fold-search nil)
  8427. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8428. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8429. targets txt re files f desc descre fast-path-p level pos0)
  8430. (message "Getting targets...")
  8431. (with-current-buffer (or default-buffer (current-buffer))
  8432. (while (setq entry (pop entries))
  8433. (setq files (car entry) desc (cdr entry))
  8434. (setq fast-path-p nil)
  8435. (cond
  8436. ((null files) (setq files (list (current-buffer))))
  8437. ((eq files 'org-agenda-files)
  8438. (setq files (org-agenda-files 'unrestricted)))
  8439. ((and (symbolp files) (fboundp files))
  8440. (setq files (funcall files)))
  8441. ((and (symbolp files) (boundp files))
  8442. (setq files (symbol-value files))))
  8443. (if (stringp files) (setq files (list files)))
  8444. (cond
  8445. ((eq (car desc) :tag)
  8446. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8447. ((eq (car desc) :todo)
  8448. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8449. ((eq (car desc) :regexp)
  8450. (setq descre (cdr desc)))
  8451. ((eq (car desc) :level)
  8452. (setq descre (concat "^\\*\\{" (number-to-string
  8453. (if org-odd-levels-only
  8454. (1- (* 2 (cdr desc)))
  8455. (cdr desc)))
  8456. "\\}[ \t]")))
  8457. ((eq (car desc) :maxlevel)
  8458. (setq fast-path-p t)
  8459. (setq descre (concat "^\\*\\{1," (number-to-string
  8460. (if org-odd-levels-only
  8461. (1- (* 2 (cdr desc)))
  8462. (cdr desc)))
  8463. "\\}[ \t]")))
  8464. (t (error "Bad refiling target description %s" desc)))
  8465. (while (setq f (pop files))
  8466. (with-current-buffer
  8467. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8468. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8469. (setq f (and f (expand-file-name f)))
  8470. (if (eq org-refile-use-outline-path 'file)
  8471. (push (list (file-name-nondirectory f) f nil nil) targets))
  8472. (save-excursion
  8473. (save-restriction
  8474. (widen)
  8475. (goto-char (point-min))
  8476. (while (re-search-forward descre nil t)
  8477. (goto-char (setq pos0 (point-at-bol)))
  8478. (catch 'next
  8479. (when org-refile-target-verify-function
  8480. (save-match-data
  8481. (or (funcall org-refile-target-verify-function)
  8482. (throw 'next t))))
  8483. (when (looking-at org-complex-heading-regexp)
  8484. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8485. txt (org-link-display-format (match-string 4))
  8486. re (concat "^" (regexp-quote
  8487. (buffer-substring (match-beginning 1)
  8488. (match-end 4)))))
  8489. (if (match-end 5) (setq re (concat re "[ \t]+"
  8490. (regexp-quote
  8491. (match-string 5)))))
  8492. (setq re (concat re "[ \t]*$"))
  8493. (when org-refile-use-outline-path
  8494. (setq txt (mapconcat 'org-protect-slash
  8495. (append
  8496. (if (eq org-refile-use-outline-path 'file)
  8497. (list (file-name-nondirectory
  8498. (buffer-file-name (buffer-base-buffer))))
  8499. (if (eq org-refile-use-outline-path 'full-file-path)
  8500. (list (buffer-file-name (buffer-base-buffer)))))
  8501. (org-get-outline-path fast-path-p level txt)
  8502. (list txt))
  8503. "/")))
  8504. (push (list txt f re (point)) targets)))
  8505. (when (= (point) pos0)
  8506. ;; verification function has not moved point
  8507. (goto-char (point-at-eol))))))))))
  8508. (message "Getting targets...done")
  8509. (nreverse targets)))
  8510. (defun org-protect-slash (s)
  8511. (while (string-match "/" s)
  8512. (setq s (replace-match "\\" t t s)))
  8513. s)
  8514. (defvar org-olpa (make-vector 20 nil))
  8515. (defun org-get-outline-path (&optional fastp level heading)
  8516. "Return the outline path to the current entry, as a list.
  8517. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8518. routine which makes outline path derivations for an entire file,
  8519. avoiding backtracing."
  8520. (if fastp
  8521. (progn
  8522. (if (> level 19)
  8523. (error "Outline path failure, more than 19 levels."))
  8524. (loop for i from level upto 19 do
  8525. (aset org-olpa i nil))
  8526. (prog1
  8527. (delq nil (append org-olpa nil))
  8528. (aset org-olpa level heading)))
  8529. (let (rtn case-fold-search)
  8530. (save-excursion
  8531. (save-restriction
  8532. (widen)
  8533. (while (org-up-heading-safe)
  8534. (when (looking-at org-complex-heading-regexp)
  8535. (push (org-match-string-no-properties 4) rtn)))
  8536. rtn)))))
  8537. (defun org-format-outline-path (path &optional width prefix)
  8538. "Format the outlie path PATH for display.
  8539. Width is the maximum number of characters that is available.
  8540. Prefix is a prefix to be included in the returned string,
  8541. such as the file name."
  8542. (setq width (or width 79))
  8543. (if prefix (setq width (- width (length prefix))))
  8544. (if (not path)
  8545. (or prefix "")
  8546. (let* ((nsteps (length path))
  8547. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8548. (maxwidth (if (<= total-width width)
  8549. 10000 ;; everything fits
  8550. ;; we need to shorten the level headings
  8551. (/ (- width nsteps) nsteps)))
  8552. (org-odd-levels-only nil)
  8553. (n 0)
  8554. (total (1+ (length prefix))))
  8555. (setq maxwidth (max maxwidth 10))
  8556. (concat prefix
  8557. (mapconcat
  8558. (lambda (h)
  8559. (setq n (1+ n))
  8560. (if (and (= n nsteps) (< maxwidth 10000))
  8561. (setq maxwidth (- total-width total)))
  8562. (if (< (length h) maxwidth)
  8563. (progn (setq total (+ total (length h) 1)) h)
  8564. (setq h (substring h 0 (- maxwidth 2))
  8565. total (+ total maxwidth 1))
  8566. (if (string-match "[ \t]+\\'" h)
  8567. (setq h (substring h 0 (match-beginning 0))))
  8568. (setq h (concat h "..")))
  8569. (org-add-props h nil 'face
  8570. (nth (% (1- n) org-n-level-faces)
  8571. org-level-faces))
  8572. h)
  8573. path "/")))))
  8574. (defun org-display-outline-path (&optional file current)
  8575. "Display the current outline path in the echo area."
  8576. (interactive "P")
  8577. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8578. (case-fold-search nil)
  8579. (path (and (org-mode-p) (org-get-outline-path))))
  8580. (if current (setq path (append path
  8581. (save-excursion
  8582. (org-back-to-heading t)
  8583. (if (looking-at org-complex-heading-regexp)
  8584. (list (match-string 4)))))))
  8585. (message "%s"
  8586. (org-format-outline-path
  8587. path
  8588. (1- (frame-width))
  8589. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8590. (defvar org-refile-history nil
  8591. "History for refiling operations.")
  8592. (defvar org-after-refile-insert-hook nil
  8593. "Hook run after `org-refile' has inserted its stuff at the new location.
  8594. Note that this is still *before* the stuff will be removed from
  8595. the *old* location.")
  8596. (defun org-refile (&optional goto default-buffer rfloc)
  8597. "Move the entry at point to another heading.
  8598. The list of target headings is compiled using the information in
  8599. `org-refile-targets', which see. This list is created before each use
  8600. and will therefore always be up-to-date.
  8601. At the target location, the entry is filed as a subitem of the target heading.
  8602. Depending on `org-reverse-note-order', the new subitem will either be the
  8603. first or the last subitem.
  8604. If there is an active region, all entries in that region will be moved.
  8605. However, the region must fulfil the requirement that the first heading
  8606. is the first one sets the top-level of the moved text - at most siblings
  8607. below it are allowed.
  8608. With prefix arg GOTO, the command will only visit the target location,
  8609. not actually move anything.
  8610. With a double prefix `C-u C-u', go to the location where the last refiling
  8611. operation has put the subtree.
  8612. With a prefix argument of `2', refile to the running clock.
  8613. RFLOC can be a refile location obtained in a different way.
  8614. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8615. (interactive "P")
  8616. (let* ((cbuf (current-buffer))
  8617. (regionp (org-region-active-p))
  8618. (region-start (and regionp (region-beginning)))
  8619. (region-end (and regionp (region-end)))
  8620. (region-length (and regionp (- region-end region-start)))
  8621. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8622. pos it nbuf file re level reversed)
  8623. (setq last-command nil)
  8624. (when regionp
  8625. (goto-char region-start)
  8626. (or (bolp) (goto-char (point-at-bol)))
  8627. (setq region-start (point))
  8628. (unless (org-kill-is-subtree-p
  8629. (buffer-substring region-start region-end))
  8630. (error "The region is not a (sequence of) subtree(s)")))
  8631. (if (equal goto '(16))
  8632. (org-refile-goto-last-stored)
  8633. (when (or
  8634. (and (equal goto 2)
  8635. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8636. (prog1
  8637. (setq it (list (or org-clock-heading "running clock")
  8638. (buffer-file-name
  8639. (marker-buffer org-clock-hd-marker))
  8640. ""
  8641. (marker-position org-clock-hd-marker)))
  8642. (setq goto nil)))
  8643. (setq it (or rfloc
  8644. (save-excursion
  8645. (org-refile-get-location
  8646. (if goto "Goto: " "Refile to: ") default-buffer
  8647. org-refile-allow-creating-parent-nodes)))))
  8648. (setq file (nth 1 it)
  8649. re (nth 2 it)
  8650. pos (nth 3 it))
  8651. (if (and (not goto)
  8652. pos
  8653. (equal (buffer-file-name) file)
  8654. (if regionp
  8655. (and (>= pos region-start)
  8656. (<= pos region-end))
  8657. (and (>= pos (point))
  8658. (< pos (save-excursion
  8659. (org-end-of-subtree t t))))))
  8660. (error "Cannot refile to position inside the tree or region"))
  8661. (setq nbuf (or (find-buffer-visiting file)
  8662. (find-file-noselect file)))
  8663. (if goto
  8664. (progn
  8665. (switch-to-buffer nbuf)
  8666. (goto-char pos)
  8667. (org-show-context 'org-goto))
  8668. (if regionp
  8669. (progn
  8670. (org-kill-new (buffer-substring region-start region-end))
  8671. (org-save-markers-in-region region-start region-end))
  8672. (org-copy-subtree 1 nil t))
  8673. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8674. (find-file-noselect file)))
  8675. (setq reversed (org-notes-order-reversed-p))
  8676. (save-excursion
  8677. (save-restriction
  8678. (widen)
  8679. (if pos
  8680. (progn
  8681. (goto-char pos)
  8682. (looking-at outline-regexp)
  8683. (setq level (org-get-valid-level (funcall outline-level) 1))
  8684. (goto-char
  8685. (if reversed
  8686. (or (outline-next-heading) (point-max))
  8687. (or (save-excursion (org-get-next-sibling))
  8688. (org-end-of-subtree t t)
  8689. (point-max)))))
  8690. (setq level 1)
  8691. (if (not reversed)
  8692. (goto-char (point-max))
  8693. (goto-char (point-min))
  8694. (or (outline-next-heading) (goto-char (point-max)))))
  8695. (if (not (bolp)) (newline))
  8696. (org-paste-subtree level)
  8697. (when org-log-refile
  8698. (org-add-log-setup 'refile nil nil 'findpos
  8699. org-log-refile)
  8700. (unless (eq org-log-refile 'note)
  8701. (save-excursion (org-add-log-note))))
  8702. (and org-auto-align-tags (org-set-tags nil t))
  8703. (bookmark-set "org-refile-last-stored")
  8704. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8705. (run-hooks 'org-after-refile-insert-hook))))
  8706. (if regionp
  8707. (delete-region (point) (+ (point) region-length))
  8708. (org-cut-subtree))
  8709. (when (featurep 'org-inlinetask)
  8710. (org-inlinetask-remove-END-maybe))
  8711. (setq org-markers-to-move nil)
  8712. (message "Refiled to \"%s\"" (car it))))))
  8713. (org-reveal))
  8714. (defun org-refile-goto-last-stored ()
  8715. "Go to the location where the last refile was stored."
  8716. (interactive)
  8717. (bookmark-jump "org-refile-last-stored")
  8718. (message "This is the location of the last refile"))
  8719. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8720. "Prompt the user for a refile location, using PROMPT."
  8721. (let ((org-refile-targets org-refile-targets)
  8722. (org-refile-use-outline-path org-refile-use-outline-path))
  8723. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8724. (unless org-refile-target-table
  8725. (error "No refile targets"))
  8726. (let* ((cbuf (current-buffer))
  8727. (partial-completion-mode nil)
  8728. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8729. (cfunc (if (and org-refile-use-outline-path
  8730. org-outline-path-complete-in-steps)
  8731. 'org-olpath-completing-read
  8732. 'org-icompleting-read))
  8733. (extra (if org-refile-use-outline-path "/" ""))
  8734. (filename (and cfn (expand-file-name cfn)))
  8735. (tbl (mapcar
  8736. (lambda (x)
  8737. (if (and (not (member org-refile-use-outline-path
  8738. '(file full-file-path)))
  8739. (not (equal filename (nth 1 x))))
  8740. (cons (concat (car x) extra " ("
  8741. (file-name-nondirectory (nth 1 x)) ")")
  8742. (cdr x))
  8743. (cons (concat (car x) extra) (cdr x))))
  8744. org-refile-target-table))
  8745. (completion-ignore-case t)
  8746. pa answ parent-target child parent old-hist)
  8747. (setq old-hist org-refile-history)
  8748. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8749. nil 'org-refile-history))
  8750. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8751. (if pa
  8752. (progn
  8753. (when (or (not org-refile-history)
  8754. (not (eq old-hist org-refile-history))
  8755. (not (equal (car pa) (car org-refile-history))))
  8756. (setq org-refile-history
  8757. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8758. org-refile-history
  8759. (cdr org-refile-history))))
  8760. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8761. (pop org-refile-history)))
  8762. pa)
  8763. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8764. (progn
  8765. (setq parent (match-string 1 answ)
  8766. child (match-string 2 answ))
  8767. (setq parent-target (or (assoc parent tbl)
  8768. (assoc (concat parent "/") tbl)))
  8769. (when (and parent-target
  8770. (or (eq new-nodes t)
  8771. (and (eq new-nodes 'confirm)
  8772. (y-or-n-p (format "Create new node \"%s\"? "
  8773. child)))))
  8774. (org-refile-new-child parent-target child)))
  8775. (error "Invalid target location")))))
  8776. (defun org-refile-new-child (parent-target child)
  8777. "Use refile target PARENT-TARGET to add new CHILD below it."
  8778. (unless parent-target
  8779. (error "Cannot find parent for new node"))
  8780. (let ((file (nth 1 parent-target))
  8781. (pos (nth 3 parent-target))
  8782. level)
  8783. (with-current-buffer (or (find-buffer-visiting file)
  8784. (find-file-noselect file))
  8785. (save-excursion
  8786. (save-restriction
  8787. (widen)
  8788. (if pos
  8789. (goto-char pos)
  8790. (goto-char (point-max))
  8791. (if (not (bolp)) (newline)))
  8792. (when (looking-at outline-regexp)
  8793. (setq level (funcall outline-level))
  8794. (org-end-of-subtree t t))
  8795. (org-back-over-empty-lines)
  8796. (insert "\n" (make-string
  8797. (if pos (org-get-valid-level level 1) 1) ?*)
  8798. " " child "\n")
  8799. (beginning-of-line 0)
  8800. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8801. (defun org-olpath-completing-read (prompt collection &rest args)
  8802. "Read an outline path like a file name."
  8803. (let ((thetable collection)
  8804. (org-completion-use-ido nil) ; does not work with ido.
  8805. (org-completion-use-iswitchb nil)) ; or iswitchb
  8806. (apply
  8807. 'org-icompleting-read prompt
  8808. (lambda (string predicate &optional flag)
  8809. (let (rtn r f (l (length string)))
  8810. (cond
  8811. ((eq flag nil)
  8812. ;; try completion
  8813. (try-completion string thetable))
  8814. ((eq flag t)
  8815. ;; all-completions
  8816. (setq rtn (all-completions string thetable predicate))
  8817. (mapcar
  8818. (lambda (x)
  8819. (setq r (substring x l))
  8820. (if (string-match " ([^)]*)$" x)
  8821. (setq f (match-string 0 x))
  8822. (setq f ""))
  8823. (if (string-match "/" r)
  8824. (concat string (substring r 0 (match-end 0)) f)
  8825. x))
  8826. rtn))
  8827. ((eq flag 'lambda)
  8828. ;; exact match?
  8829. (assoc string thetable)))
  8830. ))
  8831. args)))
  8832. ;;;; Dynamic blocks
  8833. (defun org-find-dblock (name)
  8834. "Find the first dynamic block with name NAME in the buffer.
  8835. If not found, stay at current position and return nil."
  8836. (let (pos)
  8837. (save-excursion
  8838. (goto-char (point-min))
  8839. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8840. nil t)
  8841. (match-beginning 0))))
  8842. (if pos (goto-char pos))
  8843. pos))
  8844. (defconst org-dblock-start-re
  8845. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8846. "Matches the start line of a dynamic block, with parameters.")
  8847. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8848. "Matches the end of a dynamic block.")
  8849. (defun org-create-dblock (plist)
  8850. "Create a dynamic block section, with parameters taken from PLIST.
  8851. PLIST must contain a :name entry which is used as name of the block."
  8852. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8853. (end-of-line 1)
  8854. (newline))
  8855. (let ((col (current-column))
  8856. (name (plist-get plist :name)))
  8857. (insert "#+BEGIN: " name)
  8858. (while plist
  8859. (if (eq (car plist) :name)
  8860. (setq plist (cddr plist))
  8861. (insert " " (prin1-to-string (pop plist)))))
  8862. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8863. (beginning-of-line -2)))
  8864. (defun org-prepare-dblock ()
  8865. "Prepare dynamic block for refresh.
  8866. This empties the block, puts the cursor at the insert position and returns
  8867. the property list including an extra property :name with the block name."
  8868. (unless (looking-at org-dblock-start-re)
  8869. (error "Not at a dynamic block"))
  8870. (let* ((begdel (1+ (match-end 0)))
  8871. (name (org-no-properties (match-string 1)))
  8872. (params (append (list :name name)
  8873. (read (concat "(" (match-string 3) ")")))))
  8874. (save-excursion
  8875. (beginning-of-line 1)
  8876. (skip-chars-forward " \t")
  8877. (setq params (plist-put params :indentation-column (current-column))))
  8878. (unless (re-search-forward org-dblock-end-re nil t)
  8879. (error "Dynamic block not terminated"))
  8880. (setq params
  8881. (append params
  8882. (list :content (buffer-substring
  8883. begdel (match-beginning 0)))))
  8884. (delete-region begdel (match-beginning 0))
  8885. (goto-char begdel)
  8886. (open-line 1)
  8887. params))
  8888. (defun org-map-dblocks (&optional command)
  8889. "Apply COMMAND to all dynamic blocks in the current buffer.
  8890. If COMMAND is not given, use `org-update-dblock'."
  8891. (let ((cmd (or command 'org-update-dblock)))
  8892. (save-excursion
  8893. (goto-char (point-min))
  8894. (while (re-search-forward org-dblock-start-re nil t)
  8895. (goto-char (match-beginning 0))
  8896. (save-excursion
  8897. (condition-case nil
  8898. (funcall cmd)
  8899. (error (message "Error during update of dynamic block"))))
  8900. (unless (re-search-forward org-dblock-end-re nil t)
  8901. (error "Dynamic block not terminated"))))))
  8902. (defun org-dblock-update (&optional arg)
  8903. "User command for updating dynamic blocks.
  8904. Update the dynamic block at point. With prefix ARG, update all dynamic
  8905. blocks in the buffer."
  8906. (interactive "P")
  8907. (if arg
  8908. (org-update-all-dblocks)
  8909. (or (looking-at org-dblock-start-re)
  8910. (org-beginning-of-dblock))
  8911. (org-update-dblock)))
  8912. (defun org-update-dblock ()
  8913. "Update the dynamic block at point
  8914. This means to empty the block, parse for parameters and then call
  8915. the correct writing function."
  8916. (save-window-excursion
  8917. (let* ((pos (point))
  8918. (line (org-current-line))
  8919. (params (org-prepare-dblock))
  8920. (name (plist-get params :name))
  8921. (indent (plist-get params :indentation-column))
  8922. (cmd (intern (concat "org-dblock-write:" name))))
  8923. (message "Updating dynamic block `%s' at line %d..." name line)
  8924. (funcall cmd params)
  8925. (message "Updating dynamic block `%s' at line %d...done" name line)
  8926. (goto-char pos)
  8927. (when (and indent (> indent 0))
  8928. (setq indent (make-string indent ?\ ))
  8929. (save-excursion
  8930. (org-beginning-of-dblock)
  8931. (forward-line 1)
  8932. (while (not (looking-at org-dblock-end-re))
  8933. (insert indent)
  8934. (beginning-of-line 2))
  8935. (when (looking-at org-dblock-end-re)
  8936. (and (looking-at "[ \t]+")
  8937. (replace-match ""))
  8938. (insert indent)))))))
  8939. (defun org-beginning-of-dblock ()
  8940. "Find the beginning of the dynamic block at point.
  8941. Error if there is no such block at point."
  8942. (let ((pos (point))
  8943. beg)
  8944. (end-of-line 1)
  8945. (if (and (re-search-backward org-dblock-start-re nil t)
  8946. (setq beg (match-beginning 0))
  8947. (re-search-forward org-dblock-end-re nil t)
  8948. (> (match-end 0) pos))
  8949. (goto-char beg)
  8950. (goto-char pos)
  8951. (error "Not in a dynamic block"))))
  8952. (defun org-update-all-dblocks ()
  8953. "Update all dynamic blocks in the buffer.
  8954. This function can be used in a hook."
  8955. (when (org-mode-p)
  8956. (org-map-dblocks 'org-update-dblock)))
  8957. ;;;; Completion
  8958. (defconst org-additional-option-like-keywords
  8959. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8960. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8961. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8962. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8963. "BEGIN:" "END:"
  8964. "ORGTBL" "TBLFM:" "TBLNAME:"
  8965. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8966. "BEGIN_QUOTE" "END_QUOTE"
  8967. "BEGIN_VERSE" "END_VERSE"
  8968. "BEGIN_CENTER" "END_CENTER"
  8969. "BEGIN_SRC" "END_SRC"
  8970. "CATEGORY" "COLUMNS"
  8971. "CAPTION" "LABEL"
  8972. "SETUPFILE"
  8973. "BIND"
  8974. "MACRO"))
  8975. (defcustom org-structure-template-alist
  8976. '(
  8977. ("s" "#+begin_src ?\n\n#+end_src"
  8978. "<src lang=\"?\">\n\n</src>")
  8979. ("e" "#+begin_example\n?\n#+end_example"
  8980. "<example>\n?\n</example>")
  8981. ("q" "#+begin_quote\n?\n#+end_quote"
  8982. "<quote>\n?\n</quote>")
  8983. ("v" "#+begin_verse\n?\n#+end_verse"
  8984. "<verse>\n?\n/verse>")
  8985. ("c" "#+begin_center\n?\n#+end_center"
  8986. "<center>\n?\n/center>")
  8987. ("l" "#+begin_latex\n?\n#+end_latex"
  8988. "<literal style=\"latex\">\n?\n</literal>")
  8989. ("L" "#+latex: "
  8990. "<literal style=\"latex\">?</literal>")
  8991. ("h" "#+begin_html\n?\n#+end_html"
  8992. "<literal style=\"html\">\n?\n</literal>")
  8993. ("H" "#+html: "
  8994. "<literal style=\"html\">?</literal>")
  8995. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8996. ("A" "#+ascii: ")
  8997. ("i" "#+include %file ?"
  8998. "<include file=%file markup=\"?\">")
  8999. )
  9000. "Structure completion elements.
  9001. This is a list of abbreviation keys and values. The value gets inserted
  9002. if you type `<' followed by the key and then press the completion key,
  9003. usually `M-TAB'. %file will be replaced by a file name after prompting
  9004. for the file using completion.
  9005. There are two templates for each key, the first uses the original Org syntax,
  9006. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9007. the default when the /org-mtags.el/ module has been loaded. See also the
  9008. variable `org-mtags-prefer-muse-templates'.
  9009. This is an experimental feature, it is undecided if it is going to stay in."
  9010. :group 'org-completion
  9011. :type '(repeat
  9012. (string :tag "Key")
  9013. (string :tag "Template")
  9014. (string :tag "Muse Template")))
  9015. (defun org-try-structure-completion ()
  9016. "Try to complete a structure template before point.
  9017. This looks for strings like \"<e\" on an otherwise empty line and
  9018. expands them."
  9019. (let ((l (buffer-substring (point-at-bol) (point)))
  9020. a)
  9021. (when (and (looking-at "[ \t]*$")
  9022. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9023. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9024. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9025. (match-beginning 1)) a)
  9026. t)))
  9027. (defun org-complete-expand-structure-template (start cell)
  9028. "Expand a structure template."
  9029. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9030. (rpl (nth (if musep 2 1) cell))
  9031. (ind ""))
  9032. (delete-region start (point))
  9033. (when (string-match "\\`#\\+" rpl)
  9034. (cond
  9035. ((bolp))
  9036. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9037. (setq ind (buffer-substring (point-at-bol) (point))))
  9038. (t (newline))))
  9039. (setq start (point))
  9040. (if (string-match "%file" rpl)
  9041. (setq rpl (replace-match
  9042. (concat
  9043. "\""
  9044. (save-match-data
  9045. (abbreviate-file-name (read-file-name "Include file: ")))
  9046. "\"")
  9047. t t rpl)))
  9048. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9049. (concat "\n" ind)))
  9050. (insert rpl)
  9051. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9052. (defun org-complete (&optional arg)
  9053. "Perform completion on word at point.
  9054. At the beginning of a headline, this completes TODO keywords as given in
  9055. `org-todo-keywords'.
  9056. If the current word is preceded by a backslash, completes the TeX symbols
  9057. that are supported for HTML support.
  9058. If the current word is preceded by \"#+\", completes special words for
  9059. setting file options.
  9060. In the line after \"#+STARTUP:, complete valid keywords.\"
  9061. At all other locations, this simply calls the value of
  9062. `org-completion-fallback-command'."
  9063. (interactive "P")
  9064. (org-without-partial-completion
  9065. (catch 'exit
  9066. (let* ((a nil)
  9067. (end (point))
  9068. (beg1 (save-excursion
  9069. (skip-chars-backward (org-re "[:alnum:]_@"))
  9070. (point)))
  9071. (beg (save-excursion
  9072. (skip-chars-backward "a-zA-Z0-9_:$")
  9073. (point)))
  9074. (confirm (lambda (x) (stringp (car x))))
  9075. (searchhead (equal (char-before beg) ?*))
  9076. (struct
  9077. (when (and (member (char-before beg1) '(?. ?<))
  9078. (setq a (assoc (buffer-substring beg1 (point))
  9079. org-structure-template-alist)))
  9080. (org-complete-expand-structure-template (1- beg1) a)
  9081. (throw 'exit t)))
  9082. (tag (and (equal (char-before beg1) ?:)
  9083. (equal (char-after (point-at-bol)) ?*)))
  9084. (prop (and (equal (char-before beg1) ?:)
  9085. (not (equal (char-after (point-at-bol)) ?*))))
  9086. (texp (equal (char-before beg) ?\\))
  9087. (link (equal (char-before beg) ?\[))
  9088. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9089. beg)
  9090. "#+"))
  9091. (startup (string-match "^#\\+STARTUP:.*"
  9092. (buffer-substring (point-at-bol) (point))))
  9093. (completion-ignore-case opt)
  9094. (type nil)
  9095. (tbl nil)
  9096. (table (cond
  9097. (opt
  9098. (setq type :opt)
  9099. (require 'org-exp)
  9100. (append
  9101. (delq nil
  9102. (mapcar
  9103. (lambda (x)
  9104. (if (string-match
  9105. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9106. (cons (match-string 2 x)
  9107. (match-string 1 x))))
  9108. (org-split-string (org-get-current-options) "\n")))
  9109. (mapcar 'list org-additional-option-like-keywords)))
  9110. (startup
  9111. (setq type :startup)
  9112. org-startup-options)
  9113. (link (append org-link-abbrev-alist-local
  9114. org-link-abbrev-alist))
  9115. (texp
  9116. (setq type :tex)
  9117. (append org-entities-user org-entities))
  9118. ((string-match "\\`\\*+[ \t]+\\'"
  9119. (buffer-substring (point-at-bol) beg))
  9120. (setq type :todo)
  9121. (mapcar 'list org-todo-keywords-1))
  9122. (searchhead
  9123. (setq type :searchhead)
  9124. (save-excursion
  9125. (goto-char (point-min))
  9126. (while (re-search-forward org-todo-line-regexp nil t)
  9127. (push (list
  9128. (org-make-org-heading-search-string
  9129. (match-string 3) t))
  9130. tbl)))
  9131. tbl)
  9132. (tag (setq type :tag beg beg1)
  9133. (or org-tag-alist (org-get-buffer-tags)))
  9134. (prop (setq type :prop beg beg1)
  9135. (mapcar 'list (org-buffer-property-keys nil t t)))
  9136. (t (progn
  9137. (call-interactively org-completion-fallback-command)
  9138. (throw 'exit nil)))))
  9139. (pattern (buffer-substring-no-properties beg end))
  9140. (completion (try-completion pattern table confirm)))
  9141. (cond ((eq completion t)
  9142. (if (not (assoc (upcase pattern) table))
  9143. (message "Already complete")
  9144. (if (and (equal type :opt)
  9145. (not (member (car (assoc (upcase pattern) table))
  9146. org-additional-option-like-keywords)))
  9147. (insert (substring (cdr (assoc (upcase pattern) table))
  9148. (length pattern)))
  9149. (if (memq type '(:tag :prop)) (insert ":")))))
  9150. ((null completion)
  9151. (message "Can't find completion for \"%s\"" pattern)
  9152. (ding))
  9153. ((not (string= pattern completion))
  9154. (delete-region beg end)
  9155. (if (string-match " +$" completion)
  9156. (setq completion (replace-match "" t t completion)))
  9157. (insert completion)
  9158. (if (get-buffer-window "*Completions*")
  9159. (delete-window (get-buffer-window "*Completions*")))
  9160. (if (assoc completion table)
  9161. (if (eq type :todo) (insert " ")
  9162. (if (memq type '(:tag :prop)) (insert ":"))))
  9163. (if (and (equal type :opt) (assoc completion table))
  9164. (message "%s" (substitute-command-keys
  9165. "Press \\[org-complete] again to insert example settings"))))
  9166. (t
  9167. (message "Making completion list...")
  9168. (let ((list (sort (all-completions pattern table confirm)
  9169. 'string<)))
  9170. (with-output-to-temp-buffer "*Completions*"
  9171. (condition-case nil
  9172. ;; Protection needed for XEmacs and emacs 21
  9173. (display-completion-list list pattern)
  9174. (error (display-completion-list list)))))
  9175. (message "Making completion list...%s" "done")))))))
  9176. ;;;; TODO, DEADLINE, Comments
  9177. (defun org-toggle-comment ()
  9178. "Change the COMMENT state of an entry."
  9179. (interactive)
  9180. (save-excursion
  9181. (org-back-to-heading)
  9182. (let (case-fold-search)
  9183. (if (looking-at (concat outline-regexp
  9184. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9185. (replace-match "" t t nil 1)
  9186. (if (looking-at outline-regexp)
  9187. (progn
  9188. (goto-char (match-end 0))
  9189. (insert org-comment-string " ")))))))
  9190. (defvar org-last-todo-state-is-todo nil
  9191. "This is non-nil when the last TODO state change led to a TODO state.
  9192. If the last change removed the TODO tag or switched to DONE, then
  9193. this is nil.")
  9194. (defvar org-setting-tags nil) ; dynamically skipped
  9195. (defun org-parse-local-options (string var)
  9196. "Parse STRING for startup setting relevant for variable VAR."
  9197. (let ((rtn (symbol-value var))
  9198. e opts)
  9199. (save-match-data
  9200. (if (or (not string) (not (string-match "\\S-" string)))
  9201. rtn
  9202. (setq opts (delq nil (mapcar (lambda (x)
  9203. (setq e (assoc x org-startup-options))
  9204. (if (eq (nth 1 e) var) e nil))
  9205. (org-split-string string "[ \t]+"))))
  9206. (if (not opts)
  9207. rtn
  9208. (setq rtn nil)
  9209. (while (setq e (pop opts))
  9210. (if (not (nth 3 e))
  9211. (setq rtn (nth 2 e))
  9212. (if (not (listp rtn)) (setq rtn nil))
  9213. (push (nth 2 e) rtn)))
  9214. rtn)))))
  9215. (defvar org-todo-setup-filter-hook nil
  9216. "Hook for functions that pre-filter todo specs.
  9217. Each function takes a todo spec and returns either `nil' or the spec
  9218. transformed into canonical form." )
  9219. (defvar org-todo-get-default-hook nil
  9220. "Hook for functions that get a default item for todo.
  9221. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9222. `nil' or a string to be used for the todo mark." )
  9223. (defvar org-agenda-headline-snapshot-before-repeat)
  9224. (defun org-todo (&optional arg)
  9225. "Change the TODO state of an item.
  9226. The state of an item is given by a keyword at the start of the heading,
  9227. like
  9228. *** TODO Write paper
  9229. *** DONE Call mom
  9230. The different keywords are specified in the variable `org-todo-keywords'.
  9231. By default the available states are \"TODO\" and \"DONE\".
  9232. So for this example: when the item starts with TODO, it is changed to DONE.
  9233. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9234. DONE are present, add TODO at the beginning of the heading.
  9235. With C-u prefix arg, use completion to determine the new state.
  9236. With numeric prefix arg, switch to that state.
  9237. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9238. With a triple C-u prefix, circumvent any state blocking.
  9239. For calling through lisp, arg is also interpreted in the following way:
  9240. 'none -> empty state
  9241. \"\"(empty string) -> switch to empty state
  9242. 'done -> switch to DONE
  9243. 'nextset -> switch to the next set of keywords
  9244. 'previousset -> switch to the previous set of keywords
  9245. \"WAITING\" -> switch to the specified keyword, but only if it
  9246. really is a member of `org-todo-keywords'."
  9247. (interactive "P")
  9248. (if (equal arg '(16)) (setq arg 'nextset))
  9249. (let ((org-blocker-hook org-blocker-hook)
  9250. (case-fold-search nil))
  9251. (when (equal arg '(64))
  9252. (setq arg nil org-blocker-hook nil))
  9253. (when (and org-blocker-hook
  9254. (or org-inhibit-blocking
  9255. (org-entry-get nil "NOBLOCKING")))
  9256. (setq org-blocker-hook nil))
  9257. (save-excursion
  9258. (catch 'exit
  9259. (org-back-to-heading t)
  9260. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9261. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9262. (looking-at " *"))
  9263. (let* ((match-data (match-data))
  9264. (startpos (point-at-bol))
  9265. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9266. (org-log-done org-log-done)
  9267. (org-log-repeat org-log-repeat)
  9268. (org-todo-log-states org-todo-log-states)
  9269. (this (match-string 1))
  9270. (hl-pos (match-beginning 0))
  9271. (head (org-get-todo-sequence-head this))
  9272. (ass (assoc head org-todo-kwd-alist))
  9273. (interpret (nth 1 ass))
  9274. (done-word (nth 3 ass))
  9275. (final-done-word (nth 4 ass))
  9276. (last-state (or this ""))
  9277. (completion-ignore-case t)
  9278. (member (member this org-todo-keywords-1))
  9279. (tail (cdr member))
  9280. (state (cond
  9281. ((and org-todo-key-trigger
  9282. (or (and (equal arg '(4))
  9283. (eq org-use-fast-todo-selection 'prefix))
  9284. (and (not arg) org-use-fast-todo-selection
  9285. (not (eq org-use-fast-todo-selection
  9286. 'prefix)))))
  9287. ;; Use fast selection
  9288. (org-fast-todo-selection))
  9289. ((and (equal arg '(4))
  9290. (or (not org-use-fast-todo-selection)
  9291. (not org-todo-key-trigger)))
  9292. ;; Read a state with completion
  9293. (org-icompleting-read
  9294. "State: " (mapcar (lambda(x) (list x))
  9295. org-todo-keywords-1)
  9296. nil t))
  9297. ((eq arg 'right)
  9298. (if this
  9299. (if tail (car tail) nil)
  9300. (car org-todo-keywords-1)))
  9301. ((eq arg 'left)
  9302. (if (equal member org-todo-keywords-1)
  9303. nil
  9304. (if this
  9305. (nth (- (length org-todo-keywords-1)
  9306. (length tail) 2)
  9307. org-todo-keywords-1)
  9308. (org-last org-todo-keywords-1))))
  9309. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9310. (setq arg nil))) ; hack to fall back to cycling
  9311. (arg
  9312. ;; user or caller requests a specific state
  9313. (cond
  9314. ((equal arg "") nil)
  9315. ((eq arg 'none) nil)
  9316. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9317. ((eq arg 'nextset)
  9318. (or (car (cdr (member head org-todo-heads)))
  9319. (car org-todo-heads)))
  9320. ((eq arg 'previousset)
  9321. (let ((org-todo-heads (reverse org-todo-heads)))
  9322. (or (car (cdr (member head org-todo-heads)))
  9323. (car org-todo-heads))))
  9324. ((car (member arg org-todo-keywords-1)))
  9325. ((stringp arg)
  9326. (error "State `%s' not valid in this file" arg))
  9327. ((nth (1- (prefix-numeric-value arg))
  9328. org-todo-keywords-1))))
  9329. ((null member) (or head (car org-todo-keywords-1)))
  9330. ((equal this final-done-word) nil) ;; -> make empty
  9331. ((null tail) nil) ;; -> first entry
  9332. ((memq interpret '(type priority))
  9333. (if (eq this-command last-command)
  9334. (car tail)
  9335. (if (> (length tail) 0)
  9336. (or done-word (car org-done-keywords))
  9337. nil)))
  9338. (t
  9339. (car tail))))
  9340. (state (or
  9341. (run-hook-with-args-until-success
  9342. 'org-todo-get-default-hook state last-state)
  9343. state))
  9344. (next (if state (concat " " state " ") " "))
  9345. (change-plist (list :type 'todo-state-change :from this :to state
  9346. :position startpos))
  9347. dolog now-done-p)
  9348. (when org-blocker-hook
  9349. (setq org-last-todo-state-is-todo
  9350. (not (member this org-done-keywords)))
  9351. (unless (save-excursion
  9352. (save-match-data
  9353. (run-hook-with-args-until-failure
  9354. 'org-blocker-hook change-plist)))
  9355. (if (interactive-p)
  9356. (error "TODO state change from %s to %s blocked" this state)
  9357. ;; fail silently
  9358. (message "TODO state change from %s to %s blocked" this state)
  9359. (throw 'exit nil))))
  9360. (store-match-data match-data)
  9361. (replace-match next t t)
  9362. (unless (pos-visible-in-window-p hl-pos)
  9363. (message "TODO state changed to %s" (org-trim next)))
  9364. (unless head
  9365. (setq head (org-get-todo-sequence-head state)
  9366. ass (assoc head org-todo-kwd-alist)
  9367. interpret (nth 1 ass)
  9368. done-word (nth 3 ass)
  9369. final-done-word (nth 4 ass)))
  9370. (when (memq arg '(nextset previousset))
  9371. (message "Keyword-Set %d/%d: %s"
  9372. (- (length org-todo-sets) -1
  9373. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9374. (length org-todo-sets)
  9375. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9376. (setq org-last-todo-state-is-todo
  9377. (not (member state org-done-keywords)))
  9378. (setq now-done-p (and (member state org-done-keywords)
  9379. (not (member this org-done-keywords))))
  9380. (and logging (org-local-logging logging))
  9381. (when (and (or org-todo-log-states org-log-done)
  9382. (not (eq org-inhibit-logging t))
  9383. (not (memq arg '(nextset previousset))))
  9384. ;; we need to look at recording a time and note
  9385. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9386. (nth 2 (assoc this org-todo-log-states))))
  9387. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9388. (setq dolog 'time))
  9389. (when (and state
  9390. (member state org-not-done-keywords)
  9391. (not (member this org-not-done-keywords)))
  9392. ;; This is now a todo state and was not one before
  9393. ;; If there was a CLOSED time stamp, get rid of it.
  9394. (org-add-planning-info nil nil 'closed))
  9395. (when (and now-done-p org-log-done)
  9396. ;; It is now done, and it was not done before
  9397. (org-add-planning-info 'closed (org-current-time))
  9398. (if (and (not dolog) (eq 'note org-log-done))
  9399. (org-add-log-setup 'done state this 'findpos 'note)))
  9400. (when (and state dolog)
  9401. ;; This is a non-nil state, and we need to log it
  9402. (org-add-log-setup 'state state this 'findpos dolog)))
  9403. ;; Fixup tag positioning
  9404. (org-todo-trigger-tag-changes state)
  9405. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9406. (when org-provide-todo-statistics
  9407. (org-update-parent-todo-statistics))
  9408. (run-hooks 'org-after-todo-state-change-hook)
  9409. (if (and arg (not (member state org-done-keywords)))
  9410. (setq head (org-get-todo-sequence-head state)))
  9411. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9412. ;; Do we need to trigger a repeat?
  9413. (when now-done-p
  9414. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9415. ;; This is for the agenda, take a snapshot of the headline.
  9416. (save-match-data
  9417. (setq org-agenda-headline-snapshot-before-repeat
  9418. (org-get-heading))))
  9419. (org-auto-repeat-maybe state))
  9420. ;; Fixup cursor location if close to the keyword
  9421. (if (and (outline-on-heading-p)
  9422. (not (bolp))
  9423. (save-excursion (beginning-of-line 1)
  9424. (looking-at org-todo-line-regexp))
  9425. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9426. (progn
  9427. (goto-char (or (match-end 2) (match-end 1)))
  9428. (and (looking-at " ") (just-one-space))))
  9429. (when org-trigger-hook
  9430. (save-excursion
  9431. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9432. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9433. "Block turning an entry into a TODO, using the hierarchy.
  9434. This checks whether the current task should be blocked from state
  9435. changes. Such blocking occurs when:
  9436. 1. The task has children which are not all in a completed state.
  9437. 2. A task has a parent with the property :ORDERED:, and there
  9438. are siblings prior to the current task with incomplete
  9439. status.
  9440. 3. The parent of the task is blocked because it has siblings that should
  9441. be done first, or is child of a block grandparent TODO entry."
  9442. (if (not org-enforce-todo-dependencies)
  9443. t ; if locally turned off don't block
  9444. (catch 'dont-block
  9445. ;; If this is not a todo state change, or if this entry is already DONE,
  9446. ;; do not block
  9447. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9448. (member (plist-get change-plist :from)
  9449. (cons 'done org-done-keywords))
  9450. (member (plist-get change-plist :to)
  9451. (cons 'todo org-not-done-keywords))
  9452. (not (plist-get change-plist :to)))
  9453. (throw 'dont-block t))
  9454. ;; If this task has children, and any are undone, it's blocked
  9455. (save-excursion
  9456. (org-back-to-heading t)
  9457. (let ((this-level (funcall outline-level)))
  9458. (outline-next-heading)
  9459. (let ((child-level (funcall outline-level)))
  9460. (while (and (not (eobp))
  9461. (> child-level this-level))
  9462. ;; this todo has children, check whether they are all
  9463. ;; completed
  9464. (if (and (not (org-entry-is-done-p))
  9465. (org-entry-is-todo-p))
  9466. (throw 'dont-block nil))
  9467. (outline-next-heading)
  9468. (setq child-level (funcall outline-level))))))
  9469. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9470. ;; any previous siblings are undone, it's blocked
  9471. (save-excursion
  9472. (org-back-to-heading t)
  9473. (let* ((pos (point))
  9474. (parent-pos (and (org-up-heading-safe) (point))))
  9475. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9476. (when (and (org-entry-get (point) "ORDERED")
  9477. (forward-line 1)
  9478. (re-search-forward org-not-done-heading-regexp pos t))
  9479. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9480. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9481. (while t
  9482. (goto-char parent-pos)
  9483. (if (not (looking-at org-not-done-heading-regexp))
  9484. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9485. (setq pos (point))
  9486. (setq parent-pos (and (org-up-heading-safe) (point)))
  9487. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9488. (when (and (org-entry-get (point) "ORDERED")
  9489. (forward-line 1)
  9490. (re-search-forward org-not-done-heading-regexp pos t))
  9491. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9492. (defcustom org-track-ordered-property-with-tag nil
  9493. "Should the ORDERED property also be shown as a tag?
  9494. The ORDERED property decides if an entry should require subtasks to be
  9495. completed in sequence. Since a property is not very visible, setting
  9496. this option means that toggling the ORDERED property with the command
  9497. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9498. not relevant for the behavior, but it makes things more visible.
  9499. Note that toggling the tag with tags commands will not change the property
  9500. and therefore not influence behavior!
  9501. This can be t, meaning the tag ORDERED should be used, It can also be a
  9502. string to select a different tag for this task."
  9503. :group 'org-todo
  9504. :type '(choice
  9505. (const :tag "No tracking" nil)
  9506. (const :tag "Track with ORDERED tag" t)
  9507. (string :tag "Use other tag")))
  9508. (defun org-toggle-ordered-property ()
  9509. "Toggle the ORDERED property of the current entry.
  9510. For better visibility, you can track the value of this property with a tag.
  9511. See variable `org-track-ordered-property-with-tag'."
  9512. (interactive)
  9513. (let* ((t1 org-track-ordered-property-with-tag)
  9514. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9515. (save-excursion
  9516. (org-back-to-heading)
  9517. (if (org-entry-get nil "ORDERED")
  9518. (progn
  9519. (org-delete-property "ORDERED")
  9520. (and tag (org-toggle-tag tag 'off))
  9521. (message "Subtasks can be completed in arbitrary order"))
  9522. (org-entry-put nil "ORDERED" "t")
  9523. (and tag (org-toggle-tag tag 'on))
  9524. (message "Subtasks must be completed in sequence")))))
  9525. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9526. (defun org-block-todo-from-checkboxes (change-plist)
  9527. "Block turning an entry into a TODO, using checkboxes.
  9528. This checks whether the current task should be blocked from state
  9529. changes because there are unchecked boxes in this entry."
  9530. (if (not org-enforce-todo-checkbox-dependencies)
  9531. t ; if locally turned off don't block
  9532. (catch 'dont-block
  9533. ;; If this is not a todo state change, or if this entry is already DONE,
  9534. ;; do not block
  9535. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9536. (member (plist-get change-plist :from)
  9537. (cons 'done org-done-keywords))
  9538. (member (plist-get change-plist :to)
  9539. (cons 'todo org-not-done-keywords))
  9540. (not (plist-get change-plist :to)))
  9541. (throw 'dont-block t))
  9542. ;; If this task has checkboxes that are not checked, it's blocked
  9543. (save-excursion
  9544. (org-back-to-heading t)
  9545. (let ((beg (point)) end)
  9546. (outline-next-heading)
  9547. (setq end (point))
  9548. (goto-char beg)
  9549. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9550. end t)
  9551. (progn
  9552. (if (boundp 'org-blocked-by-checkboxes)
  9553. (setq org-blocked-by-checkboxes t))
  9554. (throw 'dont-block nil)))))
  9555. t))) ; do not block
  9556. (defun org-entry-blocked-p ()
  9557. "Is the current entry blocked?"
  9558. (if (org-entry-get nil "NOBLOCKING")
  9559. nil ;; Never block this entry
  9560. (not
  9561. (run-hook-with-args-until-failure
  9562. 'org-blocker-hook
  9563. (list :type 'todo-state-change
  9564. :position (point)
  9565. :from 'todo
  9566. :to 'done)))))
  9567. (defun org-update-statistics-cookies (all)
  9568. "Update the statistics cookie, either from TODO or from checkboxes.
  9569. This should be called with the cursor in a line with a statistics cookie."
  9570. (interactive "P")
  9571. (if all
  9572. (progn
  9573. (org-update-checkbox-count 'all)
  9574. (org-map-entries 'org-update-parent-todo-statistics))
  9575. (if (not (org-on-heading-p))
  9576. (org-update-checkbox-count)
  9577. (let ((pos (move-marker (make-marker) (point)))
  9578. end l1 l2)
  9579. (ignore-errors (org-back-to-heading t))
  9580. (if (not (org-on-heading-p))
  9581. (org-update-checkbox-count)
  9582. (setq l1 (org-outline-level))
  9583. (setq end (save-excursion
  9584. (outline-next-heading)
  9585. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9586. (point)))
  9587. (if (and (save-excursion
  9588. (re-search-forward
  9589. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9590. (not (save-excursion (re-search-forward
  9591. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9592. (org-update-checkbox-count)
  9593. (if (and l2 (> l2 l1))
  9594. (progn
  9595. (goto-char end)
  9596. (org-update-parent-todo-statistics))
  9597. (goto-char pos)
  9598. (beginning-of-line 1)
  9599. (while (re-search-forward
  9600. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9601. (point-at-eol) t)
  9602. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9603. (goto-char pos)
  9604. (move-marker pos nil)))))
  9605. (defvar org-entry-property-inherited-from) ;; defined below
  9606. (defun org-update-parent-todo-statistics ()
  9607. "Update any statistics cookie in the parent of the current headline.
  9608. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9609. the entire subtree and this will travel up the hierarchy and update
  9610. statistics everywhere."
  9611. (interactive)
  9612. (let* ((lim 0) prop
  9613. (recursive (or (not org-hierarchical-todo-statistics)
  9614. (string-match
  9615. "\\<recursive\\>"
  9616. (or (setq prop (org-entry-get
  9617. nil "COOKIE_DATA" 'inherit)) ""))))
  9618. (lim (or (and prop (marker-position
  9619. org-entry-property-inherited-from))
  9620. lim))
  9621. (first t)
  9622. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9623. level ltoggle l1 new ndel
  9624. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9625. (catch 'exit
  9626. (save-excursion
  9627. (beginning-of-line 1)
  9628. (if (org-at-heading-p)
  9629. (setq ltoggle (funcall outline-level))
  9630. (error "This should not happen"))
  9631. (while (and (setq level (org-up-heading-safe))
  9632. (or recursive first)
  9633. (>= (point) lim))
  9634. (setq first nil cookie-present nil)
  9635. (unless (and level
  9636. (not (string-match
  9637. "\\<checkbox\\>"
  9638. (downcase
  9639. (or (org-entry-get
  9640. nil "COOKIE_DATA")
  9641. "")))))
  9642. (throw 'exit nil))
  9643. (while (re-search-forward box-re (point-at-eol) t)
  9644. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9645. (setq is-percent (match-end 2))
  9646. (save-match-data
  9647. (unless (outline-next-heading) (throw 'exit nil))
  9648. (while (and (looking-at org-complex-heading-regexp)
  9649. (> (setq l1 (length (match-string 1))) level))
  9650. (setq kwd (and (or recursive (= l1 ltoggle))
  9651. (match-string 2)))
  9652. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9653. (and (listp org-provide-todo-statistics)
  9654. (or (member kwd org-provide-todo-statistics)
  9655. (member kwd org-done-keywords))))
  9656. (setq cnt-all (1+ cnt-all))
  9657. (if (eq org-provide-todo-statistics t)
  9658. (and kwd (setq cnt-all (1+ cnt-all)))))
  9659. (and (member kwd org-done-keywords)
  9660. (setq cnt-done (1+ cnt-done)))
  9661. (outline-next-heading)))
  9662. (setq new
  9663. (if is-percent
  9664. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9665. (format "[%d/%d]" cnt-done cnt-all))
  9666. ndel (- (match-end 0) (match-beginning 0)))
  9667. (goto-char (match-beginning 0))
  9668. (insert new)
  9669. (delete-region (point) (+ (point) ndel)))
  9670. (when cookie-present
  9671. (run-hook-with-args 'org-after-todo-statistics-hook
  9672. cnt-done (- cnt-all cnt-done))))))
  9673. (run-hooks 'org-todo-statistics-hook)))
  9674. (defvar org-after-todo-statistics-hook nil
  9675. "Hook that is called after a TODO statistics cookie has been updated.
  9676. Each function is called with two arguments: the number of not-done entries
  9677. and the number of done entries.
  9678. For example, the following function, when added to this hook, will switch
  9679. an entry to DONE when all children are done, and back to TODO when new
  9680. entries are set to a TODO status. Note that this hook is only called
  9681. when there is a statistics cookie in the headline!
  9682. (defun org-summary-todo (n-done n-not-done)
  9683. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9684. (let (org-log-done org-log-states) ; turn off logging
  9685. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9686. ")
  9687. (defvar org-todo-statistics-hook nil
  9688. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9689. This hook runs even if there is no statistics cookie present, in which case
  9690. `org-after-todo-statistics-hook' would not run.")
  9691. (defun org-todo-trigger-tag-changes (state)
  9692. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9693. (let ((l org-todo-state-tags-triggers)
  9694. changes)
  9695. (when (or (not state) (equal state ""))
  9696. (setq changes (append changes (cdr (assoc "" l)))))
  9697. (when (and (stringp state) (> (length state) 0))
  9698. (setq changes (append changes (cdr (assoc state l)))))
  9699. (when (member state org-not-done-keywords)
  9700. (setq changes (append changes (cdr (assoc 'todo l)))))
  9701. (when (member state org-done-keywords)
  9702. (setq changes (append changes (cdr (assoc 'done l)))))
  9703. (dolist (c changes)
  9704. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9705. (defun org-local-logging (value)
  9706. "Get logging settings from a property VALUE."
  9707. (let* (words w a)
  9708. ;; directly set the variables, they are already local.
  9709. (setq org-log-done nil
  9710. org-log-repeat nil
  9711. org-todo-log-states nil)
  9712. (setq words (org-split-string value))
  9713. (while (setq w (pop words))
  9714. (cond
  9715. ((setq a (assoc w org-startup-options))
  9716. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9717. (set (nth 1 a) (nth 2 a))))
  9718. ((setq a (org-extract-log-state-settings w))
  9719. (and (member (car a) org-todo-keywords-1)
  9720. (push a org-todo-log-states)))))))
  9721. (defun org-get-todo-sequence-head (kwd)
  9722. "Return the head of the TODO sequence to which KWD belongs.
  9723. If KWD is not set, check if there is a text property remembering the
  9724. right sequence."
  9725. (let (p)
  9726. (cond
  9727. ((not kwd)
  9728. (or (get-text-property (point-at-bol) 'org-todo-head)
  9729. (progn
  9730. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9731. nil (point-at-eol)))
  9732. (get-text-property p 'org-todo-head))))
  9733. ((not (member kwd org-todo-keywords-1))
  9734. (car org-todo-keywords-1))
  9735. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9736. (defun org-fast-todo-selection ()
  9737. "Fast TODO keyword selection with single keys.
  9738. Returns the new TODO keyword, or nil if no state change should occur."
  9739. (let* ((fulltable org-todo-key-alist)
  9740. (done-keywords org-done-keywords) ;; needed for the faces.
  9741. (maxlen (apply 'max (mapcar
  9742. (lambda (x)
  9743. (if (stringp (car x)) (string-width (car x)) 0))
  9744. fulltable)))
  9745. (expert nil)
  9746. (fwidth (+ maxlen 3 1 3))
  9747. (ncol (/ (- (window-width) 4) fwidth))
  9748. tg cnt e c tbl
  9749. groups ingroup)
  9750. (save-excursion
  9751. (save-window-excursion
  9752. (if expert
  9753. (set-buffer (get-buffer-create " *Org todo*"))
  9754. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9755. (erase-buffer)
  9756. (org-set-local 'org-done-keywords done-keywords)
  9757. (setq tbl fulltable cnt 0)
  9758. (while (setq e (pop tbl))
  9759. (cond
  9760. ((equal e '(:startgroup))
  9761. (push '() groups) (setq ingroup t)
  9762. (when (not (= cnt 0))
  9763. (setq cnt 0)
  9764. (insert "\n"))
  9765. (insert "{ "))
  9766. ((equal e '(:endgroup))
  9767. (setq ingroup nil cnt 0)
  9768. (insert "}\n"))
  9769. ((equal e '(:newline))
  9770. (when (not (= cnt 0))
  9771. (setq cnt 0)
  9772. (insert "\n")
  9773. (setq e (car tbl))
  9774. (while (equal (car tbl) '(:newline))
  9775. (insert "\n")
  9776. (setq tbl (cdr tbl)))))
  9777. (t
  9778. (setq tg (car e) c (cdr e))
  9779. (if ingroup (push tg (car groups)))
  9780. (setq tg (org-add-props tg nil 'face
  9781. (org-get-todo-face tg)))
  9782. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9783. (insert "[" c "] " tg (make-string
  9784. (- fwidth 4 (length tg)) ?\ ))
  9785. (when (= (setq cnt (1+ cnt)) ncol)
  9786. (insert "\n")
  9787. (if ingroup (insert " "))
  9788. (setq cnt 0)))))
  9789. (insert "\n")
  9790. (goto-char (point-min))
  9791. (if (not expert) (org-fit-window-to-buffer))
  9792. (message "[a-z..]:Set [SPC]:clear")
  9793. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9794. (cond
  9795. ((or (= c ?\C-g)
  9796. (and (= c ?q) (not (rassoc c fulltable))))
  9797. (setq quit-flag t))
  9798. ((= c ?\ ) nil)
  9799. ((setq e (rassoc c fulltable) tg (car e))
  9800. tg)
  9801. (t (setq quit-flag t)))))))
  9802. (defun org-entry-is-todo-p ()
  9803. (member (org-get-todo-state) org-not-done-keywords))
  9804. (defun org-entry-is-done-p ()
  9805. (member (org-get-todo-state) org-done-keywords))
  9806. (defun org-get-todo-state ()
  9807. (save-excursion
  9808. (org-back-to-heading t)
  9809. (and (looking-at org-todo-line-regexp)
  9810. (match-end 2)
  9811. (match-string 2))))
  9812. (defun org-at-date-range-p (&optional inactive-ok)
  9813. "Is the cursor inside a date range?"
  9814. (interactive)
  9815. (save-excursion
  9816. (catch 'exit
  9817. (let ((pos (point)))
  9818. (skip-chars-backward "^[<\r\n")
  9819. (skip-chars-backward "<[")
  9820. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9821. (>= (match-end 0) pos)
  9822. (throw 'exit t))
  9823. (skip-chars-backward "^<[\r\n")
  9824. (skip-chars-backward "<[")
  9825. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9826. (>= (match-end 0) pos)
  9827. (throw 'exit t)))
  9828. nil)))
  9829. (defun org-get-repeat (&optional tagline)
  9830. "Check if there is a deadline/schedule with repeater in this entry."
  9831. (save-match-data
  9832. (save-excursion
  9833. (org-back-to-heading t)
  9834. (and (re-search-forward (if tagline
  9835. (concat tagline "\\s-*" org-repeat-re)
  9836. org-repeat-re)
  9837. (org-entry-end-position) t)
  9838. (match-string-no-properties 1)))))
  9839. (defvar org-last-changed-timestamp)
  9840. (defvar org-last-inserted-timestamp)
  9841. (defvar org-log-post-message)
  9842. (defvar org-log-note-purpose)
  9843. (defvar org-log-note-how)
  9844. (defvar org-log-note-extra)
  9845. (defun org-auto-repeat-maybe (done-word)
  9846. "Check if the current headline contains a repeated deadline/schedule.
  9847. If yes, set TODO state back to what it was and change the base date
  9848. of repeating deadline/scheduled time stamps to new date.
  9849. This function is run automatically after each state change to a DONE state."
  9850. ;; last-state is dynamically scoped into this function
  9851. (let* ((repeat (org-get-repeat))
  9852. (aa (assoc last-state org-todo-kwd-alist))
  9853. (interpret (nth 1 aa))
  9854. (head (nth 2 aa))
  9855. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9856. (msg "Entry repeats: ")
  9857. (org-log-done nil)
  9858. (org-todo-log-states nil)
  9859. (nshiftmax 10) (nshift 0)
  9860. re type n what ts time to-state)
  9861. (when repeat
  9862. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9863. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  9864. org-todo-repeat-to-state))
  9865. (unless (and to-state (member to-state org-todo-keywords-1))
  9866. (setq to-state (if (eq interpret 'type) last-state head)))
  9867. (org-todo to-state)
  9868. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  9869. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9870. (org-time-stamp-format t t))))
  9871. (when org-log-repeat
  9872. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9873. (memq 'org-add-log-note post-command-hook))
  9874. ;; OK, we are already setup for some record
  9875. (if (eq org-log-repeat 'note)
  9876. ;; make sure we take a note, not only a time stamp
  9877. (setq org-log-note-how 'note))
  9878. ;; Set up for taking a record
  9879. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9880. last-state
  9881. 'findpos org-log-repeat)))
  9882. (org-back-to-heading t)
  9883. (org-add-planning-info nil nil 'closed)
  9884. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9885. org-deadline-time-regexp "\\)\\|\\("
  9886. org-ts-regexp "\\)"))
  9887. (while (re-search-forward
  9888. re (save-excursion (outline-next-heading) (point)) t)
  9889. (setq type (if (match-end 1) org-scheduled-string
  9890. (if (match-end 3) org-deadline-string "Plain:"))
  9891. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9892. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9893. (setq n (string-to-number (match-string 2 ts))
  9894. what (match-string 3 ts))
  9895. (if (equal what "w") (setq n (* n 7) what "d"))
  9896. ;; Preparation, see if we need to modify the start date for the change
  9897. (when (match-end 1)
  9898. (setq time (save-match-data (org-time-string-to-time ts)))
  9899. (cond
  9900. ((equal (match-string 1 ts) ".")
  9901. ;; Shift starting date to today
  9902. (org-timestamp-change
  9903. (- (time-to-days (current-time)) (time-to-days time))
  9904. 'day))
  9905. ((equal (match-string 1 ts) "+")
  9906. (while (or (= nshift 0)
  9907. (<= (time-to-days time) (time-to-days (current-time))))
  9908. (when (= (incf nshift) nshiftmax)
  9909. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9910. (error "Abort")))
  9911. (org-timestamp-change n (cdr (assoc what whata)))
  9912. (org-at-timestamp-p t)
  9913. (setq ts (match-string 1))
  9914. (setq time (save-match-data (org-time-string-to-time ts))))
  9915. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9916. ;; rematch, so that we have everything in place for the real shift
  9917. (org-at-timestamp-p t)
  9918. (setq ts (match-string 1))
  9919. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9920. (org-timestamp-change n (cdr (assoc what whata)))
  9921. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9922. (setq org-log-post-message msg)
  9923. (message "%s" msg))))
  9924. (defun org-show-todo-tree (arg)
  9925. "Make a compact tree which shows all headlines marked with TODO.
  9926. The tree will show the lines where the regexp matches, and all higher
  9927. headlines above the match.
  9928. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9929. With a numeric prefix N, construct a sparse tree for the Nth element
  9930. of `org-todo-keywords-1'."
  9931. (interactive "P")
  9932. (let ((case-fold-search nil)
  9933. (kwd-re
  9934. (cond ((null arg) org-not-done-regexp)
  9935. ((equal arg '(4))
  9936. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9937. (mapcar 'list org-todo-keywords-1))))
  9938. (concat "\\("
  9939. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9940. "\\)\\>")))
  9941. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9942. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9943. org-todo-keywords-1)))
  9944. (t (error "Invalid prefix argument: %s" arg)))))
  9945. (message "%d TODO entries found"
  9946. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9947. (defun org-deadline (&optional remove time)
  9948. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9949. With argument REMOVE, remove any deadline from the item.
  9950. When TIME is set, it should be an internal time specification, and the
  9951. scheduling will use the corresponding date."
  9952. (interactive "P")
  9953. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9954. (repeater (and old-date
  9955. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9956. (match-string 1 old-date))))
  9957. (if remove
  9958. (progn
  9959. (when (and old-date org-log-redeadline)
  9960. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9961. org-log-redeadline))
  9962. (org-remove-timestamp-with-keyword org-deadline-string)
  9963. (message "Item no longer has a deadline."))
  9964. (org-add-planning-info 'deadline time 'closed)
  9965. (when (and old-date org-log-redeadline
  9966. (not (equal old-date
  9967. (substring org-last-inserted-timestamp 1 -1))))
  9968. (org-add-log-setup 'redeadline nil old-date 'findpos
  9969. org-log-redeadline))
  9970. (when repeater
  9971. (save-excursion
  9972. (org-back-to-heading t)
  9973. (when (re-search-forward (concat org-deadline-string " "
  9974. org-last-inserted-timestamp)
  9975. (save-excursion
  9976. (outline-next-heading) (point)) t)
  9977. (goto-char (1- (match-end 0)))
  9978. (insert " " repeater)
  9979. (setq org-last-inserted-timestamp
  9980. (concat (substring org-last-inserted-timestamp 0 -1)
  9981. " " repeater
  9982. (substring org-last-inserted-timestamp -1))))))
  9983. (message "Deadline on %s" org-last-inserted-timestamp))))
  9984. (defun org-schedule (&optional remove time)
  9985. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9986. With argument REMOVE, remove any scheduling date from the item.
  9987. When TIME is set, it should be an internal time specification, and the
  9988. scheduling will use the corresponding date."
  9989. (interactive "P")
  9990. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9991. (repeater (and old-date
  9992. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9993. (match-string 1 old-date))))
  9994. (if remove
  9995. (progn
  9996. (when (and old-date org-log-reschedule)
  9997. (org-add-log-setup 'delschedule nil old-date 'findpos
  9998. org-log-reschedule))
  9999. (org-remove-timestamp-with-keyword org-scheduled-string)
  10000. (message "Item is no longer scheduled."))
  10001. (org-add-planning-info 'scheduled time 'closed)
  10002. (when (and old-date org-log-reschedule
  10003. (not (equal old-date
  10004. (substring org-last-inserted-timestamp 1 -1))))
  10005. (org-add-log-setup 'reschedule nil old-date 'findpos
  10006. org-log-reschedule))
  10007. (when repeater
  10008. (save-excursion
  10009. (org-back-to-heading t)
  10010. (when (re-search-forward (concat org-scheduled-string " "
  10011. org-last-inserted-timestamp)
  10012. (save-excursion
  10013. (outline-next-heading) (point)) t)
  10014. (goto-char (1- (match-end 0)))
  10015. (insert " " repeater)
  10016. (setq org-last-inserted-timestamp
  10017. (concat (substring org-last-inserted-timestamp 0 -1)
  10018. " " repeater
  10019. (substring org-last-inserted-timestamp -1))))))
  10020. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10021. (defun org-get-scheduled-time (pom &optional inherit)
  10022. "Get the scheduled time as a time tuple, of a format suitable
  10023. for calling org-schedule with, or if there is no scheduling,
  10024. returns nil."
  10025. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10026. (when time
  10027. (apply 'encode-time (org-parse-time-string time)))))
  10028. (defun org-get-deadline-time (pom &optional inherit)
  10029. "Get the deadine as a time tuple, of a format suitable for
  10030. calling org-deadline with, or if there is no scheduling, returns
  10031. nil."
  10032. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10033. (when time
  10034. (apply 'encode-time (org-parse-time-string time)))))
  10035. (defun org-remove-timestamp-with-keyword (keyword)
  10036. "Remove all time stamps with KEYWORD in the current entry."
  10037. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10038. beg)
  10039. (save-excursion
  10040. (org-back-to-heading t)
  10041. (setq beg (point))
  10042. (outline-next-heading)
  10043. (while (re-search-backward re beg t)
  10044. (replace-match "")
  10045. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10046. (equal (char-before) ?\ ))
  10047. (backward-delete-char 1)
  10048. (if (string-match "^[ \t]*$" (buffer-substring
  10049. (point-at-bol) (point-at-eol)))
  10050. (delete-region (point-at-bol)
  10051. (min (point-max) (1+ (point-at-eol))))))))))
  10052. (defun org-add-planning-info (what &optional time &rest remove)
  10053. "Insert new timestamp with keyword in the line directly after the headline.
  10054. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10055. If non is given, the user is prompted for a date.
  10056. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10057. be removed."
  10058. (interactive)
  10059. (let (org-time-was-given org-end-time-was-given ts
  10060. end default-time default-input)
  10061. (catch 'exit
  10062. (when (and (not time) (memq what '(scheduled deadline)))
  10063. ;; Try to get a default date/time from existing timestamp
  10064. (save-excursion
  10065. (org-back-to-heading t)
  10066. (setq end (save-excursion (outline-next-heading) (point)))
  10067. (when (re-search-forward (if (eq what 'scheduled)
  10068. org-scheduled-time-regexp
  10069. org-deadline-time-regexp)
  10070. end t)
  10071. (setq ts (match-string 1)
  10072. default-time
  10073. (apply 'encode-time (org-parse-time-string ts))
  10074. default-input (and ts (org-get-compact-tod ts))))))
  10075. (when what
  10076. ;; If necessary, get the time from the user
  10077. (setq time (or time (org-read-date nil 'to-time nil nil
  10078. default-time default-input))))
  10079. (when (and org-insert-labeled-timestamps-at-point
  10080. (member what '(scheduled deadline)))
  10081. (insert
  10082. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10083. (org-insert-time-stamp time org-time-was-given
  10084. nil nil nil (list org-end-time-was-given))
  10085. (setq what nil))
  10086. (save-excursion
  10087. (save-restriction
  10088. (let (col list elt ts buffer-invisibility-spec)
  10089. (org-back-to-heading t)
  10090. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10091. (goto-char (match-end 1))
  10092. (setq col (current-column))
  10093. (goto-char (match-end 0))
  10094. (if (eobp) (insert "\n") (forward-char 1))
  10095. (when (and (not what)
  10096. (not (looking-at
  10097. (concat "[ \t]*"
  10098. org-keyword-time-not-clock-regexp))))
  10099. ;; Nothing to add, nothing to remove...... :-)
  10100. (throw 'exit nil))
  10101. (if (and (not (looking-at outline-regexp))
  10102. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10103. "[^\r\n]*"))
  10104. (not (equal (match-string 1) org-clock-string)))
  10105. (narrow-to-region (match-beginning 0) (match-end 0))
  10106. (insert-before-markers "\n")
  10107. (backward-char 1)
  10108. (narrow-to-region (point) (point))
  10109. (and org-adapt-indentation (org-indent-to-column col)))
  10110. ;; Check if we have to remove something.
  10111. (setq list (cons what remove))
  10112. (while list
  10113. (setq elt (pop list))
  10114. (goto-char (point-min))
  10115. (when (or (and (eq elt 'scheduled)
  10116. (re-search-forward org-scheduled-time-regexp nil t))
  10117. (and (eq elt 'deadline)
  10118. (re-search-forward org-deadline-time-regexp nil t))
  10119. (and (eq elt 'closed)
  10120. (re-search-forward org-closed-time-regexp nil t)))
  10121. (replace-match "")
  10122. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10123. (skip-chars-backward " ")
  10124. (if (looking-at " +") (replace-match ""))))
  10125. (goto-char (point-max))
  10126. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10127. (when what
  10128. (insert
  10129. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10130. (cond ((eq what 'scheduled) org-scheduled-string)
  10131. ((eq what 'deadline) org-deadline-string)
  10132. ((eq what 'closed) org-closed-string))
  10133. " ")
  10134. (setq ts (org-insert-time-stamp
  10135. time
  10136. (or org-time-was-given
  10137. (and (eq what 'closed) org-log-done-with-time))
  10138. (eq what 'closed)
  10139. nil nil (list org-end-time-was-given)))
  10140. (end-of-line 1))
  10141. (goto-char (point-min))
  10142. (widen)
  10143. (if (and (looking-at "[ \t]+\n")
  10144. (equal (char-before) ?\n))
  10145. (delete-region (1- (point)) (point-at-eol)))
  10146. ts))))))
  10147. (defvar org-log-note-marker (make-marker))
  10148. (defvar org-log-note-purpose nil)
  10149. (defvar org-log-note-state nil)
  10150. (defvar org-log-note-previous-state nil)
  10151. (defvar org-log-note-how nil)
  10152. (defvar org-log-note-extra nil)
  10153. (defvar org-log-note-window-configuration nil)
  10154. (defvar org-log-note-return-to (make-marker))
  10155. (defvar org-log-post-message nil
  10156. "Message to be displayed after a log note has been stored.
  10157. The auto-repeater uses this.")
  10158. (defun org-add-note ()
  10159. "Add a note to the current entry.
  10160. This is done in the same way as adding a state change note."
  10161. (interactive)
  10162. (org-add-log-setup 'note nil nil 'findpos nil))
  10163. (defvar org-property-end-re)
  10164. (defun org-add-log-setup (&optional purpose state prev-state
  10165. findpos how &optional extra)
  10166. "Set up the post command hook to take a note.
  10167. If this is about to TODO state change, the new state is expected in STATE.
  10168. When FINDPOS is non-nil, find the correct position for the note in
  10169. the current entry. If not, assume that it can be inserted at point.
  10170. HOW is an indicator what kind of note should be created.
  10171. EXTRA is additional text that will be inserted into the notes buffer."
  10172. (let* ((org-log-into-drawer (org-log-into-drawer))
  10173. (drawer (cond ((stringp org-log-into-drawer)
  10174. org-log-into-drawer)
  10175. (org-log-into-drawer "LOGBOOK")
  10176. (t nil))))
  10177. (save-restriction
  10178. (save-excursion
  10179. (when findpos
  10180. (org-back-to-heading t)
  10181. (narrow-to-region (point) (save-excursion
  10182. (outline-next-heading) (point)))
  10183. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10184. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10185. "[^\r\n]*\\)?"))
  10186. (goto-char (match-end 0))
  10187. (cond
  10188. (drawer
  10189. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10190. nil t)
  10191. (progn
  10192. (goto-char (match-end 0))
  10193. (or org-log-states-order-reversed
  10194. (and (re-search-forward org-property-end-re nil t)
  10195. (goto-char (1- (match-beginning 0))))))
  10196. (insert "\n:" drawer ":\n:END:")
  10197. (beginning-of-line 0)
  10198. (org-indent-line-function)
  10199. (beginning-of-line 2)
  10200. (org-indent-line-function)
  10201. (end-of-line 0)))
  10202. ((and org-log-state-notes-insert-after-drawers
  10203. (save-excursion
  10204. (forward-line) (looking-at org-drawer-regexp)))
  10205. (forward-line)
  10206. (while (looking-at org-drawer-regexp)
  10207. (goto-char (match-end 0))
  10208. (re-search-forward org-property-end-re (point-max) t)
  10209. (forward-line))
  10210. (forward-line -1)))
  10211. (unless org-log-states-order-reversed
  10212. (and (= (char-after) ?\n) (forward-char 1))
  10213. (org-skip-over-state-notes)
  10214. (skip-chars-backward " \t\n\r")))
  10215. (move-marker org-log-note-marker (point))
  10216. (setq org-log-note-purpose purpose
  10217. org-log-note-state state
  10218. org-log-note-previous-state prev-state
  10219. org-log-note-how how
  10220. org-log-note-extra extra)
  10221. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10222. (defun org-skip-over-state-notes ()
  10223. "Skip past the list of State notes in an entry."
  10224. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10225. (while (looking-at "[ \t]*- State")
  10226. (condition-case nil
  10227. (org-next-item)
  10228. (error (org-end-of-item)))))
  10229. (defun org-add-log-note (&optional purpose)
  10230. "Pop up a window for taking a note, and add this note later at point."
  10231. (remove-hook 'post-command-hook 'org-add-log-note)
  10232. (setq org-log-note-window-configuration (current-window-configuration))
  10233. (delete-other-windows)
  10234. (move-marker org-log-note-return-to (point))
  10235. (switch-to-buffer (marker-buffer org-log-note-marker))
  10236. (goto-char org-log-note-marker)
  10237. (org-switch-to-buffer-other-window "*Org Note*")
  10238. (erase-buffer)
  10239. (if (memq org-log-note-how '(time state))
  10240. (let (current-prefix-arg) (org-store-log-note))
  10241. (let ((org-inhibit-startup t)) (org-mode))
  10242. (insert (format "# Insert note for %s.
  10243. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10244. (cond
  10245. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10246. ((eq org-log-note-purpose 'done) "closed todo item")
  10247. ((eq org-log-note-purpose 'state)
  10248. (format "state change from \"%s\" to \"%s\""
  10249. (or org-log-note-previous-state "")
  10250. (or org-log-note-state "")))
  10251. ((eq org-log-note-purpose 'reschedule)
  10252. "rescheduling")
  10253. ((eq org-log-note-purpose 'delschedule)
  10254. "no longer scheduled")
  10255. ((eq org-log-note-purpose 'redeadline)
  10256. "changing deadline")
  10257. ((eq org-log-note-purpose 'deldeadline)
  10258. "removing deadline")
  10259. ((eq org-log-note-purpose 'refile)
  10260. "refiling")
  10261. ((eq org-log-note-purpose 'note)
  10262. "this entry")
  10263. (t (error "This should not happen")))))
  10264. (if org-log-note-extra (insert org-log-note-extra))
  10265. (org-set-local 'org-finish-function 'org-store-log-note)))
  10266. (defvar org-note-abort nil) ; dynamically scoped
  10267. (defun org-store-log-note ()
  10268. "Finish taking a log note, and insert it to where it belongs."
  10269. (let ((txt (buffer-string))
  10270. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10271. lines ind)
  10272. (kill-buffer (current-buffer))
  10273. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10274. (setq txt (replace-match "" t t txt)))
  10275. (if (string-match "\\s-+\\'" txt)
  10276. (setq txt (replace-match "" t t txt)))
  10277. (setq lines (org-split-string txt "\n"))
  10278. (when (and note (string-match "\\S-" note))
  10279. (setq note
  10280. (org-replace-escapes
  10281. note
  10282. (list (cons "%u" (user-login-name))
  10283. (cons "%U" user-full-name)
  10284. (cons "%t" (format-time-string
  10285. (org-time-stamp-format 'long 'inactive)
  10286. (current-time)))
  10287. (cons "%s" (if org-log-note-state
  10288. (concat "\"" org-log-note-state "\"")
  10289. ""))
  10290. (cons "%S" (if org-log-note-previous-state
  10291. (concat "\"" org-log-note-previous-state "\"")
  10292. "\"\"")))))
  10293. (if lines (setq note (concat note " \\\\")))
  10294. (push note lines))
  10295. (when (or current-prefix-arg org-note-abort)
  10296. (when org-log-into-drawer
  10297. (org-remove-empty-drawer-at
  10298. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10299. org-log-note-marker))
  10300. (setq lines nil))
  10301. (when lines
  10302. (with-current-buffer (marker-buffer org-log-note-marker)
  10303. (save-excursion
  10304. (goto-char org-log-note-marker)
  10305. (move-marker org-log-note-marker nil)
  10306. (end-of-line 1)
  10307. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10308. (insert "- " (pop lines))
  10309. (org-indent-line-function)
  10310. (beginning-of-line 1)
  10311. (looking-at "[ \t]*")
  10312. (setq ind (concat (match-string 0) " "))
  10313. (end-of-line 1)
  10314. (while lines (insert "\n" ind (pop lines)))
  10315. (message "Note stored")
  10316. (org-back-to-heading t)
  10317. (org-cycle-hide-drawers 'children)))))
  10318. (set-window-configuration org-log-note-window-configuration)
  10319. (with-current-buffer (marker-buffer org-log-note-return-to)
  10320. (goto-char org-log-note-return-to))
  10321. (move-marker org-log-note-return-to nil)
  10322. (and org-log-post-message (message "%s" org-log-post-message)))
  10323. (defun org-remove-empty-drawer-at (drawer pos)
  10324. "Remove an empty drawer DRAWER at position POS.
  10325. POS may also be a marker."
  10326. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10327. (save-excursion
  10328. (save-restriction
  10329. (widen)
  10330. (goto-char pos)
  10331. (if (org-in-regexp
  10332. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10333. (replace-match ""))))))
  10334. (defun org-sparse-tree (&optional arg)
  10335. "Create a sparse tree, prompt for the details.
  10336. This command can create sparse trees. You first need to select the type
  10337. of match used to create the tree:
  10338. t Show entries with a specific TODO keyword.
  10339. m Show entries selected by a tags/property match.
  10340. p Enter a property name and its value (both with completion on existing
  10341. names/values) and show entries with that property.
  10342. / Show entries matching a regular expression (`r' can be used as well)
  10343. d Show deadlines due within `org-deadline-warning-days'.
  10344. b Show deadlines and scheduled items before a date.
  10345. a Show deadlines and scheduled items after a date."
  10346. (interactive "P")
  10347. (let (ans kwd value)
  10348. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10349. (setq ans (read-char-exclusive))
  10350. (cond
  10351. ((equal ans ?d)
  10352. (call-interactively 'org-check-deadlines))
  10353. ((equal ans ?b)
  10354. (call-interactively 'org-check-before-date))
  10355. ((equal ans ?a)
  10356. (call-interactively 'org-check-after-date))
  10357. ((equal ans ?t)
  10358. (org-show-todo-tree '(4)))
  10359. ((member ans '(?T ?m))
  10360. (call-interactively 'org-match-sparse-tree))
  10361. ((member ans '(?p ?P))
  10362. (setq kwd (org-icompleting-read "Property: "
  10363. (mapcar 'list (org-buffer-property-keys))))
  10364. (setq value (org-icompleting-read "Value: "
  10365. (mapcar 'list (org-property-values kwd))))
  10366. (unless (string-match "\\`{.*}\\'" value)
  10367. (setq value (concat "\"" value "\"")))
  10368. (org-match-sparse-tree arg (concat kwd "=" value)))
  10369. ((member ans '(?r ?R ?/))
  10370. (call-interactively 'org-occur))
  10371. (t (error "No such sparse tree command \"%c\"" ans)))))
  10372. (defvar org-occur-highlights nil
  10373. "List of overlays used for occur matches.")
  10374. (make-variable-buffer-local 'org-occur-highlights)
  10375. (defvar org-occur-parameters nil
  10376. "Parameters of the active org-occur calls.
  10377. This is a list, each call to org-occur pushes as cons cell,
  10378. containing the regular expression and the callback, onto the list.
  10379. The list can contain several entries if `org-occur' has been called
  10380. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10381. will only contain one set of parameters. When the highlights are
  10382. removed (for example with `C-c C-c', or with the next edit (depending
  10383. on `org-remove-highlights-with-change'), this variable is emptied
  10384. as well.")
  10385. (make-variable-buffer-local 'org-occur-parameters)
  10386. (defun org-occur (regexp &optional keep-previous callback)
  10387. "Make a compact tree which shows all matches of REGEXP.
  10388. The tree will show the lines where the regexp matches, and all higher
  10389. headlines above the match. It will also show the heading after the match,
  10390. to make sure editing the matching entry is easy.
  10391. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10392. call to `org-occur' will be kept, to allow stacking of calls to this
  10393. command.
  10394. If CALLBACK is non-nil, it is a function which is called to confirm
  10395. that the match should indeed be shown."
  10396. (interactive "sRegexp: \nP")
  10397. (when (equal regexp "")
  10398. (error "Regexp cannot be empty"))
  10399. (unless keep-previous
  10400. (org-remove-occur-highlights nil nil t))
  10401. (push (cons regexp callback) org-occur-parameters)
  10402. (let ((cnt 0))
  10403. (save-excursion
  10404. (goto-char (point-min))
  10405. (if (or (not keep-previous) ; do not want to keep
  10406. (not org-occur-highlights)) ; no previous matches
  10407. ;; hide everything
  10408. (org-overview))
  10409. (while (re-search-forward regexp nil t)
  10410. (when (or (not callback)
  10411. (save-match-data (funcall callback)))
  10412. (setq cnt (1+ cnt))
  10413. (when org-highlight-sparse-tree-matches
  10414. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10415. (org-show-context 'occur-tree))))
  10416. (when org-remove-highlights-with-change
  10417. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10418. nil 'local))
  10419. (unless org-sparse-tree-open-archived-trees
  10420. (org-hide-archived-subtrees (point-min) (point-max)))
  10421. (run-hooks 'org-occur-hook)
  10422. (if (interactive-p)
  10423. (message "%d match(es) for regexp %s" cnt regexp))
  10424. cnt))
  10425. (defun org-show-context (&optional key)
  10426. "Make sure point and context and visible.
  10427. How much context is shown depends upon the variables
  10428. `org-show-hierarchy-above', `org-show-following-heading'. and
  10429. `org-show-siblings'."
  10430. (let ((heading-p (org-on-heading-p t))
  10431. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10432. (following-p (org-get-alist-option org-show-following-heading key))
  10433. (entry-p (org-get-alist-option org-show-entry-below key))
  10434. (siblings-p (org-get-alist-option org-show-siblings key)))
  10435. (catch 'exit
  10436. ;; Show heading or entry text
  10437. (if (and heading-p (not entry-p))
  10438. (org-flag-heading nil) ; only show the heading
  10439. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10440. (org-show-hidden-entry))) ; show entire entry
  10441. (when following-p
  10442. ;; Show next sibling, or heading below text
  10443. (save-excursion
  10444. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10445. (org-flag-heading nil))))
  10446. (when siblings-p (org-show-siblings))
  10447. (when hierarchy-p
  10448. ;; show all higher headings, possibly with siblings
  10449. (save-excursion
  10450. (while (and (condition-case nil
  10451. (progn (org-up-heading-all 1) t)
  10452. (error nil))
  10453. (not (bobp)))
  10454. (org-flag-heading nil)
  10455. (when siblings-p (org-show-siblings))))))))
  10456. (defvar org-reveal-start-hook nil
  10457. "Hook run before revealing a location.")
  10458. (defun org-reveal (&optional siblings)
  10459. "Show current entry, hierarchy above it, and the following headline.
  10460. This can be used to show a consistent set of context around locations
  10461. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10462. not t for the search context.
  10463. With optional argument SIBLINGS, on each level of the hierarchy all
  10464. siblings are shown. This repairs the tree structure to what it would
  10465. look like when opened with hierarchical calls to `org-cycle'.
  10466. With double optional argument `C-u C-u', go to the parent and show the
  10467. entire tree."
  10468. (interactive "P")
  10469. (run-hooks 'org-reveal-start-hook)
  10470. (let ((org-show-hierarchy-above t)
  10471. (org-show-following-heading t)
  10472. (org-show-siblings (if siblings t org-show-siblings)))
  10473. (org-show-context nil))
  10474. (when (equal siblings '(16))
  10475. (save-excursion
  10476. (when (org-up-heading-safe)
  10477. (org-show-subtree)
  10478. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10479. (defun org-highlight-new-match (beg end)
  10480. "Highlight from BEG to END and mark the highlight is an occur headline."
  10481. (let ((ov (make-overlay beg end)))
  10482. (overlay-put ov 'face 'secondary-selection)
  10483. (push ov org-occur-highlights)))
  10484. (defun org-remove-occur-highlights (&optional beg end noremove)
  10485. "Remove the occur highlights from the buffer.
  10486. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10487. from the `before-change-functions' in the current buffer."
  10488. (interactive)
  10489. (unless org-inhibit-highlight-removal
  10490. (mapc 'delete-overlay org-occur-highlights)
  10491. (setq org-occur-highlights nil)
  10492. (setq org-occur-parameters nil)
  10493. (unless noremove
  10494. (remove-hook 'before-change-functions
  10495. 'org-remove-occur-highlights 'local))))
  10496. ;;;; Priorities
  10497. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10498. "Regular expression matching the priority indicator.")
  10499. (defvar org-remove-priority-next-time nil)
  10500. (defun org-priority-up ()
  10501. "Increase the priority of the current item."
  10502. (interactive)
  10503. (org-priority 'up))
  10504. (defun org-priority-down ()
  10505. "Decrease the priority of the current item."
  10506. (interactive)
  10507. (org-priority 'down))
  10508. (defun org-priority (&optional action)
  10509. "Change the priority of an item by ARG.
  10510. ACTION can be `set', `up', `down', or a character."
  10511. (interactive)
  10512. (unless org-enable-priority-commands
  10513. (error "Priority commands are disabled"))
  10514. (setq action (or action 'set))
  10515. (let (current new news have remove)
  10516. (save-excursion
  10517. (org-back-to-heading t)
  10518. (if (looking-at org-priority-regexp)
  10519. (setq current (string-to-char (match-string 2))
  10520. have t)
  10521. (setq current org-default-priority))
  10522. (cond
  10523. ((eq action 'remove)
  10524. (setq remove t new ?\ ))
  10525. ((or (eq action 'set)
  10526. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10527. (if (not (eq action 'set))
  10528. (setq new action)
  10529. (message "Priority %c-%c, SPC to remove: "
  10530. org-highest-priority org-lowest-priority)
  10531. (setq new (read-char-exclusive)))
  10532. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10533. (= (upcase org-lowest-priority) org-lowest-priority))
  10534. (setq new (upcase new)))
  10535. (cond ((equal new ?\ ) (setq remove t))
  10536. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10537. (error "Priority must be between `%c' and `%c'"
  10538. org-highest-priority org-lowest-priority))))
  10539. ((eq action 'up)
  10540. (if (and (not have) (eq last-command this-command))
  10541. (setq new org-lowest-priority)
  10542. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10543. org-default-priority (1- current)))))
  10544. ((eq action 'down)
  10545. (if (and (not have) (eq last-command this-command))
  10546. (setq new org-highest-priority)
  10547. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10548. org-default-priority (1+ current)))))
  10549. (t (error "Invalid action")))
  10550. (if (or (< (upcase new) org-highest-priority)
  10551. (> (upcase new) org-lowest-priority))
  10552. (setq remove t))
  10553. (setq news (format "%c" new))
  10554. (if have
  10555. (if remove
  10556. (replace-match "" t t nil 1)
  10557. (replace-match news t t nil 2))
  10558. (if remove
  10559. (error "No priority cookie found in line")
  10560. (let ((case-fold-search nil))
  10561. (looking-at org-todo-line-regexp))
  10562. (if (match-end 2)
  10563. (progn
  10564. (goto-char (match-end 2))
  10565. (insert " [#" news "]"))
  10566. (goto-char (match-beginning 3))
  10567. (insert "[#" news "] "))))
  10568. (org-preserve-lc (org-set-tags nil 'align)))
  10569. (if remove
  10570. (message "Priority removed")
  10571. (message "Priority of current item set to %s" news))))
  10572. (defun org-get-priority (s)
  10573. "Find priority cookie and return priority."
  10574. (save-match-data
  10575. (if (not (string-match org-priority-regexp s))
  10576. (* 1000 (- org-lowest-priority org-default-priority))
  10577. (* 1000 (- org-lowest-priority
  10578. (string-to-char (match-string 2 s)))))))
  10579. ;;;; Tags
  10580. (defvar org-agenda-archives-mode)
  10581. (defvar org-map-continue-from nil
  10582. "Position from where mapping should continue.
  10583. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10584. (defvar org-scanner-tags nil
  10585. "The current tag list while the tags scanner is running.")
  10586. (defvar org-trust-scanner-tags nil
  10587. "Should `org-get-tags-at' use the tags fro the scanner.
  10588. This is for internal dynamical scoping only.
  10589. When this is non-nil, the function `org-get-tags-at' will return the value
  10590. of `org-scanner-tags' instead of building the list by itself. This
  10591. can lead to large speed-ups when the tags scanner is used in a file with
  10592. many entries, and when the list of tags is retrieved, for example to
  10593. obtain a list of properties. Building the tags list for each entry in such
  10594. a file becomes an N^2 operation - but with this variable set, it scales
  10595. as N.")
  10596. (defun org-scan-tags (action matcher &optional todo-only)
  10597. "Scan headline tags with inheritance and produce output ACTION.
  10598. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10599. or `agenda' to produce an entry list for an agenda view. It can also be
  10600. a Lisp form or a function that should be called at each matched headline, in
  10601. this case the return value is a list of all return values from these calls.
  10602. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10603. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10604. only lines with a TODO keyword are included in the output."
  10605. (require 'org-agenda)
  10606. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10607. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10608. (org-re
  10609. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10610. (props (list 'face 'default
  10611. 'done-face 'org-agenda-done
  10612. 'undone-face 'default
  10613. 'mouse-face 'highlight
  10614. 'org-not-done-regexp org-not-done-regexp
  10615. 'org-todo-regexp org-todo-regexp
  10616. 'help-echo
  10617. (format "mouse-2 or RET jump to org file %s"
  10618. (abbreviate-file-name
  10619. (or (buffer-file-name (buffer-base-buffer))
  10620. (buffer-name (buffer-base-buffer)))))))
  10621. (case-fold-search nil)
  10622. (org-map-continue-from nil)
  10623. lspos tags tags-list
  10624. (tags-alist (list (cons 0 org-file-tags)))
  10625. (llast 0) rtn rtn1 level category i txt
  10626. todo marker entry priority)
  10627. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10628. (setq action (list 'lambda nil action)))
  10629. (save-excursion
  10630. (goto-char (point-min))
  10631. (when (eq action 'sparse-tree)
  10632. (org-overview)
  10633. (org-remove-occur-highlights))
  10634. (while (re-search-forward re nil t)
  10635. (catch :skip
  10636. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10637. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10638. (goto-char (setq lspos (match-beginning 0)))
  10639. (setq level (org-reduced-level (funcall outline-level))
  10640. category (org-get-category))
  10641. (setq i llast llast level)
  10642. ;; remove tag lists from same and sublevels
  10643. (while (>= i level)
  10644. (when (setq entry (assoc i tags-alist))
  10645. (setq tags-alist (delete entry tags-alist)))
  10646. (setq i (1- i)))
  10647. ;; add the next tags
  10648. (when tags
  10649. (setq tags (org-split-string tags ":")
  10650. tags-alist
  10651. (cons (cons level tags) tags-alist)))
  10652. ;; compile tags for current headline
  10653. (setq tags-list
  10654. (if org-use-tag-inheritance
  10655. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10656. tags)
  10657. org-scanner-tags tags-list)
  10658. (when org-use-tag-inheritance
  10659. (setcdr (car tags-alist)
  10660. (mapcar (lambda (x)
  10661. (setq x (copy-sequence x))
  10662. (org-add-prop-inherited x))
  10663. (cdar tags-alist))))
  10664. (when (and tags org-use-tag-inheritance
  10665. (or (not (eq t org-use-tag-inheritance))
  10666. org-tags-exclude-from-inheritance))
  10667. ;; selective inheritance, remove uninherited ones
  10668. (setcdr (car tags-alist)
  10669. (org-remove-uniherited-tags (cdar tags-alist))))
  10670. (when (and (or (not todo-only)
  10671. (and (member todo org-not-done-keywords)
  10672. (or (not org-agenda-tags-todo-honor-ignore-options)
  10673. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10674. (let ((case-fold-search t)) (eval matcher))
  10675. (or
  10676. (not (member org-archive-tag tags-list))
  10677. ;; we have an archive tag, should we use this anyway?
  10678. (or (not org-agenda-skip-archived-trees)
  10679. (and (eq action 'agenda) org-agenda-archives-mode))))
  10680. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10681. ;; select this headline
  10682. (cond
  10683. ((eq action 'sparse-tree)
  10684. (and org-highlight-sparse-tree-matches
  10685. (org-get-heading) (match-end 0)
  10686. (org-highlight-new-match
  10687. (match-beginning 0) (match-beginning 1)))
  10688. (org-show-context 'tags-tree))
  10689. ((eq action 'agenda)
  10690. (setq txt (org-format-agenda-item
  10691. ""
  10692. (concat
  10693. (if (eq org-tags-match-list-sublevels 'indented)
  10694. (make-string (1- level) ?.) "")
  10695. (org-get-heading))
  10696. category
  10697. tags-list
  10698. )
  10699. priority (org-get-priority txt))
  10700. (goto-char lspos)
  10701. (setq marker (org-agenda-new-marker))
  10702. (org-add-props txt props
  10703. 'org-marker marker 'org-hd-marker marker 'org-category category
  10704. 'todo-state todo
  10705. 'priority priority 'type "tagsmatch")
  10706. (push txt rtn))
  10707. ((functionp action)
  10708. (setq org-map-continue-from nil)
  10709. (save-excursion
  10710. (setq rtn1 (funcall action))
  10711. (push rtn1 rtn)))
  10712. (t (error "Invalid action")))
  10713. ;; if we are to skip sublevels, jump to end of subtree
  10714. (unless org-tags-match-list-sublevels
  10715. (org-end-of-subtree t)
  10716. (backward-char 1))))
  10717. ;; Get the correct position from where to continue
  10718. (if org-map-continue-from
  10719. (goto-char org-map-continue-from)
  10720. (and (= (point) lspos) (end-of-line 1)))))
  10721. (when (and (eq action 'sparse-tree)
  10722. (not org-sparse-tree-open-archived-trees))
  10723. (org-hide-archived-subtrees (point-min) (point-max)))
  10724. (nreverse rtn)))
  10725. (defun org-remove-uniherited-tags (tags)
  10726. "Remove all tags that are not inherited from the list TAGS."
  10727. (cond
  10728. ((eq org-use-tag-inheritance t)
  10729. (if org-tags-exclude-from-inheritance
  10730. (org-delete-all org-tags-exclude-from-inheritance tags)
  10731. tags))
  10732. ((not org-use-tag-inheritance) nil)
  10733. ((stringp org-use-tag-inheritance)
  10734. (delq nil (mapcar
  10735. (lambda (x)
  10736. (if (and (string-match org-use-tag-inheritance x)
  10737. (not (member x org-tags-exclude-from-inheritance)))
  10738. x nil))
  10739. tags)))
  10740. ((listp org-use-tag-inheritance)
  10741. (delq nil (mapcar
  10742. (lambda (x)
  10743. (if (member x org-use-tag-inheritance) x nil))
  10744. tags)))))
  10745. (defvar todo-only) ;; dynamically scoped
  10746. (defun org-match-sparse-tree (&optional todo-only match)
  10747. "Create a sparse tree according to tags string MATCH.
  10748. MATCH can contain positive and negative selection of tags, like
  10749. \"+WORK+URGENT-WITHBOSS\".
  10750. If optional argument TODO-ONLY is non-nil, only select lines that are
  10751. also TODO lines."
  10752. (interactive "P")
  10753. (org-prepare-agenda-buffers (list (current-buffer)))
  10754. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10755. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10756. (defvar org-cached-props nil)
  10757. (defun org-cached-entry-get (pom property)
  10758. (if (or (eq t org-use-property-inheritance)
  10759. (and (stringp org-use-property-inheritance)
  10760. (string-match org-use-property-inheritance property))
  10761. (and (listp org-use-property-inheritance)
  10762. (member property org-use-property-inheritance)))
  10763. ;; Caching is not possible, check it directly
  10764. (org-entry-get pom property 'inherit)
  10765. ;; Get all properties, so that we can do complicated checks easily
  10766. (cdr (assoc property (or org-cached-props
  10767. (setq org-cached-props
  10768. (org-entry-properties pom)))))))
  10769. (defun org-global-tags-completion-table (&optional files)
  10770. "Return the list of all tags in all agenda buffer/files."
  10771. (save-excursion
  10772. (org-uniquify
  10773. (delq nil
  10774. (apply 'append
  10775. (mapcar
  10776. (lambda (file)
  10777. (set-buffer (find-file-noselect file))
  10778. (append (org-get-buffer-tags)
  10779. (mapcar (lambda (x) (if (stringp (car-safe x))
  10780. (list (car-safe x)) nil))
  10781. org-tag-alist)))
  10782. (if (and files (car files))
  10783. files
  10784. (org-agenda-files))))))))
  10785. (defun org-make-tags-matcher (match)
  10786. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10787. ;; todo-only is scoped dynamically into this function, and the function
  10788. ;; may change it if the matcher asks for it.
  10789. (unless match
  10790. ;; Get a new match request, with completion
  10791. (let ((org-last-tags-completion-table
  10792. (org-global-tags-completion-table)))
  10793. (setq match (org-completing-read-no-i
  10794. "Match: " 'org-tags-completion-function nil nil nil
  10795. 'org-tags-history))))
  10796. ;; Parse the string and create a lisp form
  10797. (let ((match0 match)
  10798. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10799. minus tag mm
  10800. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10801. orterms term orlist re-p str-p level-p level-op time-p
  10802. prop-p pn pv po cat-p gv rest)
  10803. (if (string-match "/+" match)
  10804. ;; match contains also a todo-matching request
  10805. (progn
  10806. (setq tagsmatch (substring match 0 (match-beginning 0))
  10807. todomatch (substring match (match-end 0)))
  10808. (if (string-match "^!" todomatch)
  10809. (setq todo-only t todomatch (substring todomatch 1)))
  10810. (if (string-match "^\\s-*$" todomatch)
  10811. (setq todomatch nil)))
  10812. ;; only matching tags
  10813. (setq tagsmatch match todomatch nil))
  10814. ;; Make the tags matcher
  10815. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10816. (setq tagsmatcher t)
  10817. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10818. (while (setq term (pop orterms))
  10819. (while (and (equal (substring term -1) "\\") orterms)
  10820. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10821. (while (string-match re term)
  10822. (setq rest (substring term (match-end 0))
  10823. minus (and (match-end 1)
  10824. (equal (match-string 1 term) "-"))
  10825. tag (match-string 2 term)
  10826. re-p (equal (string-to-char tag) ?{)
  10827. level-p (match-end 4)
  10828. prop-p (match-end 5)
  10829. mm (cond
  10830. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10831. (level-p
  10832. (setq level-op (org-op-to-function (match-string 3 term)))
  10833. `(,level-op level ,(string-to-number
  10834. (match-string 4 term))))
  10835. (prop-p
  10836. (setq pn (match-string 5 term)
  10837. po (match-string 6 term)
  10838. pv (match-string 7 term)
  10839. cat-p (equal pn "CATEGORY")
  10840. re-p (equal (string-to-char pv) ?{)
  10841. str-p (equal (string-to-char pv) ?\")
  10842. time-p (save-match-data
  10843. (string-match "^\"[[<].*[]>]\"$" pv))
  10844. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10845. (if time-p (setq pv (org-matcher-time pv)))
  10846. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10847. (cond
  10848. ((equal pn "CATEGORY")
  10849. (setq gv '(get-text-property (point) 'org-category)))
  10850. ((equal pn "TODO")
  10851. (setq gv 'todo))
  10852. (t
  10853. (setq gv `(org-cached-entry-get nil ,pn))))
  10854. (if re-p
  10855. (if (eq po 'org<>)
  10856. `(not (string-match ,pv (or ,gv "")))
  10857. `(string-match ,pv (or ,gv "")))
  10858. (if str-p
  10859. `(,po (or ,gv "") ,pv)
  10860. `(,po (string-to-number (or ,gv ""))
  10861. ,(string-to-number pv) ))))
  10862. (t `(member ,tag tags-list)))
  10863. mm (if minus (list 'not mm) mm)
  10864. term rest)
  10865. (push mm tagsmatcher))
  10866. (push (if (> (length tagsmatcher) 1)
  10867. (cons 'and tagsmatcher)
  10868. (car tagsmatcher))
  10869. orlist)
  10870. (setq tagsmatcher nil))
  10871. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10872. (setq tagsmatcher
  10873. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10874. ;; Make the todo matcher
  10875. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10876. (setq todomatcher t)
  10877. (setq orterms (org-split-string todomatch "|") orlist nil)
  10878. (while (setq term (pop orterms))
  10879. (while (string-match re term)
  10880. (setq minus (and (match-end 1)
  10881. (equal (match-string 1 term) "-"))
  10882. kwd (match-string 2 term)
  10883. re-p (equal (string-to-char kwd) ?{)
  10884. term (substring term (match-end 0))
  10885. mm (if re-p
  10886. `(string-match ,(substring kwd 1 -1) todo)
  10887. (list 'equal 'todo kwd))
  10888. mm (if minus (list 'not mm) mm))
  10889. (push mm todomatcher))
  10890. (push (if (> (length todomatcher) 1)
  10891. (cons 'and todomatcher)
  10892. (car todomatcher))
  10893. orlist)
  10894. (setq todomatcher nil))
  10895. (setq todomatcher (if (> (length orlist) 1)
  10896. (cons 'or orlist) (car orlist))))
  10897. ;; Return the string and lisp forms of the matcher
  10898. (setq matcher (if todomatcher
  10899. (list 'and tagsmatcher todomatcher)
  10900. tagsmatcher))
  10901. (cons match0 matcher)))
  10902. (defun org-op-to-function (op &optional stringp)
  10903. "Turn an operator into the appropriate function."
  10904. (setq op
  10905. (cond
  10906. ((equal op "<" ) '(< string< org-time<))
  10907. ((equal op ">" ) '(> org-string> org-time>))
  10908. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10909. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10910. ((member op '("=" "==")) '(= string= org-time=))
  10911. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10912. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10913. (defun org<> (a b) (not (= a b)))
  10914. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10915. (defun org-string>= (a b) (not (string< a b)))
  10916. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10917. (defun org-string<> (a b) (not (string= a b)))
  10918. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10919. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10920. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10921. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10922. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10923. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10924. (defun org-2ft (s)
  10925. "Convert S to a floating point time.
  10926. If S is already a number, just return it. If it is a string, parse
  10927. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10928. (cond
  10929. ((numberp s) s)
  10930. ((stringp s)
  10931. (condition-case nil
  10932. (float-time (apply 'encode-time (org-parse-time-string s)))
  10933. (error 0.)))
  10934. (t 0.)))
  10935. (defun org-time-today ()
  10936. "Time in seconds today at 0:00.
  10937. Returns the float number of seconds since the beginning of the
  10938. epoch to the beginning of today (00:00)."
  10939. (float-time (apply 'encode-time
  10940. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10941. (defun org-matcher-time (s)
  10942. "Interpret a time comparison value."
  10943. (save-match-data
  10944. (cond
  10945. ((string= s "<now>") (float-time))
  10946. ((string= s "<today>") (org-time-today))
  10947. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10948. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10949. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10950. (+ (org-time-today)
  10951. (* (string-to-number (match-string 1 s))
  10952. (cdr (assoc (match-string 2 s)
  10953. '(("d" . 86400.0) ("w" . 604800.0)
  10954. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10955. (t (org-2ft s)))))
  10956. (defun org-match-any-p (re list)
  10957. "Does re match any element of list?"
  10958. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10959. (delq nil list))
  10960. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10961. (defvar org-tags-overlay (make-overlay 1 1))
  10962. (org-detach-overlay org-tags-overlay)
  10963. (defun org-get-local-tags-at (&optional pos)
  10964. "Get a list of tags defined in the current headline."
  10965. (org-get-tags-at pos 'local))
  10966. (defun org-get-local-tags ()
  10967. "Get a list of tags defined in the current headline."
  10968. (org-get-tags-at nil 'local))
  10969. (defun org-get-tags-at (&optional pos local)
  10970. "Get a list of all headline tags applicable at POS.
  10971. POS defaults to point. If tags are inherited, the list contains
  10972. the targets in the same sequence as the headlines appear, i.e.
  10973. the tags of the current headline come last.
  10974. When LOCAL is non-nil, only return tags from the current headline,
  10975. ignore inherited ones."
  10976. (interactive)
  10977. (if (and org-trust-scanner-tags
  10978. (or (not pos) (equal pos (point)))
  10979. (not local))
  10980. org-scanner-tags
  10981. (let (tags ltags lastpos parent)
  10982. (save-excursion
  10983. (save-restriction
  10984. (widen)
  10985. (goto-char (or pos (point)))
  10986. (save-match-data
  10987. (catch 'done
  10988. (condition-case nil
  10989. (progn
  10990. (org-back-to-heading t)
  10991. (while (not (equal lastpos (point)))
  10992. (setq lastpos (point))
  10993. (when (looking-at
  10994. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10995. (setq ltags (org-split-string
  10996. (org-match-string-no-properties 1) ":"))
  10997. (when parent
  10998. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10999. (setq tags (append
  11000. (if parent
  11001. (org-remove-uniherited-tags ltags)
  11002. ltags)
  11003. tags)))
  11004. (or org-use-tag-inheritance (throw 'done t))
  11005. (if local (throw 'done t))
  11006. (or (org-up-heading-safe) (error nil))
  11007. (setq parent t)))
  11008. (error nil)))))
  11009. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11010. (defun org-add-prop-inherited (s)
  11011. (add-text-properties 0 (length s) '(inherited t) s)
  11012. s)
  11013. (defun org-toggle-tag (tag &optional onoff)
  11014. "Toggle the tag TAG for the current line.
  11015. If ONOFF is `on' or `off', don't toggle but set to this state."
  11016. (let (res current)
  11017. (save-excursion
  11018. (org-back-to-heading t)
  11019. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11020. (point-at-eol) t)
  11021. (progn
  11022. (setq current (match-string 1))
  11023. (replace-match ""))
  11024. (setq current ""))
  11025. (setq current (nreverse (org-split-string current ":")))
  11026. (cond
  11027. ((eq onoff 'on)
  11028. (setq res t)
  11029. (or (member tag current) (push tag current)))
  11030. ((eq onoff 'off)
  11031. (or (not (member tag current)) (setq current (delete tag current))))
  11032. (t (if (member tag current)
  11033. (setq current (delete tag current))
  11034. (setq res t)
  11035. (push tag current))))
  11036. (end-of-line 1)
  11037. (if current
  11038. (progn
  11039. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11040. (org-set-tags nil t))
  11041. (delete-horizontal-space))
  11042. (run-hooks 'org-after-tags-change-hook))
  11043. res))
  11044. (defun org-align-tags-here (to-col)
  11045. ;; Assumes that this is a headline
  11046. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11047. (beginning-of-line 1)
  11048. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11049. (< pos (match-beginning 2)))
  11050. (progn
  11051. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11052. (goto-char (match-beginning 1))
  11053. (insert " ")
  11054. (delete-region (point) (1+ (match-beginning 2)))
  11055. (setq ncol (max (1+ (current-column))
  11056. (1+ col)
  11057. (if (> to-col 0)
  11058. to-col
  11059. (- (abs to-col) tags-l))))
  11060. (setq p (point))
  11061. (insert (make-string (- ncol (current-column)) ?\ ))
  11062. (setq ncol (current-column))
  11063. (when indent-tabs-mode (tabify p (point-at-eol)))
  11064. (org-move-to-column (min ncol col) t))
  11065. (goto-char pos))))
  11066. (defun org-set-tags-command (&optional arg just-align)
  11067. "Call the set-tags command for the current entry."
  11068. (interactive "P")
  11069. (if (org-on-heading-p)
  11070. (org-set-tags arg just-align)
  11071. (save-excursion
  11072. (org-back-to-heading t)
  11073. (org-set-tags arg just-align))))
  11074. (defun org-set-tags-to (data)
  11075. "Set the tags of the current entry to DATA, replacing the current tags.
  11076. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11077. If DATA is nil or the empty string, any tags will be removed."
  11078. (interactive "sTags: ")
  11079. (setq data
  11080. (cond
  11081. ((eq data nil) "")
  11082. ((equal data "") "")
  11083. ((stringp data)
  11084. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11085. ":"))
  11086. ((listp data)
  11087. (concat ":" (mapconcat 'identity data ":") ":"))
  11088. (t nil)))
  11089. (when data
  11090. (save-excursion
  11091. (org-back-to-heading t)
  11092. (when (looking-at org-complex-heading-regexp)
  11093. (if (match-end 5)
  11094. (progn
  11095. (goto-char (match-beginning 5))
  11096. (insert data)
  11097. (delete-region (point) (point-at-eol))
  11098. (org-set-tags nil 'align))
  11099. (goto-char (point-at-eol))
  11100. (insert " " data)
  11101. (org-set-tags nil 'align)))
  11102. (beginning-of-line 1)
  11103. (if (looking-at ".*?\\([ \t]+\\)$")
  11104. (delete-region (match-beginning 1) (match-end 1))))))
  11105. (defun org-align-all-tags ()
  11106. "Align the tags i all headings."
  11107. (interactive)
  11108. (save-excursion
  11109. (or (ignore-errors (org-back-to-heading t))
  11110. (outline-next-heading))
  11111. (if (org-on-heading-p)
  11112. (org-set-tags t)
  11113. (message "No headings"))))
  11114. (defun org-set-tags (&optional arg just-align)
  11115. "Set the tags for the current headline.
  11116. With prefix ARG, realign all tags in headings in the current buffer."
  11117. (interactive "P")
  11118. (let* ((re (concat "^" outline-regexp))
  11119. (current (org-get-tags-string))
  11120. (col (current-column))
  11121. (org-setting-tags t)
  11122. table current-tags inherited-tags ; computed below when needed
  11123. tags p0 c0 c1 rpl)
  11124. (if arg
  11125. (save-excursion
  11126. (goto-char (point-min))
  11127. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11128. (while (re-search-forward re nil t)
  11129. (org-set-tags nil t)
  11130. (end-of-line 1)))
  11131. (message "All tags realigned to column %d" org-tags-column))
  11132. (if just-align
  11133. (setq tags current)
  11134. ;; Get a new set of tags from the user
  11135. (save-excursion
  11136. (setq table (append org-tag-persistent-alist
  11137. (or org-tag-alist (org-get-buffer-tags))
  11138. (and org-complete-tags-always-offer-all-agenda-tags
  11139. (org-global-tags-completion-table (org-agenda-files))))
  11140. org-last-tags-completion-table table
  11141. current-tags (org-split-string current ":")
  11142. inherited-tags (nreverse
  11143. (nthcdr (length current-tags)
  11144. (nreverse (org-get-tags-at))))
  11145. tags
  11146. (if (or (eq t org-use-fast-tag-selection)
  11147. (and org-use-fast-tag-selection
  11148. (delq nil (mapcar 'cdr table))))
  11149. (org-fast-tag-selection
  11150. current-tags inherited-tags table
  11151. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11152. (let ((org-add-colon-after-tag-completion t))
  11153. (org-trim
  11154. (org-without-partial-completion
  11155. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11156. nil nil current 'org-tags-history)))))))
  11157. (while (string-match "[-+&]+" tags)
  11158. ;; No boolean logic, just a list
  11159. (setq tags (replace-match ":" t t tags))))
  11160. (if org-tags-sort-function
  11161. (setq tags (mapconcat 'identity
  11162. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11163. org-tags-sort-function) ":")))
  11164. (if (string-match "\\`[\t ]*\\'" tags)
  11165. (setq tags "")
  11166. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11167. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11168. ;; Insert new tags at the correct column
  11169. (beginning-of-line 1)
  11170. (cond
  11171. ((and (equal current "") (equal tags "")))
  11172. ((re-search-forward
  11173. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11174. (point-at-eol) t)
  11175. (if (equal tags "")
  11176. (setq rpl "")
  11177. (goto-char (match-beginning 0))
  11178. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11179. (1+ (point)) (point))
  11180. c1 (max (1+ c0) (if (> org-tags-column 0)
  11181. org-tags-column
  11182. (- (- org-tags-column) (length tags))))
  11183. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11184. (replace-match rpl t t)
  11185. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11186. tags)
  11187. (t (error "Tags alignment failed")))
  11188. (org-move-to-column col)
  11189. (unless just-align
  11190. (run-hooks 'org-after-tags-change-hook)))))
  11191. (defun org-change-tag-in-region (beg end tag off)
  11192. "Add or remove TAG for each entry in the region.
  11193. This works in the agenda, and also in an org-mode buffer."
  11194. (interactive
  11195. (list (region-beginning) (region-end)
  11196. (let ((org-last-tags-completion-table
  11197. (if (org-mode-p)
  11198. (org-get-buffer-tags)
  11199. (org-global-tags-completion-table))))
  11200. (org-icompleting-read
  11201. "Tag: " 'org-tags-completion-function nil nil nil
  11202. 'org-tags-history))
  11203. (progn
  11204. (message "[s]et or [r]emove? ")
  11205. (equal (read-char-exclusive) ?r))))
  11206. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11207. (let ((agendap (equal major-mode 'org-agenda-mode))
  11208. l1 l2 m buf pos newhead (cnt 0))
  11209. (goto-char end)
  11210. (setq l2 (1- (org-current-line)))
  11211. (goto-char beg)
  11212. (setq l1 (org-current-line))
  11213. (loop for l from l1 to l2 do
  11214. (org-goto-line l)
  11215. (setq m (get-text-property (point) 'org-hd-marker))
  11216. (when (or (and (org-mode-p) (org-on-heading-p))
  11217. (and agendap m))
  11218. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11219. pos (if agendap m (point)))
  11220. (with-current-buffer buf
  11221. (save-excursion
  11222. (save-restriction
  11223. (goto-char pos)
  11224. (setq cnt (1+ cnt))
  11225. (org-toggle-tag tag (if off 'off 'on))
  11226. (setq newhead (org-get-heading)))))
  11227. (and agendap (org-agenda-change-all-lines newhead m))))
  11228. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11229. (defun org-tags-completion-function (string predicate &optional flag)
  11230. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11231. (confirm (lambda (x) (stringp (car x)))))
  11232. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11233. (setq s1 (match-string 1 string)
  11234. s2 (match-string 2 string))
  11235. (setq s1 "" s2 string))
  11236. (cond
  11237. ((eq flag nil)
  11238. ;; try completion
  11239. (setq rtn (try-completion s2 ctable confirm))
  11240. (if (stringp rtn)
  11241. (setq rtn
  11242. (concat s1 s2 (substring rtn (length s2))
  11243. (if (and org-add-colon-after-tag-completion
  11244. (assoc rtn ctable))
  11245. ":" ""))))
  11246. rtn)
  11247. ((eq flag t)
  11248. ;; all-completions
  11249. (all-completions s2 ctable confirm)
  11250. )
  11251. ((eq flag 'lambda)
  11252. ;; exact match?
  11253. (assoc s2 ctable)))
  11254. ))
  11255. (defun org-fast-tag-insert (kwd tags face &optional end)
  11256. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11257. (insert (format "%-12s" (concat kwd ":"))
  11258. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11259. (or end "")))
  11260. (defun org-fast-tag-show-exit (flag)
  11261. (save-excursion
  11262. (org-goto-line 3)
  11263. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11264. (replace-match ""))
  11265. (when flag
  11266. (end-of-line 1)
  11267. (org-move-to-column (- (window-width) 19) t)
  11268. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11269. (defun org-set-current-tags-overlay (current prefix)
  11270. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11271. (if (featurep 'xemacs)
  11272. (org-overlay-display org-tags-overlay (concat prefix s)
  11273. 'secondary-selection)
  11274. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11275. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11276. (defvar org-last-tag-selection-key nil)
  11277. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11278. "Fast tag selection with single keys.
  11279. CURRENT is the current list of tags in the headline, INHERITED is the
  11280. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11281. possibly with grouping information. TODO-TABLE is a similar table with
  11282. TODO keywords, should these have keys assigned to them.
  11283. If the keys are nil, a-z are automatically assigned.
  11284. Returns the new tags string, or nil to not change the current settings."
  11285. (let* ((fulltable (append table todo-table))
  11286. (maxlen (apply 'max (mapcar
  11287. (lambda (x)
  11288. (if (stringp (car x)) (string-width (car x)) 0))
  11289. fulltable)))
  11290. (buf (current-buffer))
  11291. (expert (eq org-fast-tag-selection-single-key 'expert))
  11292. (buffer-tags nil)
  11293. (fwidth (+ maxlen 3 1 3))
  11294. (ncol (/ (- (window-width) 4) fwidth))
  11295. (i-face 'org-done)
  11296. (c-face 'org-todo)
  11297. tg cnt e c char c1 c2 ntable tbl rtn
  11298. ov-start ov-end ov-prefix
  11299. (exit-after-next org-fast-tag-selection-single-key)
  11300. (done-keywords org-done-keywords)
  11301. groups ingroup)
  11302. (save-excursion
  11303. (beginning-of-line 1)
  11304. (if (looking-at
  11305. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11306. (setq ov-start (match-beginning 1)
  11307. ov-end (match-end 1)
  11308. ov-prefix "")
  11309. (setq ov-start (1- (point-at-eol))
  11310. ov-end (1+ ov-start))
  11311. (skip-chars-forward "^\n\r")
  11312. (setq ov-prefix
  11313. (concat
  11314. (buffer-substring (1- (point)) (point))
  11315. (if (> (current-column) org-tags-column)
  11316. " "
  11317. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11318. (move-overlay org-tags-overlay ov-start ov-end)
  11319. (save-window-excursion
  11320. (if expert
  11321. (set-buffer (get-buffer-create " *Org tags*"))
  11322. (delete-other-windows)
  11323. (split-window-vertically)
  11324. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11325. (erase-buffer)
  11326. (org-set-local 'org-done-keywords done-keywords)
  11327. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11328. (org-fast-tag-insert "Current" current c-face "\n\n")
  11329. (org-fast-tag-show-exit exit-after-next)
  11330. (org-set-current-tags-overlay current ov-prefix)
  11331. (setq tbl fulltable char ?a cnt 0)
  11332. (while (setq e (pop tbl))
  11333. (cond
  11334. ((equal (car e) :startgroup)
  11335. (push '() groups) (setq ingroup t)
  11336. (when (not (= cnt 0))
  11337. (setq cnt 0)
  11338. (insert "\n"))
  11339. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11340. ((equal (car e) :endgroup)
  11341. (setq ingroup nil cnt 0)
  11342. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11343. ((equal e '(:newline))
  11344. (when (not (= cnt 0))
  11345. (setq cnt 0)
  11346. (insert "\n")
  11347. (setq e (car tbl))
  11348. (while (equal (car tbl) '(:newline))
  11349. (insert "\n")
  11350. (setq tbl (cdr tbl)))))
  11351. (t
  11352. (setq tg (copy-sequence (car e)) c2 nil)
  11353. (if (cdr e)
  11354. (setq c (cdr e))
  11355. ;; automatically assign a character.
  11356. (setq c1 (string-to-char
  11357. (downcase (substring
  11358. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11359. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11360. (while (or (rassoc char ntable) (rassoc char table))
  11361. (setq char (1+ char)))
  11362. (setq c2 c1))
  11363. (setq c (or c2 char)))
  11364. (if ingroup (push tg (car groups)))
  11365. (setq tg (org-add-props tg nil 'face
  11366. (cond
  11367. ((not (assoc tg table))
  11368. (org-get-todo-face tg))
  11369. ((member tg current) c-face)
  11370. ((member tg inherited) i-face)
  11371. (t nil))))
  11372. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11373. (insert "[" c "] " tg (make-string
  11374. (- fwidth 4 (length tg)) ?\ ))
  11375. (push (cons tg c) ntable)
  11376. (when (= (setq cnt (1+ cnt)) ncol)
  11377. (insert "\n")
  11378. (if ingroup (insert " "))
  11379. (setq cnt 0)))))
  11380. (setq ntable (nreverse ntable))
  11381. (insert "\n")
  11382. (goto-char (point-min))
  11383. (if (not expert) (org-fit-window-to-buffer))
  11384. (setq rtn
  11385. (catch 'exit
  11386. (while t
  11387. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11388. (if (not groups) "no " "")
  11389. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11390. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11391. (setq org-last-tag-selection-key c)
  11392. (cond
  11393. ((= c ?\r) (throw 'exit t))
  11394. ((= c ?!)
  11395. (setq groups (not groups))
  11396. (goto-char (point-min))
  11397. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11398. ((= c ?\C-c)
  11399. (if (not expert)
  11400. (org-fast-tag-show-exit
  11401. (setq exit-after-next (not exit-after-next)))
  11402. (setq expert nil)
  11403. (delete-other-windows)
  11404. (split-window-vertically)
  11405. (org-switch-to-buffer-other-window " *Org tags*")
  11406. (org-fit-window-to-buffer)))
  11407. ((or (= c ?\C-g)
  11408. (and (= c ?q) (not (rassoc c ntable))))
  11409. (org-detach-overlay org-tags-overlay)
  11410. (setq quit-flag t))
  11411. ((= c ?\ )
  11412. (setq current nil)
  11413. (if exit-after-next (setq exit-after-next 'now)))
  11414. ((= c ?\t)
  11415. (condition-case nil
  11416. (setq tg (org-icompleting-read
  11417. "Tag: "
  11418. (or buffer-tags
  11419. (with-current-buffer buf
  11420. (org-get-buffer-tags)))))
  11421. (quit (setq tg "")))
  11422. (when (string-match "\\S-" tg)
  11423. (add-to-list 'buffer-tags (list tg))
  11424. (if (member tg current)
  11425. (setq current (delete tg current))
  11426. (push tg current)))
  11427. (if exit-after-next (setq exit-after-next 'now)))
  11428. ((setq e (rassoc c todo-table) tg (car e))
  11429. (with-current-buffer buf
  11430. (save-excursion (org-todo tg)))
  11431. (if exit-after-next (setq exit-after-next 'now)))
  11432. ((setq e (rassoc c ntable) tg (car e))
  11433. (if (member tg current)
  11434. (setq current (delete tg current))
  11435. (loop for g in groups do
  11436. (if (member tg g)
  11437. (mapc (lambda (x)
  11438. (setq current (delete x current)))
  11439. g)))
  11440. (push tg current))
  11441. (if exit-after-next (setq exit-after-next 'now))))
  11442. ;; Create a sorted list
  11443. (setq current
  11444. (sort current
  11445. (lambda (a b)
  11446. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11447. (if (eq exit-after-next 'now) (throw 'exit t))
  11448. (goto-char (point-min))
  11449. (beginning-of-line 2)
  11450. (delete-region (point) (point-at-eol))
  11451. (org-fast-tag-insert "Current" current c-face)
  11452. (org-set-current-tags-overlay current ov-prefix)
  11453. (while (re-search-forward
  11454. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11455. (setq tg (match-string 1))
  11456. (add-text-properties
  11457. (match-beginning 1) (match-end 1)
  11458. (list 'face
  11459. (cond
  11460. ((member tg current) c-face)
  11461. ((member tg inherited) i-face)
  11462. (t (get-text-property (match-beginning 1) 'face))))))
  11463. (goto-char (point-min)))))
  11464. (org-detach-overlay org-tags-overlay)
  11465. (if rtn
  11466. (mapconcat 'identity current ":")
  11467. nil))))
  11468. (defun org-get-tags-string ()
  11469. "Get the TAGS string in the current headline."
  11470. (unless (org-on-heading-p t)
  11471. (error "Not on a heading"))
  11472. (save-excursion
  11473. (beginning-of-line 1)
  11474. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11475. (org-match-string-no-properties 1)
  11476. "")))
  11477. (defun org-get-tags ()
  11478. "Get the list of tags specified in the current headline."
  11479. (org-split-string (org-get-tags-string) ":"))
  11480. (defun org-get-buffer-tags ()
  11481. "Get a table of all tags used in the buffer, for completion."
  11482. (let (tags)
  11483. (save-excursion
  11484. (goto-char (point-min))
  11485. (while (re-search-forward
  11486. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11487. (when (equal (char-after (point-at-bol 0)) ?*)
  11488. (mapc (lambda (x) (add-to-list 'tags x))
  11489. (org-split-string (org-match-string-no-properties 1) ":")))))
  11490. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11491. (mapcar 'list tags)))
  11492. ;;;; The mapping API
  11493. ;;;###autoload
  11494. (defun org-map-entries (func &optional match scope &rest skip)
  11495. "Call FUNC at each headline selected by MATCH in SCOPE.
  11496. FUNC is a function or a lisp form. The function will be called without
  11497. arguments, with the cursor positioned at the beginning of the headline.
  11498. The return values of all calls to the function will be collected and
  11499. returned as a list.
  11500. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11501. does not need to preserve point. After evaluation, the cursor will be
  11502. moved to the end of the line (presumably of the headline of the
  11503. processed entry) and search continues from there. Under some
  11504. circumstances, this may not produce the wanted results. For example,
  11505. if you have removed (e.g. archived) the current (sub)tree it could
  11506. mean that the next entry will be skipped entirely. In such cases, you
  11507. can specify the position from where search should continue by making
  11508. FUNC set the variable `org-map-continue-from' to the desired buffer
  11509. position.
  11510. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11511. Only headlines that are matched by this query will be considered during
  11512. the iteration. When MATCH is nil or t, all headlines will be
  11513. visited by the iteration.
  11514. SCOPE determines the scope of this command. It can be any of:
  11515. nil The current buffer, respecting the restriction if any
  11516. tree The subtree started with the entry at point
  11517. file The current buffer, without restriction
  11518. file-with-archives
  11519. The current buffer, and any archives associated with it
  11520. agenda All agenda files
  11521. agenda-with-archives
  11522. All agenda files with any archive files associated with them
  11523. \(file1 file2 ...)
  11524. If this is a list, all files in the list will be scanned
  11525. The remaining args are treated as settings for the skipping facilities of
  11526. the scanner. The following items can be given here:
  11527. archive skip trees with the archive tag.
  11528. comment skip trees with the COMMENT keyword
  11529. function or Emacs Lisp form:
  11530. will be used as value for `org-agenda-skip-function', so whenever
  11531. the function returns t, FUNC will not be called for that
  11532. entry and search will continue from the point where the
  11533. function leaves it.
  11534. If your function needs to retrieve the tags including inherited tags
  11535. at the *current* entry, you can use the value of the variable
  11536. `org-scanner-tags' which will be much faster than getting the value
  11537. with `org-get-tags-at'. If your function gets properties with
  11538. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11539. to t around the call to `org-entry-properties' to get the same speedup.
  11540. Note that if your function moves around to retrieve tags and properties at
  11541. a *different* entry, you cannot use these techniques."
  11542. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11543. (org-agenda-skip-archived-trees (memq 'archive skip))
  11544. (org-agenda-skip-comment-trees (memq 'comment skip))
  11545. (org-agenda-skip-function
  11546. (car (org-delete-all '(comment archive) skip)))
  11547. (org-tags-match-list-sublevels t)
  11548. matcher file res
  11549. org-todo-keywords-for-agenda
  11550. org-done-keywords-for-agenda
  11551. org-todo-keyword-alist-for-agenda
  11552. org-drawers-for-agenda
  11553. org-tag-alist-for-agenda)
  11554. (cond
  11555. ((eq match t) (setq matcher t))
  11556. ((eq match nil) (setq matcher t))
  11557. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11558. (save-excursion
  11559. (save-restriction
  11560. (when (eq scope 'tree)
  11561. (org-back-to-heading t)
  11562. (org-narrow-to-subtree)
  11563. (setq scope nil))
  11564. (if (not scope)
  11565. (progn
  11566. (org-prepare-agenda-buffers
  11567. (list (buffer-file-name (current-buffer))))
  11568. (setq res (org-scan-tags func matcher)))
  11569. ;; Get the right scope
  11570. (cond
  11571. ((and scope (listp scope) (symbolp (car scope)))
  11572. (setq scope (eval scope)))
  11573. ((eq scope 'agenda)
  11574. (setq scope (org-agenda-files t)))
  11575. ((eq scope 'agenda-with-archives)
  11576. (setq scope (org-agenda-files t))
  11577. (setq scope (org-add-archive-files scope)))
  11578. ((eq scope 'file)
  11579. (setq scope (list (buffer-file-name))))
  11580. ((eq scope 'file-with-archives)
  11581. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11582. (org-prepare-agenda-buffers scope)
  11583. (while (setq file (pop scope))
  11584. (with-current-buffer (org-find-base-buffer-visiting file)
  11585. (save-excursion
  11586. (save-restriction
  11587. (widen)
  11588. (goto-char (point-min))
  11589. (setq res (append res (org-scan-tags func matcher))))))))))
  11590. res))
  11591. ;;;; Properties
  11592. ;;; Setting and retrieving properties
  11593. (defconst org-special-properties
  11594. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11595. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11596. "The special properties valid in Org-mode.
  11597. These are properties that are not defined in the property drawer,
  11598. but in some other way.")
  11599. (defconst org-default-properties
  11600. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11601. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11602. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11603. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11604. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11605. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11606. "Some properties that are used by Org-mode for various purposes.
  11607. Being in this list makes sure that they are offered for completion.")
  11608. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11609. "Regular expression matching the first line of a property drawer.")
  11610. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11611. "Regular expression matching the last line of a property drawer.")
  11612. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11613. "Regular expression matching the first line of a property drawer.")
  11614. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11615. "Regular expression matching the first line of a property drawer.")
  11616. (defconst org-property-drawer-re
  11617. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11618. org-property-end-re "\\)\n?")
  11619. "Matches an entire property drawer.")
  11620. (defconst org-clock-drawer-re
  11621. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11622. org-property-end-re "\\)\n?")
  11623. "Matches an entire clock drawer.")
  11624. (defun org-property-action ()
  11625. "Do an action on properties."
  11626. (interactive)
  11627. (let (c)
  11628. (org-at-property-p)
  11629. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11630. (setq c (read-char-exclusive))
  11631. (cond
  11632. ((equal c ?s)
  11633. (call-interactively 'org-set-property))
  11634. ((equal c ?d)
  11635. (call-interactively 'org-delete-property))
  11636. ((equal c ?D)
  11637. (call-interactively 'org-delete-property-globally))
  11638. ((equal c ?c)
  11639. (call-interactively 'org-compute-property-at-point))
  11640. (t (error "No such property action %c" c)))))
  11641. (defun org-set-effort (&optional value)
  11642. "Set the effort property of the current entry.
  11643. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11644. allowed value."
  11645. (interactive "P")
  11646. (if (equal value 0) (setq value 10))
  11647. (let* ((completion-ignore-case t)
  11648. (prop org-effort-property)
  11649. (cur (org-entry-get nil prop))
  11650. (allowed (org-property-get-allowed-values nil prop 'table))
  11651. (existing (mapcar 'list (org-property-values prop)))
  11652. rpl
  11653. (val (cond
  11654. ((stringp value) value)
  11655. ((and allowed (integerp value))
  11656. (or (car (nth (1- value) allowed))
  11657. (car (org-last allowed))))
  11658. (allowed
  11659. (message "Select 1-9,0, [RET%s]: %s"
  11660. (if cur (concat "=" cur) "")
  11661. (mapconcat 'car allowed " "))
  11662. (setq rpl (read-char-exclusive))
  11663. (if (equal rpl ?\r)
  11664. cur
  11665. (setq rpl (- rpl ?0))
  11666. (if (equal rpl 0) (setq rpl 10))
  11667. (if (and (> rpl 0) (<= rpl (length allowed)))
  11668. (car (nth (1- rpl) allowed))
  11669. (org-completing-read "Effort: " allowed nil))))
  11670. (t
  11671. (let (org-completion-use-ido org-completion-use-iswitchb)
  11672. (org-completing-read
  11673. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11674. (concat "[" cur "]") "")
  11675. ": ")
  11676. existing nil nil "" nil cur))))))
  11677. (unless (equal (org-entry-get nil prop) val)
  11678. (org-entry-put nil prop val))
  11679. (message "%s is now %s" prop val)))
  11680. (defun org-at-property-p ()
  11681. "Is cursor inside a property drawer?"
  11682. (save-excursion
  11683. (beginning-of-line 1)
  11684. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11685. (let ((match (match-data)) ;; Keep match-data for use by calling
  11686. (p (point)) ;; procedures.
  11687. (range (unless (org-before-first-heading-p)
  11688. (org-get-property-block))))
  11689. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11690. (set-match-data match))))))
  11691. (defun org-get-property-block (&optional beg end force)
  11692. "Return the (beg . end) range of the body of the property drawer.
  11693. BEG and END can be beginning and end of subtree, if not given
  11694. they will be found.
  11695. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11696. (catch 'exit
  11697. (save-excursion
  11698. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11699. (end (or end (progn (outline-next-heading) (point)))))
  11700. (goto-char beg)
  11701. (if (re-search-forward org-property-start-re end t)
  11702. (setq beg (1+ (match-end 0)))
  11703. (if force
  11704. (save-excursion
  11705. (org-insert-property-drawer)
  11706. (setq end (progn (outline-next-heading) (point))))
  11707. (throw 'exit nil))
  11708. (goto-char beg)
  11709. (if (re-search-forward org-property-start-re end t)
  11710. (setq beg (1+ (match-end 0)))))
  11711. (if (re-search-forward org-property-end-re end t)
  11712. (setq end (match-beginning 0))
  11713. (or force (throw 'exit nil))
  11714. (goto-char beg)
  11715. (setq end beg)
  11716. (org-indent-line-function)
  11717. (insert ":END:\n"))
  11718. (cons beg end)))))
  11719. (defun org-entry-properties (&optional pom which specific)
  11720. "Get all properties of the entry at point-or-marker POM.
  11721. This includes the TODO keyword, the tags, time strings for deadline,
  11722. scheduled, and clocking, and any additional properties defined in the
  11723. entry. The return value is an alist, keys may occur multiple times
  11724. if the property key was used several times.
  11725. POM may also be nil, in which case the current entry is used.
  11726. If WHICH is nil or `all', get all properties. If WHICH is
  11727. `special' or `standard', only get that subclass. If WHICH
  11728. is a string only get exactly this property. Specific can be a string, the
  11729. specific property we are interested in. Specifying it can speed
  11730. things up because then unnecessary parsing is avoided."
  11731. (setq which (or which 'all))
  11732. (org-with-point-at pom
  11733. (let ((clockstr (substring org-clock-string 0 -1))
  11734. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11735. (case-fold-search nil)
  11736. beg end range props sum-props key value string clocksum)
  11737. (save-excursion
  11738. (when (condition-case nil
  11739. (and (org-mode-p) (org-back-to-heading t))
  11740. (error nil))
  11741. (setq beg (point))
  11742. (setq sum-props (get-text-property (point) 'org-summaries))
  11743. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11744. (outline-next-heading)
  11745. (setq end (point))
  11746. (when (memq which '(all special))
  11747. ;; Get the special properties, like TODO and tags
  11748. (goto-char beg)
  11749. (when (and (or (not specific) (string= specific "TODO"))
  11750. (looking-at org-todo-line-regexp) (match-end 2))
  11751. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11752. (when (and (or (not specific) (string= specific "PRIORITY"))
  11753. (looking-at org-priority-regexp))
  11754. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11755. (when (and (or (not specific) (string= specific "TAGS"))
  11756. (setq value (org-get-tags-string))
  11757. (string-match "\\S-" value))
  11758. (push (cons "TAGS" value) props))
  11759. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11760. (setq value (org-get-tags-at)))
  11761. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11762. ":"))
  11763. props))
  11764. (when (or (not specific) (string= specific "BLOCKED"))
  11765. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11766. (when (or (not specific)
  11767. (member specific org-all-time-keywords)
  11768. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11769. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11770. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11771. string (if (equal key clockstr)
  11772. (org-no-properties
  11773. (org-trim
  11774. (buffer-substring
  11775. (match-beginning 3) (goto-char (point-at-eol)))))
  11776. (substring (org-match-string-no-properties 3) 1 -1)))
  11777. (unless key
  11778. (if (= (char-after (match-beginning 3)) ?\[)
  11779. (setq key "TIMESTAMP_IA")
  11780. (setq key "TIMESTAMP")))
  11781. (when (or (equal key clockstr) (not (assoc key props)))
  11782. (push (cons key string) props))))
  11783. )
  11784. (when (memq which '(all standard))
  11785. ;; Get the standard properties, like :PROP: ...
  11786. (setq range (org-get-property-block beg end))
  11787. (when range
  11788. (goto-char (car range))
  11789. (while (re-search-forward
  11790. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11791. (cdr range) t)
  11792. (setq key (org-match-string-no-properties 1)
  11793. value (org-trim (or (org-match-string-no-properties 2) "")))
  11794. (unless (member key excluded)
  11795. (push (cons key (or value "")) props)))))
  11796. (if clocksum
  11797. (push (cons "CLOCKSUM"
  11798. (org-columns-number-to-string (/ (float clocksum) 60.)
  11799. 'add_times))
  11800. props))
  11801. (unless (assoc "CATEGORY" props)
  11802. (setq value (or (org-get-category)
  11803. (progn (org-refresh-category-properties)
  11804. (org-get-category))))
  11805. (push (cons "CATEGORY" value) props))
  11806. (append sum-props (nreverse props)))))))
  11807. (defun org-entry-get (pom property &optional inherit)
  11808. "Get value of PROPERTY for entry at point-or-marker POM.
  11809. If INHERIT is non-nil and the entry does not have the property,
  11810. then also check higher levels of the hierarchy.
  11811. If INHERIT is the symbol `selective', use inheritance only if the setting
  11812. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11813. If the property is present but empty, the return value is the empty string.
  11814. If the property is not present at all, nil is returned."
  11815. (org-with-point-at pom
  11816. (if (and inherit (if (eq inherit 'selective)
  11817. (org-property-inherit-p property)
  11818. t))
  11819. (org-entry-get-with-inheritance property)
  11820. (if (member property org-special-properties)
  11821. ;; We need a special property. Use `org-entry-properties' to
  11822. ;; retrieve it, but specify the wanted property
  11823. (cdr (assoc property (org-entry-properties nil 'special property)))
  11824. (let ((range (org-get-property-block)))
  11825. (if (and range
  11826. (goto-char (car range))
  11827. (re-search-forward
  11828. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11829. (cdr range) t))
  11830. ;; Found the property, return it.
  11831. (if (match-end 1)
  11832. (org-match-string-no-properties 1)
  11833. "")))))))
  11834. (defun org-property-or-variable-value (var &optional inherit)
  11835. "Check if there is a property fixing the value of VAR.
  11836. If yes, return this value. If not, return the current value of the variable."
  11837. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11838. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11839. (read prop)
  11840. (symbol-value var))))
  11841. (defun org-entry-delete (pom property)
  11842. "Delete the property PROPERTY from entry at point-or-marker POM."
  11843. (org-with-point-at pom
  11844. (if (member property org-special-properties)
  11845. nil ; cannot delete these properties.
  11846. (let ((range (org-get-property-block)))
  11847. (if (and range
  11848. (goto-char (car range))
  11849. (re-search-forward
  11850. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11851. (cdr range) t))
  11852. (progn
  11853. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11854. t)
  11855. nil)))))
  11856. ;; Multi-values properties are properties that contain multiple values
  11857. ;; These values are assumed to be single words, separated by whitespace.
  11858. (defun org-entry-add-to-multivalued-property (pom property value)
  11859. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11860. (let* ((old (org-entry-get pom property))
  11861. (values (and old (org-split-string old "[ \t]"))))
  11862. (setq value (org-entry-protect-space value))
  11863. (unless (member value values)
  11864. (setq values (cons value values))
  11865. (org-entry-put pom property
  11866. (mapconcat 'identity values " ")))))
  11867. (defun org-entry-remove-from-multivalued-property (pom property value)
  11868. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11869. (let* ((old (org-entry-get pom property))
  11870. (values (and old (org-split-string old "[ \t]"))))
  11871. (setq value (org-entry-protect-space value))
  11872. (when (member value values)
  11873. (setq values (delete value values))
  11874. (org-entry-put pom property
  11875. (mapconcat 'identity values " ")))))
  11876. (defun org-entry-member-in-multivalued-property (pom property value)
  11877. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11878. (let* ((old (org-entry-get pom property))
  11879. (values (and old (org-split-string old "[ \t]"))))
  11880. (setq value (org-entry-protect-space value))
  11881. (member value values)))
  11882. (defun org-entry-get-multivalued-property (pom property)
  11883. "Return a list of values in a multivalued property."
  11884. (let* ((value (org-entry-get pom property))
  11885. (values (and value (org-split-string value "[ \t]"))))
  11886. (mapcar 'org-entry-restore-space values)))
  11887. (defun org-entry-put-multivalued-property (pom property &rest values)
  11888. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11889. VALUES should be a list of strings. Spaces will be protected."
  11890. (org-entry-put pom property
  11891. (mapconcat 'org-entry-protect-space values " "))
  11892. (let* ((value (org-entry-get pom property))
  11893. (values (and value (org-split-string value "[ \t]"))))
  11894. (mapcar 'org-entry-restore-space values)))
  11895. (defun org-entry-protect-space (s)
  11896. "Protect spaces and newline in string S."
  11897. (while (string-match " " s)
  11898. (setq s (replace-match "%20" t t s)))
  11899. (while (string-match "\n" s)
  11900. (setq s (replace-match "%0A" t t s)))
  11901. s)
  11902. (defun org-entry-restore-space (s)
  11903. "Restore spaces and newline in string S."
  11904. (while (string-match "%20" s)
  11905. (setq s (replace-match " " t t s)))
  11906. (while (string-match "%0A" s)
  11907. (setq s (replace-match "\n" t t s)))
  11908. s)
  11909. (defvar org-entry-property-inherited-from (make-marker)
  11910. "Marker pointing to the entry from where a property was inherited.
  11911. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11912. location of the entry where the inheritance search matched. If there was
  11913. no match, the marker will point nowhere.
  11914. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11915. is set.")
  11916. (defun org-entry-get-with-inheritance (property)
  11917. "Get entry property, and search higher levels if not present."
  11918. (move-marker org-entry-property-inherited-from nil)
  11919. (let (tmp)
  11920. (save-excursion
  11921. (save-restriction
  11922. (widen)
  11923. (catch 'ex
  11924. (while t
  11925. (when (setq tmp (org-entry-get nil property))
  11926. (org-back-to-heading t)
  11927. (move-marker org-entry-property-inherited-from (point))
  11928. (throw 'ex tmp))
  11929. (or (org-up-heading-safe) (throw 'ex nil)))))
  11930. (or tmp
  11931. (cdr (assoc property org-file-properties))
  11932. (cdr (assoc property org-global-properties))
  11933. (cdr (assoc property org-global-properties-fixed))))))
  11934. (defvar org-property-changed-functions nil
  11935. "Hook called when the value of a property has changed.
  11936. Each hook function should accept two arguments, the name of the property
  11937. and the new value.")
  11938. (defun org-entry-put (pom property value)
  11939. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11940. (org-with-point-at pom
  11941. (org-back-to-heading t)
  11942. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11943. range)
  11944. (cond
  11945. ((equal property "TODO")
  11946. (when (and (stringp value) (string-match "\\S-" value)
  11947. (not (member value org-todo-keywords-1)))
  11948. (error "\"%s\" is not a valid TODO state" value))
  11949. (if (or (not value)
  11950. (not (string-match "\\S-" value)))
  11951. (setq value 'none))
  11952. (org-todo value)
  11953. (org-set-tags nil 'align))
  11954. ((equal property "PRIORITY")
  11955. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11956. (string-to-char value) ?\ ))
  11957. (org-set-tags nil 'align))
  11958. ((equal property "SCHEDULED")
  11959. (if (re-search-forward org-scheduled-time-regexp end t)
  11960. (cond
  11961. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11962. ((eq value 'later) (org-timestamp-change 1 'day))
  11963. (t (call-interactively 'org-schedule)))
  11964. (call-interactively 'org-schedule)))
  11965. ((equal property "DEADLINE")
  11966. (if (re-search-forward org-deadline-time-regexp end t)
  11967. (cond
  11968. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11969. ((eq value 'later) (org-timestamp-change 1 'day))
  11970. (t (call-interactively 'org-deadline)))
  11971. (call-interactively 'org-deadline)))
  11972. ((member property org-special-properties)
  11973. (error "The %s property can not yet be set with `org-entry-put'"
  11974. property))
  11975. (t ; a non-special property
  11976. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11977. (setq range (org-get-property-block beg end 'force))
  11978. (goto-char (car range))
  11979. (if (re-search-forward
  11980. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11981. (progn
  11982. (delete-region (match-beginning 1) (match-end 1))
  11983. (goto-char (match-beginning 1)))
  11984. (goto-char (cdr range))
  11985. (insert "\n")
  11986. (backward-char 1)
  11987. (org-indent-line-function)
  11988. (insert ":" property ":"))
  11989. (and value (insert " " value))
  11990. (org-indent-line-function)))))
  11991. (run-hook-with-args 'org-property-changed-functions property value)))
  11992. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11993. "Get all property keys in the current buffer.
  11994. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11995. like tags and TODO state.
  11996. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11997. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11998. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11999. formats in the current buffer."
  12000. (let (rtn range cfmt s p)
  12001. (save-excursion
  12002. (save-restriction
  12003. (widen)
  12004. (goto-char (point-min))
  12005. (while (re-search-forward org-property-start-re nil t)
  12006. (setq range (org-get-property-block))
  12007. (goto-char (car range))
  12008. (while (re-search-forward
  12009. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12010. (cdr range) t)
  12011. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12012. (outline-next-heading))))
  12013. (when include-specials
  12014. (setq rtn (append org-special-properties rtn)))
  12015. (when include-defaults
  12016. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12017. (add-to-list 'rtn org-effort-property))
  12018. (when include-columns
  12019. (save-excursion
  12020. (save-restriction
  12021. (widen)
  12022. (goto-char (point-min))
  12023. (while (re-search-forward
  12024. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12025. nil t)
  12026. (setq cfmt (match-string 2) s 0)
  12027. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12028. cfmt s)
  12029. (setq s (match-end 0)
  12030. p (match-string 1 cfmt))
  12031. (unless (or (equal p "ITEM")
  12032. (member p org-special-properties))
  12033. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12034. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12035. (defun org-property-values (key)
  12036. "Return a list of all values of property KEY."
  12037. (save-excursion
  12038. (save-restriction
  12039. (widen)
  12040. (goto-char (point-min))
  12041. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12042. values)
  12043. (while (re-search-forward re nil t)
  12044. (add-to-list 'values (org-trim (match-string 1))))
  12045. (delete "" values)))))
  12046. (defun org-insert-property-drawer ()
  12047. "Insert a property drawer into the current entry."
  12048. (interactive)
  12049. (org-back-to-heading t)
  12050. (looking-at outline-regexp)
  12051. (let ((indent (if org-adapt-indentation
  12052. (- (match-end 0)(match-beginning 0))
  12053. 0))
  12054. (beg (point))
  12055. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12056. end hiddenp)
  12057. (outline-next-heading)
  12058. (setq end (point))
  12059. (goto-char beg)
  12060. (while (re-search-forward re end t))
  12061. (setq hiddenp (org-invisible-p))
  12062. (end-of-line 1)
  12063. (and (equal (char-after) ?\n) (forward-char 1))
  12064. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12065. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12066. ;; just skip this line
  12067. (beginning-of-line 2)
  12068. ;; Drawer start, find the end
  12069. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12070. (beginning-of-line 1)))
  12071. (org-skip-over-state-notes)
  12072. (skip-chars-backward " \t\n\r")
  12073. (if (eq (char-before) ?*) (forward-char 1))
  12074. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12075. (beginning-of-line 0)
  12076. (org-indent-to-column indent)
  12077. (beginning-of-line 2)
  12078. (org-indent-to-column indent)
  12079. (beginning-of-line 0)
  12080. (if hiddenp
  12081. (save-excursion
  12082. (org-back-to-heading t)
  12083. (hide-entry))
  12084. (org-flag-drawer t))))
  12085. (defun org-set-property (property value)
  12086. "In the current entry, set PROPERTY to VALUE.
  12087. When called interactively, this will prompt for a property name, offering
  12088. completion on existing and default properties. And then it will prompt
  12089. for a value, offering completion either on allowed values (via an inherited
  12090. xxx_ALL property) or on existing values in other instances of this property
  12091. in the current file."
  12092. (interactive
  12093. (let* ((completion-ignore-case t)
  12094. (keys (org-buffer-property-keys nil t t))
  12095. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12096. (prop (if (member prop0 keys)
  12097. prop0
  12098. (or (cdr (assoc (downcase prop0)
  12099. (mapcar (lambda (x) (cons (downcase x) x))
  12100. keys)))
  12101. prop0)))
  12102. (cur (org-entry-get nil prop))
  12103. (prompt (concat prop " value"
  12104. (if (and cur (string-match "\\S-" cur))
  12105. (concat " [" cur "]") "") ": "))
  12106. (allowed (org-property-get-allowed-values nil prop 'table))
  12107. (existing (mapcar 'list (org-property-values prop)))
  12108. (val (if allowed
  12109. (org-completing-read prompt allowed nil
  12110. (not (get-text-property 0 'org-unrestricted
  12111. (caar allowed))))
  12112. (let (org-completion-use-ido org-completion-use-iswitchb)
  12113. (org-completing-read prompt existing nil nil "" nil cur)))))
  12114. (list prop (if (equal val "") cur val))))
  12115. (unless (equal (org-entry-get nil property) value)
  12116. (org-entry-put nil property value)))
  12117. (defun org-delete-property (property)
  12118. "In the current entry, delete PROPERTY."
  12119. (interactive
  12120. (let* ((completion-ignore-case t)
  12121. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12122. (list prop)))
  12123. (message "Property %s %s" property
  12124. (if (org-entry-delete nil property)
  12125. "deleted"
  12126. "was not present in the entry")))
  12127. (defun org-delete-property-globally (property)
  12128. "Remove PROPERTY globally, from all entries."
  12129. (interactive
  12130. (let* ((completion-ignore-case t)
  12131. (prop (org-icompleting-read
  12132. "Globally remove property: "
  12133. (mapcar 'list (org-buffer-property-keys)))))
  12134. (list prop)))
  12135. (save-excursion
  12136. (save-restriction
  12137. (widen)
  12138. (goto-char (point-min))
  12139. (let ((cnt 0))
  12140. (while (re-search-forward
  12141. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12142. nil t)
  12143. (setq cnt (1+ cnt))
  12144. (replace-match ""))
  12145. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12146. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12147. (defun org-compute-property-at-point ()
  12148. "Compute the property at point.
  12149. This looks for an enclosing column format, extracts the operator and
  12150. then applies it to the property in the column format's scope."
  12151. (interactive)
  12152. (unless (org-at-property-p)
  12153. (error "Not at a property"))
  12154. (let ((prop (org-match-string-no-properties 2)))
  12155. (org-columns-get-format-and-top-level)
  12156. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12157. (error "No operator defined for property %s" prop))
  12158. (org-columns-compute prop)))
  12159. (defvar org-property-allowed-value-functions nil
  12160. "Hook for functions supplying allowed values for a specific property.
  12161. The functions must take a single argument, the name of the property, and
  12162. return a flat list of allowed values. If \":ETC\" is one of
  12163. the values, this means that these values are intended as defaults for
  12164. completion, but that other values should be allowed too.
  12165. The functions must return nil if they are not responsible for this
  12166. property.")
  12167. (defun org-property-get-allowed-values (pom property &optional table)
  12168. "Get allowed values for the property PROPERTY.
  12169. When TABLE is non-nil, return an alist that can directly be used for
  12170. completion."
  12171. (let (vals)
  12172. (cond
  12173. ((equal property "TODO")
  12174. (setq vals (org-with-point-at pom
  12175. (append org-todo-keywords-1 '("")))))
  12176. ((equal property "PRIORITY")
  12177. (let ((n org-lowest-priority))
  12178. (while (>= n org-highest-priority)
  12179. (push (char-to-string n) vals)
  12180. (setq n (1- n)))))
  12181. ((member property org-special-properties))
  12182. ((setq vals (run-hook-with-args-until-success
  12183. 'org-property-allowed-value-functions property)))
  12184. (t
  12185. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12186. (when (and vals (string-match "\\S-" vals))
  12187. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12188. (setq vals (mapcar (lambda (x)
  12189. (cond ((stringp x) x)
  12190. ((numberp x) (number-to-string x))
  12191. ((symbolp x) (symbol-name x))
  12192. (t "???")))
  12193. vals)))))
  12194. (when (member ":ETC" vals)
  12195. (setq vals (remove ":ETC" vals))
  12196. (org-add-props (car vals) '(org-unrestricted t)))
  12197. (if table (mapcar 'list vals) vals)))
  12198. (defun org-property-previous-allowed-value (&optional previous)
  12199. "Switch to the next allowed value for this property."
  12200. (interactive)
  12201. (org-property-next-allowed-value t))
  12202. (defun org-property-next-allowed-value (&optional previous)
  12203. "Switch to the next allowed value for this property."
  12204. (interactive)
  12205. (unless (org-at-property-p)
  12206. (error "Not at a property"))
  12207. (let* ((key (match-string 2))
  12208. (value (match-string 3))
  12209. (allowed (or (org-property-get-allowed-values (point) key)
  12210. (and (member value '("[ ]" "[-]" "[X]"))
  12211. '("[ ]" "[X]"))))
  12212. nval)
  12213. (unless allowed
  12214. (error "Allowed values for this property have not been defined"))
  12215. (if previous (setq allowed (reverse allowed)))
  12216. (if (member value allowed)
  12217. (setq nval (car (cdr (member value allowed)))))
  12218. (setq nval (or nval (car allowed)))
  12219. (if (equal nval value)
  12220. (error "Only one allowed value for this property"))
  12221. (org-at-property-p)
  12222. (replace-match (concat " :" key ": " nval) t t)
  12223. (org-indent-line-function)
  12224. (beginning-of-line 1)
  12225. (skip-chars-forward " \t")
  12226. (run-hook-with-args 'org-property-changed-functions key nval)))
  12227. (defun org-find-entry-with-id (ident)
  12228. "Locate the entry that contains the ID property with exact value IDENT.
  12229. IDENT can be a string, a symbol or a number, this function will search for
  12230. the string representation of it.
  12231. Return the position where this entry starts, or nil if there is no such entry."
  12232. (interactive "sID: ")
  12233. (let ((id (cond
  12234. ((stringp ident) ident)
  12235. ((symbol-name ident) (symbol-name ident))
  12236. ((numberp ident) (number-to-string ident))
  12237. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12238. (case-fold-search nil))
  12239. (save-excursion
  12240. (save-restriction
  12241. (widen)
  12242. (goto-char (point-min))
  12243. (when (re-search-forward
  12244. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12245. nil t)
  12246. (org-back-to-heading t)
  12247. (point))))))
  12248. ;;;; Timestamps
  12249. (defvar org-last-changed-timestamp nil)
  12250. (defvar org-last-inserted-timestamp nil
  12251. "The last time stamp inserted with `org-insert-time-stamp'.")
  12252. (defvar org-time-was-given) ; dynamically scoped parameter
  12253. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12254. (defvar org-ts-what) ; dynamically scoped parameter
  12255. (defun org-time-stamp (arg &optional inactive)
  12256. "Prompt for a date/time and insert a time stamp.
  12257. If the user specifies a time like HH:MM, or if this command is called
  12258. with a prefix argument, the time stamp will contain date and time.
  12259. Otherwise, only the date will be included. All parts of a date not
  12260. specified by the user will be filled in from the current date/time.
  12261. So if you press just return without typing anything, the time stamp
  12262. will represent the current date/time. If there is already a timestamp
  12263. at the cursor, it will be modified."
  12264. (interactive "P")
  12265. (let* ((ts nil)
  12266. (default-time
  12267. ;; Default time is either today, or, when entering a range,
  12268. ;; the range start.
  12269. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12270. (save-excursion
  12271. (re-search-backward
  12272. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12273. (- (point) 20) t)))
  12274. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12275. (current-time)))
  12276. (default-input (and ts (org-get-compact-tod ts)))
  12277. org-time-was-given org-end-time-was-given time)
  12278. (cond
  12279. ((and (org-at-timestamp-p t)
  12280. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12281. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12282. (insert "--")
  12283. (setq time (let ((this-command this-command))
  12284. (org-read-date arg 'totime nil nil
  12285. default-time default-input)))
  12286. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12287. ((org-at-timestamp-p t)
  12288. (setq time (let ((this-command this-command))
  12289. (org-read-date arg 'totime nil nil default-time default-input)))
  12290. (when (org-at-timestamp-p t) ; just to get the match data
  12291. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12292. (replace-match "")
  12293. (setq org-last-changed-timestamp
  12294. (org-insert-time-stamp
  12295. time (or org-time-was-given arg)
  12296. inactive nil nil (list org-end-time-was-given))))
  12297. (message "Timestamp updated"))
  12298. (t
  12299. (setq time (let ((this-command this-command))
  12300. (org-read-date arg 'totime nil nil default-time default-input)))
  12301. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12302. nil nil (list org-end-time-was-given))))))
  12303. ;; FIXME: can we use this for something else, like computing time differences?
  12304. (defun org-get-compact-tod (s)
  12305. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12306. (let* ((t1 (match-string 1 s))
  12307. (h1 (string-to-number (match-string 2 s)))
  12308. (m1 (string-to-number (match-string 3 s)))
  12309. (t2 (and (match-end 4) (match-string 5 s)))
  12310. (h2 (and t2 (string-to-number (match-string 6 s))))
  12311. (m2 (and t2 (string-to-number (match-string 7 s))))
  12312. dh dm)
  12313. (if (not t2)
  12314. t1
  12315. (setq dh (- h2 h1) dm (- m2 m1))
  12316. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12317. (concat t1 "+" (number-to-string dh)
  12318. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12319. (defun org-time-stamp-inactive (&optional arg)
  12320. "Insert an inactive time stamp.
  12321. An inactive time stamp is enclosed in square brackets instead of angle
  12322. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12323. does not link to the calendar and cannot be changed with the S-cursor keys.
  12324. So these are more for recording a certain time/date."
  12325. (interactive "P")
  12326. (org-time-stamp arg 'inactive))
  12327. (defvar org-date-ovl (make-overlay 1 1))
  12328. (overlay-put org-date-ovl 'face 'org-warning)
  12329. (org-detach-overlay org-date-ovl)
  12330. (defvar org-ans1) ; dynamically scoped parameter
  12331. (defvar org-ans2) ; dynamically scoped parameter
  12332. (defvar org-plain-time-of-day-regexp) ; defined below
  12333. (defvar org-overriding-default-time nil) ; dynamically scoped
  12334. (defvar org-read-date-overlay nil)
  12335. (defvar org-dcst nil) ; dynamically scoped
  12336. (defvar org-read-date-history nil)
  12337. (defvar org-read-date-final-answer nil)
  12338. (defun org-read-date (&optional with-time to-time from-string prompt
  12339. default-time default-input)
  12340. "Read a date, possibly a time, and make things smooth for the user.
  12341. The prompt will suggest to enter an ISO date, but you can also enter anything
  12342. which will at least partially be understood by `parse-time-string'.
  12343. Unrecognized parts of the date will default to the current day, month, year,
  12344. hour and minute. If this command is called to replace a timestamp at point,
  12345. of to enter the second timestamp of a range, the default time is taken from the
  12346. existing stamp. For example,
  12347. 3-2-5 --> 2003-02-05
  12348. feb 15 --> currentyear-02-15
  12349. sep 12 9 --> 2009-09-12
  12350. 12:45 --> today 12:45
  12351. 22 sept 0:34 --> currentyear-09-22 0:34
  12352. 12 --> currentyear-currentmonth-12
  12353. Fri --> nearest Friday (today or later)
  12354. etc.
  12355. Furthermore you can specify a relative date by giving, as the *first* thing
  12356. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12357. change in days weeks, months, years.
  12358. With a single plus or minus, the date is relative to today. With a double
  12359. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12360. +4d --> four days from today
  12361. +4 --> same as above
  12362. +2w --> two weeks from today
  12363. ++5 --> five days from default date
  12364. The function understands only English month and weekday abbreviations,
  12365. but this can be configured with the variables `parse-time-months' and
  12366. `parse-time-weekdays'.
  12367. While prompting, a calendar is popped up - you can also select the
  12368. date with the mouse (button 1). The calendar shows a period of three
  12369. months. To scroll it to other months, use the keys `>' and `<'.
  12370. If you don't like the calendar, turn it off with
  12371. \(setq org-read-date-popup-calendar nil)
  12372. With optional argument TO-TIME, the date will immediately be converted
  12373. to an internal time.
  12374. With an optional argument WITH-TIME, the prompt will suggest to also
  12375. insert a time. Note that when WITH-TIME is not set, you can still
  12376. enter a time, and this function will inform the calling routine about
  12377. this change. The calling routine may then choose to change the format
  12378. used to insert the time stamp into the buffer to include the time.
  12379. With optional argument FROM-STRING, read from this string instead from
  12380. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12381. the time/date that is used for everything that is not specified by the
  12382. user."
  12383. (require 'parse-time)
  12384. (let* ((org-time-stamp-rounding-minutes
  12385. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12386. (org-dcst org-display-custom-times)
  12387. (ct (org-current-time))
  12388. (def (or org-overriding-default-time default-time ct))
  12389. (defdecode (decode-time def))
  12390. (dummy (progn
  12391. (when (< (nth 2 defdecode) org-extend-today-until)
  12392. (setcar (nthcdr 2 defdecode) -1)
  12393. (setcar (nthcdr 1 defdecode) 59)
  12394. (setq def (apply 'encode-time defdecode)
  12395. defdecode (decode-time def)))))
  12396. (calendar-frame-setup nil)
  12397. (calendar-move-hook nil)
  12398. (calendar-view-diary-initially-flag nil)
  12399. (calendar-view-holidays-initially-flag nil)
  12400. (timestr (format-time-string
  12401. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12402. (prompt (concat (if prompt (concat prompt " ") "")
  12403. (format "Date+time [%s]: " timestr)))
  12404. ans (org-ans0 "") org-ans1 org-ans2 final)
  12405. (cond
  12406. (from-string (setq ans from-string))
  12407. (org-read-date-popup-calendar
  12408. (save-excursion
  12409. (save-window-excursion
  12410. (calendar)
  12411. (calendar-forward-day (- (time-to-days def)
  12412. (calendar-absolute-from-gregorian
  12413. (calendar-current-date))))
  12414. (org-eval-in-calendar nil t)
  12415. (let* ((old-map (current-local-map))
  12416. (map (copy-keymap calendar-mode-map))
  12417. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12418. (org-defkey map (kbd "RET") 'org-calendar-select)
  12419. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12420. 'org-calendar-select-mouse)
  12421. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12422. 'org-calendar-select-mouse)
  12423. (org-defkey minibuffer-local-map [(meta shift left)]
  12424. (lambda () (interactive)
  12425. (org-eval-in-calendar '(calendar-backward-month 1))))
  12426. (org-defkey minibuffer-local-map [(meta shift right)]
  12427. (lambda () (interactive)
  12428. (org-eval-in-calendar '(calendar-forward-month 1))))
  12429. (org-defkey minibuffer-local-map [(meta shift up)]
  12430. (lambda () (interactive)
  12431. (org-eval-in-calendar '(calendar-backward-year 1))))
  12432. (org-defkey minibuffer-local-map [(meta shift down)]
  12433. (lambda () (interactive)
  12434. (org-eval-in-calendar '(calendar-forward-year 1))))
  12435. (org-defkey minibuffer-local-map [?\e (shift left)]
  12436. (lambda () (interactive)
  12437. (org-eval-in-calendar '(calendar-backward-month 1))))
  12438. (org-defkey minibuffer-local-map [?\e (shift right)]
  12439. (lambda () (interactive)
  12440. (org-eval-in-calendar '(calendar-forward-month 1))))
  12441. (org-defkey minibuffer-local-map [?\e (shift up)]
  12442. (lambda () (interactive)
  12443. (org-eval-in-calendar '(calendar-backward-year 1))))
  12444. (org-defkey minibuffer-local-map [?\e (shift down)]
  12445. (lambda () (interactive)
  12446. (org-eval-in-calendar '(calendar-forward-year 1))))
  12447. (org-defkey minibuffer-local-map [(shift up)]
  12448. (lambda () (interactive)
  12449. (org-eval-in-calendar '(calendar-backward-week 1))))
  12450. (org-defkey minibuffer-local-map [(shift down)]
  12451. (lambda () (interactive)
  12452. (org-eval-in-calendar '(calendar-forward-week 1))))
  12453. (org-defkey minibuffer-local-map [(shift left)]
  12454. (lambda () (interactive)
  12455. (org-eval-in-calendar '(calendar-backward-day 1))))
  12456. (org-defkey minibuffer-local-map [(shift right)]
  12457. (lambda () (interactive)
  12458. (org-eval-in-calendar '(calendar-forward-day 1))))
  12459. (org-defkey minibuffer-local-map ">"
  12460. (lambda () (interactive)
  12461. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12462. (org-defkey minibuffer-local-map "<"
  12463. (lambda () (interactive)
  12464. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12465. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12466. (unwind-protect
  12467. (progn
  12468. (use-local-map map)
  12469. (add-hook 'post-command-hook 'org-read-date-display)
  12470. (setq org-ans0 (read-string prompt default-input
  12471. 'org-read-date-history nil))
  12472. ;; org-ans0: from prompt
  12473. ;; org-ans1: from mouse click
  12474. ;; org-ans2: from calendar motion
  12475. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12476. (remove-hook 'post-command-hook 'org-read-date-display)
  12477. (use-local-map old-map)
  12478. (when org-read-date-overlay
  12479. (delete-overlay org-read-date-overlay)
  12480. (setq org-read-date-overlay nil)))))))
  12481. (t ; Naked prompt only
  12482. (unwind-protect
  12483. (setq ans (read-string prompt default-input
  12484. 'org-read-date-history timestr))
  12485. (when org-read-date-overlay
  12486. (delete-overlay org-read-date-overlay)
  12487. (setq org-read-date-overlay nil)))))
  12488. (setq final (org-read-date-analyze ans def defdecode))
  12489. (setq org-read-date-final-answer ans)
  12490. (if to-time
  12491. (apply 'encode-time final)
  12492. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12493. (format "%04d-%02d-%02d %02d:%02d"
  12494. (nth 5 final) (nth 4 final) (nth 3 final)
  12495. (nth 2 final) (nth 1 final))
  12496. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12497. (defvar def)
  12498. (defvar defdecode)
  12499. (defvar with-time)
  12500. (defvar org-read-date-analyze-futurep nil)
  12501. (defun org-read-date-display ()
  12502. "Display the current date prompt interpretation in the minibuffer."
  12503. (when org-read-date-display-live
  12504. (when org-read-date-overlay
  12505. (delete-overlay org-read-date-overlay))
  12506. (let ((p (point)))
  12507. (end-of-line 1)
  12508. (while (not (equal (buffer-substring
  12509. (max (point-min) (- (point) 4)) (point))
  12510. " "))
  12511. (insert " "))
  12512. (goto-char p))
  12513. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12514. " " (or org-ans1 org-ans2)))
  12515. (org-end-time-was-given nil)
  12516. (f (org-read-date-analyze ans def defdecode))
  12517. (fmts (if org-dcst
  12518. org-time-stamp-custom-formats
  12519. org-time-stamp-formats))
  12520. (fmt (if (or with-time
  12521. (and (boundp 'org-time-was-given) org-time-was-given))
  12522. (cdr fmts)
  12523. (car fmts)))
  12524. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12525. (when (and org-end-time-was-given
  12526. (string-match org-plain-time-of-day-regexp txt))
  12527. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12528. org-end-time-was-given
  12529. (substring txt (match-end 0)))))
  12530. (when org-read-date-analyze-futurep
  12531. (setq txt (concat txt " (=>F)")))
  12532. (setq org-read-date-overlay
  12533. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12534. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12535. (defun org-read-date-analyze (ans def defdecode)
  12536. "Analyse the combined answer of the date prompt."
  12537. ;; FIXME: cleanup and comment
  12538. (let ((nowdecode (decode-time (current-time)))
  12539. delta deltan deltaw deltadef year month day
  12540. hour minute second wday pm h2 m2 tl wday1
  12541. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12542. (setq org-read-date-analyze-futurep nil)
  12543. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12544. (setq ans "+0"))
  12545. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12546. (setq ans (replace-match "" t t ans)
  12547. deltan (car delta)
  12548. deltaw (nth 1 delta)
  12549. deltadef (nth 2 delta)))
  12550. ;; Check if there is an iso week date in there
  12551. ;; If yes, store the info and postpone interpreting it until the rest
  12552. ;; of the parsing is done
  12553. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12554. (setq iso-year (if (match-end 1)
  12555. (org-small-year-to-year
  12556. (string-to-number (match-string 1 ans))))
  12557. iso-weekday (if (match-end 3)
  12558. (string-to-number (match-string 3 ans)))
  12559. iso-week (string-to-number (match-string 2 ans)))
  12560. (setq ans (replace-match "" t t ans)))
  12561. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12562. (when (string-match
  12563. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12564. (setq year (if (match-end 2)
  12565. (string-to-number (match-string 2 ans))
  12566. (progn (setq kill-year t)
  12567. (string-to-number (format-time-string "%Y"))))
  12568. month (string-to-number (match-string 3 ans))
  12569. day (string-to-number (match-string 4 ans)))
  12570. (if (< year 100) (setq year (+ 2000 year)))
  12571. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12572. t nil ans)))
  12573. ;; Help matching american dates, like 5/30 or 5/30/7
  12574. (when (string-match
  12575. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12576. (setq year (if (match-end 4)
  12577. (string-to-number (match-string 4 ans))
  12578. (progn (setq kill-year t)
  12579. (string-to-number (format-time-string "%Y"))))
  12580. month (string-to-number (match-string 1 ans))
  12581. day (string-to-number (match-string 2 ans)))
  12582. (if (< year 100) (setq year (+ 2000 year)))
  12583. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12584. t nil ans)))
  12585. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12586. ;; If there is a time with am/pm, and *no* time without it, we convert
  12587. ;; so that matching will be successful.
  12588. (loop for i from 1 to 2 do ; twice, for end time as well
  12589. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12590. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12591. (setq hour (string-to-number (match-string 1 ans))
  12592. minute (if (match-end 3)
  12593. (string-to-number (match-string 3 ans))
  12594. 0)
  12595. pm (equal ?p
  12596. (string-to-char (downcase (match-string 4 ans)))))
  12597. (if (and (= hour 12) (not pm))
  12598. (setq hour 0)
  12599. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12600. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12601. t t ans))))
  12602. ;; Check if a time range is given as a duration
  12603. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12604. (setq hour (string-to-number (match-string 1 ans))
  12605. h2 (+ hour (string-to-number (match-string 3 ans)))
  12606. minute (string-to-number (match-string 2 ans))
  12607. m2 (+ minute (if (match-end 5) (string-to-number
  12608. (match-string 5 ans))0)))
  12609. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12610. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12611. t t ans)))
  12612. ;; Check if there is a time range
  12613. (when (boundp 'org-end-time-was-given)
  12614. (setq org-time-was-given nil)
  12615. (when (and (string-match org-plain-time-of-day-regexp ans)
  12616. (match-end 8))
  12617. (setq org-end-time-was-given (match-string 8 ans))
  12618. (setq ans (concat (substring ans 0 (match-beginning 7))
  12619. (substring ans (match-end 7))))))
  12620. (setq tl (parse-time-string ans)
  12621. day (or (nth 3 tl) (nth 3 defdecode))
  12622. month (or (nth 4 tl)
  12623. (if (and org-read-date-prefer-future
  12624. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12625. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12626. (nth 4 defdecode)))
  12627. year (or (and (not kill-year) (nth 5 tl))
  12628. (if (and org-read-date-prefer-future
  12629. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12630. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12631. (nth 5 defdecode)))
  12632. hour (or (nth 2 tl) (nth 2 defdecode))
  12633. minute (or (nth 1 tl) (nth 1 defdecode))
  12634. second (or (nth 0 tl) 0)
  12635. wday (nth 6 tl))
  12636. (when (and (eq org-read-date-prefer-future 'time)
  12637. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12638. (equal day (nth 3 nowdecode))
  12639. (equal month (nth 4 nowdecode))
  12640. (equal year (nth 5 nowdecode))
  12641. (nth 2 tl)
  12642. (or (< (nth 2 tl) (nth 2 nowdecode))
  12643. (and (= (nth 2 tl) (nth 2 nowdecode))
  12644. (nth 1 tl)
  12645. (< (nth 1 tl) (nth 1 nowdecode)))))
  12646. (setq day (1+ day)
  12647. futurep t))
  12648. ;; Special date definitions below
  12649. (cond
  12650. (iso-week
  12651. ;; There was an iso week
  12652. (require 'cal-iso)
  12653. (setq futurep nil)
  12654. (setq year (or iso-year year)
  12655. day (or iso-weekday wday 1)
  12656. wday nil ; to make sure that the trigger below does not match
  12657. iso-date (calendar-gregorian-from-absolute
  12658. (calendar-absolute-from-iso
  12659. (list iso-week day year))))
  12660. ; FIXME: Should we also push ISO weeks into the future?
  12661. ; (when (and org-read-date-prefer-future
  12662. ; (not iso-year)
  12663. ; (< (calendar-absolute-from-gregorian iso-date)
  12664. ; (time-to-days (current-time))))
  12665. ; (setq year (1+ year)
  12666. ; iso-date (calendar-gregorian-from-absolute
  12667. ; (calendar-absolute-from-iso
  12668. ; (list iso-week day year)))))
  12669. (setq month (car iso-date)
  12670. year (nth 2 iso-date)
  12671. day (nth 1 iso-date)))
  12672. (deltan
  12673. (setq futurep nil)
  12674. (unless deltadef
  12675. (let ((now (decode-time (current-time))))
  12676. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12677. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12678. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12679. ((equal deltaw "m") (setq month (+ month deltan)))
  12680. ((equal deltaw "y") (setq year (+ year deltan)))))
  12681. ((and wday (not (nth 3 tl)))
  12682. (setq futurep nil)
  12683. ;; Weekday was given, but no day, so pick that day in the week
  12684. ;; on or after the derived date.
  12685. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12686. (unless (equal wday wday1)
  12687. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12688. (if (and (boundp 'org-time-was-given)
  12689. (nth 2 tl))
  12690. (setq org-time-was-given t))
  12691. (if (< year 100) (setq year (+ 2000 year)))
  12692. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12693. (setq org-read-date-analyze-futurep futurep)
  12694. (list second minute hour day month year)))
  12695. (defvar parse-time-weekdays)
  12696. (defun org-read-date-get-relative (s today default)
  12697. "Check string S for special relative date string.
  12698. TODAY and DEFAULT are internal times, for today and for a default.
  12699. Return shift list (N what def-flag)
  12700. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12701. N is the number of WHATs to shift.
  12702. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12703. the DEFAULT date rather than TODAY."
  12704. (when (and
  12705. (string-match
  12706. (concat
  12707. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12708. "\\([0-9]+\\)?"
  12709. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12710. "\\([ \t]\\|$\\)") s)
  12711. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12712. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12713. (string-to-char (substring (match-string 1 s) -1))
  12714. ?+))
  12715. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12716. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12717. (what (if (match-end 3) (match-string 3 s) "d"))
  12718. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12719. (date (if rel default today))
  12720. (wday (nth 6 (decode-time date)))
  12721. delta)
  12722. (if wday1
  12723. (progn
  12724. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12725. (if (= dir ?-) (setq delta (- delta 7)))
  12726. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12727. (list delta "d" rel))
  12728. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12729. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12730. "Turn a user-specified date into the internal representation.
  12731. The internal representation needed by the calendar is (month day year).
  12732. This is a wrapper to handle the brain-dead convention in calendar that
  12733. user function argument order change dependent on argument order."
  12734. (if (boundp 'calendar-date-style)
  12735. (cond
  12736. ((eq calendar-date-style 'american)
  12737. (list arg1 arg2 arg3))
  12738. ((eq calendar-date-style 'european)
  12739. (list arg2 arg1 arg3))
  12740. ((eq calendar-date-style 'iso)
  12741. (list arg2 arg3 arg1)))
  12742. (if (org-bound-and-true-p european-calendar-style)
  12743. (list arg2 arg1 arg3)
  12744. (list arg1 arg2 arg3))))
  12745. (defun org-eval-in-calendar (form &optional keepdate)
  12746. "Eval FORM in the calendar window and return to current window.
  12747. Also, store the cursor date in variable org-ans2."
  12748. (let ((sf (selected-frame))
  12749. (sw (selected-window)))
  12750. (select-window (get-buffer-window "*Calendar*" t))
  12751. (eval form)
  12752. (when (and (not keepdate) (calendar-cursor-to-date))
  12753. (let* ((date (calendar-cursor-to-date))
  12754. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12755. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12756. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12757. (select-window sw)
  12758. (org-select-frame-set-input-focus sf)))
  12759. (defun org-calendar-select ()
  12760. "Return to `org-read-date' with the date currently selected.
  12761. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12762. (interactive)
  12763. (when (calendar-cursor-to-date)
  12764. (let* ((date (calendar-cursor-to-date))
  12765. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12766. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12767. (if (active-minibuffer-window) (exit-minibuffer))))
  12768. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12769. "Insert a date stamp for the date given by the internal TIME.
  12770. WITH-HM means use the stamp format that includes the time of the day.
  12771. INACTIVE means use square brackets instead of angular ones, so that the
  12772. stamp will not contribute to the agenda.
  12773. PRE and POST are optional strings to be inserted before and after the
  12774. stamp.
  12775. The command returns the inserted time stamp."
  12776. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12777. stamp)
  12778. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12779. (insert-before-markers (or pre ""))
  12780. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12781. (when (listp extra)
  12782. (setq extra (car extra))
  12783. (if (and (stringp extra)
  12784. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12785. (setq extra (format "-%02d:%02d"
  12786. (string-to-number (match-string 1 extra))
  12787. (string-to-number (match-string 2 extra))))
  12788. (setq extra nil)))
  12789. (when extra
  12790. (backward-char 1)
  12791. (insert-before-markers extra)
  12792. (forward-char 1))
  12793. (insert-before-markers (or post ""))
  12794. (setq org-last-inserted-timestamp stamp)))
  12795. (defun org-toggle-time-stamp-overlays ()
  12796. "Toggle the use of custom time stamp formats."
  12797. (interactive)
  12798. (setq org-display-custom-times (not org-display-custom-times))
  12799. (unless org-display-custom-times
  12800. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12801. (while (setq p (next-single-property-change p 'display))
  12802. (if (and (get-text-property p 'display)
  12803. (eq (get-text-property p 'face) 'org-date))
  12804. (remove-text-properties
  12805. p (setq p (next-single-property-change p 'display))
  12806. '(display t))))
  12807. (set-buffer-modified-p bmp)))
  12808. (if (featurep 'xemacs)
  12809. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12810. (org-restart-font-lock)
  12811. (setq org-table-may-need-update t)
  12812. (if org-display-custom-times
  12813. (message "Time stamps are overlayed with custom format")
  12814. (message "Time stamp overlays removed")))
  12815. (defun org-display-custom-time (beg end)
  12816. "Overlay modified time stamp format over timestamp between BEG and END."
  12817. (let* ((ts (buffer-substring beg end))
  12818. t1 w1 with-hm tf time str w2 (off 0))
  12819. (save-match-data
  12820. (setq t1 (org-parse-time-string ts t))
  12821. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12822. (setq off (- (match-end 0) (match-beginning 0)))))
  12823. (setq end (- end off))
  12824. (setq w1 (- end beg)
  12825. with-hm (and (nth 1 t1) (nth 2 t1))
  12826. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12827. time (org-fix-decoded-time t1)
  12828. str (org-add-props
  12829. (format-time-string
  12830. (substring tf 1 -1) (apply 'encode-time time))
  12831. nil 'mouse-face 'highlight)
  12832. w2 (length str))
  12833. (if (not (= w2 w1))
  12834. (add-text-properties (1+ beg) (+ 2 beg)
  12835. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12836. (if (featurep 'xemacs)
  12837. (progn
  12838. (put-text-property beg end 'invisible t)
  12839. (put-text-property beg end 'end-glyph (make-glyph str)))
  12840. (put-text-property beg end 'display str))))
  12841. (defun org-translate-time (string)
  12842. "Translate all timestamps in STRING to custom format.
  12843. But do this only if the variable `org-display-custom-times' is set."
  12844. (when org-display-custom-times
  12845. (save-match-data
  12846. (let* ((start 0)
  12847. (re org-ts-regexp-both)
  12848. t1 with-hm inactive tf time str beg end)
  12849. (while (setq start (string-match re string start))
  12850. (setq beg (match-beginning 0)
  12851. end (match-end 0)
  12852. t1 (save-match-data
  12853. (org-parse-time-string (substring string beg end) t))
  12854. with-hm (and (nth 1 t1) (nth 2 t1))
  12855. inactive (equal (substring string beg (1+ beg)) "[")
  12856. tf (funcall (if with-hm 'cdr 'car)
  12857. org-time-stamp-custom-formats)
  12858. time (org-fix-decoded-time t1)
  12859. str (format-time-string
  12860. (concat
  12861. (if inactive "[" "<") (substring tf 1 -1)
  12862. (if inactive "]" ">"))
  12863. (apply 'encode-time time))
  12864. string (replace-match str t t string)
  12865. start (+ start (length str)))))))
  12866. string)
  12867. (defun org-fix-decoded-time (time)
  12868. "Set 0 instead of nil for the first 6 elements of time.
  12869. Don't touch the rest."
  12870. (let ((n 0))
  12871. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12872. (defun org-days-to-time (timestamp-string)
  12873. "Difference between TIMESTAMP-STRING and now in days."
  12874. (- (time-to-days (org-time-string-to-time timestamp-string))
  12875. (time-to-days (current-time))))
  12876. (defun org-deadline-close (timestamp-string &optional ndays)
  12877. "Is the time in TIMESTAMP-STRING close to the current date?"
  12878. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12879. (and (< (org-days-to-time timestamp-string) ndays)
  12880. (not (org-entry-is-done-p))))
  12881. (defun org-get-wdays (ts)
  12882. "Get the deadline lead time appropriate for timestring TS."
  12883. (cond
  12884. ((<= org-deadline-warning-days 0)
  12885. ;; 0 or negative, enforce this value no matter what
  12886. (- org-deadline-warning-days))
  12887. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12888. ;; lead time is specified.
  12889. (floor (* (string-to-number (match-string 1 ts))
  12890. (cdr (assoc (match-string 2 ts)
  12891. '(("d" . 1) ("w" . 7)
  12892. ("m" . 30.4) ("y" . 365.25)))))))
  12893. ;; go for the default.
  12894. (t org-deadline-warning-days)))
  12895. (defun org-calendar-select-mouse (ev)
  12896. "Return to `org-read-date' with the date currently selected.
  12897. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12898. (interactive "e")
  12899. (mouse-set-point ev)
  12900. (when (calendar-cursor-to-date)
  12901. (let* ((date (calendar-cursor-to-date))
  12902. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12903. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12904. (if (active-minibuffer-window) (exit-minibuffer))))
  12905. (defun org-check-deadlines (ndays)
  12906. "Check if there are any deadlines due or past due.
  12907. A deadline is considered due if it happens within `org-deadline-warning-days'
  12908. days from today's date. If the deadline appears in an entry marked DONE,
  12909. it is not shown. The prefix arg NDAYS can be used to test that many
  12910. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12911. (interactive "P")
  12912. (let* ((org-warn-days
  12913. (cond
  12914. ((equal ndays '(4)) 100000)
  12915. (ndays (prefix-numeric-value ndays))
  12916. (t (abs org-deadline-warning-days))))
  12917. (case-fold-search nil)
  12918. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12919. (callback
  12920. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12921. (message "%d deadlines past-due or due within %d days"
  12922. (org-occur regexp nil callback)
  12923. org-warn-days)))
  12924. (defun org-check-before-date (date)
  12925. "Check if there are deadlines or scheduled entries before DATE."
  12926. (interactive (list (org-read-date)))
  12927. (let ((case-fold-search nil)
  12928. (regexp (concat "\\<\\(" org-deadline-string
  12929. "\\|" org-scheduled-string
  12930. "\\) *<\\([^>]+\\)>"))
  12931. (callback
  12932. (lambda () (time-less-p
  12933. (org-time-string-to-time (match-string 2))
  12934. (org-time-string-to-time date)))))
  12935. (message "%d entries before %s"
  12936. (org-occur regexp nil callback) date)))
  12937. (defun org-check-after-date (date)
  12938. "Check if there are deadlines or scheduled entries after DATE."
  12939. (interactive (list (org-read-date)))
  12940. (let ((case-fold-search nil)
  12941. (regexp (concat "\\<\\(" org-deadline-string
  12942. "\\|" org-scheduled-string
  12943. "\\) *<\\([^>]+\\)>"))
  12944. (callback
  12945. (lambda () (not
  12946. (time-less-p
  12947. (org-time-string-to-time (match-string 2))
  12948. (org-time-string-to-time date))))))
  12949. (message "%d entries after %s"
  12950. (org-occur regexp nil callback) date)))
  12951. (defun org-evaluate-time-range (&optional to-buffer)
  12952. "Evaluate a time range by computing the difference between start and end.
  12953. Normally the result is just printed in the echo area, but with prefix arg
  12954. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12955. If the time range is actually in a table, the result is inserted into the
  12956. next column.
  12957. For time difference computation, a year is assumed to be exactly 365
  12958. days in order to avoid rounding problems."
  12959. (interactive "P")
  12960. (or
  12961. (org-clock-update-time-maybe)
  12962. (save-excursion
  12963. (unless (org-at-date-range-p t)
  12964. (goto-char (point-at-bol))
  12965. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12966. (if (not (org-at-date-range-p t))
  12967. (error "Not at a time-stamp range, and none found in current line")))
  12968. (let* ((ts1 (match-string 1))
  12969. (ts2 (match-string 2))
  12970. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12971. (match-end (match-end 0))
  12972. (time1 (org-time-string-to-time ts1))
  12973. (time2 (org-time-string-to-time ts2))
  12974. (t1 (org-float-time time1))
  12975. (t2 (org-float-time time2))
  12976. (diff (abs (- t2 t1)))
  12977. (negative (< (- t2 t1) 0))
  12978. ;; (ys (floor (* 365 24 60 60)))
  12979. (ds (* 24 60 60))
  12980. (hs (* 60 60))
  12981. (fy "%dy %dd %02d:%02d")
  12982. (fy1 "%dy %dd")
  12983. (fd "%dd %02d:%02d")
  12984. (fd1 "%dd")
  12985. (fh "%02d:%02d")
  12986. y d h m align)
  12987. (if havetime
  12988. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12989. y 0
  12990. d (floor (/ diff ds)) diff (mod diff ds)
  12991. h (floor (/ diff hs)) diff (mod diff hs)
  12992. m (floor (/ diff 60)))
  12993. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12994. y 0
  12995. d (floor (+ (/ diff ds) 0.5))
  12996. h 0 m 0))
  12997. (if (not to-buffer)
  12998. (message "%s" (org-make-tdiff-string y d h m))
  12999. (if (org-at-table-p)
  13000. (progn
  13001. (goto-char match-end)
  13002. (setq align t)
  13003. (and (looking-at " *|") (goto-char (match-end 0))))
  13004. (goto-char match-end))
  13005. (if (looking-at
  13006. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13007. (replace-match ""))
  13008. (if negative (insert " -"))
  13009. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13010. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13011. (insert " " (format fh h m))))
  13012. (if align (org-table-align))
  13013. (message "Time difference inserted")))))
  13014. (defun org-make-tdiff-string (y d h m)
  13015. (let ((fmt "")
  13016. (l nil))
  13017. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13018. l (push y l)))
  13019. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13020. l (push d l)))
  13021. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13022. l (push h l)))
  13023. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13024. l (push m l)))
  13025. (apply 'format fmt (nreverse l))))
  13026. (defun org-time-string-to-time (s)
  13027. (apply 'encode-time (org-parse-time-string s)))
  13028. (defun org-time-string-to-seconds (s)
  13029. (org-float-time (org-time-string-to-time s)))
  13030. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13031. "Convert a time stamp to an absolute day number.
  13032. If there is a specifyer for a cyclic time stamp, get the closest date to
  13033. DAYNR.
  13034. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13035. the variable date is bound by the calendar when this is called."
  13036. (cond
  13037. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13038. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13039. daynr
  13040. (+ daynr 1000)))
  13041. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13042. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13043. (time-to-days (current-time))) (match-string 0 s)
  13044. prefer show-all))
  13045. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13046. (defun org-days-to-iso-week (days)
  13047. "Return the iso week number."
  13048. (require 'cal-iso)
  13049. (car (calendar-iso-from-absolute days)))
  13050. (defun org-small-year-to-year (year)
  13051. "Convert 2-digit years into 4-digit years.
  13052. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13053. The year 2000 cannot be abbreviated. Any year larger than 99
  13054. is returned unchanged."
  13055. (if (< year 38)
  13056. (setq year (+ 2000 year))
  13057. (if (< year 100)
  13058. (setq year (+ 1900 year))))
  13059. year)
  13060. (defun org-time-from-absolute (d)
  13061. "Return the time corresponding to date D.
  13062. D may be an absolute day number, or a calendar-type list (month day year)."
  13063. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13064. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13065. (defun org-calendar-holiday ()
  13066. "List of holidays, for Diary display in Org-mode."
  13067. (require 'holidays)
  13068. (let ((hl (funcall
  13069. (if (fboundp 'calendar-check-holidays)
  13070. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13071. (if hl (mapconcat 'identity hl "; "))))
  13072. (defun org-diary-sexp-entry (sexp entry date)
  13073. "Process a SEXP diary ENTRY for DATE."
  13074. (require 'diary-lib)
  13075. (let ((result (if calendar-debug-sexp
  13076. (let ((stack-trace-on-error t))
  13077. (eval (car (read-from-string sexp))))
  13078. (condition-case nil
  13079. (eval (car (read-from-string sexp)))
  13080. (error
  13081. (beep)
  13082. (message "Bad sexp at line %d in %s: %s"
  13083. (org-current-line)
  13084. (buffer-file-name) sexp)
  13085. (sleep-for 2))))))
  13086. (cond ((stringp result) result)
  13087. ((and (consp result)
  13088. (stringp (cdr result))) (cdr result))
  13089. (result entry)
  13090. (t nil))))
  13091. (defun org-diary-to-ical-string (frombuf)
  13092. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13093. This uses the icalendar.el library."
  13094. (let* ((tmpdir (if (featurep 'xemacs)
  13095. (temp-directory)
  13096. temporary-file-directory))
  13097. (tmpfile (make-temp-name
  13098. (expand-file-name "orgics" tmpdir)))
  13099. buf rtn b e)
  13100. (with-current-buffer frombuf
  13101. (icalendar-export-region (point-min) (point-max) tmpfile)
  13102. (setq buf (find-buffer-visiting tmpfile))
  13103. (set-buffer buf)
  13104. (goto-char (point-min))
  13105. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13106. (setq b (match-beginning 0)))
  13107. (goto-char (point-max))
  13108. (if (re-search-backward "^END:VEVENT" nil t)
  13109. (setq e (match-end 0)))
  13110. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13111. (kill-buffer buf)
  13112. (delete-file tmpfile)
  13113. rtn))
  13114. (defun org-closest-date (start current change prefer show-all)
  13115. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13116. When PREFER is `past' return a date that is either CURRENT or past.
  13117. When PREFER is `future', return a date that is either CURRENT or future.
  13118. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13119. ;; Make the proper lists from the dates
  13120. (catch 'exit
  13121. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13122. dn dw sday cday n1 n2 n0
  13123. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13124. (setq start (org-date-to-gregorian start)
  13125. current (org-date-to-gregorian
  13126. (if show-all
  13127. current
  13128. (time-to-days (current-time))))
  13129. sday (calendar-absolute-from-gregorian start)
  13130. cday (calendar-absolute-from-gregorian current))
  13131. (if (<= cday sday) (throw 'exit sday))
  13132. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13133. (setq dn (string-to-number (match-string 1 change))
  13134. dw (cdr (assoc (match-string 2 change) a1)))
  13135. (error "Invalid change specifyer: %s" change))
  13136. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13137. (cond
  13138. ((eq dw 'day)
  13139. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13140. n2 (+ n1 dn)))
  13141. ((eq dw 'year)
  13142. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13143. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13144. (setq date1 (list m d y1)
  13145. n1 (calendar-absolute-from-gregorian date1)
  13146. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13147. n2 (calendar-absolute-from-gregorian date2)))
  13148. ((eq dw 'month)
  13149. ;; approx number of month between the two dates
  13150. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13151. ;; How often does dn fit in there?
  13152. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13153. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13154. m (+ m nm)
  13155. ny (floor (/ m 12))
  13156. y (+ y ny)
  13157. m (- m (* ny 12)))
  13158. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13159. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13160. (setq m2 (+ m dn) y2 y)
  13161. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13162. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13163. (while (<= n2 cday)
  13164. (setq n1 n2 m m2 y y2)
  13165. (setq m2 (+ m dn) y2 y)
  13166. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13167. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13168. ;; Make sure n1 is the earlier date
  13169. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13170. (if show-all
  13171. (cond
  13172. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13173. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13174. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13175. (cond
  13176. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13177. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13178. (t (if (= cday n1) n1 n2)))))))
  13179. (defun org-date-to-gregorian (date)
  13180. "Turn any specification of DATE into a gregorian date for the calendar."
  13181. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13182. ((and (listp date) (= (length date) 3)) date)
  13183. ((stringp date)
  13184. (setq date (org-parse-time-string date))
  13185. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13186. ((listp date)
  13187. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13188. (defun org-parse-time-string (s &optional nodefault)
  13189. "Parse the standard Org-mode time string.
  13190. This should be a lot faster than the normal `parse-time-string'.
  13191. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13192. hour and minute fields will be nil if not given."
  13193. (if (string-match org-ts-regexp0 s)
  13194. (list 0
  13195. (if (or (match-beginning 8) (not nodefault))
  13196. (string-to-number (or (match-string 8 s) "0")))
  13197. (if (or (match-beginning 7) (not nodefault))
  13198. (string-to-number (or (match-string 7 s) "0")))
  13199. (string-to-number (match-string 4 s))
  13200. (string-to-number (match-string 3 s))
  13201. (string-to-number (match-string 2 s))
  13202. nil nil nil)
  13203. (error "Not a standard Org-mode time string: %s" s)))
  13204. (defun org-timestamp-up (&optional arg)
  13205. "Increase the date item at the cursor by one.
  13206. If the cursor is on the year, change the year. If it is on the month or
  13207. the day, change that.
  13208. With prefix ARG, change by that many units."
  13209. (interactive "p")
  13210. (org-timestamp-change (prefix-numeric-value arg)))
  13211. (defun org-timestamp-down (&optional arg)
  13212. "Decrease the date item at the cursor by one.
  13213. If the cursor is on the year, change the year. If it is on the month or
  13214. the day, change that.
  13215. With prefix ARG, change by that many units."
  13216. (interactive "p")
  13217. (org-timestamp-change (- (prefix-numeric-value arg))))
  13218. (defun org-timestamp-up-day (&optional arg)
  13219. "Increase the date in the time stamp by one day.
  13220. With prefix ARG, change that many days."
  13221. (interactive "p")
  13222. (if (and (not (org-at-timestamp-p t))
  13223. (org-on-heading-p))
  13224. (org-todo 'up)
  13225. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13226. (defun org-timestamp-down-day (&optional arg)
  13227. "Decrease the date in the time stamp by one day.
  13228. With prefix ARG, change that many days."
  13229. (interactive "p")
  13230. (if (and (not (org-at-timestamp-p t))
  13231. (org-on-heading-p))
  13232. (org-todo 'down)
  13233. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13234. (defun org-at-timestamp-p (&optional inactive-ok)
  13235. "Determine if the cursor is in or at a timestamp."
  13236. (interactive)
  13237. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13238. (pos (point))
  13239. (ans (or (looking-at tsr)
  13240. (save-excursion
  13241. (skip-chars-backward "^[<\n\r\t")
  13242. (if (> (point) (point-min)) (backward-char 1))
  13243. (and (looking-at tsr)
  13244. (> (- (match-end 0) pos) -1))))))
  13245. (and ans
  13246. (boundp 'org-ts-what)
  13247. (setq org-ts-what
  13248. (cond
  13249. ((= pos (match-beginning 0)) 'bracket)
  13250. ((= pos (1- (match-end 0))) 'bracket)
  13251. ((org-pos-in-match-range pos 2) 'year)
  13252. ((org-pos-in-match-range pos 3) 'month)
  13253. ((org-pos-in-match-range pos 7) 'hour)
  13254. ((org-pos-in-match-range pos 8) 'minute)
  13255. ((or (org-pos-in-match-range pos 4)
  13256. (org-pos-in-match-range pos 5)) 'day)
  13257. ((and (> pos (or (match-end 8) (match-end 5)))
  13258. (< pos (match-end 0)))
  13259. (- pos (or (match-end 8) (match-end 5))))
  13260. (t 'day))))
  13261. ans))
  13262. (defun org-toggle-timestamp-type ()
  13263. "Toggle the type (<active> or [inactive]) of a time stamp."
  13264. (interactive)
  13265. (when (org-at-timestamp-p t)
  13266. (let ((beg (match-beginning 0)) (end (match-end 0))
  13267. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13268. (save-excursion
  13269. (goto-char beg)
  13270. (while (re-search-forward "[][<>]" end t)
  13271. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13272. t t)))
  13273. (message "Timestamp is now %sactive"
  13274. (if (equal (char-after beg) ?<) "" "in")))))
  13275. (defun org-timestamp-change (n &optional what)
  13276. "Change the date in the time stamp at point.
  13277. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13278. `year', `minute', `second'. If WHAT is not given, the cursor position
  13279. in the timestamp determines what will be changed."
  13280. (let ((pos (point))
  13281. with-hm inactive
  13282. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13283. org-ts-what
  13284. extra rem
  13285. ts time time0)
  13286. (if (not (org-at-timestamp-p t))
  13287. (error "Not at a timestamp"))
  13288. (if (and (not what) (eq org-ts-what 'bracket))
  13289. (org-toggle-timestamp-type)
  13290. (if (and (not what) (not (eq org-ts-what 'day))
  13291. org-display-custom-times
  13292. (get-text-property (point) 'display)
  13293. (not (get-text-property (1- (point)) 'display)))
  13294. (setq org-ts-what 'day))
  13295. (setq org-ts-what (or what org-ts-what)
  13296. inactive (= (char-after (match-beginning 0)) ?\[)
  13297. ts (match-string 0))
  13298. (replace-match "")
  13299. (if (string-match
  13300. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13301. ts)
  13302. (setq extra (match-string 1 ts)))
  13303. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13304. (setq with-hm t))
  13305. (setq time0 (org-parse-time-string ts))
  13306. (when (and (eq org-ts-what 'minute)
  13307. (eq current-prefix-arg nil))
  13308. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13309. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13310. (setcar (cdr time0) (+ (nth 1 time0)
  13311. (if (> n 0) (- rem) (- dm rem))))))
  13312. (setq time
  13313. (encode-time (or (car time0) 0)
  13314. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13315. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13316. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13317. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13318. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13319. (nthcdr 6 time0)))
  13320. (when (and (member org-ts-what '(hour minute))
  13321. extra
  13322. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13323. (setq extra (org-modify-ts-extra
  13324. extra
  13325. (if (eq org-ts-what 'hour) 2 5)
  13326. n dm)))
  13327. (when (integerp org-ts-what)
  13328. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13329. (if (eq what 'calendar)
  13330. (let ((cal-date (org-get-date-from-calendar)))
  13331. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13332. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13333. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13334. (setcar time0 (or (car time0) 0))
  13335. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13336. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13337. (setq time (apply 'encode-time time0))))
  13338. (setq org-last-changed-timestamp
  13339. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13340. (org-clock-update-time-maybe)
  13341. (goto-char pos)
  13342. ;; Try to recenter the calendar window, if any
  13343. (if (and org-calendar-follow-timestamp-change
  13344. (get-buffer-window "*Calendar*" t)
  13345. (memq org-ts-what '(day month year)))
  13346. (org-recenter-calendar (time-to-days time))))))
  13347. (defun org-modify-ts-extra (s pos n dm)
  13348. "Change the different parts of the lead-time and repeat fields in timestamp."
  13349. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13350. ng h m new rem)
  13351. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13352. (cond
  13353. ((or (org-pos-in-match-range pos 2)
  13354. (org-pos-in-match-range pos 3))
  13355. (setq m (string-to-number (match-string 3 s))
  13356. h (string-to-number (match-string 2 s)))
  13357. (if (org-pos-in-match-range pos 2)
  13358. (setq h (+ h n))
  13359. (setq n (* dm (org-no-warnings (signum n))))
  13360. (when (not (= 0 (setq rem (% m dm))))
  13361. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13362. (setq m (+ m n)))
  13363. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13364. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13365. (setq h (min 24 (max 0 h)))
  13366. (setq ng 1 new (format "-%02d:%02d" h m)))
  13367. ((org-pos-in-match-range pos 6)
  13368. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13369. ((org-pos-in-match-range pos 5)
  13370. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13371. ((org-pos-in-match-range pos 9)
  13372. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13373. ((org-pos-in-match-range pos 8)
  13374. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13375. (when ng
  13376. (setq s (concat
  13377. (substring s 0 (match-beginning ng))
  13378. new
  13379. (substring s (match-end ng))))))
  13380. s))
  13381. (defun org-recenter-calendar (date)
  13382. "If the calendar is visible, recenter it to DATE."
  13383. (let* ((win (selected-window))
  13384. (cwin (get-buffer-window "*Calendar*" t))
  13385. (calendar-move-hook nil))
  13386. (when cwin
  13387. (select-window cwin)
  13388. (calendar-goto-date (if (listp date) date
  13389. (calendar-gregorian-from-absolute date)))
  13390. (select-window win))))
  13391. (defun org-goto-calendar (&optional arg)
  13392. "Go to the Emacs calendar at the current date.
  13393. If there is a time stamp in the current line, go to that date.
  13394. A prefix ARG can be used to force the current date."
  13395. (interactive "P")
  13396. (let ((tsr org-ts-regexp) diff
  13397. (calendar-move-hook nil)
  13398. (calendar-view-holidays-initially-flag nil)
  13399. (calendar-view-diary-initially-flag nil))
  13400. (if (or (org-at-timestamp-p)
  13401. (save-excursion
  13402. (beginning-of-line 1)
  13403. (looking-at (concat ".*" tsr))))
  13404. (let ((d1 (time-to-days (current-time)))
  13405. (d2 (time-to-days
  13406. (org-time-string-to-time (match-string 1)))))
  13407. (setq diff (- d2 d1))))
  13408. (calendar)
  13409. (calendar-goto-today)
  13410. (if (and diff (not arg)) (calendar-forward-day diff))))
  13411. (defun org-get-date-from-calendar ()
  13412. "Return a list (month day year) of date at point in calendar."
  13413. (with-current-buffer "*Calendar*"
  13414. (save-match-data
  13415. (calendar-cursor-to-date))))
  13416. (defun org-date-from-calendar ()
  13417. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13418. If there is already a time stamp at the cursor position, update it."
  13419. (interactive)
  13420. (if (org-at-timestamp-p t)
  13421. (org-timestamp-change 0 'calendar)
  13422. (let ((cal-date (org-get-date-from-calendar)))
  13423. (org-insert-time-stamp
  13424. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13425. (defun org-minutes-to-hh:mm-string (m)
  13426. "Compute H:MM from a number of minutes."
  13427. (let ((h (/ m 60)))
  13428. (setq m (- m (* 60 h)))
  13429. (format org-time-clocksum-format h m)))
  13430. (defun org-hh:mm-string-to-minutes (s)
  13431. "Convert a string H:MM to a number of minutes.
  13432. If the string is just a number, interpret it as minutes.
  13433. In fact, the first hh:mm or number in the string will be taken,
  13434. there can be extra stuff in the string.
  13435. If no number is found, the return value is 0."
  13436. (cond
  13437. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13438. (+ (* (string-to-number (match-string 1 s)) 60)
  13439. (string-to-number (match-string 2 s))))
  13440. ((string-match "\\([0-9]+\\)" s)
  13441. (string-to-number (match-string 1 s)))
  13442. (t 0)))
  13443. ;;;; Files
  13444. (defun org-save-all-org-buffers ()
  13445. "Save all Org-mode buffers without user confirmation."
  13446. (interactive)
  13447. (message "Saving all Org-mode buffers...")
  13448. (save-some-buffers t 'org-mode-p)
  13449. (when (featurep 'org-id) (org-id-locations-save))
  13450. (message "Saving all Org-mode buffers... done"))
  13451. (defun org-revert-all-org-buffers ()
  13452. "Revert all Org-mode buffers.
  13453. Prompt for confirmation when there are unsaved changes.
  13454. Be sure you know what you are doing before letting this function
  13455. overwrite your changes.
  13456. This function is useful in a setup where one tracks org files
  13457. with a version control system, to revert on one machine after pulling
  13458. changes from another. I believe the procedure must be like this:
  13459. 1. M-x org-save-all-org-buffers
  13460. 2. Pull changes from the other machine, resolve conflicts
  13461. 3. M-x org-revert-all-org-buffers"
  13462. (interactive)
  13463. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13464. (error "Abort"))
  13465. (save-excursion
  13466. (save-window-excursion
  13467. (mapc
  13468. (lambda (b)
  13469. (when (and (with-current-buffer b (org-mode-p))
  13470. (with-current-buffer b buffer-file-name))
  13471. (switch-to-buffer b)
  13472. (revert-buffer t 'no-confirm)))
  13473. (buffer-list))
  13474. (when (and (featurep 'org-id) org-id-track-globally)
  13475. (org-id-locations-load)))))
  13476. ;;;; Agenda files
  13477. ;;;###autoload
  13478. (defun org-iswitchb (&optional arg)
  13479. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13480. With a prefix argument, restrict available to files.
  13481. With two prefix arguments, restrict available buffers to agenda files."
  13482. (interactive "P")
  13483. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13484. ((equal arg '(16)) (org-buffer-list 'agenda))
  13485. (t (org-buffer-list)))))
  13486. (switch-to-buffer
  13487. (org-icompleting-read "Org buffer: "
  13488. (mapcar 'list (mapcar 'buffer-name blist))
  13489. nil t))))
  13490. ;;;###autoload
  13491. (defalias 'org-ido-switchb 'org-iswitchb)
  13492. (defun org-buffer-list (&optional predicate exclude-tmp)
  13493. "Return a list of Org buffers.
  13494. PREDICATE can be `export', `files' or `agenda'.
  13495. export restrict the list to Export buffers.
  13496. files restrict the list to buffers visiting Org files.
  13497. agenda restrict the list to buffers visiting agenda files.
  13498. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13499. (let* ((bfn nil)
  13500. (agenda-files (and (eq predicate 'agenda)
  13501. (mapcar 'file-truename (org-agenda-files t))))
  13502. (filter
  13503. (cond
  13504. ((eq predicate 'files)
  13505. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13506. ((eq predicate 'export)
  13507. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13508. ((eq predicate 'agenda)
  13509. (lambda (b)
  13510. (with-current-buffer b
  13511. (and (eq major-mode 'org-mode)
  13512. (setq bfn (buffer-file-name b))
  13513. (member (file-truename bfn) agenda-files)))))
  13514. (t (lambda (b) (with-current-buffer b
  13515. (or (eq major-mode 'org-mode)
  13516. (string-match "\*Org .*Export"
  13517. (buffer-name b)))))))))
  13518. (delq nil
  13519. (mapcar
  13520. (lambda(b)
  13521. (if (and (funcall filter b)
  13522. (or (not exclude-tmp)
  13523. (not (string-match "tmp" (buffer-name b)))))
  13524. b
  13525. nil))
  13526. (buffer-list)))))
  13527. (defun org-agenda-files (&optional unrestricted archives)
  13528. "Get the list of agenda files.
  13529. Optional UNRESTRICTED means return the full list even if a restriction
  13530. is currently in place.
  13531. When ARCHIVES is t, include all archive files that are really being
  13532. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13533. `org-agenda-archives-mode' is t."
  13534. (let ((files
  13535. (cond
  13536. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13537. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13538. ((listp org-agenda-files) org-agenda-files)
  13539. (t (error "Invalid value of `org-agenda-files'")))))
  13540. (setq files (apply 'append
  13541. (mapcar (lambda (f)
  13542. (if (file-directory-p f)
  13543. (directory-files
  13544. f t org-agenda-file-regexp)
  13545. (list f)))
  13546. files)))
  13547. (when org-agenda-skip-unavailable-files
  13548. (setq files (delq nil
  13549. (mapcar (function
  13550. (lambda (file)
  13551. (and (file-readable-p file) file)))
  13552. files))))
  13553. (when (or (eq archives t)
  13554. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13555. (setq files (org-add-archive-files files)))
  13556. files))
  13557. (defun org-edit-agenda-file-list ()
  13558. "Edit the list of agenda files.
  13559. Depending on setup, this either uses customize to edit the variable
  13560. `org-agenda-files', or it visits the file that is holding the list. In the
  13561. latter case, the buffer is set up in a way that saving it automatically kills
  13562. the buffer and restores the previous window configuration."
  13563. (interactive)
  13564. (if (stringp org-agenda-files)
  13565. (let ((cw (current-window-configuration)))
  13566. (find-file org-agenda-files)
  13567. (org-set-local 'org-window-configuration cw)
  13568. (org-add-hook 'after-save-hook
  13569. (lambda ()
  13570. (set-window-configuration
  13571. (prog1 org-window-configuration
  13572. (kill-buffer (current-buffer))))
  13573. (org-install-agenda-files-menu)
  13574. (message "New agenda file list installed"))
  13575. nil 'local)
  13576. (message "%s" (substitute-command-keys
  13577. "Edit list and finish with \\[save-buffer]")))
  13578. (customize-variable 'org-agenda-files)))
  13579. (defun org-store-new-agenda-file-list (list)
  13580. "Set new value for the agenda file list and save it correctly."
  13581. (if (stringp org-agenda-files)
  13582. (let ((fe (org-read-agenda-file-list t)) b u)
  13583. (while (setq b (find-buffer-visiting org-agenda-files))
  13584. (kill-buffer b))
  13585. (with-temp-file org-agenda-files
  13586. (insert
  13587. (mapconcat
  13588. (lambda (f) ;; Keep un-expanded entries.
  13589. (if (setq u (assoc f fe))
  13590. (cdr u)
  13591. f))
  13592. list "\n")
  13593. "\n")))
  13594. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13595. (org-insert-mode-line-in-empty-file nil))
  13596. (setq org-agenda-files list)
  13597. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13598. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13599. "Read the list of agenda files from a file.
  13600. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13601. filenames, used by `org-store-new-agenda-file-list' to write back
  13602. un-expanded file names."
  13603. (when (file-directory-p org-agenda-files)
  13604. (error "`org-agenda-files' cannot be a single directory"))
  13605. (when (stringp org-agenda-files)
  13606. (with-temp-buffer
  13607. (insert-file-contents org-agenda-files)
  13608. (mapcar
  13609. (lambda (f)
  13610. (let ((e (expand-file-name (substitute-in-file-name f)
  13611. org-directory)))
  13612. (if pair-with-expansion
  13613. (cons e f)
  13614. e)))
  13615. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13616. ;;;###autoload
  13617. (defun org-cycle-agenda-files ()
  13618. "Cycle through the files in `org-agenda-files'.
  13619. If the current buffer visits an agenda file, find the next one in the list.
  13620. If the current buffer does not, find the first agenda file."
  13621. (interactive)
  13622. (let* ((fs (org-agenda-files t))
  13623. (files (append fs (list (car fs))))
  13624. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13625. file)
  13626. (unless files (error "No agenda files"))
  13627. (catch 'exit
  13628. (while (setq file (pop files))
  13629. (if (equal (file-truename file) tcf)
  13630. (when (car files)
  13631. (find-file (car files))
  13632. (throw 'exit t))))
  13633. (find-file (car fs)))
  13634. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13635. (defun org-agenda-file-to-front (&optional to-end)
  13636. "Move/add the current file to the top of the agenda file list.
  13637. If the file is not present in the list, it is added to the front. If it is
  13638. present, it is moved there. With optional argument TO-END, add/move to the
  13639. end of the list."
  13640. (interactive "P")
  13641. (let ((org-agenda-skip-unavailable-files nil)
  13642. (file-alist (mapcar (lambda (x)
  13643. (cons (file-truename x) x))
  13644. (org-agenda-files t)))
  13645. (ctf (file-truename buffer-file-name))
  13646. x had)
  13647. (setq x (assoc ctf file-alist) had x)
  13648. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13649. (if to-end
  13650. (setq file-alist (append (delq x file-alist) (list x)))
  13651. (setq file-alist (cons x (delq x file-alist))))
  13652. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13653. (org-install-agenda-files-menu)
  13654. (message "File %s to %s of agenda file list"
  13655. (if had "moved" "added") (if to-end "end" "front"))))
  13656. (defun org-remove-file (&optional file)
  13657. "Remove current file from the list of files in variable `org-agenda-files'.
  13658. These are the files which are being checked for agenda entries.
  13659. Optional argument FILE means use this file instead of the current."
  13660. (interactive)
  13661. (let* ((org-agenda-skip-unavailable-files nil)
  13662. (file (or file buffer-file-name))
  13663. (true-file (file-truename file))
  13664. (afile (abbreviate-file-name file))
  13665. (files (delq nil (mapcar
  13666. (lambda (x)
  13667. (if (equal true-file
  13668. (file-truename x))
  13669. nil x))
  13670. (org-agenda-files t)))))
  13671. (if (not (= (length files) (length (org-agenda-files t))))
  13672. (progn
  13673. (org-store-new-agenda-file-list files)
  13674. (org-install-agenda-files-menu)
  13675. (message "Removed file: %s" afile))
  13676. (message "File was not in list: %s (not removed)" afile))))
  13677. (defun org-file-menu-entry (file)
  13678. (vector file (list 'find-file file) t))
  13679. (defun org-check-agenda-file (file)
  13680. "Make sure FILE exists. If not, ask user what to do."
  13681. (when (not (file-exists-p file))
  13682. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13683. (abbreviate-file-name file))
  13684. (let ((r (downcase (read-char-exclusive))))
  13685. (cond
  13686. ((equal r ?r)
  13687. (org-remove-file file)
  13688. (throw 'nextfile t))
  13689. (t (error "Abort"))))))
  13690. (defun org-get-agenda-file-buffer (file)
  13691. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13692. it to the list of buffers which might be released later."
  13693. (let ((buf (org-find-base-buffer-visiting file)))
  13694. (if buf
  13695. buf ; just return it
  13696. ;; Make a new buffer and remember it
  13697. (setq buf (find-file-noselect file))
  13698. (if buf (push buf org-agenda-new-buffers))
  13699. buf)))
  13700. (defun org-release-buffers (blist)
  13701. "Release all buffers in list, asking the user for confirmation when needed.
  13702. When a buffer is unmodified, it is just killed. When modified, it is saved
  13703. \(if the user agrees) and then killed."
  13704. (let (buf file)
  13705. (while (setq buf (pop blist))
  13706. (setq file (buffer-file-name buf))
  13707. (when (and (buffer-modified-p buf)
  13708. file
  13709. (y-or-n-p (format "Save file %s? " file)))
  13710. (with-current-buffer buf (save-buffer)))
  13711. (kill-buffer buf))))
  13712. (defun org-prepare-agenda-buffers (files)
  13713. "Create buffers for all agenda files, protect archived trees and comments."
  13714. (interactive)
  13715. (let ((pa '(:org-archived t))
  13716. (pc '(:org-comment t))
  13717. (pall '(:org-archived t :org-comment t))
  13718. (inhibit-read-only t)
  13719. (rea (concat ":" org-archive-tag ":"))
  13720. bmp file re)
  13721. (save-excursion
  13722. (save-restriction
  13723. (while (setq file (pop files))
  13724. (catch 'nextfile
  13725. (if (bufferp file)
  13726. (set-buffer file)
  13727. (org-check-agenda-file file)
  13728. (set-buffer (org-get-agenda-file-buffer file)))
  13729. (widen)
  13730. (setq bmp (buffer-modified-p))
  13731. (org-refresh-category-properties)
  13732. (setq org-todo-keywords-for-agenda
  13733. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13734. (setq org-done-keywords-for-agenda
  13735. (append org-done-keywords-for-agenda org-done-keywords))
  13736. (setq org-todo-keyword-alist-for-agenda
  13737. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13738. (setq org-drawers-for-agenda
  13739. (append org-drawers-for-agenda org-drawers))
  13740. (setq org-tag-alist-for-agenda
  13741. (append org-tag-alist-for-agenda org-tag-alist))
  13742. (save-excursion
  13743. (remove-text-properties (point-min) (point-max) pall)
  13744. (when org-agenda-skip-archived-trees
  13745. (goto-char (point-min))
  13746. (while (re-search-forward rea nil t)
  13747. (if (org-on-heading-p t)
  13748. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13749. (goto-char (point-min))
  13750. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13751. (while (re-search-forward re nil t)
  13752. (add-text-properties
  13753. (match-beginning 0) (org-end-of-subtree t) pc)))
  13754. (set-buffer-modified-p bmp)))))
  13755. (setq org-todo-keywords-for-agenda
  13756. (org-uniquify org-todo-keywords-for-agenda))
  13757. (setq org-todo-keyword-alist-for-agenda
  13758. (org-uniquify org-todo-keyword-alist-for-agenda)
  13759. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13760. ;;;; Embedded LaTeX
  13761. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13762. "Keymap for the minor `org-cdlatex-mode'.")
  13763. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13764. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13765. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13766. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13767. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13768. (defvar org-cdlatex-texmathp-advice-is-done nil
  13769. "Flag remembering if we have applied the advice to texmathp already.")
  13770. (define-minor-mode org-cdlatex-mode
  13771. "Toggle the minor `org-cdlatex-mode'.
  13772. This mode supports entering LaTeX environment and math in LaTeX fragments
  13773. in Org-mode.
  13774. \\{org-cdlatex-mode-map}"
  13775. nil " OCDL" nil
  13776. (when org-cdlatex-mode (require 'cdlatex))
  13777. (unless org-cdlatex-texmathp-advice-is-done
  13778. (setq org-cdlatex-texmathp-advice-is-done t)
  13779. (defadvice texmathp (around org-math-always-on activate)
  13780. "Always return t in org-mode buffers.
  13781. This is because we want to insert math symbols without dollars even outside
  13782. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13783. an embedded LaTeX fragment, let texmathp do its job.
  13784. \\[org-cdlatex-mode-map]"
  13785. (interactive)
  13786. (let (p)
  13787. (cond
  13788. ((not (org-mode-p)) ad-do-it)
  13789. ((eq this-command 'cdlatex-math-symbol)
  13790. (setq ad-return-value t
  13791. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13792. (t
  13793. (let ((p (org-inside-LaTeX-fragment-p)))
  13794. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13795. (setq ad-return-value t
  13796. texmathp-why '("Org-mode embedded math" . 0))
  13797. (if p ad-do-it)))))))))
  13798. (defun turn-on-org-cdlatex ()
  13799. "Unconditionally turn on `org-cdlatex-mode'."
  13800. (org-cdlatex-mode 1))
  13801. (defun org-inside-LaTeX-fragment-p ()
  13802. "Test if point is inside a LaTeX fragment.
  13803. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13804. sequence appearing also before point.
  13805. Even though the matchers for math are configurable, this function assumes
  13806. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13807. delimiters are skipped when they have been removed by customization.
  13808. The return value is nil, or a cons cell with the delimiter and
  13809. and the position of this delimiter.
  13810. This function does a reasonably good job, but can locally be fooled by
  13811. for example currency specifications. For example it will assume being in
  13812. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13813. fragments that are properly closed, but during editing, we have to live
  13814. with the uncertainty caused by missing closing delimiters. This function
  13815. looks only before point, not after."
  13816. (catch 'exit
  13817. (let ((pos (point))
  13818. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13819. (lim (progn
  13820. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13821. (point)))
  13822. dd-on str (start 0) m re)
  13823. (goto-char pos)
  13824. (when dodollar
  13825. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13826. re (nth 1 (assoc "$" org-latex-regexps)))
  13827. (while (string-match re str start)
  13828. (cond
  13829. ((= (match-end 0) (length str))
  13830. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13831. ((= (match-end 0) (- (length str) 5))
  13832. (throw 'exit nil))
  13833. (t (setq start (match-end 0))))))
  13834. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13835. (goto-char pos)
  13836. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13837. (and (match-beginning 2) (throw 'exit nil))
  13838. ;; count $$
  13839. (while (re-search-backward "\\$\\$" lim t)
  13840. (setq dd-on (not dd-on)))
  13841. (goto-char pos)
  13842. (if dd-on (cons "$$" m))))))
  13843. (defun org-inside-latex-macro-p ()
  13844. "Is point inside a LaTeX macro or its arguments?"
  13845. (save-match-data
  13846. (org-in-regexp
  13847. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13848. (defun test ()
  13849. (interactive)
  13850. (message "%s" (org-inside-latex-macro-p)))
  13851. (defun org-try-cdlatex-tab ()
  13852. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13853. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13854. - inside a LaTeX fragment, or
  13855. - after the first word in a line, where an abbreviation expansion could
  13856. insert a LaTeX environment."
  13857. (when org-cdlatex-mode
  13858. (cond
  13859. ((save-excursion
  13860. (skip-chars-backward "a-zA-Z0-9*")
  13861. (skip-chars-backward " \t")
  13862. (bolp))
  13863. (cdlatex-tab) t)
  13864. ((org-inside-LaTeX-fragment-p)
  13865. (cdlatex-tab) t)
  13866. (t nil))))
  13867. (defun org-cdlatex-underscore-caret (&optional arg)
  13868. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13869. Revert to the normal definition outside of these fragments."
  13870. (interactive "P")
  13871. (if (org-inside-LaTeX-fragment-p)
  13872. (call-interactively 'cdlatex-sub-superscript)
  13873. (let (org-cdlatex-mode)
  13874. (call-interactively (key-binding (vector last-input-event))))))
  13875. (defun org-cdlatex-math-modify (&optional arg)
  13876. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13877. Revert to the normal definition outside of these fragments."
  13878. (interactive "P")
  13879. (if (org-inside-LaTeX-fragment-p)
  13880. (call-interactively 'cdlatex-math-modify)
  13881. (let (org-cdlatex-mode)
  13882. (call-interactively (key-binding (vector last-input-event))))))
  13883. (defvar org-latex-fragment-image-overlays nil
  13884. "List of overlays carrying the images of latex fragments.")
  13885. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13886. (defun org-remove-latex-fragment-image-overlays ()
  13887. "Remove all overlays with LaTeX fragment images in current buffer."
  13888. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  13889. (setq org-latex-fragment-image-overlays nil))
  13890. (defun org-preview-latex-fragment (&optional subtree)
  13891. "Preview the LaTeX fragment at point, or all locally or globally.
  13892. If the cursor is in a LaTeX fragment, create the image and overlay
  13893. it over the source code. If there is no fragment at point, display
  13894. all fragments in the current text, from one headline to the next. With
  13895. prefix SUBTREE, display all fragments in the current subtree. With a
  13896. double prefix `C-u C-u', or when the cursor is before the first headline,
  13897. display all fragments in the buffer.
  13898. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13899. (interactive "P")
  13900. (org-remove-latex-fragment-image-overlays)
  13901. (save-excursion
  13902. (save-restriction
  13903. (let (beg end at msg)
  13904. (cond
  13905. ((or (equal subtree '(16))
  13906. (not (save-excursion
  13907. (re-search-backward (concat "^" outline-regexp) nil t))))
  13908. (setq beg (point-min) end (point-max)
  13909. msg "Creating images for buffer...%s"))
  13910. ((equal subtree '(4))
  13911. (org-back-to-heading)
  13912. (setq beg (point) end (org-end-of-subtree t)
  13913. msg "Creating images for subtree...%s"))
  13914. (t
  13915. (if (setq at (org-inside-LaTeX-fragment-p))
  13916. (goto-char (max (point-min) (- (cdr at) 2)))
  13917. (org-back-to-heading))
  13918. (setq beg (point) end (progn (outline-next-heading) (point))
  13919. msg (if at "Creating image...%s"
  13920. "Creating images for entry...%s"))))
  13921. (message msg "")
  13922. (narrow-to-region beg end)
  13923. (goto-char beg)
  13924. (org-format-latex
  13925. (concat "ltxpng/" (file-name-sans-extension
  13926. (file-name-nondirectory
  13927. buffer-file-name)))
  13928. default-directory 'overlays msg at 'forbuffer)
  13929. (message msg "done. Use `C-c C-c' to remove images.")))))
  13930. (defvar org-latex-regexps
  13931. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13932. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13933. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13934. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13935. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13936. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13937. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13938. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13939. "Regular expressions for matching embedded LaTeX.")
  13940. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13941. "Replace LaTeX fragments with links to an image, and produce images.
  13942. Some of the options can be changed using the variable
  13943. `org-format-latex-options'."
  13944. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13945. (let* ((prefixnodir (file-name-nondirectory prefix))
  13946. (absprefix (expand-file-name prefix dir))
  13947. (todir (file-name-directory absprefix))
  13948. (opt org-format-latex-options)
  13949. (matchers (plist-get opt :matchers))
  13950. (re-list org-latex-regexps)
  13951. (org-format-latex-header-extra
  13952. (plist-get (org-infile-export-plist) :latex-header-extra))
  13953. (cnt 0) txt hash link beg end re e checkdir
  13954. executables-checked
  13955. m n block linkfile movefile ov)
  13956. ;; Check the different regular expressions
  13957. (while (setq e (pop re-list))
  13958. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13959. block (if (nth 3 e) "\n\n" ""))
  13960. (when (member m matchers)
  13961. (goto-char (point-min))
  13962. (while (re-search-forward re nil t)
  13963. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13964. (not (get-text-property (match-beginning n)
  13965. 'org-protected))
  13966. (or (not overlays)
  13967. (not (eq (get-char-property (match-beginning n)
  13968. 'org-overlay-type)
  13969. 'org-latex-overlay))))
  13970. (setq txt (match-string n)
  13971. beg (match-beginning n) end (match-end n)
  13972. cnt (1+ cnt))
  13973. (let (print-length print-level) ; make sure full list is printed
  13974. (setq hash (sha1 (prin1-to-string
  13975. (list org-format-latex-header
  13976. org-format-latex-header-extra
  13977. org-export-latex-default-packages-alist
  13978. org-export-latex-packages-alist
  13979. org-format-latex-options
  13980. forbuffer txt)))
  13981. linkfile (format "%s_%s.png" prefix hash)
  13982. movefile (format "%s_%s.png" absprefix hash)))
  13983. (setq link (concat block "[[file:" linkfile "]]" block))
  13984. (if msg (message msg cnt))
  13985. (goto-char beg)
  13986. (unless checkdir ; make sure the directory exists
  13987. (setq checkdir t)
  13988. (or (file-directory-p todir) (make-directory todir)))
  13989. (unless executables-checked
  13990. (org-check-external-command
  13991. "latex" "needed to convert LaTeX fragments to images")
  13992. (org-check-external-command
  13993. "dvipng" "needed to convert LaTeX fragments to images")
  13994. (setq executables-checked t))
  13995. (unless (file-exists-p movefile)
  13996. (org-create-formula-image
  13997. txt movefile opt forbuffer))
  13998. (if overlays
  13999. (progn
  14000. (mapc (lambda (o)
  14001. (if (eq (overlay-get o 'org-overlay-type)
  14002. 'org-latex-overlay)
  14003. (delete-overlay o)))
  14004. (overlays-in beg end))
  14005. (setq ov (make-overlay beg end))
  14006. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14007. (if (featurep 'xemacs)
  14008. (progn
  14009. (overlay-put ov 'invisible t)
  14010. (overlay-put
  14011. ov 'end-glyph
  14012. (make-glyph (vector 'png :file movefile))))
  14013. (overlay-put
  14014. ov 'display
  14015. (list 'image :type 'png :file movefile :ascent 'center)))
  14016. (push ov org-latex-fragment-image-overlays)
  14017. (goto-char end))
  14018. (delete-region beg end)
  14019. (insert (org-add-props link
  14020. (list 'org-latex-src
  14021. (replace-regexp-in-string "\"" "" txt)))))))))))
  14022. ;; This function borrows from Ganesh Swami's latex2png.el
  14023. (defun org-create-formula-image (string tofile options buffer)
  14024. "This calls dvipng."
  14025. (require 'org-latex)
  14026. (let* ((tmpdir (if (featurep 'xemacs)
  14027. (temp-directory)
  14028. temporary-file-directory))
  14029. (texfilebase (make-temp-name
  14030. (expand-file-name "orgtex" tmpdir)))
  14031. (texfile (concat texfilebase ".tex"))
  14032. (dvifile (concat texfilebase ".dvi"))
  14033. (pngfile (concat texfilebase ".png"))
  14034. (fnh (if (featurep 'xemacs)
  14035. (font-height (get-face-font 'default))
  14036. (face-attribute 'default :height nil)))
  14037. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14038. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14039. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14040. "Black"))
  14041. (bg (or (plist-get options (if buffer :background :html-background))
  14042. "Transparent")))
  14043. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14044. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14045. (with-temp-file texfile
  14046. (insert (org-splice-latex-header
  14047. org-format-latex-header
  14048. org-export-latex-default-packages-alist
  14049. org-export-latex-packages-alist
  14050. org-format-latex-header-extra))
  14051. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14052. (require 'org-latex)
  14053. (org-export-latex-fix-inputenc))
  14054. (let ((dir default-directory))
  14055. (condition-case nil
  14056. (progn
  14057. (cd tmpdir)
  14058. (call-process "latex" nil nil nil texfile))
  14059. (error nil))
  14060. (cd dir))
  14061. (if (not (file-exists-p dvifile))
  14062. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14063. (condition-case nil
  14064. (call-process "dvipng" nil nil nil
  14065. "-fg" fg "-bg" bg
  14066. "-D" dpi
  14067. ;;"-x" scale "-y" scale
  14068. "-T" "tight"
  14069. "-o" pngfile
  14070. dvifile)
  14071. (error nil))
  14072. (if (not (file-exists-p pngfile))
  14073. (if org-format-latex-signal-error
  14074. (error "Failed to create png file from %s" texfile)
  14075. (message "Failed to create png file from %s" texfile)
  14076. nil)
  14077. ;; Use the requested file name and clean up
  14078. (copy-file pngfile tofile 'replace)
  14079. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14080. (delete-file (concat texfilebase e)))
  14081. pngfile))))
  14082. (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
  14083. "Fill a LaTeX header template TPL.
  14084. In the template, the following place holders will be recognized:
  14085. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14086. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14087. [PACKAGES] \\usepackage statements for PKG
  14088. [NO-PACKAGES] do not include PKG
  14089. [EXTRA] the string EXTRA
  14090. [NO-EXTRA] do not include EXTRA
  14091. For backward compatibility, if both the positive and the negative place
  14092. holder is missing, the positive one (without the \"NO-\") will be
  14093. assumed to be present at the end of the template.
  14094. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14095. EXTRA is a string."
  14096. (let (rpl (end ""))
  14097. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14098. (setq rpl (if (or (match-end 1) (not def-pkg))
  14099. "" (org-latex-packages-to-string def-pkg t))
  14100. tpl (replace-match rpl t t tpl))
  14101. (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
  14102. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14103. (setq rpl (if (or (match-end 1) (not pkg))
  14104. "" (org-latex-packages-to-string pkg t))
  14105. tpl (replace-match rpl t t tpl))
  14106. (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
  14107. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14108. (setq rpl (if (or (match-end 1) (not extra))
  14109. "" (concat extra "\n"))
  14110. tpl (replace-match rpl t t tpl))
  14111. (if (and extra (string-match "\\S-" extra))
  14112. (setq end (concat end "\n" extra))))
  14113. (if (string-match "\\S-" end)
  14114. (concat tpl "\n" end)
  14115. tpl)))
  14116. (defun org-latex-packages-to-string (pkg &optional newline)
  14117. "Turn an alist of packages into a string with the \\usepackage macros."
  14118. (setq pkg (mapconcat (lambda(p)
  14119. (cond
  14120. ((stringp p) p)
  14121. ((equal "" (car p))
  14122. (format "\\usepackage{%s}" (cadr p)))
  14123. (t
  14124. (format "\\usepackage[%s]{%s}"
  14125. (car p) (cadr p)))))
  14126. pkg
  14127. "\n"))
  14128. (if newline (concat pkg "\n") pkg))
  14129. (defun org-dvipng-color (attr)
  14130. "Return an rgb color specification for dvipng."
  14131. (apply 'format "rgb %s %s %s"
  14132. (mapcar 'org-normalize-color
  14133. (color-values (face-attribute 'default attr nil)))))
  14134. (defun org-normalize-color (value)
  14135. "Return string to be used as color value for an RGB component."
  14136. (format "%g" (/ value 65535.0)))
  14137. ;;;; Key bindings
  14138. ;; Make `C-c C-x' a prefix key
  14139. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14140. ;; TAB key with modifiers
  14141. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14142. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14143. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14144. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14145. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14146. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14147. ;; The following line is necessary under Suse GNU/Linux
  14148. (unless (featurep 'xemacs)
  14149. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14150. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14151. (define-key org-mode-map [backtab] 'org-shifttab)
  14152. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14153. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14154. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14155. ;; Cursor keys with modifiers
  14156. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14157. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14158. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14159. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14160. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14161. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14162. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14163. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14164. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14165. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14166. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14167. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14168. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14169. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14170. ;;; Extra keys for tty access.
  14171. ;; We only set them when really needed because otherwise the
  14172. ;; menus don't show the simple keys
  14173. (when (or org-use-extra-keys
  14174. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14175. (not window-system))
  14176. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14177. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14178. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14179. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14180. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14181. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14182. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14183. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14184. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14185. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14186. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14187. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14188. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14189. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14190. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14191. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14192. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14193. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14194. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14195. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14196. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14197. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14198. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14199. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14200. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14201. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14202. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14203. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14204. ;; All the other keys
  14205. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14206. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14207. (if (boundp 'narrow-map)
  14208. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14209. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14210. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14211. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14212. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14213. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14214. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14215. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14216. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14217. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14218. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14219. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14220. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14221. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14222. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14223. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14224. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14225. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14226. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14227. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14228. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14229. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14230. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14231. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14232. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14233. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14234. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14235. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14236. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14237. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14238. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14239. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14240. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14241. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14242. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14243. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14244. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14245. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14246. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14247. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14248. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14249. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14250. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14251. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14252. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14253. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14254. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14255. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14256. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14257. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14258. (org-defkey org-mode-map "\C-m" 'org-return)
  14259. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14260. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14261. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14262. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14263. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14264. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14265. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14266. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14267. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14268. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14269. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14270. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14271. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14272. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14273. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14274. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14275. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14276. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14277. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14278. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14279. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14280. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14281. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14282. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14283. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14284. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14285. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14286. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14287. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14288. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14289. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14290. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14291. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14292. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14293. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14294. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14295. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14296. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14297. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14298. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14299. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14300. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14301. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14302. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14303. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14304. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14305. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14306. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14307. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14308. (when (featurep 'xemacs)
  14309. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14310. (defconst org-speed-commands-default
  14311. '(
  14312. ("Outline Navigation")
  14313. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14314. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14315. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14316. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14317. ("u" . (org-speed-move-safe 'outline-up-heading))
  14318. ("j" . org-goto)
  14319. ("g" . (org-refile t))
  14320. ("Outline Visibility")
  14321. ("c" . org-cycle)
  14322. ("C" . org-shifttab)
  14323. (" " . org-display-outline-path)
  14324. ("Outline Structure Editing")
  14325. ("U" . org-shiftmetaup)
  14326. ("D" . org-shiftmetadown)
  14327. ("r" . org-metaright)
  14328. ("l" . org-metaleft)
  14329. ("R" . org-shiftmetaright)
  14330. ("L" . org-shiftmetaleft)
  14331. ("i" . (progn (forward-char 1) (call-interactively
  14332. 'org-insert-heading-respect-content)))
  14333. ("^" . org-sort)
  14334. ("w" . org-refile)
  14335. ("a" . org-archive-subtree-default-with-confirmation)
  14336. ("." . outline-mark-subtree)
  14337. ("Clock Commands")
  14338. ("I" . org-clock-in)
  14339. ("O" . org-clock-out)
  14340. ("Meta Data Editing")
  14341. ("t" . org-todo)
  14342. ("0" . (org-priority ?\ ))
  14343. ("1" . (org-priority ?A))
  14344. ("2" . (org-priority ?B))
  14345. ("3" . (org-priority ?C))
  14346. (";" . org-set-tags-command)
  14347. ("e" . org-set-effort)
  14348. ("Agenda Views etc")
  14349. ("v" . org-agenda)
  14350. ("/" . org-sparse-tree)
  14351. ("Misc")
  14352. ("o" . org-open-at-point)
  14353. ("?" . org-speed-command-help)
  14354. )
  14355. "The default speed commands.")
  14356. (defun org-print-speed-command (e)
  14357. (if (> (length (car e)) 1)
  14358. (progn
  14359. (princ "\n")
  14360. (princ (car e))
  14361. (princ "\n")
  14362. (princ (make-string (length (car e)) ?-))
  14363. (princ "\n"))
  14364. (princ (car e))
  14365. (princ " ")
  14366. (if (symbolp (cdr e))
  14367. (princ (symbol-name (cdr e)))
  14368. (prin1 (cdr e)))
  14369. (princ "\n")))
  14370. (defun org-speed-command-help ()
  14371. "Show the available speed commands."
  14372. (interactive)
  14373. (if (not org-use-speed-commands)
  14374. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14375. (with-output-to-temp-buffer "*Help*"
  14376. (princ "User-defined Speed commands\n===========================\n")
  14377. (mapc 'org-print-speed-command org-speed-commands-user)
  14378. (princ "\n")
  14379. (princ "Built-in Speed commands\n=======================\n")
  14380. (mapc 'org-print-speed-command org-speed-commands-default))
  14381. (with-current-buffer "*Help*"
  14382. (setq truncate-lines t))))
  14383. (defun org-speed-move-safe (cmd)
  14384. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14385. If not, return to the original position and throw an error."
  14386. (interactive)
  14387. (let ((pos (point)))
  14388. (call-interactively cmd)
  14389. (unless (and (bolp) (org-on-heading-p))
  14390. (goto-char pos)
  14391. (error "Boundary reached while executing %s" cmd))))
  14392. (defvar org-self-insert-command-undo-counter 0)
  14393. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14394. (defvar org-speed-command nil)
  14395. (defun org-self-insert-command (N)
  14396. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14397. If the cursor is in a table looking at whitespace, the whitespace is
  14398. overwritten, and the table is not marked as requiring realignment."
  14399. (interactive "p")
  14400. (cond
  14401. ((and org-use-speed-commands
  14402. (or (and (bolp) (looking-at outline-regexp))
  14403. (and (functionp org-use-speed-commands)
  14404. (funcall org-use-speed-commands)))
  14405. (setq
  14406. org-speed-command
  14407. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14408. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14409. (cond
  14410. ((commandp org-speed-command)
  14411. (setq this-command org-speed-command)
  14412. (call-interactively org-speed-command))
  14413. ((functionp org-speed-command)
  14414. (funcall org-speed-command))
  14415. ((and org-speed-command (listp org-speed-command))
  14416. (eval org-speed-command))
  14417. (t (let (org-use-speed-commands)
  14418. (call-interactively 'org-self-insert-command)))))
  14419. ((and
  14420. (org-table-p)
  14421. (progn
  14422. ;; check if we blank the field, and if that triggers align
  14423. (and (featurep 'org-table) org-table-auto-blank-field
  14424. (member last-command
  14425. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14426. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14427. ;; got extra space, this field does not determine column width
  14428. (let (org-table-may-need-update) (org-table-blank-field))
  14429. ;; no extra space, this field may determine column width
  14430. (org-table-blank-field)))
  14431. t)
  14432. (eq N 1)
  14433. (looking-at "[^|\n]* |"))
  14434. (let (org-table-may-need-update)
  14435. (goto-char (1- (match-end 0)))
  14436. (delete-backward-char 1)
  14437. (goto-char (match-beginning 0))
  14438. (self-insert-command N)))
  14439. (t
  14440. (setq org-table-may-need-update t)
  14441. (self-insert-command N)
  14442. (org-fix-tags-on-the-fly)
  14443. (if org-self-insert-cluster-for-undo
  14444. (if (not (eq last-command 'org-self-insert-command))
  14445. (setq org-self-insert-command-undo-counter 1)
  14446. (if (>= org-self-insert-command-undo-counter 20)
  14447. (setq org-self-insert-command-undo-counter 1)
  14448. (and (> org-self-insert-command-undo-counter 0)
  14449. buffer-undo-list
  14450. (not (cadr buffer-undo-list)) ; remove nil entry
  14451. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14452. (setq org-self-insert-command-undo-counter
  14453. (1+ org-self-insert-command-undo-counter))))))))
  14454. (defun org-fix-tags-on-the-fly ()
  14455. (when (and (equal (char-after (point-at-bol)) ?*)
  14456. (org-on-heading-p))
  14457. (org-align-tags-here org-tags-column)))
  14458. (defun org-delete-backward-char (N)
  14459. "Like `delete-backward-char', insert whitespace at field end in tables.
  14460. When deleting backwards, in tables this function will insert whitespace in
  14461. front of the next \"|\" separator, to keep the table aligned. The table will
  14462. still be marked for re-alignment if the field did fill the entire column,
  14463. because, in this case the deletion might narrow the column."
  14464. (interactive "p")
  14465. (if (and (org-table-p)
  14466. (eq N 1)
  14467. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14468. (looking-at ".*?|"))
  14469. (let ((pos (point))
  14470. (noalign (looking-at "[^|\n\r]* |"))
  14471. (c org-table-may-need-update))
  14472. (backward-delete-char N)
  14473. (skip-chars-forward "^|")
  14474. (insert " ")
  14475. (goto-char (1- pos))
  14476. ;; noalign: if there were two spaces at the end, this field
  14477. ;; does not determine the width of the column.
  14478. (if noalign (setq org-table-may-need-update c)))
  14479. (backward-delete-char N)
  14480. (org-fix-tags-on-the-fly)))
  14481. (defun org-delete-char (N)
  14482. "Like `delete-char', but insert whitespace at field end in tables.
  14483. When deleting characters, in tables this function will insert whitespace in
  14484. front of the next \"|\" separator, to keep the table aligned. The table will
  14485. still be marked for re-alignment if the field did fill the entire column,
  14486. because, in this case the deletion might narrow the column."
  14487. (interactive "p")
  14488. (if (and (org-table-p)
  14489. (not (bolp))
  14490. (not (= (char-after) ?|))
  14491. (eq N 1))
  14492. (if (looking-at ".*?|")
  14493. (let ((pos (point))
  14494. (noalign (looking-at "[^|\n\r]* |"))
  14495. (c org-table-may-need-update))
  14496. (replace-match (concat
  14497. (substring (match-string 0) 1 -1)
  14498. " |"))
  14499. (goto-char pos)
  14500. ;; noalign: if there were two spaces at the end, this field
  14501. ;; does not determine the width of the column.
  14502. (if noalign (setq org-table-may-need-update c)))
  14503. (delete-char N))
  14504. (delete-char N)
  14505. (org-fix-tags-on-the-fly)))
  14506. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14507. (put 'org-self-insert-command 'delete-selection t)
  14508. (put 'orgtbl-self-insert-command 'delete-selection t)
  14509. (put 'org-delete-char 'delete-selection 'supersede)
  14510. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14511. (put 'org-yank 'delete-selection 'yank)
  14512. ;; Make `flyspell-mode' delay after some commands
  14513. (put 'org-self-insert-command 'flyspell-delayed t)
  14514. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14515. (put 'org-delete-char 'flyspell-delayed t)
  14516. (put 'org-delete-backward-char 'flyspell-delayed t)
  14517. ;; Make pabbrev-mode expand after org-mode commands
  14518. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14519. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14520. ;; How to do this: Measure non-white length of current string
  14521. ;; If equal to column width, we should realign.
  14522. (defun org-remap (map &rest commands)
  14523. "In MAP, remap the functions given in COMMANDS.
  14524. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14525. (let (new old)
  14526. (while commands
  14527. (setq old (pop commands) new (pop commands))
  14528. (if (fboundp 'command-remapping)
  14529. (org-defkey map (vector 'remap old) new)
  14530. (substitute-key-definition old new map global-map)))))
  14531. (when (eq org-enable-table-editor 'optimized)
  14532. ;; If the user wants maximum table support, we need to hijack
  14533. ;; some standard editing functions
  14534. (org-remap org-mode-map
  14535. 'self-insert-command 'org-self-insert-command
  14536. 'delete-char 'org-delete-char
  14537. 'delete-backward-char 'org-delete-backward-char)
  14538. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14539. (defvar org-ctrl-c-ctrl-c-hook nil
  14540. "Hook for functions attaching themselves to `C-c C-c'.
  14541. This can be used to add additional functionality to the C-c C-c key which
  14542. executes context-dependent commands.
  14543. Each function will be called with no arguments. The function must check
  14544. if the context is appropriate for it to act. If yes, it should do its
  14545. thing and then return a non-nil value. If the context is wrong,
  14546. just do nothing and return nil.")
  14547. (defvar org-tab-first-hook nil
  14548. "Hook for functions to attach themselves to TAB.
  14549. See `org-ctrl-c-ctrl-c-hook' for more information.
  14550. This hook runs as the first action when TAB is pressed, even before
  14551. `org-cycle' messes around with the `outline-regexp' to cater for
  14552. inline tasks and plain list item folding.
  14553. If any function in this hook returns t, not other actions like table
  14554. field motion visibility cycling will be done.")
  14555. (defvar org-tab-after-check-for-table-hook nil
  14556. "Hook for functions to attach themselves to TAB.
  14557. See `org-ctrl-c-ctrl-c-hook' for more information.
  14558. This hook runs after it has been established that the cursor is not in a
  14559. table, but before checking if the cursor is in a headline or if global cycling
  14560. should be done.
  14561. If any function in this hook returns t, not other actions like visibility
  14562. cycling will be done.")
  14563. (defvar org-tab-after-check-for-cycling-hook nil
  14564. "Hook for functions to attach themselves to TAB.
  14565. See `org-ctrl-c-ctrl-c-hook' for more information.
  14566. This hook runs after it has been established that not table field motion and
  14567. not visibility should be done because of current context. This is probably
  14568. the place where a package like yasnippets can hook in.")
  14569. (defvar org-tab-before-tab-emulation-hook nil
  14570. "Hook for functions to attach themselves to TAB.
  14571. See `org-ctrl-c-ctrl-c-hook' for more information.
  14572. This hook runs after every other options for TAB have been exhausted, but
  14573. before indentation and \t insertion takes place.")
  14574. (defvar org-metaleft-hook nil
  14575. "Hook for functions attaching themselves to `M-left'.
  14576. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14577. (defvar org-metaright-hook nil
  14578. "Hook for functions attaching themselves to `M-right'.
  14579. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14580. (defvar org-metaup-hook nil
  14581. "Hook for functions attaching themselves to `M-up'.
  14582. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14583. (defvar org-metadown-hook nil
  14584. "Hook for functions attaching themselves to `M-down'.
  14585. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14586. (defvar org-shiftmetaleft-hook nil
  14587. "Hook for functions attaching themselves to `M-S-left'.
  14588. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14589. (defvar org-shiftmetaright-hook nil
  14590. "Hook for functions attaching themselves to `M-S-right'.
  14591. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14592. (defvar org-shiftmetaup-hook nil
  14593. "Hook for functions attaching themselves to `M-S-up'.
  14594. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14595. (defvar org-shiftmetadown-hook nil
  14596. "Hook for functions attaching themselves to `M-S-down'.
  14597. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14598. (defvar org-metareturn-hook nil
  14599. "Hook for functions attaching themselves to `M-RET'.
  14600. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14601. (defun org-modifier-cursor-error ()
  14602. "Throw an error, a modified cursor command was applied in wrong context."
  14603. (error "This command is active in special context like tables, headlines or items"))
  14604. (defun org-shiftselect-error ()
  14605. "Throw an error because Shift-Cursor command was applied in wrong context."
  14606. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14607. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14608. (error "This command works only in special context like headlines or timestamps")))
  14609. (defun org-call-for-shift-select (cmd)
  14610. (let ((this-command-keys-shift-translated t))
  14611. (call-interactively cmd)))
  14612. (defun org-shifttab (&optional arg)
  14613. "Global visibility cycling or move to previous table field.
  14614. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14615. on context.
  14616. See the individual commands for more information."
  14617. (interactive "P")
  14618. (cond
  14619. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14620. ((integerp arg)
  14621. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14622. (message "Content view to level: %d" arg)
  14623. (org-content (prefix-numeric-value arg2))
  14624. (setq org-cycle-global-status 'overview)))
  14625. (t (call-interactively 'org-global-cycle))))
  14626. (defun org-shiftmetaleft ()
  14627. "Promote subtree or delete table column.
  14628. Calls `org-promote-subtree', `org-outdent-item',
  14629. or `org-table-delete-column', depending on context.
  14630. See the individual commands for more information."
  14631. (interactive)
  14632. (cond
  14633. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14634. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14635. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14636. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14637. (t (org-modifier-cursor-error))))
  14638. (defun org-shiftmetaright ()
  14639. "Demote subtree or insert table column.
  14640. Calls `org-demote-subtree', `org-indent-item',
  14641. or `org-table-insert-column', depending on context.
  14642. See the individual commands for more information."
  14643. (interactive)
  14644. (cond
  14645. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14646. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14647. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14648. ((org-at-item-p) (call-interactively 'org-indent-item))
  14649. (t (org-modifier-cursor-error))))
  14650. (defun org-shiftmetaup (&optional arg)
  14651. "Move subtree up or kill table row.
  14652. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14653. `org-move-item-up' depending on context. See the individual commands
  14654. for more information."
  14655. (interactive "P")
  14656. (cond
  14657. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14658. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14659. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14660. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14661. (t (org-modifier-cursor-error))))
  14662. (defun org-shiftmetadown (&optional arg)
  14663. "Move subtree down or insert table row.
  14664. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14665. `org-move-item-down', depending on context. See the individual
  14666. commands for more information."
  14667. (interactive "P")
  14668. (cond
  14669. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14670. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14671. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14672. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14673. (t (org-modifier-cursor-error))))
  14674. (defun org-metaleft (&optional arg)
  14675. "Promote heading or move table column to left.
  14676. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14677. With no specific context, calls the Emacs default `backward-word'.
  14678. See the individual commands for more information."
  14679. (interactive "P")
  14680. (cond
  14681. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14682. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14683. ((or (org-on-heading-p)
  14684. (and (org-region-active-p)
  14685. (save-excursion
  14686. (goto-char (region-beginning))
  14687. (org-on-heading-p))))
  14688. (call-interactively 'org-do-promote))
  14689. ((or (org-at-item-p)
  14690. (and (org-region-active-p)
  14691. (save-excursion
  14692. (goto-char (region-beginning))
  14693. (org-at-item-p))))
  14694. (call-interactively 'org-outdent-item))
  14695. (t (call-interactively 'backward-word))))
  14696. (defun org-metaright (&optional arg)
  14697. "Demote subtree or move table column to right.
  14698. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14699. With no specific context, calls the Emacs default `forward-word'.
  14700. See the individual commands for more information."
  14701. (interactive "P")
  14702. (cond
  14703. ((run-hook-with-args-until-success 'org-metaright-hook))
  14704. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14705. ((or (org-on-heading-p)
  14706. (and (org-region-active-p)
  14707. (save-excursion
  14708. (goto-char (region-beginning))
  14709. (org-on-heading-p))))
  14710. (call-interactively 'org-do-demote))
  14711. ((or (org-at-item-p)
  14712. (and (org-region-active-p)
  14713. (save-excursion
  14714. (goto-char (region-beginning))
  14715. (org-at-item-p))))
  14716. (call-interactively 'org-indent-item))
  14717. (t (call-interactively 'forward-word))))
  14718. (defun org-metaup (&optional arg)
  14719. "Move subtree up or move table row up.
  14720. Calls `org-move-subtree-up' or `org-table-move-row' or
  14721. `org-move-item-up', depending on context. See the individual commands
  14722. for more information."
  14723. (interactive "P")
  14724. (cond
  14725. ((run-hook-with-args-until-success 'org-metaup-hook))
  14726. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14727. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14728. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14729. (t (transpose-lines 1) (beginning-of-line -1))))
  14730. (defun org-metadown (&optional arg)
  14731. "Move subtree down or move table row down.
  14732. Calls `org-move-subtree-down' or `org-table-move-row' or
  14733. `org-move-item-down', depending on context. See the individual
  14734. commands for more information."
  14735. (interactive "P")
  14736. (cond
  14737. ((run-hook-with-args-until-success 'org-metadown-hook))
  14738. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14739. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14740. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14741. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14742. (defun org-shiftup (&optional arg)
  14743. "Increase item in timestamp or increase priority of current headline.
  14744. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14745. depending on context. See the individual commands for more information."
  14746. (interactive "P")
  14747. (cond
  14748. ((and org-support-shift-select (org-region-active-p))
  14749. (org-call-for-shift-select 'previous-line))
  14750. ((org-at-timestamp-p t)
  14751. (call-interactively (if org-edit-timestamp-down-means-later
  14752. 'org-timestamp-down 'org-timestamp-up)))
  14753. ((and (not (eq org-support-shift-select 'always))
  14754. org-enable-priority-commands
  14755. (org-on-heading-p))
  14756. (call-interactively 'org-priority-up))
  14757. ((and (not org-support-shift-select) (org-at-item-p))
  14758. (call-interactively 'org-previous-item))
  14759. ((org-clocktable-try-shift 'up arg))
  14760. (org-support-shift-select
  14761. (org-call-for-shift-select 'previous-line))
  14762. (t (org-shiftselect-error))))
  14763. (defun org-shiftdown (&optional arg)
  14764. "Decrease item in timestamp or decrease priority of current headline.
  14765. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14766. depending on context. See the individual commands for more information."
  14767. (interactive "P")
  14768. (cond
  14769. ((and org-support-shift-select (org-region-active-p))
  14770. (org-call-for-shift-select 'next-line))
  14771. ((org-at-timestamp-p t)
  14772. (call-interactively (if org-edit-timestamp-down-means-later
  14773. 'org-timestamp-up 'org-timestamp-down)))
  14774. ((and (not (eq org-support-shift-select 'always))
  14775. org-enable-priority-commands
  14776. (org-on-heading-p))
  14777. (call-interactively 'org-priority-down))
  14778. ((and (not org-support-shift-select) (org-at-item-p))
  14779. (call-interactively 'org-next-item))
  14780. ((org-clocktable-try-shift 'down arg))
  14781. (org-support-shift-select
  14782. (org-call-for-shift-select 'next-line))
  14783. (t (org-shiftselect-error))))
  14784. (defun org-shiftright (&optional arg)
  14785. "Cycle the thing at point or in the current line, depending on context.
  14786. Depending on context, this does one of the following:
  14787. - switch a timestamp at point one day into the future
  14788. - on a headline, switch to the next TODO keyword.
  14789. - on an item, switch entire list to the next bullet type
  14790. - on a property line, switch to the next allowed value
  14791. - on a clocktable definition line, move time block into the future"
  14792. (interactive "P")
  14793. (cond
  14794. ((and org-support-shift-select (org-region-active-p))
  14795. (org-call-for-shift-select 'forward-char))
  14796. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14797. ((and (not (eq org-support-shift-select 'always))
  14798. (org-on-heading-p))
  14799. (let ((org-inhibit-logging
  14800. (not org-treat-S-cursor-todo-selection-as-state-change))
  14801. (org-inhibit-blocking
  14802. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14803. (org-call-with-arg 'org-todo 'right)))
  14804. ((or (and org-support-shift-select
  14805. (not (eq org-support-shift-select 'always))
  14806. (org-at-item-bullet-p))
  14807. (and (not org-support-shift-select) (org-at-item-p)))
  14808. (org-call-with-arg 'org-cycle-list-bullet nil))
  14809. ((and (not (eq org-support-shift-select 'always))
  14810. (org-at-property-p))
  14811. (call-interactively 'org-property-next-allowed-value))
  14812. ((org-clocktable-try-shift 'right arg))
  14813. (org-support-shift-select
  14814. (org-call-for-shift-select 'forward-char))
  14815. (t (org-shiftselect-error))))
  14816. (defun org-shiftleft (&optional arg)
  14817. "Cycle the thing at point or in the current line, depending on context.
  14818. Depending on context, this does one of the following:
  14819. - switch a timestamp at point one day into the past
  14820. - on a headline, switch to the previous TODO keyword.
  14821. - on an item, switch entire list to the previous bullet type
  14822. - on a property line, switch to the previous allowed value
  14823. - on a clocktable definition line, move time block into the past"
  14824. (interactive "P")
  14825. (cond
  14826. ((and org-support-shift-select (org-region-active-p))
  14827. (org-call-for-shift-select 'backward-char))
  14828. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14829. ((and (not (eq org-support-shift-select 'always))
  14830. (org-on-heading-p))
  14831. (let ((org-inhibit-logging
  14832. (not org-treat-S-cursor-todo-selection-as-state-change))
  14833. (org-inhibit-blocking
  14834. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14835. (org-call-with-arg 'org-todo 'left)))
  14836. ((or (and org-support-shift-select
  14837. (not (eq org-support-shift-select 'always))
  14838. (org-at-item-bullet-p))
  14839. (and (not org-support-shift-select) (org-at-item-p)))
  14840. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14841. ((and (not (eq org-support-shift-select 'always))
  14842. (org-at-property-p))
  14843. (call-interactively 'org-property-previous-allowed-value))
  14844. ((org-clocktable-try-shift 'left arg))
  14845. (org-support-shift-select
  14846. (org-call-for-shift-select 'backward-char))
  14847. (t (org-shiftselect-error))))
  14848. (defun org-shiftcontrolright ()
  14849. "Switch to next TODO set."
  14850. (interactive)
  14851. (cond
  14852. ((and org-support-shift-select (org-region-active-p))
  14853. (org-call-for-shift-select 'forward-word))
  14854. ((and (not (eq org-support-shift-select 'always))
  14855. (org-on-heading-p))
  14856. (org-call-with-arg 'org-todo 'nextset))
  14857. (org-support-shift-select
  14858. (org-call-for-shift-select 'forward-word))
  14859. (t (org-shiftselect-error))))
  14860. (defun org-shiftcontrolleft ()
  14861. "Switch to previous TODO set."
  14862. (interactive)
  14863. (cond
  14864. ((and org-support-shift-select (org-region-active-p))
  14865. (org-call-for-shift-select 'backward-word))
  14866. ((and (not (eq org-support-shift-select 'always))
  14867. (org-on-heading-p))
  14868. (org-call-with-arg 'org-todo 'previousset))
  14869. (org-support-shift-select
  14870. (org-call-for-shift-select 'backward-word))
  14871. (t (org-shiftselect-error))))
  14872. (defun org-ctrl-c-ret ()
  14873. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14874. (interactive)
  14875. (cond
  14876. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14877. (t (call-interactively 'org-insert-heading))))
  14878. (defun org-copy-special ()
  14879. "Copy region in table or copy current subtree.
  14880. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14881. See the individual commands for more information."
  14882. (interactive)
  14883. (call-interactively
  14884. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14885. (defun org-cut-special ()
  14886. "Cut region in table or cut current subtree.
  14887. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14888. See the individual commands for more information."
  14889. (interactive)
  14890. (call-interactively
  14891. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14892. (defun org-paste-special (arg)
  14893. "Paste rectangular region into table, or past subtree relative to level.
  14894. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14895. See the individual commands for more information."
  14896. (interactive "P")
  14897. (if (org-at-table-p)
  14898. (org-table-paste-rectangle)
  14899. (org-paste-subtree arg)))
  14900. (defun org-edit-special ()
  14901. "Call a special editor for the stuff at point.
  14902. When at a table, call the formula editor with `org-table-edit-formulas'.
  14903. When at the first line of an src example, call `org-edit-src-code'.
  14904. When in an #+include line, visit the include file. Otherwise call
  14905. `ffap' to visit the file at point."
  14906. (interactive)
  14907. (cond
  14908. ((org-at-table.el-p)
  14909. (org-edit-src-code))
  14910. ((org-at-table-p)
  14911. (call-interactively 'org-table-edit-formulas))
  14912. ((save-excursion
  14913. (beginning-of-line 1)
  14914. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14915. (find-file (org-trim (match-string 1))))
  14916. ((org-edit-src-code))
  14917. ((org-edit-fixed-width-region))
  14918. (t (call-interactively 'ffap))))
  14919. (defun org-ctrl-c-ctrl-c (&optional arg)
  14920. "Set tags in headline, or update according to changed information at point.
  14921. This command does many different things, depending on context:
  14922. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14923. this is what we do.
  14924. - If the cursor is on a statistics cookie, update it.
  14925. - If the cursor is in a headline, prompt for tags and insert them
  14926. into the current line, aligned to `org-tags-column'. When called
  14927. with prefix arg, realign all tags in the current buffer.
  14928. - If the cursor is in one of the special #+KEYWORD lines, this
  14929. triggers scanning the buffer for these lines and updating the
  14930. information.
  14931. - If the cursor is inside a table, realign the table. This command
  14932. works even if the automatic table editor has been turned off.
  14933. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14934. the entire table.
  14935. - If the cursor is at a footnote reference or definition, jump to
  14936. the corresponding definition or references, respectively.
  14937. - If the cursor is a the beginning of a dynamic block, update it.
  14938. - If the current buffer is a remember buffer, close note and file
  14939. it. A prefix argument of 1 files to the default location
  14940. without further interaction. A prefix argument of 2 files to
  14941. the currently clocking task.
  14942. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14943. links in this buffer.
  14944. - If the cursor is on a numbered item in a plain list, renumber the
  14945. ordered list.
  14946. - If the cursor is on a checkbox, toggle it."
  14947. (interactive "P")
  14948. (let ((org-enable-table-editor t))
  14949. (cond
  14950. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14951. org-occur-highlights
  14952. org-latex-fragment-image-overlays)
  14953. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14954. (org-remove-occur-highlights)
  14955. (org-remove-latex-fragment-image-overlays)
  14956. (message "Temporary highlights/overlays removed from current buffer"))
  14957. ((and (local-variable-p 'org-finish-function (current-buffer))
  14958. (fboundp org-finish-function))
  14959. (funcall org-finish-function))
  14960. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14961. ((or (looking-at org-property-start-re)
  14962. (org-at-property-p))
  14963. (call-interactively 'org-property-action))
  14964. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14965. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14966. (or (org-on-heading-p) (org-at-item-p)))
  14967. (call-interactively 'org-update-statistics-cookies))
  14968. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14969. ((org-at-table.el-p)
  14970. (message "Use C-c ' to edit table.el tables"))
  14971. ((org-at-table-p)
  14972. (org-table-maybe-eval-formula)
  14973. (if arg
  14974. (call-interactively 'org-table-recalculate)
  14975. (org-table-maybe-recalculate-line))
  14976. (call-interactively 'org-table-align))
  14977. ((or (org-footnote-at-reference-p)
  14978. (org-footnote-at-definition-p))
  14979. (call-interactively 'org-footnote-action))
  14980. ((org-at-item-checkbox-p)
  14981. (call-interactively 'org-toggle-checkbox))
  14982. ((org-at-item-p)
  14983. (if arg
  14984. (call-interactively 'org-toggle-checkbox)
  14985. (call-interactively 'org-maybe-renumber-ordered-list)))
  14986. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14987. ;; Dynamic block
  14988. (beginning-of-line 1)
  14989. (save-excursion (org-update-dblock)))
  14990. ((save-excursion
  14991. (beginning-of-line 1)
  14992. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14993. (cond
  14994. ((equal (match-string 1) "TBLFM")
  14995. ;; Recalculate the table before this line
  14996. (save-excursion
  14997. (beginning-of-line 1)
  14998. (skip-chars-backward " \r\n\t")
  14999. (if (org-at-table-p)
  15000. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15001. (t
  15002. (let ((org-inhibit-startup-visibility-stuff t)
  15003. (org-startup-align-all-tables nil))
  15004. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15005. (message "Local setup has been refreshed"))))
  15006. ((org-clock-update-time-maybe))
  15007. (t (error "C-c C-c can do nothing useful at this location")))))
  15008. (defun org-mode-restart ()
  15009. "Restart Org-mode, to scan again for special lines.
  15010. Also updates the keyword regular expressions."
  15011. (interactive)
  15012. (org-mode)
  15013. (message "Org-mode restarted"))
  15014. (defun org-kill-note-or-show-branches ()
  15015. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15016. (interactive)
  15017. (if (not org-finish-function)
  15018. (call-interactively 'show-branches)
  15019. (let ((org-note-abort t))
  15020. (funcall org-finish-function))))
  15021. (defun org-return (&optional indent)
  15022. "Goto next table row or insert a newline.
  15023. Calls `org-table-next-row' or `newline', depending on context.
  15024. See the individual commands for more information."
  15025. (interactive)
  15026. (cond
  15027. ((bobp) (if indent (newline-and-indent) (newline)))
  15028. ((org-at-table-p)
  15029. (org-table-justify-field-maybe)
  15030. (call-interactively 'org-table-next-row))
  15031. ((and org-return-follows-link
  15032. (eq (get-text-property (point) 'face) 'org-link))
  15033. (call-interactively 'org-open-at-point))
  15034. ((and (org-at-heading-p)
  15035. (looking-at
  15036. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15037. (org-show-entry)
  15038. (end-of-line 1)
  15039. (newline))
  15040. (t (if indent (newline-and-indent) (newline)))))
  15041. (defun org-return-indent ()
  15042. "Goto next table row or insert a newline and indent.
  15043. Calls `org-table-next-row' or `newline-and-indent', depending on
  15044. context. See the individual commands for more information."
  15045. (interactive)
  15046. (org-return t))
  15047. (defun org-ctrl-c-star ()
  15048. "Compute table, or change heading status of lines.
  15049. Calls `org-table-recalculate' or `org-toggle-heading',
  15050. depending on context."
  15051. (interactive)
  15052. (cond
  15053. ((org-at-table-p)
  15054. (call-interactively 'org-table-recalculate))
  15055. (t
  15056. ;; Convert all lines in region to list items
  15057. (call-interactively 'org-toggle-heading))))
  15058. (defun org-ctrl-c-minus ()
  15059. "Insert separator line in table or modify bullet status of line.
  15060. Also turns a plain line or a region of lines into list items.
  15061. Calls `org-table-insert-hline', `org-toggle-item', or
  15062. `org-cycle-list-bullet', depending on context."
  15063. (interactive)
  15064. (cond
  15065. ((org-at-table-p)
  15066. (call-interactively 'org-table-insert-hline))
  15067. ((org-region-active-p)
  15068. (call-interactively 'org-toggle-item))
  15069. ((org-in-item-p)
  15070. (call-interactively 'org-cycle-list-bullet))
  15071. (t
  15072. (call-interactively 'org-toggle-item))))
  15073. (defun org-toggle-item ()
  15074. "Convert headings or normal lines to items, items to normal lines.
  15075. If there is no active region, only the current line is considered.
  15076. If the first line in the region is a headline, convert all headlines to items.
  15077. If the first line in the region is an item, convert all items to normal lines.
  15078. If the first line is normal text, add an item bullet to each line."
  15079. (interactive)
  15080. (let (l2 l beg end)
  15081. (if (org-region-active-p)
  15082. (setq beg (region-beginning) end (region-end))
  15083. (setq beg (point-at-bol)
  15084. end (min (1+ (point-at-eol)) (point-max))))
  15085. (save-excursion
  15086. (goto-char end)
  15087. (setq l2 (org-current-line))
  15088. (goto-char beg)
  15089. (beginning-of-line 1)
  15090. (setq l (1- (org-current-line)))
  15091. (if (org-at-item-p)
  15092. ;; We already have items, de-itemize
  15093. (while (< (setq l (1+ l)) l2)
  15094. (when (org-at-item-p)
  15095. (goto-char (match-beginning 2))
  15096. (delete-region (match-beginning 2) (match-end 2))
  15097. (and (looking-at "[ \t]+") (replace-match "")))
  15098. (beginning-of-line 2))
  15099. (if (org-on-heading-p)
  15100. ;; Headings, convert to items
  15101. (while (< (setq l (1+ l)) l2)
  15102. (if (looking-at org-outline-regexp)
  15103. (replace-match "- " t t))
  15104. (beginning-of-line 2))
  15105. ;; normal lines, turn them into items
  15106. (while (< (setq l (1+ l)) l2)
  15107. (unless (org-at-item-p)
  15108. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15109. (replace-match "\\1- \\2")))
  15110. (beginning-of-line 2)))))))
  15111. (defun org-toggle-heading (&optional nstars)
  15112. "Convert headings to normal text, or items or text to headings.
  15113. If there is no active region, only the current line is considered.
  15114. If the first line is a heading, remove the stars from all headlines
  15115. in the region.
  15116. If the first line is a plain list item, turn all plain list items
  15117. into headings.
  15118. If the first line is a normal line, turn each and every line in the
  15119. region into a heading.
  15120. When converting a line into a heading, the number of stars is chosen
  15121. such that the lines become children of the current entry. However,
  15122. when a prefix argument is given, its value determines the number of
  15123. stars to add."
  15124. (interactive "P")
  15125. (let (l2 l itemp beg end)
  15126. (if (org-region-active-p)
  15127. (setq beg (region-beginning) end (region-end))
  15128. (setq beg (point-at-bol)
  15129. end (min (1+ (point-at-eol)) (point-max))))
  15130. (save-excursion
  15131. (goto-char end)
  15132. (setq l2 (org-current-line))
  15133. (goto-char beg)
  15134. (beginning-of-line 1)
  15135. (setq l (1- (org-current-line)))
  15136. (if (org-on-heading-p)
  15137. ;; We already have headlines, de-star them
  15138. (while (< (setq l (1+ l)) l2)
  15139. (when (org-on-heading-p t)
  15140. (and (looking-at outline-regexp) (replace-match "")))
  15141. (beginning-of-line 2))
  15142. (setq itemp (org-at-item-p))
  15143. (let* ((stars
  15144. (if nstars
  15145. (make-string (prefix-numeric-value current-prefix-arg)
  15146. ?*)
  15147. (save-excursion
  15148. (if (re-search-backward org-complex-heading-regexp nil t)
  15149. (match-string 1) ""))))
  15150. (add-stars (cond (nstars "")
  15151. ((equal stars "") "*")
  15152. (org-odd-levels-only "**")
  15153. (t "*")))
  15154. (rpl (concat stars add-stars " ")))
  15155. (while (< (setq l (1+ l)) l2)
  15156. (if itemp
  15157. (and (org-at-item-p) (replace-match rpl t t))
  15158. (unless (org-on-heading-p)
  15159. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15160. (replace-match (concat rpl (match-string 2))))))
  15161. (beginning-of-line 2)))))))
  15162. (defun org-meta-return (&optional arg)
  15163. "Insert a new heading or wrap a region in a table.
  15164. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15165. See the individual commands for more information."
  15166. (interactive "P")
  15167. (cond
  15168. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15169. ((org-at-table-p)
  15170. (call-interactively 'org-table-wrap-region))
  15171. (t (call-interactively 'org-insert-heading))))
  15172. ;;; Menu entries
  15173. ;; Define the Org-mode menus
  15174. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15175. '("Tbl"
  15176. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15177. ["Next Field" org-cycle (org-at-table-p)]
  15178. ["Previous Field" org-shifttab (org-at-table-p)]
  15179. ["Next Row" org-return (org-at-table-p)]
  15180. "--"
  15181. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15182. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15183. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15184. "--"
  15185. ("Column"
  15186. ["Move Column Left" org-metaleft (org-at-table-p)]
  15187. ["Move Column Right" org-metaright (org-at-table-p)]
  15188. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15189. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15190. ("Row"
  15191. ["Move Row Up" org-metaup (org-at-table-p)]
  15192. ["Move Row Down" org-metadown (org-at-table-p)]
  15193. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15194. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15195. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15196. "--"
  15197. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15198. ("Rectangle"
  15199. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15200. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15201. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15202. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15203. "--"
  15204. ("Calculate"
  15205. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15206. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15207. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15208. "--"
  15209. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15210. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15211. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15212. "--"
  15213. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15214. "--"
  15215. ["Sum Column/Rectangle" org-table-sum
  15216. (or (org-at-table-p) (org-region-active-p))]
  15217. ["Which Column?" org-table-current-column (org-at-table-p)])
  15218. ["Debug Formulas"
  15219. org-table-toggle-formula-debugger
  15220. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15221. ["Show Col/Row Numbers"
  15222. org-table-toggle-coordinate-overlays
  15223. :style toggle
  15224. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15225. "--"
  15226. ["Create" org-table-create (and (not (org-at-table-p))
  15227. org-enable-table-editor)]
  15228. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15229. ["Import from File" org-table-import (not (org-at-table-p))]
  15230. ["Export to File" org-table-export (org-at-table-p)]
  15231. "--"
  15232. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15233. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15234. '("Org"
  15235. ("Show/Hide"
  15236. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15237. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15238. ["Sparse Tree..." org-sparse-tree t]
  15239. ["Reveal Context" org-reveal t]
  15240. ["Show All" show-all t]
  15241. "--"
  15242. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15243. "--"
  15244. ["New Heading" org-insert-heading t]
  15245. ("Navigate Headings"
  15246. ["Up" outline-up-heading t]
  15247. ["Next" outline-next-visible-heading t]
  15248. ["Previous" outline-previous-visible-heading t]
  15249. ["Next Same Level" outline-forward-same-level t]
  15250. ["Previous Same Level" outline-backward-same-level t]
  15251. "--"
  15252. ["Jump" org-goto t])
  15253. ("Edit Structure"
  15254. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15255. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15256. "--"
  15257. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15258. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15259. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15260. "--"
  15261. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15262. "--"
  15263. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15264. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15265. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15266. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15267. "--"
  15268. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15269. "--"
  15270. ["Convert to odd levels" org-convert-to-odd-levels t]
  15271. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15272. ("Editing"
  15273. ["Emphasis..." org-emphasize t]
  15274. ["Edit Source Example" org-edit-special t]
  15275. "--"
  15276. ["Footnote new/jump" org-footnote-action t]
  15277. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15278. ("Archive"
  15279. ["Archive (default method)" org-archive-subtree-default t]
  15280. "--"
  15281. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15282. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15283. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15284. )
  15285. "--"
  15286. ("Hyperlinks"
  15287. ["Store Link (Global)" org-store-link t]
  15288. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15289. ["Insert Link" org-insert-link t]
  15290. ["Follow Link" org-open-at-point t]
  15291. "--"
  15292. ["Next link" org-next-link t]
  15293. ["Previous link" org-previous-link t]
  15294. "--"
  15295. ["Descriptive Links"
  15296. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15297. :style radio
  15298. :selected (member '(org-link) buffer-invisibility-spec)]
  15299. ["Literal Links"
  15300. (progn
  15301. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15302. :style radio
  15303. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15304. "--"
  15305. ("TODO Lists"
  15306. ["TODO/DONE/-" org-todo t]
  15307. ("Select keyword"
  15308. ["Next keyword" org-shiftright (org-on-heading-p)]
  15309. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15310. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15311. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15312. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15313. ["Show TODO Tree" org-show-todo-tree t]
  15314. ["Global TODO list" org-todo-list t]
  15315. "--"
  15316. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15317. :selected org-enforce-todo-dependencies :style toggle :active t]
  15318. "Settings for tree at point"
  15319. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15320. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15321. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15322. ["Do Children parallel" org-toggle-ordered-property :style radio
  15323. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15324. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15325. "--"
  15326. ["Set Priority" org-priority t]
  15327. ["Priority Up" org-shiftup t]
  15328. ["Priority Down" org-shiftdown t]
  15329. "--"
  15330. ["Get news from all feeds" org-feed-update-all t]
  15331. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15332. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15333. ("TAGS and Properties"
  15334. ["Set Tags" org-set-tags-command t]
  15335. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15336. "--"
  15337. ["Set property" org-set-property t]
  15338. ["Column view of properties" org-columns t]
  15339. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15340. ("Dates and Scheduling"
  15341. ["Timestamp" org-time-stamp t]
  15342. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15343. ("Change Date"
  15344. ["1 Day Later" org-shiftright t]
  15345. ["1 Day Earlier" org-shiftleft t]
  15346. ["1 ... Later" org-shiftup t]
  15347. ["1 ... Earlier" org-shiftdown t])
  15348. ["Compute Time Range" org-evaluate-time-range t]
  15349. ["Schedule Item" org-schedule t]
  15350. ["Deadline" org-deadline t]
  15351. "--"
  15352. ["Custom time format" org-toggle-time-stamp-overlays
  15353. :style radio :selected org-display-custom-times]
  15354. "--"
  15355. ["Goto Calendar" org-goto-calendar t]
  15356. ["Date from Calendar" org-date-from-calendar t]
  15357. "--"
  15358. ["Start/Restart Timer" org-timer-start t]
  15359. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15360. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15361. ["Insert Timer String" org-timer t]
  15362. ["Insert Timer Item" org-timer-item t])
  15363. ("Logging work"
  15364. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15365. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15366. ["Clock out" org-clock-out t]
  15367. ["Clock cancel" org-clock-cancel t]
  15368. "--"
  15369. ["Mark as default task" org-clock-mark-default-task t]
  15370. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15371. ["Goto running clock" org-clock-goto t]
  15372. "--"
  15373. ["Display times" org-clock-display t]
  15374. ["Create clock table" org-clock-report t]
  15375. "--"
  15376. ["Record DONE time"
  15377. (progn (setq org-log-done (not org-log-done))
  15378. (message "Switching to %s will %s record a timestamp"
  15379. (car org-done-keywords)
  15380. (if org-log-done "automatically" "not")))
  15381. :style toggle :selected org-log-done])
  15382. "--"
  15383. ["Agenda Command..." org-agenda t]
  15384. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15385. ("File List for Agenda")
  15386. ("Special views current file"
  15387. ["TODO Tree" org-show-todo-tree t]
  15388. ["Check Deadlines" org-check-deadlines t]
  15389. ["Timeline" org-timeline t]
  15390. ["Tags/Property tree" org-match-sparse-tree t])
  15391. "--"
  15392. ["Export/Publish..." org-export t]
  15393. ("LaTeX"
  15394. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15395. :selected org-cdlatex-mode]
  15396. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15397. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15398. ["Modify math symbol" org-cdlatex-math-modify
  15399. (org-inside-LaTeX-fragment-p)]
  15400. ["Insert citation" org-reftex-citation t]
  15401. "--"
  15402. ["Export LaTeX fragments as images"
  15403. (if (featurep 'org-exp)
  15404. (setq org-export-with-LaTeX-fragments
  15405. (not org-export-with-LaTeX-fragments))
  15406. (require 'org-exp))
  15407. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15408. org-export-with-LaTeX-fragments)]
  15409. "--"
  15410. ["Template for BEAMER" org-beamer-settings-template t])
  15411. "--"
  15412. ("MobileOrg"
  15413. ["Push Files and Views" org-mobile-push t]
  15414. ["Get Captured and Flagged" org-mobile-pull t]
  15415. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15416. "--"
  15417. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15418. "--"
  15419. ("Documentation"
  15420. ["Show Version" org-version t]
  15421. ["Info Documentation" org-info t])
  15422. ("Customize"
  15423. ["Browse Org Group" org-customize t]
  15424. "--"
  15425. ["Expand This Menu" org-create-customize-menu
  15426. (fboundp 'customize-menu-create)])
  15427. ["Send bug report" org-submit-bug-report t]
  15428. "--"
  15429. ("Refresh/Reload"
  15430. ["Refresh setup current buffer" org-mode-restart t]
  15431. ["Reload Org (after update)" org-reload t]
  15432. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15433. ))
  15434. (defun org-info (&optional node)
  15435. "Read documentation for Org-mode in the info system.
  15436. With optional NODE, go directly to that node."
  15437. (interactive)
  15438. (info (format "(org)%s" (or node ""))))
  15439. ;;;###autoload
  15440. (defun org-submit-bug-report ()
  15441. "Submit a bug report on Org-mode via mail.
  15442. Don't hesitate to report any problems or inaccurate documentation.
  15443. If you don't have setup sending mail from (X)Emacs, please copy the
  15444. output buffer into your mail program, as it gives us important
  15445. information about your Org-mode version and configuration."
  15446. (interactive)
  15447. (require 'reporter)
  15448. (org-load-modules-maybe)
  15449. (org-require-autoloaded-modules)
  15450. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15451. (reporter-submit-bug-report
  15452. "emacs-orgmode@gnu.org"
  15453. (org-version)
  15454. (let (list)
  15455. (save-window-excursion
  15456. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15457. (delete-other-windows)
  15458. (erase-buffer)
  15459. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15460. We would like to add your full Org-mode and Outline configuration to the
  15461. bug report. This greatly simplifies the work of the maintainer and
  15462. other experts on the mailing list.
  15463. HOWEVER, some variables you have customized may contain private
  15464. information. The names of customers, colleagues, or friends, might
  15465. appear in the form of file names, tags, todo states, or search strings.
  15466. If you answer yes to the prompt, you might want to check and remove
  15467. such private information before sending the email.")
  15468. (add-text-properties (point-min) (point-max) '(face org-warning))
  15469. (when (yes-or-no-p "Include your Org-mode configuration ")
  15470. (mapatoms
  15471. (lambda (v)
  15472. (and (boundp v)
  15473. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15474. (or (and (symbol-value v)
  15475. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15476. (and
  15477. (get v 'custom-type) (get v 'standard-value)
  15478. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15479. (push v list)))))
  15480. (kill-buffer (get-buffer "*Warn about privacy*"))
  15481. list))
  15482. nil nil
  15483. "Remember to cover the basics, that is, what you expected to happen and
  15484. what in fact did happen. You don't know how to make a good report? See
  15485. http://orgmode.org/manual/Feedback.html#Feedback
  15486. Your bug report will be posted to the Org-mode mailing list.
  15487. ------------------------------------------------------------------------")
  15488. (save-excursion
  15489. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15490. (replace-match "\\1Bug: \\3 [\\2]")))))
  15491. (defun org-install-agenda-files-menu ()
  15492. (let ((bl (buffer-list)))
  15493. (save-excursion
  15494. (while bl
  15495. (set-buffer (pop bl))
  15496. (if (org-mode-p) (setq bl nil)))
  15497. (when (org-mode-p)
  15498. (easy-menu-change
  15499. '("Org") "File List for Agenda"
  15500. (append
  15501. (list
  15502. ["Edit File List" (org-edit-agenda-file-list) t]
  15503. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15504. ["Remove Current File from List" org-remove-file t]
  15505. ["Cycle through agenda files" org-cycle-agenda-files t]
  15506. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15507. "--")
  15508. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15509. ;;;; Documentation
  15510. ;;;###autoload
  15511. (defun org-require-autoloaded-modules ()
  15512. (interactive)
  15513. (mapc 'require
  15514. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15515. org-docbook org-exp org-html org-icalendar
  15516. org-id org-latex
  15517. org-publish org-remember org-table
  15518. org-timer org-xoxo)))
  15519. ;;;###autoload
  15520. (defun org-reload (&optional uncompiled)
  15521. "Reload all org lisp files.
  15522. With prefix arg UNCOMPILED, load the uncompiled versions."
  15523. (interactive "P")
  15524. (require 'find-func)
  15525. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15526. (dir-org (file-name-directory (org-find-library-name "org")))
  15527. (dir-org-contrib (ignore-errors
  15528. (file-name-directory
  15529. (org-find-library-name "org-contribdir"))))
  15530. (files
  15531. (append (directory-files dir-org t file-re)
  15532. (and dir-org-contrib
  15533. (directory-files dir-org-contrib t file-re))))
  15534. (remove-re (concat (if (featurep 'xemacs)
  15535. "org-colview" "org-colview-xemacs")
  15536. "\\'")))
  15537. (setq files (mapcar 'file-name-sans-extension files))
  15538. (setq files (mapcar
  15539. (lambda (x) (if (string-match remove-re x) nil x))
  15540. files))
  15541. (setq files (delq nil files))
  15542. (mapc
  15543. (lambda (f)
  15544. (when (featurep (intern (file-name-nondirectory f)))
  15545. (if (and (not uncompiled)
  15546. (file-exists-p (concat f ".elc")))
  15547. (load (concat f ".elc") nil nil t)
  15548. (load (concat f ".el") nil nil t))))
  15549. files))
  15550. (org-version))
  15551. ;;;###autoload
  15552. (defun org-customize ()
  15553. "Call the customize function with org as argument."
  15554. (interactive)
  15555. (org-load-modules-maybe)
  15556. (org-require-autoloaded-modules)
  15557. (customize-browse 'org))
  15558. (defun org-create-customize-menu ()
  15559. "Create a full customization menu for Org-mode, insert it into the menu."
  15560. (interactive)
  15561. (org-load-modules-maybe)
  15562. (org-require-autoloaded-modules)
  15563. (if (fboundp 'customize-menu-create)
  15564. (progn
  15565. (easy-menu-change
  15566. '("Org") "Customize"
  15567. `(["Browse Org group" org-customize t]
  15568. "--"
  15569. ,(customize-menu-create 'org)
  15570. ["Set" Custom-set t]
  15571. ["Save" Custom-save t]
  15572. ["Reset to Current" Custom-reset-current t]
  15573. ["Reset to Saved" Custom-reset-saved t]
  15574. ["Reset to Standard Settings" Custom-reset-standard t]))
  15575. (message "\"Org\"-menu now contains full customization menu"))
  15576. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15577. ;;;; Miscellaneous stuff
  15578. ;;; Generally useful functions
  15579. (defun org-get-at-bol (property)
  15580. "Get text property PROPERTY at beginning of line."
  15581. (get-text-property (point-at-bol) property))
  15582. (defun org-find-text-property-in-string (prop s)
  15583. "Return the first non-nil value of property PROP in string S."
  15584. (or (get-text-property 0 prop s)
  15585. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15586. prop s)))
  15587. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15588. "Display the given MESSAGE as a warning."
  15589. (if (fboundp 'display-warning)
  15590. (display-warning 'org message
  15591. (if (featurep 'xemacs)
  15592. 'warning
  15593. :warning))
  15594. (let ((buf (get-buffer-create "*Org warnings*")))
  15595. (with-current-buffer buf
  15596. (goto-char (point-max))
  15597. (insert "Warning (Org): " message)
  15598. (unless (bolp)
  15599. (newline)))
  15600. (display-buffer buf)
  15601. (sit-for 0))))
  15602. (defun org-in-commented-line ()
  15603. "Is point in a line starting with `#'?"
  15604. (equal (char-after (point-at-bol)) ?#))
  15605. (defun org-in-verbatim-emphasis ()
  15606. (save-match-data
  15607. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15608. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15609. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15610. (if (and marker (marker-buffer marker)
  15611. (buffer-live-p (marker-buffer marker)))
  15612. (progn
  15613. (switch-to-buffer (marker-buffer marker))
  15614. (if (or (> marker (point-max)) (< marker (point-min)))
  15615. (widen))
  15616. (goto-char marker)
  15617. (org-show-context 'org-goto))
  15618. (if bookmark
  15619. (bookmark-jump bookmark)
  15620. (error "Cannot find location"))))
  15621. (defun org-quote-csv-field (s)
  15622. "Quote field for inclusion in CSV material."
  15623. (if (string-match "[\",]" s)
  15624. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15625. s))
  15626. (defun org-plist-delete (plist property)
  15627. "Delete PROPERTY from PLIST.
  15628. This is in contrast to merely setting it to 0."
  15629. (let (p)
  15630. (while plist
  15631. (if (not (eq property (car plist)))
  15632. (setq p (plist-put p (car plist) (nth 1 plist))))
  15633. (setq plist (cddr plist)))
  15634. p))
  15635. (defun org-force-self-insert (N)
  15636. "Needed to enforce self-insert under remapping."
  15637. (interactive "p")
  15638. (self-insert-command N))
  15639. (defun org-string-width (s)
  15640. "Compute width of string, ignoring invisible characters.
  15641. This ignores character with invisibility property `org-link', and also
  15642. characters with property `org-cwidth', because these will become invisible
  15643. upon the next fontification round."
  15644. (let (b l)
  15645. (when (or (eq t buffer-invisibility-spec)
  15646. (assq 'org-link buffer-invisibility-spec))
  15647. (while (setq b (text-property-any 0 (length s)
  15648. 'invisible 'org-link s))
  15649. (setq s (concat (substring s 0 b)
  15650. (substring s (or (next-single-property-change
  15651. b 'invisible s) (length s)))))))
  15652. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15653. (setq s (concat (substring s 0 b)
  15654. (substring s (or (next-single-property-change
  15655. b 'org-cwidth s) (length s))))))
  15656. (setq l (string-width s) b -1)
  15657. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15658. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15659. l))
  15660. (defun org-get-indentation (&optional line)
  15661. "Get the indentation of the current line, interpreting tabs.
  15662. When LINE is given, assume it represents a line and compute its indentation."
  15663. (if line
  15664. (if (string-match "^ *" (org-remove-tabs line))
  15665. (match-end 0))
  15666. (save-excursion
  15667. (beginning-of-line 1)
  15668. (skip-chars-forward " \t")
  15669. (current-column))))
  15670. (defun org-remove-tabs (s &optional width)
  15671. "Replace tabulators in S with spaces.
  15672. Assumes that s is a single line, starting in column 0."
  15673. (setq width (or width tab-width))
  15674. (while (string-match "\t" s)
  15675. (setq s (replace-match
  15676. (make-string
  15677. (- (* width (/ (+ (match-beginning 0) width) width))
  15678. (match-beginning 0)) ?\ )
  15679. t t s)))
  15680. s)
  15681. (defun org-fix-indentation (line ind)
  15682. "Fix indentation in LINE.
  15683. IND is a cons cell with target and minimum indentation.
  15684. If the current indentation in LINE is smaller than the minimum,
  15685. leave it alone. If it is larger than ind, set it to the target."
  15686. (let* ((l (org-remove-tabs line))
  15687. (i (org-get-indentation l))
  15688. (i1 (car ind)) (i2 (cdr ind)))
  15689. (if (>= i i2) (setq l (substring line i2)))
  15690. (if (> i1 0)
  15691. (concat (make-string i1 ?\ ) l)
  15692. l)))
  15693. (defun org-remove-indentation (code &optional n)
  15694. "Remove the maximum common indentation from the lines in CODE.
  15695. N may optionally be the number of spaces to remove."
  15696. (with-temp-buffer
  15697. (insert code)
  15698. (org-do-remove-indentation n)
  15699. (buffer-string)))
  15700. (defun org-do-remove-indentation (&optional n)
  15701. "Remove the maximum common indentation from the buffer."
  15702. (untabify (point-min) (point-max))
  15703. (let ((min 10000) re)
  15704. (if n
  15705. (setq min n)
  15706. (goto-char (point-min))
  15707. (while (re-search-forward "^ *[^ \n]" nil t)
  15708. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15709. (unless (or (= min 0) (= min 10000))
  15710. (setq re (format "^ \\{%d\\}" min))
  15711. (goto-char (point-min))
  15712. (while (re-search-forward re nil t)
  15713. (replace-match "")
  15714. (end-of-line 1))
  15715. min)))
  15716. (defun org-fill-template (template alist)
  15717. "Find each %key of ALIST in TEMPLATE and replace it."
  15718. (let ((case-fold-search nil)
  15719. entry key value)
  15720. (setq alist (sort (copy-sequence alist)
  15721. (lambda (a b) (< (length (car a)) (length (car b))))))
  15722. (while (setq entry (pop alist))
  15723. (setq template
  15724. (replace-regexp-in-string
  15725. (concat "%" (regexp-quote (car entry)))
  15726. (cdr entry) template t t)))
  15727. template))
  15728. (defun org-base-buffer (buffer)
  15729. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15730. (if (not buffer)
  15731. buffer
  15732. (or (buffer-base-buffer buffer)
  15733. buffer)))
  15734. (defun org-trim (s)
  15735. "Remove whitespace at beginning and end of string."
  15736. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15737. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15738. s)
  15739. (defun org-wrap (string &optional width lines)
  15740. "Wrap string to either a number of lines, or a width in characters.
  15741. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15742. that costs. If there is a word longer than WIDTH, the text is actually
  15743. wrapped to the length of that word.
  15744. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15745. many lines, whatever width that takes.
  15746. The return value is a list of lines, without newlines at the end."
  15747. (let* ((words (org-split-string string "[ \t\n]+"))
  15748. (maxword (apply 'max (mapcar 'org-string-width words)))
  15749. w ll)
  15750. (cond (width
  15751. (org-do-wrap words (max maxword width)))
  15752. (lines
  15753. (setq w maxword)
  15754. (setq ll (org-do-wrap words maxword))
  15755. (if (<= (length ll) lines)
  15756. ll
  15757. (setq ll words)
  15758. (while (> (length ll) lines)
  15759. (setq w (1+ w))
  15760. (setq ll (org-do-wrap words w)))
  15761. ll))
  15762. (t (error "Cannot wrap this")))))
  15763. (defun org-do-wrap (words width)
  15764. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15765. (let (lines line)
  15766. (while words
  15767. (setq line (pop words))
  15768. (while (and words (< (+ (length line) (length (car words))) width))
  15769. (setq line (concat line " " (pop words))))
  15770. (setq lines (push line lines)))
  15771. (nreverse lines)))
  15772. (defun org-split-string (string &optional separators)
  15773. "Splits STRING into substrings at SEPARATORS.
  15774. No empty strings are returned if there are matches at the beginning
  15775. and end of string."
  15776. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15777. (start 0)
  15778. notfirst
  15779. (list nil))
  15780. (while (and (string-match rexp string
  15781. (if (and notfirst
  15782. (= start (match-beginning 0))
  15783. (< start (length string)))
  15784. (1+ start) start))
  15785. (< (match-beginning 0) (length string)))
  15786. (setq notfirst t)
  15787. (or (eq (match-beginning 0) 0)
  15788. (and (eq (match-beginning 0) (match-end 0))
  15789. (eq (match-beginning 0) start))
  15790. (setq list
  15791. (cons (substring string start (match-beginning 0))
  15792. list)))
  15793. (setq start (match-end 0)))
  15794. (or (eq start (length string))
  15795. (setq list
  15796. (cons (substring string start)
  15797. list)))
  15798. (nreverse list)))
  15799. (defun org-quote-vert (s)
  15800. "Replace \"|\" with \"\\vert\"."
  15801. (while (string-match "|" s)
  15802. (setq s (replace-match "\\vert" t t s)))
  15803. s)
  15804. (defun org-uuidgen-p (s)
  15805. "Is S an ID created by UUIDGEN?"
  15806. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15807. (defun org-context ()
  15808. "Return a list of contexts of the current cursor position.
  15809. If several contexts apply, all are returned.
  15810. Each context entry is a list with a symbol naming the context, and
  15811. two positions indicating start and end of the context. Possible
  15812. contexts are:
  15813. :headline anywhere in a headline
  15814. :headline-stars on the leading stars in a headline
  15815. :todo-keyword on a TODO keyword (including DONE) in a headline
  15816. :tags on the TAGS in a headline
  15817. :priority on the priority cookie in a headline
  15818. :item on the first line of a plain list item
  15819. :item-bullet on the bullet/number of a plain list item
  15820. :checkbox on the checkbox in a plain list item
  15821. :table in an org-mode table
  15822. :table-special on a special filed in a table
  15823. :table-table in a table.el table
  15824. :link on a hyperlink
  15825. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15826. :target on a <<target>>
  15827. :radio-target on a <<<radio-target>>>
  15828. :latex-fragment on a LaTeX fragment
  15829. :latex-preview on a LaTeX fragment with overlayed preview image
  15830. This function expects the position to be visible because it uses font-lock
  15831. faces as a help to recognize the following contexts: :table-special, :link,
  15832. and :keyword."
  15833. (let* ((f (get-text-property (point) 'face))
  15834. (faces (if (listp f) f (list f)))
  15835. (p (point)) clist o)
  15836. ;; First the large context
  15837. (cond
  15838. ((org-on-heading-p t)
  15839. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15840. (when (progn
  15841. (beginning-of-line 1)
  15842. (looking-at org-todo-line-tags-regexp))
  15843. (push (org-point-in-group p 1 :headline-stars) clist)
  15844. (push (org-point-in-group p 2 :todo-keyword) clist)
  15845. (push (org-point-in-group p 4 :tags) clist))
  15846. (goto-char p)
  15847. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15848. (if (looking-at "\\[#[A-Z0-9]\\]")
  15849. (push (org-point-in-group p 0 :priority) clist)))
  15850. ((org-at-item-p)
  15851. (push (org-point-in-group p 2 :item-bullet) clist)
  15852. (push (list :item (point-at-bol)
  15853. (save-excursion (org-end-of-item) (point)))
  15854. clist)
  15855. (and (org-at-item-checkbox-p)
  15856. (push (org-point-in-group p 0 :checkbox) clist)))
  15857. ((org-at-table-p)
  15858. (push (list :table (org-table-begin) (org-table-end)) clist)
  15859. (if (memq 'org-formula faces)
  15860. (push (list :table-special
  15861. (previous-single-property-change p 'face)
  15862. (next-single-property-change p 'face)) clist)))
  15863. ((org-at-table-p 'any)
  15864. (push (list :table-table) clist)))
  15865. (goto-char p)
  15866. ;; Now the small context
  15867. (cond
  15868. ((org-at-timestamp-p)
  15869. (push (org-point-in-group p 0 :timestamp) clist))
  15870. ((memq 'org-link faces)
  15871. (push (list :link
  15872. (previous-single-property-change p 'face)
  15873. (next-single-property-change p 'face)) clist))
  15874. ((memq 'org-special-keyword faces)
  15875. (push (list :keyword
  15876. (previous-single-property-change p 'face)
  15877. (next-single-property-change p 'face)) clist))
  15878. ((org-on-target-p)
  15879. (push (org-point-in-group p 0 :target) clist)
  15880. (goto-char (1- (match-beginning 0)))
  15881. (if (looking-at org-radio-target-regexp)
  15882. (push (org-point-in-group p 0 :radio-target) clist))
  15883. (goto-char p))
  15884. ((setq o (car (delq nil
  15885. (mapcar
  15886. (lambda (x)
  15887. (if (memq x org-latex-fragment-image-overlays) x))
  15888. (overlays-at (point))))))
  15889. (push (list :latex-fragment
  15890. (overlay-start o) (overlay-end o)) clist)
  15891. (push (list :latex-preview
  15892. (overlay-start o) (overlay-end o)) clist))
  15893. ((org-inside-LaTeX-fragment-p)
  15894. ;; FIXME: positions wrong.
  15895. (push (list :latex-fragment (point) (point)) clist)))
  15896. (setq clist (nreverse (delq nil clist)))
  15897. clist))
  15898. ;; FIXME: Compare with at-regexp-p Do we need both?
  15899. (defun org-in-regexp (re &optional nlines visually)
  15900. "Check if point is inside a match of regexp.
  15901. Normally only the current line is checked, but you can include NLINES extra
  15902. lines both before and after point into the search.
  15903. If VISUALLY is set, require that the cursor is not after the match but
  15904. really on, so that the block visually is on the match."
  15905. (catch 'exit
  15906. (let ((pos (point))
  15907. (eol (point-at-eol (+ 1 (or nlines 0))))
  15908. (inc (if visually 1 0)))
  15909. (save-excursion
  15910. (beginning-of-line (- 1 (or nlines 0)))
  15911. (while (re-search-forward re eol t)
  15912. (if (and (<= (match-beginning 0) pos)
  15913. (>= (+ inc (match-end 0)) pos))
  15914. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15915. (defun org-at-regexp-p (regexp)
  15916. "Is point inside a match of REGEXP in the current line?"
  15917. (catch 'exit
  15918. (save-excursion
  15919. (let ((pos (point)) (end (point-at-eol)))
  15920. (beginning-of-line 1)
  15921. (while (re-search-forward regexp end t)
  15922. (if (and (<= (match-beginning 0) pos)
  15923. (>= (match-end 0) pos))
  15924. (throw 'exit t)))
  15925. nil))))
  15926. (defun org-in-regexps-block-p (start-re end-re)
  15927. "Returns t if the current point is between matches of START-RE and END-RE.
  15928. This will also return to if point is on one of the two matches."
  15929. (interactive)
  15930. (let ((p (point)))
  15931. (save-excursion
  15932. (and (or (org-at-regexp-p start-re)
  15933. (re-search-backward start-re nil t))
  15934. (re-search-forward end-re nil t)
  15935. (>= (point) p)))))
  15936. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15937. "Call `multi-occur' with buffers for all agenda files."
  15938. (interactive "sOrg-files matching: \np")
  15939. (let* ((files (org-agenda-files))
  15940. (tnames (mapcar 'file-truename files))
  15941. (extra org-agenda-text-search-extra-files)
  15942. f)
  15943. (when (eq (car extra) 'agenda-archives)
  15944. (setq extra (cdr extra))
  15945. (setq files (org-add-archive-files files)))
  15946. (while (setq f (pop extra))
  15947. (unless (member (file-truename f) tnames)
  15948. (add-to-list 'files f 'append)
  15949. (add-to-list 'tnames (file-truename f) 'append)))
  15950. (multi-occur
  15951. (mapcar (lambda (x)
  15952. (with-current-buffer
  15953. (or (get-file-buffer x) (find-file-noselect x))
  15954. (widen)
  15955. (current-buffer)))
  15956. files)
  15957. regexp)))
  15958. (if (boundp 'occur-mode-find-occurrence-hook)
  15959. ;; Emacs 23
  15960. (add-hook 'occur-mode-find-occurrence-hook
  15961. (lambda ()
  15962. (when (org-mode-p)
  15963. (org-reveal))))
  15964. ;; Emacs 22
  15965. (defadvice occur-mode-goto-occurrence
  15966. (after org-occur-reveal activate)
  15967. (and (org-mode-p) (org-reveal)))
  15968. (defadvice occur-mode-goto-occurrence-other-window
  15969. (after org-occur-reveal activate)
  15970. (and (org-mode-p) (org-reveal)))
  15971. (defadvice occur-mode-display-occurrence
  15972. (after org-occur-reveal activate)
  15973. (when (org-mode-p)
  15974. (let ((pos (occur-mode-find-occurrence)))
  15975. (with-current-buffer (marker-buffer pos)
  15976. (save-excursion
  15977. (goto-char pos)
  15978. (org-reveal)))))))
  15979. (defun org-occur-link-in-agenda-files ()
  15980. "Create a link and search for it in the agendas.
  15981. The link is not stored in `org-stored-links', it is just created
  15982. for the search purpose."
  15983. (interactive)
  15984. (let ((link (condition-case nil
  15985. (org-store-link nil)
  15986. (error "Unable to create a link to here"))))
  15987. (org-occur-in-agenda-files (regexp-quote link))))
  15988. (defun org-uniquify (list)
  15989. "Remove duplicate elements from LIST."
  15990. (let (res)
  15991. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15992. res))
  15993. (defun org-delete-all (elts list)
  15994. "Remove all elements in ELTS from LIST."
  15995. (while elts
  15996. (setq list (delete (pop elts) list)))
  15997. list)
  15998. (defun org-remove-if (predicate seq)
  15999. "Remove everything from SEQ that fulfills PREDICATE."
  16000. (let (res e)
  16001. (while seq
  16002. (setq e (pop seq))
  16003. (if (not (funcall predicate e)) (push e res)))
  16004. (nreverse res)))
  16005. (defun org-remove-if-not (predicate seq)
  16006. "Remove everything from SEQ that does not fulfill PREDICATE."
  16007. (let (res e)
  16008. (while seq
  16009. (setq e (pop seq))
  16010. (if (funcall predicate e) (push e res)))
  16011. (nreverse res)))
  16012. (defun org-back-over-empty-lines ()
  16013. "Move backwards over whitespace, to the beginning of the first empty line.
  16014. Returns the number of empty lines passed."
  16015. (let ((pos (point)))
  16016. (skip-chars-backward " \t\n\r")
  16017. (beginning-of-line 2)
  16018. (goto-char (min (point) pos))
  16019. (count-lines (point) pos)))
  16020. (defun org-skip-whitespace ()
  16021. (skip-chars-forward " \t\n\r"))
  16022. (defun org-point-in-group (point group &optional context)
  16023. "Check if POINT is in match-group GROUP.
  16024. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16025. match. If the match group does ot exist or point is not inside it,
  16026. return nil."
  16027. (and (match-beginning group)
  16028. (>= point (match-beginning group))
  16029. (<= point (match-end group))
  16030. (if context
  16031. (list context (match-beginning group) (match-end group))
  16032. t)))
  16033. (defun org-switch-to-buffer-other-window (&rest args)
  16034. "Switch to buffer in a second window on the current frame.
  16035. In particular, do not allow pop-up frames."
  16036. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16037. special-display-function)
  16038. (apply 'switch-to-buffer-other-window args)))
  16039. (defun org-combine-plists (&rest plists)
  16040. "Create a single property list from all plists in PLISTS.
  16041. The process starts by copying the first list, and then setting properties
  16042. from the other lists. Settings in the last list are the most significant
  16043. ones and overrule settings in the other lists."
  16044. (let ((rtn (copy-sequence (pop plists)))
  16045. p v ls)
  16046. (while plists
  16047. (setq ls (pop plists))
  16048. (while ls
  16049. (setq p (pop ls) v (pop ls))
  16050. (setq rtn (plist-put rtn p v))))
  16051. rtn))
  16052. (defun org-move-line-down (arg)
  16053. "Move the current line down. With prefix argument, move it past ARG lines."
  16054. (interactive "p")
  16055. (let ((col (current-column))
  16056. beg end pos)
  16057. (beginning-of-line 1) (setq beg (point))
  16058. (beginning-of-line 2) (setq end (point))
  16059. (beginning-of-line (+ 1 arg))
  16060. (setq pos (move-marker (make-marker) (point)))
  16061. (insert (delete-and-extract-region beg end))
  16062. (goto-char pos)
  16063. (org-move-to-column col)))
  16064. (defun org-move-line-up (arg)
  16065. "Move the current line up. With prefix argument, move it past ARG lines."
  16066. (interactive "p")
  16067. (let ((col (current-column))
  16068. beg end pos)
  16069. (beginning-of-line 1) (setq beg (point))
  16070. (beginning-of-line 2) (setq end (point))
  16071. (beginning-of-line (- arg))
  16072. (setq pos (move-marker (make-marker) (point)))
  16073. (insert (delete-and-extract-region beg end))
  16074. (goto-char pos)
  16075. (org-move-to-column col)))
  16076. (defun org-replace-escapes (string table)
  16077. "Replace %-escapes in STRING with values in TABLE.
  16078. TABLE is an association list with keys like \"%a\" and string values.
  16079. The sequences in STRING may contain normal field width and padding information,
  16080. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16081. so values can contain further %-escapes if they are define later in TABLE."
  16082. (let ((case-fold-search nil)
  16083. e re rpl)
  16084. (while (setq e (pop table))
  16085. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16086. (while (string-match re string)
  16087. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16088. (cdr e)))
  16089. (setq string (replace-match rpl t t string))))
  16090. string))
  16091. (defun org-sublist (list start end)
  16092. "Return a section of LIST, from START to END.
  16093. Counting starts at 1."
  16094. (let (rtn (c start))
  16095. (setq list (nthcdr (1- start) list))
  16096. (while (and list (<= c end))
  16097. (push (pop list) rtn)
  16098. (setq c (1+ c)))
  16099. (nreverse rtn)))
  16100. (defun org-find-base-buffer-visiting (file)
  16101. "Like `find-buffer-visiting' but always return the base buffer and
  16102. not an indirect buffer."
  16103. (let ((buf (or (get-file-buffer file)
  16104. (find-buffer-visiting file))))
  16105. (if buf
  16106. (or (buffer-base-buffer buf) buf)
  16107. nil)))
  16108. (defun org-image-file-name-regexp (&optional extensions)
  16109. "Return regexp matching the file names of images.
  16110. If EXTENSIONS is given, only match these."
  16111. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16112. (image-file-name-regexp)
  16113. (let ((image-file-name-extensions
  16114. (or extensions
  16115. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16116. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16117. (concat "\\."
  16118. (regexp-opt (nconc (mapcar 'upcase
  16119. image-file-name-extensions)
  16120. image-file-name-extensions)
  16121. t)
  16122. "\\'"))))
  16123. (defun org-file-image-p (file &optional extensions)
  16124. "Return non-nil if FILE is an image."
  16125. (save-match-data
  16126. (string-match (org-image-file-name-regexp extensions) file)))
  16127. (defun org-get-cursor-date ()
  16128. "Return the date at cursor in as a time.
  16129. This works in the calendar and in the agenda, anywhere else it just
  16130. returns the current time."
  16131. (let (date day defd)
  16132. (cond
  16133. ((eq major-mode 'calendar-mode)
  16134. (setq date (calendar-cursor-to-date)
  16135. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16136. ((eq major-mode 'org-agenda-mode)
  16137. (setq day (get-text-property (point) 'day))
  16138. (if day
  16139. (setq date (calendar-gregorian-from-absolute day)
  16140. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16141. (nth 2 date))))))
  16142. (or defd (current-time))))
  16143. (defvar org-agenda-action-marker (make-marker)
  16144. "Marker pointing to the entry for the next agenda action.")
  16145. (defun org-mark-entry-for-agenda-action ()
  16146. "Mark the current entry as target of an agenda action.
  16147. Agenda actions are actions executed from the agenda with the key `k',
  16148. which make use of the date at the cursor."
  16149. (interactive)
  16150. (move-marker org-agenda-action-marker
  16151. (save-excursion (org-back-to-heading t) (point))
  16152. (current-buffer))
  16153. (message
  16154. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16155. ;;; Paragraph filling stuff.
  16156. ;; We want this to be just right, so use the full arsenal.
  16157. (defun org-indent-line-function ()
  16158. "Indent line like previous, but further if previous was headline or item."
  16159. (interactive)
  16160. (let* ((pos (point))
  16161. (itemp (org-at-item-p))
  16162. (case-fold-search t)
  16163. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16164. column bpos bcol tpos tcol bullet btype bullet-type)
  16165. ;; Find the previous relevant line
  16166. (beginning-of-line 1)
  16167. (cond
  16168. ((looking-at "#") (setq column 0))
  16169. ((looking-at "\\*+ ") (setq column 0))
  16170. ((and (looking-at "[ \t]*:END:")
  16171. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16172. (save-excursion
  16173. (goto-char (1- (match-beginning 1)))
  16174. (setq column (current-column))))
  16175. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16176. (save-excursion
  16177. (re-search-backward
  16178. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16179. (setq column (org-get-indentation (match-string 0))))
  16180. (t
  16181. (beginning-of-line 0)
  16182. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16183. (not (looking-at "[ \t]*:END:"))
  16184. (not (looking-at org-drawer-regexp)))
  16185. (beginning-of-line 0))
  16186. (cond
  16187. ((looking-at "\\*+[ \t]+")
  16188. (if (not org-adapt-indentation)
  16189. (setq column 0)
  16190. (goto-char (match-end 0))
  16191. (setq column (current-column))))
  16192. ((looking-at org-drawer-regexp)
  16193. (goto-char (1- (match-beginning 1)))
  16194. (setq column (current-column)))
  16195. ((looking-at "\\([ \t]*\\):END:")
  16196. (goto-char (match-end 1))
  16197. (setq column (current-column)))
  16198. ((org-in-item-p)
  16199. (org-beginning-of-item)
  16200. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16201. (setq bpos (match-beginning 1) tpos (match-end 0)
  16202. bcol (progn (goto-char bpos) (current-column))
  16203. tcol (progn (goto-char tpos) (current-column))
  16204. bullet (match-string 1)
  16205. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16206. (if (> tcol (+ bcol org-description-max-indent))
  16207. (setq tcol (+ bcol 5)))
  16208. (if (not itemp)
  16209. (setq column tcol)
  16210. (goto-char pos)
  16211. (beginning-of-line 1)
  16212. (if (looking-at "\\S-")
  16213. (progn
  16214. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16215. (setq bullet (match-string 1)
  16216. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16217. (setq column (if (equal btype bullet-type) bcol tcol)))
  16218. (setq column (org-get-indentation)))))
  16219. (t (setq column (org-get-indentation))))))
  16220. (goto-char pos)
  16221. (if (<= (current-column) (current-indentation))
  16222. (org-indent-line-to column)
  16223. (save-excursion (org-indent-line-to column)))
  16224. (setq column (current-column))
  16225. (beginning-of-line 1)
  16226. (if (looking-at
  16227. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16228. (replace-match (concat (match-string 1)
  16229. (format org-property-format
  16230. (match-string 2) (match-string 3)))
  16231. t t))
  16232. (org-move-to-column column)))
  16233. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16234. "Variable to store copy of `adaptive-fill-regexp'.
  16235. Since `adaptive-fill-regexp' is set to never match, we need to
  16236. store a backup of its value before entering `org-mode' so that
  16237. the functionality can be provided as a fall-back.")
  16238. (defun org-set-autofill-regexps ()
  16239. (interactive)
  16240. ;; In the paragraph separator we include headlines, because filling
  16241. ;; text in a line directly attached to a headline would otherwise
  16242. ;; fill the headline as well.
  16243. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16244. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16245. ;; The paragraph starter includes hand-formatted lists.
  16246. (org-set-local
  16247. 'paragraph-start
  16248. (concat
  16249. "\f" "\\|"
  16250. "[ ]*$" "\\|"
  16251. "\\*+ " "\\|"
  16252. "[ \t]*#" "\\|"
  16253. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16254. "[ \t]*[:|]" "\\|"
  16255. "\\$\\$" "\\|"
  16256. "\\\\\\(begin\\|end\\|[][]\\)"))
  16257. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16258. ;; But only if the user has not turned off tables or fixed-width regions
  16259. (org-set-local
  16260. 'auto-fill-inhibit-regexp
  16261. (concat "\\*+ \\|#\\+"
  16262. "\\|[ \t]*" org-keyword-time-regexp
  16263. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16264. (concat
  16265. "\\|[ \t]*["
  16266. (if org-enable-table-editor "|" "")
  16267. (if org-enable-fixed-width-editor ":" "")
  16268. "]"))))
  16269. ;; We use our own fill-paragraph function, to make sure that tables
  16270. ;; and fixed-width regions are not wrapped. That function will pass
  16271. ;; through to `fill-paragraph' when appropriate.
  16272. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16273. ;; Adaptive filling: To get full control, first make sure that
  16274. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16275. (unless (local-variable-p 'adaptive-fill-regexp)
  16276. (org-set-local 'org-adaptive-fill-regexp-backup
  16277. adaptive-fill-regexp))
  16278. (org-set-local 'adaptive-fill-regexp "\000")
  16279. (org-set-local 'adaptive-fill-function
  16280. 'org-adaptive-fill-function)
  16281. (org-set-local
  16282. 'align-mode-rules-list
  16283. '((org-in-buffer-settings
  16284. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16285. (modes . '(org-mode))))))
  16286. (defun org-fill-paragraph (&optional justify)
  16287. "Re-align a table, pass through to fill-paragraph if no table."
  16288. (let ((table-p (org-at-table-p))
  16289. (table.el-p (org-at-table.el-p)))
  16290. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16291. (save-excursion (goto-char (point-at-bol))
  16292. (looking-at outline-regexp)))
  16293. t) ; skip headlines
  16294. (table.el-p t) ; skip table.el tables
  16295. (table-p (org-table-align) t) ; align org-mode tables
  16296. (t nil)))) ; call paragraph-fill
  16297. ;; For reference, this is the default value of adaptive-fill-regexp
  16298. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16299. (defun org-adaptive-fill-function ()
  16300. "Return a fill prefix for org-mode files.
  16301. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16302. work correctly."
  16303. (cond
  16304. ;; Comment line
  16305. ((looking-at "#[ \t]+")
  16306. (match-string-no-properties 0))
  16307. ;; Description list
  16308. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16309. (save-excursion
  16310. (if (> (match-end 1) (+ (match-beginning 1)
  16311. org-description-max-indent))
  16312. (goto-char (+ (match-beginning 1) 5))
  16313. (goto-char (match-end 0)))
  16314. (make-string (current-column) ?\ )))
  16315. ;; Ordered or unordered list
  16316. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16317. (save-excursion
  16318. (goto-char (match-end 0))
  16319. (make-string (current-column) ?\ )))
  16320. ;; Other text
  16321. ((looking-at org-adaptive-fill-regexp-backup)
  16322. (match-string-no-properties 0))))
  16323. ;;; Other stuff.
  16324. (defun org-toggle-fixed-width-section (arg)
  16325. "Toggle the fixed-width export.
  16326. If there is no active region, the QUOTE keyword at the current headline is
  16327. inserted or removed. When present, it causes the text between this headline
  16328. and the next to be exported as fixed-width text, and unmodified.
  16329. If there is an active region, this command adds or removes a colon as the
  16330. first character of this line. If the first character of a line is a colon,
  16331. this line is also exported in fixed-width font."
  16332. (interactive "P")
  16333. (let* ((cc 0)
  16334. (regionp (org-region-active-p))
  16335. (beg (if regionp (region-beginning) (point)))
  16336. (end (if regionp (region-end)))
  16337. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16338. (case-fold-search nil)
  16339. (re "[ \t]*\\(: \\)")
  16340. off)
  16341. (if regionp
  16342. (save-excursion
  16343. (goto-char beg)
  16344. (setq cc (current-column))
  16345. (beginning-of-line 1)
  16346. (setq off (looking-at re))
  16347. (while (> nlines 0)
  16348. (setq nlines (1- nlines))
  16349. (beginning-of-line 1)
  16350. (cond
  16351. (arg
  16352. (org-move-to-column cc t)
  16353. (insert ": \n")
  16354. (forward-line -1))
  16355. ((and off (looking-at re))
  16356. (replace-match "" t t nil 1))
  16357. ((not off) (org-move-to-column cc t) (insert ": ")))
  16358. (forward-line 1)))
  16359. (save-excursion
  16360. (org-back-to-heading)
  16361. (if (looking-at (concat outline-regexp
  16362. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16363. (replace-match "" t t nil 1)
  16364. (if (looking-at outline-regexp)
  16365. (progn
  16366. (goto-char (match-end 0))
  16367. (insert org-quote-string " "))))))))
  16368. (defun org-reftex-citation ()
  16369. "Use reftex-citation to insert a citation into the buffer.
  16370. This looks for a line like
  16371. #+BIBLIOGRAPHY: foo plain option:-d
  16372. and derives from it that foo.bib is the bibliography file relevant
  16373. for this document. It then installs the necessary environment for RefTeX
  16374. to work in this buffer and calls `reftex-citation' to insert a citation
  16375. into the buffer.
  16376. Export of such citations to both LaTeX and HTML is handled by the contributed
  16377. package org-exp-bibtex by Taru Karttunen."
  16378. (interactive)
  16379. (let ((reftex-docstruct-symbol 'rds)
  16380. (reftex-cite-format "\\cite{%l}")
  16381. rds bib)
  16382. (save-excursion
  16383. (save-restriction
  16384. (widen)
  16385. (let ((case-fold-search t)
  16386. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16387. (if (not (save-excursion
  16388. (or (re-search-forward re nil t)
  16389. (re-search-backward re nil t))))
  16390. (error "No bibliography defined in file")
  16391. (setq bib (concat (match-string 1) ".bib")
  16392. rds (list (list 'bib bib)))))))
  16393. (call-interactively 'reftex-citation)))
  16394. ;;;; Functions extending outline functionality
  16395. (defun org-beginning-of-line (&optional arg)
  16396. "Go to the beginning of the current line. If that is invisible, continue
  16397. to a visible line beginning. This makes the function of C-a more intuitive.
  16398. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16399. first attempt, and only move to after the tags when the cursor is already
  16400. beyond the end of the headline."
  16401. (interactive "P")
  16402. (let ((pos (point))
  16403. (special (if (consp org-special-ctrl-a/e)
  16404. (car org-special-ctrl-a/e)
  16405. org-special-ctrl-a/e))
  16406. refpos)
  16407. (if (org-bound-and-true-p line-move-visual)
  16408. (beginning-of-visual-line 1)
  16409. (beginning-of-line 1))
  16410. (if (and arg (fboundp 'move-beginning-of-line))
  16411. (call-interactively 'move-beginning-of-line)
  16412. (if (bobp)
  16413. nil
  16414. (backward-char 1)
  16415. (if (org-invisible-p)
  16416. (while (and (not (bobp)) (org-invisible-p))
  16417. (backward-char 1)
  16418. (beginning-of-line 1))
  16419. (forward-char 1))))
  16420. (when special
  16421. (cond
  16422. ((and (looking-at org-complex-heading-regexp)
  16423. (= (char-after (match-end 1)) ?\ ))
  16424. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16425. (point-at-eol)))
  16426. (goto-char
  16427. (if (eq special t)
  16428. (cond ((> pos refpos) refpos)
  16429. ((= pos (point)) refpos)
  16430. (t (point)))
  16431. (cond ((> pos (point)) (point))
  16432. ((not (eq last-command this-command)) (point))
  16433. (t refpos)))))
  16434. ((org-at-item-p)
  16435. (goto-char
  16436. (if (eq special t)
  16437. (cond ((> pos (match-end 4)) (match-end 4))
  16438. ((= pos (point)) (match-end 4))
  16439. (t (point)))
  16440. (cond ((> pos (point)) (point))
  16441. ((not (eq last-command this-command)) (point))
  16442. (t (match-end 4))))))))
  16443. (org-no-warnings
  16444. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16445. (defun org-end-of-line (&optional arg)
  16446. "Go to the end of the line.
  16447. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16448. first attempt, and only move to after the tags when the cursor is already
  16449. beyond the end of the headline."
  16450. (interactive "P")
  16451. (let ((special (if (consp org-special-ctrl-a/e)
  16452. (cdr org-special-ctrl-a/e)
  16453. org-special-ctrl-a/e)))
  16454. (if (or (not special)
  16455. (not (org-on-heading-p))
  16456. arg)
  16457. (call-interactively
  16458. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16459. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16460. (t 'end-of-line)))
  16461. (let ((pos (point)))
  16462. (beginning-of-line 1)
  16463. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16464. (if (eq special t)
  16465. (if (or (< pos (match-beginning 1))
  16466. (= pos (match-end 0)))
  16467. (goto-char (match-beginning 1))
  16468. (goto-char (match-end 0)))
  16469. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16470. (goto-char (match-end 0))
  16471. (goto-char (match-beginning 1))))
  16472. (call-interactively (if (fboundp 'move-end-of-line)
  16473. 'move-end-of-line
  16474. 'end-of-line)))))
  16475. (org-no-warnings
  16476. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16477. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16478. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16479. (define-key org-mode-map [home] 'org-beginning-of-line)
  16480. (define-key org-mode-map [end] 'org-end-of-line)
  16481. (defun org-backward-sentence (&optional arg)
  16482. "Go to beginning of sentence, or beginning of table field.
  16483. This will call `backward-sentence' or `org-table-beginning-of-field',
  16484. depending on context."
  16485. (interactive "P")
  16486. (cond
  16487. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16488. (t (call-interactively 'backward-sentence))))
  16489. (defun org-forward-sentence (&optional arg)
  16490. "Go to end of sentence, or end of table field.
  16491. This will call `forward-sentence' or `org-table-end-of-field',
  16492. depending on context."
  16493. (interactive "P")
  16494. (cond
  16495. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16496. (t (call-interactively 'forward-sentence))))
  16497. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16498. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16499. (defun org-kill-line (&optional arg)
  16500. "Kill line, to tags or end of line."
  16501. (interactive "P")
  16502. (cond
  16503. ((or (not org-special-ctrl-k)
  16504. (bolp)
  16505. (not (org-on-heading-p)))
  16506. (call-interactively 'kill-line))
  16507. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16508. (kill-region (point) (match-beginning 1))
  16509. (org-set-tags nil t))
  16510. (t (kill-region (point) (point-at-eol)))))
  16511. (define-key org-mode-map "\C-k" 'org-kill-line)
  16512. (defun org-yank (&optional arg)
  16513. "Yank. If the kill is a subtree, treat it specially.
  16514. This command will look at the current kill and check if is a single
  16515. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16516. cursor is at the beginning of a line or after the stars of a currently
  16517. empty headline, then the yank is handled specially. How exactly depends
  16518. on the value of the following variables, both set by default.
  16519. org-yank-folded-subtrees
  16520. When set, the subtree(s) will be folded after insertion, but only
  16521. if doing so would now swallow text after the yanked text.
  16522. org-yank-adjusted-subtrees
  16523. When set, the subtree will be promoted or demoted in order to
  16524. fit into the local outline tree structure, which means that the level
  16525. will be adjusted so that it becomes the smaller one of the two
  16526. *visible* surrounding headings.
  16527. Any prefix to this command will cause `yank' to be called directly with
  16528. no special treatment. In particular, a simple `C-u' prefix will just
  16529. plainly yank the text as it is.
  16530. \[1] The test checks if the first non-white line is a heading
  16531. and if there are no other headings with fewer stars."
  16532. (interactive "P")
  16533. (org-yank-generic 'yank arg))
  16534. (defun org-yank-generic (command arg)
  16535. "Perform some yank-like command.
  16536. This function implements the behavior described in the `org-yank'
  16537. documentation. However, it has been generalized to work for any
  16538. interactive command with similar behavior."
  16539. ;; pretend to be command COMMAND
  16540. (setq this-command command)
  16541. (if arg
  16542. (call-interactively command)
  16543. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16544. (and (org-kill-is-subtree-p)
  16545. (or (bolp)
  16546. (and (looking-at "[ \t]*$")
  16547. (string-match
  16548. "\\`\\*+\\'"
  16549. (buffer-substring (point-at-bol) (point)))))))
  16550. swallowp)
  16551. (cond
  16552. ((and subtreep org-yank-folded-subtrees)
  16553. (let ((beg (point))
  16554. end)
  16555. (if (and subtreep org-yank-adjusted-subtrees)
  16556. (org-paste-subtree nil nil 'for-yank)
  16557. (call-interactively command))
  16558. (setq end (point))
  16559. (goto-char beg)
  16560. (when (and (bolp) subtreep
  16561. (not (setq swallowp
  16562. (org-yank-folding-would-swallow-text beg end))))
  16563. (or (looking-at outline-regexp)
  16564. (re-search-forward (concat "^" outline-regexp) end t))
  16565. (while (and (< (point) end) (looking-at outline-regexp))
  16566. (hide-subtree)
  16567. (org-cycle-show-empty-lines 'folded)
  16568. (condition-case nil
  16569. (outline-forward-same-level 1)
  16570. (error (goto-char end)))))
  16571. (when swallowp
  16572. (message
  16573. "Inserted text not folded because that would swallow text"))
  16574. (goto-char end)
  16575. (skip-chars-forward " \t\n\r")
  16576. (beginning-of-line 1)
  16577. (push-mark beg 'nomsg)))
  16578. ((and subtreep org-yank-adjusted-subtrees)
  16579. (let ((beg (point-at-bol)))
  16580. (org-paste-subtree nil nil 'for-yank)
  16581. (push-mark beg 'nomsg)))
  16582. (t
  16583. (call-interactively command))))))
  16584. (defun org-yank-folding-would-swallow-text (beg end)
  16585. "Would hide-subtree at BEG swallow any text after END?"
  16586. (let (level)
  16587. (save-excursion
  16588. (goto-char beg)
  16589. (when (or (looking-at outline-regexp)
  16590. (re-search-forward (concat "^" outline-regexp) end t))
  16591. (setq level (org-outline-level)))
  16592. (goto-char end)
  16593. (skip-chars-forward " \t\r\n\v\f")
  16594. (if (or (eobp)
  16595. (and (bolp) (looking-at org-outline-regexp)
  16596. (<= (org-outline-level) level)))
  16597. nil ; Nothing would be swallowed
  16598. t)))) ; something would swallow
  16599. (define-key org-mode-map "\C-y" 'org-yank)
  16600. (defun org-invisible-p ()
  16601. "Check if point is at a character currently not visible."
  16602. ;; Early versions of noutline don't have `outline-invisible-p'.
  16603. (if (fboundp 'outline-invisible-p)
  16604. (outline-invisible-p)
  16605. (get-char-property (point) 'invisible)))
  16606. (defun org-invisible-p2 ()
  16607. "Check if point is at a character currently not visible."
  16608. (save-excursion
  16609. (if (and (eolp) (not (bobp))) (backward-char 1))
  16610. ;; Early versions of noutline don't have `outline-invisible-p'.
  16611. (if (fboundp 'outline-invisible-p)
  16612. (outline-invisible-p)
  16613. (get-char-property (point) 'invisible))))
  16614. (defun org-back-to-heading (&optional invisible-ok)
  16615. "Call `outline-back-to-heading', but provide a better error message."
  16616. (condition-case nil
  16617. (outline-back-to-heading invisible-ok)
  16618. (error (error "Before first headline at position %d in buffer %s"
  16619. (point) (current-buffer)))))
  16620. (defun org-before-first-heading-p ()
  16621. "Before first heading?"
  16622. (save-excursion
  16623. (null (re-search-backward "^\\*+ " nil t))))
  16624. (defun org-on-heading-p (&optional ignored)
  16625. (outline-on-heading-p t))
  16626. (defun org-at-heading-p (&optional ignored)
  16627. (outline-on-heading-p t))
  16628. (defun org-point-at-end-of-empty-headline ()
  16629. "If point is at the end of an empty headline, return t, else nil.
  16630. If the heading only contains a TODO keyword, it is still still considered
  16631. empty."
  16632. (and (looking-at "[ \t]*$")
  16633. (save-excursion
  16634. (beginning-of-line 1)
  16635. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16636. "\\)?[ \t]*$")))))
  16637. (defun org-at-heading-or-item-p ()
  16638. (or (org-on-heading-p) (org-at-item-p)))
  16639. (defun org-on-target-p ()
  16640. (or (org-in-regexp org-radio-target-regexp)
  16641. (org-in-regexp org-target-regexp)))
  16642. (defun org-up-heading-all (arg)
  16643. "Move to the heading line of which the present line is a subheading.
  16644. This function considers both visible and invisible heading lines.
  16645. With argument, move up ARG levels."
  16646. (if (fboundp 'outline-up-heading-all)
  16647. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16648. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16649. (defun org-up-heading-safe ()
  16650. "Move to the heading line of which the present line is a subheading.
  16651. This version will not throw an error. It will return the level of the
  16652. headline found, or nil if no higher level is found.
  16653. Also, this function will be a lot faster than `outline-up-heading',
  16654. because it relies on stars being the outline starters. This can really
  16655. make a significant difference in outlines with very many siblings."
  16656. (let (start-level re)
  16657. (org-back-to-heading t)
  16658. (setq start-level (funcall outline-level))
  16659. (if (equal start-level 1)
  16660. nil
  16661. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16662. (if (re-search-backward re nil t)
  16663. (funcall outline-level)))))
  16664. (defun org-first-sibling-p ()
  16665. "Is this heading the first child of its parents?"
  16666. (interactive)
  16667. (let ((re (concat "^" outline-regexp))
  16668. level l)
  16669. (unless (org-at-heading-p t)
  16670. (error "Not at a heading"))
  16671. (setq level (funcall outline-level))
  16672. (save-excursion
  16673. (if (not (re-search-backward re nil t))
  16674. t
  16675. (setq l (funcall outline-level))
  16676. (< l level)))))
  16677. (defun org-goto-sibling (&optional previous)
  16678. "Goto the next sibling, even if it is invisible.
  16679. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16680. when a sibling was found. When none is found, return nil and don't
  16681. move point."
  16682. (let ((fun (if previous 're-search-backward 're-search-forward))
  16683. (pos (point))
  16684. (re (concat "^" outline-regexp))
  16685. level l)
  16686. (when (condition-case nil (org-back-to-heading t) (error nil))
  16687. (setq level (funcall outline-level))
  16688. (catch 'exit
  16689. (or previous (forward-char 1))
  16690. (while (funcall fun re nil t)
  16691. (setq l (funcall outline-level))
  16692. (when (< l level) (goto-char pos) (throw 'exit nil))
  16693. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16694. (goto-char pos)
  16695. nil))))
  16696. (defun org-show-siblings ()
  16697. "Show all siblings of the current headline."
  16698. (save-excursion
  16699. (while (org-goto-sibling) (org-flag-heading nil)))
  16700. (save-excursion
  16701. (while (org-goto-sibling 'previous)
  16702. (org-flag-heading nil))))
  16703. (defun org-show-hidden-entry ()
  16704. "Show an entry where even the heading is hidden."
  16705. (save-excursion
  16706. (org-show-entry)))
  16707. (defun org-flag-heading (flag &optional entry)
  16708. "Flag the current heading. FLAG non-nil means make invisible.
  16709. When ENTRY is non-nil, show the entire entry."
  16710. (save-excursion
  16711. (org-back-to-heading t)
  16712. ;; Check if we should show the entire entry
  16713. (if entry
  16714. (progn
  16715. (org-show-entry)
  16716. (save-excursion
  16717. (and (outline-next-heading)
  16718. (org-flag-heading nil))))
  16719. (outline-flag-region (max (point-min) (1- (point)))
  16720. (save-excursion (outline-end-of-heading) (point))
  16721. flag))))
  16722. (defun org-get-next-sibling ()
  16723. "Move to next heading of the same level, and return point.
  16724. If there is no such heading, return nil.
  16725. This is like outline-next-sibling, but invisible headings are ok."
  16726. (let ((level (funcall outline-level)))
  16727. (outline-next-heading)
  16728. (while (and (not (eobp)) (> (funcall outline-level) level))
  16729. (outline-next-heading))
  16730. (if (or (eobp) (< (funcall outline-level) level))
  16731. nil
  16732. (point))))
  16733. (defun org-get-last-sibling ()
  16734. "Move to previous heading of the same level, and return point.
  16735. If there is no such heading, return nil."
  16736. (let ((opoint (point))
  16737. (level (funcall outline-level)))
  16738. (outline-previous-heading)
  16739. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16740. (while (and (> (funcall outline-level) level)
  16741. (not (bobp)))
  16742. (outline-previous-heading))
  16743. (if (< (funcall outline-level) level)
  16744. nil
  16745. (point)))))
  16746. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16747. ;; This contains an exact copy of the original function, but it uses
  16748. ;; `org-back-to-heading', to make it work also in invisible
  16749. ;; trees. And is uses an invisible-OK argument.
  16750. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16751. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16752. ;; with many children and grandchildren etc, this can be much faster
  16753. ;; than the outline version.
  16754. (org-back-to-heading invisible-OK)
  16755. (let ((first t)
  16756. (level (funcall outline-level)))
  16757. (if (and (org-mode-p) (< level 1000))
  16758. ;; A true heading (not a plain list item), in Org-mode
  16759. ;; This means we can easily find the end by looking
  16760. ;; only for the right number of stars. Using a regexp to do
  16761. ;; this is so much faster than using a Lisp loop.
  16762. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16763. (forward-char 1)
  16764. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16765. ;; something else, do it the slow way
  16766. (while (and (not (eobp))
  16767. (or first (> (funcall outline-level) level)))
  16768. (setq first nil)
  16769. (outline-next-heading)))
  16770. (unless to-heading
  16771. (if (memq (preceding-char) '(?\n ?\^M))
  16772. (progn
  16773. ;; Go to end of line before heading
  16774. (forward-char -1)
  16775. (if (memq (preceding-char) '(?\n ?\^M))
  16776. ;; leave blank line before heading
  16777. (forward-char -1))))))
  16778. (point))
  16779. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16780. "Use Org version in org-mode, for dramatic speed-up."
  16781. (if (eq major-mode 'org-mode)
  16782. (progn
  16783. (org-end-of-subtree nil t)
  16784. (unless (eobp) (backward-char 1)))
  16785. ad-do-it))
  16786. (defun org-forward-same-level (arg &optional invisible-ok)
  16787. "Move forward to the arg'th subheading at same level as this one.
  16788. Stop at the first and last subheadings of a superior heading."
  16789. (interactive "p")
  16790. (org-back-to-heading invisible-ok)
  16791. (org-on-heading-p)
  16792. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16793. (re (format "^\\*\\{1,%d\\} " level))
  16794. l)
  16795. (forward-char 1)
  16796. (while (> arg 0)
  16797. (while (and (re-search-forward re nil 'move)
  16798. (setq l (- (match-end 0) (match-beginning 0) 1))
  16799. (= l level)
  16800. (not invisible-ok)
  16801. (progn (backward-char 1) (org-invisible-p)))
  16802. (if (< l level) (setq arg 1)))
  16803. (setq arg (1- arg)))
  16804. (beginning-of-line 1)))
  16805. (defun org-backward-same-level (arg &optional invisible-ok)
  16806. "Move backward to the arg'th subheading at same level as this one.
  16807. Stop at the first and last subheadings of a superior heading."
  16808. (interactive "p")
  16809. (org-back-to-heading)
  16810. (org-on-heading-p)
  16811. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16812. (re (format "^\\*\\{1,%d\\} " level))
  16813. l)
  16814. (while (> arg 0)
  16815. (while (and (re-search-backward re nil 'move)
  16816. (setq l (- (match-end 0) (match-beginning 0) 1))
  16817. (= l level)
  16818. (not invisible-ok)
  16819. (org-invisible-p))
  16820. (if (< l level) (setq arg 1)))
  16821. (setq arg (1- arg)))))
  16822. (defun org-show-subtree ()
  16823. "Show everything after this heading at deeper levels."
  16824. (outline-flag-region
  16825. (point)
  16826. (save-excursion
  16827. (org-end-of-subtree t t))
  16828. nil))
  16829. (defun org-show-entry ()
  16830. "Show the body directly following this heading.
  16831. Show the heading too, if it is currently invisible."
  16832. (interactive)
  16833. (save-excursion
  16834. (condition-case nil
  16835. (progn
  16836. (org-back-to-heading t)
  16837. (outline-flag-region
  16838. (max (point-min) (1- (point)))
  16839. (save-excursion
  16840. (if (re-search-forward
  16841. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16842. (match-beginning 1)
  16843. (point-max)))
  16844. nil)
  16845. (org-cycle-hide-drawers 'children))
  16846. (error nil))))
  16847. (defun org-make-options-regexp (kwds &optional extra)
  16848. "Make a regular expression for keyword lines."
  16849. (concat
  16850. "^"
  16851. "#?[ \t]*\\+\\("
  16852. (mapconcat 'regexp-quote kwds "\\|")
  16853. (if extra (concat "\\|" extra))
  16854. "\\):[ \t]*"
  16855. "\\(.*\\)"))
  16856. ;; Make isearch reveal the necessary context
  16857. (defun org-isearch-end ()
  16858. "Reveal context after isearch exits."
  16859. (when isearch-success ; only if search was successful
  16860. (if (featurep 'xemacs)
  16861. ;; Under XEmacs, the hook is run in the correct place,
  16862. ;; we directly show the context.
  16863. (org-show-context 'isearch)
  16864. ;; In Emacs the hook runs *before* restoring the overlays.
  16865. ;; So we have to use a one-time post-command-hook to do this.
  16866. ;; (Emacs 22 has a special variable, see function `org-mode')
  16867. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16868. isearch-mode-end-hook-quit)
  16869. ;; Only when the isearch was not quitted.
  16870. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16871. 'append 'local)))))
  16872. (defun org-isearch-post-command ()
  16873. "Remove self from hook, and show context."
  16874. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16875. (org-show-context 'isearch))
  16876. ;;;; Integration with and fixes for other packages
  16877. ;;; Imenu support
  16878. (defvar org-imenu-markers nil
  16879. "All markers currently used by Imenu.")
  16880. (make-variable-buffer-local 'org-imenu-markers)
  16881. (defun org-imenu-new-marker (&optional pos)
  16882. "Return a new marker for use by Imenu, and remember the marker."
  16883. (let ((m (make-marker)))
  16884. (move-marker m (or pos (point)))
  16885. (push m org-imenu-markers)
  16886. m))
  16887. (defun org-imenu-get-tree ()
  16888. "Produce the index for Imenu."
  16889. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16890. (setq org-imenu-markers nil)
  16891. (let* ((n org-imenu-depth)
  16892. (re (concat "^" outline-regexp))
  16893. (subs (make-vector (1+ n) nil))
  16894. (last-level 0)
  16895. m level head)
  16896. (save-excursion
  16897. (save-restriction
  16898. (widen)
  16899. (goto-char (point-max))
  16900. (while (re-search-backward re nil t)
  16901. (setq level (org-reduced-level (funcall outline-level)))
  16902. (when (<= level n)
  16903. (looking-at org-complex-heading-regexp)
  16904. (setq head (org-link-display-format
  16905. (org-match-string-no-properties 4))
  16906. m (org-imenu-new-marker))
  16907. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16908. (if (>= level last-level)
  16909. (push (cons head m) (aref subs level))
  16910. (push (cons head (aref subs (1+ level))) (aref subs level))
  16911. (loop for i from (1+ level) to n do (aset subs i nil)))
  16912. (setq last-level level)))))
  16913. (aref subs 1)))
  16914. (eval-after-load "imenu"
  16915. '(progn
  16916. (add-hook 'imenu-after-jump-hook
  16917. (lambda ()
  16918. (if (eq major-mode 'org-mode)
  16919. (org-show-context 'org-goto))))))
  16920. (defun org-link-display-format (link)
  16921. "Replace a link with either the description, or the link target
  16922. if no description is present"
  16923. (save-match-data
  16924. (if (string-match org-bracket-link-analytic-regexp link)
  16925. (replace-match (if (match-end 5)
  16926. (match-string 5 link)
  16927. (concat (match-string 1 link)
  16928. (match-string 3 link)))
  16929. nil t link)
  16930. link)))
  16931. ;; Speedbar support
  16932. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  16933. "Overlay marking the agenda restriction line in speedbar.")
  16934. (overlay-put org-speedbar-restriction-lock-overlay
  16935. 'face 'org-agenda-restriction-lock)
  16936. (overlay-put org-speedbar-restriction-lock-overlay
  16937. 'help-echo "Agendas are currently limited to this item.")
  16938. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16939. (defun org-speedbar-set-agenda-restriction ()
  16940. "Restrict future agenda commands to the location at point in speedbar.
  16941. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16942. (interactive)
  16943. (require 'org-agenda)
  16944. (let (p m tp np dir txt)
  16945. (cond
  16946. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16947. 'org-imenu t))
  16948. (setq m (get-text-property p 'org-imenu-marker))
  16949. (with-current-buffer (marker-buffer m)
  16950. (goto-char m)
  16951. (org-agenda-set-restriction-lock 'subtree)))
  16952. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16953. 'speedbar-function 'speedbar-find-file))
  16954. (setq tp (previous-single-property-change
  16955. (1+ p) 'speedbar-function)
  16956. np (next-single-property-change
  16957. tp 'speedbar-function)
  16958. dir (speedbar-line-directory)
  16959. txt (buffer-substring-no-properties (or tp (point-min))
  16960. (or np (point-max))))
  16961. (with-current-buffer (find-file-noselect
  16962. (let ((default-directory dir))
  16963. (expand-file-name txt)))
  16964. (unless (org-mode-p)
  16965. (error "Cannot restrict to non-Org-mode file"))
  16966. (org-agenda-set-restriction-lock 'file)))
  16967. (t (error "Don't know how to restrict Org-mode's agenda")))
  16968. (move-overlay org-speedbar-restriction-lock-overlay
  16969. (point-at-bol) (point-at-eol))
  16970. (setq current-prefix-arg nil)
  16971. (org-agenda-maybe-redo)))
  16972. (eval-after-load "speedbar"
  16973. '(progn
  16974. (speedbar-add-supported-extension ".org")
  16975. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16976. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16977. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16978. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16979. (add-hook 'speedbar-visiting-tag-hook
  16980. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16981. ;;; Fixes and Hacks for problems with other packages
  16982. ;; Make flyspell not check words in links, to not mess up our keymap
  16983. (defun org-mode-flyspell-verify ()
  16984. "Don't let flyspell put overlays at active buttons."
  16985. (and (not (get-text-property (point) 'keymap))
  16986. (not (get-text-property (point) 'org-no-flyspell))))
  16987. (defun org-remove-flyspell-overlays-in (beg end)
  16988. "Remove flyspell overlays in region."
  16989. (and (org-bound-and-true-p flyspell-mode)
  16990. (fboundp 'flyspell-delete-region-overlays)
  16991. (flyspell-delete-region-overlays beg end))
  16992. (add-text-properties beg end '(org-no-flyspell t)))
  16993. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16994. (eval-after-load "bookmark"
  16995. '(if (boundp 'bookmark-after-jump-hook)
  16996. ;; We can use the hook
  16997. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16998. ;; Hook not available, use advice
  16999. (defadvice bookmark-jump (after org-make-visible activate)
  17000. "Make the position visible."
  17001. (org-bookmark-jump-unhide))))
  17002. ;; Make sure saveplace shows the location if it was hidden
  17003. (eval-after-load "saveplace"
  17004. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17005. "Make the position visible."
  17006. (org-bookmark-jump-unhide)))
  17007. ;; Make sure ecb shows the location if it was hidden
  17008. (eval-after-load "ecb"
  17009. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17010. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17011. (if (eq major-mode 'org-mode)
  17012. (org-show-context))))
  17013. (defun org-bookmark-jump-unhide ()
  17014. "Unhide the current position, to show the bookmark location."
  17015. (and (org-mode-p)
  17016. (or (org-invisible-p)
  17017. (save-excursion (goto-char (max (point-min) (1- (point))))
  17018. (org-invisible-p)))
  17019. (org-show-context 'bookmark-jump)))
  17020. ;; Make session.el ignore our circular variable
  17021. (eval-after-load "session"
  17022. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17023. ;;;; Experimental code
  17024. (defun org-closed-in-range ()
  17025. "Sparse tree of items closed in a certain time range.
  17026. Still experimental, may disappear in the future."
  17027. (interactive)
  17028. ;; Get the time interval from the user.
  17029. (let* ((time1 (org-float-time
  17030. (org-read-date nil 'to-time nil "Starting date: ")))
  17031. (time2 (org-float-time
  17032. (org-read-date nil 'to-time nil "End date:")))
  17033. ;; callback function
  17034. (callback (lambda ()
  17035. (let ((time
  17036. (org-float-time
  17037. (apply 'encode-time
  17038. (org-parse-time-string
  17039. (match-string 1))))))
  17040. ;; check if time in interval
  17041. (and (>= time time1) (<= time time2))))))
  17042. ;; make tree, check each match with the callback
  17043. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17044. ;;;; Finish up
  17045. (provide 'org)
  17046. (run-hooks 'org-load-hook)
  17047. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17048. ;;; org.el ends here