org.el 660 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725
  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
  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.33
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.33"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((origin default-directory)
  96. (version org-version)
  97. (git-version)
  98. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  99. (when (and (file-exists-p (expand-file-name ".git" dir))
  100. (executable-find "git"))
  101. (unwind-protect
  102. (progn
  103. (cd dir)
  104. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  105. (with-current-buffer "*Shell Command Output*"
  106. (goto-char (point-min))
  107. (setq git-version (buffer-substring (point) (point-at-eol))))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (when (string-match "\\S-"
  110. (shell-command-to-string
  111. "git diff-index --name-only HEAD --"))
  112. (setq git-version (concat git-version ".dirty")))
  113. (setq version (concat version " (" git-version ")"))))
  114. (cd origin)))
  115. (setq version (format "Org-mode version %s" version))
  116. (if here (insert version))
  117. (message version)))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  171. (const :tag " id: Global IDs for identifying entries" org-id)
  172. (const :tag " info: Links to Info nodes" org-info)
  173. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  174. (const :tag " habit: Track your consistency with habits" org-habit)
  175. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  176. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  177. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  178. (const :tag " mew Links to Mew folders/messages" org-mew)
  179. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  180. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  181. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  182. (const :tag " vm: Links to VM folders/messages" org-vm)
  183. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  184. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  185. (const :tag " mouse: Additional mouse support" org-mouse)
  186. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  187. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  188. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  189. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  190. (const :tag "C collector: Collect properties into tables" org-collector)
  191. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  192. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  193. (const :tag "C eval: Include command output as text" org-eval)
  194. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  195. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  196. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  197. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  198. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  199. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  200. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  201. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  202. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  203. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  204. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  205. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  206. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  207. (const :tag "C R: Computation using the R language" org-R)
  208. (const :tag "C registry: A registry for Org-mode links" org-registry)
  209. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  210. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  211. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  212. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  213. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  214. (const :tag "C track: Keep up with Org-mode development" org-track)
  215. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  216. (defcustom org-support-shift-select nil
  217. "Non-nil means, make shift-cursor commands select text when possible.
  218. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  219. selecting a region, or enlarge thusly regions started in this way.
  220. In Org-mode, in special contexts, these same keys are used for other
  221. purposes, important enough to compete with shift selection. Org tries
  222. to balance these needs by supporting `shift-select-mode' outside these
  223. special contexts, under control of this variable.
  224. The default of this variable is nil, to avoid confusing behavior. Shifted
  225. cursor keys will then execute Org commands in the following contexts:
  226. - on a headline, changing TODO state (left/right) and priority (up/down)
  227. - on a time stamp, changing the time
  228. - in a plain list item, changing the bullet type
  229. - in a property definition line, switching between allowed values
  230. - in the BEGIN line of a clock table (changing the time block).
  231. Outside these contexts, the commands will throw an error.
  232. When this variable is t and the cursor is not in a special context,
  233. Org-mode will support shift-selection for making and enlarging regions.
  234. To make this more effective, the bullet cycling will no longer happen
  235. anywhere in an item line, but only if the cursor is exactly on the bullet.
  236. If you set this variable to the symbol `always', then the keys
  237. will not be special in headlines, property lines, and item lines, to make
  238. shift selection work there as well. If this is what you want, you can
  239. use the following alternative commands: `C-c C-t' and `C-c ,' to
  240. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  241. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  242. edited by hand or in column view.
  243. However, when the cursor is on a timestamp, shift-cursor commands
  244. will still edit the time stamp - this is just too good to give up.
  245. XEmacs user should have this variable set to nil, because shift-select-mode
  246. is Emacs 23 only."
  247. :group 'org
  248. :type '(choice
  249. (const :tag "Never" nil)
  250. (const :tag "When outside special context" t)
  251. (const :tag "Everywhere except timestamps" always)))
  252. (defgroup org-startup nil
  253. "Options concerning startup of Org-mode."
  254. :tag "Org Startup"
  255. :group 'org)
  256. (defcustom org-startup-folded t
  257. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  258. This can also be configured on a per-file basis by adding one of
  259. the following lines anywhere in the buffer:
  260. #+STARTUP: fold (or `overview', this is equivalent)
  261. #+STARTUP: nofold (or `showall', this is equivalent)
  262. #+STARTUP: content
  263. #+STARTUP: showeverything"
  264. :group 'org-startup
  265. :type '(choice
  266. (const :tag "nofold: show all" nil)
  267. (const :tag "fold: overview" t)
  268. (const :tag "content: all headlines" content)
  269. (const :tag "show everything, even drawers" showeverything)))
  270. (defcustom org-startup-truncated t
  271. "Non-nil means, entering Org-mode will set `truncate-lines'.
  272. This is useful since some lines containing links can be very long and
  273. uninteresting. Also tables look terrible when wrapped."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-startup-indented nil
  277. "Non-nil means, turn on `org-indent-mode' on startup.
  278. This can also be configured on a per-file basis by adding one of
  279. the following lines anywhere in the buffer:
  280. #+STARTUP: indent
  281. #+STARTUP: noindent"
  282. :group 'org-structure
  283. :type '(choice
  284. (const :tag "Not" nil)
  285. (const :tag "Globally (slow on startup in large files)" t)))
  286. (defcustom org-startup-align-all-tables nil
  287. "Non-nil means, align all tables when visiting a file.
  288. This is useful when the column width in tables is forced with <N> cookies
  289. in table fields. Such tables will look correct only after the first re-align.
  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: align
  293. #+STARTUP: noalign"
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-insert-mode-line-in-empty-file nil
  297. "Non-nil means insert the first line setting Org-mode in empty files.
  298. When the function `org-mode' is called interactively in an empty file, this
  299. normally means that the file name does not automatically trigger Org-mode.
  300. To ensure that the file will always be in Org-mode in the future, a
  301. line enforcing Org-mode will be inserted into the buffer, if this option
  302. has been set."
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-replace-disputed-keys nil
  306. "Non-nil means use alternative key bindings for some keys.
  307. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  308. These keys are also used by other packages like shift-selection-mode'
  309. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  310. If you want to use Org-mode together with one of these other modes,
  311. or more generally if you would like to move some Org-mode commands to
  312. other keys, set this variable and configure the keys with the variable
  313. `org-disputed-keys'.
  314. This option is only relevant at load-time of Org-mode, and must be set
  315. *before* org.el is loaded. Changing it requires a restart of Emacs to
  316. become effective."
  317. :group 'org-startup
  318. :type 'boolean)
  319. (defcustom org-use-extra-keys nil
  320. "Non-nil means use extra key sequence definitions for certain
  321. commands. This happens automatically if you run XEmacs or if
  322. window-system is nil. This variable lets you do the same
  323. manually. You must set it before loading org.
  324. Example: on Carbon Emacs 22 running graphically, with an external
  325. keyboard on a Powerbook, the default way of setting M-left might
  326. not work for either Alt or ESC. Setting this variable will make
  327. it work for ESC."
  328. :group 'org-startup
  329. :type 'boolean)
  330. (if (fboundp 'defvaralias)
  331. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  332. (defcustom org-disputed-keys
  333. '(([(shift up)] . [(meta p)])
  334. ([(shift down)] . [(meta n)])
  335. ([(shift left)] . [(meta -)])
  336. ([(shift right)] . [(meta +)])
  337. ([(control shift right)] . [(meta shift +)])
  338. ([(control shift left)] . [(meta shift -)]))
  339. "Keys for which Org-mode and other modes compete.
  340. This is an alist, cars are the default keys, second element specifies
  341. the alternative to use when `org-replace-disputed-keys' is t.
  342. Keys can be specified in any syntax supported by `define-key'.
  343. The value of this option takes effect only at Org-mode's startup,
  344. therefore you'll have to restart Emacs to apply it after changing."
  345. :group 'org-startup
  346. :type 'alist)
  347. (defun org-key (key)
  348. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  349. Or return the original if not disputed."
  350. (if org-replace-disputed-keys
  351. (let* ((nkey (key-description key))
  352. (x (org-find-if (lambda (x)
  353. (equal (key-description (car x)) nkey))
  354. org-disputed-keys)))
  355. (if x (cdr x) key))
  356. key))
  357. (defun org-find-if (predicate seq)
  358. (catch 'exit
  359. (while seq
  360. (if (funcall predicate (car seq))
  361. (throw 'exit (car seq))
  362. (pop seq)))))
  363. (defun org-defkey (keymap key def)
  364. "Define a key, possibly translated, as returned by `org-key'."
  365. (define-key keymap (org-key key) def))
  366. (defcustom org-ellipsis nil
  367. "The ellipsis to use in the Org-mode outline.
  368. When nil, just use the standard three dots. When a string, use that instead,
  369. When a face, use the standard 3 dots, but with the specified face.
  370. The change affects only Org-mode (which will then use its own display table).
  371. Changing this requires executing `M-x org-mode' in a buffer to become
  372. effective."
  373. :group 'org-startup
  374. :type '(choice (const :tag "Default" nil)
  375. (face :tag "Face" :value org-warning)
  376. (string :tag "String" :value "...#")))
  377. (defvar org-display-table nil
  378. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  379. (defgroup org-keywords nil
  380. "Keywords in Org-mode."
  381. :tag "Org Keywords"
  382. :group 'org)
  383. (defcustom org-deadline-string "DEADLINE:"
  384. "String to mark deadline entries.
  385. A deadline is this string, followed by a time stamp. Should be a word,
  386. terminated by a colon. You can insert a schedule keyword and
  387. a timestamp with \\[org-deadline].
  388. Changes become only effective after restarting Emacs."
  389. :group 'org-keywords
  390. :type 'string)
  391. (defcustom org-scheduled-string "SCHEDULED:"
  392. "String to mark scheduled TODO entries.
  393. A schedule is this string, followed by a time stamp. Should be a word,
  394. terminated by a colon. You can insert a schedule keyword and
  395. a timestamp with \\[org-schedule].
  396. Changes become only effective after restarting Emacs."
  397. :group 'org-keywords
  398. :type 'string)
  399. (defcustom org-closed-string "CLOSED:"
  400. "String used as the prefix for timestamps logging closing a TODO entry."
  401. :group 'org-keywords
  402. :type 'string)
  403. (defcustom org-clock-string "CLOCK:"
  404. "String used as prefix for timestamps clocking work hours on an item."
  405. :group 'org-keywords
  406. :type 'string)
  407. (defcustom org-comment-string "COMMENT"
  408. "Entries starting with this keyword will never be exported.
  409. An entry can be toggled between COMMENT and normal with
  410. \\[org-toggle-comment].
  411. Changes become only effective after restarting Emacs."
  412. :group 'org-keywords
  413. :type 'string)
  414. (defcustom org-quote-string "QUOTE"
  415. "Entries starting with this keyword will be exported in fixed-width font.
  416. Quoting applies only to the text in the entry following the headline, and does
  417. not extend beyond the next headline, even if that is lower level.
  418. An entry can be toggled between QUOTE and normal with
  419. \\[org-toggle-fixed-width-section]."
  420. :group 'org-keywords
  421. :type 'string)
  422. (defconst org-repeat-re
  423. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  424. "Regular expression for specifying repeated events.
  425. After a match, group 1 contains the repeat expression.")
  426. (defgroup org-structure nil
  427. "Options concerning the general structure of Org-mode files."
  428. :tag "Org Structure"
  429. :group 'org)
  430. (defgroup org-reveal-location nil
  431. "Options about how to make context of a location visible."
  432. :tag "Org Reveal Location"
  433. :group 'org-structure)
  434. (defconst org-context-choice
  435. '(choice
  436. (const :tag "Always" t)
  437. (const :tag "Never" nil)
  438. (repeat :greedy t :tag "Individual contexts"
  439. (cons
  440. (choice :tag "Context"
  441. (const agenda)
  442. (const org-goto)
  443. (const occur-tree)
  444. (const tags-tree)
  445. (const link-search)
  446. (const mark-goto)
  447. (const bookmark-jump)
  448. (const isearch)
  449. (const default))
  450. (boolean))))
  451. "Contexts for the reveal options.")
  452. (defcustom org-show-hierarchy-above '((default . t))
  453. "Non-nil means, show full hierarchy when revealing a location.
  454. Org-mode often shows locations in an org-mode file which might have
  455. been invisible before. When this is set, the hierarchy of headings
  456. above the exposed location is shown.
  457. Turning this off for example for sparse trees makes them very compact.
  458. Instead of t, this can also be an alist specifying this option for different
  459. contexts. Valid contexts are
  460. agenda when exposing an entry from the agenda
  461. org-goto when using the command `org-goto' on key C-c C-j
  462. occur-tree when using the command `org-occur' on key C-c /
  463. tags-tree when constructing a sparse tree based on tags matches
  464. link-search when exposing search matches associated with a link
  465. mark-goto when exposing the jump goal of a mark
  466. bookmark-jump when exposing a bookmark location
  467. isearch when exiting from an incremental search
  468. default default for all contexts not set explicitly"
  469. :group 'org-reveal-location
  470. :type org-context-choice)
  471. (defcustom org-show-following-heading '((default . nil))
  472. "Non-nil means, show following heading 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 heading following the
  475. match is shown.
  476. Turning this off for example for sparse trees makes them very compact,
  477. but makes it harder to edit the location of the match. In such a case,
  478. use the command \\[org-reveal] to show more context.
  479. Instead of t, this can also be an alist specifying this option for different
  480. contexts. See `org-show-hierarchy-above' for valid contexts."
  481. :group 'org-reveal-location
  482. :type org-context-choice)
  483. (defcustom org-show-siblings '((default . nil) (isearch t))
  484. "Non-nil means, show all sibling heading when revealing a location.
  485. Org-mode often shows locations in an org-mode file which might have
  486. been invisible before. When this is set, the sibling of the current entry
  487. heading are all made visible. If `org-show-hierarchy-above' is t,
  488. the same happens on each level of the hierarchy above the current entry.
  489. By default this is on for the isearch context, off for all other contexts.
  490. Turning this off for example for sparse trees makes them very compact,
  491. but makes it harder to edit the location of the match. In such a case,
  492. use the command \\[org-reveal] to show more context.
  493. Instead of t, this can also be an alist specifying this option for different
  494. contexts. See `org-show-hierarchy-above' for valid contexts."
  495. :group 'org-reveal-location
  496. :type org-context-choice)
  497. (defcustom org-show-entry-below '((default . nil))
  498. "Non-nil means, show the entry below a headline when revealing a location.
  499. Org-mode often shows locations in an org-mode file which might have
  500. been invisible before. When this is set, the text below the headline that is
  501. exposed is also shown.
  502. By default this is off for all contexts.
  503. Instead of t, this can also be an alist specifying this option for different
  504. contexts. See `org-show-hierarchy-above' for valid contexts."
  505. :group 'org-reveal-location
  506. :type org-context-choice)
  507. (defcustom org-indirect-buffer-display 'other-window
  508. "How should indirect tree buffers be displayed?
  509. This applies to indirect buffers created with the commands
  510. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  511. Valid values are:
  512. current-window Display in the current window
  513. other-window Just display in another window.
  514. dedicated-frame Create one new frame, and re-use it each time.
  515. new-frame Make a new frame each time. Note that in this case
  516. previously-made indirect buffers are kept, and you need to
  517. kill these buffers yourself."
  518. :group 'org-structure
  519. :group 'org-agenda-windows
  520. :type '(choice
  521. (const :tag "In current window" current-window)
  522. (const :tag "In current frame, other window" other-window)
  523. (const :tag "Each time a new frame" new-frame)
  524. (const :tag "One dedicated frame" dedicated-frame)))
  525. (defcustom org-use-speed-commands nil
  526. "Non-nil means, activate single letter commands at beginning of a headline."
  527. :group 'org-structure
  528. :type 'boolean)
  529. (defcustom org-speed-commands-user nil
  530. "Alist of additional speed commands.
  531. This list will be checked before `org-speed-commands-default'
  532. when the variable `org-use-speed-commands' is non-nil
  533. and when the cursor is at the beginning of a headline.
  534. The car if each entry is a string with a single letter, which must
  535. be assigned to `self-insert-command' in the global map.
  536. The cdr is either a command to be called interactively, a function
  537. to be called, or a form to be evaluated."
  538. :group 'org-structure
  539. :type '(repeat
  540. (cons
  541. (string "Command letter")
  542. (choice
  543. (function)
  544. (sexp)))))
  545. (defgroup org-cycle nil
  546. "Options concerning visibility cycling in Org-mode."
  547. :tag "Org Cycle"
  548. :group 'org-structure)
  549. (defcustom org-cycle-skip-children-state-if-no-children t
  550. "Non-nil means, skip CHILDREN state in entries that don't have any."
  551. :group 'org-cycle
  552. :type 'boolean)
  553. (defcustom org-cycle-max-level nil
  554. "Maximum level which should still be subject to visibility cycling.
  555. Levels higher than this will, for cycling, be treated as text, not a headline.
  556. When `org-odd-levels-only' is set, a value of N in this variable actually
  557. means 2N-1 stars as the limiting headline.
  558. When nil, cycle all levels.
  559. Note that the limiting level of cycling is also influenced by
  560. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  561. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  562. than its value."
  563. :group 'org-cycle
  564. :type '(choice
  565. (const :tag "No limit" nil)
  566. (integer :tag "Maximum level")))
  567. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  568. "Names of drawers. Drawers are not opened by cycling on the headline above.
  569. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  570. this:
  571. :DRAWERNAME:
  572. .....
  573. :END:
  574. The drawer \"PROPERTIES\" is special for capturing properties through
  575. the property API.
  576. Drawers can be defined on the per-file basis with a line like:
  577. #+DRAWERS: HIDDEN STATE PROPERTIES"
  578. :group 'org-structure
  579. :group 'org-cycle
  580. :type '(repeat (string :tag "Drawer Name")))
  581. (defcustom org-hide-block-startup nil
  582. "Non-nil means, , entering Org-mode will fold all blocks.
  583. This can also be set in on a per-file basis with
  584. #+STARTUP: hideblocks
  585. #+STARTUP: showblocks"
  586. :group 'org-startup
  587. :group 'org-cycle
  588. :type 'boolean)
  589. (defcustom org-cycle-global-at-bob nil
  590. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  591. This makes it possible to do global cycling without having to use S-TAB or
  592. C-u TAB. For this special case to work, the first line of the buffer
  593. must not be a headline - it may be empty or some other text. When used in
  594. this way, `org-cycle-hook' is disables temporarily, to make sure the
  595. cursor stays at the beginning of the buffer.
  596. When this option is nil, don't do anything special at the beginning
  597. of the buffer."
  598. :group 'org-cycle
  599. :type 'boolean)
  600. (defcustom org-cycle-level-after-item/entry-creation t
  601. "Non-nil means, cycle entry level or item indentation in new empty entries.
  602. When the cursor is at the end of an empty headline, i.e with only stars
  603. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  604. and then all possible anchestor states, before returning to the original state.
  605. This makes data entry extremely fast: M-RET to create a new headline,
  606. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  607. When the cursor is at the end of an empty plain list item, one TAB will
  608. make it a subitem, two or more tabs will back up to make this an item
  609. higher up in the item hierarchy."
  610. :group 'org-cycle
  611. :type 'boolean)
  612. (defcustom org-cycle-emulate-tab t
  613. "Where should `org-cycle' emulate TAB.
  614. nil Never
  615. white Only in completely white lines
  616. whitestart Only at the beginning of lines, before the first non-white char
  617. t Everywhere except in headlines
  618. exc-hl-bol Everywhere except at the start of a headline
  619. If TAB is used in a place where it does not emulate TAB, the current subtree
  620. visibility is cycled."
  621. :group 'org-cycle
  622. :type '(choice (const :tag "Never" nil)
  623. (const :tag "Only in completely white lines" white)
  624. (const :tag "Before first char in a line" whitestart)
  625. (const :tag "Everywhere except in headlines" t)
  626. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  627. ))
  628. (defcustom org-cycle-separator-lines 2
  629. "Number of empty lines needed to keep an empty line between collapsed trees.
  630. If you leave an empty line between the end of a subtree and the following
  631. headline, this empty line is hidden when the subtree is folded.
  632. Org-mode will leave (exactly) one empty line visible if the number of
  633. empty lines is equal or larger to the number given in this variable.
  634. So the default 2 means, at least 2 empty lines after the end of a subtree
  635. are needed to produce free space between a collapsed subtree and the
  636. following headline.
  637. If the number is negative, and the number of empty lines is at least -N,
  638. all empty lines are shown.
  639. Special case: when 0, never leave empty lines in collapsed view."
  640. :group 'org-cycle
  641. :type 'integer)
  642. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  643. (defcustom org-pre-cycle-hook nil
  644. "Hook that is run before visibility cycling is happening.
  645. The function(s) in this hook must accept a single argument which indicates
  646. the new state that will be set right after running this hook. The
  647. argument is a symbol. Before a global state change, it can have the values
  648. `overview', `content', or `all'. Before a local state change, it can have
  649. the values `folded', `children', or `subtree'."
  650. :group 'org-cycle
  651. :type 'hook)
  652. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  653. org-cycle-hide-drawers
  654. org-cycle-show-empty-lines
  655. org-optimize-window-after-visibility-change)
  656. "Hook that is run after `org-cycle' has changed the buffer visibility.
  657. The function(s) in this hook must accept a single argument which indicates
  658. the new state that was set by the most recent `org-cycle' command. The
  659. argument is a symbol. After a global state change, it can have the values
  660. `overview', `content', or `all'. After a local state change, it can have
  661. the values `folded', `children', or `subtree'."
  662. :group 'org-cycle
  663. :type 'hook)
  664. (defgroup org-edit-structure nil
  665. "Options concerning structure editing in Org-mode."
  666. :tag "Org Edit Structure"
  667. :group 'org-structure)
  668. (defcustom org-odd-levels-only nil
  669. "Non-nil means, skip even levels and only use odd levels for the outline.
  670. This has the effect that two stars are being added/taken away in
  671. promotion/demotion commands. It also influences how levels are
  672. handled by the exporters.
  673. Changing it requires restart of `font-lock-mode' to become effective
  674. for fontification also in regions already fontified.
  675. You may also set this on a per-file basis by adding one of the following
  676. lines to the buffer:
  677. #+STARTUP: odd
  678. #+STARTUP: oddeven"
  679. :group 'org-edit-structure
  680. :group 'org-font-lock
  681. :type 'boolean)
  682. (defcustom org-adapt-indentation t
  683. "Non-nil means, adapt indentation to outline node level.
  684. When this variable is set, Org assumes that you write outlines by
  685. indenting text in each node to align with the headline (after the stars).
  686. The following issues are influenced by this variable:
  687. - When this is set and the *entire* text in an entry is indented, the
  688. indentation is increased by one space in a demotion command, and
  689. decreased by one in a promotion command. If any line in the entry
  690. body starts with text at column 0, indentation is not changed at all.
  691. - Property drawers and planning information is inserted indented when
  692. this variable s set. When nil, they will not be indented.
  693. - TAB indents a line relative to context. The lines below a headline
  694. will be indented when this variable is set.
  695. Note that this is all about true indentation, by adding and removing
  696. space characters. See also `org-indent.el' which does level-dependent
  697. indentation in a virtual way, i.e. at display time in Emacs."
  698. :group 'org-edit-structure
  699. :type 'boolean)
  700. (defcustom org-special-ctrl-a/e nil
  701. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  702. When t, `C-a' will bring back the cursor to the beginning of the
  703. headline text, i.e. after the stars and after a possible TODO keyword.
  704. In an item, this will be the position after the bullet.
  705. When the cursor is already at that position, another `C-a' will bring
  706. it to the beginning of the line.
  707. `C-e' will jump to the end of the headline, ignoring the presence of tags
  708. in the headline. A second `C-e' will then jump to the true end of the
  709. line, after any tags. This also means that, when this variable is
  710. non-nil, `C-e' also will never jump beyond the end of the heading of a
  711. folded section, i.e. not after the ellipses.
  712. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  713. going to the true line boundary first. Only a directly following, identical
  714. keypress will bring the cursor to the special positions.
  715. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  716. set separately."
  717. :group 'org-edit-structure
  718. :type '(choice
  719. (const :tag "off" nil)
  720. (const :tag "on: after stars/bullet and before tags first" t)
  721. (const :tag "reversed: true line boundary first" reversed)
  722. (cons :tag "Set C-a and C-e separately"
  723. (choice :tag "Special C-a"
  724. (const :tag "off" nil)
  725. (const :tag "on: after stars/bullet first" t)
  726. (const :tag "reversed: before stars/bullet first" reversed))
  727. (choice :tag "Special C-e"
  728. (const :tag "off" nil)
  729. (const :tag "on: before tags first" t)
  730. (const :tag "reversed: after tags first" reversed)))))
  731. (if (fboundp 'defvaralias)
  732. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  733. (defcustom org-special-ctrl-k nil
  734. "Non-nil means `C-k' will behave specially in headlines.
  735. When nil, `C-k' will call the default `kill-line' command.
  736. When t, the following will happen while the cursor is in the headline:
  737. - When the cursor is at the beginning of a headline, kill the entire
  738. line and possible the folded subtree below the line.
  739. - When in the middle of the headline text, kill the headline up to the tags.
  740. - When after the headline text, kill the tags."
  741. :group 'org-edit-structure
  742. :type 'boolean)
  743. (defcustom org-yank-folded-subtrees t
  744. "Non-nil means, when yanking subtrees, fold them.
  745. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  746. it starts with a heading and all other headings in it are either children
  747. or siblings, then fold all the subtrees. However, do this only if no
  748. text after the yank would be swallowed into a folded tree by this action."
  749. :group 'org-edit-structure
  750. :type 'boolean)
  751. (defcustom org-yank-adjusted-subtrees nil
  752. "Non-nil means, when yanking subtrees, adjust the level.
  753. With this setting, `org-paste-subtree' is used to insert the subtree, see
  754. this function for details."
  755. :group 'org-edit-structure
  756. :type 'boolean)
  757. (defcustom org-M-RET-may-split-line '((default . t))
  758. "Non-nil means, M-RET will split the line at the cursor position.
  759. When nil, it will go to the end of the line before making a
  760. new line.
  761. You may also set this option in a different way for different
  762. contexts. Valid contexts are:
  763. headline when creating a new headline
  764. item when creating a new item
  765. table in a table field
  766. default the value to be used for all contexts not explicitly
  767. customized"
  768. :group 'org-structure
  769. :group 'org-table
  770. :type '(choice
  771. (const :tag "Always" t)
  772. (const :tag "Never" nil)
  773. (repeat :greedy t :tag "Individual contexts"
  774. (cons
  775. (choice :tag "Context"
  776. (const headline)
  777. (const item)
  778. (const table)
  779. (const default))
  780. (boolean)))))
  781. (defcustom org-insert-heading-respect-content nil
  782. "Non-nil means, insert new headings after the current subtree.
  783. When nil, the new heading is created directly after the current line.
  784. The commands \\[org-insert-heading-respect-content] and
  785. \\[org-insert-todo-heading-respect-content] turn this variable on
  786. for the duration of the command."
  787. :group 'org-structure
  788. :type 'boolean)
  789. (defcustom org-blank-before-new-entry '((heading . auto)
  790. (plain-list-item . auto))
  791. "Should `org-insert-heading' leave a blank line before new heading/item?
  792. The value is an alist, with `heading' and `plain-list-item' as car,
  793. and a boolean flag as cdr. For plain lists, if the variable
  794. `org-empty-line-terminates-plain-lists' is set, the setting here
  795. is ignored and no empty line is inserted, to keep the list in tact."
  796. :group 'org-edit-structure
  797. :type '(list
  798. (cons (const heading)
  799. (choice (const :tag "Never" nil)
  800. (const :tag "Always" t)
  801. (const :tag "Auto" auto)))
  802. (cons (const plain-list-item)
  803. (choice (const :tag "Never" nil)
  804. (const :tag "Always" t)
  805. (const :tag "Auto" auto)))))
  806. (defcustom org-insert-heading-hook nil
  807. "Hook being run after inserting a new heading."
  808. :group 'org-edit-structure
  809. :type 'hook)
  810. (defcustom org-enable-fixed-width-editor t
  811. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  812. This currently only means, they are never auto-wrapped.
  813. When nil, such lines will be treated like ordinary lines.
  814. See also the QUOTE keyword."
  815. :group 'org-edit-structure
  816. :type 'boolean)
  817. (defcustom org-goto-auto-isearch t
  818. "Non-nil means, typing characters in org-goto starts incremental search."
  819. :group 'org-edit-structure
  820. :type 'boolean)
  821. (defgroup org-sparse-trees nil
  822. "Options concerning sparse trees in Org-mode."
  823. :tag "Org Sparse Trees"
  824. :group 'org-structure)
  825. (defcustom org-highlight-sparse-tree-matches t
  826. "Non-nil means, highlight all matches that define a sparse tree.
  827. The highlights will automatically disappear the next time the buffer is
  828. changed by an edit command."
  829. :group 'org-sparse-trees
  830. :type 'boolean)
  831. (defcustom org-remove-highlights-with-change t
  832. "Non-nil means, any change to the buffer will remove temporary highlights.
  833. Such highlights are created by `org-occur' and `org-clock-display'.
  834. When nil, `C-c C-c needs to be used to get rid of the highlights.
  835. The highlights created by `org-preview-latex-fragment' always need
  836. `C-c C-c' to be removed."
  837. :group 'org-sparse-trees
  838. :group 'org-time
  839. :type 'boolean)
  840. (defcustom org-occur-hook '(org-first-headline-recenter)
  841. "Hook that is run after `org-occur' has constructed a sparse tree.
  842. This can be used to recenter the window to show as much of the structure
  843. as possible."
  844. :group 'org-sparse-trees
  845. :type 'hook)
  846. (defgroup org-imenu-and-speedbar nil
  847. "Options concerning imenu and speedbar in Org-mode."
  848. :tag "Org Imenu and Speedbar"
  849. :group 'org-structure)
  850. (defcustom org-imenu-depth 2
  851. "The maximum level for Imenu access to Org-mode headlines.
  852. This also applied for speedbar access."
  853. :group 'org-imenu-and-speedbar
  854. :type 'integer)
  855. (defgroup org-table nil
  856. "Options concerning tables in Org-mode."
  857. :tag "Org Table"
  858. :group 'org)
  859. (defcustom org-enable-table-editor 'optimized
  860. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  861. When nil, such lines will be treated like ordinary lines.
  862. When equal to the symbol `optimized', the table editor will be optimized to
  863. do the following:
  864. - Automatic overwrite mode in front of whitespace in table fields.
  865. This makes the structure of the table stay in tact as long as the edited
  866. field does not exceed the column width.
  867. - Minimize the number of realigns. Normally, the table is aligned each time
  868. TAB or RET are pressed to move to another field. With optimization this
  869. happens only if changes to a field might have changed the column width.
  870. Optimization requires replacing the functions `self-insert-command',
  871. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  872. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  873. very good at guessing when a re-align will be necessary, but you can always
  874. force one with \\[org-ctrl-c-ctrl-c].
  875. If you would like to use the optimized version in Org-mode, but the
  876. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  877. This variable can be used to turn on and off the table editor during a session,
  878. but in order to toggle optimization, a restart is required.
  879. See also the variable `org-table-auto-blank-field'."
  880. :group 'org-table
  881. :type '(choice
  882. (const :tag "off" nil)
  883. (const :tag "on" t)
  884. (const :tag "on, optimized" optimized)))
  885. (defcustom org-self-insert-cluster-for-undo t
  886. "Non-nil means cluster self-insert commands for undo when possible.
  887. If this is set, then, like in the Emacs command loop, 20 consecutive
  888. characters will be undone together.
  889. This is configurable, because there is some impact on typing performance."
  890. :group 'org-table
  891. :type 'boolean)
  892. (defcustom org-table-tab-recognizes-table.el t
  893. "Non-nil means, TAB will automatically notice a table.el table.
  894. When it sees such a table, it moves point into it and - if necessary -
  895. calls `table-recognize-table'."
  896. :group 'org-table-editing
  897. :type 'boolean)
  898. (defgroup org-link nil
  899. "Options concerning links in Org-mode."
  900. :tag "Org Link"
  901. :group 'org)
  902. (defvar org-link-abbrev-alist-local nil
  903. "Buffer-local version of `org-link-abbrev-alist', which see.
  904. The value of this is taken from the #+LINK lines.")
  905. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  906. (defcustom org-link-abbrev-alist nil
  907. "Alist of link abbreviations.
  908. The car of each element is a string, to be replaced at the start of a link.
  909. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  910. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  911. [[linkkey:tag][description]]
  912. The 'linkkey' must be a word word, starting with a letter, followed
  913. by letters, numbers, '-' or '_'.
  914. If REPLACE is a string, the tag will simply be appended to create the link.
  915. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  916. the placeholder \"%h\" will cause a url-encoded version of the tag to
  917. be inserted at that point (see the function `url-hexify-string').
  918. REPLACE may also be a function that will be called with the tag as the
  919. only argument to create the link, which should be returned as a string.
  920. See the manual for examples."
  921. :group 'org-link
  922. :type '(repeat
  923. (cons
  924. (string :tag "Protocol")
  925. (choice
  926. (string :tag "Format")
  927. (function)))))
  928. (defcustom org-descriptive-links t
  929. "Non-nil means, hide link part and only show description of bracket links.
  930. Bracket links are like [[link][description]]. This variable sets the initial
  931. state in new org-mode buffers. The setting can then be toggled on a
  932. per-buffer basis from the Org->Hyperlinks menu."
  933. :group 'org-link
  934. :type 'boolean)
  935. (defcustom org-link-file-path-type 'adaptive
  936. "How the path name in file links should be stored.
  937. Valid values are:
  938. relative Relative to the current directory, i.e. the directory of the file
  939. into which the link is being inserted.
  940. absolute Absolute path, if possible with ~ for home directory.
  941. noabbrev Absolute path, no abbreviation of home directory.
  942. adaptive Use relative path for files in the current directory and sub-
  943. directories of it. For other files, use an absolute path."
  944. :group 'org-link
  945. :type '(choice
  946. (const relative)
  947. (const absolute)
  948. (const noabbrev)
  949. (const adaptive)))
  950. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  951. "Types of links that should be activated in Org-mode files.
  952. This is a list of symbols, each leading to the activation of a certain link
  953. type. In principle, it does not hurt to turn on most link types - there may
  954. be a small gain when turning off unused link types. The types are:
  955. bracket The recommended [[link][description]] or [[link]] links with hiding.
  956. angular Links in angular brackets that may contain whitespace like
  957. <bbdb:Carsten Dominik>.
  958. plain Plain links in normal text, no whitespace, like http://google.com.
  959. radio Text that is matched by a radio target, see manual for details.
  960. tag Tag settings in a headline (link to tag search).
  961. date Time stamps (link to calendar).
  962. footnote Footnote labels.
  963. Changing this variable requires a restart of Emacs to become effective."
  964. :group 'org-link
  965. :type '(set :greedy t
  966. (const :tag "Double bracket links (new style)" bracket)
  967. (const :tag "Angular bracket links (old style)" angular)
  968. (const :tag "Plain text links" plain)
  969. (const :tag "Radio target matches" radio)
  970. (const :tag "Tags" tag)
  971. (const :tag "Timestamps" date)
  972. (const :tag "Footnotes" footnote)))
  973. (defcustom org-make-link-description-function nil
  974. "Function to use to generate link descriptions from links. If
  975. nil the link location will be used. This function must take two
  976. parameters; the first is the link and the second the description
  977. org-insert-link has generated, and should return the description
  978. to use."
  979. :group 'org-link
  980. :type 'function)
  981. (defgroup org-link-store nil
  982. "Options concerning storing links in Org-mode."
  983. :tag "Org Store Link"
  984. :group 'org-link)
  985. (defcustom org-email-link-description-format "Email %c: %.30s"
  986. "Format of the description part of a link to an email or usenet message.
  987. The following %-escapes will be replaced by corresponding information:
  988. %F full \"From\" field
  989. %f name, taken from \"From\" field, address if no name
  990. %T full \"To\" field
  991. %t first name in \"To\" field, address if no name
  992. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  993. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  994. %s subject
  995. %m message-id.
  996. You may use normal field width specification between the % and the letter.
  997. This is for example useful to limit the length of the subject.
  998. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  999. :group 'org-link-store
  1000. :type 'string)
  1001. (defcustom org-from-is-user-regexp
  1002. (let (r1 r2)
  1003. (when (and user-mail-address (not (string= user-mail-address "")))
  1004. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1005. (when (and user-full-name (not (string= user-full-name "")))
  1006. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1007. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1008. "Regexp matched against the \"From:\" header of an email or usenet message.
  1009. It should match if the message is from the user him/herself."
  1010. :group 'org-link-store
  1011. :type 'regexp)
  1012. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1013. "Non-nil means, storing a link to an Org file will use entry IDs.
  1014. Note that before this variable is even considered, org-id must be loaded,
  1015. so please customize `org-modules' and turn it on.
  1016. The variable can have the following values:
  1017. t Create an ID if needed to make a link to the current entry.
  1018. create-if-interactive
  1019. If `org-store-link' is called directly (interactively, as a user
  1020. command), do create an ID to support the link. But when doing the
  1021. job for remember, only use the ID if it already exists. The
  1022. purpose of this setting is to avoid proliferation of unwanted
  1023. IDs, just because you happen to be in an Org file when you
  1024. call `org-remember' that automatically and preemptively
  1025. creates a link. If you do want to get an ID link in a remember
  1026. template to an entry not having an ID, create it first by
  1027. explicitly creating a link to it, using `C-c C-l' first.
  1028. create-if-interactive-and-no-custom-id
  1029. Like create-if-interactive, but do not create an ID if there is
  1030. a CUSTOM_ID property defined in the entry. This is the default.
  1031. use-existing
  1032. Use existing ID, do not create one.
  1033. nil Never use an ID to make a link, instead link using a text search for
  1034. the headline text."
  1035. :group 'org-link-store
  1036. :type '(choice
  1037. (const :tag "Create ID to make link" t)
  1038. (const :tag "Create if storing link interactively"
  1039. create-if-interactive)
  1040. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1041. create-if-interactive-and-no-custom-id)
  1042. (const :tag "Only use existing" use-existing)
  1043. (const :tag "Do not use ID to create link" nil)))
  1044. (defcustom org-context-in-file-links t
  1045. "Non-nil means, file links from `org-store-link' contain context.
  1046. A search string will be added to the file name with :: as separator and
  1047. used to find the context when the link is activated by the command
  1048. `org-open-at-point'.
  1049. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1050. negates this setting for the duration of the command."
  1051. :group 'org-link-store
  1052. :type 'boolean)
  1053. (defcustom org-keep-stored-link-after-insertion nil
  1054. "Non-nil means, keep link in list for entire session.
  1055. The command `org-store-link' adds a link pointing to the current
  1056. location to an internal list. These links accumulate during a session.
  1057. The command `org-insert-link' can be used to insert links into any
  1058. Org-mode file (offering completion for all stored links). When this
  1059. option is nil, every link which has been inserted once using \\[org-insert-link]
  1060. will be removed from the list, to make completing the unused links
  1061. more efficient."
  1062. :group 'org-link-store
  1063. :type 'boolean)
  1064. (defgroup org-link-follow nil
  1065. "Options concerning following links in Org-mode."
  1066. :tag "Org Follow Link"
  1067. :group 'org-link)
  1068. (defcustom org-link-translation-function nil
  1069. "Function to translate links with different syntax to Org syntax.
  1070. This can be used to translate links created for example by the Planner
  1071. or emacs-wiki packages to Org syntax.
  1072. The function must accept two parameters, a TYPE containing the link
  1073. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1074. which is everything after the link protocol. It should return a cons
  1075. with possibly modified values of type and path.
  1076. Org contains a function for this, so if you set this variable to
  1077. `org-translate-link-from-planner', you should be able follow many
  1078. links created by planner."
  1079. :group 'org-link-follow
  1080. :type 'function)
  1081. (defcustom org-follow-link-hook nil
  1082. "Hook that is run after a link has been followed."
  1083. :group 'org-link-follow
  1084. :type 'hook)
  1085. (defcustom org-tab-follows-link nil
  1086. "Non-nil means, on links TAB will follow the link.
  1087. Needs to be set before org.el is loaded.
  1088. This really should not be used, it does not make sense, and the
  1089. implementation is bad."
  1090. :group 'org-link-follow
  1091. :type 'boolean)
  1092. (defcustom org-return-follows-link nil
  1093. "Non-nil means, on links RET will follow the link.
  1094. Needs to be set before org.el is loaded."
  1095. :group 'org-link-follow
  1096. :type 'boolean)
  1097. (defcustom org-mouse-1-follows-link
  1098. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1099. "Non-nil means, mouse-1 on a link will follow the link.
  1100. A longer mouse click will still set point. Does not work on XEmacs.
  1101. Needs to be set before org.el is loaded."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-mark-ring-length 4
  1105. "Number of different positions to be recorded in the ring
  1106. Changing this requires a restart of Emacs to work correctly."
  1107. :group 'org-link-follow
  1108. :type 'integer)
  1109. (defcustom org-link-frame-setup
  1110. '((vm . vm-visit-folder-other-frame)
  1111. (gnus . gnus-other-frame)
  1112. (file . find-file-other-window))
  1113. "Setup the frame configuration for following links.
  1114. When following a link with Emacs, it may often be useful to display
  1115. this link in another window or frame. This variable can be used to
  1116. set this up for the different types of links.
  1117. For VM, use any of
  1118. `vm-visit-folder'
  1119. `vm-visit-folder-other-frame'
  1120. For Gnus, use any of
  1121. `gnus'
  1122. `gnus-other-frame'
  1123. `org-gnus-no-new-news'
  1124. For FILE, use any of
  1125. `find-file'
  1126. `find-file-other-window'
  1127. `find-file-other-frame'
  1128. For the calendar, use the variable `calendar-setup'.
  1129. For BBDB, it is currently only possible to display the matches in
  1130. another window."
  1131. :group 'org-link-follow
  1132. :type '(list
  1133. (cons (const vm)
  1134. (choice
  1135. (const vm-visit-folder)
  1136. (const vm-visit-folder-other-window)
  1137. (const vm-visit-folder-other-frame)))
  1138. (cons (const gnus)
  1139. (choice
  1140. (const gnus)
  1141. (const gnus-other-frame)
  1142. (const org-gnus-no-new-news)))
  1143. (cons (const file)
  1144. (choice
  1145. (const find-file)
  1146. (const find-file-other-window)
  1147. (const find-file-other-frame)))))
  1148. (defcustom org-display-internal-link-with-indirect-buffer nil
  1149. "Non-nil means, use indirect buffer to display infile links.
  1150. Activating internal links (from one location in a file to another location
  1151. in the same file) normally just jumps to the location. When the link is
  1152. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1153. another window. When this option is set, the other window actually displays
  1154. an indirect buffer clone of the current buffer, to avoid any visibility
  1155. changes to the current buffer."
  1156. :group 'org-link-follow
  1157. :type 'boolean)
  1158. (defcustom org-open-non-existing-files nil
  1159. "Non-nil means, `org-open-file' will open non-existing files.
  1160. When nil, an error will be generated.
  1161. This variable applies only to external applications because they
  1162. might choke on non-existing files. If the link is to a file that
  1163. will be opened in Emacs, the variable is ignored."
  1164. :group 'org-link-follow
  1165. :type 'boolean)
  1166. (defcustom org-open-directory-means-index-dot-org nil
  1167. "Non-nil means, a link to a directory really means to index.org.
  1168. When nil, following a directory link will run dired or open a finder/explorer
  1169. window on that directory."
  1170. :group 'org-link-follow
  1171. :type 'boolean)
  1172. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1173. "Function and arguments to call for following mailto links.
  1174. This is a list with the first element being a lisp function, and the
  1175. remaining elements being arguments to the function. In string arguments,
  1176. %a will be replaced by the address, and %s will be replaced by the subject
  1177. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1178. :group 'org-link-follow
  1179. :type '(choice
  1180. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1181. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1182. (const :tag "message-mail" (message-mail "%a" "%s"))
  1183. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1184. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1185. "Non-nil means, ask for confirmation before executing shell links.
  1186. Shell links can be dangerous: just think about a link
  1187. [[shell:rm -rf ~/*][Google Search]]
  1188. This link would show up in your Org-mode document as \"Google Search\",
  1189. but really it would remove your entire home directory.
  1190. Therefore we advise against setting this variable to nil.
  1191. Just change it to `y-or-n-p' if you want to confirm with a
  1192. single keystroke rather than having to type \"yes\"."
  1193. :group 'org-link-follow
  1194. :type '(choice
  1195. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1196. (const :tag "with y-or-n (faster)" y-or-n-p)
  1197. (const :tag "no confirmation (dangerous)" nil)))
  1198. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1199. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1200. Elisp links can be dangerous: just think about a link
  1201. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1202. This link would show up in your Org-mode document as \"Google Search\",
  1203. but really it would remove your entire home directory.
  1204. Therefore we advise against setting this variable to nil.
  1205. Just change it to `y-or-n-p' if you want to confirm with a
  1206. single keystroke rather than having to type \"yes\"."
  1207. :group 'org-link-follow
  1208. :type '(choice
  1209. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1210. (const :tag "with y-or-n (faster)" y-or-n-p)
  1211. (const :tag "no confirmation (dangerous)" nil)))
  1212. (defconst org-file-apps-defaults-gnu
  1213. '((remote . emacs)
  1214. (system . mailcap)
  1215. (t . mailcap))
  1216. "Default file applications on a UNIX or GNU/Linux system.
  1217. See `org-file-apps'.")
  1218. (defconst org-file-apps-defaults-macosx
  1219. '((remote . emacs)
  1220. (t . "open %s")
  1221. (system . "open %s")
  1222. ("ps.gz" . "gv %s")
  1223. ("eps.gz" . "gv %s")
  1224. ("dvi" . "xdvi %s")
  1225. ("fig" . "xfig %s"))
  1226. "Default file applications on a MacOS X system.
  1227. The system \"open\" is known as a default, but we use X11 applications
  1228. for some files for which the OS does not have a good default.
  1229. See `org-file-apps'.")
  1230. (defconst org-file-apps-defaults-windowsnt
  1231. (list
  1232. '(remote . emacs)
  1233. (cons t
  1234. (list (if (featurep 'xemacs)
  1235. 'mswindows-shell-execute
  1236. 'w32-shell-execute)
  1237. "open" 'file))
  1238. (cons 'system
  1239. (list (if (featurep 'xemacs)
  1240. 'mswindows-shell-execute
  1241. 'w32-shell-execute)
  1242. "open" 'file)))
  1243. "Default file applications on a Windows NT system.
  1244. The system \"open\" is used for most files.
  1245. See `org-file-apps'.")
  1246. (defcustom org-file-apps
  1247. '(
  1248. (auto-mode . emacs)
  1249. ("\\.mm\\'" . default)
  1250. ("\\.x?html?\\'" . default)
  1251. ("\\.pdf\\'" . default)
  1252. )
  1253. "External applications for opening `file:path' items in a document.
  1254. Org-mode uses system defaults for different file types, but
  1255. you can use this variable to set the application for a given file
  1256. extension. The entries in this list are cons cells where the car identifies
  1257. files and the cdr the corresponding command. Possible values for the
  1258. file identifier are
  1259. \"regex\" Regular expression matched against the file name. For backward
  1260. compatibility, this can also be a string with only alphanumeric
  1261. characters, which is then interpreted as an extension.
  1262. `directory' Matches a directory
  1263. `remote' Matches a remote file, accessible through tramp or efs.
  1264. Remote files most likely should be visited through Emacs
  1265. because external applications cannot handle such paths.
  1266. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1267. so all files Emacs knows how to handle. Using this with
  1268. command `emacs' will open most files in Emacs. Beware that this
  1269. will also open html files inside Emacs, unless you add
  1270. (\"html\" . default) to the list as well.
  1271. t Default for files not matched by any of the other options.
  1272. `system' The system command to open files, like `open' on Windows
  1273. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1274. that will be selected if you call `C-c C-o' with a double
  1275. `C-u C-u' prefix.
  1276. Possible values for the command are:
  1277. `emacs' The file will be visited by the current Emacs process.
  1278. `default' Use the default application for this file type, which is the
  1279. association for t in the list, most likely in the system-specific
  1280. part.
  1281. This can be used to overrule an unwanted setting in the
  1282. system-specific variable.
  1283. `system' Use the system command for opening files, like \"open\".
  1284. This command is specified by the entry whose car is `system'.
  1285. Most likely, the system-specific version of this variable
  1286. does define this command, but you can overrule/replace it
  1287. here.
  1288. string A command to be executed by a shell; %s will be replaced
  1289. by the path to the file.
  1290. sexp A Lisp form which will be evaluated. The file path will
  1291. be available in the Lisp variable `file'.
  1292. For more examples, see the system specific constants
  1293. `org-file-apps-defaults-macosx'
  1294. `org-file-apps-defaults-windowsnt'
  1295. `org-file-apps-defaults-gnu'."
  1296. :group 'org-link-follow
  1297. :type '(repeat
  1298. (cons (choice :value ""
  1299. (string :tag "Extension")
  1300. (const :tag "System command to open files" system)
  1301. (const :tag "Default for unrecognized files" t)
  1302. (const :tag "Remote file" remote)
  1303. (const :tag "Links to a directory" directory)
  1304. (const :tag "Any files that have Emacs modes"
  1305. auto-mode))
  1306. (choice :value ""
  1307. (const :tag "Visit with Emacs" emacs)
  1308. (const :tag "Use default" default)
  1309. (const :tag "Use the system command" system)
  1310. (string :tag "Command")
  1311. (sexp :tag "Lisp form")))))
  1312. (defgroup org-refile nil
  1313. "Options concerning refiling entries in Org-mode."
  1314. :tag "Org Refile"
  1315. :group 'org)
  1316. (defcustom org-directory "~/org"
  1317. "Directory with org files.
  1318. This is just a default location to look for Org files. There is no need
  1319. at all to put your files into this directory. It is only used in the
  1320. following situations:
  1321. 1. When a remember template specifies a target file that is not an
  1322. absolute path. The path will then be interpreted relative to
  1323. `org-directory'
  1324. 2. When a remember note is filed away in an interactive way (when exiting the
  1325. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1326. with `org-directory' as the default path."
  1327. :group 'org-refile
  1328. :group 'org-remember
  1329. :type 'directory)
  1330. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1331. "Default target for storing notes.
  1332. Used by the hooks for remember.el. This can be a string, or nil to mean
  1333. the value of `remember-data-file'.
  1334. You can set this on a per-template basis with the variable
  1335. `org-remember-templates'."
  1336. :group 'org-refile
  1337. :group 'org-remember
  1338. :type '(choice
  1339. (const :tag "Default from remember-data-file" nil)
  1340. file))
  1341. (defcustom org-goto-interface 'outline
  1342. "The default interface to be used for `org-goto'.
  1343. Allowed values are:
  1344. outline The interface shows an outline of the relevant file
  1345. and the correct heading is found by moving through
  1346. the outline or by searching with incremental search.
  1347. outline-path-completion Headlines in the current buffer are offered via
  1348. completion. This is the interface also used by
  1349. the refile command."
  1350. :group 'org-refile
  1351. :type '(choice
  1352. (const :tag "Outline" outline)
  1353. (const :tag "Outline-path-completion" outline-path-completion)))
  1354. (defcustom org-goto-max-level 5
  1355. "Maximum level to be considered when running org-goto with refile interface."
  1356. :group 'org-refile
  1357. :type 'integer)
  1358. (defcustom org-reverse-note-order nil
  1359. "Non-nil means, store new notes at the beginning of a file or entry.
  1360. When nil, new notes will be filed to the end of a file or entry.
  1361. This can also be a list with cons cells of regular expressions that
  1362. are matched against file names, and values."
  1363. :group 'org-remember
  1364. :group 'org-refile
  1365. :type '(choice
  1366. (const :tag "Reverse always" t)
  1367. (const :tag "Reverse never" nil)
  1368. (repeat :tag "By file name regexp"
  1369. (cons regexp boolean))))
  1370. (defcustom org-refile-targets nil
  1371. "Targets for refiling entries with \\[org-refile].
  1372. This is list of cons cells. Each cell contains:
  1373. - a specification of the files to be considered, either a list of files,
  1374. or a symbol whose function or variable value will be used to retrieve
  1375. a file name or a list of file names. If you use `org-agenda-files' for
  1376. that, all agenda files will be scanned for targets. Nil means, consider
  1377. headings in the current buffer.
  1378. - A specification of how to find candidate refile targets. This may be
  1379. any of:
  1380. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1381. This tag has to be present in all target headlines, inheritance will
  1382. not be considered.
  1383. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1384. todo keyword.
  1385. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1386. headlines that are refiling targets.
  1387. - a cons cell (:level . N). Any headline of level N is considered a target.
  1388. Note that, when `org-odd-levels-only' is set, level corresponds to
  1389. order in hierarchy, not to the number of stars.
  1390. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1391. Note that, when `org-odd-levels-only' is set, level corresponds to
  1392. order in hierarchy, not to the number of stars.
  1393. You can set the variable `org-refile-target-verify-function' to a function
  1394. to verify each headline found by the simple critery above.
  1395. When this variable is nil, all top-level headlines in the current buffer
  1396. are used, equivalent to the value `((nil . (:level . 1))'."
  1397. :group 'org-refile
  1398. :type '(repeat
  1399. (cons
  1400. (choice :value org-agenda-files
  1401. (const :tag "All agenda files" org-agenda-files)
  1402. (const :tag "Current buffer" nil)
  1403. (function) (variable) (file))
  1404. (choice :tag "Identify target headline by"
  1405. (cons :tag "Specific tag" (const :value :tag) (string))
  1406. (cons :tag "TODO keyword" (const :value :todo) (string))
  1407. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1408. (cons :tag "Level number" (const :value :level) (integer))
  1409. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1410. (defcustom org-refile-target-verify-function nil
  1411. "Function to verify if the headline at point should be a refile target.
  1412. The function will be called without arguments, with point at the
  1413. beginning of the headline. It should return t and leave point
  1414. where it is if the headline is a valid target for refiling.
  1415. If the target should not be selected, the function must return nil.
  1416. In addition to this, it may move point to a place from where the search
  1417. should be continued. For example, the function may decide that the entire
  1418. subtree of the current entry should be excluded and move point to the end
  1419. of the subtree."
  1420. :group 'org-refile
  1421. :type 'function)
  1422. (defcustom org-refile-use-outline-path nil
  1423. "Non-nil means, provide refile targets as paths.
  1424. So a level 3 headline will be available as level1/level2/level3.
  1425. When the value is `file', also include the file name (without directory)
  1426. into the path. In this case, you can also stop the completion after
  1427. the file name, to get entries inserted as top level in the file.
  1428. When `full-file-path', include the full file path."
  1429. :group 'org-refile
  1430. :type '(choice
  1431. (const :tag "Not" nil)
  1432. (const :tag "Yes" t)
  1433. (const :tag "Start with file name" file)
  1434. (const :tag "Start with full file path" full-file-path)))
  1435. (defcustom org-outline-path-complete-in-steps t
  1436. "Non-nil means, complete the outline path in hierarchical steps.
  1437. When Org-mode uses the refile interface to select an outline path
  1438. \(see variable `org-refile-use-outline-path'), the completion of
  1439. the path can be done is a single go, or if can be done in steps down
  1440. the headline hierarchy. Going in steps is probably the best if you
  1441. do not use a special completion package like `ido' or `icicles'.
  1442. However, when using these packages, going in one step can be very
  1443. fast, while still showing the whole path to the entry."
  1444. :group 'org-refile
  1445. :type 'boolean)
  1446. (defcustom org-refile-allow-creating-parent-nodes nil
  1447. "Non-nil means, allow to create new nodes as refile targets.
  1448. New nodes are then created by adding \"/new node name\" to the completion
  1449. of an existing node. When the value of this variable is `confirm',
  1450. new node creation must be confirmed by the user (recommended)
  1451. When nil, the completion must match an existing entry.
  1452. Note that, if the new heading is not seen by the criteria
  1453. listed in `org-refile-targets', multiple instances of the same
  1454. heading would be created by trying again to file under the new
  1455. heading."
  1456. :group 'org-refile
  1457. :type '(choice
  1458. (const :tag "Never" nil)
  1459. (const :tag "Always" t)
  1460. (const :tag "Prompt for confirmation" confirm)))
  1461. (defgroup org-todo nil
  1462. "Options concerning TODO items in Org-mode."
  1463. :tag "Org TODO"
  1464. :group 'org)
  1465. (defgroup org-progress nil
  1466. "Options concerning Progress logging in Org-mode."
  1467. :tag "Org Progress"
  1468. :group 'org-time)
  1469. (defvar org-todo-interpretation-widgets
  1470. '(
  1471. (:tag "Sequence (cycling hits every state)" sequence)
  1472. (:tag "Type (cycling directly to DONE)" type))
  1473. "The available interpretation symbols for customizing
  1474. `org-todo-keywords'.
  1475. Interested libraries should add to this list.")
  1476. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1477. "List of TODO entry keyword sequences and their interpretation.
  1478. \\<org-mode-map>This is a list of sequences.
  1479. Each sequence starts with a symbol, either `sequence' or `type',
  1480. indicating if the keywords should be interpreted as a sequence of
  1481. action steps, or as different types of TODO items. The first
  1482. keywords are states requiring action - these states will select a headline
  1483. for inclusion into the global TODO list Org-mode produces. If one of
  1484. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1485. signify that no further action is necessary. If \"|\" is not found,
  1486. the last keyword is treated as the only DONE state of the sequence.
  1487. The command \\[org-todo] cycles an entry through these states, and one
  1488. additional state where no keyword is present. For details about this
  1489. cycling, see the manual.
  1490. TODO keywords and interpretation can also be set on a per-file basis with
  1491. the special #+SEQ_TODO and #+TYP_TODO lines.
  1492. Each keyword can optionally specify a character for fast state selection
  1493. \(in combination with the variable `org-use-fast-todo-selection')
  1494. and specifiers for state change logging, using the same syntax
  1495. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1496. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1497. indicates to record a time stamp each time this state is selected.
  1498. Each keyword may also specify if a timestamp or a note should be
  1499. recorded when entering or leaving the state, by adding additional
  1500. characters in the parenthesis after the keyword. This looks like this:
  1501. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1502. record only the time of the state change. With X and Y being either
  1503. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1504. Y when leaving the state if and only if the *target* state does not
  1505. define X. You may omit any of the fast-selection key or X or /Y,
  1506. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1507. For backward compatibility, this variable may also be just a list
  1508. of keywords - in this case the interpretation (sequence or type) will be
  1509. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1510. :group 'org-todo
  1511. :group 'org-keywords
  1512. :type '(choice
  1513. (repeat :tag "Old syntax, just keywords"
  1514. (string :tag "Keyword"))
  1515. (repeat :tag "New syntax"
  1516. (cons
  1517. (choice
  1518. :tag "Interpretation"
  1519. ;;Quick and dirty way to see
  1520. ;;`org-todo-interpretations'. This takes the
  1521. ;;place of item arguments
  1522. :convert-widget
  1523. (lambda (widget)
  1524. (widget-put widget
  1525. :args (mapcar
  1526. #'(lambda (x)
  1527. (widget-convert
  1528. (cons 'const x)))
  1529. org-todo-interpretation-widgets))
  1530. widget))
  1531. (repeat
  1532. (string :tag "Keyword"))))))
  1533. (defvar org-todo-keywords-1 nil
  1534. "All TODO and DONE keywords active in a buffer.")
  1535. (make-variable-buffer-local 'org-todo-keywords-1)
  1536. (defvar org-todo-keywords-for-agenda nil)
  1537. (defvar org-done-keywords-for-agenda nil)
  1538. (defvar org-drawers-for-agenda nil)
  1539. (defvar org-todo-keyword-alist-for-agenda nil)
  1540. (defvar org-tag-alist-for-agenda nil)
  1541. (defvar org-agenda-contributing-files nil)
  1542. (defvar org-not-done-keywords nil)
  1543. (make-variable-buffer-local 'org-not-done-keywords)
  1544. (defvar org-done-keywords nil)
  1545. (make-variable-buffer-local 'org-done-keywords)
  1546. (defvar org-todo-heads nil)
  1547. (make-variable-buffer-local 'org-todo-heads)
  1548. (defvar org-todo-sets nil)
  1549. (make-variable-buffer-local 'org-todo-sets)
  1550. (defvar org-todo-log-states nil)
  1551. (make-variable-buffer-local 'org-todo-log-states)
  1552. (defvar org-todo-kwd-alist nil)
  1553. (make-variable-buffer-local 'org-todo-kwd-alist)
  1554. (defvar org-todo-key-alist nil)
  1555. (make-variable-buffer-local 'org-todo-key-alist)
  1556. (defvar org-todo-key-trigger nil)
  1557. (make-variable-buffer-local 'org-todo-key-trigger)
  1558. (defcustom org-todo-interpretation 'sequence
  1559. "Controls how TODO keywords are interpreted.
  1560. This variable is in principle obsolete and is only used for
  1561. backward compatibility, if the interpretation of todo keywords is
  1562. not given already in `org-todo-keywords'. See that variable for
  1563. more information."
  1564. :group 'org-todo
  1565. :group 'org-keywords
  1566. :type '(choice (const sequence)
  1567. (const type)))
  1568. (defcustom org-use-fast-todo-selection t
  1569. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1570. This variable describes if and under what circumstances the cycling
  1571. mechanism for TODO keywords will be replaced by a single-key, direct
  1572. selection scheme.
  1573. When nil, fast selection is never used.
  1574. When the symbol `prefix', it will be used when `org-todo' is called with
  1575. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1576. in an agenda buffer.
  1577. When t, fast selection is used by default. In this case, the prefix
  1578. argument forces cycling instead.
  1579. In all cases, the special interface is only used if access keys have actually
  1580. been assigned by the user, i.e. if keywords in the configuration are followed
  1581. by a letter in parenthesis, like TODO(t)."
  1582. :group 'org-todo
  1583. :type '(choice
  1584. (const :tag "Never" nil)
  1585. (const :tag "By default" t)
  1586. (const :tag "Only with C-u C-c C-t" prefix)))
  1587. (defcustom org-provide-todo-statistics t
  1588. "Non-nil means, update todo statistics after insert and toggle.
  1589. ALL-HEADLINES means update todo statistics by including headlines
  1590. with no TODO keyword as well, counting them as not done.
  1591. A list of TODO keywords means the same, but skip keywords that are
  1592. not in this list.
  1593. When this is set, todo statistics is updated in the parent of the
  1594. current entry each time a todo state is changed."
  1595. :group 'org-todo
  1596. :type '(choice
  1597. (const :tag "Yes, only for TODO entries" t)
  1598. (const :tag "Yes, including all entries" 'all-headlines)
  1599. (repeat :tag "Yes, for TODOs in this list"
  1600. (string :tag "TODO keyword"))
  1601. (other :tag "No TODO statistics" nil)))
  1602. (defcustom org-hierarchical-todo-statistics t
  1603. "Non-nil means, TODO statistics covers just direct children.
  1604. When nil, all entries in the subtree are considered.
  1605. This has only an effect if `org-provide-todo-statistics' is set.
  1606. To set this to nil for only a single subtree, use a COOKIE_DATA
  1607. property and include the word \"recursive\" into the value."
  1608. :group 'org-todo
  1609. :type 'boolean)
  1610. (defcustom org-after-todo-state-change-hook nil
  1611. "Hook which is run after the state of a TODO item was changed.
  1612. The new state (a string with a TODO keyword, or nil) is available in the
  1613. Lisp variable `state'."
  1614. :group 'org-todo
  1615. :type 'hook)
  1616. (defvar org-blocker-hook nil
  1617. "Hook for functions that are allowed to block a state change.
  1618. Each function gets as its single argument a property list, see
  1619. `org-trigger-hook' for more information about this list.
  1620. If any of the functions in this hook returns nil, the state change
  1621. is blocked.")
  1622. (defvar org-trigger-hook nil
  1623. "Hook for functions that are triggered by a state change.
  1624. Each function gets as its single argument a property list with at least
  1625. the following elements:
  1626. (:type type-of-change :position pos-at-entry-start
  1627. :from old-state :to new-state)
  1628. Depending on the type, more properties may be present.
  1629. This mechanism is currently implemented for:
  1630. TODO state changes
  1631. ------------------
  1632. :type todo-state-change
  1633. :from previous state (keyword as a string), or nil, or a symbol
  1634. 'todo' or 'done', to indicate the general type of state.
  1635. :to new state, like in :from")
  1636. (defcustom org-enforce-todo-dependencies nil
  1637. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1638. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1639. be blocked if any prior sibling is not yet done.
  1640. Finally, if the parent is blocked because of ordered siblings of its own,
  1641. the child will also be blocked.
  1642. This variable needs to be set before org.el is loaded, and you need to
  1643. restart Emacs after a change to make the change effective. The only way
  1644. to change is while Emacs is running is through the customize interface."
  1645. :set (lambda (var val)
  1646. (set var val)
  1647. (if val
  1648. (add-hook 'org-blocker-hook
  1649. 'org-block-todo-from-children-or-siblings-or-parent)
  1650. (remove-hook 'org-blocker-hook
  1651. 'org-block-todo-from-children-or-siblings-or-parent)))
  1652. :group 'org-todo
  1653. :type 'boolean)
  1654. (defcustom org-enforce-todo-checkbox-dependencies nil
  1655. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1656. When this is nil, checkboxes have no influence on switching TODO states.
  1657. When non-nil, you first need to check off all check boxes before the TODO
  1658. entry can be switched to DONE.
  1659. This variable needs to be set before org.el is loaded, and you need to
  1660. restart Emacs after a change to make the change effective. The only way
  1661. to change is while Emacs is running is through the customize interface."
  1662. :set (lambda (var val)
  1663. (set var val)
  1664. (if val
  1665. (add-hook 'org-blocker-hook
  1666. 'org-block-todo-from-checkboxes)
  1667. (remove-hook 'org-blocker-hook
  1668. 'org-block-todo-from-checkboxes)))
  1669. :group 'org-todo
  1670. :type 'boolean)
  1671. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1672. "Non-nil means, inserting a TODO heading is treated as state change.
  1673. So when the command \\[org-insert-todo-heading] is used, state change
  1674. logging will apply if appropriate. When nil, the new TODO item will
  1675. be inserted directly, and no logging will take place."
  1676. :group 'org-todo
  1677. :type 'boolean)
  1678. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1679. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1680. This is the default behavior. However, setting this to nil allows a
  1681. convenient way to select a TODO state and bypass any logging associated
  1682. with that."
  1683. :group 'org-todo
  1684. :type 'boolean)
  1685. (defcustom org-todo-state-tags-triggers nil
  1686. "Tag changes that should be triggered by TODO state changes.
  1687. This is a list. Each entry is
  1688. (state-change (tag . flag) .......)
  1689. State-change can be a string with a state, and empty string to indicate the
  1690. state that has no TODO keyword, or it can be one of the symbols `todo'
  1691. or `done', meaning any not-done or done state, respectively."
  1692. :group 'org-todo
  1693. :group 'org-tags
  1694. :type '(repeat
  1695. (cons (choice :tag "When changing to"
  1696. (const :tag "Not-done state" todo)
  1697. (const :tag "Done state" done)
  1698. (string :tag "State"))
  1699. (repeat
  1700. (cons :tag "Tag action"
  1701. (string :tag "Tag")
  1702. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1703. (defcustom org-log-done nil
  1704. "Information to record when a task moves to the DONE state.
  1705. Possible values are:
  1706. nil Don't add anything, just change the keyword
  1707. time Add a time stamp to the task
  1708. note Prompt for a note and add it with template `org-log-note-headings'
  1709. This option can also be set with on a per-file-basis with
  1710. #+STARTUP: nologdone
  1711. #+STARTUP: logdone
  1712. #+STARTUP: lognotedone
  1713. You can have local logging settings for a subtree by setting the LOGGING
  1714. property to one or more of these keywords."
  1715. :group 'org-todo
  1716. :group 'org-progress
  1717. :type '(choice
  1718. (const :tag "No logging" nil)
  1719. (const :tag "Record CLOSED timestamp" time)
  1720. (const :tag "Record CLOSED timestamp with note." note)))
  1721. ;; Normalize old uses of org-log-done.
  1722. (cond
  1723. ((eq org-log-done t) (setq org-log-done 'time))
  1724. ((and (listp org-log-done) (memq 'done org-log-done))
  1725. (setq org-log-done 'note)))
  1726. (defcustom org-log-reschedule nil
  1727. "Information to record when the scheduling date of a tasks is modified.
  1728. Possible values are:
  1729. nil Don't add anything, just change the date
  1730. time Add a time stamp to the task
  1731. note Prompt for a note and add it with template `org-log-note-headings'
  1732. This option can also be set with on a per-file-basis with
  1733. #+STARTUP: nologreschedule
  1734. #+STARTUP: logreschedule
  1735. #+STARTUP: lognotereschedule"
  1736. :group 'org-todo
  1737. :group 'org-progress
  1738. :type '(choice
  1739. (const :tag "No logging" nil)
  1740. (const :tag "Record timestamp" time)
  1741. (const :tag "Record timestamp with note." note)))
  1742. (defcustom org-log-redeadline nil
  1743. "Information to record when the deadline date of a tasks is modified.
  1744. Possible values are:
  1745. nil Don't add anything, just change the date
  1746. time Add a time stamp to the task
  1747. note Prompt for a note and add it with template `org-log-note-headings'
  1748. This option can also be set with on a per-file-basis with
  1749. #+STARTUP: nologredeadline
  1750. #+STARTUP: logredeadline
  1751. #+STARTUP: lognoteredeadline
  1752. You can have local logging settings for a subtree by setting the LOGGING
  1753. property to one or more of these keywords."
  1754. :group 'org-todo
  1755. :group 'org-progress
  1756. :type '(choice
  1757. (const :tag "No logging" nil)
  1758. (const :tag "Record timestamp" time)
  1759. (const :tag "Record timestamp with note." note)))
  1760. (defcustom org-log-note-clock-out nil
  1761. "Non-nil means, record a note when clocking out of an item.
  1762. This can also be configured on a per-file basis by adding one of
  1763. the following lines anywhere in the buffer:
  1764. #+STARTUP: lognoteclock-out
  1765. #+STARTUP: nolognoteclock-out"
  1766. :group 'org-todo
  1767. :group 'org-progress
  1768. :type 'boolean)
  1769. (defcustom org-log-done-with-time t
  1770. "Non-nil means, the CLOSED time stamp will contain date and time.
  1771. When nil, only the date will be recorded."
  1772. :group 'org-progress
  1773. :type 'boolean)
  1774. (defcustom org-log-note-headings
  1775. '((done . "CLOSING NOTE %t")
  1776. (state . "State %-12s from %-12S %t")
  1777. (note . "Note taken on %t")
  1778. (reschedule . "Rescheduled from %S on %t")
  1779. (redeadline . "New deadline from %S on %t")
  1780. (clock-out . ""))
  1781. "Headings for notes added to entries.
  1782. The value is an alist, with the car being a symbol indicating the note
  1783. context, and the cdr is the heading to be used. The heading may also be the
  1784. empty string.
  1785. %t in the heading will be replaced by a time stamp.
  1786. %s will be replaced by the new TODO state, in double quotes.
  1787. %S will be replaced by the old TODO state, in double quotes.
  1788. %u will be replaced by the user name.
  1789. %U will be replaced by the full user name."
  1790. :group 'org-todo
  1791. :group 'org-progress
  1792. :type '(list :greedy t
  1793. (cons (const :tag "Heading when closing an item" done) string)
  1794. (cons (const :tag
  1795. "Heading when changing todo state (todo sequence only)"
  1796. state) string)
  1797. (cons (const :tag "Heading when just taking a note" note) string)
  1798. (cons (const :tag "Heading when clocking out" clock-out) string)
  1799. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1800. (cons (const :tag "Heading when changing deadline" redeadline) string)))
  1801. (unless (assq 'note org-log-note-headings)
  1802. (push '(note . "%t") org-log-note-headings))
  1803. (defcustom org-log-into-drawer nil
  1804. "Non-nil means, insert state change notes and time stamps into a drawer.
  1805. When nil, state changes notes will be inserted after the headline and
  1806. any scheduling and clock lines, but not inside a drawer.
  1807. The value of this variable should be the name of the drawer to use.
  1808. LOGBOOK is proposed at the default drawer for this purpose, you can
  1809. also set this to a string to define the drawer of your choice.
  1810. A value of t is also allowed, representing \"LOGBOOK\".
  1811. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1812. will be ignored.
  1813. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1814. a subtree."
  1815. :group 'org-todo
  1816. :group 'org-progress
  1817. :type '(choice
  1818. (const :tag "Not into a drawer" nil)
  1819. (const :tag "LOGBOOK" t)
  1820. (string :tag "Other")))
  1821. (if (fboundp 'defvaralias)
  1822. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1823. (defun org-log-into-drawer ()
  1824. "Return the value of `org-log-into-drawer', but let properties overrule.
  1825. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1826. used instead of the default value."
  1827. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1828. (cond
  1829. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1830. ((equal p "t") "LOGBOOK")
  1831. (t p))))
  1832. (defcustom org-log-state-notes-insert-after-drawers nil
  1833. "Non-nil means, insert state change notes after any drawers in entry.
  1834. Only the drawers that *immediately* follow the headline and the
  1835. deadline/scheduled line are skipped.
  1836. When nil, insert notes right after the heading and perhaps the line
  1837. with deadline/scheduling if present.
  1838. This variable will have no effect if `org-log-into-drawer' is
  1839. set."
  1840. :group 'org-todo
  1841. :group 'org-progress
  1842. :type 'boolean)
  1843. (defcustom org-log-states-order-reversed t
  1844. "Non-nil means, the latest state change note will be directly after heading.
  1845. When nil, the notes will be orderer according to time."
  1846. :group 'org-todo
  1847. :group 'org-progress
  1848. :type 'boolean)
  1849. (defcustom org-log-repeat 'time
  1850. "Non-nil means, record moving through the DONE state when triggering repeat.
  1851. An auto-repeating task is immediately switched back to TODO when
  1852. marked DONE. If you are not logging state changes (by adding \"@\"
  1853. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1854. record a closing note, there will be no record of the task moving
  1855. through DONE. This variable forces taking a note anyway.
  1856. nil Don't force a record
  1857. time Record a time stamp
  1858. note Record a note
  1859. This option can also be set with on a per-file-basis with
  1860. #+STARTUP: logrepeat
  1861. #+STARTUP: lognoterepeat
  1862. #+STARTUP: nologrepeat
  1863. You can have local logging settings for a subtree by setting the LOGGING
  1864. property to one or more of these keywords."
  1865. :group 'org-todo
  1866. :group 'org-progress
  1867. :type '(choice
  1868. (const :tag "Don't force a record" nil)
  1869. (const :tag "Force recording the DONE state" time)
  1870. (const :tag "Force recording a note with the DONE state" note)))
  1871. (defgroup org-priorities nil
  1872. "Priorities in Org-mode."
  1873. :tag "Org Priorities"
  1874. :group 'org-todo)
  1875. (defcustom org-enable-priority-commands t
  1876. "Non-nil means, priority commands are active.
  1877. When nil, these commands will be disabled, so that you never accidentally
  1878. set a priority."
  1879. :group 'org-priorities
  1880. :type 'boolean)
  1881. (defcustom org-highest-priority ?A
  1882. "The highest priority of TODO items. A character like ?A, ?B etc.
  1883. Must have a smaller ASCII number than `org-lowest-priority'."
  1884. :group 'org-priorities
  1885. :type 'character)
  1886. (defcustom org-lowest-priority ?C
  1887. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1888. Must have a larger ASCII number than `org-highest-priority'."
  1889. :group 'org-priorities
  1890. :type 'character)
  1891. (defcustom org-default-priority ?B
  1892. "The default priority of TODO items.
  1893. This is the priority an item get if no explicit priority is given."
  1894. :group 'org-priorities
  1895. :type 'character)
  1896. (defcustom org-priority-start-cycle-with-default t
  1897. "Non-nil means, start with default priority when starting to cycle.
  1898. When this is nil, the first step in the cycle will be (depending on the
  1899. command used) one higher or lower that the default priority."
  1900. :group 'org-priorities
  1901. :type 'boolean)
  1902. (defgroup org-time nil
  1903. "Options concerning time stamps and deadlines in Org-mode."
  1904. :tag "Org Time"
  1905. :group 'org)
  1906. (defcustom org-insert-labeled-timestamps-at-point nil
  1907. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1908. When nil, these labeled time stamps are forces into the second line of an
  1909. entry, just after the headline. When scheduling from the global TODO list,
  1910. the time stamp will always be forced into the second line."
  1911. :group 'org-time
  1912. :type 'boolean)
  1913. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1914. "Formats for `format-time-string' which are used for time stamps.
  1915. It is not recommended to change this constant.")
  1916. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1917. "Number of minutes to round time stamps to.
  1918. These are two values, the first applies when first creating a time stamp.
  1919. The second applies when changing it with the commands `S-up' and `S-down'.
  1920. When changing the time stamp, this means that it will change in steps
  1921. of N minutes, as given by the second value.
  1922. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1923. numbers should be factors of 60, so for example 5, 10, 15.
  1924. When this is larger than 1, you can still force an exact time-stamp by using
  1925. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1926. and by using a prefix arg to `S-up/down' to specify the exact number
  1927. of minutes to shift."
  1928. :group 'org-time
  1929. :get '(lambda (var) ; Make sure all entries have 5 elements
  1930. (if (integerp (default-value var))
  1931. (list (default-value var) 5)
  1932. (default-value var)))
  1933. :type '(list
  1934. (integer :tag "when inserting times")
  1935. (integer :tag "when modifying times")))
  1936. ;; Normalize old customizations of this variable.
  1937. (when (integerp org-time-stamp-rounding-minutes)
  1938. (setq org-time-stamp-rounding-minutes
  1939. (list org-time-stamp-rounding-minutes
  1940. org-time-stamp-rounding-minutes)))
  1941. (defcustom org-display-custom-times nil
  1942. "Non-nil means, overlay custom formats over all time stamps.
  1943. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1944. To turn this on on a per-file basis, insert anywhere in the file:
  1945. #+STARTUP: customtime"
  1946. :group 'org-time
  1947. :set 'set-default
  1948. :type 'sexp)
  1949. (make-variable-buffer-local 'org-display-custom-times)
  1950. (defcustom org-time-stamp-custom-formats
  1951. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1952. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1953. These are overlayed over the default ISO format if the variable
  1954. `org-display-custom-times' is set. Time like %H:%M should be at the
  1955. end of the second format. The custom formats are also honored by export
  1956. commands, if custom time display is turned on at the time of export."
  1957. :group 'org-time
  1958. :type 'sexp)
  1959. (defun org-time-stamp-format (&optional long inactive)
  1960. "Get the right format for a time string."
  1961. (let ((f (if long (cdr org-time-stamp-formats)
  1962. (car org-time-stamp-formats))))
  1963. (if inactive
  1964. (concat "[" (substring f 1 -1) "]")
  1965. f)))
  1966. (defcustom org-time-clocksum-format "%d:%02d"
  1967. "The format string used when creating CLOCKSUM lines, or when
  1968. org-mode generates a time duration."
  1969. :group 'org-time
  1970. :type 'string)
  1971. (defcustom org-time-clocksum-use-fractional nil
  1972. "If non-nil, \\[org-clock-display] uses fractional times.
  1973. org-mode generates a time duration."
  1974. :group 'org-time
  1975. :type 'boolean)
  1976. (defcustom org-time-clocksum-fractional-format "%.2f"
  1977. "The format string used when creating CLOCKSUM lines, or when
  1978. org-mode generates a time duration."
  1979. :group 'org-time
  1980. :type 'string)
  1981. (defcustom org-deadline-warning-days 14
  1982. "No. of days before expiration during which a deadline becomes active.
  1983. This variable governs the display in sparse trees and in the agenda.
  1984. When 0 or negative, it means use this number (the absolute value of it)
  1985. even if a deadline has a different individual lead time specified.
  1986. Custom commands can set this variable in the options section."
  1987. :group 'org-time
  1988. :group 'org-agenda-daily/weekly
  1989. :type 'integer)
  1990. (defcustom org-read-date-prefer-future t
  1991. "Non-nil means, assume future for incomplete date input from user.
  1992. This affects the following situations:
  1993. 1. The user gives a month but not a year.
  1994. For example, if it is april and you enter \"feb 2\", this will be read
  1995. as feb 2, *next* year. \"May 5\", however, will be this year.
  1996. 2. The user gives a day, but no month.
  1997. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1998. read this as the third of *next* month. However, if you enter \"17\",
  1999. it will be considered as *this* month.
  2000. If you set this variable to the symbol `time', then also the following
  2001. will work:
  2002. 3. If the user gives a time, but no day. If the time is before now,
  2003. to will be interpreted as tomorrow.
  2004. Currently none of this works for ISO week specifications.
  2005. When this option is nil, the current day, month and year will always be
  2006. used as defaults."
  2007. :group 'org-time
  2008. :type '(choice
  2009. (const :tag "Never" nil)
  2010. (const :tag "Check month and day" t)
  2011. (const :tag "Check month, day, and time" time)))
  2012. (defcustom org-read-date-display-live t
  2013. "Non-nil means, display current interpretation of date prompt live.
  2014. This display will be in an overlay, in the minibuffer."
  2015. :group 'org-time
  2016. :type 'boolean)
  2017. (defcustom org-read-date-popup-calendar t
  2018. "Non-nil means, pop up a calendar when prompting for a date.
  2019. In the calendar, the date can be selected with mouse-1. However, the
  2020. minibuffer will also be active, and you can simply enter the date as well.
  2021. When nil, only the minibuffer will be available."
  2022. :group 'org-time
  2023. :type 'boolean)
  2024. (if (fboundp 'defvaralias)
  2025. (defvaralias 'org-popup-calendar-for-date-prompt
  2026. 'org-read-date-popup-calendar))
  2027. (defcustom org-read-date-minibuffer-setup-hook nil
  2028. "Hook to be used to set up keys for the date/time interface.
  2029. Add key definitions to `minibuffer-local-map', which will be a temporary
  2030. copy."
  2031. :group 'org-time
  2032. :type 'hook)
  2033. (defcustom org-extend-today-until 0
  2034. "The hour when your day really ends. Must be an integer.
  2035. This has influence for the following applications:
  2036. - When switching the agenda to \"today\". It it is still earlier than
  2037. the time given here, the day recognized as TODAY is actually yesterday.
  2038. - When a date is read from the user and it is still before the time given
  2039. here, the current date and time will be assumed to be yesterday, 23:59.
  2040. Also, timestamps inserted in remember templates follow this rule.
  2041. IMPORTANT: This is a feature whose implementation is and likely will
  2042. remain incomplete. Really, it is only here because past midnight seems to
  2043. be the favorite working time of John Wiegley :-)"
  2044. :group 'org-time
  2045. :type 'integer)
  2046. (defcustom org-edit-timestamp-down-means-later nil
  2047. "Non-nil means, S-down will increase the time in a time stamp.
  2048. When nil, S-up will increase."
  2049. :group 'org-time
  2050. :type 'boolean)
  2051. (defcustom org-calendar-follow-timestamp-change t
  2052. "Non-nil means, make the calendar window follow timestamp changes.
  2053. When a timestamp is modified and the calendar window is visible, it will be
  2054. moved to the new date."
  2055. :group 'org-time
  2056. :type 'boolean)
  2057. (defgroup org-tags nil
  2058. "Options concerning tags in Org-mode."
  2059. :tag "Org Tags"
  2060. :group 'org)
  2061. (defcustom org-tag-alist nil
  2062. "List of tags allowed in Org-mode files.
  2063. When this list is nil, Org-mode will base TAG input on what is already in the
  2064. buffer.
  2065. The value of this variable is an alist, the car of each entry must be a
  2066. keyword as a string, the cdr may be a character that is used to select
  2067. that tag through the fast-tag-selection interface.
  2068. See the manual for details."
  2069. :group 'org-tags
  2070. :type '(repeat
  2071. (choice
  2072. (cons (string :tag "Tag name")
  2073. (character :tag "Access char"))
  2074. (list :tag "Start radio group"
  2075. (const :startgroup)
  2076. (option (string :tag "Group description")))
  2077. (list :tag "End radio group"
  2078. (const :endgroup)
  2079. (option (string :tag "Group description")))
  2080. (const :tag "New line" (:newline)))))
  2081. (defcustom org-tag-persistent-alist nil
  2082. "List of tags that will always appear in all Org-mode files.
  2083. This is in addition to any in buffer settings or customizations
  2084. of `org-tag-alist'.
  2085. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2086. The value of this variable is an alist, the car of each entry must be a
  2087. keyword as a string, the cdr may be a character that is used to select
  2088. that tag through the fast-tag-selection interface.
  2089. See the manual for details.
  2090. To disable these tags on a per-file basis, insert anywhere in the file:
  2091. #+STARTUP: noptag"
  2092. :group 'org-tags
  2093. :type '(repeat
  2094. (choice
  2095. (cons (string :tag "Tag name")
  2096. (character :tag "Access char"))
  2097. (const :tag "Start radio group" (:startgroup))
  2098. (const :tag "End radio group" (:endgroup))
  2099. (const :tag "New line" (:newline)))))
  2100. (defvar org-file-tags nil
  2101. "List of tags that can be inherited by all entries in the file.
  2102. The tags will be inherited if the variable `org-use-tag-inheritance'
  2103. says they should be.
  2104. This variable is populated from #+FILETAGS lines.")
  2105. (defcustom org-use-fast-tag-selection 'auto
  2106. "Non-nil means, use fast tag selection scheme.
  2107. This is a special interface to select and deselect tags with single keys.
  2108. When nil, fast selection is never used.
  2109. When the symbol `auto', fast selection is used if and only if selection
  2110. characters for tags have been configured, either through the variable
  2111. `org-tag-alist' or through a #+TAGS line in the buffer.
  2112. When t, fast selection is always used and selection keys are assigned
  2113. automatically if necessary."
  2114. :group 'org-tags
  2115. :type '(choice
  2116. (const :tag "Always" t)
  2117. (const :tag "Never" nil)
  2118. (const :tag "When selection characters are configured" 'auto)))
  2119. (defcustom org-fast-tag-selection-single-key nil
  2120. "Non-nil means, fast tag selection exits after first change.
  2121. When nil, you have to press RET to exit it.
  2122. During fast tag selection, you can toggle this flag with `C-c'.
  2123. This variable can also have the value `expert'. In this case, the window
  2124. displaying the tags menu is not even shown, until you press C-c again."
  2125. :group 'org-tags
  2126. :type '(choice
  2127. (const :tag "No" nil)
  2128. (const :tag "Yes" t)
  2129. (const :tag "Expert" expert)))
  2130. (defvar org-fast-tag-selection-include-todo nil
  2131. "Non-nil means, fast tags selection interface will also offer TODO states.
  2132. This is an undocumented feature, you should not rely on it.")
  2133. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2134. "The column to which tags should be indented in a headline.
  2135. If this number is positive, it specifies the column. If it is negative,
  2136. it means that the tags should be flushright to that column. For example,
  2137. -80 works well for a normal 80 character screen."
  2138. :group 'org-tags
  2139. :type 'integer)
  2140. (defcustom org-auto-align-tags t
  2141. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2142. These operations change the length of a headline and therefore shift
  2143. the tags around. With this options turned on, after each such operation
  2144. the tags are again aligned to `org-tags-column'."
  2145. :group 'org-tags
  2146. :type 'boolean)
  2147. (defcustom org-use-tag-inheritance t
  2148. "Non-nil means, tags in levels apply also for sublevels.
  2149. When nil, only the tags directly given in a specific line apply there.
  2150. This may also be a list of tags that should be inherited, or a regexp that
  2151. matches tags that should be inherited. Additional control is possible
  2152. with the variable `org-tags-exclude-from-inheritance' which gives an
  2153. explicit list of tags to be excluded from inheritance., even if the value of
  2154. `org-use-tag-inheritance' would select it for inheritance.
  2155. If this option is t, a match early-on in a tree can lead to a large
  2156. number of matches in the subtree when constructing the agenda or creating
  2157. a sparse tree. If you only want to see the first match in a tree during
  2158. a search, check out the variable `org-tags-match-list-sublevels'."
  2159. :group 'org-tags
  2160. :type '(choice
  2161. (const :tag "Not" nil)
  2162. (const :tag "Always" t)
  2163. (repeat :tag "Specific tags" (string :tag "Tag"))
  2164. (regexp :tag "Tags matched by regexp")))
  2165. (defcustom org-tags-exclude-from-inheritance nil
  2166. "List of tags that should never be inherited.
  2167. This is a way to exclude a few tags from inheritance. For way to do
  2168. the opposite, to actively allow inheritance for selected tags,
  2169. see the variable `org-use-tag-inheritance'."
  2170. :group 'org-tags
  2171. :type '(repeat (string :tag "Tag")))
  2172. (defun org-tag-inherit-p (tag)
  2173. "Check if TAG is one that should be inherited."
  2174. (cond
  2175. ((member tag org-tags-exclude-from-inheritance) nil)
  2176. ((eq org-use-tag-inheritance t) t)
  2177. ((not org-use-tag-inheritance) nil)
  2178. ((stringp org-use-tag-inheritance)
  2179. (string-match org-use-tag-inheritance tag))
  2180. ((listp org-use-tag-inheritance)
  2181. (member tag org-use-tag-inheritance))
  2182. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2183. (defcustom org-tags-match-list-sublevels t
  2184. "Non-nil means list also sublevels of headlines matching a search.
  2185. This variable applies to tags/property searches, and also to stuck
  2186. projects because this search is based on a tags match as well.
  2187. When set to the symbol `indented', sublevels are indented with
  2188. leading dots.
  2189. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2190. the sublevels of a headline matching a tag search often also match
  2191. the same search. Listing all of them can create very long lists.
  2192. Setting this variable to nil causes subtrees of a match to be skipped.
  2193. This variable is semi-obsolete and probably should always be true. It
  2194. is better to limit inheritance to certain tags using the variables
  2195. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2196. :group 'org-tags
  2197. :type '(choice
  2198. (const :tag "No, don't list them" nil)
  2199. (const :tag "Yes, do list them" t)
  2200. (const :tag "List them, indented with leading dots" indented)))
  2201. (defcustom org-tags-sort-function nil
  2202. "When set, tags are sorted using this function as a comparator"
  2203. :group 'org-tags
  2204. :type '(choice
  2205. (const :tag "No sorting" nil)
  2206. (const :tag "Alphabetical" string<)
  2207. (const :tag "Reverse alphabetical" string>)
  2208. (function :tag "Custom function" nil)))
  2209. (defvar org-tags-history nil
  2210. "History of minibuffer reads for tags.")
  2211. (defvar org-last-tags-completion-table nil
  2212. "The last used completion table for tags.")
  2213. (defvar org-after-tags-change-hook nil
  2214. "Hook that is run after the tags in a line have changed.")
  2215. (defgroup org-properties nil
  2216. "Options concerning properties in Org-mode."
  2217. :tag "Org Properties"
  2218. :group 'org)
  2219. (defcustom org-property-format "%-10s %s"
  2220. "How property key/value pairs should be formatted by `indent-line'.
  2221. When `indent-line' hits a property definition, it will format the line
  2222. according to this format, mainly to make sure that the values are
  2223. lined-up with respect to each other."
  2224. :group 'org-properties
  2225. :type 'string)
  2226. (defcustom org-use-property-inheritance nil
  2227. "Non-nil means, properties apply also for sublevels.
  2228. This setting is chiefly used during property searches. Turning it on can
  2229. cause significant overhead when doing a search, which is why it is not
  2230. on by default.
  2231. When nil, only the properties directly given in the current entry count.
  2232. When t, every property is inherited. The value may also be a list of
  2233. properties that should have inheritance, or a regular expression matching
  2234. properties that should be inherited.
  2235. However, note that some special properties use inheritance under special
  2236. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2237. and the properties ending in \"_ALL\" when they are used as descriptor
  2238. for valid values of a property.
  2239. Note for programmers:
  2240. When querying an entry with `org-entry-get', you can control if inheritance
  2241. should be used. By default, `org-entry-get' looks only at the local
  2242. properties. You can request inheritance by setting the inherit argument
  2243. to t (to force inheritance) or to `selective' (to respect the setting
  2244. in this variable)."
  2245. :group 'org-properties
  2246. :type '(choice
  2247. (const :tag "Not" nil)
  2248. (const :tag "Always" t)
  2249. (repeat :tag "Specific properties" (string :tag "Property"))
  2250. (regexp :tag "Properties matched by regexp")))
  2251. (defun org-property-inherit-p (property)
  2252. "Check if PROPERTY is one that should be inherited."
  2253. (cond
  2254. ((eq org-use-property-inheritance t) t)
  2255. ((not org-use-property-inheritance) nil)
  2256. ((stringp org-use-property-inheritance)
  2257. (string-match org-use-property-inheritance property))
  2258. ((listp org-use-property-inheritance)
  2259. (member property org-use-property-inheritance))
  2260. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2261. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2262. "The default column format, if no other format has been defined.
  2263. This variable can be set on the per-file basis by inserting a line
  2264. #+COLUMNS: %25ITEM ....."
  2265. :group 'org-properties
  2266. :type 'string)
  2267. (defcustom org-columns-ellipses ".."
  2268. "The ellipses to be used when a field in column view is truncated.
  2269. When this is the empty string, as many characters as possible are shown,
  2270. but then there will be no visual indication that the field has been truncated.
  2271. When this is a string of length N, the last N characters of a truncated
  2272. field are replaced by this string. If the column is narrower than the
  2273. ellipses string, only part of the ellipses string will be shown."
  2274. :group 'org-properties
  2275. :type 'string)
  2276. (defcustom org-columns-modify-value-for-display-function nil
  2277. "Function that modifies values for display in column view.
  2278. For example, it can be used to cut out a certain part from a time stamp.
  2279. The function must take 2 arguments:
  2280. column-title The title of the column (*not* the property name)
  2281. value The value that should be modified.
  2282. The function should return the value that should be displayed,
  2283. or nil if the normal value should be used."
  2284. :group 'org-properties
  2285. :type 'function)
  2286. (defcustom org-effort-property "Effort"
  2287. "The property that is being used to keep track of effort estimates.
  2288. Effort estimates given in this property need to have the format H:MM."
  2289. :group 'org-properties
  2290. :group 'org-progress
  2291. :type '(string :tag "Property"))
  2292. (defconst org-global-properties-fixed
  2293. '(("VISIBILITY_ALL" . "folded children content all")
  2294. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2295. "List of property/value pairs that can be inherited by any entry.
  2296. These are fixed values, for the preset properties. The user variable
  2297. that can be used to add to this list is `org-global-properties'.
  2298. The entries in this list are cons cells where the car is a property
  2299. name and cdr is a string with the value. If the value represents
  2300. multiple items like an \"_ALL\" property, separate the items by
  2301. spaces.")
  2302. (defcustom org-global-properties nil
  2303. "List of property/value pairs that can be inherited by any entry.
  2304. This list will be combined with the constant `org-global-properties-fixed'.
  2305. The entries in this list are cons cells where the car is a property
  2306. name and cdr is a string with the value.
  2307. You can set buffer-local values for the same purpose in the variable
  2308. `org-file-properties' this by adding lines like
  2309. #+PROPERTY: NAME VALUE"
  2310. :group 'org-properties
  2311. :type '(repeat
  2312. (cons (string :tag "Property")
  2313. (string :tag "Value"))))
  2314. (defvar org-file-properties nil
  2315. "List of property/value pairs that can be inherited by any entry.
  2316. Valid for the current buffer.
  2317. This variable is populated from #+PROPERTY lines.")
  2318. (make-variable-buffer-local 'org-file-properties)
  2319. (defgroup org-agenda nil
  2320. "Options concerning agenda views in Org-mode."
  2321. :tag "Org Agenda"
  2322. :group 'org)
  2323. (defvar org-category nil
  2324. "Variable used by org files to set a category for agenda display.
  2325. Such files should use a file variable to set it, for example
  2326. # -*- mode: org; org-category: \"ELisp\"
  2327. or contain a special line
  2328. #+CATEGORY: ELisp
  2329. If the file does not specify a category, then file's base name
  2330. is used instead.")
  2331. (make-variable-buffer-local 'org-category)
  2332. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2333. (defcustom org-agenda-files nil
  2334. "The files to be used for agenda display.
  2335. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2336. \\[org-remove-file]. You can also use customize to edit the list.
  2337. If an entry is a directory, all files in that directory that are matched by
  2338. `org-agenda-file-regexp' will be part of the file list.
  2339. If the value of the variable is not a list but a single file name, then
  2340. the list of agenda files is actually stored and maintained in that file, one
  2341. agenda file per line."
  2342. :group 'org-agenda
  2343. :type '(choice
  2344. (repeat :tag "List of files and directories" file)
  2345. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2346. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2347. "Regular expression to match files for `org-agenda-files'.
  2348. If any element in the list in that variable contains a directory instead
  2349. of a normal file, all files in that directory that are matched by this
  2350. regular expression will be included."
  2351. :group 'org-agenda
  2352. :type 'regexp)
  2353. (defcustom org-agenda-text-search-extra-files nil
  2354. "List of extra files to be searched by text search commands.
  2355. These files will be search in addition to the agenda files by the
  2356. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2357. Note that these files will only be searched for text search commands,
  2358. not for the other agenda views like todo lists, tag searches or the weekly
  2359. agenda. This variable is intended to list notes and possibly archive files
  2360. that should also be searched by these two commands.
  2361. In fact, if the first element in the list is the symbol `agenda-archives',
  2362. than all archive files of all agenda files will be added to the search
  2363. scope."
  2364. :group 'org-agenda
  2365. :type '(set :greedy t
  2366. (const :tag "Agenda Archives" agenda-archives)
  2367. (repeat :inline t (file))))
  2368. (if (fboundp 'defvaralias)
  2369. (defvaralias 'org-agenda-multi-occur-extra-files
  2370. 'org-agenda-text-search-extra-files))
  2371. (defcustom org-agenda-skip-unavailable-files nil
  2372. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2373. A nil value means to remove them, after a query, from the list."
  2374. :group 'org-agenda
  2375. :type 'boolean)
  2376. (defcustom org-calendar-to-agenda-key [?c]
  2377. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2378. The command `org-calendar-goto-agenda' will be bound to this key. The
  2379. default is the character `c' because then `c' can be used to switch back and
  2380. forth between agenda and calendar."
  2381. :group 'org-agenda
  2382. :type 'sexp)
  2383. (defcustom org-calendar-agenda-action-key [?k]
  2384. "The key to be installed in `calendar-mode-map' for agenda-action.
  2385. The command `org-agenda-action' will be bound to this key. The
  2386. default is the character `k' because we use the same key in the agenda."
  2387. :group 'org-agenda
  2388. :type 'sexp)
  2389. (defcustom org-calendar-insert-diary-entry-key [?i]
  2390. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2391. This option is irrelevant until `org-agenda-diary-file' has been configured
  2392. to point to an Org-mode file. When that is the case, the command
  2393. `org-agenda-diary-entry' will be bound to the key given here, by default
  2394. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2395. if you want to continue doing this, you need to change this to a different
  2396. key."
  2397. :group 'org-agenda
  2398. :type 'sexp)
  2399. (defcustom org-agenda-diary-file 'diary-file
  2400. "File to which to add new entries with the `i' key in agenda and calendar.
  2401. When this is the symbol `diary-file', the functionality in the Emacs
  2402. calendar will be used to add entries to the `diary-file'. But when this
  2403. points to a file, `org-agenda-diary-entry' will be used instead."
  2404. :group 'org-agenda
  2405. :type '(choice
  2406. (const :tag "The standard Emacs diary file" diary-file)
  2407. (file :tag "Special Org file diary entries")))
  2408. (eval-after-load "calendar"
  2409. '(progn
  2410. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2411. 'org-calendar-goto-agenda)
  2412. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2413. 'org-agenda-action)
  2414. (add-hook 'calendar-mode-hook
  2415. (lambda ()
  2416. (unless (eq org-agenda-diary-file 'diary-file)
  2417. (define-key calendar-mode-map
  2418. org-calendar-insert-diary-entry-key
  2419. 'org-agenda-diary-entry))))))
  2420. (defgroup org-latex nil
  2421. "Options for embedding LaTeX code into Org-mode."
  2422. :tag "Org LaTeX"
  2423. :group 'org)
  2424. (defcustom org-format-latex-options
  2425. '(:foreground default :background default :scale 1.0
  2426. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2427. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2428. "Options for creating images from LaTeX fragments.
  2429. This is a property list with the following properties:
  2430. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2431. `default' means use the foreground of the default face.
  2432. :background the background color, or \"Transparent\".
  2433. `default' means use the background of the default face.
  2434. :scale a scaling factor for the size of the images.
  2435. :html-foreground, :html-background, :html-scale
  2436. the same numbers for HTML export.
  2437. :matchers a list indicating which matchers should be used to
  2438. find LaTeX fragments. Valid members of this list are:
  2439. \"begin\" find environments
  2440. \"$1\" find single characters surrounded by $.$
  2441. \"$\" find math expressions surrounded by $...$
  2442. \"$$\" find math expressions surrounded by $$....$$
  2443. \"\\(\" find math expressions surrounded by \\(...\\)
  2444. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2445. :group 'org-latex
  2446. :type 'plist)
  2447. (defcustom org-format-latex-header "\\documentclass{article}
  2448. \\usepackage{amssymb}
  2449. \\usepackage[usenames]{color}
  2450. \\usepackage{amsmath}
  2451. \\usepackage{latexsym}
  2452. \\usepackage[mathscr]{eucal}
  2453. \\pagestyle{empty} % do not remove
  2454. % The settings below are copied from fullpage.sty
  2455. \\setlength{\\textwidth}{\\paperwidth}
  2456. \\addtolength{\\textwidth}{-3cm}
  2457. \\setlength{\\oddsidemargin}{1.5cm}
  2458. \\addtolength{\\oddsidemargin}{-2.54cm}
  2459. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2460. \\setlength{\\textheight}{\\paperheight}
  2461. \\addtolength{\\textheight}{-\\headheight}
  2462. \\addtolength{\\textheight}{-\\headsep}
  2463. \\addtolength{\\textheight}{-\\footskip}
  2464. \\addtolength{\\textheight}{-3cm}
  2465. \\setlength{\\topmargin}{1.5cm}
  2466. \\addtolength{\\topmargin}{-2.54cm}"
  2467. "The document header used for processing LaTeX fragments.
  2468. It is imperative that this header make sure that no page number
  2469. appears on the page."
  2470. :group 'org-latex
  2471. :type 'string)
  2472. (defgroup org-font-lock nil
  2473. "Font-lock settings for highlighting in Org-mode."
  2474. :tag "Org Font Lock"
  2475. :group 'org)
  2476. (defcustom org-level-color-stars-only nil
  2477. "Non-nil means fontify only the stars in each headline.
  2478. When nil, the entire headline is fontified.
  2479. Changing it requires restart of `font-lock-mode' to become effective
  2480. also in regions already fontified."
  2481. :group 'org-font-lock
  2482. :type 'boolean)
  2483. (defcustom org-hide-leading-stars nil
  2484. "Non-nil means, hide the first N-1 stars in a headline.
  2485. This works by using the face `org-hide' for these stars. This
  2486. face is white for a light background, and black for a dark
  2487. background. You may have to customize the face `org-hide' to
  2488. make this work.
  2489. Changing it requires restart of `font-lock-mode' to become effective
  2490. also in regions already fontified.
  2491. You may also set this on a per-file basis by adding one of the following
  2492. lines to the buffer:
  2493. #+STARTUP: hidestars
  2494. #+STARTUP: showstars"
  2495. :group 'org-font-lock
  2496. :type 'boolean)
  2497. (defcustom org-fontify-done-headline nil
  2498. "Non-nil means, change the face of a headline if it is marked DONE.
  2499. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2500. When this is non-nil, the headline after the keyword is set to the
  2501. `org-headline-done' as an additional indication."
  2502. :group 'org-font-lock
  2503. :type 'boolean)
  2504. (defcustom org-fontify-emphasized-text t
  2505. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2506. Changing this variable requires a restart of Emacs to take effect."
  2507. :group 'org-font-lock
  2508. :type 'boolean)
  2509. (defcustom org-fontify-whole-heading-line nil
  2510. "Non-nil means fontify the whole line for headings.
  2511. This is useful when setting a background color for the
  2512. org-level-* faces."
  2513. :group 'org-font-lock
  2514. :type 'boolean)
  2515. (defcustom org-highlight-latex-fragments-and-specials nil
  2516. "Non-nil means, fontify what is treated specially by the exporters."
  2517. :group 'org-font-lock
  2518. :type 'boolean)
  2519. (defcustom org-hide-emphasis-markers nil
  2520. "Non-nil mean font-lock should hide the emphasis marker characters."
  2521. :group 'org-font-lock
  2522. :type 'boolean)
  2523. (defvar org-emph-re nil
  2524. "Regular expression for matching emphasis.")
  2525. (defvar org-verbatim-re nil
  2526. "Regular expression for matching verbatim text.")
  2527. (defvar org-emphasis-regexp-components) ; defined just below
  2528. (defvar org-emphasis-alist) ; defined just below
  2529. (defun org-set-emph-re (var val)
  2530. "Set variable and compute the emphasis regular expression."
  2531. (set var val)
  2532. (when (and (boundp 'org-emphasis-alist)
  2533. (boundp 'org-emphasis-regexp-components)
  2534. org-emphasis-alist org-emphasis-regexp-components)
  2535. (let* ((e org-emphasis-regexp-components)
  2536. (pre (car e))
  2537. (post (nth 1 e))
  2538. (border (nth 2 e))
  2539. (body (nth 3 e))
  2540. (nl (nth 4 e))
  2541. (body1 (concat body "*?"))
  2542. (markers (mapconcat 'car org-emphasis-alist ""))
  2543. (vmarkers (mapconcat
  2544. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2545. org-emphasis-alist "")))
  2546. ;; make sure special characters appear at the right position in the class
  2547. (if (string-match "\\^" markers)
  2548. (setq markers (concat (replace-match "" t t markers) "^")))
  2549. (if (string-match "-" markers)
  2550. (setq markers (concat (replace-match "" t t markers) "-")))
  2551. (if (string-match "\\^" vmarkers)
  2552. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2553. (if (string-match "-" vmarkers)
  2554. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2555. (if (> nl 0)
  2556. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2557. (int-to-string nl) "\\}")))
  2558. ;; Make the regexp
  2559. (setq org-emph-re
  2560. (concat "\\([" pre "]\\|^\\)"
  2561. "\\("
  2562. "\\([" markers "]\\)"
  2563. "\\("
  2564. "[^" border "]\\|"
  2565. "[^" border "]"
  2566. body1
  2567. "[^" border "]"
  2568. "\\)"
  2569. "\\3\\)"
  2570. "\\([" post "]\\|$\\)"))
  2571. (setq org-verbatim-re
  2572. (concat "\\([" pre "]\\|^\\)"
  2573. "\\("
  2574. "\\([" vmarkers "]\\)"
  2575. "\\("
  2576. "[^" border "]\\|"
  2577. "[^" border "]"
  2578. body1
  2579. "[^" border "]"
  2580. "\\)"
  2581. "\\3\\)"
  2582. "\\([" post "]\\|$\\)")))))
  2583. (defcustom org-emphasis-regexp-components
  2584. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2585. "Components used to build the regular expression for emphasis.
  2586. This is a list with 6 entries. Terminology: In an emphasis string
  2587. like \" *strong word* \", we call the initial space PREMATCH, the final
  2588. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2589. and \"trong wor\" is the body. The different components in this variable
  2590. specify what is allowed/forbidden in each part:
  2591. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2592. post Chars allowed as postmatch. End of line will be allowed too.
  2593. border The chars *forbidden* as border characters.
  2594. body-regexp A regexp like \".\" to match a body character. Don't use
  2595. non-shy groups here, and don't allow newline here.
  2596. newline The maximum number of newlines allowed in an emphasis exp.
  2597. Use customize to modify this, or restart Emacs after changing it."
  2598. :group 'org-font-lock
  2599. :set 'org-set-emph-re
  2600. :type '(list
  2601. (sexp :tag "Allowed chars in pre ")
  2602. (sexp :tag "Allowed chars in post ")
  2603. (sexp :tag "Forbidden chars in border ")
  2604. (sexp :tag "Regexp for body ")
  2605. (integer :tag "number of newlines allowed")
  2606. (option (boolean :tag "Please ignore this button"))))
  2607. (defcustom org-emphasis-alist
  2608. `(("*" bold "<b>" "</b>")
  2609. ("/" italic "<i>" "</i>")
  2610. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2611. ("=" org-code "<code>" "</code>" verbatim)
  2612. ("~" org-verbatim "<code>" "</code>" verbatim)
  2613. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2614. "<del>" "</del>")
  2615. )
  2616. "Special syntax for emphasized text.
  2617. Text starting and ending with a special character will be emphasized, for
  2618. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2619. characters, the face to be used by font-lock for highlighting in Org-mode
  2620. Emacs buffers, and the HTML tags to be used for this.
  2621. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2622. Use customize to modify this, or restart Emacs after changing it."
  2623. :group 'org-font-lock
  2624. :set 'org-set-emph-re
  2625. :type '(repeat
  2626. (list
  2627. (string :tag "Marker character")
  2628. (choice
  2629. (face :tag "Font-lock-face")
  2630. (plist :tag "Face property list"))
  2631. (string :tag "HTML start tag")
  2632. (string :tag "HTML end tag")
  2633. (option (const verbatim)))))
  2634. (defvar org-protecting-blocks
  2635. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2636. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2637. This is needed for font-lock setup.")
  2638. ;;; Miscellaneous options
  2639. (defgroup org-completion nil
  2640. "Completion in Org-mode."
  2641. :tag "Org Completion"
  2642. :group 'org)
  2643. (defcustom org-completion-use-ido nil
  2644. "Non-nil means, use ido completion wherever possible.
  2645. Note that `ido-mode' must be active for this variable to be relevant.
  2646. If you decide to turn this variable on, you might well want to turn off
  2647. `org-outline-path-complete-in-steps'.
  2648. See also `org-completion-use-iswitchb'."
  2649. :group 'org-completion
  2650. :type 'boolean)
  2651. (defcustom org-completion-use-iswitchb nil
  2652. "Non-nil means, use iswitchb completion wherever possible.
  2653. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2654. If you decide to turn this variable on, you might well want to turn off
  2655. `org-outline-path-complete-in-steps'.
  2656. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2657. :group 'org-completion
  2658. :type 'boolean)
  2659. (defcustom org-completion-fallback-command 'hippie-expand
  2660. "The expansion command called by \\[org-complete] in normal context.
  2661. Normal means, no org-mode-specific context."
  2662. :group 'org-completion
  2663. :type 'function)
  2664. ;;; Functions and variables from their packages
  2665. ;; Declared here to avoid compiler warnings
  2666. ;; XEmacs only
  2667. (defvar outline-mode-menu-heading)
  2668. (defvar outline-mode-menu-show)
  2669. (defvar outline-mode-menu-hide)
  2670. (defvar zmacs-regions) ; XEmacs regions
  2671. ;; Emacs only
  2672. (defvar mark-active)
  2673. ;; Various packages
  2674. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2675. (declare-function calendar-forward-day "cal-move" (arg))
  2676. (declare-function calendar-goto-date "cal-move" (date))
  2677. (declare-function calendar-goto-today "cal-move" ())
  2678. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2679. (defvar calc-embedded-close-formula)
  2680. (defvar calc-embedded-open-formula)
  2681. (declare-function cdlatex-tab "ext:cdlatex" ())
  2682. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2683. (defvar font-lock-unfontify-region-function)
  2684. (declare-function iswitchb-read-buffer "iswitchb"
  2685. (prompt &optional default require-match start matches-set))
  2686. (defvar iswitchb-temp-buflist)
  2687. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2688. (defvar org-agenda-tags-todo-honor-ignore-options)
  2689. (declare-function org-agenda-skip "org-agenda" ())
  2690. (declare-function org-format-agenda-item "org-agenda"
  2691. (extra txt &optional category tags dotime noprefix remove-re))
  2692. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2693. (declare-function org-agenda-change-all-lines "org-agenda"
  2694. (newhead hdmarker &optional fixface just-this))
  2695. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2696. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2697. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2698. (beg end))
  2699. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2700. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2701. "org-agenda" (&optional end))
  2702. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2703. (declare-function org-indent-mode "org-indent" (&optional arg))
  2704. (declare-function parse-time-string "parse-time" (string))
  2705. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2706. (defvar remember-data-file)
  2707. (defvar texmathp-why)
  2708. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2709. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2710. (defvar w3m-current-url)
  2711. (defvar w3m-current-title)
  2712. (defvar org-latex-regexps)
  2713. ;;; Autoload and prepare some org modules
  2714. ;; Some table stuff that needs to be defined here, because it is used
  2715. ;; by the functions setting up org-mode or checking for table context.
  2716. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2717. "Detects an org-type or table-type table.")
  2718. (defconst org-table-line-regexp "^[ \t]*|"
  2719. "Detects an org-type table line.")
  2720. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2721. "Detects an org-type table line.")
  2722. (defconst org-table-hline-regexp "^[ \t]*|-"
  2723. "Detects an org-type table hline.")
  2724. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2725. "Detects a table-type table hline.")
  2726. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2727. "Searching from within a table (any type) this finds the first line
  2728. outside the table.")
  2729. ;; Autoload the functions in org-table.el that are needed by functions here.
  2730. (eval-and-compile
  2731. (org-autoload "org-table"
  2732. '(org-table-align org-table-begin org-table-blank-field
  2733. org-table-convert org-table-convert-region org-table-copy-down
  2734. org-table-copy-region org-table-create
  2735. org-table-create-or-convert-from-region
  2736. org-table-create-with-table.el org-table-current-dline
  2737. org-table-cut-region org-table-delete-column org-table-edit-field
  2738. org-table-edit-formulas org-table-end org-table-eval-formula
  2739. org-table-export org-table-field-info
  2740. org-table-get-stored-formulas org-table-goto-column
  2741. org-table-hline-and-move org-table-import org-table-insert-column
  2742. org-table-insert-hline org-table-insert-row org-table-iterate
  2743. org-table-justify-field-maybe org-table-kill-row
  2744. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2745. org-table-move-column org-table-move-column-left
  2746. org-table-move-column-right org-table-move-row
  2747. org-table-move-row-down org-table-move-row-up
  2748. org-table-next-field org-table-next-row org-table-paste-rectangle
  2749. org-table-previous-field org-table-recalculate
  2750. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2751. org-table-toggle-coordinate-overlays
  2752. org-table-toggle-formula-debugger org-table-wrap-region
  2753. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2754. (defun org-at-table-p (&optional table-type)
  2755. "Return t if the cursor is inside an org-type table.
  2756. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2757. (if org-enable-table-editor
  2758. (save-excursion
  2759. (beginning-of-line 1)
  2760. (looking-at (if table-type org-table-any-line-regexp
  2761. org-table-line-regexp)))
  2762. nil))
  2763. (defsubst org-table-p () (org-at-table-p))
  2764. (defun org-at-table.el-p ()
  2765. "Return t if and only if we are at a table.el table."
  2766. (and (org-at-table-p 'any)
  2767. (save-excursion
  2768. (goto-char (org-table-begin 'any))
  2769. (looking-at org-table1-hline-regexp))))
  2770. (defun org-table-recognize-table.el ()
  2771. "If there is a table.el table nearby, recognize it and move into it."
  2772. (if org-table-tab-recognizes-table.el
  2773. (if (org-at-table.el-p)
  2774. (progn
  2775. (beginning-of-line 1)
  2776. (if (looking-at org-table-dataline-regexp)
  2777. nil
  2778. (if (looking-at org-table1-hline-regexp)
  2779. (progn
  2780. (beginning-of-line 2)
  2781. (if (looking-at org-table-any-border-regexp)
  2782. (beginning-of-line -1)))))
  2783. (if (re-search-forward "|" (org-table-end t) t)
  2784. (progn
  2785. (require 'table)
  2786. (if (table--at-cell-p (point))
  2787. t
  2788. (message "recognizing table.el table...")
  2789. (table-recognize-table)
  2790. (message "recognizing table.el table...done")))
  2791. (error "This should not happen..."))
  2792. t)
  2793. nil)
  2794. nil))
  2795. (defun org-at-table-hline-p ()
  2796. "Return t if the cursor is inside a hline in a table."
  2797. (if org-enable-table-editor
  2798. (save-excursion
  2799. (beginning-of-line 1)
  2800. (looking-at org-table-hline-regexp))
  2801. nil))
  2802. (defvar org-table-clean-did-remove-column nil)
  2803. (defun org-table-map-tables (function)
  2804. "Apply FUNCTION to the start of all tables in the buffer."
  2805. (save-excursion
  2806. (save-restriction
  2807. (widen)
  2808. (goto-char (point-min))
  2809. (while (re-search-forward org-table-any-line-regexp nil t)
  2810. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2811. (beginning-of-line 1)
  2812. (when (looking-at org-table-line-regexp)
  2813. (save-excursion (funcall function))
  2814. (or (looking-at org-table-line-regexp)
  2815. (forward-char 1)))
  2816. (re-search-forward org-table-any-border-regexp nil 1))))
  2817. (message "Mapping tables: done"))
  2818. ;; Declare and autoload functions from org-exp.el & Co
  2819. (declare-function org-default-export-plist "org-exp")
  2820. (declare-function org-infile-export-plist "org-exp")
  2821. (declare-function org-get-current-options "org-exp")
  2822. (eval-and-compile
  2823. (org-autoload "org-exp"
  2824. '(org-export org-export-visible
  2825. org-insert-export-options-template
  2826. org-table-clean-before-export))
  2827. (org-autoload "org-ascii"
  2828. '(org-export-as-ascii org-export-ascii-preprocess
  2829. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2830. org-export-region-as-ascii))
  2831. (org-autoload "org-html"
  2832. '(org-export-as-html-and-open
  2833. org-export-as-html-batch org-export-as-html-to-buffer
  2834. org-replace-region-by-html org-export-region-as-html
  2835. org-export-as-html))
  2836. (org-autoload "org-icalendar"
  2837. '(org-export-icalendar-this-file
  2838. org-export-icalendar-all-agenda-files
  2839. org-export-icalendar-combine-agenda-files))
  2840. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2841. ;; Declare and autoload functions from org-agenda.el
  2842. (eval-and-compile
  2843. (org-autoload "org-agenda"
  2844. '(org-agenda org-agenda-list org-search-view
  2845. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2846. org-diary org-agenda-to-appt
  2847. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2848. ;; Autoload org-remember
  2849. (eval-and-compile
  2850. (org-autoload "org-remember"
  2851. '(org-remember-insinuate org-remember-annotation
  2852. org-remember-apply-template org-remember org-remember-handler)))
  2853. ;; Autoload org-clock.el
  2854. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2855. (beg end))
  2856. (declare-function org-clock-update-mode-line "org-clock" ())
  2857. (declare-function org-resolve-clocks "org-clock"
  2858. (&optional also-non-dangling-p prompt last-valid))
  2859. (defvar org-clock-start-time)
  2860. (defvar org-clock-marker (make-marker)
  2861. "Marker recording the last clock-in.")
  2862. (defvar org-clock-hd-marker (make-marker)
  2863. "Marker recording the last clock-in, but the headline position.")
  2864. (defvar org-clock-heading ""
  2865. "The heading of the current clock entry.")
  2866. (defun org-clock-is-active ()
  2867. "Return non-nil if clock is currently running.
  2868. The return value is actually the clock marker."
  2869. (marker-buffer org-clock-marker))
  2870. (eval-and-compile
  2871. (org-autoload
  2872. "org-clock"
  2873. '(org-clock-in org-clock-out org-clock-cancel
  2874. org-clock-goto org-clock-sum org-clock-display
  2875. org-clock-remove-overlays org-clock-report
  2876. org-clocktable-shift org-dblock-write:clocktable
  2877. org-get-clocktable org-resolve-clocks)))
  2878. (defun org-clock-update-time-maybe ()
  2879. "If this is a CLOCK line, update it and return t.
  2880. Otherwise, return nil."
  2881. (interactive)
  2882. (save-excursion
  2883. (beginning-of-line 1)
  2884. (skip-chars-forward " \t")
  2885. (when (looking-at org-clock-string)
  2886. (let ((re (concat "[ \t]*" org-clock-string
  2887. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2888. "\\([ \t]*=>.*\\)?\\)?"))
  2889. ts te h m s neg)
  2890. (cond
  2891. ((not (looking-at re))
  2892. nil)
  2893. ((not (match-end 2))
  2894. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2895. (> org-clock-marker (point))
  2896. (<= org-clock-marker (point-at-eol)))
  2897. ;; The clock is running here
  2898. (setq org-clock-start-time
  2899. (apply 'encode-time
  2900. (org-parse-time-string (match-string 1))))
  2901. (org-clock-update-mode-line)))
  2902. (t
  2903. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2904. (end-of-line 1)
  2905. (setq ts (match-string 1)
  2906. te (match-string 3))
  2907. (setq s (- (org-float-time
  2908. (apply 'encode-time (org-parse-time-string te)))
  2909. (org-float-time
  2910. (apply 'encode-time (org-parse-time-string ts))))
  2911. neg (< s 0)
  2912. s (abs s)
  2913. h (floor (/ s 3600))
  2914. s (- s (* 3600 h))
  2915. m (floor (/ s 60))
  2916. s (- s (* 60 s)))
  2917. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2918. t))))))
  2919. (defun org-check-running-clock ()
  2920. "Check if the current buffer contains the running clock.
  2921. If yes, offer to stop it and to save the buffer with the changes."
  2922. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2923. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2924. (buffer-name))))
  2925. (org-clock-out)
  2926. (when (y-or-n-p "Save changed buffer?")
  2927. (save-buffer))))
  2928. (defun org-clocktable-try-shift (dir n)
  2929. "Check if this line starts a clock table, if yes, shift the time block."
  2930. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2931. (org-clocktable-shift dir n)))
  2932. ;; Autoload org-timer.el
  2933. (eval-and-compile
  2934. (org-autoload
  2935. "org-timer"
  2936. '(org-timer-start org-timer org-timer-item
  2937. org-timer-change-times-in-region
  2938. org-timer-set-timer
  2939. org-timer-reset-timers
  2940. org-timer-show-remaining-time)))
  2941. ;; Autoload org-feed.el
  2942. (eval-and-compile
  2943. (org-autoload
  2944. "org-feed"
  2945. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2946. ;; Autoload org-indent.el
  2947. ;; Define the variable already here, to make sure we have it.
  2948. (defvar org-indent-mode nil
  2949. "Non-nil if Org-Indent mode is enabled.
  2950. Use the command `org-indent-mode' to change this variable.")
  2951. (eval-and-compile
  2952. (org-autoload
  2953. "org-indent"
  2954. '(org-indent-mode)))
  2955. ;; Autoload org-mobile.el
  2956. (eval-and-compile
  2957. (org-autoload
  2958. "org-mobile"
  2959. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2960. ;; Autoload archiving code
  2961. ;; The stuff that is needed for cycling and tags has to be defined here.
  2962. (defgroup org-archive nil
  2963. "Options concerning archiving in Org-mode."
  2964. :tag "Org Archive"
  2965. :group 'org-structure)
  2966. (defcustom org-archive-location "%s_archive::"
  2967. "The location where subtrees should be archived.
  2968. The value of this variable is a string, consisting of two parts,
  2969. separated by a double-colon. The first part is a filename and
  2970. the second part is a headline.
  2971. When the filename is omitted, archiving happens in the same file.
  2972. %s in the filename will be replaced by the current file
  2973. name (without the directory part). Archiving to a different file
  2974. is useful to keep archived entries from contributing to the
  2975. Org-mode Agenda.
  2976. The archived entries will be filed as subtrees of the specified
  2977. headline. When the headline is omitted, the subtrees are simply
  2978. filed away at the end of the file, as top-level entries. Also in
  2979. the heading you can use %s to represent the file name, this can be
  2980. useful when using the same archive for a number of different files.
  2981. Here are a few examples:
  2982. \"%s_archive::\"
  2983. If the current file is Projects.org, archive in file
  2984. Projects.org_archive, as top-level trees. This is the default.
  2985. \"::* Archived Tasks\"
  2986. Archive in the current file, under the top-level headline
  2987. \"* Archived Tasks\".
  2988. \"~/org/archive.org::\"
  2989. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2990. \"~/org/archive.org::From %s\"
  2991. Archive in file ~/org/archive.org (absolute path), under headlines
  2992. \"From FILENAME\" where file name is the current file name.
  2993. \"basement::** Finished Tasks\"
  2994. Archive in file ./basement (relative path), as level 3 trees
  2995. below the level 2 heading \"** Finished Tasks\".
  2996. You may set this option on a per-file basis by adding to the buffer a
  2997. line like
  2998. #+ARCHIVE: basement::** Finished Tasks
  2999. You may also define it locally for a subtree by setting an ARCHIVE property
  3000. in the entry. If such a property is found in an entry, or anywhere up
  3001. the hierarchy, it will be used."
  3002. :group 'org-archive
  3003. :type 'string)
  3004. (defcustom org-archive-tag "ARCHIVE"
  3005. "The tag that marks a subtree as archived.
  3006. An archived subtree does not open during visibility cycling, and does
  3007. not contribute to the agenda listings.
  3008. After changing this, font-lock must be restarted in the relevant buffers to
  3009. get the proper fontification."
  3010. :group 'org-archive
  3011. :group 'org-keywords
  3012. :type 'string)
  3013. (defcustom org-agenda-skip-archived-trees t
  3014. "Non-nil means, the agenda will skip any items located in archived trees.
  3015. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3016. variable is no longer recommended, you should leave it at the value t.
  3017. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3018. :group 'org-archive
  3019. :group 'org-agenda-skip
  3020. :type 'boolean)
  3021. (defcustom org-columns-skip-archived-trees t
  3022. "Non-nil means, ignore archived trees when creating column view."
  3023. :group 'org-archive
  3024. :group 'org-properties
  3025. :type 'boolean)
  3026. (defcustom org-cycle-open-archived-trees nil
  3027. "Non-nil means, `org-cycle' will open archived trees.
  3028. An archived tree is a tree marked with the tag ARCHIVE.
  3029. When nil, archived trees will stay folded. You can still open them with
  3030. normal outline commands like `show-all', but not with the cycling commands."
  3031. :group 'org-archive
  3032. :group 'org-cycle
  3033. :type 'boolean)
  3034. (defcustom org-sparse-tree-open-archived-trees nil
  3035. "Non-nil means sparse tree construction shows matches in archived trees.
  3036. When nil, matches in these trees are highlighted, but the trees are kept in
  3037. collapsed state."
  3038. :group 'org-archive
  3039. :group 'org-sparse-trees
  3040. :type 'boolean)
  3041. (defun org-cycle-hide-archived-subtrees (state)
  3042. "Re-hide all archived subtrees after a visibility state change."
  3043. (when (and (not org-cycle-open-archived-trees)
  3044. (not (memq state '(overview folded))))
  3045. (save-excursion
  3046. (let* ((globalp (memq state '(contents all)))
  3047. (beg (if globalp (point-min) (point)))
  3048. (end (if globalp (point-max) (org-end-of-subtree t))))
  3049. (org-hide-archived-subtrees beg end)
  3050. (goto-char beg)
  3051. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3052. (message "%s" (substitute-command-keys
  3053. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3054. (defun org-force-cycle-archived ()
  3055. "Cycle subtree even if it is archived."
  3056. (interactive)
  3057. (setq this-command 'org-cycle)
  3058. (let ((org-cycle-open-archived-trees t))
  3059. (call-interactively 'org-cycle)))
  3060. (defun org-hide-archived-subtrees (beg end)
  3061. "Re-hide all archived subtrees after a visibility state change."
  3062. (save-excursion
  3063. (let* ((re (concat ":" org-archive-tag ":")))
  3064. (goto-char beg)
  3065. (while (re-search-forward re end t)
  3066. (and (org-on-heading-p) (org-flag-subtree t))
  3067. (org-end-of-subtree t)))))
  3068. (defun org-flag-subtree (flag)
  3069. (save-excursion
  3070. (org-back-to-heading t)
  3071. (outline-end-of-heading)
  3072. (outline-flag-region (point)
  3073. (progn (org-end-of-subtree t) (point))
  3074. flag)))
  3075. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3076. (eval-and-compile
  3077. (org-autoload "org-archive"
  3078. '(org-add-archive-files org-archive-subtree
  3079. org-archive-to-archive-sibling org-toggle-archive-tag)))
  3080. ;; Autoload Column View Code
  3081. (declare-function org-columns-number-to-string "org-colview")
  3082. (declare-function org-columns-get-format-and-top-level "org-colview")
  3083. (declare-function org-columns-compute "org-colview")
  3084. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3085. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3086. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3087. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3088. ;; Autoload ID code
  3089. (declare-function org-id-store-link "org-id")
  3090. (declare-function org-id-locations-load "org-id")
  3091. (declare-function org-id-locations-save "org-id")
  3092. (defvar org-id-track-globally)
  3093. (org-autoload "org-id"
  3094. '(org-id-get-create org-id-new org-id-copy org-id-get
  3095. org-id-get-with-outline-path-completion
  3096. org-id-get-with-outline-drilling
  3097. org-id-goto org-id-find org-id-store-link))
  3098. ;; Autoload Plotting Code
  3099. (org-autoload "org-plot"
  3100. '(org-plot/gnuplot))
  3101. ;;; Variables for pre-computed regular expressions, all buffer local
  3102. (defvar org-drawer-regexp nil
  3103. "Matches first line of a hidden block.")
  3104. (make-variable-buffer-local 'org-drawer-regexp)
  3105. (defvar org-todo-regexp nil
  3106. "Matches any of the TODO state keywords.")
  3107. (make-variable-buffer-local 'org-todo-regexp)
  3108. (defvar org-not-done-regexp nil
  3109. "Matches any of the TODO state keywords except the last one.")
  3110. (make-variable-buffer-local 'org-not-done-regexp)
  3111. (defvar org-not-done-heading-regexp nil
  3112. "Matches a TODO headline that is not done.")
  3113. (make-variable-buffer-local 'org-not-done-regexp)
  3114. (defvar org-todo-line-regexp nil
  3115. "Matches a headline and puts TODO state into group 2 if present.")
  3116. (make-variable-buffer-local 'org-todo-line-regexp)
  3117. (defvar org-complex-heading-regexp nil
  3118. "Matches a headline and puts everything into groups:
  3119. group 1: the stars
  3120. group 2: The todo keyword, maybe
  3121. group 3: Priority cookie
  3122. group 4: True headline
  3123. group 5: Tags")
  3124. (make-variable-buffer-local 'org-complex-heading-regexp)
  3125. (defvar org-complex-heading-regexp-format nil)
  3126. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3127. (defvar org-todo-line-tags-regexp nil
  3128. "Matches a headline and puts TODO state into group 2 if present.
  3129. Also put tags into group 4 if tags are present.")
  3130. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3131. (defvar org-nl-done-regexp nil
  3132. "Matches newline followed by a headline with the DONE keyword.")
  3133. (make-variable-buffer-local 'org-nl-done-regexp)
  3134. (defvar org-looking-at-done-regexp nil
  3135. "Matches the DONE keyword a point.")
  3136. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3137. (defvar org-ds-keyword-length 12
  3138. "Maximum length of the Deadline and SCHEDULED keywords.")
  3139. (make-variable-buffer-local 'org-ds-keyword-length)
  3140. (defvar org-deadline-regexp nil
  3141. "Matches the DEADLINE keyword.")
  3142. (make-variable-buffer-local 'org-deadline-regexp)
  3143. (defvar org-deadline-time-regexp nil
  3144. "Matches the DEADLINE keyword together with a time stamp.")
  3145. (make-variable-buffer-local 'org-deadline-time-regexp)
  3146. (defvar org-deadline-line-regexp nil
  3147. "Matches the DEADLINE keyword and the rest of the line.")
  3148. (make-variable-buffer-local 'org-deadline-line-regexp)
  3149. (defvar org-scheduled-regexp nil
  3150. "Matches the SCHEDULED keyword.")
  3151. (make-variable-buffer-local 'org-scheduled-regexp)
  3152. (defvar org-scheduled-time-regexp nil
  3153. "Matches the SCHEDULED keyword together with a time stamp.")
  3154. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3155. (defvar org-closed-time-regexp nil
  3156. "Matches the CLOSED keyword together with a time stamp.")
  3157. (make-variable-buffer-local 'org-closed-time-regexp)
  3158. (defvar org-keyword-time-regexp nil
  3159. "Matches any of the 4 keywords, together with the time stamp.")
  3160. (make-variable-buffer-local 'org-keyword-time-regexp)
  3161. (defvar org-keyword-time-not-clock-regexp nil
  3162. "Matches any of the 3 keywords, together with the time stamp.")
  3163. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3164. (defvar org-maybe-keyword-time-regexp nil
  3165. "Matches a timestamp, possibly preceeded by a keyword.")
  3166. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3167. (defvar org-planning-or-clock-line-re nil
  3168. "Matches a line with planning or clock info.")
  3169. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3170. (defconst org-plain-time-of-day-regexp
  3171. (concat
  3172. "\\(\\<[012]?[0-9]"
  3173. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3174. "\\(--?"
  3175. "\\(\\<[012]?[0-9]"
  3176. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3177. "\\)?")
  3178. "Regular expression to match a plain time or time range.
  3179. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3180. groups carry important information:
  3181. 0 the full match
  3182. 1 the first time, range or not
  3183. 8 the second time, if it is a range.")
  3184. (defconst org-plain-time-extension-regexp
  3185. (concat
  3186. "\\(\\<[012]?[0-9]"
  3187. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3188. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3189. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3190. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3191. groups carry important information:
  3192. 0 the full match
  3193. 7 hours of duration
  3194. 9 minutes of duration")
  3195. (defconst org-stamp-time-of-day-regexp
  3196. (concat
  3197. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3198. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3199. "\\(--?"
  3200. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3201. "Regular expression to match a timestamp time or time range.
  3202. After a match, the following groups carry important information:
  3203. 0 the full match
  3204. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3205. 2 the first time, range or not
  3206. 4 the second time, if it is a range.")
  3207. (defconst org-startup-options
  3208. '(("fold" org-startup-folded t)
  3209. ("overview" org-startup-folded t)
  3210. ("nofold" org-startup-folded nil)
  3211. ("showall" org-startup-folded nil)
  3212. ("showeverything" org-startup-folded showeverything)
  3213. ("content" org-startup-folded content)
  3214. ("indent" org-startup-indented t)
  3215. ("noindent" org-startup-indented nil)
  3216. ("hidestars" org-hide-leading-stars t)
  3217. ("showstars" org-hide-leading-stars nil)
  3218. ("odd" org-odd-levels-only t)
  3219. ("oddeven" org-odd-levels-only nil)
  3220. ("align" org-startup-align-all-tables t)
  3221. ("noalign" org-startup-align-all-tables nil)
  3222. ("customtime" org-display-custom-times t)
  3223. ("logdone" org-log-done time)
  3224. ("lognotedone" org-log-done note)
  3225. ("nologdone" org-log-done nil)
  3226. ("lognoteclock-out" org-log-note-clock-out t)
  3227. ("nolognoteclock-out" org-log-note-clock-out nil)
  3228. ("logrepeat" org-log-repeat state)
  3229. ("lognoterepeat" org-log-repeat note)
  3230. ("nologrepeat" org-log-repeat nil)
  3231. ("logreschedule" org-log-reschedule time)
  3232. ("lognotereschedule" org-log-reschedule note)
  3233. ("nologreschedule" org-log-reschedule nil)
  3234. ("logredeadline" org-log-redeadline time)
  3235. ("lognoteredeadline" org-log-redeadline note)
  3236. ("nologredeadline" org-log-redeadline nil)
  3237. ("fninline" org-footnote-define-inline t)
  3238. ("nofninline" org-footnote-define-inline nil)
  3239. ("fnlocal" org-footnote-section nil)
  3240. ("fnauto" org-footnote-auto-label t)
  3241. ("fnprompt" org-footnote-auto-label nil)
  3242. ("fnconfirm" org-footnote-auto-label confirm)
  3243. ("fnplain" org-footnote-auto-label plain)
  3244. ("fnadjust" org-footnote-auto-adjust t)
  3245. ("nofnadjust" org-footnote-auto-adjust nil)
  3246. ("constcgs" constants-unit-system cgs)
  3247. ("constSI" constants-unit-system SI)
  3248. ("noptag" org-tag-persistent-alist nil)
  3249. ("hideblocks" org-hide-block-startup t)
  3250. ("nohideblocks" org-hide-block-startup nil))
  3251. "Variable associated with STARTUP options for org-mode.
  3252. Each element is a list of three items: The startup options as written
  3253. in the #+STARTUP line, the corresponding variable, and the value to
  3254. set this variable to if the option is found. An optional forth element PUSH
  3255. means to push this value onto the list in the variable.")
  3256. (defun org-set-regexps-and-options ()
  3257. "Precompute regular expressions for current buffer."
  3258. (when (org-mode-p)
  3259. (org-set-local 'org-todo-kwd-alist nil)
  3260. (org-set-local 'org-todo-key-alist nil)
  3261. (org-set-local 'org-todo-key-trigger nil)
  3262. (org-set-local 'org-todo-keywords-1 nil)
  3263. (org-set-local 'org-done-keywords nil)
  3264. (org-set-local 'org-todo-heads nil)
  3265. (org-set-local 'org-todo-sets nil)
  3266. (org-set-local 'org-todo-log-states nil)
  3267. (org-set-local 'org-file-properties nil)
  3268. (org-set-local 'org-file-tags nil)
  3269. (let ((re (org-make-options-regexp
  3270. '("CATEGORY" "TODO" "COLUMNS"
  3271. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3272. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3273. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3274. (splitre "[ \t]+")
  3275. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3276. tail sep kws1 prio props ftags drawers
  3277. ext-setup-or-nil setup-contents (start 0))
  3278. (save-excursion
  3279. (save-restriction
  3280. (widen)
  3281. (goto-char (point-min))
  3282. (while (or (and ext-setup-or-nil
  3283. (string-match re ext-setup-or-nil start)
  3284. (setq start (match-end 0)))
  3285. (and (setq ext-setup-or-nil nil start 0)
  3286. (re-search-forward re nil t)))
  3287. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3288. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3289. (cond
  3290. ((equal key "CATEGORY")
  3291. (if (string-match "[ \t]+$" value)
  3292. (setq value (replace-match "" t t value)))
  3293. (setq cat value))
  3294. ((member key '("SEQ_TODO" "TODO"))
  3295. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3296. ((equal key "TYP_TODO")
  3297. (push (cons 'type (org-split-string value splitre)) kwds))
  3298. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3299. ;; general TODO-like setup
  3300. (push (cons (intern (downcase (match-string 1 key)))
  3301. (org-split-string value splitre)) kwds))
  3302. ((equal key "TAGS")
  3303. (setq tags (append tags (if tags '("\\n") nil)
  3304. (org-split-string value splitre))))
  3305. ((equal key "COLUMNS")
  3306. (org-set-local 'org-columns-default-format value))
  3307. ((equal key "LINK")
  3308. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3309. (push (cons (match-string 1 value)
  3310. (org-trim (match-string 2 value)))
  3311. links)))
  3312. ((equal key "PRIORITIES")
  3313. (setq prio (org-split-string value " +")))
  3314. ((equal key "PROPERTY")
  3315. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3316. (push (cons (match-string 1 value) (match-string 2 value))
  3317. props)))
  3318. ((equal key "FILETAGS")
  3319. (when (string-match "\\S-" value)
  3320. (setq ftags
  3321. (append
  3322. ftags
  3323. (apply 'append
  3324. (mapcar (lambda (x) (org-split-string x ":"))
  3325. (org-split-string value)))))))
  3326. ((equal key "DRAWERS")
  3327. (setq drawers (org-split-string value splitre)))
  3328. ((equal key "CONSTANTS")
  3329. (setq const (append const (org-split-string value splitre))))
  3330. ((equal key "STARTUP")
  3331. (let ((opts (org-split-string value splitre))
  3332. l var val)
  3333. (while (setq l (pop opts))
  3334. (when (setq l (assoc l org-startup-options))
  3335. (setq var (nth 1 l) val (nth 2 l))
  3336. (if (not (nth 3 l))
  3337. (set (make-local-variable var) val)
  3338. (if (not (listp (symbol-value var)))
  3339. (set (make-local-variable var) nil))
  3340. (set (make-local-variable var) (symbol-value var))
  3341. (add-to-list var val))))))
  3342. ((equal key "ARCHIVE")
  3343. (string-match " *$" value)
  3344. (setq arch (replace-match "" t t value))
  3345. (remove-text-properties 0 (length arch)
  3346. '(face t fontified t) arch))
  3347. ((equal key "SETUPFILE")
  3348. (setq setup-contents (org-file-contents
  3349. (expand-file-name
  3350. (org-remove-double-quotes value))
  3351. 'noerror))
  3352. (if (not ext-setup-or-nil)
  3353. (setq ext-setup-or-nil setup-contents start 0)
  3354. (setq ext-setup-or-nil
  3355. (concat (substring ext-setup-or-nil 0 start)
  3356. "\n" setup-contents "\n"
  3357. (substring ext-setup-or-nil start)))))
  3358. ))))
  3359. (when cat
  3360. (org-set-local 'org-category (intern cat))
  3361. (push (cons "CATEGORY" cat) props))
  3362. (when prio
  3363. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3364. (setq prio (mapcar 'string-to-char prio))
  3365. (org-set-local 'org-highest-priority (nth 0 prio))
  3366. (org-set-local 'org-lowest-priority (nth 1 prio))
  3367. (org-set-local 'org-default-priority (nth 2 prio)))
  3368. (and props (org-set-local 'org-file-properties (nreverse props)))
  3369. (and ftags (org-set-local 'org-file-tags
  3370. (mapcar 'org-add-prop-inherited ftags)))
  3371. (and drawers (org-set-local 'org-drawers drawers))
  3372. (and arch (org-set-local 'org-archive-location arch))
  3373. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3374. ;; Process the TODO keywords
  3375. (unless kwds
  3376. ;; Use the global values as if they had been given locally.
  3377. (setq kwds (default-value 'org-todo-keywords))
  3378. (if (stringp (car kwds))
  3379. (setq kwds (list (cons org-todo-interpretation
  3380. (default-value 'org-todo-keywords)))))
  3381. (setq kwds (reverse kwds)))
  3382. (setq kwds (nreverse kwds))
  3383. (let (inter kws kw)
  3384. (while (setq kws (pop kwds))
  3385. (let ((kws (or
  3386. (run-hook-with-args-until-success
  3387. 'org-todo-setup-filter-hook kws)
  3388. kws)))
  3389. (setq inter (pop kws) sep (member "|" kws)
  3390. kws0 (delete "|" (copy-sequence kws))
  3391. kwsa nil
  3392. kws1 (mapcar
  3393. (lambda (x)
  3394. ;; 1 2
  3395. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3396. (progn
  3397. (setq kw (match-string 1 x)
  3398. key (and (match-end 2) (match-string 2 x))
  3399. log (org-extract-log-state-settings x))
  3400. (push (cons kw (and key (string-to-char key))) kwsa)
  3401. (and log (push log org-todo-log-states))
  3402. kw)
  3403. (error "Invalid TODO keyword %s" x)))
  3404. kws0)
  3405. kwsa (if kwsa (append '((:startgroup))
  3406. (nreverse kwsa)
  3407. '((:endgroup))))
  3408. hw (car kws1)
  3409. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3410. tail (list inter hw (car dws) (org-last dws))))
  3411. (add-to-list 'org-todo-heads hw 'append)
  3412. (push kws1 org-todo-sets)
  3413. (setq org-done-keywords (append org-done-keywords dws nil))
  3414. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3415. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3416. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3417. (setq org-todo-sets (nreverse org-todo-sets)
  3418. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3419. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3420. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3421. ;; Process the constants
  3422. (when const
  3423. (let (e cst)
  3424. (while (setq e (pop const))
  3425. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3426. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3427. (setq org-table-formula-constants-local cst)))
  3428. ;; Process the tags.
  3429. (when tags
  3430. (let (e tgs)
  3431. (while (setq e (pop tags))
  3432. (cond
  3433. ((equal e "{") (push '(:startgroup) tgs))
  3434. ((equal e "}") (push '(:endgroup) tgs))
  3435. ((equal e "\\n") (push '(:newline) tgs))
  3436. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3437. (push (cons (match-string 1 e)
  3438. (string-to-char (match-string 2 e)))
  3439. tgs))
  3440. (t (push (list e) tgs))))
  3441. (org-set-local 'org-tag-alist nil)
  3442. (while (setq e (pop tgs))
  3443. (or (and (stringp (car e))
  3444. (assoc (car e) org-tag-alist))
  3445. (push e org-tag-alist)))))
  3446. ;; Compute the regular expressions and other local variables
  3447. (if (not org-done-keywords)
  3448. (setq org-done-keywords (and org-todo-keywords-1
  3449. (list (org-last org-todo-keywords-1)))))
  3450. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3451. (length org-scheduled-string)
  3452. (length org-clock-string)
  3453. (length org-closed-string)))
  3454. org-drawer-regexp
  3455. (concat "^[ \t]*:\\("
  3456. (mapconcat 'regexp-quote org-drawers "\\|")
  3457. "\\):[ \t]*$")
  3458. org-not-done-keywords
  3459. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3460. org-todo-regexp
  3461. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3462. "\\|") "\\)\\>")
  3463. org-not-done-regexp
  3464. (concat "\\<\\("
  3465. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3466. "\\)\\>")
  3467. org-not-done-heading-regexp
  3468. (concat "^\\(\\*+\\)[ \t]+\\("
  3469. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3470. "\\)\\>")
  3471. org-todo-line-regexp
  3472. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3473. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3474. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3475. org-complex-heading-regexp
  3476. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3477. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3478. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3479. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3480. org-complex-heading-regexp-format
  3481. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3482. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3483. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3484. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3485. org-nl-done-regexp
  3486. (concat "\n\\*+[ \t]+"
  3487. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3488. "\\)" "\\>")
  3489. org-todo-line-tags-regexp
  3490. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3491. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3492. (org-re
  3493. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3494. org-looking-at-done-regexp
  3495. (concat "^" "\\(?:"
  3496. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3497. "\\>")
  3498. org-deadline-regexp (concat "\\<" org-deadline-string)
  3499. org-deadline-time-regexp
  3500. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3501. org-deadline-line-regexp
  3502. (concat "\\<\\(" org-deadline-string "\\).*")
  3503. org-scheduled-regexp
  3504. (concat "\\<" org-scheduled-string)
  3505. org-scheduled-time-regexp
  3506. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3507. org-closed-time-regexp
  3508. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3509. org-keyword-time-regexp
  3510. (concat "\\<\\(" org-scheduled-string
  3511. "\\|" org-deadline-string
  3512. "\\|" org-closed-string
  3513. "\\|" org-clock-string "\\)"
  3514. " *[[<]\\([^]>]+\\)[]>]")
  3515. org-keyword-time-not-clock-regexp
  3516. (concat "\\<\\(" org-scheduled-string
  3517. "\\|" org-deadline-string
  3518. "\\|" org-closed-string
  3519. "\\)"
  3520. " *[[<]\\([^]>]+\\)[]>]")
  3521. org-maybe-keyword-time-regexp
  3522. (concat "\\(\\<\\(" org-scheduled-string
  3523. "\\|" org-deadline-string
  3524. "\\|" org-closed-string
  3525. "\\|" org-clock-string "\\)\\)?"
  3526. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3527. org-planning-or-clock-line-re
  3528. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3529. "\\|" org-deadline-string
  3530. "\\|" org-closed-string "\\|" org-clock-string
  3531. "\\)\\>\\)")
  3532. )
  3533. (org-compute-latex-and-specials-regexp)
  3534. (org-set-font-lock-defaults))))
  3535. (defun org-file-contents (file &optional noerror)
  3536. "Return the contents of FILE, as a string."
  3537. (if (or (not file)
  3538. (not (file-readable-p file)))
  3539. (if noerror
  3540. (progn
  3541. (message "Cannot read file %s" file)
  3542. (ding) (sit-for 2)
  3543. "")
  3544. (error "Cannot read file %s" file))
  3545. (with-temp-buffer
  3546. (insert-file-contents file)
  3547. (buffer-string))))
  3548. (defun org-extract-log-state-settings (x)
  3549. "Extract the log state setting from a TODO keyword string.
  3550. This will extract info from a string like \"WAIT(w@/!)\"."
  3551. (let (kw key log1 log2)
  3552. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3553. (setq kw (match-string 1 x)
  3554. key (and (match-end 2) (match-string 2 x))
  3555. log1 (and (match-end 3) (match-string 3 x))
  3556. log2 (and (match-end 4) (match-string 4 x)))
  3557. (and (or log1 log2)
  3558. (list kw
  3559. (and log1 (if (equal log1 "!") 'time 'note))
  3560. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3561. (defun org-remove-keyword-keys (list)
  3562. "Remove a pair of parenthesis at the end of each string in LIST."
  3563. (mapcar (lambda (x)
  3564. (if (string-match "(.*)$" x)
  3565. (substring x 0 (match-beginning 0))
  3566. x))
  3567. list))
  3568. ;; FIXME: this could be done much better, using second characters etc.
  3569. (defun org-assign-fast-keys (alist)
  3570. "Assign fast keys to a keyword-key alist.
  3571. Respect keys that are already there."
  3572. (let (new e k c c1 c2 (char ?a))
  3573. (while (setq e (pop alist))
  3574. (cond
  3575. ((equal e '(:startgroup)) (push e new))
  3576. ((equal e '(:endgroup)) (push e new))
  3577. ((equal e '(:newline)) (push e new))
  3578. (t
  3579. (setq k (car e) c2 nil)
  3580. (if (cdr e)
  3581. (setq c (cdr e))
  3582. ;; automatically assign a character.
  3583. (setq c1 (string-to-char
  3584. (downcase (substring
  3585. k (if (= (string-to-char k) ?@) 1 0)))))
  3586. (if (or (rassoc c1 new) (rassoc c1 alist))
  3587. (while (or (rassoc char new) (rassoc char alist))
  3588. (setq char (1+ char)))
  3589. (setq c2 c1))
  3590. (setq c (or c2 char)))
  3591. (push (cons k c) new))))
  3592. (nreverse new)))
  3593. ;;; Some variables used in various places
  3594. (defvar org-window-configuration nil
  3595. "Used in various places to store a window configuration.")
  3596. (defvar org-selected-window nil
  3597. "Used in various places to store a window configuration.")
  3598. (defvar org-finish-function nil
  3599. "Function to be called when `C-c C-c' is used.
  3600. This is for getting out of special buffers like remember.")
  3601. ;; FIXME: Occasionally check by commenting these, to make sure
  3602. ;; no other functions uses these, forgetting to let-bind them.
  3603. (defvar entry)
  3604. (defvar last-state)
  3605. (defvar date)
  3606. ;; Defined somewhere in this file, but used before definition.
  3607. (defvar org-html-entities)
  3608. (defvar org-struct-menu)
  3609. (defvar org-org-menu)
  3610. (defvar org-tbl-menu)
  3611. ;;;; Define the Org-mode
  3612. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3613. (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."))
  3614. ;; We use a before-change function to check if a table might need
  3615. ;; an update.
  3616. (defvar org-table-may-need-update t
  3617. "Indicates that a table might need an update.
  3618. This variable is set by `org-before-change-function'.
  3619. `org-table-align' sets it back to nil.")
  3620. (defun org-before-change-function (beg end)
  3621. "Every change indicates that a table might need an update."
  3622. (setq org-table-may-need-update t))
  3623. (defvar org-mode-map)
  3624. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3625. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3626. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3627. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3628. (defvar org-table-buffer-is-an nil)
  3629. (defconst org-outline-regexp "\\*+ ")
  3630. ;;;###autoload
  3631. (define-derived-mode org-mode outline-mode "Org"
  3632. "Outline-based notes management and organizer, alias
  3633. \"Carsten's outline-mode for keeping track of everything.\"
  3634. Org-mode develops organizational tasks around a NOTES file which
  3635. contains information about projects as plain text. Org-mode is
  3636. implemented on top of outline-mode, which is ideal to keep the content
  3637. of large files well structured. It supports ToDo items, deadlines and
  3638. time stamps, which magically appear in the diary listing of the Emacs
  3639. calendar. Tables are easily created with a built-in table editor.
  3640. Plain text URL-like links connect to websites, emails (VM), Usenet
  3641. messages (Gnus), BBDB entries, and any files related to the project.
  3642. For printing and sharing of notes, an Org-mode file (or a part of it)
  3643. can be exported as a structured ASCII or HTML file.
  3644. The following commands are available:
  3645. \\{org-mode-map}"
  3646. ;; Get rid of Outline menus, they are not needed
  3647. ;; Need to do this here because define-derived-mode sets up
  3648. ;; the keymap so late. Still, it is a waste to call this each time
  3649. ;; we switch another buffer into org-mode.
  3650. (if (featurep 'xemacs)
  3651. (when (boundp 'outline-mode-menu-heading)
  3652. ;; Assume this is Greg's port, it used easymenu
  3653. (easy-menu-remove outline-mode-menu-heading)
  3654. (easy-menu-remove outline-mode-menu-show)
  3655. (easy-menu-remove outline-mode-menu-hide))
  3656. (define-key org-mode-map [menu-bar headings] 'undefined)
  3657. (define-key org-mode-map [menu-bar hide] 'undefined)
  3658. (define-key org-mode-map [menu-bar show] 'undefined))
  3659. (org-load-modules-maybe)
  3660. (easy-menu-add org-org-menu)
  3661. (easy-menu-add org-tbl-menu)
  3662. (org-install-agenda-files-menu)
  3663. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3664. (org-add-to-invisibility-spec '(org-cwidth))
  3665. (org-add-to-invisibility-spec '(org-hide-block . t))
  3666. (when (featurep 'xemacs)
  3667. (org-set-local 'line-move-ignore-invisible t))
  3668. (org-set-local 'outline-regexp org-outline-regexp)
  3669. (org-set-local 'outline-level 'org-outline-level)
  3670. (when (and org-ellipsis
  3671. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3672. (fboundp 'make-glyph-code))
  3673. (unless org-display-table
  3674. (setq org-display-table (make-display-table)))
  3675. (set-display-table-slot
  3676. org-display-table 4
  3677. (vconcat (mapcar
  3678. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3679. org-ellipsis)))
  3680. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3681. (setq buffer-display-table org-display-table))
  3682. (org-set-regexps-and-options)
  3683. (when (and org-tag-faces (not org-tags-special-faces-re))
  3684. ;; tag faces set outside customize.... force initialization.
  3685. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3686. ;; Calc embedded
  3687. (org-set-local 'calc-embedded-open-mode "# ")
  3688. (modify-syntax-entry ?# "<")
  3689. (modify-syntax-entry ?@ "w")
  3690. (if org-startup-truncated (setq truncate-lines t))
  3691. (org-set-local 'font-lock-unfontify-region-function
  3692. 'org-unfontify-region)
  3693. ;; Activate before-change-function
  3694. (org-set-local 'org-table-may-need-update t)
  3695. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3696. 'local)
  3697. ;; Check for running clock before killing a buffer
  3698. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3699. ;; Paragraphs and auto-filling
  3700. (org-set-autofill-regexps)
  3701. (setq indent-line-function 'org-indent-line-function)
  3702. (org-update-radio-target-regexp)
  3703. ;; Make sure dependence stuff works reliably, even for users who set it
  3704. ;; too late :-(
  3705. (if org-enforce-todo-dependencies
  3706. (add-hook 'org-blocker-hook
  3707. 'org-block-todo-from-children-or-siblings-or-parent)
  3708. (remove-hook 'org-blocker-hook
  3709. 'org-block-todo-from-children-or-siblings-or-parent))
  3710. (if org-enforce-todo-checkbox-dependencies
  3711. (add-hook 'org-blocker-hook
  3712. 'org-block-todo-from-checkboxes)
  3713. (remove-hook 'org-blocker-hook
  3714. 'org-block-todo-from-checkboxes))
  3715. ;; Comment characters
  3716. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3717. (org-set-local 'comment-padding " ")
  3718. ;; Align options lines
  3719. (org-set-local
  3720. 'align-mode-rules-list
  3721. '((org-in-buffer-settings
  3722. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3723. (modes . '(org-mode)))))
  3724. ;; Imenu
  3725. (org-set-local 'imenu-create-index-function
  3726. 'org-imenu-get-tree)
  3727. ;; Make isearch reveal context
  3728. (if (or (featurep 'xemacs)
  3729. (not (boundp 'outline-isearch-open-invisible-function)))
  3730. ;; Emacs 21 and XEmacs make use of the hook
  3731. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3732. ;; Emacs 22 deals with this through a special variable
  3733. (org-set-local 'outline-isearch-open-invisible-function
  3734. (lambda (&rest ignore) (org-show-context 'isearch))))
  3735. ;; If empty file that did not turn on org-mode automatically, make it to.
  3736. (if (and org-insert-mode-line-in-empty-file
  3737. (interactive-p)
  3738. (= (point-min) (point-max)))
  3739. (insert "# -*- mode: org -*-\n\n"))
  3740. (unless org-inhibit-startup
  3741. (when org-startup-align-all-tables
  3742. (let ((bmp (buffer-modified-p)))
  3743. (org-table-map-tables 'org-table-align)
  3744. (set-buffer-modified-p bmp)))
  3745. (when org-startup-indented
  3746. (require 'org-indent)
  3747. (org-indent-mode 1))
  3748. (org-set-startup-visibility)))
  3749. (when (fboundp 'abbrev-table-put)
  3750. (abbrev-table-put org-mode-abbrev-table
  3751. :parents (list text-mode-abbrev-table)))
  3752. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3753. (defun org-current-time ()
  3754. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3755. (if (> (car org-time-stamp-rounding-minutes) 1)
  3756. (let ((r (car org-time-stamp-rounding-minutes))
  3757. (time (decode-time)))
  3758. (apply 'encode-time
  3759. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3760. (nthcdr 2 time))))
  3761. (current-time)))
  3762. ;;;; Font-Lock stuff, including the activators
  3763. (defvar org-mouse-map (make-sparse-keymap))
  3764. (org-defkey org-mouse-map
  3765. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3766. (org-defkey org-mouse-map
  3767. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3768. (when org-mouse-1-follows-link
  3769. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3770. (when org-tab-follows-link
  3771. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3772. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3773. (require 'font-lock)
  3774. (defconst org-non-link-chars "]\t\n\r<>")
  3775. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3776. "shell" "elisp"))
  3777. (defvar org-link-types-re nil
  3778. "Matches a link that has a url-like prefix like \"http:\"")
  3779. (defvar org-link-re-with-space nil
  3780. "Matches a link with spaces, optional angular brackets around it.")
  3781. (defvar org-link-re-with-space2 nil
  3782. "Matches a link with spaces, optional angular brackets around it.")
  3783. (defvar org-link-re-with-space3 nil
  3784. "Matches a link with spaces, only for internal part in bracket links.")
  3785. (defvar org-angle-link-re nil
  3786. "Matches link with angular brackets, spaces are allowed.")
  3787. (defvar org-plain-link-re nil
  3788. "Matches plain link, without spaces.")
  3789. (defvar org-bracket-link-regexp nil
  3790. "Matches a link in double brackets.")
  3791. (defvar org-bracket-link-analytic-regexp nil
  3792. "Regular expression used to analyze links.
  3793. Here is what the match groups contain after a match:
  3794. 1: http:
  3795. 2: http
  3796. 3: path
  3797. 4: [desc]
  3798. 5: desc")
  3799. (defvar org-bracket-link-analytic-regexp++ nil
  3800. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3801. (defvar org-any-link-re nil
  3802. "Regular expression matching any link.")
  3803. (defun org-make-link-regexps ()
  3804. "Update the link regular expressions.
  3805. This should be called after the variable `org-link-types' has changed."
  3806. (setq org-link-types-re
  3807. (concat
  3808. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3809. org-link-re-with-space
  3810. (concat
  3811. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3812. "\\([^" org-non-link-chars " ]"
  3813. "[^" org-non-link-chars "]*"
  3814. "[^" org-non-link-chars " ]\\)>?")
  3815. org-link-re-with-space2
  3816. (concat
  3817. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3818. "\\([^" org-non-link-chars " ]"
  3819. "[^\t\n\r]*"
  3820. "[^" org-non-link-chars " ]\\)>?")
  3821. org-link-re-with-space3
  3822. (concat
  3823. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3824. "\\([^" org-non-link-chars " ]"
  3825. "[^\t\n\r]*\\)")
  3826. org-angle-link-re
  3827. (concat
  3828. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3829. "\\([^" org-non-link-chars " ]"
  3830. "[^" org-non-link-chars "]*"
  3831. "\\)>")
  3832. org-plain-link-re
  3833. (concat
  3834. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3835. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3836. org-bracket-link-regexp
  3837. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3838. org-bracket-link-analytic-regexp
  3839. (concat
  3840. "\\[\\["
  3841. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3842. "\\([^]]+\\)"
  3843. "\\]"
  3844. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3845. "\\]")
  3846. org-bracket-link-analytic-regexp++
  3847. (concat
  3848. "\\[\\["
  3849. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3850. "\\([^]]+\\)"
  3851. "\\]"
  3852. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3853. "\\]")
  3854. org-any-link-re
  3855. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3856. org-angle-link-re "\\)\\|\\("
  3857. org-plain-link-re "\\)")))
  3858. (org-make-link-regexps)
  3859. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3860. "Regular expression for fast time stamp matching.")
  3861. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3862. "Regular expression for fast time stamp matching.")
  3863. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3864. "Regular expression matching time strings for analysis.
  3865. This one does not require the space after the date, so it can be used
  3866. on a string that terminates immediately after the date.")
  3867. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3868. "Regular expression matching time strings for analysis.")
  3869. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3870. "Regular expression matching time stamps, with groups.")
  3871. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3872. "Regular expression matching time stamps (also [..]), with groups.")
  3873. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3874. "Regular expression matching a time stamp range.")
  3875. (defconst org-tr-regexp-both
  3876. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3877. "Regular expression matching a time stamp range.")
  3878. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3879. org-ts-regexp "\\)?")
  3880. "Regular expression matching a time stamp or time stamp range.")
  3881. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3882. org-ts-regexp-both "\\)?")
  3883. "Regular expression matching a time stamp or time stamp range.
  3884. The time stamps may be either active or inactive.")
  3885. (defvar org-emph-face nil)
  3886. (defun org-do-emphasis-faces (limit)
  3887. "Run through the buffer and add overlays to links."
  3888. (let (rtn a)
  3889. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3890. (if (not (= (char-after (match-beginning 3))
  3891. (char-after (match-beginning 4))))
  3892. (progn
  3893. (setq rtn t)
  3894. (setq a (assoc (match-string 3) org-emphasis-alist))
  3895. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3896. 'face
  3897. (nth 1 a))
  3898. (and (nth 4 a)
  3899. (org-remove-flyspell-overlays-in
  3900. (match-beginning 0) (match-end 0)))
  3901. (add-text-properties (match-beginning 2) (match-end 2)
  3902. '(font-lock-multiline t))
  3903. (when org-hide-emphasis-markers
  3904. (add-text-properties (match-end 4) (match-beginning 5)
  3905. '(invisible org-link))
  3906. (add-text-properties (match-beginning 3) (match-end 3)
  3907. '(invisible org-link)))))
  3908. (backward-char 1))
  3909. rtn))
  3910. (defun org-emphasize (&optional char)
  3911. "Insert or change an emphasis, i.e. a font like bold or italic.
  3912. If there is an active region, change that region to a new emphasis.
  3913. If there is no region, just insert the marker characters and position
  3914. the cursor between them.
  3915. CHAR should be either the marker character, or the first character of the
  3916. HTML tag associated with that emphasis. If CHAR is a space, the means
  3917. to remove the emphasis of the selected region.
  3918. If char is not given (for example in an interactive call) it
  3919. will be prompted for."
  3920. (interactive)
  3921. (let ((eal org-emphasis-alist) e det
  3922. (erc org-emphasis-regexp-components)
  3923. (prompt "")
  3924. (string "") beg end move tag c s)
  3925. (if (org-region-active-p)
  3926. (setq beg (region-beginning) end (region-end)
  3927. string (buffer-substring beg end))
  3928. (setq move t))
  3929. (while (setq e (pop eal))
  3930. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3931. c (aref tag 0))
  3932. (push (cons c (string-to-char (car e))) det)
  3933. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3934. (substring tag 1)))))
  3935. (setq det (nreverse det))
  3936. (unless char
  3937. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3938. (setq char (read-char-exclusive)))
  3939. (setq char (or (cdr (assoc char det)) char))
  3940. (if (equal char ?\ )
  3941. (setq s "" move nil)
  3942. (unless (assoc (char-to-string char) org-emphasis-alist)
  3943. (error "No such emphasis marker: \"%c\"" char))
  3944. (setq s (char-to-string char)))
  3945. (while (and (> (length string) 1)
  3946. (equal (substring string 0 1) (substring string -1))
  3947. (assoc (substring string 0 1) org-emphasis-alist))
  3948. (setq string (substring string 1 -1)))
  3949. (setq string (concat s string s))
  3950. (if beg (delete-region beg end))
  3951. (unless (or (bolp)
  3952. (string-match (concat "[" (nth 0 erc) "\n]")
  3953. (char-to-string (char-before (point)))))
  3954. (insert " "))
  3955. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3956. (char-to-string (char-after (point))))
  3957. (insert " ") (backward-char 1))
  3958. (insert string)
  3959. (and move (backward-char 1))))
  3960. (defconst org-nonsticky-props
  3961. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3962. (defsubst org-rear-nonsticky-at (pos)
  3963. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3964. (defun org-activate-plain-links (limit)
  3965. "Run through the buffer and add overlays to links."
  3966. (catch 'exit
  3967. (let (f)
  3968. (if (re-search-forward org-plain-link-re limit t)
  3969. (progn
  3970. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3971. (setq f (get-text-property (match-beginning 0) 'face))
  3972. (if (or (eq f 'org-tag)
  3973. (and (listp f) (memq 'org-tag f)))
  3974. nil
  3975. (add-text-properties (match-beginning 0) (match-end 0)
  3976. (list 'mouse-face 'highlight
  3977. 'keymap org-mouse-map))
  3978. (org-rear-nonsticky-at (match-end 0)))
  3979. t)))))
  3980. (defun org-activate-code (limit)
  3981. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3982. (progn
  3983. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3984. (remove-text-properties (match-beginning 0) (match-end 0)
  3985. '(display t invisible t intangible t))
  3986. t)))
  3987. (defun org-fontify-meta-lines-and-blocks (limit)
  3988. "Fontify #+ lines and blocks, in the correct ways."
  3989. (let ((case-fold-search t))
  3990. (if (re-search-forward
  3991. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3992. limit t)
  3993. (let ((beg (match-beginning 0))
  3994. (beg1 (line-beginning-position 2))
  3995. (dc1 (downcase (match-string 2)))
  3996. (dc3 (downcase (match-string 3)))
  3997. end end1 quoting)
  3998. (cond
  3999. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4000. ;; a single line of backend-specific content
  4001. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4002. (remove-text-properties (match-beginning 0) (match-end 0)
  4003. '(display t invisible t intangible t))
  4004. (add-text-properties (match-beginning 1) (match-end 3)
  4005. '(font-lock-fontified t face org-meta-line))
  4006. (add-text-properties (match-beginning 6) (match-end 6)
  4007. '(font-lock-fontified t face org-block))
  4008. t)
  4009. ((and (match-end 4) (equal dc3 "begin"))
  4010. ;; Truely a block
  4011. (setq quoting (member (downcase (match-string 5))
  4012. org-protecting-blocks))
  4013. (when (re-search-forward
  4014. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4015. nil t) ;; on purpose, we look further than LIMIT
  4016. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4017. (when quoting
  4018. (remove-text-properties beg end
  4019. '(display t invisible t intangible t)))
  4020. (add-text-properties
  4021. beg end
  4022. '(font-lock-fontified t font-lock-multiline t))
  4023. (add-text-properties beg beg1 '(face org-meta-line))
  4024. (add-text-properties end1 end '(face org-meta-line))
  4025. (when quoting
  4026. (add-text-properties beg1 end1 '(face org-block)))
  4027. t))
  4028. ((not (member (char-after beg) '(?\ ?\t)))
  4029. ;; just any other in-buffer setting, but not indented
  4030. (add-text-properties
  4031. beg (match-end 0)
  4032. '(font-lock-fontified t face org-meta-line))
  4033. t)
  4034. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4035. "orgtbl:" "tblfm:" "tblname:"))
  4036. (and (match-end 4) (equal dc3 "attr")))
  4037. (add-text-properties
  4038. beg (match-end 0)
  4039. '(font-lock-fontified t face org-meta-line))
  4040. t)
  4041. ((member dc3 '(" " ""))
  4042. (add-text-properties
  4043. beg (match-end 0)
  4044. '(font-lock-fontified t face font-lock-comment-face)))
  4045. (t nil))))))
  4046. (defun org-activate-angle-links (limit)
  4047. "Run through the buffer and add overlays to links."
  4048. (if (re-search-forward org-angle-link-re limit t)
  4049. (progn
  4050. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4051. (add-text-properties (match-beginning 0) (match-end 0)
  4052. (list 'mouse-face 'highlight
  4053. 'keymap org-mouse-map))
  4054. (org-rear-nonsticky-at (match-end 0))
  4055. t)))
  4056. (defun org-activate-footnote-links (limit)
  4057. "Run through the buffer and add overlays to links."
  4058. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4059. limit t)
  4060. (progn
  4061. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4062. (add-text-properties (match-beginning 2) (match-end 2)
  4063. (list 'mouse-face 'highlight
  4064. 'keymap org-mouse-map
  4065. 'help-echo
  4066. (if (= (point-at-bol) (match-beginning 2))
  4067. "Footnote definition"
  4068. "Footnote reference")
  4069. ))
  4070. (org-rear-nonsticky-at (match-end 2))
  4071. t)))
  4072. (defun org-activate-bracket-links (limit)
  4073. "Run through the buffer and add overlays to bracketed links."
  4074. (if (re-search-forward org-bracket-link-regexp limit t)
  4075. (let* ((help (concat "LINK: "
  4076. (org-match-string-no-properties 1)))
  4077. ;; FIXME: above we should remove the escapes.
  4078. ;; but that requires another match, protecting match data,
  4079. ;; a lot of overhead for font-lock.
  4080. (ip (org-maybe-intangible
  4081. (list 'invisible 'org-link
  4082. 'keymap org-mouse-map 'mouse-face 'highlight
  4083. 'font-lock-multiline t 'help-echo help)))
  4084. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4085. 'font-lock-multiline t 'help-echo help)))
  4086. ;; We need to remove the invisible property here. Table narrowing
  4087. ;; may have made some of this invisible.
  4088. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4089. (remove-text-properties (match-beginning 0) (match-end 0)
  4090. '(invisible nil))
  4091. (if (match-end 3)
  4092. (progn
  4093. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4094. (org-rear-nonsticky-at (match-beginning 3))
  4095. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4096. (org-rear-nonsticky-at (match-end 3))
  4097. (add-text-properties (match-end 3) (match-end 0) ip)
  4098. (org-rear-nonsticky-at (match-end 0)))
  4099. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4100. (org-rear-nonsticky-at (match-beginning 1))
  4101. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4102. (org-rear-nonsticky-at (match-end 1))
  4103. (add-text-properties (match-end 1) (match-end 0) ip)
  4104. (org-rear-nonsticky-at (match-end 0)))
  4105. t)))
  4106. (defun org-activate-dates (limit)
  4107. "Run through the buffer and add overlays to dates."
  4108. (if (re-search-forward org-tsr-regexp-both limit t)
  4109. (progn
  4110. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4111. (add-text-properties (match-beginning 0) (match-end 0)
  4112. (list 'mouse-face 'highlight
  4113. 'keymap org-mouse-map))
  4114. (org-rear-nonsticky-at (match-end 0))
  4115. (when org-display-custom-times
  4116. (if (match-end 3)
  4117. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4118. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4119. t)))
  4120. (defvar org-target-link-regexp nil
  4121. "Regular expression matching radio targets in plain text.")
  4122. (make-variable-buffer-local 'org-target-link-regexp)
  4123. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4124. "Regular expression matching a link target.")
  4125. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4126. "Regular expression matching a radio target.")
  4127. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4128. "Regular expression matching any target.")
  4129. (defun org-activate-target-links (limit)
  4130. "Run through the buffer and add overlays to target matches."
  4131. (when org-target-link-regexp
  4132. (let ((case-fold-search t))
  4133. (if (re-search-forward org-target-link-regexp limit t)
  4134. (progn
  4135. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4136. (add-text-properties (match-beginning 0) (match-end 0)
  4137. (list 'mouse-face 'highlight
  4138. 'keymap org-mouse-map
  4139. 'help-echo "Radio target link"
  4140. 'org-linked-text t))
  4141. (org-rear-nonsticky-at (match-end 0))
  4142. t)))))
  4143. (defun org-update-radio-target-regexp ()
  4144. "Find all radio targets in this file and update the regular expression."
  4145. (interactive)
  4146. (when (memq 'radio org-activate-links)
  4147. (setq org-target-link-regexp
  4148. (org-make-target-link-regexp (org-all-targets 'radio)))
  4149. (org-restart-font-lock)))
  4150. (defun org-hide-wide-columns (limit)
  4151. (let (s e)
  4152. (setq s (text-property-any (point) (or limit (point-max))
  4153. 'org-cwidth t))
  4154. (when s
  4155. (setq e (next-single-property-change s 'org-cwidth))
  4156. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4157. (goto-char e)
  4158. t)))
  4159. (defvar org-latex-and-specials-regexp nil
  4160. "Regular expression for highlighting export special stuff.")
  4161. (defvar org-match-substring-regexp)
  4162. (defvar org-match-substring-with-braces-regexp)
  4163. ;; This should be with the exporter code, but we also use if for font-locking
  4164. (defconst org-export-html-special-string-regexps
  4165. '(("\\\\-" . "&shy;")
  4166. ("---\\([^-]\\)" . "&mdash;\\1")
  4167. ("--\\([^-]\\)" . "&ndash;\\1")
  4168. ("\\.\\.\\." . "&hellip;"))
  4169. "Regular expressions for special string conversion.")
  4170. (defun org-compute-latex-and-specials-regexp ()
  4171. "Compute regular expression for stuff treated specially by exporters."
  4172. (if (not org-highlight-latex-fragments-and-specials)
  4173. (org-set-local 'org-latex-and-specials-regexp nil)
  4174. (require 'org-exp)
  4175. (let*
  4176. ((matchers (plist-get org-format-latex-options :matchers))
  4177. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4178. org-latex-regexps)))
  4179. (options (org-combine-plists (org-default-export-plist)
  4180. (org-infile-export-plist)))
  4181. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4182. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4183. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4184. (org-export-html-expand (plist-get options :expand-quoted-html))
  4185. (org-export-with-special-strings (plist-get options :special-strings))
  4186. (re-sub
  4187. (cond
  4188. ((equal org-export-with-sub-superscripts '{})
  4189. (list org-match-substring-with-braces-regexp))
  4190. (org-export-with-sub-superscripts
  4191. (list org-match-substring-regexp))
  4192. (t nil)))
  4193. (re-latex
  4194. (if org-export-with-LaTeX-fragments
  4195. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4196. (re-macros
  4197. (if org-export-with-TeX-macros
  4198. (list (concat "\\\\"
  4199. (regexp-opt
  4200. (append (mapcar 'car org-html-entities)
  4201. (if (boundp 'org-latex-entities)
  4202. (mapcar (lambda (x)
  4203. (or (car-safe x) x))
  4204. org-latex-entities)
  4205. nil))
  4206. 'words))) ; FIXME
  4207. ))
  4208. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4209. (re-special (if org-export-with-special-strings
  4210. (mapcar (lambda (x) (car x))
  4211. org-export-html-special-string-regexps)))
  4212. (re-rest
  4213. (delq nil
  4214. (list
  4215. (if org-export-html-expand "@<[^>\n]+>")
  4216. ))))
  4217. (org-set-local
  4218. 'org-latex-and-specials-regexp
  4219. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4220. re-rest) "\\|")))))
  4221. (defun org-do-latex-and-special-faces (limit)
  4222. "Run through the buffer and add overlays to links."
  4223. (when org-latex-and-specials-regexp
  4224. (let (rtn d)
  4225. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4226. limit t))
  4227. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4228. 'face))
  4229. '(org-code org-verbatim underline)))
  4230. (progn
  4231. (setq rtn t
  4232. d (cond ((member (char-after (1+ (match-beginning 0)))
  4233. '(?_ ?^)) 1)
  4234. (t 0)))
  4235. (font-lock-prepend-text-property
  4236. (+ d (match-beginning 0)) (match-end 0)
  4237. 'face 'org-latex-and-export-specials)
  4238. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4239. '(font-lock-multiline t)))))
  4240. rtn)))
  4241. (defun org-restart-font-lock ()
  4242. "Restart font-lock-mode, to force refontification."
  4243. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4244. (font-lock-mode -1)
  4245. (font-lock-mode 1)))
  4246. (defun org-all-targets (&optional radio)
  4247. "Return a list of all targets in this file.
  4248. With optional argument RADIO, only find radio targets."
  4249. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4250. rtn)
  4251. (save-excursion
  4252. (goto-char (point-min))
  4253. (while (re-search-forward re nil t)
  4254. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4255. rtn)))
  4256. (defun org-make-target-link-regexp (targets)
  4257. "Make regular expression matching all strings in TARGETS.
  4258. The regular expression finds the targets also if there is a line break
  4259. between words."
  4260. (and targets
  4261. (concat
  4262. "\\<\\("
  4263. (mapconcat
  4264. (lambda (x)
  4265. (while (string-match " +" x)
  4266. (setq x (replace-match "\\s-+" t t x)))
  4267. x)
  4268. targets
  4269. "\\|")
  4270. "\\)\\>")))
  4271. (defun org-activate-tags (limit)
  4272. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4273. (progn
  4274. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4275. (add-text-properties (match-beginning 1) (match-end 1)
  4276. (list 'mouse-face 'highlight
  4277. 'keymap org-mouse-map))
  4278. (org-rear-nonsticky-at (match-end 1))
  4279. t)))
  4280. (defun org-outline-level ()
  4281. "Compute the outline level of the heading at point.
  4282. This function assumes that the cursor is at the beginning of a line matched
  4283. by outline-regexp. Otherwise it returns garbage.
  4284. If this is called at a normal headline, the level is the number of stars.
  4285. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4286. For plain list items, if they are matched by `outline-regexp', this returns
  4287. 1000 plus the line indentation."
  4288. (save-excursion
  4289. (looking-at outline-regexp)
  4290. (if (match-beginning 1)
  4291. (+ (org-get-string-indentation (match-string 1)) 1000)
  4292. (1- (- (match-end 0) (match-beginning 0))))))
  4293. (defvar org-font-lock-keywords nil)
  4294. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4295. "Regular expression matching a property line.")
  4296. (defvar org-font-lock-hook nil
  4297. "Functions to be called for special font lock stuff.")
  4298. (defun org-font-lock-hook (limit)
  4299. (run-hook-with-args 'org-font-lock-hook limit))
  4300. (defun org-set-font-lock-defaults ()
  4301. (let* ((em org-fontify-emphasized-text)
  4302. (lk org-activate-links)
  4303. (org-font-lock-extra-keywords
  4304. (list
  4305. ;; Call the hook
  4306. '(org-font-lock-hook)
  4307. ;; Headlines
  4308. `(,(if org-fontify-whole-heading-line
  4309. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4310. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4311. (1 (org-get-level-face 1))
  4312. (2 (org-get-level-face 2))
  4313. (3 (org-get-level-face 3)))
  4314. ;; Table lines
  4315. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4316. (1 'org-table t))
  4317. ;; Table internals
  4318. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4319. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4320. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4321. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4322. ;; Drawers
  4323. (list org-drawer-regexp '(0 'org-special-keyword t))
  4324. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4325. ;; Properties
  4326. (list org-property-re
  4327. '(1 'org-special-keyword t)
  4328. '(3 'org-property-value t))
  4329. ;; Links
  4330. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4331. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4332. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4333. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4334. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4335. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4336. (if (memq 'footnote lk) '(org-activate-footnote-links
  4337. (2 'org-footnote t)))
  4338. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4339. '(org-hide-wide-columns (0 nil append))
  4340. ;; TODO lines
  4341. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4342. '(1 (org-get-todo-face 1) t))
  4343. ;; DONE
  4344. (if org-fontify-done-headline
  4345. (list (concat "^[*]+ +\\<\\("
  4346. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4347. "\\)\\(.*\\)")
  4348. '(2 'org-headline-done t))
  4349. nil)
  4350. ;; Priorities
  4351. '(org-font-lock-add-priority-faces)
  4352. ;; Tags
  4353. '(org-font-lock-add-tag-faces)
  4354. ;; Special keywords
  4355. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4356. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4357. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4358. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4359. ;; Emphasis
  4360. (if em
  4361. (if (featurep 'xemacs)
  4362. '(org-do-emphasis-faces (0 nil append))
  4363. '(org-do-emphasis-faces)))
  4364. ;; Checkboxes
  4365. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4366. 2 'org-checkbox prepend)
  4367. (if org-provide-checkbox-statistics
  4368. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4369. (0 (org-get-checkbox-statistics-face) t)))
  4370. ;; Description list items
  4371. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4372. 2 'bold prepend)
  4373. ;; ARCHIVEd headings
  4374. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4375. '(1 'org-archived prepend))
  4376. ;; Specials
  4377. '(org-do-latex-and-special-faces)
  4378. ;; Code
  4379. '(org-activate-code (1 'org-code t))
  4380. ;; COMMENT
  4381. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4382. "\\|" org-quote-string "\\)\\>")
  4383. '(1 'org-special-keyword t))
  4384. '("^#.*" (0 'font-lock-comment-face t))
  4385. ;; Blocks and meta lines
  4386. '(org-fontify-meta-lines-and-blocks)
  4387. )))
  4388. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4389. ;; Now set the full font-lock-keywords
  4390. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4391. (org-set-local 'font-lock-defaults
  4392. '(org-font-lock-keywords t nil nil backward-paragraph))
  4393. (kill-local-variable 'font-lock-keywords) nil))
  4394. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4395. "Fontify string S like in Org-mode"
  4396. (with-temp-buffer
  4397. (insert s)
  4398. (let ((org-odd-levels-only odd-levels))
  4399. (org-mode)
  4400. (font-lock-fontify-buffer)
  4401. (buffer-string))))
  4402. (defvar org-m nil)
  4403. (defvar org-l nil)
  4404. (defvar org-f nil)
  4405. (defun org-get-level-face (n)
  4406. "Get the right face for match N in font-lock matching of headlines."
  4407. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4408. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4409. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4410. (cond
  4411. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4412. ((eq n 2) org-f)
  4413. (t (if org-level-color-stars-only nil org-f))))
  4414. (defun org-get-todo-face (kwd)
  4415. "Get the right face for a TODO keyword KWD.
  4416. If KWD is a number, get the corresponding match group."
  4417. (if (numberp kwd) (setq kwd (match-string kwd)))
  4418. (or (cdr (assoc kwd org-todo-keyword-faces))
  4419. (and (member kwd org-done-keywords) 'org-done)
  4420. 'org-todo))
  4421. (defun org-font-lock-add-tag-faces (limit)
  4422. "Add the special tag faces."
  4423. (when (and org-tag-faces org-tags-special-faces-re)
  4424. (while (re-search-forward org-tags-special-faces-re limit t)
  4425. (add-text-properties (match-beginning 1) (match-end 1)
  4426. (list 'face (org-get-tag-face 1)
  4427. 'font-lock-fontified t))
  4428. (backward-char 1))))
  4429. (defun org-font-lock-add-priority-faces (limit)
  4430. "Add the special priority faces."
  4431. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4432. (add-text-properties
  4433. (match-beginning 0) (match-end 0)
  4434. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4435. org-priority-faces))
  4436. 'org-special-keyword)
  4437. 'font-lock-fontified t))))
  4438. (defun org-get-tag-face (kwd)
  4439. "Get the right face for a TODO keyword KWD.
  4440. If KWD is a number, get the corresponding match group."
  4441. (if (numberp kwd) (setq kwd (match-string kwd)))
  4442. (or (cdr (assoc kwd org-tag-faces))
  4443. 'org-tag))
  4444. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4445. "Remove fontification and activation overlays from links."
  4446. (font-lock-default-unfontify-region beg end)
  4447. (let* ((buffer-undo-list t)
  4448. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4449. (inhibit-modification-hooks t)
  4450. deactivate-mark buffer-file-name buffer-file-truename)
  4451. (remove-text-properties
  4452. beg end
  4453. (if org-indent-mode
  4454. ;; also remove line-prefix and wrap-prefix properties
  4455. '(mouse-face t keymap t org-linked-text t
  4456. invisible t intangible t
  4457. line-prefix t wrap-prefix t
  4458. org-no-flyspell t)
  4459. '(mouse-face t keymap t org-linked-text t
  4460. invisible t intangible t
  4461. org-no-flyspell t)))))
  4462. ;;;; Visibility cycling, including org-goto and indirect buffer
  4463. ;;; Cycling
  4464. (defvar org-cycle-global-status nil)
  4465. (make-variable-buffer-local 'org-cycle-global-status)
  4466. (defvar org-cycle-subtree-status nil)
  4467. (make-variable-buffer-local 'org-cycle-subtree-status)
  4468. ;;;###autoload
  4469. (defvar org-inlinetask-min-level)
  4470. (defun org-cycle (&optional arg)
  4471. "TAB-action and visibility cycling for Org-mode.
  4472. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4473. is outline visibility cycling, but it also invokes other actions
  4474. in special contexts.
  4475. - When this function is called with a prefix argument, rotate the entire
  4476. buffer through 3 states (global cycling)
  4477. 1. OVERVIEW: Show only top-level headlines.
  4478. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4479. 3. SHOW ALL: Show everything.
  4480. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4481. determined by the variable `org-startup-folded', and by any VISIBILITY
  4482. properties in the buffer.
  4483. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4484. including any drawers.
  4485. - When inside a table, re-align the table and move to the next field.
  4486. - When point is at the beginning of a headline, rotate the subtree started
  4487. by this line through 3 different states (local cycling)
  4488. 1. FOLDED: Only the main headline is shown.
  4489. 2. CHILDREN: The main headline and the direct children are shown.
  4490. From this state, you can move to one of the children
  4491. and zoom in further.
  4492. 3. SUBTREE: Show the entire subtree, including body text.
  4493. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4494. - When there is a numeric prefix, go up to a heading with level ARG, do
  4495. a `show-subtree' and return to the previous cursor position. If ARG
  4496. is negative, go up that many levels.
  4497. - When point is not at the beginning of a headline, execute the global
  4498. binding for TAB, which is re-indenting the line. See the option
  4499. `org-cycle-emulate-tab' for details.
  4500. - Special case: if point is at the beginning of the buffer and there is
  4501. no headline in line 1, this function will act as if called with prefix arg.
  4502. But only if also the variable `org-cycle-global-at-bob' is t."
  4503. (interactive "P")
  4504. (org-load-modules-maybe)
  4505. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4506. (and org-cycle-level-after-item/entry-creation
  4507. (or (org-cycle-level)
  4508. (org-cycle-item-indentation))))
  4509. (let* ((limit-level
  4510. (or org-cycle-max-level
  4511. (and (boundp 'org-inlinetask-min-level)
  4512. org-inlinetask-min-level
  4513. (1- org-inlinetask-min-level))))
  4514. (nstars (and limit-level
  4515. (if org-odd-levels-only
  4516. (and limit-level (1- (* limit-level 2)))
  4517. limit-level)))
  4518. (outline-regexp
  4519. (cond
  4520. ((not (org-mode-p)) outline-regexp)
  4521. ((or (eq org-cycle-include-plain-lists 'integrate)
  4522. (and org-cycle-include-plain-lists (org-at-item-p)))
  4523. (concat "\\(?:\\*"
  4524. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4525. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4526. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4527. (bob-special (and org-cycle-global-at-bob (bobp)
  4528. (not (looking-at outline-regexp))))
  4529. (org-cycle-hook
  4530. (if bob-special
  4531. (delq 'org-optimize-window-after-visibility-change
  4532. (copy-sequence org-cycle-hook))
  4533. org-cycle-hook))
  4534. (pos (point)))
  4535. (if (or bob-special (equal arg '(4)))
  4536. ;; special case: use global cycling
  4537. (setq arg t))
  4538. (cond
  4539. ((equal arg '(16))
  4540. (org-set-startup-visibility)
  4541. (message "Startup visibility, plus VISIBILITY properties"))
  4542. ((equal arg '(64))
  4543. (show-all)
  4544. (message "Entire buffer visible, including drawers"))
  4545. ((org-at-table-p 'any)
  4546. ;; Enter the table or move to the next field in the table
  4547. (or (org-table-recognize-table.el)
  4548. (progn
  4549. (if arg (org-table-edit-field t)
  4550. (org-table-justify-field-maybe)
  4551. (call-interactively 'org-table-next-field)))))
  4552. ((run-hook-with-args-until-success
  4553. 'org-tab-after-check-for-table-hook))
  4554. ((eq arg t) ;; Global cycling
  4555. (org-cycle-internal-global))
  4556. ((and org-drawers org-drawer-regexp
  4557. (save-excursion
  4558. (beginning-of-line 1)
  4559. (looking-at org-drawer-regexp)))
  4560. ;; Toggle block visibility
  4561. (org-flag-drawer
  4562. (not (get-char-property (match-end 0) 'invisible))))
  4563. ((integerp arg)
  4564. ;; Show-subtree, ARG levels up from here.
  4565. (save-excursion
  4566. (org-back-to-heading)
  4567. (outline-up-heading (if (< arg 0) (- arg)
  4568. (- (funcall outline-level) arg)))
  4569. (org-show-subtree)))
  4570. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4571. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4572. (org-cycle-internal-local))
  4573. ;; TAB emulation and template completion
  4574. (buffer-read-only (org-back-to-heading))
  4575. ((run-hook-with-args-until-success
  4576. 'org-tab-after-check-for-cycling-hook))
  4577. ((org-try-structure-completion))
  4578. ((org-try-cdlatex-tab))
  4579. ((run-hook-with-args-until-success
  4580. 'org-tab-before-tab-emulation-hook))
  4581. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4582. (or (not (bolp))
  4583. (not (looking-at outline-regexp))))
  4584. (call-interactively (global-key-binding "\t")))
  4585. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4586. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4587. (or (and (eq org-cycle-emulate-tab 'white)
  4588. (= (match-end 0) (point-at-eol)))
  4589. (and (eq org-cycle-emulate-tab 'whitestart)
  4590. (>= (match-end 0) pos))))
  4591. t
  4592. (eq org-cycle-emulate-tab t))
  4593. (call-interactively (global-key-binding "\t")))
  4594. (t (save-excursion
  4595. (org-back-to-heading)
  4596. (org-cycle)))))))
  4597. (defun org-cycle-internal-global ()
  4598. "Do the global cycling action."
  4599. (cond
  4600. ((and (eq last-command this-command)
  4601. (eq org-cycle-global-status 'overview))
  4602. ;; We just created the overview - now do table of contents
  4603. ;; This can be slow in very large buffers, so indicate action
  4604. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4605. (message "CONTENTS...")
  4606. (org-content)
  4607. (message "CONTENTS...done")
  4608. (setq org-cycle-global-status 'contents)
  4609. (run-hook-with-args 'org-cycle-hook 'contents))
  4610. ((and (eq last-command this-command)
  4611. (eq org-cycle-global-status 'contents))
  4612. ;; We just showed the table of contents - now show everything
  4613. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4614. (show-all)
  4615. (message "SHOW ALL")
  4616. (setq org-cycle-global-status 'all)
  4617. (run-hook-with-args 'org-cycle-hook 'all))
  4618. (t
  4619. ;; Default action: go to overview
  4620. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4621. (org-overview)
  4622. (message "OVERVIEW")
  4623. (setq org-cycle-global-status 'overview)
  4624. (run-hook-with-args 'org-cycle-hook 'overview))))
  4625. (defun org-cycle-internal-local ()
  4626. "Do the local cycling action."
  4627. (org-back-to-heading)
  4628. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4629. ;; First, some boundaries
  4630. (save-excursion
  4631. (org-back-to-heading)
  4632. (setq level (funcall outline-level))
  4633. (save-excursion
  4634. (beginning-of-line 2)
  4635. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4636. ; XEmacs does not have `next-single-char-property-change'
  4637. ; I'm not sure about Emacs 21.
  4638. (while (and (not (eobp)) ;; this is like `next-line'
  4639. (get-char-property (1- (point)) 'invisible))
  4640. (beginning-of-line 2))
  4641. (while (and (not (eobp)) ;; this is like `next-line'
  4642. (get-char-property (1- (point)) 'invisible))
  4643. (goto-char (next-single-char-property-change (point) 'invisible))
  4644. ;;;??? (or (bolp) (beginning-of-line 2))))
  4645. (and (eolp) (beginning-of-line 2))))
  4646. (setq eol (point)))
  4647. (outline-end-of-heading) (setq eoh (point))
  4648. (save-excursion
  4649. (outline-next-heading)
  4650. (setq has-children (and (org-at-heading-p t)
  4651. (> (funcall outline-level) level))))
  4652. (org-end-of-subtree t)
  4653. (unless (eobp)
  4654. (skip-chars-forward " \t\n")
  4655. (beginning-of-line 1) ; in case this is an item
  4656. )
  4657. (setq eos (if (eobp) (point) (1- (point)))))
  4658. ;; Find out what to do next and set `this-command'
  4659. (cond
  4660. ((= eos eoh)
  4661. ;; Nothing is hidden behind this heading
  4662. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4663. (message "EMPTY ENTRY")
  4664. (setq org-cycle-subtree-status nil)
  4665. (save-excursion
  4666. (goto-char eos)
  4667. (outline-next-heading)
  4668. (if (org-invisible-p) (org-flag-heading nil))))
  4669. ((and (or (>= eol eos)
  4670. (not (string-match "\\S-" (buffer-substring eol eos))))
  4671. (or has-children
  4672. (not (setq children-skipped
  4673. org-cycle-skip-children-state-if-no-children))))
  4674. ;; Entire subtree is hidden in one line: children view
  4675. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4676. (org-show-entry)
  4677. (show-children)
  4678. (message "CHILDREN")
  4679. (save-excursion
  4680. (goto-char eos)
  4681. (outline-next-heading)
  4682. (if (org-invisible-p) (org-flag-heading nil)))
  4683. (setq org-cycle-subtree-status 'children)
  4684. (run-hook-with-args 'org-cycle-hook 'children))
  4685. ((or children-skipped
  4686. (and (eq last-command this-command)
  4687. (eq org-cycle-subtree-status 'children)))
  4688. ;; We just showed the children, or no children are there,
  4689. ;; now show everything.
  4690. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4691. (org-show-subtree)
  4692. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4693. (setq org-cycle-subtree-status 'subtree)
  4694. (run-hook-with-args 'org-cycle-hook 'subtree))
  4695. (t
  4696. ;; Default action: hide the subtree.
  4697. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4698. (hide-subtree)
  4699. (message "FOLDED")
  4700. (setq org-cycle-subtree-status 'folded)
  4701. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4702. ;;;###autoload
  4703. (defun org-global-cycle (&optional arg)
  4704. "Cycle the global visibility. For details see `org-cycle'.
  4705. With C-u prefix arg, switch to startup visibility.
  4706. With a numeric prefix, show all headlines up to that level."
  4707. (interactive "P")
  4708. (let ((org-cycle-include-plain-lists
  4709. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4710. (cond
  4711. ((integerp arg)
  4712. (show-all)
  4713. (hide-sublevels arg)
  4714. (setq org-cycle-global-status 'contents))
  4715. ((equal arg '(4))
  4716. (org-set-startup-visibility)
  4717. (message "Startup visibility, plus VISIBILITY properties."))
  4718. (t
  4719. (org-cycle '(4))))))
  4720. (defun org-set-startup-visibility ()
  4721. "Set the visibility required by startup options and properties."
  4722. (cond
  4723. ((eq org-startup-folded t)
  4724. (org-cycle '(4)))
  4725. ((eq org-startup-folded 'content)
  4726. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4727. (org-cycle '(4)) (org-cycle '(4)))))
  4728. (unless (eq org-startup-folded 'showeverything)
  4729. (if org-hide-block-startup (org-hide-block-all))
  4730. (org-set-visibility-according-to-property 'no-cleanup)
  4731. (org-cycle-hide-archived-subtrees 'all)
  4732. (org-cycle-hide-drawers 'all)
  4733. (org-cycle-show-empty-lines 'all)))
  4734. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4735. "Switch subtree visibilities according to :VISIBILITY: property."
  4736. (interactive)
  4737. (let (org-show-entry-below state)
  4738. (save-excursion
  4739. (goto-char (point-min))
  4740. (while (re-search-forward
  4741. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4742. nil t)
  4743. (setq state (match-string 1))
  4744. (save-excursion
  4745. (org-back-to-heading t)
  4746. (hide-subtree)
  4747. (org-reveal)
  4748. (cond
  4749. ((equal state '("fold" "folded"))
  4750. (hide-subtree))
  4751. ((equal state "children")
  4752. (org-show-hidden-entry)
  4753. (show-children))
  4754. ((equal state "content")
  4755. (save-excursion
  4756. (save-restriction
  4757. (org-narrow-to-subtree)
  4758. (org-content))))
  4759. ((member state '("all" "showall"))
  4760. (show-subtree)))))
  4761. (unless no-cleanup
  4762. (org-cycle-hide-archived-subtrees 'all)
  4763. (org-cycle-hide-drawers 'all)
  4764. (org-cycle-show-empty-lines 'all)))))
  4765. (defun org-overview ()
  4766. "Switch to overview mode, showing only top-level headlines.
  4767. Really, this shows all headlines with level equal or greater than the level
  4768. of the first headline in the buffer. This is important, because if the
  4769. first headline is not level one, then (hide-sublevels 1) gives confusing
  4770. results."
  4771. (interactive)
  4772. (let ((level (save-excursion
  4773. (goto-char (point-min))
  4774. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4775. (progn
  4776. (goto-char (match-beginning 0))
  4777. (funcall outline-level))))))
  4778. (and level (hide-sublevels level))))
  4779. (defun org-content (&optional arg)
  4780. "Show all headlines in the buffer, like a table of contents.
  4781. With numerical argument N, show content up to level N."
  4782. (interactive "P")
  4783. (save-excursion
  4784. ;; Visit all headings and show their offspring
  4785. (and (integerp arg) (org-overview))
  4786. (goto-char (point-max))
  4787. (catch 'exit
  4788. (while (and (progn (condition-case nil
  4789. (outline-previous-visible-heading 1)
  4790. (error (goto-char (point-min))))
  4791. t)
  4792. (looking-at outline-regexp))
  4793. (if (integerp arg)
  4794. (show-children (1- arg))
  4795. (show-branches))
  4796. (if (bobp) (throw 'exit nil))))))
  4797. (defun org-optimize-window-after-visibility-change (state)
  4798. "Adjust the window after a change in outline visibility.
  4799. This function is the default value of the hook `org-cycle-hook'."
  4800. (when (get-buffer-window (current-buffer))
  4801. (cond
  4802. ((eq state 'content) nil)
  4803. ((eq state 'all) nil)
  4804. ((eq state 'folded) nil)
  4805. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4806. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4807. ;; FIXME: no longer in use
  4808. (defun org-compact-display-after-subtree-move ()
  4809. "Show a compacter version of the tree of the entry's parent."
  4810. (save-excursion
  4811. (if (org-up-heading-safe)
  4812. (progn
  4813. (hide-subtree)
  4814. (show-entry)
  4815. (show-children)
  4816. (org-cycle-show-empty-lines 'children)
  4817. (org-cycle-hide-drawers 'children))
  4818. (org-overview))))
  4819. (defun org-remove-empty-overlays-at (pos)
  4820. "Remove outline overlays that do not contain non-white stuff."
  4821. (mapc
  4822. (lambda (o)
  4823. (and (eq 'outline (org-overlay-get o 'invisible))
  4824. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4825. (org-overlay-end o))))
  4826. (org-delete-overlay o)))
  4827. (org-overlays-at pos)))
  4828. (defun org-clean-visibility-after-subtree-move ()
  4829. "Fix visibility issues after moving a subtree."
  4830. ;; First, find a reasonable region to look at:
  4831. ;; Start two siblings above, end three below
  4832. (let* ((beg (save-excursion
  4833. (and (org-get-last-sibling)
  4834. (org-get-last-sibling))
  4835. (point)))
  4836. (end (save-excursion
  4837. (and (org-get-next-sibling)
  4838. (org-get-next-sibling)
  4839. (org-get-next-sibling))
  4840. (if (org-at-heading-p)
  4841. (point-at-eol)
  4842. (point))))
  4843. (level (looking-at "\\*+"))
  4844. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4845. (save-excursion
  4846. (save-restriction
  4847. (narrow-to-region beg end)
  4848. (when re
  4849. ;; Properly fold already folded siblings
  4850. (goto-char (point-min))
  4851. (while (re-search-forward re nil t)
  4852. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4853. (hide-entry))))
  4854. (org-cycle-show-empty-lines 'overview)
  4855. (org-cycle-hide-drawers 'overview)))))
  4856. (defun org-cycle-show-empty-lines (state)
  4857. "Show empty lines above all visible headlines.
  4858. The region to be covered depends on STATE when called through
  4859. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4860. entire buffer covered. Note that an empty line is only shown if there
  4861. are at least `org-cycle-separator-lines' empty lines before the headline."
  4862. (when (not (= org-cycle-separator-lines 0))
  4863. (save-excursion
  4864. (let* ((n (abs org-cycle-separator-lines))
  4865. (re (cond
  4866. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4867. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4868. (t (let ((ns (number-to-string (- n 2))))
  4869. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4870. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4871. beg end b e)
  4872. (cond
  4873. ((memq state '(overview contents t))
  4874. (setq beg (point-min) end (point-max)))
  4875. ((memq state '(children folded))
  4876. (setq beg (point) end (progn (org-end-of-subtree t t)
  4877. (beginning-of-line 2)
  4878. (point)))))
  4879. (when beg
  4880. (goto-char beg)
  4881. (while (re-search-forward re end t)
  4882. (unless (get-char-property (match-end 1) 'invisible)
  4883. (setq e (match-end 1))
  4884. (if (< org-cycle-separator-lines 0)
  4885. (setq b (save-excursion
  4886. (goto-char (match-beginning 0))
  4887. (org-back-over-empty-lines)
  4888. (if (save-excursion
  4889. (goto-char (max (point-min) (1- (point))))
  4890. (org-on-heading-p))
  4891. (1- (point))
  4892. (point))))
  4893. (setq b (match-beginning 1)))
  4894. (outline-flag-region b e nil)))))))
  4895. ;; Never hide empty lines at the end of the file.
  4896. (save-excursion
  4897. (goto-char (point-max))
  4898. (outline-previous-heading)
  4899. (outline-end-of-heading)
  4900. (if (and (looking-at "[ \t\n]+")
  4901. (= (match-end 0) (point-max)))
  4902. (outline-flag-region (point) (match-end 0) nil))))
  4903. (defun org-show-empty-lines-in-parent ()
  4904. "Move to the parent and re-show empty lines before visible headlines."
  4905. (save-excursion
  4906. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4907. (org-cycle-show-empty-lines context))))
  4908. (defun org-files-list ()
  4909. "Return `org-agenda-files' list, plus all open org-mode files.
  4910. This is useful for operations that need to scan all of a user's
  4911. open and agenda-wise Org files."
  4912. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4913. (dolist (buf (buffer-list))
  4914. (with-current-buffer buf
  4915. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4916. (let ((file (expand-file-name (buffer-file-name))))
  4917. (unless (member file files)
  4918. (push file files))))))
  4919. files))
  4920. (defsubst org-entry-beginning-position ()
  4921. "Return the beginning position of the current entry."
  4922. (save-excursion (outline-back-to-heading t) (point)))
  4923. (defsubst org-entry-end-position ()
  4924. "Return the end position of the current entry."
  4925. (save-excursion (outline-next-heading) (point)))
  4926. (defun org-cycle-hide-drawers (state)
  4927. "Re-hide all drawers after a visibility state change."
  4928. (when (and (org-mode-p)
  4929. (not (memq state '(overview folded contents))))
  4930. (save-excursion
  4931. (let* ((globalp (memq state '(contents all)))
  4932. (beg (if globalp (point-min) (point)))
  4933. (end (if globalp (point-max)
  4934. (if (eq state 'children)
  4935. (save-excursion (outline-next-heading) (point))
  4936. (org-end-of-subtree t)))))
  4937. (goto-char beg)
  4938. (while (re-search-forward org-drawer-regexp end t)
  4939. (org-flag-drawer t))))))
  4940. (defun org-flag-drawer (flag)
  4941. (save-excursion
  4942. (beginning-of-line 1)
  4943. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4944. (let ((b (match-end 0))
  4945. (outline-regexp org-outline-regexp))
  4946. (if (re-search-forward
  4947. "^[ \t]*:END:"
  4948. (save-excursion (outline-next-heading) (point)) t)
  4949. (outline-flag-region b (point-at-eol) flag)
  4950. (error ":END: line missing at position %s" b))))))
  4951. (defun org-subtree-end-visible-p ()
  4952. "Is the end of the current subtree visible?"
  4953. (pos-visible-in-window-p
  4954. (save-excursion (org-end-of-subtree t) (point))))
  4955. (defun org-first-headline-recenter (&optional N)
  4956. "Move cursor to the first headline and recenter the headline.
  4957. Optional argument N means, put the headline into the Nth line of the window."
  4958. (goto-char (point-min))
  4959. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4960. (beginning-of-line)
  4961. (recenter (prefix-numeric-value N))))
  4962. ;;; Folding of blocks
  4963. (defconst org-block-regexp
  4964. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4965. "Regular expression for hiding blocks.")
  4966. (defvar org-hide-block-overlays nil
  4967. "Overlays hiding blocks.")
  4968. (make-variable-buffer-local 'org-hide-block-overlays)
  4969. (defun org-block-map (function &optional start end)
  4970. "Call func at the head of all source blocks in the current
  4971. buffer. Optional arguments START and END can be used to limit
  4972. the range."
  4973. (let ((start (or start (point-min)))
  4974. (end (or end (point-max))))
  4975. (save-excursion
  4976. (goto-char start)
  4977. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4978. (save-excursion
  4979. (save-match-data
  4980. (goto-char (match-beginning 0))
  4981. (funcall function)))))))
  4982. (defun org-hide-block-toggle-all ()
  4983. "Toggle the visibility of all blocks in the current buffer."
  4984. (org-block-map #'org-hide-block-toggle))
  4985. (defun org-hide-block-all ()
  4986. "Fold all blocks in the current buffer."
  4987. (interactive)
  4988. (org-show-block-all)
  4989. (org-block-map #'org-hide-block-toggle-maybe))
  4990. (defun org-show-block-all ()
  4991. "Unfold all blocks in the current buffer."
  4992. (mapc 'org-delete-overlay org-hide-block-overlays)
  4993. (setq org-hide-block-overlays nil))
  4994. (defun org-hide-block-toggle-maybe ()
  4995. "Toggle visibility of block at point."
  4996. (interactive)
  4997. (let ((case-fold-search t))
  4998. (if (save-excursion
  4999. (beginning-of-line 1)
  5000. (looking-at org-block-regexp))
  5001. (progn (org-hide-block-toggle)
  5002. t) ;; to signal that we took action
  5003. nil))) ;; to signal that we did not
  5004. (defun org-hide-block-toggle (&optional force)
  5005. "Toggle the visibility of the current block."
  5006. (interactive)
  5007. (save-excursion
  5008. (beginning-of-line)
  5009. (if (re-search-forward org-block-regexp nil t)
  5010. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5011. (end (match-end 0)) ;; end of entire body
  5012. ov)
  5013. (if (memq t (mapcar (lambda (overlay)
  5014. (eq (org-overlay-get overlay 'invisible)
  5015. 'org-hide-block))
  5016. (org-overlays-at start)))
  5017. (if (or (not force) (eq force 'off))
  5018. (mapc (lambda (ov)
  5019. (when (member ov org-hide-block-overlays)
  5020. (setq org-hide-block-overlays
  5021. (delq ov org-hide-block-overlays)))
  5022. (when (eq (org-overlay-get ov 'invisible)
  5023. 'org-hide-block)
  5024. (org-delete-overlay ov)))
  5025. (org-overlays-at start)))
  5026. (setq ov (org-make-overlay start end))
  5027. (org-overlay-put ov 'invisible 'org-hide-block)
  5028. ;; make the block accessible to isearch
  5029. (org-overlay-put
  5030. ov 'isearch-open-invisible
  5031. (lambda (ov)
  5032. (when (member ov org-hide-block-overlays)
  5033. (setq org-hide-block-overlays
  5034. (delq ov org-hide-block-overlays)))
  5035. (when (eq (org-overlay-get ov 'invisible)
  5036. 'org-hide-block)
  5037. (org-delete-overlay ov))))
  5038. (push ov org-hide-block-overlays)))
  5039. (error "Not looking at a source block"))))
  5040. ;; org-tab-after-check-for-cycling-hook
  5041. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5042. ;; Remove overlays when changing major mode
  5043. (add-hook 'org-mode-hook
  5044. (lambda () (org-add-hook 'change-major-mode-hook
  5045. 'org-show-block-all 'append 'local)))
  5046. ;;; Org-goto
  5047. (defvar org-goto-window-configuration nil)
  5048. (defvar org-goto-marker nil)
  5049. (defvar org-goto-map
  5050. (let ((map (make-sparse-keymap)))
  5051. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5052. (while (setq cmd (pop cmds))
  5053. (substitute-key-definition cmd cmd map global-map)))
  5054. (suppress-keymap map)
  5055. (org-defkey map "\C-m" 'org-goto-ret)
  5056. (org-defkey map [(return)] 'org-goto-ret)
  5057. (org-defkey map [(left)] 'org-goto-left)
  5058. (org-defkey map [(right)] 'org-goto-right)
  5059. (org-defkey map [(control ?g)] 'org-goto-quit)
  5060. (org-defkey map "\C-i" 'org-cycle)
  5061. (org-defkey map [(tab)] 'org-cycle)
  5062. (org-defkey map [(down)] 'outline-next-visible-heading)
  5063. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5064. (if org-goto-auto-isearch
  5065. (if (fboundp 'define-key-after)
  5066. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5067. nil)
  5068. (org-defkey map "q" 'org-goto-quit)
  5069. (org-defkey map "n" 'outline-next-visible-heading)
  5070. (org-defkey map "p" 'outline-previous-visible-heading)
  5071. (org-defkey map "f" 'outline-forward-same-level)
  5072. (org-defkey map "b" 'outline-backward-same-level)
  5073. (org-defkey map "u" 'outline-up-heading))
  5074. (org-defkey map "/" 'org-occur)
  5075. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5076. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5077. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5078. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5079. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5080. map))
  5081. (defconst org-goto-help
  5082. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5083. RET=jump to location [Q]uit and return to previous location
  5084. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5085. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5086. ;; FIXME: Docstring does not mention both interfaces
  5087. (defun org-goto (&optional alternative-interface)
  5088. "Look up a different location in the current file, keeping current visibility.
  5089. When you want look-up or go to a different location in a document, the
  5090. fastest way is often to fold the entire buffer and then dive into the tree.
  5091. This method has the disadvantage, that the previous location will be folded,
  5092. which may not be what you want.
  5093. This command works around this by showing a copy of the current buffer
  5094. in an indirect buffer, in overview mode. You can dive into the tree in
  5095. that copy, use org-occur and incremental search to find a location.
  5096. When pressing RET or `Q', the command returns to the original buffer in
  5097. which the visibility is still unchanged. After RET is will also jump to
  5098. the location selected in the indirect buffer and expose the
  5099. the headline hierarchy above."
  5100. (interactive "P")
  5101. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5102. (org-refile-use-outline-path t)
  5103. (org-refile-target-verify-function nil)
  5104. (interface
  5105. (if (not alternative-interface)
  5106. org-goto-interface
  5107. (if (eq org-goto-interface 'outline)
  5108. 'outline-path-completion
  5109. 'outline)))
  5110. (org-goto-start-pos (point))
  5111. (selected-point
  5112. (if (eq interface 'outline)
  5113. (car (org-get-location (current-buffer) org-goto-help))
  5114. (nth 3 (org-refile-get-location "Goto: ")))))
  5115. (if selected-point
  5116. (progn
  5117. (org-mark-ring-push org-goto-start-pos)
  5118. (goto-char selected-point)
  5119. (if (or (org-invisible-p) (org-invisible-p2))
  5120. (org-show-context 'org-goto)))
  5121. (message "Quit"))))
  5122. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5123. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5124. (defvar org-goto-local-auto-isearch-map) ; defined below
  5125. (defun org-get-location (buf help)
  5126. "Let the user select a location in the Org-mode buffer BUF.
  5127. This function uses a recursive edit. It returns the selected position
  5128. or nil."
  5129. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5130. (isearch-hide-immediately nil)
  5131. (isearch-search-fun-function
  5132. (lambda () 'org-goto-local-search-headings))
  5133. (org-goto-selected-point org-goto-exit-command))
  5134. (save-excursion
  5135. (save-window-excursion
  5136. (delete-other-windows)
  5137. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5138. (switch-to-buffer
  5139. (condition-case nil
  5140. (make-indirect-buffer (current-buffer) "*org-goto*")
  5141. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5142. (with-output-to-temp-buffer "*Help*"
  5143. (princ help))
  5144. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5145. (setq buffer-read-only nil)
  5146. (let ((org-startup-truncated t)
  5147. (org-startup-folded nil)
  5148. (org-startup-align-all-tables nil))
  5149. (org-mode)
  5150. (org-overview))
  5151. (setq buffer-read-only t)
  5152. (if (and (boundp 'org-goto-start-pos)
  5153. (integer-or-marker-p org-goto-start-pos))
  5154. (let ((org-show-hierarchy-above t)
  5155. (org-show-siblings t)
  5156. (org-show-following-heading t))
  5157. (goto-char org-goto-start-pos)
  5158. (and (org-invisible-p) (org-show-context)))
  5159. (goto-char (point-min)))
  5160. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5161. (message "Select location and press RET")
  5162. (use-local-map org-goto-map)
  5163. (recursive-edit)
  5164. ))
  5165. (kill-buffer "*org-goto*")
  5166. (cons org-goto-selected-point org-goto-exit-command)))
  5167. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5168. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5169. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5170. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5171. (defun org-goto-local-search-headings (string bound noerror)
  5172. "Search and make sure that any matches are in headlines."
  5173. (catch 'return
  5174. (while (if isearch-forward
  5175. (search-forward string bound noerror)
  5176. (search-backward string bound noerror))
  5177. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5178. (and (member :headline context)
  5179. (not (member :tags context))))
  5180. (throw 'return (point))))))
  5181. (defun org-goto-local-auto-isearch ()
  5182. "Start isearch."
  5183. (interactive)
  5184. (goto-char (point-min))
  5185. (let ((keys (this-command-keys)))
  5186. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5187. (isearch-mode t)
  5188. (isearch-process-search-char (string-to-char keys)))))
  5189. (defun org-goto-ret (&optional arg)
  5190. "Finish `org-goto' by going to the new location."
  5191. (interactive "P")
  5192. (setq org-goto-selected-point (point)
  5193. org-goto-exit-command 'return)
  5194. (throw 'exit nil))
  5195. (defun org-goto-left ()
  5196. "Finish `org-goto' by going to the new location."
  5197. (interactive)
  5198. (if (org-on-heading-p)
  5199. (progn
  5200. (beginning-of-line 1)
  5201. (setq org-goto-selected-point (point)
  5202. org-goto-exit-command 'left)
  5203. (throw 'exit nil))
  5204. (error "Not on a heading")))
  5205. (defun org-goto-right ()
  5206. "Finish `org-goto' by going to the new location."
  5207. (interactive)
  5208. (if (org-on-heading-p)
  5209. (progn
  5210. (setq org-goto-selected-point (point)
  5211. org-goto-exit-command 'right)
  5212. (throw 'exit nil))
  5213. (error "Not on a heading")))
  5214. (defun org-goto-quit ()
  5215. "Finish `org-goto' without cursor motion."
  5216. (interactive)
  5217. (setq org-goto-selected-point nil)
  5218. (setq org-goto-exit-command 'quit)
  5219. (throw 'exit nil))
  5220. ;;; Indirect buffer display of subtrees
  5221. (defvar org-indirect-dedicated-frame nil
  5222. "This is the frame being used for indirect tree display.")
  5223. (defvar org-last-indirect-buffer nil)
  5224. (defun org-tree-to-indirect-buffer (&optional arg)
  5225. "Create indirect buffer and narrow it to current subtree.
  5226. With numerical prefix ARG, go up to this level and then take that tree.
  5227. If ARG is negative, go up that many levels.
  5228. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5229. indirect buffer previously made with this command, to avoid proliferation of
  5230. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5231. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5232. is kept so that you can work with several indirect buffers at the same time.
  5233. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5234. requests that a new frame be made for the new buffer, so that the dedicated
  5235. frame is not changed."
  5236. (interactive "P")
  5237. (let ((cbuf (current-buffer))
  5238. (cwin (selected-window))
  5239. (pos (point))
  5240. beg end level heading ibuf)
  5241. (save-excursion
  5242. (org-back-to-heading t)
  5243. (when (numberp arg)
  5244. (setq level (org-outline-level))
  5245. (if (< arg 0) (setq arg (+ level arg)))
  5246. (while (> (setq level (org-outline-level)) arg)
  5247. (outline-up-heading 1 t)))
  5248. (setq beg (point)
  5249. heading (org-get-heading))
  5250. (org-end-of-subtree t t) (setq end (point)))
  5251. (if (and (buffer-live-p org-last-indirect-buffer)
  5252. (not (eq org-indirect-buffer-display 'new-frame))
  5253. (not arg))
  5254. (kill-buffer org-last-indirect-buffer))
  5255. (setq ibuf (org-get-indirect-buffer cbuf)
  5256. org-last-indirect-buffer ibuf)
  5257. (cond
  5258. ((or (eq org-indirect-buffer-display 'new-frame)
  5259. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5260. (select-frame (make-frame))
  5261. (delete-other-windows)
  5262. (switch-to-buffer ibuf)
  5263. (org-set-frame-title heading))
  5264. ((eq org-indirect-buffer-display 'dedicated-frame)
  5265. (raise-frame
  5266. (select-frame (or (and org-indirect-dedicated-frame
  5267. (frame-live-p org-indirect-dedicated-frame)
  5268. org-indirect-dedicated-frame)
  5269. (setq org-indirect-dedicated-frame (make-frame)))))
  5270. (delete-other-windows)
  5271. (switch-to-buffer ibuf)
  5272. (org-set-frame-title (concat "Indirect: " heading)))
  5273. ((eq org-indirect-buffer-display 'current-window)
  5274. (switch-to-buffer ibuf))
  5275. ((eq org-indirect-buffer-display 'other-window)
  5276. (pop-to-buffer ibuf))
  5277. (t (error "Invalid value")))
  5278. (if (featurep 'xemacs)
  5279. (save-excursion (org-mode) (turn-on-font-lock)))
  5280. (narrow-to-region beg end)
  5281. (show-all)
  5282. (goto-char pos)
  5283. (and (window-live-p cwin) (select-window cwin))))
  5284. (defun org-get-indirect-buffer (&optional buffer)
  5285. (setq buffer (or buffer (current-buffer)))
  5286. (let ((n 1) (base (buffer-name buffer)) bname)
  5287. (while (buffer-live-p
  5288. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5289. (setq n (1+ n)))
  5290. (condition-case nil
  5291. (make-indirect-buffer buffer bname 'clone)
  5292. (error (make-indirect-buffer buffer bname)))))
  5293. (defun org-set-frame-title (title)
  5294. "Set the title of the current frame to the string TITLE."
  5295. ;; FIXME: how to name a single frame in XEmacs???
  5296. (unless (featurep 'xemacs)
  5297. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5298. ;;;; Structure editing
  5299. ;;; Inserting headlines
  5300. (defun org-previous-line-empty-p ()
  5301. (save-excursion
  5302. (and (not (bobp))
  5303. (or (beginning-of-line 0) t)
  5304. (save-match-data
  5305. (looking-at "[ \t]*$")))))
  5306. (defun org-insert-heading (&optional force-heading)
  5307. "Insert a new heading or item with same depth at point.
  5308. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5309. If point is at the beginning of a headline, insert a sibling before the
  5310. current headline. If point is not at the beginning, do not split the line,
  5311. but create the new headline after the current line."
  5312. (interactive "P")
  5313. (if (= (buffer-size) 0)
  5314. (insert "\n* ")
  5315. (when (or force-heading (not (org-insert-item)))
  5316. (let* ((empty-line-p nil)
  5317. (head (save-excursion
  5318. (condition-case nil
  5319. (progn
  5320. (org-back-to-heading)
  5321. (setq empty-line-p (org-previous-line-empty-p))
  5322. (match-string 0))
  5323. (error "*"))))
  5324. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5325. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5326. pos hide-previous previous-pos)
  5327. (cond
  5328. ((and (org-on-heading-p) (bolp)
  5329. (or (bobp)
  5330. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5331. ;; insert before the current line
  5332. (open-line (if blank 2 1)))
  5333. ((and (bolp)
  5334. (not org-insert-heading-respect-content)
  5335. (or (bobp)
  5336. (save-excursion
  5337. (backward-char 1) (not (org-invisible-p)))))
  5338. ;; insert right here
  5339. nil)
  5340. (t
  5341. ;; somewhere in the line
  5342. (save-excursion
  5343. (setq previous-pos (point-at-bol))
  5344. (end-of-line)
  5345. (setq hide-previous (org-invisible-p)))
  5346. (and org-insert-heading-respect-content (org-show-subtree))
  5347. (let ((split
  5348. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5349. (save-excursion
  5350. (let ((p (point)))
  5351. (goto-char (point-at-bol))
  5352. (and (looking-at org-complex-heading-regexp)
  5353. (> p (match-beginning 4)))))))
  5354. tags pos)
  5355. (cond
  5356. (org-insert-heading-respect-content
  5357. (org-end-of-subtree nil t)
  5358. (or (bolp) (newline))
  5359. (or (org-previous-line-empty-p)
  5360. (and blank (newline)))
  5361. (open-line 1))
  5362. ((org-on-heading-p)
  5363. (when hide-previous
  5364. (show-children)
  5365. (org-show-entry))
  5366. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5367. (setq tags (and (match-end 2) (match-string 2)))
  5368. (and (match-end 1)
  5369. (delete-region (match-beginning 1) (match-end 1)))
  5370. (setq pos (point-at-bol))
  5371. (or split (end-of-line 1))
  5372. (delete-horizontal-space)
  5373. (newline (if blank 2 1))
  5374. (when tags
  5375. (save-excursion
  5376. (goto-char pos)
  5377. (end-of-line 1)
  5378. (insert " " tags)
  5379. (org-set-tags nil 'align))))
  5380. (t
  5381. (or split (end-of-line 1))
  5382. (newline (if blank 2 1)))))))
  5383. (insert head) (just-one-space)
  5384. (setq pos (point))
  5385. (end-of-line 1)
  5386. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5387. (when (and org-insert-heading-respect-content hide-previous)
  5388. (save-excursion
  5389. (goto-char previous-pos)
  5390. (hide-subtree)))
  5391. (run-hooks 'org-insert-heading-hook)))))
  5392. (defun org-get-heading (&optional no-tags)
  5393. "Return the heading of the current entry, without the stars."
  5394. (save-excursion
  5395. (org-back-to-heading t)
  5396. (if (looking-at
  5397. (if no-tags
  5398. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5399. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5400. (match-string 1) "")))
  5401. (defun org-heading-components ()
  5402. "Return the components of the current heading.
  5403. This is a list with the following elements:
  5404. - the level as an integer
  5405. - the reduced level, different if `org-odd-levels-only' is set.
  5406. - the TODO keyword, or nil
  5407. - the priority character, like ?A, or nil if no priority is given
  5408. - the headline text itself, or the tags string if no headline text
  5409. - the tags string, or nil."
  5410. (save-excursion
  5411. (org-back-to-heading t)
  5412. (if (looking-at org-complex-heading-regexp)
  5413. (list (length (match-string 1))
  5414. (org-reduced-level (length (match-string 1)))
  5415. (org-match-string-no-properties 2)
  5416. (and (match-end 3) (aref (match-string 3) 2))
  5417. (org-match-string-no-properties 4)
  5418. (org-match-string-no-properties 5)))))
  5419. (defun org-get-entry ()
  5420. "Get the entry text, after heading, entire subtree."
  5421. (save-excursion
  5422. (org-back-to-heading t)
  5423. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5424. (defun org-insert-heading-after-current ()
  5425. "Insert a new heading with same level as current, after current subtree."
  5426. (interactive)
  5427. (org-back-to-heading)
  5428. (org-insert-heading)
  5429. (org-move-subtree-down)
  5430. (end-of-line 1))
  5431. (defun org-insert-heading-respect-content ()
  5432. (interactive)
  5433. (let ((org-insert-heading-respect-content t))
  5434. (org-insert-heading t)))
  5435. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5436. (interactive "P")
  5437. (let ((org-insert-heading-respect-content t))
  5438. (org-insert-todo-heading force-state t)))
  5439. (defun org-insert-todo-heading (arg &optional force-heading)
  5440. "Insert a new heading with the same level and TODO state as current heading.
  5441. If the heading has no TODO state, or if the state is DONE, use the first
  5442. state (TODO by default). Also with prefix arg, force first state."
  5443. (interactive "P")
  5444. (when (or force-heading (not (org-insert-item 'checkbox)))
  5445. (org-insert-heading force-heading)
  5446. (save-excursion
  5447. (org-back-to-heading)
  5448. (outline-previous-heading)
  5449. (looking-at org-todo-line-regexp))
  5450. (let*
  5451. ((new-mark-x
  5452. (if (or arg
  5453. (not (match-beginning 2))
  5454. (member (match-string 2) org-done-keywords))
  5455. (car org-todo-keywords-1)
  5456. (match-string 2)))
  5457. (new-mark
  5458. (or
  5459. (run-hook-with-args-until-success
  5460. 'org-todo-get-default-hook new-mark-x nil)
  5461. new-mark-x)))
  5462. (beginning-of-line 1)
  5463. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5464. (if org-treat-insert-todo-heading-as-state-change
  5465. (org-todo new-mark)
  5466. (insert new-mark " "))))
  5467. (when org-provide-todo-statistics
  5468. (org-update-parent-todo-statistics))))
  5469. (defun org-insert-subheading (arg)
  5470. "Insert a new subheading and demote it.
  5471. Works for outline headings and for plain lists alike."
  5472. (interactive "P")
  5473. (org-insert-heading arg)
  5474. (cond
  5475. ((org-on-heading-p) (org-do-demote))
  5476. ((org-at-item-p) (org-indent-item 1))))
  5477. (defun org-insert-todo-subheading (arg)
  5478. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5479. Works for outline headings and for plain lists alike."
  5480. (interactive "P")
  5481. (org-insert-todo-heading arg)
  5482. (cond
  5483. ((org-on-heading-p) (org-do-demote))
  5484. ((org-at-item-p) (org-indent-item 1))))
  5485. ;;; Promotion and Demotion
  5486. (defvar org-after-demote-entry-hook nil
  5487. "Hook run after an entry has been demoted.
  5488. The cursor will be at the beginning of the entry.
  5489. When a subtree is being demoted, the hook will be called for each node.")
  5490. (defvar org-after-promote-entry-hook nil
  5491. "Hook run after an entry has been promoted.
  5492. The cursor will be at the beginning of the entry.
  5493. When a subtree is being promoted, the hook will be called for each node.")
  5494. (defun org-promote-subtree ()
  5495. "Promote the entire subtree.
  5496. See also `org-promote'."
  5497. (interactive)
  5498. (save-excursion
  5499. (org-map-tree 'org-promote))
  5500. (org-fix-position-after-promote))
  5501. (defun org-demote-subtree ()
  5502. "Demote the entire subtree. See `org-demote'.
  5503. See also `org-promote'."
  5504. (interactive)
  5505. (save-excursion
  5506. (org-map-tree 'org-demote))
  5507. (org-fix-position-after-promote))
  5508. (defun org-do-promote ()
  5509. "Promote the current heading higher up the tree.
  5510. If the region is active in `transient-mark-mode', promote all headings
  5511. in the region."
  5512. (interactive)
  5513. (save-excursion
  5514. (if (org-region-active-p)
  5515. (org-map-region 'org-promote (region-beginning) (region-end))
  5516. (org-promote)))
  5517. (org-fix-position-after-promote))
  5518. (defun org-do-demote ()
  5519. "Demote the current heading lower down the tree.
  5520. If the region is active in `transient-mark-mode', demote all headings
  5521. in the region."
  5522. (interactive)
  5523. (save-excursion
  5524. (if (org-region-active-p)
  5525. (org-map-region 'org-demote (region-beginning) (region-end))
  5526. (org-demote)))
  5527. (org-fix-position-after-promote))
  5528. (defun org-fix-position-after-promote ()
  5529. "Make sure that after pro/demotion cursor position is right."
  5530. (let ((pos (point)))
  5531. (when (save-excursion
  5532. (beginning-of-line 1)
  5533. (looking-at org-todo-line-regexp)
  5534. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5535. (cond ((eobp) (insert " "))
  5536. ((eolp) (insert " "))
  5537. ((equal (char-after) ?\ ) (forward-char 1))))))
  5538. (defun org-current-level ()
  5539. "Return the level of the current entry, or nil if before the first headline.
  5540. The level is the number of stars at the beginning of the headline."
  5541. (save-excursion
  5542. (condition-case nil
  5543. (progn
  5544. (org-back-to-heading t)
  5545. (funcall outline-level))
  5546. (error nil))))
  5547. (defun org-reduced-level (l)
  5548. "Compute the effective level of a heading.
  5549. This takes into account the setting of `org-odd-levels-only'."
  5550. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5551. (defun org-get-valid-level (level &optional change)
  5552. "Rectify a level change under the influence of `org-odd-levels-only'
  5553. LEVEL is a current level, CHANGE is by how much the level should be
  5554. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5555. even level numbers will become the next higher odd number."
  5556. (if org-odd-levels-only
  5557. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5558. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5559. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5560. (max 1 (+ level (or change 0)))))
  5561. (if (boundp 'define-obsolete-function-alias)
  5562. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5563. (define-obsolete-function-alias 'org-get-legal-level
  5564. 'org-get-valid-level)
  5565. (define-obsolete-function-alias 'org-get-legal-level
  5566. 'org-get-valid-level "23.1")))
  5567. (defun org-promote ()
  5568. "Promote the current heading higher up the tree.
  5569. If the region is active in `transient-mark-mode', promote all headings
  5570. in the region."
  5571. (org-back-to-heading t)
  5572. (let* ((level (save-match-data (funcall outline-level)))
  5573. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5574. (diff (abs (- level (length up-head) -1))))
  5575. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5576. (replace-match up-head nil t)
  5577. ;; Fixup tag positioning
  5578. (and org-auto-align-tags (org-set-tags nil t))
  5579. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5580. (run-hooks 'org-after-promote-entry-hook)))
  5581. (defun org-demote ()
  5582. "Demote the current heading lower down the tree.
  5583. If the region is active in `transient-mark-mode', demote all headings
  5584. in the region."
  5585. (org-back-to-heading t)
  5586. (let* ((level (save-match-data (funcall outline-level)))
  5587. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5588. (diff (abs (- level (length down-head) -1))))
  5589. (replace-match down-head nil t)
  5590. ;; Fixup tag positioning
  5591. (and org-auto-align-tags (org-set-tags nil t))
  5592. (if org-adapt-indentation (org-fixup-indentation diff))
  5593. (run-hooks 'org-after-demote-entry-hook)))
  5594. (defvar org-tab-ind-state nil)
  5595. (defun org-cycle-level ()
  5596. (let ((org-adapt-indentation nil))
  5597. (when (and (looking-at "[ \t]*$")
  5598. (looking-back
  5599. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5600. (setq this-command 'org-cycle-level)
  5601. (if (eq last-command 'org-cycle-level)
  5602. (condition-case nil
  5603. (progn (org-do-promote)
  5604. (if (equal org-tab-ind-state (org-current-level))
  5605. (org-do-promote)))
  5606. (error
  5607. (progn
  5608. (save-excursion
  5609. (beginning-of-line 1)
  5610. (and (looking-at "\\*+")
  5611. (replace-match
  5612. (make-string org-tab-ind-state ?*))))
  5613. (setq this-command 'org-cycle))))
  5614. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5615. (org-do-demote))
  5616. t)))
  5617. (defun org-map-tree (fun)
  5618. "Call FUN for every heading underneath the current one."
  5619. (org-back-to-heading)
  5620. (let ((level (funcall outline-level)))
  5621. (save-excursion
  5622. (funcall fun)
  5623. (while (and (progn
  5624. (outline-next-heading)
  5625. (> (funcall outline-level) level))
  5626. (not (eobp)))
  5627. (funcall fun)))))
  5628. (defun org-map-region (fun beg end)
  5629. "Call FUN for every heading between BEG and END."
  5630. (let ((org-ignore-region t))
  5631. (save-excursion
  5632. (setq end (copy-marker end))
  5633. (goto-char beg)
  5634. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5635. (< (point) end))
  5636. (funcall fun))
  5637. (while (and (progn
  5638. (outline-next-heading)
  5639. (< (point) end))
  5640. (not (eobp)))
  5641. (funcall fun)))))
  5642. (defun org-fixup-indentation (diff)
  5643. "Change the indentation in the current entry by DIFF
  5644. However, if any line in the current entry has no indentation, or if it
  5645. would end up with no indentation after the change, nothing at all is done."
  5646. (save-excursion
  5647. (let ((end (save-excursion (outline-next-heading)
  5648. (point-marker)))
  5649. (prohibit (if (> diff 0)
  5650. "^\\S-"
  5651. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5652. col)
  5653. (unless (save-excursion (end-of-line 1)
  5654. (re-search-forward prohibit end t))
  5655. (while (and (< (point) end)
  5656. (re-search-forward "^[ \t]+" end t))
  5657. (goto-char (match-end 0))
  5658. (setq col (current-column))
  5659. (if (< diff 0) (replace-match ""))
  5660. (org-indent-to-column (+ diff col))))
  5661. (move-marker end nil))))
  5662. (defun org-convert-to-odd-levels ()
  5663. "Convert an org-mode file with all levels allowed to one with odd levels.
  5664. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5665. level 5 etc."
  5666. (interactive)
  5667. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5668. (let ((outline-regexp org-outline-regexp)
  5669. (outline-level 'org-outline-level)
  5670. (org-odd-levels-only nil) n)
  5671. (save-excursion
  5672. (goto-char (point-min))
  5673. (while (re-search-forward "^\\*\\*+ " nil t)
  5674. (setq n (- (length (match-string 0)) 2))
  5675. (while (>= (setq n (1- n)) 0)
  5676. (org-demote))
  5677. (end-of-line 1))))))
  5678. (defun org-convert-to-oddeven-levels ()
  5679. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5680. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5681. section with an even level, conversion would destroy the structure of the file. An error
  5682. is signaled in this case."
  5683. (interactive)
  5684. (goto-char (point-min))
  5685. ;; First check if there are no even levels
  5686. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5687. (org-show-context t)
  5688. (error "Not all levels are odd in this file. Conversion not possible"))
  5689. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5690. (let ((outline-regexp org-outline-regexp)
  5691. (outline-level 'org-outline-level)
  5692. (org-odd-levels-only nil) n)
  5693. (save-excursion
  5694. (goto-char (point-min))
  5695. (while (re-search-forward "^\\*\\*+ " nil t)
  5696. (setq n (/ (1- (length (match-string 0))) 2))
  5697. (while (>= (setq n (1- n)) 0)
  5698. (org-promote))
  5699. (end-of-line 1))))))
  5700. (defun org-tr-level (n)
  5701. "Make N odd if required."
  5702. (if org-odd-levels-only (1+ (/ n 2)) n))
  5703. ;;; Vertical tree motion, cutting and pasting of subtrees
  5704. (defun org-move-subtree-up (&optional arg)
  5705. "Move the current subtree up past ARG headlines of the same level."
  5706. (interactive "p")
  5707. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5708. (defun org-move-subtree-down (&optional arg)
  5709. "Move the current subtree down past ARG headlines of the same level."
  5710. (interactive "p")
  5711. (setq arg (prefix-numeric-value arg))
  5712. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5713. 'org-get-last-sibling))
  5714. (ins-point (make-marker))
  5715. (cnt (abs arg))
  5716. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5717. ;; Select the tree
  5718. (org-back-to-heading)
  5719. (setq beg0 (point))
  5720. (save-excursion
  5721. (setq ne-beg (org-back-over-empty-lines))
  5722. (setq beg (point)))
  5723. (save-match-data
  5724. (save-excursion (outline-end-of-heading)
  5725. (setq folded (org-invisible-p)))
  5726. (outline-end-of-subtree))
  5727. (outline-next-heading)
  5728. (setq ne-end (org-back-over-empty-lines))
  5729. (setq end (point))
  5730. (goto-char beg0)
  5731. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5732. ;; include less whitespace
  5733. (save-excursion
  5734. (goto-char beg)
  5735. (forward-line (- ne-beg ne-end))
  5736. (setq beg (point))))
  5737. ;; Find insertion point, with error handling
  5738. (while (> cnt 0)
  5739. (or (and (funcall movfunc) (looking-at outline-regexp))
  5740. (progn (goto-char beg0)
  5741. (error "Cannot move past superior level or buffer limit")))
  5742. (setq cnt (1- cnt)))
  5743. (if (> arg 0)
  5744. ;; Moving forward - still need to move over subtree
  5745. (progn (org-end-of-subtree t t)
  5746. (save-excursion
  5747. (org-back-over-empty-lines)
  5748. (or (bolp) (newline)))))
  5749. (setq ne-ins (org-back-over-empty-lines))
  5750. (move-marker ins-point (point))
  5751. (setq txt (buffer-substring beg end))
  5752. (org-save-markers-in-region beg end)
  5753. (delete-region beg end)
  5754. (org-remove-empty-overlays-at beg)
  5755. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5756. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5757. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5758. (let ((bbb (point)))
  5759. (insert-before-markers txt)
  5760. (org-reinstall-markers-in-region bbb)
  5761. (move-marker ins-point bbb))
  5762. (or (bolp) (insert "\n"))
  5763. (setq ins-end (point))
  5764. (goto-char ins-point)
  5765. (org-skip-whitespace)
  5766. (when (and (< arg 0)
  5767. (org-first-sibling-p)
  5768. (> ne-ins ne-beg))
  5769. ;; Move whitespace back to beginning
  5770. (save-excursion
  5771. (goto-char ins-end)
  5772. (let ((kill-whole-line t))
  5773. (kill-line (- ne-ins ne-beg)) (point)))
  5774. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5775. (move-marker ins-point nil)
  5776. (if folded
  5777. (hide-subtree)
  5778. (org-show-entry)
  5779. (show-children)
  5780. (org-cycle-hide-drawers 'children))
  5781. (org-clean-visibility-after-subtree-move)))
  5782. (defvar org-subtree-clip ""
  5783. "Clipboard for cut and paste of subtrees.
  5784. This is actually only a copy of the kill, because we use the normal kill
  5785. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5786. (defvar org-subtree-clip-folded nil
  5787. "Was the last copied subtree folded?
  5788. This is used to fold the tree back after pasting.")
  5789. (defun org-cut-subtree (&optional n)
  5790. "Cut the current subtree into the clipboard.
  5791. With prefix arg N, cut this many sequential subtrees.
  5792. This is a short-hand for marking the subtree and then cutting it."
  5793. (interactive "p")
  5794. (org-copy-subtree n 'cut))
  5795. (defun org-copy-subtree (&optional n cut force-store-markers)
  5796. "Cut the current subtree into the clipboard.
  5797. With prefix arg N, cut this many sequential subtrees.
  5798. This is a short-hand for marking the subtree and then copying it.
  5799. If CUT is non-nil, actually cut the subtree.
  5800. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5801. of some markers in the region, even if CUT is non-nil. This is
  5802. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5803. (interactive "p")
  5804. (let (beg end folded (beg0 (point)))
  5805. (if (interactive-p)
  5806. (org-back-to-heading nil) ; take what looks like a subtree
  5807. (org-back-to-heading t)) ; take what is really there
  5808. (org-back-over-empty-lines)
  5809. (setq beg (point))
  5810. (skip-chars-forward " \t\r\n")
  5811. (save-match-data
  5812. (save-excursion (outline-end-of-heading)
  5813. (setq folded (org-invisible-p)))
  5814. (condition-case nil
  5815. (org-forward-same-level (1- n) t)
  5816. (error nil))
  5817. (org-end-of-subtree t t))
  5818. (org-back-over-empty-lines)
  5819. (setq end (point))
  5820. (goto-char beg0)
  5821. (when (> end beg)
  5822. (setq org-subtree-clip-folded folded)
  5823. (when (or cut force-store-markers)
  5824. (org-save-markers-in-region beg end))
  5825. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5826. (setq org-subtree-clip (current-kill 0))
  5827. (message "%s: Subtree(s) with %d characters"
  5828. (if cut "Cut" "Copied")
  5829. (length org-subtree-clip)))))
  5830. (defun org-paste-subtree (&optional level tree for-yank)
  5831. "Paste the clipboard as a subtree, with modification of headline level.
  5832. The entire subtree is promoted or demoted in order to match a new headline
  5833. level.
  5834. If the cursor is at the beginning of a headline, the same level as
  5835. that headline is used to paste the tree
  5836. If not, the new level is derived from the *visible* headings
  5837. before and after the insertion point, and taken to be the inferior headline
  5838. level of the two. So if the previous visible heading is level 3 and the
  5839. next is level 4 (or vice versa), level 4 will be used for insertion.
  5840. This makes sure that the subtree remains an independent subtree and does
  5841. not swallow low level entries.
  5842. You can also force a different level, either by using a numeric prefix
  5843. argument, or by inserting the heading marker by hand. For example, if the
  5844. cursor is after \"*****\", then the tree will be shifted to level 5.
  5845. If optional TREE is given, use this text instead of the kill ring.
  5846. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5847. move back over whitespace before inserting, and move point to the end of
  5848. the inserted text when done."
  5849. (interactive "P")
  5850. (setq tree (or tree (and kill-ring (current-kill 0))))
  5851. (unless (org-kill-is-subtree-p tree)
  5852. (error "%s"
  5853. (substitute-command-keys
  5854. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5855. (let* ((visp (not (org-invisible-p)))
  5856. (txt tree)
  5857. (^re (concat "^\\(" outline-regexp "\\)"))
  5858. (re (concat "\\(" outline-regexp "\\)"))
  5859. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5860. (old-level (if (string-match ^re txt)
  5861. (- (match-end 0) (match-beginning 0) 1)
  5862. -1))
  5863. (force-level (cond (level (prefix-numeric-value level))
  5864. ((and (looking-at "[ \t]*$")
  5865. (string-match
  5866. ^re_ (buffer-substring
  5867. (point-at-bol) (point))))
  5868. (- (match-end 1) (match-beginning 1)))
  5869. ((and (bolp)
  5870. (looking-at org-outline-regexp))
  5871. (- (match-end 0) (point) 1))
  5872. (t nil)))
  5873. (previous-level (save-excursion
  5874. (condition-case nil
  5875. (progn
  5876. (outline-previous-visible-heading 1)
  5877. (if (looking-at re)
  5878. (- (match-end 0) (match-beginning 0) 1)
  5879. 1))
  5880. (error 1))))
  5881. (next-level (save-excursion
  5882. (condition-case nil
  5883. (progn
  5884. (or (looking-at outline-regexp)
  5885. (outline-next-visible-heading 1))
  5886. (if (looking-at re)
  5887. (- (match-end 0) (match-beginning 0) 1)
  5888. 1))
  5889. (error 1))))
  5890. (new-level (or force-level (max previous-level next-level)))
  5891. (shift (if (or (= old-level -1)
  5892. (= new-level -1)
  5893. (= old-level new-level))
  5894. 0
  5895. (- new-level old-level)))
  5896. (delta (if (> shift 0) -1 1))
  5897. (func (if (> shift 0) 'org-demote 'org-promote))
  5898. (org-odd-levels-only nil)
  5899. beg end newend)
  5900. ;; Remove the forced level indicator
  5901. (if force-level
  5902. (delete-region (point-at-bol) (point)))
  5903. ;; Paste
  5904. (beginning-of-line 1)
  5905. (unless for-yank (org-back-over-empty-lines))
  5906. (setq beg (point))
  5907. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5908. (insert-before-markers txt)
  5909. (unless (string-match "\n\\'" txt) (insert "\n"))
  5910. (setq newend (point))
  5911. (org-reinstall-markers-in-region beg)
  5912. (setq end (point))
  5913. (goto-char beg)
  5914. (skip-chars-forward " \t\n\r")
  5915. (setq beg (point))
  5916. (if (and (org-invisible-p) visp)
  5917. (save-excursion (outline-show-heading)))
  5918. ;; Shift if necessary
  5919. (unless (= shift 0)
  5920. (save-restriction
  5921. (narrow-to-region beg end)
  5922. (while (not (= shift 0))
  5923. (org-map-region func (point-min) (point-max))
  5924. (setq shift (+ delta shift)))
  5925. (goto-char (point-min))
  5926. (setq newend (point-max))))
  5927. (when (or (interactive-p) for-yank)
  5928. (message "Clipboard pasted as level %d subtree" new-level))
  5929. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5930. kill-ring
  5931. (eq org-subtree-clip (current-kill 0))
  5932. org-subtree-clip-folded)
  5933. ;; The tree was folded before it was killed/copied
  5934. (hide-subtree))
  5935. (and for-yank (goto-char newend))))
  5936. (defun org-kill-is-subtree-p (&optional txt)
  5937. "Check if the current kill is an outline subtree, or a set of trees.
  5938. Returns nil if kill does not start with a headline, or if the first
  5939. headline level is not the largest headline level in the tree.
  5940. So this will actually accept several entries of equal levels as well,
  5941. which is OK for `org-paste-subtree'.
  5942. If optional TXT is given, check this string instead of the current kill."
  5943. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5944. (start-level (and kill
  5945. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5946. org-outline-regexp "\\)")
  5947. kill)
  5948. (- (match-end 2) (match-beginning 2) 1)))
  5949. (re (concat "^" org-outline-regexp))
  5950. (start (1+ (or (match-beginning 2) -1))))
  5951. (if (not start-level)
  5952. (progn
  5953. nil) ;; does not even start with a heading
  5954. (catch 'exit
  5955. (while (setq start (string-match re kill (1+ start)))
  5956. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5957. (throw 'exit nil)))
  5958. t))))
  5959. (defvar org-markers-to-move nil
  5960. "Markers that should be moved with a cut-and-paste operation.
  5961. Those markers are stored together with their positions relative to
  5962. the start of the region.")
  5963. (defun org-save-markers-in-region (beg end)
  5964. "Check markers in region.
  5965. If these markers are between BEG and END, record their position relative
  5966. to BEG, so that after moving the block of text, we can put the markers back
  5967. into place.
  5968. This function gets called just before an entry or tree gets cut from the
  5969. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5970. called immediately, to move the markers with the entries."
  5971. (setq org-markers-to-move nil)
  5972. (when (featurep 'org-clock)
  5973. (org-clock-save-markers-for-cut-and-paste beg end))
  5974. (when (featurep 'org-agenda)
  5975. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5976. (defun org-check-and-save-marker (marker beg end)
  5977. "Check if MARKER is between BEG and END.
  5978. If yes, remember the marker and the distance to BEG."
  5979. (when (and (marker-buffer marker)
  5980. (equal (marker-buffer marker) (current-buffer)))
  5981. (if (and (>= marker beg) (< marker end))
  5982. (push (cons marker (- marker beg)) org-markers-to-move))))
  5983. (defun org-reinstall-markers-in-region (beg)
  5984. "Move all remembered markers to their position relative to BEG."
  5985. (mapc (lambda (x)
  5986. (move-marker (car x) (+ beg (cdr x))))
  5987. org-markers-to-move)
  5988. (setq org-markers-to-move nil))
  5989. (defun org-narrow-to-subtree ()
  5990. "Narrow buffer to the current subtree."
  5991. (interactive)
  5992. (save-excursion
  5993. (save-match-data
  5994. (narrow-to-region
  5995. (progn (org-back-to-heading t) (point))
  5996. (progn (org-end-of-subtree t t) (point))))))
  5997. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5998. "Clone the task (subtree) at point N times.
  5999. The clones will be inserted as siblings.
  6000. In interactive use, the user will be prompted for the number of clones
  6001. to be produced, and for a time SHIFT, which may be a repeater as used
  6002. in time stamps, for example `+3d'.
  6003. When a valid repeater is given and the entry contains any time stamps,
  6004. the clones will become a sequence in time, with time stamps in the
  6005. subtree shifted for each clone produced. If SHIFT is nil or the
  6006. empty string, time stamps will be left alone.
  6007. If the original subtree did contain time stamps with a repeater,
  6008. the following will happen:
  6009. - the repeater will be removed in each clone
  6010. - an additional clone will be produced, with the current, unshifted
  6011. date(s) in the entry.
  6012. - the original entry will be placed *after* all the clones, with
  6013. repeater intact.
  6014. - the start days in the repeater in the original entry will be shifted
  6015. to past the last clone.
  6016. I this way you can spell out a number of instances of a repeating task,
  6017. and still retain the repeater to cover future instances of the task."
  6018. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6019. (let (beg end template task
  6020. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6021. (if (not (and (integerp n) (> n 0)))
  6022. (error "Invalid number of replications %s" n))
  6023. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6024. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6025. shift)))
  6026. (error "Invalid shift specification %s" shift))
  6027. (when doshift
  6028. (setq shift-n (string-to-number (match-string 1 shift))
  6029. shift-what (cdr (assoc (match-string 2 shift)
  6030. '(("d" . day) ("w" . week)
  6031. ("m" . month) ("y" . year))))))
  6032. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6033. (setq nmin 1 nmax n)
  6034. (org-back-to-heading t)
  6035. (setq beg (point))
  6036. (org-end-of-subtree t t)
  6037. (or (bolp) (insert "\n"))
  6038. (setq end (point))
  6039. (setq template (buffer-substring beg end))
  6040. (when (and doshift
  6041. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6042. (delete-region beg end)
  6043. (setq end beg)
  6044. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6045. (goto-char end)
  6046. (loop for n from nmin to nmax do
  6047. (if (not doshift)
  6048. (setq task template)
  6049. (with-temp-buffer
  6050. (insert template)
  6051. (org-mode)
  6052. (goto-char (point-min))
  6053. (while (re-search-forward org-ts-regexp-both nil t)
  6054. (org-timestamp-change (* n shift-n) shift-what))
  6055. (unless (= n n-no-remove)
  6056. (goto-char (point-min))
  6057. (while (re-search-forward org-ts-regexp nil t)
  6058. (save-excursion
  6059. (goto-char (match-beginning 0))
  6060. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6061. (delete-region (match-beginning 1) (match-end 1))))))
  6062. (setq task (buffer-string))))
  6063. (insert task))
  6064. (goto-char beg)))
  6065. ;;; Outline Sorting
  6066. (defun org-sort (with-case)
  6067. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6068. Optional argument WITH-CASE means sort case-sensitively.
  6069. With a double prefix argument, also remove duplicate entries."
  6070. (interactive "P")
  6071. (if (org-at-table-p)
  6072. (org-call-with-arg 'org-table-sort-lines with-case)
  6073. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6074. (defun org-sort-remove-invisible (s)
  6075. (remove-text-properties 0 (length s) org-rm-props s)
  6076. (while (string-match org-bracket-link-regexp s)
  6077. (setq s (replace-match (if (match-end 2)
  6078. (match-string 3 s)
  6079. (match-string 1 s)) t t s)))
  6080. s)
  6081. (defvar org-priority-regexp) ; defined later in the file
  6082. (defvar org-after-sorting-entries-or-items-hook nil
  6083. "Hook that is run after a bunch of entries or items have been sorted.
  6084. When children are sorted, the cursor is in the parent line when this
  6085. hook gets called. When a region or a plain list is sorted, the cursor
  6086. will be in the first entry of the sorted region/list.")
  6087. (defun org-sort-entries-or-items
  6088. (&optional with-case sorting-type getkey-func compare-func property)
  6089. "Sort entries on a certain level of an outline tree, or plain list items.
  6090. If there is an active region, the entries in the region are sorted.
  6091. Else, if the cursor is before the first entry, sort the top-level items.
  6092. Else, the children of the entry at point are sorted.
  6093. If the cursor is at the first item in a plain list, the list items will be
  6094. sorted.
  6095. Sorting can be alphabetically, numerically, by date/time as given by
  6096. a time stamp, by a property or by priority.
  6097. The command prompts for the sorting type unless it has been given to the
  6098. function through the SORTING-TYPE argument, which needs to a character,
  6099. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6100. precise meaning of each character:
  6101. n Numerically, by converting the beginning of the entry/item to a number.
  6102. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6103. t By date/time, either the first active time stamp in the entry, or, if
  6104. none exist, by the first inactive one.
  6105. In items, only the first line will be checked.
  6106. s By the scheduled date/time.
  6107. d By deadline date/time.
  6108. c By creation time, which is assumed to be the first inactive time stamp
  6109. at the beginning of a line.
  6110. p By priority according to the cookie.
  6111. r By the value of a property.
  6112. Capital letters will reverse the sort order.
  6113. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6114. called with point at the beginning of the record. It must return either
  6115. a string or a number that should serve as the sorting key for that record.
  6116. Comparing entries ignores case by default. However, with an optional argument
  6117. WITH-CASE, the sorting considers case as well."
  6118. (interactive "P")
  6119. (let ((case-func (if with-case 'identity 'downcase))
  6120. start beg end stars re re2
  6121. txt what tmp plain-list-p)
  6122. ;; Find beginning and end of region to sort
  6123. (cond
  6124. ((org-region-active-p)
  6125. ;; we will sort the region
  6126. (setq end (region-end)
  6127. what "region")
  6128. (goto-char (region-beginning))
  6129. (if (not (org-on-heading-p)) (outline-next-heading))
  6130. (setq start (point)))
  6131. ((org-at-item-p)
  6132. ;; we will sort this plain list
  6133. (org-beginning-of-item-list) (setq start (point))
  6134. (org-end-of-item-list) (setq end (point))
  6135. (goto-char start)
  6136. (setq plain-list-p t
  6137. what "plain list"))
  6138. ((or (org-on-heading-p)
  6139. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6140. ;; we will sort the children of the current headline
  6141. (org-back-to-heading)
  6142. (setq start (point)
  6143. end (progn (org-end-of-subtree t t)
  6144. (org-back-over-empty-lines)
  6145. (point))
  6146. what "children")
  6147. (goto-char start)
  6148. (show-subtree)
  6149. (outline-next-heading))
  6150. (t
  6151. ;; we will sort the top-level entries in this file
  6152. (goto-char (point-min))
  6153. (or (org-on-heading-p) (outline-next-heading))
  6154. (setq start (point) end (point-max) what "top-level")
  6155. (goto-char start)
  6156. (show-all)))
  6157. (setq beg (point))
  6158. (if (>= beg end) (error "Nothing to sort"))
  6159. (unless plain-list-p
  6160. (looking-at "\\(\\*+\\)")
  6161. (setq stars (match-string 1)
  6162. re (concat "^" (regexp-quote stars) " +")
  6163. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6164. txt (buffer-substring beg end))
  6165. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6166. (if (and (not (equal stars "*")) (string-match re2 txt))
  6167. (error "Region to sort contains a level above the first entry")))
  6168. (unless sorting-type
  6169. (message
  6170. (if plain-list-p
  6171. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6172. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6173. [t]ime [s]cheduled [d]eadline [c]reated
  6174. A/N/T/S/D/C/P/O/F means reversed:")
  6175. what)
  6176. (setq sorting-type (read-char-exclusive))
  6177. (and (= (downcase sorting-type) ?f)
  6178. (setq getkey-func
  6179. (org-icompleting-read "Sort using function: "
  6180. obarray 'fboundp t nil nil))
  6181. (setq getkey-func (intern getkey-func)))
  6182. (and (= (downcase sorting-type) ?r)
  6183. (setq property
  6184. (org-icompleting-read "Property: "
  6185. (mapcar 'list (org-buffer-property-keys t))
  6186. nil t))))
  6187. (message "Sorting entries...")
  6188. (save-restriction
  6189. (narrow-to-region start end)
  6190. (let ((dcst (downcase sorting-type))
  6191. (case-fold-search nil)
  6192. (now (current-time)))
  6193. (sort-subr
  6194. (/= dcst sorting-type)
  6195. ;; This function moves to the beginning character of the "record" to
  6196. ;; be sorted.
  6197. (if plain-list-p
  6198. (lambda nil
  6199. (if (org-at-item-p) t (goto-char (point-max))))
  6200. (lambda nil
  6201. (if (re-search-forward re nil t)
  6202. (goto-char (match-beginning 0))
  6203. (goto-char (point-max)))))
  6204. ;; This function moves to the last character of the "record" being
  6205. ;; sorted.
  6206. (if plain-list-p
  6207. 'org-end-of-item
  6208. (lambda nil
  6209. (save-match-data
  6210. (condition-case nil
  6211. (outline-forward-same-level 1)
  6212. (error
  6213. (goto-char (point-max)))))))
  6214. ;; This function returns the value that gets sorted against.
  6215. (if plain-list-p
  6216. (lambda nil
  6217. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6218. (cond
  6219. ((= dcst ?n)
  6220. (string-to-number (buffer-substring (match-end 0)
  6221. (point-at-eol))))
  6222. ((= dcst ?a)
  6223. (buffer-substring (match-end 0) (point-at-eol)))
  6224. ((= dcst ?t)
  6225. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6226. (re-search-forward org-ts-regexp-both
  6227. (point-at-eol) t))
  6228. (org-time-string-to-seconds (match-string 0))
  6229. (org-float-time now)))
  6230. ((= dcst ?f)
  6231. (if getkey-func
  6232. (progn
  6233. (setq tmp (funcall getkey-func))
  6234. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6235. tmp)
  6236. (error "Invalid key function `%s'" getkey-func)))
  6237. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6238. (lambda nil
  6239. (cond
  6240. ((= dcst ?n)
  6241. (if (looking-at org-complex-heading-regexp)
  6242. (string-to-number (match-string 4))
  6243. nil))
  6244. ((= dcst ?a)
  6245. (if (looking-at org-complex-heading-regexp)
  6246. (funcall case-func (match-string 4))
  6247. nil))
  6248. ((= dcst ?t)
  6249. (let ((end (save-excursion (outline-next-heading) (point))))
  6250. (if (or (re-search-forward org-ts-regexp end t)
  6251. (re-search-forward org-ts-regexp-both end t))
  6252. (org-time-string-to-seconds (match-string 0))
  6253. (org-float-time now))))
  6254. ((= dcst ?c)
  6255. (let ((end (save-excursion (outline-next-heading) (point))))
  6256. (if (re-search-forward
  6257. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6258. end t)
  6259. (org-time-string-to-seconds (match-string 0))
  6260. (org-float-time now))))
  6261. ((= dcst ?s)
  6262. (let ((end (save-excursion (outline-next-heading) (point))))
  6263. (if (re-search-forward org-scheduled-time-regexp end t)
  6264. (org-time-string-to-seconds (match-string 1))
  6265. (org-float-time now))))
  6266. ((= dcst ?d)
  6267. (let ((end (save-excursion (outline-next-heading) (point))))
  6268. (if (re-search-forward org-deadline-time-regexp end t)
  6269. (org-time-string-to-seconds (match-string 1))
  6270. (org-float-time now))))
  6271. ((= dcst ?p)
  6272. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6273. (string-to-char (match-string 2))
  6274. org-default-priority))
  6275. ((= dcst ?r)
  6276. (or (org-entry-get nil property) ""))
  6277. ((= dcst ?o)
  6278. (if (looking-at org-complex-heading-regexp)
  6279. (- 9999 (length (member (match-string 2)
  6280. org-todo-keywords-1)))))
  6281. ((= dcst ?f)
  6282. (if getkey-func
  6283. (progn
  6284. (setq tmp (funcall getkey-func))
  6285. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6286. tmp)
  6287. (error "Invalid key function `%s'" getkey-func)))
  6288. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6289. nil
  6290. (cond
  6291. ((= dcst ?a) 'string<)
  6292. ((= dcst ?f) compare-func)
  6293. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6294. (t nil)))))
  6295. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6296. (message "Sorting entries...done")))
  6297. (defun org-do-sort (table what &optional with-case sorting-type)
  6298. "Sort TABLE of WHAT according to SORTING-TYPE.
  6299. The user will be prompted for the SORTING-TYPE if the call to this
  6300. function does not specify it. WHAT is only for the prompt, to indicate
  6301. what is being sorted. The sorting key will be extracted from
  6302. the car of the elements of the table.
  6303. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6304. (unless sorting-type
  6305. (message
  6306. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6307. what)
  6308. (setq sorting-type (read-char-exclusive)))
  6309. (let ((dcst (downcase sorting-type))
  6310. extractfun comparefun)
  6311. ;; Define the appropriate functions
  6312. (cond
  6313. ((= dcst ?n)
  6314. (setq extractfun 'string-to-number
  6315. comparefun (if (= dcst sorting-type) '< '>)))
  6316. ((= dcst ?a)
  6317. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6318. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6319. comparefun (if (= dcst sorting-type)
  6320. 'string<
  6321. (lambda (a b) (and (not (string< a b))
  6322. (not (string= a b)))))))
  6323. ((= dcst ?t)
  6324. (setq extractfun
  6325. (lambda (x)
  6326. (if (or (string-match org-ts-regexp x)
  6327. (string-match org-ts-regexp-both x))
  6328. (org-float-time
  6329. (org-time-string-to-time (match-string 0 x)))
  6330. 0))
  6331. comparefun (if (= dcst sorting-type) '< '>)))
  6332. (t (error "Invalid sorting type `%c'" sorting-type)))
  6333. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6334. table)
  6335. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6336. ;;; The orgstruct minor mode
  6337. ;; Define a minor mode which can be used in other modes in order to
  6338. ;; integrate the org-mode structure editing commands.
  6339. ;; This is really a hack, because the org-mode structure commands use
  6340. ;; keys which normally belong to the major mode. Here is how it
  6341. ;; works: The minor mode defines all the keys necessary to operate the
  6342. ;; structure commands, but wraps the commands into a function which
  6343. ;; tests if the cursor is currently at a headline or a plain list
  6344. ;; item. If that is the case, the structure command is used,
  6345. ;; temporarily setting many Org-mode variables like regular
  6346. ;; expressions for filling etc. However, when any of those keys is
  6347. ;; used at a different location, function uses `key-binding' to look
  6348. ;; up if the key has an associated command in another currently active
  6349. ;; keymap (minor modes, major mode, global), and executes that
  6350. ;; command. There might be problems if any of the keys is otherwise
  6351. ;; used as a prefix key.
  6352. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6353. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6354. ;; addresses this by checking explicitly for both bindings.
  6355. (defvar orgstruct-mode-map (make-sparse-keymap)
  6356. "Keymap for the minor `orgstruct-mode'.")
  6357. (defvar org-local-vars nil
  6358. "List of local variables, for use by `orgstruct-mode'")
  6359. ;;;###autoload
  6360. (define-minor-mode orgstruct-mode
  6361. "Toggle the minor more `orgstruct-mode'.
  6362. This mode is for using Org-mode structure commands in other modes.
  6363. The following key behave as if Org-mode was active, if the cursor
  6364. is on a headline, or on a plain list item (both in the definition
  6365. of Org-mode).
  6366. M-up Move entry/item up
  6367. M-down Move entry/item down
  6368. M-left Promote
  6369. M-right Demote
  6370. M-S-up Move entry/item up
  6371. M-S-down Move entry/item down
  6372. M-S-left Promote subtree
  6373. M-S-right Demote subtree
  6374. M-q Fill paragraph and items like in Org-mode
  6375. C-c ^ Sort entries
  6376. C-c - Cycle list bullet
  6377. TAB Cycle item visibility
  6378. M-RET Insert new heading/item
  6379. S-M-RET Insert new TODO heading / Checkbox item
  6380. C-c C-c Set tags / toggle checkbox"
  6381. nil " OrgStruct" nil
  6382. (org-load-modules-maybe)
  6383. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6384. ;;;###autoload
  6385. (defun turn-on-orgstruct ()
  6386. "Unconditionally turn on `orgstruct-mode'."
  6387. (orgstruct-mode 1))
  6388. (defun orgstruct++-mode (&optional arg)
  6389. "Toggle `orgstruct-mode', the enhanced version of it.
  6390. In addition to setting orgstruct-mode, this also exports all indentation
  6391. and autofilling variables from org-mode into the buffer. It will also
  6392. recognize item context in multiline items.
  6393. Note that turning off orgstruct-mode will *not* remove the
  6394. indentation/paragraph settings. This can only be done by refreshing the
  6395. major mode, for example with \\[normal-mode]."
  6396. (interactive "P")
  6397. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6398. (if (< arg 1)
  6399. (orgstruct-mode -1)
  6400. (orgstruct-mode 1)
  6401. (let (var val)
  6402. (mapc
  6403. (lambda (x)
  6404. (when (string-match
  6405. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6406. (symbol-name (car x)))
  6407. (setq var (car x) val (nth 1 x))
  6408. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6409. org-local-vars)
  6410. (org-set-local 'orgstruct-is-++ t))))
  6411. (defvar orgstruct-is-++ nil
  6412. "Is orgstruct-mode in ++ version in the current-buffer?")
  6413. (make-variable-buffer-local 'orgstruct-is-++)
  6414. ;;;###autoload
  6415. (defun turn-on-orgstruct++ ()
  6416. "Unconditionally turn on `orgstruct++-mode'."
  6417. (orgstruct++-mode 1))
  6418. (defun orgstruct-error ()
  6419. "Error when there is no default binding for a structure key."
  6420. (interactive)
  6421. (error "This key has no function outside structure elements"))
  6422. (defun orgstruct-setup ()
  6423. "Setup orgstruct keymaps."
  6424. (let ((nfunc 0)
  6425. (bindings
  6426. (list
  6427. '([(meta up)] org-metaup)
  6428. '([(meta down)] org-metadown)
  6429. '([(meta left)] org-metaleft)
  6430. '([(meta right)] org-metaright)
  6431. '([(meta shift up)] org-shiftmetaup)
  6432. '([(meta shift down)] org-shiftmetadown)
  6433. '([(meta shift left)] org-shiftmetaleft)
  6434. '([(meta shift right)] org-shiftmetaright)
  6435. '([?\e (up)] org-metaup)
  6436. '([?\e (down)] org-metadown)
  6437. '([?\e (left)] org-metaleft)
  6438. '([?\e (right)] org-metaright)
  6439. '([?\e (shift up)] org-shiftmetaup)
  6440. '([?\e (shift down)] org-shiftmetadown)
  6441. '([?\e (shift left)] org-shiftmetaleft)
  6442. '([?\e (shift right)] org-shiftmetaright)
  6443. '([(shift up)] org-shiftup)
  6444. '([(shift down)] org-shiftdown)
  6445. '([(shift left)] org-shiftleft)
  6446. '([(shift right)] org-shiftright)
  6447. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6448. '("\M-q" fill-paragraph)
  6449. '("\C-c^" org-sort)
  6450. '("\C-c-" org-cycle-list-bullet)))
  6451. elt key fun cmd)
  6452. (while (setq elt (pop bindings))
  6453. (setq nfunc (1+ nfunc))
  6454. (setq key (org-key (car elt))
  6455. fun (nth 1 elt)
  6456. cmd (orgstruct-make-binding fun nfunc key))
  6457. (org-defkey orgstruct-mode-map key cmd))
  6458. ;; Special treatment needed for TAB and RET
  6459. (org-defkey orgstruct-mode-map [(tab)]
  6460. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6461. (org-defkey orgstruct-mode-map "\C-i"
  6462. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6463. (org-defkey orgstruct-mode-map "\M-\C-m"
  6464. (orgstruct-make-binding 'org-insert-heading 105
  6465. "\M-\C-m" [(meta return)]))
  6466. (org-defkey orgstruct-mode-map [(meta return)]
  6467. (orgstruct-make-binding 'org-insert-heading 106
  6468. [(meta return)] "\M-\C-m"))
  6469. (org-defkey orgstruct-mode-map [(shift meta return)]
  6470. (orgstruct-make-binding 'org-insert-todo-heading 107
  6471. [(meta return)] "\M-\C-m"))
  6472. (org-defkey orgstruct-mode-map "\e\C-m"
  6473. (orgstruct-make-binding 'org-insert-heading 108
  6474. "\e\C-m" [?\e (return)]))
  6475. (org-defkey orgstruct-mode-map [?\e (return)]
  6476. (orgstruct-make-binding 'org-insert-heading 109
  6477. [?\e (return)] "\e\C-m"))
  6478. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6479. (orgstruct-make-binding 'org-insert-todo-heading 110
  6480. [?\e (return)] "\e\C-m"))
  6481. (unless org-local-vars
  6482. (setq org-local-vars (org-get-local-variables)))
  6483. t))
  6484. (defun orgstruct-make-binding (fun n &rest keys)
  6485. "Create a function for binding in the structure minor mode.
  6486. FUN is the command to call inside a table. N is used to create a unique
  6487. command name. KEYS are keys that should be checked in for a command
  6488. to execute outside of tables."
  6489. (eval
  6490. (list 'defun
  6491. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6492. '(arg)
  6493. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6494. "Outside of structure, run the binding of `"
  6495. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6496. "'.")
  6497. '(interactive "p")
  6498. (list 'if
  6499. `(org-context-p 'headline 'item
  6500. (and orgstruct-is-++
  6501. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6502. 'item-body))
  6503. (list 'org-run-like-in-org-mode (list 'quote fun))
  6504. (list 'let '(orgstruct-mode)
  6505. (list 'call-interactively
  6506. (append '(or)
  6507. (mapcar (lambda (k)
  6508. (list 'key-binding k))
  6509. keys)
  6510. '('orgstruct-error))))))))
  6511. (defun org-context-p (&rest contexts)
  6512. "Check if local context is any of CONTEXTS.
  6513. Possible values in the list of contexts are `table', `headline', and `item'."
  6514. (let ((pos (point)))
  6515. (goto-char (point-at-bol))
  6516. (prog1 (or (and (memq 'table contexts)
  6517. (looking-at "[ \t]*|"))
  6518. (and (memq 'headline contexts)
  6519. ;;????????? (looking-at "\\*+"))
  6520. (looking-at outline-regexp))
  6521. (and (memq 'item contexts)
  6522. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6523. (and (memq 'item-body contexts)
  6524. (org-in-item-p)))
  6525. (goto-char pos))))
  6526. (defun org-get-local-variables ()
  6527. "Return a list of all local variables in an org-mode buffer."
  6528. (let (varlist)
  6529. (with-current-buffer (get-buffer-create "*Org tmp*")
  6530. (erase-buffer)
  6531. (org-mode)
  6532. (setq varlist (buffer-local-variables)))
  6533. (kill-buffer "*Org tmp*")
  6534. (delq nil
  6535. (mapcar
  6536. (lambda (x)
  6537. (setq x
  6538. (if (symbolp x)
  6539. (list x)
  6540. (list (car x) (list 'quote (cdr x)))))
  6541. (if (string-match
  6542. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6543. (symbol-name (car x)))
  6544. x nil))
  6545. varlist))))
  6546. ;;;###autoload
  6547. (defun org-run-like-in-org-mode (cmd)
  6548. "Run a command, pretending that the current buffer is in Org-mode.
  6549. This will temporarily bind local variables that are typically bound in
  6550. Org-mode to the values they have in Org-mode, and then interactively
  6551. call CMD."
  6552. (org-load-modules-maybe)
  6553. (unless org-local-vars
  6554. (setq org-local-vars (org-get-local-variables)))
  6555. (eval (list 'let org-local-vars
  6556. (list 'call-interactively (list 'quote cmd)))))
  6557. ;;;; Archiving
  6558. (defun org-get-category (&optional pos)
  6559. "Get the category applying to position POS."
  6560. (get-text-property (or pos (point)) 'org-category))
  6561. (defun org-refresh-category-properties ()
  6562. "Refresh category text properties in the buffer."
  6563. (let ((def-cat (cond
  6564. ((null org-category)
  6565. (if buffer-file-name
  6566. (file-name-sans-extension
  6567. (file-name-nondirectory buffer-file-name))
  6568. "???"))
  6569. ((symbolp org-category) (symbol-name org-category))
  6570. (t org-category)))
  6571. beg end cat pos optionp)
  6572. (org-unmodified
  6573. (save-excursion
  6574. (save-restriction
  6575. (widen)
  6576. (goto-char (point-min))
  6577. (put-text-property (point) (point-max) 'org-category def-cat)
  6578. (while (re-search-forward
  6579. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6580. (setq pos (match-end 0)
  6581. optionp (equal (char-after (match-beginning 0)) ?#)
  6582. cat (org-trim (match-string 2)))
  6583. (if optionp
  6584. (setq beg (point-at-bol) end (point-max))
  6585. (org-back-to-heading t)
  6586. (setq beg (point) end (org-end-of-subtree t t)))
  6587. (put-text-property beg end 'org-category cat)
  6588. (goto-char pos)))))))
  6589. ;;;; Link Stuff
  6590. ;;; Link abbreviations
  6591. (defun org-link-expand-abbrev (link)
  6592. "Apply replacements as defined in `org-link-abbrev-alist."
  6593. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6594. (let* ((key (match-string 1 link))
  6595. (as (or (assoc key org-link-abbrev-alist-local)
  6596. (assoc key org-link-abbrev-alist)))
  6597. (tag (and (match-end 2) (match-string 3 link)))
  6598. rpl)
  6599. (if (not as)
  6600. link
  6601. (setq rpl (cdr as))
  6602. (cond
  6603. ((symbolp rpl) (funcall rpl tag))
  6604. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6605. ((string-match "%h" rpl)
  6606. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6607. (t (concat rpl tag)))))
  6608. link))
  6609. ;;; Storing and inserting links
  6610. (defvar org-insert-link-history nil
  6611. "Minibuffer history for links inserted with `org-insert-link'.")
  6612. (defvar org-stored-links nil
  6613. "Contains the links stored with `org-store-link'.")
  6614. (defvar org-store-link-plist nil
  6615. "Plist with info about the most recently link created with `org-store-link'.")
  6616. (defvar org-link-protocols nil
  6617. "Link protocols added to Org-mode using `org-add-link-type'.")
  6618. (defvar org-store-link-functions nil
  6619. "List of functions that are called to create and store a link.
  6620. Each function will be called in turn until one returns a non-nil
  6621. value. Each function should check if it is responsible for creating
  6622. this link (for example by looking at the major mode).
  6623. If not, it must exit and return nil.
  6624. If yes, it should return a non-nil value after a calling
  6625. `org-store-link-props' with a list of properties and values.
  6626. Special properties are:
  6627. :type The link prefix. like \"http\". This must be given.
  6628. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6629. This is obligatory as well.
  6630. :description Optional default description for the second pair
  6631. of brackets in an Org-mode link. The user can still change
  6632. this when inserting this link into an Org-mode buffer.
  6633. In addition to these, any additional properties can be specified
  6634. and then used in remember templates.")
  6635. (defun org-add-link-type (type &optional follow export)
  6636. "Add TYPE to the list of `org-link-types'.
  6637. Re-compute all regular expressions depending on `org-link-types'
  6638. FOLLOW and EXPORT are two functions.
  6639. FOLLOW should take the link path as the single argument and do whatever
  6640. is necessary to follow the link, for example find a file or display
  6641. a mail message.
  6642. EXPORT should format the link path for export to one of the export formats.
  6643. It should be a function accepting three arguments:
  6644. path the path of the link, the text after the prefix (like \"http:\")
  6645. desc the description of the link, if any, nil if there was no description
  6646. format the export format, a symbol like `html' or `latex'.
  6647. The function may use the FORMAT information to return different values
  6648. depending on the format. The return value will be put literally into
  6649. the exported file.
  6650. Org-mode has a built-in default for exporting links. If you are happy with
  6651. this default, there is no need to define an export function for the link
  6652. type. For a simple example of an export function, see `org-bbdb.el'."
  6653. (add-to-list 'org-link-types type t)
  6654. (org-make-link-regexps)
  6655. (if (assoc type org-link-protocols)
  6656. (setcdr (assoc type org-link-protocols) (list follow export))
  6657. (push (list type follow export) org-link-protocols)))
  6658. (defvar org-agenda-buffer-name)
  6659. ;;;###autoload
  6660. (defun org-store-link (arg)
  6661. "\\<org-mode-map>Store an org-link to the current location.
  6662. This link is added to `org-stored-links' and can later be inserted
  6663. into an org-buffer with \\[org-insert-link].
  6664. For some link types, a prefix arg is interpreted:
  6665. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6666. For file links, arg negates `org-context-in-file-links'."
  6667. (interactive "P")
  6668. (org-load-modules-maybe)
  6669. (setq org-store-link-plist nil) ; reset
  6670. (let ((outline-regexp (org-get-limited-outline-regexp))
  6671. link cpltxt desc description search txt custom-id)
  6672. (cond
  6673. ((run-hook-with-args-until-success 'org-store-link-functions)
  6674. (setq link (plist-get org-store-link-plist :link)
  6675. desc (or (plist-get org-store-link-plist :description) link)))
  6676. ((equal (buffer-name) "*Org Edit Src Example*")
  6677. (let (label gc)
  6678. (while (or (not label)
  6679. (save-excursion
  6680. (save-restriction
  6681. (widen)
  6682. (goto-char (point-min))
  6683. (re-search-forward
  6684. (regexp-quote (format org-coderef-label-format label))
  6685. nil t))))
  6686. (when label (message "Label exists already") (sit-for 2))
  6687. (setq label (read-string "Code line label: " label)))
  6688. (end-of-line 1)
  6689. (setq link (format org-coderef-label-format label))
  6690. (setq gc (- 79 (length link)))
  6691. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6692. (insert link)
  6693. (setq link (concat "(" label ")") desc nil)))
  6694. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6695. ;; We are in the agenda, link to referenced location
  6696. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6697. (get-text-property (point) 'org-marker))))
  6698. (when m
  6699. (org-with-point-at m
  6700. (call-interactively 'org-store-link)))))
  6701. ((eq major-mode 'calendar-mode)
  6702. (let ((cd (calendar-cursor-to-date)))
  6703. (setq link
  6704. (format-time-string
  6705. (car org-time-stamp-formats)
  6706. (apply 'encode-time
  6707. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6708. nil nil nil))))
  6709. (org-store-link-props :type "calendar" :date cd)))
  6710. ((eq major-mode 'w3-mode)
  6711. (setq cpltxt (if (and (buffer-name)
  6712. (not (string-match "Untitled" (buffer-name))))
  6713. (buffer-name)
  6714. (url-view-url t))
  6715. link (org-make-link (url-view-url t)))
  6716. (org-store-link-props :type "w3" :url (url-view-url t)))
  6717. ((eq major-mode 'w3m-mode)
  6718. (setq cpltxt (or w3m-current-title w3m-current-url)
  6719. link (org-make-link w3m-current-url))
  6720. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6721. ((setq search (run-hook-with-args-until-success
  6722. 'org-create-file-search-functions))
  6723. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6724. "::" search))
  6725. (setq cpltxt (or description link)))
  6726. ((eq major-mode 'image-mode)
  6727. (setq cpltxt (concat "file:"
  6728. (abbreviate-file-name buffer-file-name))
  6729. link (org-make-link cpltxt))
  6730. (org-store-link-props :type "image" :file buffer-file-name))
  6731. ((eq major-mode 'dired-mode)
  6732. ;; link to the file in the current line
  6733. (setq cpltxt (concat "file:"
  6734. (abbreviate-file-name
  6735. (expand-file-name
  6736. (dired-get-filename nil t))))
  6737. link (org-make-link cpltxt)))
  6738. ((and buffer-file-name (org-mode-p))
  6739. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6740. (cond
  6741. ((org-in-regexp "<<\\(.*?\\)>>")
  6742. (setq cpltxt
  6743. (concat "file:"
  6744. (abbreviate-file-name buffer-file-name)
  6745. "::" (match-string 1))
  6746. link (org-make-link cpltxt)))
  6747. ((and (featurep 'org-id)
  6748. (or (eq org-link-to-org-use-id t)
  6749. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6750. (interactive-p))
  6751. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6752. (interactive-p)
  6753. (not custom-id))
  6754. (and org-link-to-org-use-id
  6755. (condition-case nil
  6756. (org-entry-get nil "ID")
  6757. (error nil)))))
  6758. ;; We can make a link using the ID.
  6759. (setq link (condition-case nil
  6760. (prog1 (org-id-store-link)
  6761. (setq desc (plist-get org-store-link-plist
  6762. :description)))
  6763. (error
  6764. ;; probably before first headline, link to file only
  6765. (concat "file:"
  6766. (abbreviate-file-name buffer-file-name))))))
  6767. (t
  6768. ;; Just link to current headline
  6769. (setq cpltxt (concat "file:"
  6770. (abbreviate-file-name buffer-file-name)))
  6771. ;; Add a context search string
  6772. (when (org-xor org-context-in-file-links arg)
  6773. (setq txt (cond
  6774. ((org-on-heading-p) nil)
  6775. ((org-region-active-p)
  6776. (buffer-substring (region-beginning) (region-end)))
  6777. (t nil)))
  6778. (when (or (null txt) (string-match "\\S-" txt))
  6779. (setq cpltxt
  6780. (concat cpltxt "::"
  6781. (condition-case nil
  6782. (org-make-org-heading-search-string txt)
  6783. (error "")))
  6784. desc (or (nth 4 (ignore-errors
  6785. (org-heading-components))) "NONE"))))
  6786. (if (string-match "::\\'" cpltxt)
  6787. (setq cpltxt (substring cpltxt 0 -2)))
  6788. (setq link (org-make-link cpltxt)))))
  6789. ((buffer-file-name (buffer-base-buffer))
  6790. ;; Just link to this file here.
  6791. (setq cpltxt (concat "file:"
  6792. (abbreviate-file-name
  6793. (buffer-file-name (buffer-base-buffer)))))
  6794. ;; Add a context string
  6795. (when (org-xor org-context-in-file-links arg)
  6796. (setq txt (if (org-region-active-p)
  6797. (buffer-substring (region-beginning) (region-end))
  6798. (buffer-substring (point-at-bol) (point-at-eol))))
  6799. ;; Only use search option if there is some text.
  6800. (when (string-match "\\S-" txt)
  6801. (setq cpltxt
  6802. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6803. desc "NONE")))
  6804. (setq link (org-make-link cpltxt)))
  6805. ((interactive-p)
  6806. (error "Cannot link to a buffer which is not visiting a file"))
  6807. (t (setq link nil)))
  6808. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6809. (setq link (or link cpltxt)
  6810. desc (or desc cpltxt))
  6811. (if (equal desc "NONE") (setq desc nil))
  6812. (if (and (or (interactive-p) executing-kbd-macro) link)
  6813. (progn
  6814. (setq org-stored-links
  6815. (cons (list link desc) org-stored-links))
  6816. (message "Stored: %s" (or desc link))
  6817. (when custom-id
  6818. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6819. "::#" custom-id))
  6820. (setq org-stored-links
  6821. (cons (list link desc) org-stored-links))))
  6822. (and link (org-make-link-string link desc)))))
  6823. (defun org-store-link-props (&rest plist)
  6824. "Store link properties, extract names and addresses."
  6825. (let (x adr)
  6826. (when (setq x (plist-get plist :from))
  6827. (setq adr (mail-extract-address-components x))
  6828. (setq plist (plist-put plist :fromname (car adr)))
  6829. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6830. (when (setq x (plist-get plist :to))
  6831. (setq adr (mail-extract-address-components x))
  6832. (setq plist (plist-put plist :toname (car adr)))
  6833. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6834. (let ((from (plist-get plist :from))
  6835. (to (plist-get plist :to)))
  6836. (when (and from to org-from-is-user-regexp)
  6837. (setq plist
  6838. (plist-put plist :fromto
  6839. (if (string-match org-from-is-user-regexp from)
  6840. (concat "to %t")
  6841. (concat "from %f"))))))
  6842. (setq org-store-link-plist plist))
  6843. (defun org-add-link-props (&rest plist)
  6844. "Add these properties to the link property list."
  6845. (let (key value)
  6846. (while plist
  6847. (setq key (pop plist) value (pop plist))
  6848. (setq org-store-link-plist
  6849. (plist-put org-store-link-plist key value)))))
  6850. (defun org-email-link-description (&optional fmt)
  6851. "Return the description part of an email link.
  6852. This takes information from `org-store-link-plist' and formats it
  6853. according to FMT (default from `org-email-link-description-format')."
  6854. (setq fmt (or fmt org-email-link-description-format))
  6855. (let* ((p org-store-link-plist)
  6856. (to (plist-get p :toaddress))
  6857. (from (plist-get p :fromaddress))
  6858. (table
  6859. (list
  6860. (cons "%c" (plist-get p :fromto))
  6861. (cons "%F" (plist-get p :from))
  6862. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6863. (cons "%T" (plist-get p :to))
  6864. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6865. (cons "%s" (plist-get p :subject))
  6866. (cons "%m" (plist-get p :message-id)))))
  6867. (when (string-match "%c" fmt)
  6868. ;; Check if the user wrote this message
  6869. (if (and org-from-is-user-regexp from to
  6870. (save-match-data (string-match org-from-is-user-regexp from)))
  6871. (setq fmt (replace-match "to %t" t t fmt))
  6872. (setq fmt (replace-match "from %f" t t fmt))))
  6873. (org-replace-escapes fmt table)))
  6874. (defun org-make-org-heading-search-string (&optional string heading)
  6875. "Make search string for STRING or current headline."
  6876. (interactive)
  6877. (let ((s (or string (org-get-heading))))
  6878. (unless (and string (not heading))
  6879. ;; We are using a headline, clean up garbage in there.
  6880. (if (string-match org-todo-regexp s)
  6881. (setq s (replace-match "" t t s)))
  6882. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6883. (setq s (replace-match "" t t s)))
  6884. (setq s (org-trim s))
  6885. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6886. org-comment-string "\\)") s)
  6887. (setq s (replace-match "" t t s)))
  6888. (while (string-match org-ts-regexp s)
  6889. (setq s (replace-match "" t t s))))
  6890. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6891. (setq s (replace-match " " t t s)))
  6892. (or string (setq s (concat "*" s))) ; Add * for headlines
  6893. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6894. (defun org-make-link (&rest strings)
  6895. "Concatenate STRINGS."
  6896. (apply 'concat strings))
  6897. (defun org-make-link-string (link &optional description)
  6898. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6899. (unless (string-match "\\S-" link)
  6900. (error "Empty link"))
  6901. (when (stringp description)
  6902. ;; Remove brackets from the description, they are fatal.
  6903. (while (string-match "\\[" description)
  6904. (setq description (replace-match "{" t t description)))
  6905. (while (string-match "\\]" description)
  6906. (setq description (replace-match "}" t t description))))
  6907. (when (equal (org-link-escape link) description)
  6908. ;; No description needed, it is identical
  6909. (setq description nil))
  6910. (when (and (not description)
  6911. (not (equal link (org-link-escape link))))
  6912. (setq description (org-extract-attributes link)))
  6913. (concat "[[" (org-link-escape link) "]"
  6914. (if description (concat "[" description "]") "")
  6915. "]"))
  6916. (defconst org-link-escape-chars
  6917. '((?\ . "%20")
  6918. (?\[ . "%5B")
  6919. (?\] . "%5D")
  6920. (?\340 . "%E0") ; `a
  6921. (?\342 . "%E2") ; ^a
  6922. (?\347 . "%E7") ; ,c
  6923. (?\350 . "%E8") ; `e
  6924. (?\351 . "%E9") ; 'e
  6925. (?\352 . "%EA") ; ^e
  6926. (?\356 . "%EE") ; ^i
  6927. (?\364 . "%F4") ; ^o
  6928. (?\371 . "%F9") ; `u
  6929. (?\373 . "%FB") ; ^u
  6930. (?\; . "%3B")
  6931. (?? . "%3F")
  6932. (?= . "%3D")
  6933. (?+ . "%2B")
  6934. )
  6935. "Association list of escapes for some characters problematic in links.
  6936. This is the list that is used for internal purposes.")
  6937. (defvar org-url-encoding-use-url-hexify nil)
  6938. (defconst org-link-escape-chars-browser
  6939. '((?\ . "%20")) ; 32 for the SPC char
  6940. "Association list of escapes for some characters problematic in links.
  6941. This is the list that is used before handing over to the browser.")
  6942. (defun org-link-escape (text &optional table)
  6943. "Escape characters in TEXT that are problematic for links."
  6944. (if org-url-encoding-use-url-hexify
  6945. (url-hexify-string text)
  6946. (setq table (or table org-link-escape-chars))
  6947. (when text
  6948. (let ((re (mapconcat (lambda (x) (regexp-quote
  6949. (char-to-string (car x))))
  6950. table "\\|")))
  6951. (while (string-match re text)
  6952. (setq text
  6953. (replace-match
  6954. (cdr (assoc (string-to-char (match-string 0 text))
  6955. table))
  6956. t t text)))
  6957. text))))
  6958. (defun org-link-unescape (text &optional table)
  6959. "Reverse the action of `org-link-escape'."
  6960. (if org-url-encoding-use-url-hexify
  6961. (url-unhex-string text)
  6962. (setq table (or table org-link-escape-chars))
  6963. (when text
  6964. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6965. table "\\|")))
  6966. (while (string-match re text)
  6967. (setq text
  6968. (replace-match
  6969. (char-to-string (car (rassoc (match-string 0 text) table)))
  6970. t t text)))
  6971. text))))
  6972. (defun org-xor (a b)
  6973. "Exclusive or."
  6974. (if a (not b) b))
  6975. (defun org-fixup-message-id-for-http (s)
  6976. "Replace special characters in a message id, so it can be used in an http query."
  6977. (while (string-match "<" s)
  6978. (setq s (replace-match "%3C" t t s)))
  6979. (while (string-match ">" s)
  6980. (setq s (replace-match "%3E" t t s)))
  6981. (while (string-match "@" s)
  6982. (setq s (replace-match "%40" t t s)))
  6983. s)
  6984. ;;;###autoload
  6985. (defun org-insert-link-global ()
  6986. "Insert a link like Org-mode does.
  6987. This command can be called in any mode to insert a link in Org-mode syntax."
  6988. (interactive)
  6989. (org-load-modules-maybe)
  6990. (org-run-like-in-org-mode 'org-insert-link))
  6991. (defun org-insert-link (&optional complete-file link-location)
  6992. "Insert a link. At the prompt, enter the link.
  6993. Completion can be used to insert any of the link protocol prefixes like
  6994. http or ftp in use.
  6995. The history can be used to select a link previously stored with
  6996. `org-store-link'. When the empty string is entered (i.e. if you just
  6997. press RET at the prompt), the link defaults to the most recently
  6998. stored link. As SPC triggers completion in the minibuffer, you need to
  6999. use M-SPC or C-q SPC to force the insertion of a space character.
  7000. You will also be prompted for a description, and if one is given, it will
  7001. be displayed in the buffer instead of the link.
  7002. If there is already a link at point, this command will allow you to edit link
  7003. and description parts.
  7004. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7005. be selected using completion. The path to the file will be relative to the
  7006. current directory if the file is in the current directory or a subdirectory.
  7007. Otherwise, the link will be the absolute path as completed in the minibuffer
  7008. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7009. option `org-link-file-path-type'.
  7010. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7011. the current directory or below.
  7012. With three \\[universal-argument] prefixes, negate the meaning of
  7013. `org-keep-stored-link-after-insertion'.
  7014. If `org-make-link-description-function' is non-nil, this function will be
  7015. called with the link target, and the result will be the default
  7016. link description.
  7017. If the LINK-LOCATION parameter is non-nil, this value will be
  7018. used as the link location instead of reading one interactively."
  7019. (interactive "P")
  7020. (let* ((wcf (current-window-configuration))
  7021. (region (if (org-region-active-p)
  7022. (buffer-substring (region-beginning) (region-end))))
  7023. (remove (and region (list (region-beginning) (region-end))))
  7024. (desc region)
  7025. tmphist ; byte-compile incorrectly complains about this
  7026. (link link-location)
  7027. entry file all-prefixes)
  7028. (cond
  7029. (link-location) ; specified by arg, just use it.
  7030. ((org-in-regexp org-bracket-link-regexp 1)
  7031. ;; We do have a link at point, and we are going to edit it.
  7032. (setq remove (list (match-beginning 0) (match-end 0)))
  7033. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7034. (setq link (read-string "Link: "
  7035. (org-link-unescape
  7036. (org-match-string-no-properties 1)))))
  7037. ((or (org-in-regexp org-angle-link-re)
  7038. (org-in-regexp org-plain-link-re))
  7039. ;; Convert to bracket link
  7040. (setq remove (list (match-beginning 0) (match-end 0))
  7041. link (read-string "Link: "
  7042. (org-remove-angle-brackets (match-string 0)))))
  7043. ((member complete-file '((4) (16)))
  7044. ;; Completing read for file names.
  7045. (setq link (org-file-complete-link complete-file)))
  7046. (t
  7047. ;; Read link, with completion for stored links.
  7048. (with-output-to-temp-buffer "*Org Links*"
  7049. (princ "Insert a link.
  7050. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7051. (when org-stored-links
  7052. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7053. (princ (mapconcat
  7054. (lambda (x)
  7055. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7056. (reverse org-stored-links) "\n"))))
  7057. (let ((cw (selected-window)))
  7058. (select-window (get-buffer-window "*Org Links*"))
  7059. (setq truncate-lines t)
  7060. (unless (pos-visible-in-window-p (point-max))
  7061. (org-fit-window-to-buffer))
  7062. (and (window-live-p cw) (select-window cw)))
  7063. ;; Fake a link history, containing the stored links.
  7064. (setq tmphist (append (mapcar 'car org-stored-links)
  7065. org-insert-link-history))
  7066. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7067. (mapcar 'car org-link-abbrev-alist)
  7068. org-link-types))
  7069. (unwind-protect
  7070. (progn
  7071. (setq link
  7072. (let ((org-completion-use-ido nil)
  7073. (org-completion-use-iswitchb nil))
  7074. (org-completing-read
  7075. "Link: "
  7076. (append
  7077. (mapcar (lambda (x) (list (concat x ":")))
  7078. all-prefixes)
  7079. (mapcar 'car org-stored-links))
  7080. nil nil nil
  7081. 'tmphist
  7082. (car (car org-stored-links)))))
  7083. (if (or (member link all-prefixes)
  7084. (and (equal ":" (substring link -1))
  7085. (member (substring link 0 -1) all-prefixes)
  7086. (setq link (substring link 0 -1))))
  7087. (setq link (org-link-try-special-completion link))))
  7088. (set-window-configuration wcf)
  7089. (kill-buffer "*Org Links*"))
  7090. (setq entry (assoc link org-stored-links))
  7091. (or entry (push link org-insert-link-history))
  7092. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7093. (not org-keep-stored-link-after-insertion))
  7094. (setq org-stored-links (delq (assoc link org-stored-links)
  7095. org-stored-links)))
  7096. (setq desc (or desc (nth 1 entry)))))
  7097. (if (string-match org-plain-link-re link)
  7098. ;; URL-like link, normalize the use of angular brackets.
  7099. (setq link (org-make-link (org-remove-angle-brackets link))))
  7100. ;; Check if we are linking to the current file with a search option
  7101. ;; If yes, simplify the link by using only the search option.
  7102. (when (and buffer-file-name
  7103. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7104. (let* ((path (match-string 1 link))
  7105. (case-fold-search nil)
  7106. (search (match-string 2 link)))
  7107. (save-match-data
  7108. (if (equal (file-truename buffer-file-name) (file-truename path))
  7109. ;; We are linking to this same file, with a search option
  7110. (setq link search)))))
  7111. ;; Check if we can/should use a relative path. If yes, simplify the link
  7112. (when (string-match "^file:\\(.*\\)" link)
  7113. (let* ((path (match-string 1 link))
  7114. (origpath path)
  7115. (case-fold-search nil))
  7116. (cond
  7117. ((or (eq org-link-file-path-type 'absolute)
  7118. (equal complete-file '(16)))
  7119. (setq path (abbreviate-file-name (expand-file-name path))))
  7120. ((eq org-link-file-path-type 'noabbrev)
  7121. (setq path (expand-file-name path)))
  7122. ((eq org-link-file-path-type 'relative)
  7123. (setq path (file-relative-name path)))
  7124. (t
  7125. (save-match-data
  7126. (if (string-match (concat "^" (regexp-quote
  7127. (file-name-as-directory
  7128. (expand-file-name "."))))
  7129. (expand-file-name path))
  7130. ;; We are linking a file with relative path name.
  7131. (setq path (substring (expand-file-name path)
  7132. (match-end 0)))
  7133. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7134. (setq link (concat "file:" path))
  7135. (if (equal desc origpath)
  7136. (setq desc path))))
  7137. (if org-make-link-description-function
  7138. (setq desc (funcall org-make-link-description-function link desc)))
  7139. (setq desc (read-string "Description: " desc))
  7140. (unless (string-match "\\S-" desc) (setq desc nil))
  7141. (if remove (apply 'delete-region remove))
  7142. (insert (org-make-link-string link desc))))
  7143. (defun org-link-try-special-completion (type)
  7144. "If there is completion support for link type TYPE, offer it."
  7145. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7146. (if (functionp fun)
  7147. (funcall fun)
  7148. (read-string "Link (no completion support): " (concat type ":")))))
  7149. (defun org-file-complete-link (&optional arg)
  7150. "Create a file link using completion."
  7151. (let (file link)
  7152. (setq file (read-file-name "File: "))
  7153. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7154. (pwd1 (file-name-as-directory (abbreviate-file-name
  7155. (expand-file-name ".")))))
  7156. (cond
  7157. ((equal arg '(16))
  7158. (setq link (org-make-link
  7159. "file:"
  7160. (abbreviate-file-name (expand-file-name file)))))
  7161. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7162. (setq link (org-make-link "file:" (match-string 1 file))))
  7163. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7164. (expand-file-name file))
  7165. (setq link (org-make-link
  7166. "file:" (match-string 1 (expand-file-name file)))))
  7167. (t (setq link (org-make-link "file:" file)))))
  7168. link))
  7169. (defun org-completing-read (&rest args)
  7170. "Completing-read with SPACE being a normal character."
  7171. (let ((minibuffer-local-completion-map
  7172. (copy-keymap minibuffer-local-completion-map)))
  7173. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7174. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7175. (apply 'org-icompleting-read args)))
  7176. (defun org-completing-read-no-i (&rest args)
  7177. (let (org-completion-use-ido org-completion-use-iswitchb)
  7178. (apply 'org-completing-read args)))
  7179. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7180. "Use iswitch as a completing-read replacement to choose from choices.
  7181. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7182. from."
  7183. (let* ((iswitchb-use-virtual-buffers nil)
  7184. (iswitchb-make-buflist-hook
  7185. (lambda ()
  7186. (setq iswitchb-temp-buflist choices))))
  7187. (iswitchb-read-buffer prompt)))
  7188. (defun org-icompleting-read (&rest args)
  7189. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7190. (org-without-partial-completion
  7191. (if (and org-completion-use-ido
  7192. (fboundp 'ido-completing-read)
  7193. (boundp 'ido-mode) ido-mode
  7194. (listp (second args)))
  7195. (let ((ido-enter-matching-directory nil))
  7196. (apply 'ido-completing-read (concat (car args))
  7197. (if (consp (car (nth 1 args)))
  7198. (mapcar (lambda (x) (car x)) (nth 1 args))
  7199. (nth 1 args))
  7200. (cddr args)))
  7201. (if (and org-completion-use-iswitchb
  7202. (boundp 'iswitchb-mode) iswitchb-mode
  7203. (listp (second args)))
  7204. (apply 'org-iswitchb-completing-read (concat (car args))
  7205. (if (consp (car (nth 1 args)))
  7206. (mapcar (lambda (x) (car x)) (nth 1 args))
  7207. (nth 1 args))
  7208. (cddr args))
  7209. (apply 'completing-read args)))))
  7210. (defun org-extract-attributes (s)
  7211. "Extract the attributes cookie from a string and set as text property."
  7212. (let (a attr (start 0) key value)
  7213. (save-match-data
  7214. (when (string-match "{{\\([^}]+\\)}}$" s)
  7215. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7216. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7217. (setq key (match-string 1 a) value (match-string 2 a)
  7218. start (match-end 0)
  7219. attr (plist-put attr (intern key) value))))
  7220. (org-add-props s nil 'org-attr attr))
  7221. s))
  7222. (defun org-extract-attributes-from-string (tag)
  7223. (let (key value attr)
  7224. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7225. (setq key (match-string 1 tag) value (match-string 2 tag)
  7226. tag (replace-match "" t t tag)
  7227. attr (plist-put attr (intern key) value)))
  7228. (cons tag attr)))
  7229. (defun org-attributes-to-string (plist)
  7230. "Format a property list into an HTML attribute list."
  7231. (let ((s "") key value)
  7232. (while plist
  7233. (setq key (pop plist) value (pop plist))
  7234. (and value
  7235. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7236. s))
  7237. ;;; Opening/following a link
  7238. (defvar org-link-search-failed nil)
  7239. (defun org-next-link ()
  7240. "Move forward to the next link.
  7241. If the link is in hidden text, expose it."
  7242. (interactive)
  7243. (when (and org-link-search-failed (eq this-command last-command))
  7244. (goto-char (point-min))
  7245. (message "Link search wrapped back to beginning of buffer"))
  7246. (setq org-link-search-failed nil)
  7247. (let* ((pos (point))
  7248. (ct (org-context))
  7249. (a (assoc :link ct)))
  7250. (if a (goto-char (nth 2 a)))
  7251. (if (re-search-forward org-any-link-re nil t)
  7252. (progn
  7253. (goto-char (match-beginning 0))
  7254. (if (org-invisible-p) (org-show-context)))
  7255. (goto-char pos)
  7256. (setq org-link-search-failed t)
  7257. (error "No further link found"))))
  7258. (defun org-previous-link ()
  7259. "Move backward to the previous link.
  7260. If the link is in hidden text, expose it."
  7261. (interactive)
  7262. (when (and org-link-search-failed (eq this-command last-command))
  7263. (goto-char (point-max))
  7264. (message "Link search wrapped back to end of buffer"))
  7265. (setq org-link-search-failed nil)
  7266. (let* ((pos (point))
  7267. (ct (org-context))
  7268. (a (assoc :link ct)))
  7269. (if a (goto-char (nth 1 a)))
  7270. (if (re-search-backward org-any-link-re nil t)
  7271. (progn
  7272. (goto-char (match-beginning 0))
  7273. (if (org-invisible-p) (org-show-context)))
  7274. (goto-char pos)
  7275. (setq org-link-search-failed t)
  7276. (error "No further link found"))))
  7277. (defun org-translate-link (s)
  7278. "Translate a link string if a translation function has been defined."
  7279. (if (and org-link-translation-function
  7280. (fboundp org-link-translation-function)
  7281. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7282. (progn
  7283. (setq s (funcall org-link-translation-function
  7284. (match-string 1) (match-string 2)))
  7285. (concat (car s) ":" (cdr s)))
  7286. s))
  7287. (defun org-translate-link-from-planner (type path)
  7288. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7289. This is still an experimental function, your mileage may vary."
  7290. (cond
  7291. ((member type '("http" "https" "news" "ftp"))
  7292. ;; standard Internet links are the same.
  7293. nil)
  7294. ((and (equal type "irc") (string-match "^//" path))
  7295. ;; Planner has two / at the beginning of an irc link, we have 1.
  7296. ;; We should have zero, actually....
  7297. (setq path (substring path 1)))
  7298. ((and (equal type "lisp") (string-match "^/" path))
  7299. ;; Planner has a slash, we do not.
  7300. (setq type "elisp" path (substring path 1)))
  7301. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7302. ;; A typical message link. Planner has the id after the final slash,
  7303. ;; we separate it with a hash mark
  7304. (setq path (concat (match-string 1 path) "#"
  7305. (org-remove-angle-brackets (match-string 2 path)))))
  7306. )
  7307. (cons type path))
  7308. (defun org-find-file-at-mouse (ev)
  7309. "Open file link or URL at mouse."
  7310. (interactive "e")
  7311. (mouse-set-point ev)
  7312. (org-open-at-point 'in-emacs))
  7313. (defun org-open-at-mouse (ev)
  7314. "Open file link or URL at mouse."
  7315. (interactive "e")
  7316. (mouse-set-point ev)
  7317. (if (eq major-mode 'org-agenda-mode)
  7318. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7319. (org-open-at-point))
  7320. (defvar org-window-config-before-follow-link nil
  7321. "The window configuration before following a link.
  7322. This is saved in case the need arises to restore it.")
  7323. (defvar org-open-link-marker (make-marker)
  7324. "Marker pointing to the location where `org-open-at-point; was called.")
  7325. ;;;###autoload
  7326. (defun org-open-at-point-global ()
  7327. "Follow a link like Org-mode does.
  7328. This command can be called in any mode to follow a link that has
  7329. Org-mode syntax."
  7330. (interactive)
  7331. (org-run-like-in-org-mode 'org-open-at-point))
  7332. ;;;###autoload
  7333. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7334. "Open a link in the string S, as if it was in Org-mode."
  7335. (interactive "sLink: \nP")
  7336. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7337. (with-temp-buffer
  7338. (let ((org-inhibit-startup t))
  7339. (org-mode)
  7340. (insert s)
  7341. (goto-char (point-min))
  7342. (org-open-at-point arg reference-buffer)))))
  7343. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7344. "Open link at or after point.
  7345. If there is no link at point, this function will search forward up to
  7346. the end of the current line.
  7347. Normally, files will be opened by an appropriate application. If the
  7348. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7349. With a double prefix argument, try to open outside of Emacs, in the
  7350. application the system uses for this file type."
  7351. (interactive "P")
  7352. (org-load-modules-maybe)
  7353. (move-marker org-open-link-marker (point))
  7354. (setq org-window-config-before-follow-link (current-window-configuration))
  7355. (org-remove-occur-highlights nil nil t)
  7356. (cond
  7357. ((and (org-on-heading-p)
  7358. (not (org-in-regexp
  7359. (concat org-plain-link-re "\\|"
  7360. org-bracket-link-regexp "\\|"
  7361. org-angle-link-re "\\|"
  7362. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7363. (or (org-offer-links-in-entry in-emacs)
  7364. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7365. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7366. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7367. (org-footnote-action))
  7368. (t
  7369. (let (type path link line search (pos (point)))
  7370. (catch 'match
  7371. (save-excursion
  7372. (skip-chars-forward "^]\n\r")
  7373. (when (org-in-regexp org-bracket-link-regexp)
  7374. (setq link (org-extract-attributes
  7375. (org-link-unescape (org-match-string-no-properties 1))))
  7376. (while (string-match " *\n *" link)
  7377. (setq link (replace-match " " t t link)))
  7378. (setq link (org-link-expand-abbrev link))
  7379. (cond
  7380. ((or (file-name-absolute-p link)
  7381. (string-match "^\\.\\.?/" link))
  7382. (setq type "file" path link))
  7383. ((string-match org-link-re-with-space3 link)
  7384. (setq type (match-string 1 link) path (match-string 2 link)))
  7385. (t (setq type "thisfile" path link)))
  7386. (throw 'match t)))
  7387. (when (get-text-property (point) 'org-linked-text)
  7388. (setq type "thisfile"
  7389. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7390. (1+ (point)) (point))
  7391. path (buffer-substring
  7392. (previous-single-property-change pos 'org-linked-text)
  7393. (next-single-property-change pos 'org-linked-text)))
  7394. (throw 'match t))
  7395. (save-excursion
  7396. (when (or (org-in-regexp org-angle-link-re)
  7397. (org-in-regexp org-plain-link-re))
  7398. (setq type (match-string 1) path (match-string 2))
  7399. (throw 'match t)))
  7400. (save-excursion
  7401. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7402. (setq type "tags"
  7403. path (match-string 1))
  7404. (while (string-match ":" path)
  7405. (setq path (replace-match "+" t t path)))
  7406. (throw 'match t)))
  7407. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7408. (setq type "tree-match"
  7409. path (match-string 1))
  7410. (throw 'match t)))
  7411. (unless path
  7412. (error "No link found"))
  7413. ;; switch back to reference buffer
  7414. ;; needed when if called in a temporary buffer through
  7415. ;; org-open-link-from-string
  7416. (with-current-buffer (or reference-buffer (current-buffer))
  7417. ;; Remove any trailing spaces in path
  7418. (if (string-match " +\\'" path)
  7419. (setq path (replace-match "" t t path)))
  7420. (if (and org-link-translation-function
  7421. (fboundp org-link-translation-function))
  7422. ;; Check if we need to translate the link
  7423. (let ((tmp (funcall org-link-translation-function type path)))
  7424. (setq type (car tmp) path (cdr tmp))))
  7425. (cond
  7426. ((assoc type org-link-protocols)
  7427. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7428. ((equal type "mailto")
  7429. (let ((cmd (car org-link-mailto-program))
  7430. (args (cdr org-link-mailto-program)) args1
  7431. (address path) (subject "") a)
  7432. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7433. (setq address (match-string 1 path)
  7434. subject (org-link-escape (match-string 2 path))))
  7435. (while args
  7436. (cond
  7437. ((not (stringp (car args))) (push (pop args) args1))
  7438. (t (setq a (pop args))
  7439. (if (string-match "%a" a)
  7440. (setq a (replace-match address t t a)))
  7441. (if (string-match "%s" a)
  7442. (setq a (replace-match subject t t a)))
  7443. (push a args1))))
  7444. (apply cmd (nreverse args1))))
  7445. ((member type '("http" "https" "ftp" "news"))
  7446. (browse-url (concat type ":" (org-link-escape
  7447. path org-link-escape-chars-browser))))
  7448. ((member type '("message"))
  7449. (browse-url (concat type ":" path)))
  7450. ((string= type "tags")
  7451. (org-tags-view in-emacs path))
  7452. ((string= type "thisfile")
  7453. (if in-emacs
  7454. (switch-to-buffer-other-window
  7455. (org-get-buffer-for-internal-link (current-buffer)))
  7456. (org-mark-ring-push))
  7457. (let ((cmd `(org-link-search
  7458. ,path
  7459. ,(cond ((equal in-emacs '(4)) 'occur)
  7460. ((equal in-emacs '(16)) 'org-occur)
  7461. (t nil))
  7462. ,pos)))
  7463. (condition-case nil (eval cmd)
  7464. (error (progn (widen) (eval cmd))))))
  7465. ((string= type "tree-match")
  7466. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7467. ((string= type "file")
  7468. (if (string-match "::\\([0-9]+\\)\\'" path)
  7469. (setq line (string-to-number (match-string 1 path))
  7470. path (substring path 0 (match-beginning 0)))
  7471. (if (string-match "::\\(.+\\)\\'" path)
  7472. (setq search (match-string 1 path)
  7473. path (substring path 0 (match-beginning 0)))))
  7474. (if (string-match "[*?{]" (file-name-nondirectory path))
  7475. (dired path)
  7476. (org-open-file path in-emacs line search)))
  7477. ((string= type "news")
  7478. (require 'org-gnus)
  7479. (org-gnus-follow-link path))
  7480. ((string= type "shell")
  7481. (let ((cmd path))
  7482. (if (or (not org-confirm-shell-link-function)
  7483. (funcall org-confirm-shell-link-function
  7484. (format "Execute \"%s\" in shell? "
  7485. (org-add-props cmd nil
  7486. 'face 'org-warning))))
  7487. (progn
  7488. (message "Executing %s" cmd)
  7489. (shell-command cmd))
  7490. (error "Abort"))))
  7491. ((string= type "elisp")
  7492. (let ((cmd path))
  7493. (if (or (not org-confirm-elisp-link-function)
  7494. (funcall org-confirm-elisp-link-function
  7495. (format "Execute \"%s\" as elisp? "
  7496. (org-add-props cmd nil
  7497. 'face 'org-warning))))
  7498. (message "%s => %s" cmd
  7499. (if (equal (string-to-char cmd) ?\()
  7500. (eval (read cmd))
  7501. (call-interactively (read cmd))))
  7502. (error "Abort"))))
  7503. (t
  7504. (browse-url-at-point)))))))
  7505. (move-marker org-open-link-marker nil)
  7506. (run-hook-with-args 'org-follow-link-hook))
  7507. (defun org-offer-links-in-entry (&optional nth zero)
  7508. "Offer links in the current entry and follow the selected link.
  7509. If there is only one link, follow it immediately as well.
  7510. If NTH is an integer, immediately pick the NTH link found.
  7511. If ZERO is a string, check also this string for a link, and if
  7512. there is one, offer it as link number zero."
  7513. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7514. "\\(" org-angle-link-re "\\)\\|"
  7515. "\\(" org-plain-link-re "\\)"))
  7516. (cnt ?0)
  7517. (in-emacs (if (integerp nth) nil nth))
  7518. have-zero end links link c)
  7519. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7520. (push (match-string 0 zero) links)
  7521. (setq cnt (1- cnt) have-zero t))
  7522. (save-excursion
  7523. (org-back-to-heading t)
  7524. (setq end (save-excursion (outline-next-heading) (point)))
  7525. (while (re-search-forward re end t)
  7526. (push (match-string 0) links))
  7527. (setq links (org-uniquify (reverse links))))
  7528. (cond
  7529. ((null links)
  7530. (message "No links"))
  7531. ((equal (length links) 1)
  7532. (setq link (car links)))
  7533. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7534. (setq link (nth (if have-zero nth (1- nth)) links)))
  7535. (t ; we have to select a link
  7536. (save-excursion
  7537. (save-window-excursion
  7538. (delete-other-windows)
  7539. (with-output-to-temp-buffer "*Select Link*"
  7540. (mapc (lambda (l)
  7541. (if (not (string-match org-bracket-link-regexp l))
  7542. (princ (format "[%c] %s\n" (incf cnt)
  7543. (org-remove-angle-brackets l)))
  7544. (if (match-end 3)
  7545. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7546. (match-string 3 l) (match-string 1 l)))
  7547. (princ (format "[%c] %s\n" (incf cnt)
  7548. (match-string 1 l))))))
  7549. links))
  7550. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7551. (message "Select link to open:")
  7552. (setq c (read-char-exclusive))
  7553. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7554. (when (equal c ?q) (error "Abort"))
  7555. (setq nth (- c ?0))
  7556. (if have-zero (setq nth (1+ nth)))
  7557. (unless (and (integerp nth) (>= (length links) nth))
  7558. (error "Invalid link selection"))
  7559. (setq link (nth (1- nth) links))))
  7560. (if link
  7561. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7562. nil)))
  7563. ;;;; Time estimates
  7564. (defun org-get-effort (&optional pom)
  7565. "Get the effort estimate for the current entry."
  7566. (org-entry-get pom org-effort-property))
  7567. ;;; File search
  7568. (defvar org-create-file-search-functions nil
  7569. "List of functions to construct the right search string for a file link.
  7570. These functions are called in turn with point at the location to
  7571. which the link should point.
  7572. A function in the hook should first test if it would like to
  7573. handle this file type, for example by checking the major-mode or
  7574. the file extension. If it decides not to handle this file, it
  7575. should just return nil to give other functions a chance. If it
  7576. does handle the file, it must return the search string to be used
  7577. when following the link. The search string will be part of the
  7578. file link, given after a double colon, and `org-open-at-point'
  7579. will automatically search for it. If special measures must be
  7580. taken to make the search successful, another function should be
  7581. added to the companion hook `org-execute-file-search-functions',
  7582. which see.
  7583. A function in this hook may also use `setq' to set the variable
  7584. `description' to provide a suggestion for the descriptive text to
  7585. be used for this link when it gets inserted into an Org-mode
  7586. buffer with \\[org-insert-link].")
  7587. (defvar org-execute-file-search-functions nil
  7588. "List of functions to execute a file search triggered by a link.
  7589. Functions added to this hook must accept a single argument, the
  7590. search string that was part of the file link, the part after the
  7591. double colon. The function must first check if it would like to
  7592. handle this search, for example by checking the major-mode or the
  7593. file extension. If it decides not to handle this search, it
  7594. should just return nil to give other functions a chance. If it
  7595. does handle the search, it must return a non-nil value to keep
  7596. other functions from trying.
  7597. Each function can access the current prefix argument through the
  7598. variable `current-prefix-argument'. Note that a single prefix is
  7599. used to force opening a link in Emacs, so it may be good to only
  7600. use a numeric or double prefix to guide the search function.
  7601. In case this is needed, a function in this hook can also restore
  7602. the window configuration before `org-open-at-point' was called using:
  7603. (set-window-configuration org-window-config-before-follow-link)")
  7604. (defun org-link-search (s &optional type avoid-pos)
  7605. "Search for a link search option.
  7606. If S is surrounded by forward slashes, it is interpreted as a
  7607. regular expression. In org-mode files, this will create an `org-occur'
  7608. sparse tree. In ordinary files, `occur' will be used to list matches.
  7609. If the current buffer is in `dired-mode', grep will be used to search
  7610. in all files. If AVOID-POS is given, ignore matches near that position."
  7611. (let ((case-fold-search t)
  7612. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7613. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7614. (append '(("") (" ") ("\t") ("\n"))
  7615. org-emphasis-alist)
  7616. "\\|") "\\)"))
  7617. (pos (point))
  7618. (pre nil) (post nil)
  7619. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7620. (cond
  7621. ;; First check if there are any special
  7622. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7623. ;; Now try the builtin stuff
  7624. ((and (equal (string-to-char s0) ?#)
  7625. (> (length s0) 1)
  7626. (save-excursion
  7627. (goto-char (point-min))
  7628. (and
  7629. (re-search-forward
  7630. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7631. (setq type 'dedicated
  7632. pos (match-beginning 0))))
  7633. ;; There is an exact target for this
  7634. (goto-char pos)
  7635. (org-back-to-heading t)))
  7636. ((save-excursion
  7637. (goto-char (point-min))
  7638. (and
  7639. (re-search-forward
  7640. (concat "<<" (regexp-quote s0) ">>") nil t)
  7641. (setq type 'dedicated
  7642. pos (match-beginning 0))))
  7643. ;; There is an exact target for this
  7644. (goto-char pos))
  7645. ((and (string-match "^(\\(.*\\))$" s0)
  7646. (save-excursion
  7647. (goto-char (point-min))
  7648. (and
  7649. (re-search-forward
  7650. (concat "[^[]" (regexp-quote
  7651. (format org-coderef-label-format
  7652. (match-string 1 s0))))
  7653. nil t)
  7654. (setq type 'dedicated
  7655. pos (1+ (match-beginning 0))))))
  7656. ;; There is a coderef target for this
  7657. (goto-char pos))
  7658. ((string-match "^/\\(.*\\)/$" s)
  7659. ;; A regular expression
  7660. (cond
  7661. ((org-mode-p)
  7662. (org-occur (match-string 1 s)))
  7663. ;;((eq major-mode 'dired-mode)
  7664. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7665. (t (org-do-occur (match-string 1 s)))))
  7666. (t
  7667. ;; A normal search strings
  7668. (when (equal (string-to-char s) ?*)
  7669. ;; Anchor on headlines, post may include tags.
  7670. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7671. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7672. s (substring s 1)))
  7673. (remove-text-properties
  7674. 0 (length s)
  7675. '(face nil mouse-face nil keymap nil fontified nil) s)
  7676. ;; Make a series of regular expressions to find a match
  7677. (setq words (org-split-string s "[ \n\r\t]+")
  7678. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7679. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7680. "\\)" markers)
  7681. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7682. re2a (concat "[ \t\r\n]" re2a_)
  7683. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7684. re4 (concat "[^a-zA-Z_]" re4_)
  7685. re1 (concat pre re2 post)
  7686. re3 (concat pre (if pre re4_ re4) post)
  7687. re5 (concat pre ".*" re4)
  7688. re2 (concat pre re2)
  7689. re2a (concat pre (if pre re2a_ re2a))
  7690. re4 (concat pre (if pre re4_ re4))
  7691. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7692. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7693. re5 "\\)"
  7694. ))
  7695. (cond
  7696. ((eq type 'org-occur) (org-occur reall))
  7697. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7698. (t (goto-char (point-min))
  7699. (setq type 'fuzzy)
  7700. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7701. (org-search-not-self 1 re1 nil t)
  7702. (org-search-not-self 1 re2 nil t)
  7703. (org-search-not-self 1 re2a nil t)
  7704. (org-search-not-self 1 re3 nil t)
  7705. (org-search-not-self 1 re4 nil t)
  7706. (org-search-not-self 1 re5 nil t)
  7707. )
  7708. (goto-char (match-beginning 1))
  7709. (goto-char pos)
  7710. (error "No match")))))
  7711. (t
  7712. ;; Normal string-search
  7713. (goto-char (point-min))
  7714. (if (search-forward s nil t)
  7715. (goto-char (match-beginning 0))
  7716. (error "No match"))))
  7717. (and (org-mode-p) (org-show-context 'link-search))
  7718. type))
  7719. (defun org-search-not-self (group &rest args)
  7720. "Execute `re-search-forward', but only accept matches that do not
  7721. enclose the position of `org-open-link-marker'."
  7722. (let ((m org-open-link-marker))
  7723. (catch 'exit
  7724. (while (apply 're-search-forward args)
  7725. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7726. (goto-char (match-end group))
  7727. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7728. (> (match-beginning 0) (marker-position m))
  7729. (< (match-end 0) (marker-position m)))
  7730. (save-match-data
  7731. (or (not (org-in-regexp
  7732. org-bracket-link-analytic-regexp 1))
  7733. (not (match-end 4)) ; no description
  7734. (and (<= (match-beginning 4) (point))
  7735. (>= (match-end 4) (point))))))
  7736. (throw 'exit (point))))))))
  7737. (defun org-get-buffer-for-internal-link (buffer)
  7738. "Return a buffer to be used for displaying the link target of internal links."
  7739. (cond
  7740. ((not org-display-internal-link-with-indirect-buffer)
  7741. buffer)
  7742. ((string-match "(Clone)$" (buffer-name buffer))
  7743. (message "Buffer is already a clone, not making another one")
  7744. ;; we also do not modify visibility in this case
  7745. buffer)
  7746. (t ; make a new indirect buffer for displaying the link
  7747. (let* ((bn (buffer-name buffer))
  7748. (ibn (concat bn "(Clone)"))
  7749. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7750. (with-current-buffer ib (org-overview))
  7751. ib))))
  7752. (defun org-do-occur (regexp &optional cleanup)
  7753. "Call the Emacs command `occur'.
  7754. If CLEANUP is non-nil, remove the printout of the regular expression
  7755. in the *Occur* buffer. This is useful if the regex is long and not useful
  7756. to read."
  7757. (occur regexp)
  7758. (when cleanup
  7759. (let ((cwin (selected-window)) win beg end)
  7760. (when (setq win (get-buffer-window "*Occur*"))
  7761. (select-window win))
  7762. (goto-char (point-min))
  7763. (when (re-search-forward "match[a-z]+" nil t)
  7764. (setq beg (match-end 0))
  7765. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7766. (setq end (1- (match-beginning 0)))))
  7767. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7768. (goto-char (point-min))
  7769. (select-window cwin))))
  7770. ;;; The mark ring for links jumps
  7771. (defvar org-mark-ring nil
  7772. "Mark ring for positions before jumps in Org-mode.")
  7773. (defvar org-mark-ring-last-goto nil
  7774. "Last position in the mark ring used to go back.")
  7775. ;; Fill and close the ring
  7776. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7777. (loop for i from 1 to org-mark-ring-length do
  7778. (push (make-marker) org-mark-ring))
  7779. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7780. org-mark-ring)
  7781. (defun org-mark-ring-push (&optional pos buffer)
  7782. "Put the current position or POS into the mark ring and rotate it."
  7783. (interactive)
  7784. (setq pos (or pos (point)))
  7785. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7786. (move-marker (car org-mark-ring)
  7787. (or pos (point))
  7788. (or buffer (current-buffer)))
  7789. (message "%s"
  7790. (substitute-command-keys
  7791. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7792. (defun org-mark-ring-goto (&optional n)
  7793. "Jump to the previous position in the mark ring.
  7794. With prefix arg N, jump back that many stored positions. When
  7795. called several times in succession, walk through the entire ring.
  7796. Org-mode commands jumping to a different position in the current file,
  7797. or to another Org-mode file, automatically push the old position
  7798. onto the ring."
  7799. (interactive "p")
  7800. (let (p m)
  7801. (if (eq last-command this-command)
  7802. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7803. (setq p org-mark-ring))
  7804. (setq org-mark-ring-last-goto p)
  7805. (setq m (car p))
  7806. (switch-to-buffer (marker-buffer m))
  7807. (goto-char m)
  7808. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7809. (defun org-remove-angle-brackets (s)
  7810. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7811. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7812. s)
  7813. (defun org-add-angle-brackets (s)
  7814. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7815. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7816. s)
  7817. (defun org-remove-double-quotes (s)
  7818. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7819. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7820. s)
  7821. ;;; Following specific links
  7822. (defun org-follow-timestamp-link ()
  7823. (cond
  7824. ((org-at-date-range-p t)
  7825. (let ((org-agenda-start-on-weekday)
  7826. (t1 (match-string 1))
  7827. (t2 (match-string 2)))
  7828. (setq t1 (time-to-days (org-time-string-to-time t1))
  7829. t2 (time-to-days (org-time-string-to-time t2)))
  7830. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7831. ((org-at-timestamp-p t)
  7832. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7833. (substring (match-string 1) 0 10)))
  7834. 1))
  7835. (t (error "This should not happen"))))
  7836. ;;; Following file links
  7837. (defvar org-wait nil)
  7838. (defun org-open-file (path &optional in-emacs line search)
  7839. "Open the file at PATH.
  7840. First, this expands any special file name abbreviations. Then the
  7841. configuration variable `org-file-apps' is checked if it contains an
  7842. entry for this file type, and if yes, the corresponding command is launched.
  7843. If no application is found, Emacs simply visits the file.
  7844. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7845. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7846. and o use an external application to visit the file.
  7847. Optional LINE specifies a line to go to, optional SEARCH a string to
  7848. search for. If LINE or SEARCH is given, the file will always be
  7849. opened in Emacs.
  7850. If the file does not exist, an error is thrown."
  7851. (setq in-emacs (or in-emacs line search))
  7852. (let* ((file (if (equal path "")
  7853. buffer-file-name
  7854. (substitute-in-file-name (expand-file-name path))))
  7855. (apps (append org-file-apps (org-default-apps)))
  7856. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7857. (dirp (if remp nil (file-directory-p file)))
  7858. (file (if (and dirp org-open-directory-means-index-dot-org)
  7859. (concat (file-name-as-directory file) "index.org")
  7860. file))
  7861. (a-m-a-p (assq 'auto-mode apps))
  7862. (dfile (downcase file))
  7863. (old-buffer (current-buffer))
  7864. (old-pos (point))
  7865. (old-mode major-mode)
  7866. ext cmd)
  7867. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7868. (setq ext (match-string 1 dfile))
  7869. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7870. (setq ext (match-string 1 dfile))))
  7871. (cond
  7872. ((equal in-emacs '(16))
  7873. (setq cmd (cdr (assoc 'system apps))))
  7874. (in-emacs (setq cmd 'emacs))
  7875. (t
  7876. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7877. (and dirp (cdr (assoc 'directory apps)))
  7878. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7879. 'string-match)
  7880. (cdr (assoc ext apps))
  7881. (cdr (assoc t apps))))))
  7882. (when (eq cmd 'system)
  7883. (setq cmd (cdr (assoc 'system apps))))
  7884. (when (eq cmd 'default)
  7885. (setq cmd (cdr (assoc t apps))))
  7886. (when (eq cmd 'mailcap)
  7887. (require 'mailcap)
  7888. (mailcap-parse-mailcaps)
  7889. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7890. (command (mailcap-mime-info mime-type)))
  7891. (if (stringp command)
  7892. (setq cmd command)
  7893. (setq cmd 'emacs))))
  7894. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7895. (not (file-exists-p file))
  7896. (not org-open-non-existing-files))
  7897. (error "No such file: %s" file))
  7898. (cond
  7899. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7900. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7901. (while (string-match "['\"]%s['\"]" cmd)
  7902. (setq cmd (replace-match "%s" t t cmd)))
  7903. (while (string-match "%s" cmd)
  7904. (setq cmd (replace-match
  7905. (save-match-data
  7906. (shell-quote-argument
  7907. (convert-standard-filename file)))
  7908. t t cmd)))
  7909. (save-window-excursion
  7910. (start-process-shell-command cmd nil cmd)
  7911. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7912. ))
  7913. ((or (stringp cmd)
  7914. (eq cmd 'emacs))
  7915. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7916. (widen)
  7917. (if line (org-goto-line line)
  7918. (if search (org-link-search search))))
  7919. ((consp cmd)
  7920. (let ((file (convert-standard-filename file)))
  7921. (eval cmd)))
  7922. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7923. (and (org-mode-p) (eq old-mode 'org-mode)
  7924. (or (not (equal old-buffer (current-buffer)))
  7925. (not (equal old-pos (point))))
  7926. (org-mark-ring-push old-pos old-buffer))))
  7927. (defun org-default-apps ()
  7928. "Return the default applications for this operating system."
  7929. (cond
  7930. ((eq system-type 'darwin)
  7931. org-file-apps-defaults-macosx)
  7932. ((eq system-type 'windows-nt)
  7933. org-file-apps-defaults-windowsnt)
  7934. (t org-file-apps-defaults-gnu)))
  7935. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7936. "Convert extensions to regular expressions in the cars of LIST.
  7937. Also, weed out any non-string entries, because the return value is used
  7938. only for regexp matching.
  7939. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7940. point to the symbol `emacs', indicating that the file should
  7941. be opened in Emacs."
  7942. (append
  7943. (delq nil
  7944. (mapcar (lambda (x)
  7945. (if (not (stringp (car x)))
  7946. nil
  7947. (if (string-match "\\W" (car x))
  7948. x
  7949. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7950. list))
  7951. (if add-auto-mode
  7952. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7953. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7954. (defun org-file-remote-p (file)
  7955. "Test whether FILE specifies a location on a remote system.
  7956. Return non-nil if the location is indeed remote.
  7957. For example, the filename \"/user@host:/foo\" specifies a location
  7958. on the system \"/user@host:\"."
  7959. (cond ((fboundp 'file-remote-p)
  7960. (file-remote-p file))
  7961. ((fboundp 'tramp-handle-file-remote-p)
  7962. (tramp-handle-file-remote-p file))
  7963. ((and (boundp 'ange-ftp-name-format)
  7964. (string-match (car ange-ftp-name-format) file))
  7965. t)
  7966. (t nil)))
  7967. ;;;; Refiling
  7968. (defun org-get-org-file ()
  7969. "Read a filename, with default directory `org-directory'."
  7970. (let ((default (or org-default-notes-file remember-data-file)))
  7971. (read-file-name (format "File name [%s]: " default)
  7972. (file-name-as-directory org-directory)
  7973. default)))
  7974. (defun org-notes-order-reversed-p ()
  7975. "Check if the current file should receive notes in reversed order."
  7976. (cond
  7977. ((not org-reverse-note-order) nil)
  7978. ((eq t org-reverse-note-order) t)
  7979. ((not (listp org-reverse-note-order)) nil)
  7980. (t (catch 'exit
  7981. (let ((all org-reverse-note-order)
  7982. entry)
  7983. (while (setq entry (pop all))
  7984. (if (string-match (car entry) buffer-file-name)
  7985. (throw 'exit (cdr entry))))
  7986. nil)))))
  7987. (defvar org-refile-target-table nil
  7988. "The list of refile targets, created by `org-refile'.")
  7989. (defvar org-agenda-new-buffers nil
  7990. "Buffers created to visit agenda files.")
  7991. (defun org-get-refile-targets (&optional default-buffer)
  7992. "Produce a table with refile targets."
  7993. (let ((case-fold-search nil)
  7994. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7995. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7996. targets txt re files f desc descre fast-path-p level pos0)
  7997. (message "Getting targets...")
  7998. (with-current-buffer (or default-buffer (current-buffer))
  7999. (while (setq entry (pop entries))
  8000. (setq files (car entry) desc (cdr entry))
  8001. (setq fast-path-p nil)
  8002. (cond
  8003. ((null files) (setq files (list (current-buffer))))
  8004. ((eq files 'org-agenda-files)
  8005. (setq files (org-agenda-files 'unrestricted)))
  8006. ((and (symbolp files) (fboundp files))
  8007. (setq files (funcall files)))
  8008. ((and (symbolp files) (boundp files))
  8009. (setq files (symbol-value files))))
  8010. (if (stringp files) (setq files (list files)))
  8011. (cond
  8012. ((eq (car desc) :tag)
  8013. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8014. ((eq (car desc) :todo)
  8015. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8016. ((eq (car desc) :regexp)
  8017. (setq descre (cdr desc)))
  8018. ((eq (car desc) :level)
  8019. (setq descre (concat "^\\*\\{" (number-to-string
  8020. (if org-odd-levels-only
  8021. (1- (* 2 (cdr desc)))
  8022. (cdr desc)))
  8023. "\\}[ \t]")))
  8024. ((eq (car desc) :maxlevel)
  8025. (setq fast-path-p t)
  8026. (setq descre (concat "^\\*\\{1," (number-to-string
  8027. (if org-odd-levels-only
  8028. (1- (* 2 (cdr desc)))
  8029. (cdr desc)))
  8030. "\\}[ \t]")))
  8031. (t (error "Bad refiling target description %s" desc)))
  8032. (while (setq f (pop files))
  8033. (with-current-buffer
  8034. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8035. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8036. (setq f (expand-file-name f))
  8037. (if (eq org-refile-use-outline-path 'file)
  8038. (push (list (file-name-nondirectory f) f nil nil) targets))
  8039. (save-excursion
  8040. (save-restriction
  8041. (widen)
  8042. (goto-char (point-min))
  8043. (while (re-search-forward descre nil t)
  8044. (goto-char (setq pos0 (point-at-bol)))
  8045. (catch 'next
  8046. (when org-refile-target-verify-function
  8047. (save-match-data
  8048. (or (funcall org-refile-target-verify-function)
  8049. (throw 'next t))))
  8050. (when (looking-at org-complex-heading-regexp)
  8051. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8052. txt (org-link-display-format (match-string 4))
  8053. re (concat "^" (regexp-quote
  8054. (buffer-substring (match-beginning 1)
  8055. (match-end 4)))))
  8056. (if (match-end 5) (setq re (concat re "[ \t]+"
  8057. (regexp-quote
  8058. (match-string 5)))))
  8059. (setq re (concat re "[ \t]*$"))
  8060. (when org-refile-use-outline-path
  8061. (setq txt (mapconcat 'org-protect-slash
  8062. (append
  8063. (if (eq org-refile-use-outline-path 'file)
  8064. (list (file-name-nondirectory
  8065. (buffer-file-name (buffer-base-buffer))))
  8066. (if (eq org-refile-use-outline-path 'full-file-path)
  8067. (list (buffer-file-name (buffer-base-buffer)))))
  8068. (org-get-outline-path fast-path-p level txt)
  8069. (list txt))
  8070. "/")))
  8071. (push (list txt f re (point)) targets)))
  8072. (when (= (point) pos0)
  8073. ;; verification function has not moved point
  8074. (goto-char (point-at-eol))))))))))
  8075. (message "Getting targets...done")
  8076. (nreverse targets)))
  8077. (defun org-protect-slash (s)
  8078. (while (string-match "/" s)
  8079. (setq s (replace-match "\\" t t s)))
  8080. s)
  8081. (defvar org-olpa (make-vector 20 nil))
  8082. (defun org-get-outline-path (&optional fastp level heading)
  8083. "Return the outline path to the current entry, as a list."
  8084. (if fastp
  8085. (progn
  8086. (if (> level 19)
  8087. (error "Outline path failure, more than 19 levels."))
  8088. (loop for i from level upto 19 do
  8089. (aset org-olpa i nil))
  8090. (prog1
  8091. (delq nil (append org-olpa nil))
  8092. (aset org-olpa level heading)))
  8093. (let (rtn)
  8094. (save-excursion
  8095. (while (org-up-heading-safe)
  8096. (when (looking-at org-complex-heading-regexp)
  8097. (push (org-match-string-no-properties 4) rtn)))
  8098. rtn))))
  8099. (defvar org-refile-history nil
  8100. "History for refiling operations.")
  8101. (defvar org-after-refile-insert-hook nil
  8102. "Hook run after `org-refile' has inserted its stuff at the new location.
  8103. Note that this is still *before* the stuff will be removed from
  8104. the *old* location.")
  8105. (defun org-refile (&optional goto default-buffer rfloc)
  8106. "Move the entry at point to another heading.
  8107. The list of target headings is compiled using the information in
  8108. `org-refile-targets', which see. This list is created before each use
  8109. and will therefore always be up-to-date.
  8110. At the target location, the entry is filed as a subitem of the target heading.
  8111. Depending on `org-reverse-note-order', the new subitem will either be the
  8112. first or the last subitem.
  8113. If there is an active region, all entries in that region will be moved.
  8114. However, the region must fulfil the requirement that the first heading
  8115. is the first one sets the top-level of the moved text - at most siblings
  8116. below it are allowed.
  8117. With prefix arg GOTO, the command will only visit the target location,
  8118. not actually move anything.
  8119. With a double prefix `C-u C-u', go to the location where the last refiling
  8120. operation has put the subtree.
  8121. With a prefix argument of `2', refile to the running clock.
  8122. RFLOC can be a refile location obtained in a different way.
  8123. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8124. (interactive "P")
  8125. (let* ((cbuf (current-buffer))
  8126. (regionp (org-region-active-p))
  8127. (region-start (and regionp (region-beginning)))
  8128. (region-end (and regionp (region-end)))
  8129. (region-length (and regionp (- region-end region-start)))
  8130. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8131. pos it nbuf file re level reversed)
  8132. (when regionp
  8133. (goto-char region-start)
  8134. (or (bolp) (goto-char (point-at-bol)))
  8135. (setq region-start (point))
  8136. (unless (org-kill-is-subtree-p
  8137. (buffer-substring region-start region-end))
  8138. (error "The region is not a (sequence of) subtree(s)")))
  8139. (if (equal goto '(16))
  8140. (org-refile-goto-last-stored)
  8141. (when (or
  8142. (and (equal goto 2)
  8143. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8144. (prog1
  8145. (setq it (list (or org-clock-heading "running clock")
  8146. (buffer-file-name
  8147. (marker-buffer org-clock-hd-marker))
  8148. ""
  8149. (marker-position org-clock-hd-marker)))
  8150. (setq goto nil)))
  8151. (setq it (or rfloc
  8152. (save-excursion
  8153. (org-refile-get-location
  8154. (if goto "Goto: " "Refile to: ") default-buffer
  8155. org-refile-allow-creating-parent-nodes)))))
  8156. (setq file (nth 1 it)
  8157. re (nth 2 it)
  8158. pos (nth 3 it))
  8159. (if (and (not goto)
  8160. pos
  8161. (equal (buffer-file-name) file)
  8162. (if regionp
  8163. (and (>= pos region-start)
  8164. (<= pos region-end))
  8165. (and (>= pos (point))
  8166. (< pos (save-excursion
  8167. (org-end-of-subtree t t))))))
  8168. (error "Cannot refile to position inside the tree or region"))
  8169. (setq nbuf (or (find-buffer-visiting file)
  8170. (find-file-noselect file)))
  8171. (if goto
  8172. (progn
  8173. (switch-to-buffer nbuf)
  8174. (goto-char pos)
  8175. (org-show-context 'org-goto))
  8176. (if regionp
  8177. (progn
  8178. (org-kill-new (buffer-substring region-start region-end))
  8179. (org-save-markers-in-region region-start region-end))
  8180. (org-copy-subtree 1 nil t))
  8181. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8182. (find-file-noselect file)))
  8183. (setq reversed (org-notes-order-reversed-p))
  8184. (save-excursion
  8185. (save-restriction
  8186. (widen)
  8187. (if pos
  8188. (progn
  8189. (goto-char pos)
  8190. (looking-at outline-regexp)
  8191. (setq level (org-get-valid-level (funcall outline-level) 1))
  8192. (goto-char
  8193. (if reversed
  8194. (or (outline-next-heading) (point-max))
  8195. (or (save-excursion (org-get-next-sibling))
  8196. (org-end-of-subtree t t)
  8197. (point-max)))))
  8198. (setq level 1)
  8199. (if (not reversed)
  8200. (goto-char (point-max))
  8201. (goto-char (point-min))
  8202. (or (outline-next-heading) (goto-char (point-max)))))
  8203. (if (not (bolp)) (newline))
  8204. (bookmark-set "org-refile-last-stored")
  8205. (org-paste-subtree level)
  8206. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8207. (run-hooks 'org-after-refile-insert-hook))))
  8208. (if regionp
  8209. (delete-region (point) (+ (point) region-length))
  8210. (org-cut-subtree))
  8211. (when (featurep 'org-inlinetask)
  8212. (org-inlinetask-remove-END-maybe))
  8213. (setq org-markers-to-move nil)
  8214. (message "Refiled to \"%s\"" (car it))))))
  8215. (org-reveal))
  8216. (defun org-refile-goto-last-stored ()
  8217. "Go to the location where the last refile was stored."
  8218. (interactive)
  8219. (bookmark-jump "org-refile-last-stored")
  8220. (message "This is the location of the last refile"))
  8221. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8222. "Prompt the user for a refile location, using PROMPT."
  8223. (let ((org-refile-targets org-refile-targets)
  8224. (org-refile-use-outline-path org-refile-use-outline-path))
  8225. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8226. (unless org-refile-target-table
  8227. (error "No refile targets"))
  8228. (let* ((cbuf (current-buffer))
  8229. (partial-completion-mode nil)
  8230. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8231. (cfunc (if (and org-refile-use-outline-path
  8232. org-outline-path-complete-in-steps)
  8233. 'org-olpath-completing-read
  8234. 'org-icompleting-read))
  8235. (extra (if org-refile-use-outline-path "/" ""))
  8236. (filename (and cfn (expand-file-name cfn)))
  8237. (tbl (mapcar
  8238. (lambda (x)
  8239. (if (and (not (member org-refile-use-outline-path
  8240. '(file full-file-path)))
  8241. (not (equal filename (nth 1 x))))
  8242. (cons (concat (car x) extra " ("
  8243. (file-name-nondirectory (nth 1 x)) ")")
  8244. (cdr x))
  8245. (cons (concat (car x) extra) (cdr x))))
  8246. org-refile-target-table))
  8247. (completion-ignore-case t)
  8248. pa answ parent-target child parent old-hist)
  8249. (setq old-hist org-refile-history)
  8250. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8251. nil 'org-refile-history))
  8252. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8253. (if pa
  8254. (progn
  8255. (when (or (not org-refile-history)
  8256. (not (eq old-hist org-refile-history))
  8257. (not (equal (car pa) (car org-refile-history))))
  8258. (setq org-refile-history
  8259. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8260. org-refile-history
  8261. (cdr org-refile-history))))
  8262. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8263. (pop org-refile-history)))
  8264. pa)
  8265. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8266. (setq parent (match-string 1 answ)
  8267. child (match-string 2 answ))
  8268. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8269. (when (and parent-target
  8270. (or (eq new-nodes t)
  8271. (and (eq new-nodes 'confirm)
  8272. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8273. (org-refile-new-child parent-target child))))))
  8274. (defun org-refile-new-child (parent-target child)
  8275. "Use refile target PARENT-TARGET to add new CHILD below it."
  8276. (unless parent-target
  8277. (error "Cannot find parent for new node"))
  8278. (let ((file (nth 1 parent-target))
  8279. (pos (nth 3 parent-target))
  8280. level)
  8281. (with-current-buffer (or (find-buffer-visiting file)
  8282. (find-file-noselect file))
  8283. (save-excursion
  8284. (save-restriction
  8285. (widen)
  8286. (if pos
  8287. (goto-char pos)
  8288. (goto-char (point-max))
  8289. (if (not (bolp)) (newline)))
  8290. (when (looking-at outline-regexp)
  8291. (setq level (funcall outline-level))
  8292. (org-end-of-subtree t t))
  8293. (org-back-over-empty-lines)
  8294. (insert "\n" (make-string
  8295. (if pos (org-get-valid-level level 1) 1) ?*)
  8296. " " child "\n")
  8297. (beginning-of-line 0)
  8298. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8299. (defun org-olpath-completing-read (prompt collection &rest args)
  8300. "Read an outline path like a file name."
  8301. (let ((thetable collection)
  8302. (org-completion-use-ido nil) ; does not work with ido.
  8303. (org-completion-use-iswitchb nil)) ; or iswitchb
  8304. (apply
  8305. 'org-icompleting-read prompt
  8306. (lambda (string predicate &optional flag)
  8307. (let (rtn r f (l (length string)))
  8308. (cond
  8309. ((eq flag nil)
  8310. ;; try completion
  8311. (try-completion string thetable))
  8312. ((eq flag t)
  8313. ;; all-completions
  8314. (setq rtn (all-completions string thetable predicate))
  8315. (mapcar
  8316. (lambda (x)
  8317. (setq r (substring x l))
  8318. (if (string-match " ([^)]*)$" x)
  8319. (setq f (match-string 0 x))
  8320. (setq f ""))
  8321. (if (string-match "/" r)
  8322. (concat string (substring r 0 (match-end 0)) f)
  8323. x))
  8324. rtn))
  8325. ((eq flag 'lambda)
  8326. ;; exact match?
  8327. (assoc string thetable)))
  8328. ))
  8329. args)))
  8330. ;;;; Dynamic blocks
  8331. (defun org-find-dblock (name)
  8332. "Find the first dynamic block with name NAME in the buffer.
  8333. If not found, stay at current position and return nil."
  8334. (let (pos)
  8335. (save-excursion
  8336. (goto-char (point-min))
  8337. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8338. nil t)
  8339. (match-beginning 0))))
  8340. (if pos (goto-char pos))
  8341. pos))
  8342. (defconst org-dblock-start-re
  8343. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8344. "Matches the start line of a dynamic block, with parameters.")
  8345. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8346. "Matches the end of a dynamic block.")
  8347. (defun org-create-dblock (plist)
  8348. "Create a dynamic block section, with parameters taken from PLIST.
  8349. PLIST must contain a :name entry which is used as name of the block."
  8350. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8351. (end-of-line 1)
  8352. (newline))
  8353. (let ((col (current-column))
  8354. (name (plist-get plist :name)))
  8355. (insert "#+BEGIN: " name)
  8356. (while plist
  8357. (if (eq (car plist) :name)
  8358. (setq plist (cddr plist))
  8359. (insert " " (prin1-to-string (pop plist)))))
  8360. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8361. (beginning-of-line -2)))
  8362. (defun org-prepare-dblock ()
  8363. "Prepare dynamic block for refresh.
  8364. This empties the block, puts the cursor at the insert position and returns
  8365. the property list including an extra property :name with the block name."
  8366. (unless (looking-at org-dblock-start-re)
  8367. (error "Not at a dynamic block"))
  8368. (let* ((begdel (1+ (match-end 0)))
  8369. (name (org-no-properties (match-string 1)))
  8370. (params (append (list :name name)
  8371. (read (concat "(" (match-string 3) ")")))))
  8372. (save-excursion
  8373. (beginning-of-line 1)
  8374. (skip-chars-forward " \t")
  8375. (setq params (plist-put params :indentation-column (current-column))))
  8376. (unless (re-search-forward org-dblock-end-re nil t)
  8377. (error "Dynamic block not terminated"))
  8378. (setq params
  8379. (append params
  8380. (list :content (buffer-substring
  8381. begdel (match-beginning 0)))))
  8382. (delete-region begdel (match-beginning 0))
  8383. (goto-char begdel)
  8384. (open-line 1)
  8385. params))
  8386. (defun org-map-dblocks (&optional command)
  8387. "Apply COMMAND to all dynamic blocks in the current buffer.
  8388. If COMMAND is not given, use `org-update-dblock'."
  8389. (let ((cmd (or command 'org-update-dblock))
  8390. pos)
  8391. (save-excursion
  8392. (goto-char (point-min))
  8393. (while (re-search-forward org-dblock-start-re nil t)
  8394. (goto-char (setq pos (match-beginning 0)))
  8395. (condition-case nil
  8396. (funcall cmd)
  8397. (error (message "Error during update of dynamic block")))
  8398. (goto-char pos)
  8399. (unless (re-search-forward org-dblock-end-re nil t)
  8400. (error "Dynamic block not terminated"))))))
  8401. (defun org-dblock-update (&optional arg)
  8402. "User command for updating dynamic blocks.
  8403. Update the dynamic block at point. With prefix ARG, update all dynamic
  8404. blocks in the buffer."
  8405. (interactive "P")
  8406. (if arg
  8407. (org-update-all-dblocks)
  8408. (or (looking-at org-dblock-start-re)
  8409. (org-beginning-of-dblock))
  8410. (org-update-dblock)))
  8411. (defun org-update-dblock ()
  8412. "Update the dynamic block at point
  8413. This means to empty the block, parse for parameters and then call
  8414. the correct writing function."
  8415. (save-window-excursion
  8416. (let* ((pos (point))
  8417. (line (org-current-line))
  8418. (params (org-prepare-dblock))
  8419. (name (plist-get params :name))
  8420. (indent (plist-get params :indentation-column))
  8421. (cmd (intern (concat "org-dblock-write:" name))))
  8422. (message "Updating dynamic block `%s' at line %d..." name line)
  8423. (funcall cmd params)
  8424. (message "Updating dynamic block `%s' at line %d...done" name line)
  8425. (goto-char pos)
  8426. (when (and indent (> indent 0))
  8427. (setq indent (make-string indent ?\ ))
  8428. (save-excursion
  8429. (org-beginning-of-dblock)
  8430. (forward-line 1)
  8431. (while (not (looking-at org-dblock-end-re))
  8432. (insert indent)
  8433. (beginning-of-line 2))
  8434. (when (looking-at org-dblock-end-re)
  8435. (and (looking-at "[ \t]+")
  8436. (replace-match ""))
  8437. (insert indent)))))))
  8438. (defun org-beginning-of-dblock ()
  8439. "Find the beginning of the dynamic block at point.
  8440. Error if there is no such block at point."
  8441. (let ((pos (point))
  8442. beg)
  8443. (end-of-line 1)
  8444. (if (and (re-search-backward org-dblock-start-re nil t)
  8445. (setq beg (match-beginning 0))
  8446. (re-search-forward org-dblock-end-re nil t)
  8447. (> (match-end 0) pos))
  8448. (goto-char beg)
  8449. (goto-char pos)
  8450. (error "Not in a dynamic block"))))
  8451. (defun org-update-all-dblocks ()
  8452. "Update all dynamic blocks in the buffer.
  8453. This function can be used in a hook."
  8454. (when (org-mode-p)
  8455. (org-map-dblocks 'org-update-dblock)))
  8456. ;;;; Completion
  8457. (defconst org-additional-option-like-keywords
  8458. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8459. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8460. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8461. "BEGIN:" "END:"
  8462. "ORGTBL" "TBLFM:" "TBLNAME:"
  8463. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8464. "BEGIN_QUOTE" "END_QUOTE"
  8465. "BEGIN_VERSE" "END_VERSE"
  8466. "BEGIN_CENTER" "END_CENTER"
  8467. "BEGIN_SRC" "END_SRC"
  8468. "CATEGORY" "COLUMNS"
  8469. "CAPTION" "LABEL"
  8470. "SETUPFILE"
  8471. "BIND"
  8472. "MACRO"))
  8473. (defcustom org-structure-template-alist
  8474. '(
  8475. ("s" "#+begin_src ?\n\n#+end_src"
  8476. "<src lang=\"?\">\n\n</src>")
  8477. ("e" "#+begin_example\n?\n#+end_example"
  8478. "<example>\n?\n</example>")
  8479. ("q" "#+begin_quote\n?\n#+end_quote"
  8480. "<quote>\n?\n</quote>")
  8481. ("v" "#+begin_verse\n?\n#+end_verse"
  8482. "<verse>\n?\n/verse>")
  8483. ("c" "#+begin_center\n?\n#+end_center"
  8484. "<center>\n?\n/center>")
  8485. ("l" "#+begin_latex\n?\n#+end_latex"
  8486. "<literal style=\"latex\">\n?\n</literal>")
  8487. ("L" "#+latex: "
  8488. "<literal style=\"latex\">?</literal>")
  8489. ("h" "#+begin_html\n?\n#+end_html"
  8490. "<literal style=\"html\">\n?\n</literal>")
  8491. ("H" "#+html: "
  8492. "<literal style=\"html\">?</literal>")
  8493. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8494. ("A" "#+ascii: ")
  8495. ("i" "#+include %file ?"
  8496. "<include file=%file markup=\"?\">")
  8497. )
  8498. "Structure completion elements.
  8499. This is a list of abbreviation keys and values. The value gets inserted
  8500. it you type @samp{.} followed by the key and then the completion key,
  8501. usually `M-TAB'. %file will be replaced by a file name after prompting
  8502. for the file using completion.
  8503. There are two templates for each key, the first uses the original Org syntax,
  8504. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8505. the default when the /org-mtags.el/ module has been loaded. See also the
  8506. variable `org-mtags-prefer-muse-templates'.
  8507. This is an experimental feature, it is undecided if it is going to stay in."
  8508. :group 'org-completion
  8509. :type '(repeat
  8510. (string :tag "Key")
  8511. (string :tag "Template")
  8512. (string :tag "Muse Template")))
  8513. (defun org-try-structure-completion ()
  8514. "Try to complete a structure template before point.
  8515. This looks for strings like \"<e\" on an otherwise empty line and
  8516. expands them."
  8517. (let ((l (buffer-substring (point-at-bol) (point)))
  8518. a)
  8519. (when (and (looking-at "[ \t]*$")
  8520. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8521. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8522. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8523. (match-beginning 1)) a)
  8524. t)))
  8525. (defun org-complete-expand-structure-template (start cell)
  8526. "Expand a structure template."
  8527. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8528. (rpl (nth (if musep 2 1) cell))
  8529. (ind ""))
  8530. (delete-region start (point))
  8531. (when (string-match "\\`#\\+" rpl)
  8532. (cond
  8533. ((bolp))
  8534. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8535. (setq ind (buffer-substring (point-at-bol) (point))))
  8536. (t (newline))))
  8537. (setq start (point))
  8538. (if (string-match "%file" rpl)
  8539. (setq rpl (replace-match
  8540. (concat
  8541. "\""
  8542. (save-match-data
  8543. (abbreviate-file-name (read-file-name "Include file: ")))
  8544. "\"")
  8545. t t rpl)))
  8546. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8547. (concat "\n" ind)))
  8548. (insert rpl)
  8549. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8550. (defun org-complete (&optional arg)
  8551. "Perform completion on word at point.
  8552. At the beginning of a headline, this completes TODO keywords as given in
  8553. `org-todo-keywords'.
  8554. If the current word is preceded by a backslash, completes the TeX symbols
  8555. that are supported for HTML support.
  8556. If the current word is preceded by \"#+\", completes special words for
  8557. setting file options.
  8558. In the line after \"#+STARTUP:, complete valid keywords.\"
  8559. At all other locations, this simply calls the value of
  8560. `org-completion-fallback-command'."
  8561. (interactive "P")
  8562. (org-without-partial-completion
  8563. (catch 'exit
  8564. (let* ((a nil)
  8565. (end (point))
  8566. (beg1 (save-excursion
  8567. (skip-chars-backward (org-re "[:alnum:]_@"))
  8568. (point)))
  8569. (beg (save-excursion
  8570. (skip-chars-backward "a-zA-Z0-9_:$")
  8571. (point)))
  8572. (confirm (lambda (x) (stringp (car x))))
  8573. (searchhead (equal (char-before beg) ?*))
  8574. (struct
  8575. (when (and (member (char-before beg1) '(?. ?<))
  8576. (setq a (assoc (buffer-substring beg1 (point))
  8577. org-structure-template-alist)))
  8578. (org-complete-expand-structure-template (1- beg1) a)
  8579. (throw 'exit t)))
  8580. (tag (and (equal (char-before beg1) ?:)
  8581. (equal (char-after (point-at-bol)) ?*)))
  8582. (prop (and (equal (char-before beg1) ?:)
  8583. (not (equal (char-after (point-at-bol)) ?*))))
  8584. (texp (equal (char-before beg) ?\\))
  8585. (link (equal (char-before beg) ?\[))
  8586. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8587. beg)
  8588. "#+"))
  8589. (startup (string-match "^#\\+STARTUP:.*"
  8590. (buffer-substring (point-at-bol) (point))))
  8591. (completion-ignore-case opt)
  8592. (type nil)
  8593. (tbl nil)
  8594. (table (cond
  8595. (opt
  8596. (setq type :opt)
  8597. (require 'org-exp)
  8598. (append
  8599. (delq nil
  8600. (mapcar
  8601. (lambda (x)
  8602. (if (string-match
  8603. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8604. (cons (match-string 2 x)
  8605. (match-string 1 x))))
  8606. (org-split-string (org-get-current-options) "\n")))
  8607. (mapcar 'list org-additional-option-like-keywords)))
  8608. (startup
  8609. (setq type :startup)
  8610. org-startup-options)
  8611. (link (append org-link-abbrev-alist-local
  8612. org-link-abbrev-alist))
  8613. (texp
  8614. (setq type :tex)
  8615. org-html-entities)
  8616. ((string-match "\\`\\*+[ \t]+\\'"
  8617. (buffer-substring (point-at-bol) beg))
  8618. (setq type :todo)
  8619. (mapcar 'list org-todo-keywords-1))
  8620. (searchhead
  8621. (setq type :searchhead)
  8622. (save-excursion
  8623. (goto-char (point-min))
  8624. (while (re-search-forward org-todo-line-regexp nil t)
  8625. (push (list
  8626. (org-make-org-heading-search-string
  8627. (match-string 3) t))
  8628. tbl)))
  8629. tbl)
  8630. (tag (setq type :tag beg beg1)
  8631. (or org-tag-alist (org-get-buffer-tags)))
  8632. (prop (setq type :prop beg beg1)
  8633. (mapcar 'list (org-buffer-property-keys nil t t)))
  8634. (t (progn
  8635. (call-interactively org-completion-fallback-command)
  8636. (throw 'exit nil)))))
  8637. (pattern (buffer-substring-no-properties beg end))
  8638. (completion (try-completion pattern table confirm)))
  8639. (cond ((eq completion t)
  8640. (if (not (assoc (upcase pattern) table))
  8641. (message "Already complete")
  8642. (if (and (equal type :opt)
  8643. (not (member (car (assoc (upcase pattern) table))
  8644. org-additional-option-like-keywords)))
  8645. (insert (substring (cdr (assoc (upcase pattern) table))
  8646. (length pattern)))
  8647. (if (memq type '(:tag :prop)) (insert ":")))))
  8648. ((null completion)
  8649. (message "Can't find completion for \"%s\"" pattern)
  8650. (ding))
  8651. ((not (string= pattern completion))
  8652. (delete-region beg end)
  8653. (if (string-match " +$" completion)
  8654. (setq completion (replace-match "" t t completion)))
  8655. (insert completion)
  8656. (if (get-buffer-window "*Completions*")
  8657. (delete-window (get-buffer-window "*Completions*")))
  8658. (if (assoc completion table)
  8659. (if (eq type :todo) (insert " ")
  8660. (if (memq type '(:tag :prop)) (insert ":"))))
  8661. (if (and (equal type :opt) (assoc completion table))
  8662. (message "%s" (substitute-command-keys
  8663. "Press \\[org-complete] again to insert example settings"))))
  8664. (t
  8665. (message "Making completion list...")
  8666. (let ((list (sort (all-completions pattern table confirm)
  8667. 'string<)))
  8668. (with-output-to-temp-buffer "*Completions*"
  8669. (condition-case nil
  8670. ;; Protection needed for XEmacs and emacs 21
  8671. (display-completion-list list pattern)
  8672. (error (display-completion-list list)))))
  8673. (message "Making completion list...%s" "done")))))))
  8674. ;;;; TODO, DEADLINE, Comments
  8675. (defun org-toggle-comment ()
  8676. "Change the COMMENT state of an entry."
  8677. (interactive)
  8678. (save-excursion
  8679. (org-back-to-heading)
  8680. (let (case-fold-search)
  8681. (if (looking-at (concat outline-regexp
  8682. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8683. (replace-match "" t t nil 1)
  8684. (if (looking-at outline-regexp)
  8685. (progn
  8686. (goto-char (match-end 0))
  8687. (insert org-comment-string " ")))))))
  8688. (defvar org-last-todo-state-is-todo nil
  8689. "This is non-nil when the last TODO state change led to a TODO state.
  8690. If the last change removed the TODO tag or switched to DONE, then
  8691. this is nil.")
  8692. (defvar org-setting-tags nil) ; dynamically skipped
  8693. (defun org-parse-local-options (string var)
  8694. "Parse STRING for startup setting relevant for variable VAR."
  8695. (let ((rtn (symbol-value var))
  8696. e opts)
  8697. (save-match-data
  8698. (if (or (not string) (not (string-match "\\S-" string)))
  8699. rtn
  8700. (setq opts (delq nil (mapcar (lambda (x)
  8701. (setq e (assoc x org-startup-options))
  8702. (if (eq (nth 1 e) var) e nil))
  8703. (org-split-string string "[ \t]+"))))
  8704. (if (not opts)
  8705. rtn
  8706. (setq rtn nil)
  8707. (while (setq e (pop opts))
  8708. (if (not (nth 3 e))
  8709. (setq rtn (nth 2 e))
  8710. (if (not (listp rtn)) (setq rtn nil))
  8711. (push (nth 2 e) rtn)))
  8712. rtn)))))
  8713. (defvar org-todo-setup-filter-hook nil
  8714. "Hook for functions that pre-filter todo specs.
  8715. Each function takes a todo spec and returns either `nil' or the spec
  8716. transformed into canonical form." )
  8717. (defvar org-todo-get-default-hook nil
  8718. "Hook for functions that get a default item for todo.
  8719. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8720. `nil' or a string to be used for the todo mark." )
  8721. (defvar org-agenda-headline-snapshot-before-repeat)
  8722. (defun org-todo (&optional arg)
  8723. "Change the TODO state of an item.
  8724. The state of an item is given by a keyword at the start of the heading,
  8725. like
  8726. *** TODO Write paper
  8727. *** DONE Call mom
  8728. The different keywords are specified in the variable `org-todo-keywords'.
  8729. By default the available states are \"TODO\" and \"DONE\".
  8730. So for this example: when the item starts with TODO, it is changed to DONE.
  8731. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8732. DONE are present, add TODO at the beginning of the heading.
  8733. With C-u prefix arg, use completion to determine the new state.
  8734. With numeric prefix arg, switch to that state.
  8735. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8736. With a triple C-u prefix, circumvent any state blocking.
  8737. For calling through lisp, arg is also interpreted in the following way:
  8738. 'none -> empty state
  8739. \"\"(empty string) -> switch to empty state
  8740. 'done -> switch to DONE
  8741. 'nextset -> switch to the next set of keywords
  8742. 'previousset -> switch to the previous set of keywords
  8743. \"WAITING\" -> switch to the specified keyword, but only if it
  8744. really is a member of `org-todo-keywords'."
  8745. (interactive "P")
  8746. (if (equal arg '(16)) (setq arg 'nextset))
  8747. (let ((org-blocker-hook org-blocker-hook)
  8748. (case-fold-search nil))
  8749. (when (equal arg '(64))
  8750. (setq arg nil org-blocker-hook nil))
  8751. (when (and org-blocker-hook
  8752. (or org-inhibit-blocking
  8753. (org-entry-get nil "NOBLOCKING")))
  8754. (setq org-blocker-hook nil))
  8755. (save-excursion
  8756. (catch 'exit
  8757. (org-back-to-heading t)
  8758. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8759. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8760. (looking-at " *"))
  8761. (let* ((match-data (match-data))
  8762. (startpos (point-at-bol))
  8763. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8764. (org-log-done org-log-done)
  8765. (org-log-repeat org-log-repeat)
  8766. (org-todo-log-states org-todo-log-states)
  8767. (this (match-string 1))
  8768. (hl-pos (match-beginning 0))
  8769. (head (org-get-todo-sequence-head this))
  8770. (ass (assoc head org-todo-kwd-alist))
  8771. (interpret (nth 1 ass))
  8772. (done-word (nth 3 ass))
  8773. (final-done-word (nth 4 ass))
  8774. (last-state (or this ""))
  8775. (completion-ignore-case t)
  8776. (member (member this org-todo-keywords-1))
  8777. (tail (cdr member))
  8778. (state (cond
  8779. ((and org-todo-key-trigger
  8780. (or (and (equal arg '(4))
  8781. (eq org-use-fast-todo-selection 'prefix))
  8782. (and (not arg) org-use-fast-todo-selection
  8783. (not (eq org-use-fast-todo-selection
  8784. 'prefix)))))
  8785. ;; Use fast selection
  8786. (org-fast-todo-selection))
  8787. ((and (equal arg '(4))
  8788. (or (not org-use-fast-todo-selection)
  8789. (not org-todo-key-trigger)))
  8790. ;; Read a state with completion
  8791. (org-icompleting-read
  8792. "State: " (mapcar (lambda(x) (list x))
  8793. org-todo-keywords-1)
  8794. nil t))
  8795. ((eq arg 'right)
  8796. (if this
  8797. (if tail (car tail) nil)
  8798. (car org-todo-keywords-1)))
  8799. ((eq arg 'left)
  8800. (if (equal member org-todo-keywords-1)
  8801. nil
  8802. (if this
  8803. (nth (- (length org-todo-keywords-1)
  8804. (length tail) 2)
  8805. org-todo-keywords-1)
  8806. (org-last org-todo-keywords-1))))
  8807. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8808. (setq arg nil))) ; hack to fall back to cycling
  8809. (arg
  8810. ;; user or caller requests a specific state
  8811. (cond
  8812. ((equal arg "") nil)
  8813. ((eq arg 'none) nil)
  8814. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8815. ((eq arg 'nextset)
  8816. (or (car (cdr (member head org-todo-heads)))
  8817. (car org-todo-heads)))
  8818. ((eq arg 'previousset)
  8819. (let ((org-todo-heads (reverse org-todo-heads)))
  8820. (or (car (cdr (member head org-todo-heads)))
  8821. (car org-todo-heads))))
  8822. ((car (member arg org-todo-keywords-1)))
  8823. ((stringp arg)
  8824. (error "State `%s' not valid in this file" arg))
  8825. ((nth (1- (prefix-numeric-value arg))
  8826. org-todo-keywords-1))))
  8827. ((null member) (or head (car org-todo-keywords-1)))
  8828. ((equal this final-done-word) nil) ;; -> make empty
  8829. ((null tail) nil) ;; -> first entry
  8830. ((memq interpret '(type priority))
  8831. (if (eq this-command last-command)
  8832. (car tail)
  8833. (if (> (length tail) 0)
  8834. (or done-word (car org-done-keywords))
  8835. nil)))
  8836. (t
  8837. (car tail))))
  8838. (state (or
  8839. (run-hook-with-args-until-success
  8840. 'org-todo-get-default-hook state last-state)
  8841. state))
  8842. (next (if state (concat " " state " ") " "))
  8843. (change-plist (list :type 'todo-state-change :from this :to state
  8844. :position startpos))
  8845. dolog now-done-p)
  8846. (when org-blocker-hook
  8847. (setq org-last-todo-state-is-todo
  8848. (not (member this org-done-keywords)))
  8849. (unless (save-excursion
  8850. (save-match-data
  8851. (run-hook-with-args-until-failure
  8852. 'org-blocker-hook change-plist)))
  8853. (if (interactive-p)
  8854. (error "TODO state change from %s to %s blocked" this state)
  8855. ;; fail silently
  8856. (message "TODO state change from %s to %s blocked" this state)
  8857. (throw 'exit nil))))
  8858. (store-match-data match-data)
  8859. (replace-match next t t)
  8860. (unless (pos-visible-in-window-p hl-pos)
  8861. (message "TODO state changed to %s" (org-trim next)))
  8862. (unless head
  8863. (setq head (org-get-todo-sequence-head state)
  8864. ass (assoc head org-todo-kwd-alist)
  8865. interpret (nth 1 ass)
  8866. done-word (nth 3 ass)
  8867. final-done-word (nth 4 ass)))
  8868. (when (memq arg '(nextset previousset))
  8869. (message "Keyword-Set %d/%d: %s"
  8870. (- (length org-todo-sets) -1
  8871. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8872. (length org-todo-sets)
  8873. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8874. (setq org-last-todo-state-is-todo
  8875. (not (member state org-done-keywords)))
  8876. (setq now-done-p (and (member state org-done-keywords)
  8877. (not (member this org-done-keywords))))
  8878. (and logging (org-local-logging logging))
  8879. (when (and (or org-todo-log-states org-log-done)
  8880. (not (eq org-inhibit-logging t))
  8881. (not (memq arg '(nextset previousset))))
  8882. ;; we need to look at recording a time and note
  8883. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8884. (nth 2 (assoc this org-todo-log-states))))
  8885. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8886. (setq dolog 'time))
  8887. (when (and state
  8888. (member state org-not-done-keywords)
  8889. (not (member this org-not-done-keywords)))
  8890. ;; This is now a todo state and was not one before
  8891. ;; If there was a CLOSED time stamp, get rid of it.
  8892. (org-add-planning-info nil nil 'closed))
  8893. (when (and now-done-p org-log-done)
  8894. ;; It is now done, and it was not done before
  8895. (org-add-planning-info 'closed (org-current-time))
  8896. (if (and (not dolog) (eq 'note org-log-done))
  8897. (org-add-log-setup 'done state this 'findpos 'note)))
  8898. (when (and state dolog)
  8899. ;; This is a non-nil state, and we need to log it
  8900. (org-add-log-setup 'state state this 'findpos dolog)))
  8901. ;; Fixup tag positioning
  8902. (org-todo-trigger-tag-changes state)
  8903. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8904. (when org-provide-todo-statistics
  8905. (org-update-parent-todo-statistics))
  8906. (run-hooks 'org-after-todo-state-change-hook)
  8907. (if (and arg (not (member state org-done-keywords)))
  8908. (setq head (org-get-todo-sequence-head state)))
  8909. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8910. ;; Do we need to trigger a repeat?
  8911. (when now-done-p
  8912. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8913. ;; This is for the agenda, take a snapshot of the headline.
  8914. (save-match-data
  8915. (setq org-agenda-headline-snapshot-before-repeat
  8916. (org-get-heading))))
  8917. (org-auto-repeat-maybe state))
  8918. ;; Fixup cursor location if close to the keyword
  8919. (if (and (outline-on-heading-p)
  8920. (not (bolp))
  8921. (save-excursion (beginning-of-line 1)
  8922. (looking-at org-todo-line-regexp))
  8923. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8924. (progn
  8925. (goto-char (or (match-end 2) (match-end 1)))
  8926. (and (looking-at " ") (just-one-space))))
  8927. (when org-trigger-hook
  8928. (save-excursion
  8929. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8930. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8931. "Block turning an entry into a TODO, using the hierarchy.
  8932. This checks whether the current task should be blocked from state
  8933. changes. Such blocking occurs when:
  8934. 1. The task has children which are not all in a completed state.
  8935. 2. A task has a parent with the property :ORDERED:, and there
  8936. are siblings prior to the current task with incomplete
  8937. status.
  8938. 3. The parent of the task is blocked because it has siblings that should
  8939. be done first, or is child of a block grandparent TODO entry."
  8940. (catch 'dont-block
  8941. ;; If this is not a todo state change, or if this entry is already DONE,
  8942. ;; do not block
  8943. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8944. (member (plist-get change-plist :from)
  8945. (cons 'done org-done-keywords))
  8946. (member (plist-get change-plist :to)
  8947. (cons 'todo org-not-done-keywords))
  8948. (not (plist-get change-plist :to)))
  8949. (throw 'dont-block t))
  8950. ;; If this task has children, and any are undone, it's blocked
  8951. (save-excursion
  8952. (org-back-to-heading t)
  8953. (let ((this-level (funcall outline-level)))
  8954. (outline-next-heading)
  8955. (let ((child-level (funcall outline-level)))
  8956. (while (and (not (eobp))
  8957. (> child-level this-level))
  8958. ;; this todo has children, check whether they are all
  8959. ;; completed
  8960. (if (and (not (org-entry-is-done-p))
  8961. (org-entry-is-todo-p))
  8962. (throw 'dont-block nil))
  8963. (outline-next-heading)
  8964. (setq child-level (funcall outline-level))))))
  8965. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8966. ;; any previous siblings are undone, it's blocked
  8967. (save-excursion
  8968. (org-back-to-heading t)
  8969. (let* ((pos (point))
  8970. (parent-pos (and (org-up-heading-safe) (point))))
  8971. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8972. (when (and (org-entry-get (point) "ORDERED")
  8973. (forward-line 1)
  8974. (re-search-forward org-not-done-heading-regexp pos t))
  8975. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8976. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8977. (while t
  8978. (goto-char parent-pos)
  8979. (if (not (looking-at org-not-done-heading-regexp))
  8980. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8981. (setq pos (point))
  8982. (setq parent-pos (and (org-up-heading-safe) (point)))
  8983. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8984. (when (and (org-entry-get (point) "ORDERED")
  8985. (forward-line 1)
  8986. (re-search-forward org-not-done-heading-regexp pos t))
  8987. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8988. (defcustom org-track-ordered-property-with-tag nil
  8989. "Should the ORDERED property also be shown as a tag?
  8990. The ORDERED property decides if an entry should require subtasks to be
  8991. completed in sequence. Since a property is not very visible, setting
  8992. this option means that toggling the ORDERED property with the command
  8993. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8994. not relevant for the behavior, but it makes things more visible.
  8995. Note that toggling the tag with tags commands will not change the property
  8996. and therefore not influence behavior!
  8997. This can be t, meaning the tag ORDERED should be used, It can also be a
  8998. string to select a different tag for this task."
  8999. :group 'org-todo
  9000. :type '(choice
  9001. (const :tag "No tracking" nil)
  9002. (const :tag "Track with ORDERED tag" t)
  9003. (string :tag "Use other tag")))
  9004. (defun org-toggle-ordered-property ()
  9005. "Toggle the ORDERED property of the current entry.
  9006. For better visibility, you can track the value of this property with a tag.
  9007. See variable `org-track-ordered-property-with-tag'."
  9008. (interactive)
  9009. (let* ((t1 org-track-ordered-property-with-tag)
  9010. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9011. (save-excursion
  9012. (org-back-to-heading)
  9013. (if (org-entry-get nil "ORDERED")
  9014. (progn
  9015. (org-delete-property "ORDERED")
  9016. (and tag (org-toggle-tag tag 'off))
  9017. (message "Subtasks can be completed in arbitrary order"))
  9018. (org-entry-put nil "ORDERED" "t")
  9019. (and tag (org-toggle-tag tag 'on))
  9020. (message "Subtasks must be completed in sequence")))))
  9021. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9022. (defun org-block-todo-from-checkboxes (change-plist)
  9023. "Block turning an entry into a TODO, using checkboxes.
  9024. This checks whether the current task should be blocked from state
  9025. changes because there are unchecked boxes in this entry."
  9026. (catch 'dont-block
  9027. ;; If this is not a todo state change, or if this entry is already DONE,
  9028. ;; do not block
  9029. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9030. (member (plist-get change-plist :from)
  9031. (cons 'done org-done-keywords))
  9032. (member (plist-get change-plist :to)
  9033. (cons 'todo org-not-done-keywords))
  9034. (not (plist-get change-plist :to)))
  9035. (throw 'dont-block t))
  9036. ;; If this task has checkboxes that are not checked, it's blocked
  9037. (save-excursion
  9038. (org-back-to-heading t)
  9039. (let ((beg (point)) end)
  9040. (outline-next-heading)
  9041. (setq end (point))
  9042. (goto-char beg)
  9043. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9044. end t)
  9045. (progn
  9046. (if (boundp 'org-blocked-by-checkboxes)
  9047. (setq org-blocked-by-checkboxes t))
  9048. (throw 'dont-block nil)))))
  9049. t)) ; do not block
  9050. (defun org-update-statistics-cookies (all)
  9051. "Update the statistics cookie, either from TODO or from checkboxes.
  9052. This should be called with the cursor in a line with a statistics cookie."
  9053. (interactive "P")
  9054. (if all
  9055. (progn
  9056. (org-update-checkbox-count 'all)
  9057. (org-map-entries 'org-update-parent-todo-statistics))
  9058. (if (not (org-on-heading-p))
  9059. (org-update-checkbox-count)
  9060. (let ((pos (move-marker (make-marker) (point)))
  9061. end l1 l2)
  9062. (ignore-errors (org-back-to-heading t))
  9063. (if (not (org-on-heading-p))
  9064. (org-update-checkbox-count)
  9065. (setq l1 (org-outline-level))
  9066. (setq end (save-excursion
  9067. (outline-next-heading)
  9068. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9069. (point)))
  9070. (if (and (save-excursion (re-search-forward
  9071. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  9072. (not (save-excursion (re-search-forward
  9073. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9074. (org-update-checkbox-count)
  9075. (if (and l2 (> l2 l1))
  9076. (progn
  9077. (goto-char end)
  9078. (org-update-parent-todo-statistics))
  9079. (error "No data for statistics cookie"))))
  9080. (goto-char pos)
  9081. (move-marker pos nil)))))
  9082. (defvar org-entry-property-inherited-from) ;; defined below
  9083. (defun org-update-parent-todo-statistics ()
  9084. "Update any statistics cookie in the parent of the current headline.
  9085. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9086. the entire subtree and this will travel up the hierarchy and update
  9087. statistics everywhere."
  9088. (interactive)
  9089. (let* ((lim 0) prop
  9090. (recursive (or (not org-hierarchical-todo-statistics)
  9091. (string-match
  9092. "\\<recursive\\>"
  9093. (or (setq prop (org-entry-get
  9094. nil "COOKIE_DATA" 'inherit)) ""))))
  9095. (lim (or (and prop (marker-position
  9096. org-entry-property-inherited-from))
  9097. lim))
  9098. (first t)
  9099. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9100. level ltoggle l1 new ndel
  9101. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9102. (catch 'exit
  9103. (save-excursion
  9104. (beginning-of-line 1)
  9105. (if (org-at-heading-p)
  9106. (setq ltoggle (funcall outline-level))
  9107. (error "This should not happen"))
  9108. (while (and (setq level (org-up-heading-safe))
  9109. (or recursive first)
  9110. (>= (point) lim))
  9111. (setq first nil cookie-present nil)
  9112. (unless (and level
  9113. (not (string-match
  9114. "\\<checkbox\\>"
  9115. (downcase
  9116. (or (org-entry-get
  9117. nil "COOKIE_DATA")
  9118. "")))))
  9119. (throw 'exit nil))
  9120. (while (re-search-forward box-re (point-at-eol) t)
  9121. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9122. (setq is-percent (match-end 2))
  9123. (save-match-data
  9124. (unless (outline-next-heading) (throw 'exit nil))
  9125. (while (and (looking-at org-complex-heading-regexp)
  9126. (> (setq l1 (length (match-string 1))) level))
  9127. (setq kwd (and (or recursive (= l1 ltoggle))
  9128. (match-string 2)))
  9129. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9130. (and (listp org-provide-todo-statistics)
  9131. (or (member kwd org-provide-todo-statistics)
  9132. (member kwd org-done-keywords))))
  9133. (setq cnt-all (1+ cnt-all))
  9134. (if (eq org-provide-todo-statistics t)
  9135. (and kwd (setq cnt-all (1+ cnt-all)))))
  9136. (and (member kwd org-done-keywords)
  9137. (setq cnt-done (1+ cnt-done)))
  9138. (outline-next-heading)))
  9139. (setq new
  9140. (if is-percent
  9141. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9142. (format "[%d/%d]" cnt-done cnt-all))
  9143. ndel (- (match-end 0) (match-beginning 0)))
  9144. (goto-char (match-beginning 0))
  9145. (insert new)
  9146. (delete-region (point) (+ (point) ndel)))
  9147. (when cookie-present
  9148. (run-hook-with-args 'org-after-todo-statistics-hook
  9149. cnt-done (- cnt-all cnt-done))))))
  9150. (run-hooks 'org-todo-statistics-hook)))
  9151. (defvar org-after-todo-statistics-hook nil
  9152. "Hook that is called after a TODO statistics cookie has been updated.
  9153. Each function is called with two arguments: the number of not-done entries
  9154. and the number of done entries.
  9155. For example, the following function, when added to this hook, will switch
  9156. an entry to DONE when all children are done, and back to TODO when new
  9157. entries are set to a TODO status. Note that this hook is only called
  9158. when there is a statistics cookie in the headline!
  9159. (defun org-summary-todo (n-done n-not-done)
  9160. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9161. (let (org-log-done org-log-states) ; turn off logging
  9162. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9163. ")
  9164. (defvar org-todo-statistics-hook nil
  9165. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9166. This hook runs even if there is no statistics cookie present, in which case
  9167. `org-after-todo-statistics-hook' would not run.")
  9168. (defun org-todo-trigger-tag-changes (state)
  9169. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9170. (let ((l org-todo-state-tags-triggers)
  9171. changes)
  9172. (when (or (not state) (equal state ""))
  9173. (setq changes (append changes (cdr (assoc "" l)))))
  9174. (when (and (stringp state) (> (length state) 0))
  9175. (setq changes (append changes (cdr (assoc state l)))))
  9176. (when (member state org-not-done-keywords)
  9177. (setq changes (append changes (cdr (assoc 'todo l)))))
  9178. (when (member state org-done-keywords)
  9179. (setq changes (append changes (cdr (assoc 'done l)))))
  9180. (dolist (c changes)
  9181. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9182. (defun org-local-logging (value)
  9183. "Get logging settings from a property VALUE."
  9184. (let* (words w a)
  9185. ;; directly set the variables, they are already local.
  9186. (setq org-log-done nil
  9187. org-log-repeat nil
  9188. org-todo-log-states nil)
  9189. (setq words (org-split-string value))
  9190. (while (setq w (pop words))
  9191. (cond
  9192. ((setq a (assoc w org-startup-options))
  9193. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9194. (set (nth 1 a) (nth 2 a))))
  9195. ((setq a (org-extract-log-state-settings w))
  9196. (and (member (car a) org-todo-keywords-1)
  9197. (push a org-todo-log-states)))))))
  9198. (defun org-get-todo-sequence-head (kwd)
  9199. "Return the head of the TODO sequence to which KWD belongs.
  9200. If KWD is not set, check if there is a text property remembering the
  9201. right sequence."
  9202. (let (p)
  9203. (cond
  9204. ((not kwd)
  9205. (or (get-text-property (point-at-bol) 'org-todo-head)
  9206. (progn
  9207. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9208. nil (point-at-eol)))
  9209. (get-text-property p 'org-todo-head))))
  9210. ((not (member kwd org-todo-keywords-1))
  9211. (car org-todo-keywords-1))
  9212. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9213. (defun org-fast-todo-selection ()
  9214. "Fast TODO keyword selection with single keys.
  9215. Returns the new TODO keyword, or nil if no state change should occur."
  9216. (let* ((fulltable org-todo-key-alist)
  9217. (done-keywords org-done-keywords) ;; needed for the faces.
  9218. (maxlen (apply 'max (mapcar
  9219. (lambda (x)
  9220. (if (stringp (car x)) (string-width (car x)) 0))
  9221. fulltable)))
  9222. (expert nil)
  9223. (fwidth (+ maxlen 3 1 3))
  9224. (ncol (/ (- (window-width) 4) fwidth))
  9225. tg cnt e c tbl
  9226. groups ingroup)
  9227. (save-excursion
  9228. (save-window-excursion
  9229. (if expert
  9230. (set-buffer (get-buffer-create " *Org todo*"))
  9231. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9232. (erase-buffer)
  9233. (org-set-local 'org-done-keywords done-keywords)
  9234. (setq tbl fulltable cnt 0)
  9235. (while (setq e (pop tbl))
  9236. (cond
  9237. ((equal e '(:startgroup))
  9238. (push '() groups) (setq ingroup t)
  9239. (when (not (= cnt 0))
  9240. (setq cnt 0)
  9241. (insert "\n"))
  9242. (insert "{ "))
  9243. ((equal e '(:endgroup))
  9244. (setq ingroup nil cnt 0)
  9245. (insert "}\n"))
  9246. ((equal e '(:newline))
  9247. (when (not (= cnt 0))
  9248. (setq cnt 0)
  9249. (insert "\n")
  9250. (setq e (car tbl))
  9251. (while (equal (car tbl) '(:newline))
  9252. (insert "\n")
  9253. (setq tbl (cdr tbl)))))
  9254. (t
  9255. (setq tg (car e) c (cdr e))
  9256. (if ingroup (push tg (car groups)))
  9257. (setq tg (org-add-props tg nil 'face
  9258. (org-get-todo-face tg)))
  9259. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9260. (insert "[" c "] " tg (make-string
  9261. (- fwidth 4 (length tg)) ?\ ))
  9262. (when (= (setq cnt (1+ cnt)) ncol)
  9263. (insert "\n")
  9264. (if ingroup (insert " "))
  9265. (setq cnt 0)))))
  9266. (insert "\n")
  9267. (goto-char (point-min))
  9268. (if (not expert) (org-fit-window-to-buffer))
  9269. (message "[a-z..]:Set [SPC]:clear")
  9270. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9271. (cond
  9272. ((or (= c ?\C-g)
  9273. (and (= c ?q) (not (rassoc c fulltable))))
  9274. (setq quit-flag t))
  9275. ((= c ?\ ) nil)
  9276. ((setq e (rassoc c fulltable) tg (car e))
  9277. tg)
  9278. (t (setq quit-flag t)))))))
  9279. (defun org-entry-is-todo-p ()
  9280. (member (org-get-todo-state) org-not-done-keywords))
  9281. (defun org-entry-is-done-p ()
  9282. (member (org-get-todo-state) org-done-keywords))
  9283. (defun org-get-todo-state ()
  9284. (save-excursion
  9285. (org-back-to-heading t)
  9286. (and (looking-at org-todo-line-regexp)
  9287. (match-end 2)
  9288. (match-string 2))))
  9289. (defun org-at-date-range-p (&optional inactive-ok)
  9290. "Is the cursor inside a date range?"
  9291. (interactive)
  9292. (save-excursion
  9293. (catch 'exit
  9294. (let ((pos (point)))
  9295. (skip-chars-backward "^[<\r\n")
  9296. (skip-chars-backward "<[")
  9297. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9298. (>= (match-end 0) pos)
  9299. (throw 'exit t))
  9300. (skip-chars-backward "^<[\r\n")
  9301. (skip-chars-backward "<[")
  9302. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9303. (>= (match-end 0) pos)
  9304. (throw 'exit t)))
  9305. nil)))
  9306. (defun org-get-repeat (&optional tagline)
  9307. "Check if there is a deadline/schedule with repeater in this entry."
  9308. (save-match-data
  9309. (save-excursion
  9310. (org-back-to-heading t)
  9311. (and (re-search-forward (if tagline
  9312. (concat tagline "\\s-*" org-repeat-re)
  9313. org-repeat-re)
  9314. (org-entry-end-position) t)
  9315. (match-string-no-properties 1)))))
  9316. (defvar org-last-changed-timestamp)
  9317. (defvar org-last-inserted-timestamp)
  9318. (defvar org-log-post-message)
  9319. (defvar org-log-note-purpose)
  9320. (defvar org-log-note-how)
  9321. (defvar org-log-note-extra)
  9322. (defun org-auto-repeat-maybe (done-word)
  9323. "Check if the current headline contains a repeated deadline/schedule.
  9324. If yes, set TODO state back to what it was and change the base date
  9325. of repeating deadline/scheduled time stamps to new date.
  9326. This function is run automatically after each state change to a DONE state."
  9327. ;; last-state is dynamically scoped into this function
  9328. (let* ((repeat (org-get-repeat))
  9329. (aa (assoc last-state org-todo-kwd-alist))
  9330. (interpret (nth 1 aa))
  9331. (head (nth 2 aa))
  9332. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9333. (msg "Entry repeats: ")
  9334. (org-log-done nil)
  9335. (org-todo-log-states nil)
  9336. (nshiftmax 10) (nshift 0)
  9337. re type n what ts time)
  9338. (when repeat
  9339. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9340. (org-todo (if (eq interpret 'type) last-state head))
  9341. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9342. (org-time-stamp-format t t)))
  9343. (when org-log-repeat
  9344. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9345. (memq 'org-add-log-note post-command-hook))
  9346. ;; OK, we are already setup for some record
  9347. (if (eq org-log-repeat 'note)
  9348. ;; make sure we take a note, not only a time stamp
  9349. (setq org-log-note-how 'note))
  9350. ;; Set up for taking a record
  9351. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9352. last-state
  9353. 'findpos org-log-repeat)))
  9354. (org-back-to-heading t)
  9355. (org-add-planning-info nil nil 'closed)
  9356. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9357. org-deadline-time-regexp "\\)\\|\\("
  9358. org-ts-regexp "\\)"))
  9359. (while (re-search-forward
  9360. re (save-excursion (outline-next-heading) (point)) t)
  9361. (setq type (if (match-end 1) org-scheduled-string
  9362. (if (match-end 3) org-deadline-string "Plain:"))
  9363. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9364. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9365. (setq n (string-to-number (match-string 2 ts))
  9366. what (match-string 3 ts))
  9367. (if (equal what "w") (setq n (* n 7) what "d"))
  9368. ;; Preparation, see if we need to modify the start date for the change
  9369. (when (match-end 1)
  9370. (setq time (save-match-data (org-time-string-to-time ts)))
  9371. (cond
  9372. ((equal (match-string 1 ts) ".")
  9373. ;; Shift starting date to today
  9374. (org-timestamp-change
  9375. (- (time-to-days (current-time)) (time-to-days time))
  9376. 'day))
  9377. ((equal (match-string 1 ts) "+")
  9378. (while (or (= nshift 0)
  9379. (<= (time-to-days time) (time-to-days (current-time))))
  9380. (when (= (incf nshift) nshiftmax)
  9381. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9382. (error "Abort")))
  9383. (org-timestamp-change n (cdr (assoc what whata)))
  9384. (org-at-timestamp-p t)
  9385. (setq ts (match-string 1))
  9386. (setq time (save-match-data (org-time-string-to-time ts))))
  9387. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9388. ;; rematch, so that we have everything in place for the real shift
  9389. (org-at-timestamp-p t)
  9390. (setq ts (match-string 1))
  9391. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9392. (org-timestamp-change n (cdr (assoc what whata)))
  9393. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9394. (setq org-log-post-message msg)
  9395. (message "%s" msg))))
  9396. (defun org-show-todo-tree (arg)
  9397. "Make a compact tree which shows all headlines marked with TODO.
  9398. The tree will show the lines where the regexp matches, and all higher
  9399. headlines above the match.
  9400. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9401. With a numeric prefix N, construct a sparse tree for the Nth element
  9402. of `org-todo-keywords-1'."
  9403. (interactive "P")
  9404. (let ((case-fold-search nil)
  9405. (kwd-re
  9406. (cond ((null arg) org-not-done-regexp)
  9407. ((equal arg '(4))
  9408. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9409. (mapcar 'list org-todo-keywords-1))))
  9410. (concat "\\("
  9411. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9412. "\\)\\>")))
  9413. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9414. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9415. org-todo-keywords-1)))
  9416. (t (error "Invalid prefix argument: %s" arg)))))
  9417. (message "%d TODO entries found"
  9418. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9419. (defun org-deadline (&optional remove time)
  9420. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9421. With argument REMOVE, remove any deadline from the item.
  9422. When TIME is set, it should be an internal time specification, and the
  9423. scheduling will use the corresponding date."
  9424. (interactive "P")
  9425. (let ((old-date (org-entry-get nil "DEADLINE")))
  9426. (if remove
  9427. (progn
  9428. (org-remove-timestamp-with-keyword org-deadline-string)
  9429. (message "Item no longer has a deadline."))
  9430. (if (org-get-repeat)
  9431. (error "Cannot change deadline on task with repeater, please do that by hand")
  9432. (org-add-planning-info 'deadline time 'closed)
  9433. (when (and old-date org-log-redeadline
  9434. (not (equal old-date
  9435. (substring org-last-inserted-timestamp 1 -1))))
  9436. (org-add-log-setup 'redeadline nil old-date 'findpos
  9437. org-log-redeadline))
  9438. (message "Deadline on %s" org-last-inserted-timestamp)))))
  9439. (defun org-schedule (&optional remove time)
  9440. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9441. With argument REMOVE, remove any scheduling date from the item.
  9442. When TIME is set, it should be an internal time specification, and the
  9443. scheduling will use the corresponding date."
  9444. (interactive "P")
  9445. (let ((old-date (org-entry-get nil "SCHEDULED")))
  9446. (if remove
  9447. (progn
  9448. (org-remove-timestamp-with-keyword org-scheduled-string)
  9449. (message "Item is no longer scheduled."))
  9450. (if (org-get-repeat)
  9451. (error "Cannot reschedule task with repeater, please do that by hand")
  9452. (org-add-planning-info 'scheduled time 'closed)
  9453. (when (and old-date org-log-reschedule
  9454. (not (equal old-date
  9455. (substring org-last-inserted-timestamp 1 -1))))
  9456. (org-add-log-setup 'reschedule nil old-date 'findpos
  9457. org-log-reschedule))
  9458. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  9459. (defun org-get-scheduled-time (pom &optional inherit)
  9460. "Get the scheduled time as a time tuple, of a format suitable
  9461. for calling org-schedule with, or if there is no scheduling,
  9462. returns nil."
  9463. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9464. (when time
  9465. (apply 'encode-time (org-parse-time-string time)))))
  9466. (defun org-get-deadline-time (pom &optional inherit)
  9467. "Get the deadine as a time tuple, of a format suitable for
  9468. calling org-deadline with, or if there is no scheduling, returns
  9469. nil."
  9470. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9471. (when time
  9472. (apply 'encode-time (org-parse-time-string time)))))
  9473. (defun org-remove-timestamp-with-keyword (keyword)
  9474. "Remove all time stamps with KEYWORD in the current entry."
  9475. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9476. beg)
  9477. (save-excursion
  9478. (org-back-to-heading t)
  9479. (setq beg (point))
  9480. (outline-next-heading)
  9481. (while (re-search-backward re beg t)
  9482. (replace-match "")
  9483. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9484. (equal (char-before) ?\ ))
  9485. (backward-delete-char 1)
  9486. (if (string-match "^[ \t]*$" (buffer-substring
  9487. (point-at-bol) (point-at-eol)))
  9488. (delete-region (point-at-bol)
  9489. (min (point-max) (1+ (point-at-eol))))))))))
  9490. (defun org-add-planning-info (what &optional time &rest remove)
  9491. "Insert new timestamp with keyword in the line directly after the headline.
  9492. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9493. If non is given, the user is prompted for a date.
  9494. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9495. be removed."
  9496. (interactive)
  9497. (let (org-time-was-given org-end-time-was-given ts
  9498. end default-time default-input)
  9499. (catch 'exit
  9500. (when (and (not time) (memq what '(scheduled deadline)))
  9501. ;; Try to get a default date/time from existing timestamp
  9502. (save-excursion
  9503. (org-back-to-heading t)
  9504. (setq end (save-excursion (outline-next-heading) (point)))
  9505. (when (re-search-forward (if (eq what 'scheduled)
  9506. org-scheduled-time-regexp
  9507. org-deadline-time-regexp)
  9508. end t)
  9509. (setq ts (match-string 1)
  9510. default-time
  9511. (apply 'encode-time (org-parse-time-string ts))
  9512. default-input (and ts (org-get-compact-tod ts))))))
  9513. (when what
  9514. ;; If necessary, get the time from the user
  9515. (setq time (or time (org-read-date nil 'to-time nil nil
  9516. default-time default-input))))
  9517. (when (and org-insert-labeled-timestamps-at-point
  9518. (member what '(scheduled deadline)))
  9519. (insert
  9520. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9521. (org-insert-time-stamp time org-time-was-given
  9522. nil nil nil (list org-end-time-was-given))
  9523. (setq what nil))
  9524. (save-excursion
  9525. (save-restriction
  9526. (let (col list elt ts buffer-invisibility-spec)
  9527. (org-back-to-heading t)
  9528. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9529. (goto-char (match-end 1))
  9530. (setq col (current-column))
  9531. (goto-char (match-end 0))
  9532. (if (eobp) (insert "\n") (forward-char 1))
  9533. (when (and (not what)
  9534. (not (looking-at
  9535. (concat "[ \t]*"
  9536. org-keyword-time-not-clock-regexp))))
  9537. ;; Nothing to add, nothing to remove...... :-)
  9538. (throw 'exit nil))
  9539. (if (and (not (looking-at outline-regexp))
  9540. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9541. "[^\r\n]*"))
  9542. (not (equal (match-string 1) org-clock-string)))
  9543. (narrow-to-region (match-beginning 0) (match-end 0))
  9544. (insert-before-markers "\n")
  9545. (backward-char 1)
  9546. (narrow-to-region (point) (point))
  9547. (and org-adapt-indentation (org-indent-to-column col)))
  9548. ;; Check if we have to remove something.
  9549. (setq list (cons what remove))
  9550. (while list
  9551. (setq elt (pop list))
  9552. (goto-char (point-min))
  9553. (when (or (and (eq elt 'scheduled)
  9554. (re-search-forward org-scheduled-time-regexp nil t))
  9555. (and (eq elt 'deadline)
  9556. (re-search-forward org-deadline-time-regexp nil t))
  9557. (and (eq elt 'closed)
  9558. (re-search-forward org-closed-time-regexp nil t)))
  9559. (replace-match "")
  9560. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9561. (skip-chars-backward " ")
  9562. (if (looking-at " +") (replace-match ""))))
  9563. (goto-char (point-max))
  9564. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9565. (when what
  9566. (insert
  9567. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9568. (cond ((eq what 'scheduled) org-scheduled-string)
  9569. ((eq what 'deadline) org-deadline-string)
  9570. ((eq what 'closed) org-closed-string))
  9571. " ")
  9572. (setq ts (org-insert-time-stamp
  9573. time
  9574. (or org-time-was-given
  9575. (and (eq what 'closed) org-log-done-with-time))
  9576. (eq what 'closed)
  9577. nil nil (list org-end-time-was-given)))
  9578. (end-of-line 1))
  9579. (goto-char (point-min))
  9580. (widen)
  9581. (if (and (looking-at "[ \t]+\n")
  9582. (equal (char-before) ?\n))
  9583. (delete-region (1- (point)) (point-at-eol)))
  9584. ts))))))
  9585. (defvar org-log-note-marker (make-marker))
  9586. (defvar org-log-note-purpose nil)
  9587. (defvar org-log-note-state nil)
  9588. (defvar org-log-note-previous-state nil)
  9589. (defvar org-log-note-how nil)
  9590. (defvar org-log-note-extra nil)
  9591. (defvar org-log-note-window-configuration nil)
  9592. (defvar org-log-note-return-to (make-marker))
  9593. (defvar org-log-post-message nil
  9594. "Message to be displayed after a log note has been stored.
  9595. The auto-repeater uses this.")
  9596. (defun org-add-note ()
  9597. "Add a note to the current entry.
  9598. This is done in the same way as adding a state change note."
  9599. (interactive)
  9600. (org-add-log-setup 'note nil nil 'findpos nil))
  9601. (defvar org-property-end-re)
  9602. (defun org-add-log-setup (&optional purpose state prev-state
  9603. findpos how &optional extra)
  9604. "Set up the post command hook to take a note.
  9605. If this is about to TODO state change, the new state is expected in STATE.
  9606. When FINDPOS is non-nil, find the correct position for the note in
  9607. the current entry. If not, assume that it can be inserted at point.
  9608. HOW is an indicator what kind of note should be created.
  9609. EXTRA is additional text that will be inserted into the notes buffer."
  9610. (let* ((org-log-into-drawer (org-log-into-drawer))
  9611. (drawer (cond ((stringp org-log-into-drawer)
  9612. org-log-into-drawer)
  9613. (org-log-into-drawer "LOGBOOK")
  9614. (t nil))))
  9615. (save-restriction
  9616. (save-excursion
  9617. (when findpos
  9618. (org-back-to-heading t)
  9619. (narrow-to-region (point) (save-excursion
  9620. (outline-next-heading) (point)))
  9621. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9622. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9623. "[^\r\n]*\\)?"))
  9624. (goto-char (match-end 0))
  9625. (cond
  9626. (drawer
  9627. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9628. nil t)
  9629. (progn
  9630. (goto-char (match-end 0))
  9631. (or org-log-states-order-reversed
  9632. (and (re-search-forward org-property-end-re nil t)
  9633. (goto-char (1- (match-beginning 0))))))
  9634. (insert "\n:" drawer ":\n:END:")
  9635. (beginning-of-line 0)
  9636. (org-indent-line-function)
  9637. (beginning-of-line 2)
  9638. (org-indent-line-function)
  9639. (end-of-line 0)))
  9640. ((and org-log-state-notes-insert-after-drawers
  9641. (save-excursion
  9642. (forward-line) (looking-at org-drawer-regexp)))
  9643. (forward-line)
  9644. (while (looking-at org-drawer-regexp)
  9645. (goto-char (match-end 0))
  9646. (re-search-forward org-property-end-re (point-max) t)
  9647. (forward-line))
  9648. (forward-line -1)))
  9649. (unless org-log-states-order-reversed
  9650. (and (= (char-after) ?\n) (forward-char 1))
  9651. (org-skip-over-state-notes)
  9652. (skip-chars-backward " \t\n\r")))
  9653. (move-marker org-log-note-marker (point))
  9654. (setq org-log-note-purpose purpose
  9655. org-log-note-state state
  9656. org-log-note-previous-state prev-state
  9657. org-log-note-how how
  9658. org-log-note-extra extra)
  9659. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9660. (defun org-skip-over-state-notes ()
  9661. "Skip past the list of State notes in an entry."
  9662. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9663. (while (looking-at "[ \t]*- State")
  9664. (condition-case nil
  9665. (org-next-item)
  9666. (error (org-end-of-item)))))
  9667. (defun org-add-log-note (&optional purpose)
  9668. "Pop up a window for taking a note, and add this note later at point."
  9669. (remove-hook 'post-command-hook 'org-add-log-note)
  9670. (setq org-log-note-window-configuration (current-window-configuration))
  9671. (delete-other-windows)
  9672. (move-marker org-log-note-return-to (point))
  9673. (switch-to-buffer (marker-buffer org-log-note-marker))
  9674. (goto-char org-log-note-marker)
  9675. (org-switch-to-buffer-other-window "*Org Note*")
  9676. (erase-buffer)
  9677. (if (memq org-log-note-how '(time state))
  9678. (let (current-prefix-arg) (org-store-log-note))
  9679. (let ((org-inhibit-startup t)) (org-mode))
  9680. (insert (format "# Insert note for %s.
  9681. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9682. (cond
  9683. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9684. ((eq org-log-note-purpose 'done) "closed todo item")
  9685. ((eq org-log-note-purpose 'state)
  9686. (format "state change from \"%s\" to \"%s\""
  9687. (or org-log-note-previous-state "")
  9688. (or org-log-note-state "")))
  9689. ((eq org-log-note-purpose 'reschedule)
  9690. "rescheduling")
  9691. ((eq org-log-note-purpose 'redeadline)
  9692. "changing deadline")
  9693. ((eq org-log-note-purpose 'note)
  9694. "this entry")
  9695. (t (error "This should not happen")))))
  9696. (if org-log-note-extra (insert org-log-note-extra))
  9697. (org-set-local 'org-finish-function 'org-store-log-note)))
  9698. (defvar org-note-abort nil) ; dynamically scoped
  9699. (defun org-store-log-note ()
  9700. "Finish taking a log note, and insert it to where it belongs."
  9701. (let ((txt (buffer-string))
  9702. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9703. lines ind)
  9704. (kill-buffer (current-buffer))
  9705. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9706. (setq txt (replace-match "" t t txt)))
  9707. (if (string-match "\\s-+\\'" txt)
  9708. (setq txt (replace-match "" t t txt)))
  9709. (setq lines (org-split-string txt "\n"))
  9710. (when (and note (string-match "\\S-" note))
  9711. (setq note
  9712. (org-replace-escapes
  9713. note
  9714. (list (cons "%u" (user-login-name))
  9715. (cons "%U" user-full-name)
  9716. (cons "%t" (format-time-string
  9717. (org-time-stamp-format 'long 'inactive)
  9718. (current-time)))
  9719. (cons "%s" (if org-log-note-state
  9720. (concat "\"" org-log-note-state "\"")
  9721. ""))
  9722. (cons "%S" (if org-log-note-previous-state
  9723. (concat "\"" org-log-note-previous-state "\"")
  9724. "\"\"")))))
  9725. (if lines (setq note (concat note " \\\\")))
  9726. (push note lines))
  9727. (when (or current-prefix-arg org-note-abort)
  9728. (when org-log-into-drawer
  9729. (org-remove-empty-drawer-at
  9730. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9731. org-log-note-marker))
  9732. (setq lines nil))
  9733. (when lines
  9734. (with-current-buffer (marker-buffer org-log-note-marker)
  9735. (save-excursion
  9736. (goto-char org-log-note-marker)
  9737. (move-marker org-log-note-marker nil)
  9738. (end-of-line 1)
  9739. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9740. (insert "- " (pop lines))
  9741. (org-indent-line-function)
  9742. (beginning-of-line 1)
  9743. (looking-at "[ \t]*")
  9744. (setq ind (concat (match-string 0) " "))
  9745. (end-of-line 1)
  9746. (while lines (insert "\n" ind (pop lines)))
  9747. (message "Note stored")
  9748. (org-back-to-heading t)
  9749. (org-cycle-hide-drawers 'children)))))
  9750. (set-window-configuration org-log-note-window-configuration)
  9751. (with-current-buffer (marker-buffer org-log-note-return-to)
  9752. (goto-char org-log-note-return-to))
  9753. (move-marker org-log-note-return-to nil)
  9754. (and org-log-post-message (message "%s" org-log-post-message)))
  9755. (defun org-remove-empty-drawer-at (drawer pos)
  9756. "Remove an empty drawer DRAWER at position POS.
  9757. POS may also be a marker."
  9758. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9759. (save-excursion
  9760. (save-restriction
  9761. (widen)
  9762. (goto-char pos)
  9763. (if (org-in-regexp
  9764. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9765. (replace-match ""))))))
  9766. (defun org-sparse-tree (&optional arg)
  9767. "Create a sparse tree, prompt for the details.
  9768. This command can create sparse trees. You first need to select the type
  9769. of match used to create the tree:
  9770. t Show entries with a specific TODO keyword.
  9771. m Show entries selected by a tags/property match.
  9772. p Enter a property name and its value (both with completion on existing
  9773. names/values) and show entries with that property.
  9774. / Show entries matching a regular expression (`r' can be used as well)
  9775. d Show deadlines due within `org-deadline-warning-days'.
  9776. b Show deadlines and scheduled items before a date.
  9777. a Show deadlines and scheduled items after a date."
  9778. (interactive "P")
  9779. (let (ans kwd value)
  9780. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9781. (setq ans (read-char-exclusive))
  9782. (cond
  9783. ((equal ans ?d)
  9784. (call-interactively 'org-check-deadlines))
  9785. ((equal ans ?b)
  9786. (call-interactively 'org-check-before-date))
  9787. ((equal ans ?a)
  9788. (call-interactively 'org-check-after-date))
  9789. ((equal ans ?t)
  9790. (org-show-todo-tree '(4)))
  9791. ((member ans '(?T ?m))
  9792. (call-interactively 'org-match-sparse-tree))
  9793. ((member ans '(?p ?P))
  9794. (setq kwd (org-icompleting-read "Property: "
  9795. (mapcar 'list (org-buffer-property-keys))))
  9796. (setq value (org-icompleting-read "Value: "
  9797. (mapcar 'list (org-property-values kwd))))
  9798. (unless (string-match "\\`{.*}\\'" value)
  9799. (setq value (concat "\"" value "\"")))
  9800. (org-match-sparse-tree arg (concat kwd "=" value)))
  9801. ((member ans '(?r ?R ?/))
  9802. (call-interactively 'org-occur))
  9803. (t (error "No such sparse tree command \"%c\"" ans)))))
  9804. (defvar org-occur-highlights nil
  9805. "List of overlays used for occur matches.")
  9806. (make-variable-buffer-local 'org-occur-highlights)
  9807. (defvar org-occur-parameters nil
  9808. "Parameters of the active org-occur calls.
  9809. This is a list, each call to org-occur pushes as cons cell,
  9810. containing the regular expression and the callback, onto the list.
  9811. The list can contain several entries if `org-occur' has been called
  9812. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9813. will only contain one set of parameters. When the highlights are
  9814. removed (for example with `C-c C-c', or with the next edit (depending
  9815. on `org-remove-highlights-with-change'), this variable is emptied
  9816. as well.")
  9817. (make-variable-buffer-local 'org-occur-parameters)
  9818. (defun org-occur (regexp &optional keep-previous callback)
  9819. "Make a compact tree which shows all matches of REGEXP.
  9820. The tree will show the lines where the regexp matches, and all higher
  9821. headlines above the match. It will also show the heading after the match,
  9822. to make sure editing the matching entry is easy.
  9823. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9824. call to `org-occur' will be kept, to allow stacking of calls to this
  9825. command.
  9826. If CALLBACK is non-nil, it is a function which is called to confirm
  9827. that the match should indeed be shown."
  9828. (interactive "sRegexp: \nP")
  9829. (when (equal regexp "")
  9830. (error "Regexp cannot be empty"))
  9831. (unless keep-previous
  9832. (org-remove-occur-highlights nil nil t))
  9833. (push (cons regexp callback) org-occur-parameters)
  9834. (let ((cnt 0))
  9835. (save-excursion
  9836. (goto-char (point-min))
  9837. (if (or (not keep-previous) ; do not want to keep
  9838. (not org-occur-highlights)) ; no previous matches
  9839. ;; hide everything
  9840. (org-overview))
  9841. (while (re-search-forward regexp nil t)
  9842. (when (or (not callback)
  9843. (save-match-data (funcall callback)))
  9844. (setq cnt (1+ cnt))
  9845. (when org-highlight-sparse-tree-matches
  9846. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9847. (org-show-context 'occur-tree))))
  9848. (when org-remove-highlights-with-change
  9849. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9850. nil 'local))
  9851. (unless org-sparse-tree-open-archived-trees
  9852. (org-hide-archived-subtrees (point-min) (point-max)))
  9853. (run-hooks 'org-occur-hook)
  9854. (if (interactive-p)
  9855. (message "%d match(es) for regexp %s" cnt regexp))
  9856. cnt))
  9857. (defun org-show-context (&optional key)
  9858. "Make sure point and context and visible.
  9859. How much context is shown depends upon the variables
  9860. `org-show-hierarchy-above', `org-show-following-heading'. and
  9861. `org-show-siblings'."
  9862. (let ((heading-p (org-on-heading-p t))
  9863. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9864. (following-p (org-get-alist-option org-show-following-heading key))
  9865. (entry-p (org-get-alist-option org-show-entry-below key))
  9866. (siblings-p (org-get-alist-option org-show-siblings key)))
  9867. (catch 'exit
  9868. ;; Show heading or entry text
  9869. (if (and heading-p (not entry-p))
  9870. (org-flag-heading nil) ; only show the heading
  9871. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9872. (org-show-hidden-entry))) ; show entire entry
  9873. (when following-p
  9874. ;; Show next sibling, or heading below text
  9875. (save-excursion
  9876. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9877. (org-flag-heading nil))))
  9878. (when siblings-p (org-show-siblings))
  9879. (when hierarchy-p
  9880. ;; show all higher headings, possibly with siblings
  9881. (save-excursion
  9882. (while (and (condition-case nil
  9883. (progn (org-up-heading-all 1) t)
  9884. (error nil))
  9885. (not (bobp)))
  9886. (org-flag-heading nil)
  9887. (when siblings-p (org-show-siblings))))))))
  9888. (defun org-reveal (&optional siblings)
  9889. "Show current entry, hierarchy above it, and the following headline.
  9890. This can be used to show a consistent set of context around locations
  9891. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9892. not t for the search context.
  9893. With optional argument SIBLINGS, on each level of the hierarchy all
  9894. siblings are shown. This repairs the tree structure to what it would
  9895. look like when opened with hierarchical calls to `org-cycle'."
  9896. (interactive "P")
  9897. (let ((org-show-hierarchy-above t)
  9898. (org-show-following-heading t)
  9899. (org-show-siblings (if siblings t org-show-siblings)))
  9900. (org-show-context nil)))
  9901. (defun org-highlight-new-match (beg end)
  9902. "Highlight from BEG to END and mark the highlight is an occur headline."
  9903. (let ((ov (org-make-overlay beg end)))
  9904. (org-overlay-put ov 'face 'secondary-selection)
  9905. (push ov org-occur-highlights)))
  9906. (defun org-remove-occur-highlights (&optional beg end noremove)
  9907. "Remove the occur highlights from the buffer.
  9908. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9909. from the `before-change-functions' in the current buffer."
  9910. (interactive)
  9911. (unless org-inhibit-highlight-removal
  9912. (mapc 'org-delete-overlay org-occur-highlights)
  9913. (setq org-occur-highlights nil)
  9914. (setq org-occur-parameters nil)
  9915. (unless noremove
  9916. (remove-hook 'before-change-functions
  9917. 'org-remove-occur-highlights 'local))))
  9918. ;;;; Priorities
  9919. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9920. "Regular expression matching the priority indicator.")
  9921. (defvar org-remove-priority-next-time nil)
  9922. (defun org-priority-up ()
  9923. "Increase the priority of the current item."
  9924. (interactive)
  9925. (org-priority 'up))
  9926. (defun org-priority-down ()
  9927. "Decrease the priority of the current item."
  9928. (interactive)
  9929. (org-priority 'down))
  9930. (defun org-priority (&optional action)
  9931. "Change the priority of an item by ARG.
  9932. ACTION can be `set', `up', `down', or a character."
  9933. (interactive)
  9934. (unless org-enable-priority-commands
  9935. (error "Priority commands are disabled"))
  9936. (setq action (or action 'set))
  9937. (let (current new news have remove)
  9938. (save-excursion
  9939. (org-back-to-heading t)
  9940. (if (looking-at org-priority-regexp)
  9941. (setq current (string-to-char (match-string 2))
  9942. have t)
  9943. (setq current org-default-priority))
  9944. (cond
  9945. ((eq action 'remove)
  9946. (setq remove t new ?\ ))
  9947. ((or (eq action 'set)
  9948. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9949. (if (not (eq action 'set))
  9950. (setq new action)
  9951. (message "Priority %c-%c, SPC to remove: "
  9952. org-highest-priority org-lowest-priority)
  9953. (setq new (read-char-exclusive)))
  9954. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9955. (= (upcase org-lowest-priority) org-lowest-priority))
  9956. (setq new (upcase new)))
  9957. (cond ((equal new ?\ ) (setq remove t))
  9958. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9959. (error "Priority must be between `%c' and `%c'"
  9960. org-highest-priority org-lowest-priority))))
  9961. ((eq action 'up)
  9962. (if (and (not have) (eq last-command this-command))
  9963. (setq new org-lowest-priority)
  9964. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9965. org-default-priority (1- current)))))
  9966. ((eq action 'down)
  9967. (if (and (not have) (eq last-command this-command))
  9968. (setq new org-highest-priority)
  9969. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9970. org-default-priority (1+ current)))))
  9971. (t (error "Invalid action")))
  9972. (if (or (< (upcase new) org-highest-priority)
  9973. (> (upcase new) org-lowest-priority))
  9974. (setq remove t))
  9975. (setq news (format "%c" new))
  9976. (if have
  9977. (if remove
  9978. (replace-match "" t t nil 1)
  9979. (replace-match news t t nil 2))
  9980. (if remove
  9981. (error "No priority cookie found in line")
  9982. (let ((case-fold-search nil))
  9983. (looking-at org-todo-line-regexp))
  9984. (if (match-end 2)
  9985. (progn
  9986. (goto-char (match-end 2))
  9987. (insert " [#" news "]"))
  9988. (goto-char (match-beginning 3))
  9989. (insert "[#" news "] "))))
  9990. (org-preserve-lc (org-set-tags nil 'align)))
  9991. (if remove
  9992. (message "Priority removed")
  9993. (message "Priority of current item set to %s" news))))
  9994. (defun org-get-priority (s)
  9995. "Find priority cookie and return priority."
  9996. (save-match-data
  9997. (if (not (string-match org-priority-regexp s))
  9998. (* 1000 (- org-lowest-priority org-default-priority))
  9999. (* 1000 (- org-lowest-priority
  10000. (string-to-char (match-string 2 s)))))))
  10001. ;;;; Tags
  10002. (defvar org-agenda-archives-mode)
  10003. (defvar org-map-continue-from nil
  10004. "Position from where mapping should continue.
  10005. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10006. (defvar org-scanner-tags nil
  10007. "The current tag list while the tags scanner is running.")
  10008. (defvar org-trust-scanner-tags nil
  10009. "Should `org-get-tags-at' use the tags fro the scanner.
  10010. This is for internal dynamical scoping only.
  10011. When this is non-nil, the function `org-get-tags-at' will return the value
  10012. of `org-scanner-tags' instead of building the list by itself. This
  10013. can lead to large speed-ups when the tags scanner is used in a file with
  10014. many entries, and when the list of tags is retrieved, for example to
  10015. obtain a list of properties. Building the tags list for each entry in such
  10016. a file becomes an N^2 operation - but with this variable set, it scales
  10017. as N.")
  10018. (defun org-scan-tags (action matcher &optional todo-only)
  10019. "Scan headline tags with inheritance and produce output ACTION.
  10020. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10021. or `agenda' to produce an entry list for an agenda view. It can also be
  10022. a Lisp form or a function that should be called at each matched headline, in
  10023. this case the return value is a list of all return values from these calls.
  10024. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10025. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10026. only lines with a TODO keyword are included in the output."
  10027. (require 'org-agenda)
  10028. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10029. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10030. (org-re
  10031. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10032. (props (list 'face 'default
  10033. 'done-face 'org-agenda-done
  10034. 'undone-face 'default
  10035. 'mouse-face 'highlight
  10036. 'org-not-done-regexp org-not-done-regexp
  10037. 'org-todo-regexp org-todo-regexp
  10038. 'help-echo
  10039. (format "mouse-2 or RET jump to org file %s"
  10040. (abbreviate-file-name
  10041. (or (buffer-file-name (buffer-base-buffer))
  10042. (buffer-name (buffer-base-buffer)))))))
  10043. (case-fold-search nil)
  10044. (org-map-continue-from nil)
  10045. lspos tags tags-list
  10046. (tags-alist (list (cons 0 org-file-tags)))
  10047. (llast 0) rtn rtn1 level category i txt
  10048. todo marker entry priority)
  10049. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10050. (setq action (list 'lambda nil action)))
  10051. (save-excursion
  10052. (goto-char (point-min))
  10053. (when (eq action 'sparse-tree)
  10054. (org-overview)
  10055. (org-remove-occur-highlights))
  10056. (while (re-search-forward re nil t)
  10057. (catch :skip
  10058. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10059. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10060. (goto-char (setq lspos (match-beginning 0)))
  10061. (setq level (org-reduced-level (funcall outline-level))
  10062. category (org-get-category))
  10063. (setq i llast llast level)
  10064. ;; remove tag lists from same and sublevels
  10065. (while (>= i level)
  10066. (when (setq entry (assoc i tags-alist))
  10067. (setq tags-alist (delete entry tags-alist)))
  10068. (setq i (1- i)))
  10069. ;; add the next tags
  10070. (when tags
  10071. (setq tags (org-split-string tags ":")
  10072. tags-alist
  10073. (cons (cons level tags) tags-alist)))
  10074. ;; compile tags for current headline
  10075. (setq tags-list
  10076. (if org-use-tag-inheritance
  10077. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10078. tags)
  10079. org-scanner-tags tags-list)
  10080. (when org-use-tag-inheritance
  10081. (setcdr (car tags-alist)
  10082. (mapcar (lambda (x)
  10083. (setq x (copy-sequence x))
  10084. (org-add-prop-inherited x))
  10085. (cdar tags-alist))))
  10086. (when (and tags org-use-tag-inheritance
  10087. (or (not (eq t org-use-tag-inheritance))
  10088. org-tags-exclude-from-inheritance))
  10089. ;; selective inheritance, remove uninherited ones
  10090. (setcdr (car tags-alist)
  10091. (org-remove-uniherited-tags (cdar tags-alist))))
  10092. (when (and (or (not todo-only)
  10093. (and (member todo org-not-done-keywords)
  10094. (or (not org-agenda-tags-todo-honor-ignore-options)
  10095. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10096. (let ((case-fold-search t)) (eval matcher))
  10097. (or
  10098. (not (member org-archive-tag tags-list))
  10099. ;; we have an archive tag, should we use this anyway?
  10100. (or (not org-agenda-skip-archived-trees)
  10101. (and (eq action 'agenda) org-agenda-archives-mode))))
  10102. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10103. ;; select this headline
  10104. (cond
  10105. ((eq action 'sparse-tree)
  10106. (and org-highlight-sparse-tree-matches
  10107. (org-get-heading) (match-end 0)
  10108. (org-highlight-new-match
  10109. (match-beginning 0) (match-beginning 1)))
  10110. (org-show-context 'tags-tree))
  10111. ((eq action 'agenda)
  10112. (setq txt (org-format-agenda-item
  10113. ""
  10114. (concat
  10115. (if (eq org-tags-match-list-sublevels 'indented)
  10116. (make-string (1- level) ?.) "")
  10117. (org-get-heading))
  10118. category
  10119. tags-list
  10120. )
  10121. priority (org-get-priority txt))
  10122. (goto-char lspos)
  10123. (setq marker (org-agenda-new-marker))
  10124. (org-add-props txt props
  10125. 'org-marker marker 'org-hd-marker marker 'org-category category
  10126. 'todo-state todo
  10127. 'priority priority 'type "tagsmatch")
  10128. (push txt rtn))
  10129. ((functionp action)
  10130. (setq org-map-continue-from nil)
  10131. (save-excursion
  10132. (setq rtn1 (funcall action))
  10133. (push rtn1 rtn)))
  10134. (t (error "Invalid action")))
  10135. ;; if we are to skip sublevels, jump to end of subtree
  10136. (unless org-tags-match-list-sublevels
  10137. (org-end-of-subtree t)
  10138. (backward-char 1))))
  10139. ;; Get the correct position from where to continue
  10140. (if org-map-continue-from
  10141. (goto-char org-map-continue-from)
  10142. (and (= (point) lspos) (end-of-line 1)))))
  10143. (when (and (eq action 'sparse-tree)
  10144. (not org-sparse-tree-open-archived-trees))
  10145. (org-hide-archived-subtrees (point-min) (point-max)))
  10146. (nreverse rtn)))
  10147. (defun org-remove-uniherited-tags (tags)
  10148. "Remove all tags that are not inherited from the list TAGS."
  10149. (cond
  10150. ((eq org-use-tag-inheritance t)
  10151. (if org-tags-exclude-from-inheritance
  10152. (org-delete-all org-tags-exclude-from-inheritance tags)
  10153. tags))
  10154. ((not org-use-tag-inheritance) nil)
  10155. ((stringp org-use-tag-inheritance)
  10156. (delq nil (mapcar
  10157. (lambda (x)
  10158. (if (and (string-match org-use-tag-inheritance x)
  10159. (not (member x org-tags-exclude-from-inheritance)))
  10160. x nil))
  10161. tags)))
  10162. ((listp org-use-tag-inheritance)
  10163. (delq nil (mapcar
  10164. (lambda (x)
  10165. (if (member x org-use-tag-inheritance) x nil))
  10166. tags)))))
  10167. (defvar todo-only) ;; dynamically scoped
  10168. (defun org-match-sparse-tree (&optional todo-only match)
  10169. "Create a sparse tree according to tags string MATCH.
  10170. MATCH can contain positive and negative selection of tags, like
  10171. \"+WORK+URGENT-WITHBOSS\".
  10172. If optional argument TODO-ONLY is non-nil, only select lines that are
  10173. also TODO lines."
  10174. (interactive "P")
  10175. (org-prepare-agenda-buffers (list (current-buffer)))
  10176. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10177. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10178. (defvar org-cached-props nil)
  10179. (defun org-cached-entry-get (pom property)
  10180. (if (or (eq t org-use-property-inheritance)
  10181. (and (stringp org-use-property-inheritance)
  10182. (string-match org-use-property-inheritance property))
  10183. (and (listp org-use-property-inheritance)
  10184. (member property org-use-property-inheritance)))
  10185. ;; Caching is not possible, check it directly
  10186. (org-entry-get pom property 'inherit)
  10187. ;; Get all properties, so that we can do complicated checks easily
  10188. (cdr (assoc property (or org-cached-props
  10189. (setq org-cached-props
  10190. (org-entry-properties pom)))))))
  10191. (defun org-global-tags-completion-table (&optional files)
  10192. "Return the list of all tags in all agenda buffer/files."
  10193. (save-excursion
  10194. (org-uniquify
  10195. (delq nil
  10196. (apply 'append
  10197. (mapcar
  10198. (lambda (file)
  10199. (set-buffer (find-file-noselect file))
  10200. (append (org-get-buffer-tags)
  10201. (mapcar (lambda (x) (if (stringp (car-safe x))
  10202. (list (car-safe x)) nil))
  10203. org-tag-alist)))
  10204. (if (and files (car files))
  10205. files
  10206. (org-agenda-files))))))))
  10207. (defun org-make-tags-matcher (match)
  10208. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10209. ;; todo-only is scoped dynamically into this function, and the function
  10210. ;; may change it if the matcher asks for it.
  10211. (unless match
  10212. ;; Get a new match request, with completion
  10213. (let ((org-last-tags-completion-table
  10214. (org-global-tags-completion-table)))
  10215. (setq match (org-completing-read-no-i
  10216. "Match: " 'org-tags-completion-function nil nil nil
  10217. 'org-tags-history))))
  10218. ;; Parse the string and create a lisp form
  10219. (let ((match0 match)
  10220. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10221. minus tag mm
  10222. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10223. orterms term orlist re-p str-p level-p level-op time-p
  10224. prop-p pn pv po cat-p gv rest)
  10225. (if (string-match "/+" match)
  10226. ;; match contains also a todo-matching request
  10227. (progn
  10228. (setq tagsmatch (substring match 0 (match-beginning 0))
  10229. todomatch (substring match (match-end 0)))
  10230. (if (string-match "^!" todomatch)
  10231. (setq todo-only t todomatch (substring todomatch 1)))
  10232. (if (string-match "^\\s-*$" todomatch)
  10233. (setq todomatch nil)))
  10234. ;; only matching tags
  10235. (setq tagsmatch match todomatch nil))
  10236. ;; Make the tags matcher
  10237. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10238. (setq tagsmatcher t)
  10239. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10240. (while (setq term (pop orterms))
  10241. (while (and (equal (substring term -1) "\\") orterms)
  10242. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10243. (while (string-match re term)
  10244. (setq rest (substring term (match-end 0))
  10245. minus (and (match-end 1)
  10246. (equal (match-string 1 term) "-"))
  10247. tag (match-string 2 term)
  10248. re-p (equal (string-to-char tag) ?{)
  10249. level-p (match-end 4)
  10250. prop-p (match-end 5)
  10251. mm (cond
  10252. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10253. (level-p
  10254. (setq level-op (org-op-to-function (match-string 3 term)))
  10255. `(,level-op level ,(string-to-number
  10256. (match-string 4 term))))
  10257. (prop-p
  10258. (setq pn (match-string 5 term)
  10259. po (match-string 6 term)
  10260. pv (match-string 7 term)
  10261. cat-p (equal pn "CATEGORY")
  10262. re-p (equal (string-to-char pv) ?{)
  10263. str-p (equal (string-to-char pv) ?\")
  10264. time-p (save-match-data
  10265. (string-match "^\"[[<].*[]>]\"$" pv))
  10266. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10267. (if time-p (setq pv (org-matcher-time pv)))
  10268. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10269. (cond
  10270. ((equal pn "CATEGORY")
  10271. (setq gv '(get-text-property (point) 'org-category)))
  10272. ((equal pn "TODO")
  10273. (setq gv 'todo))
  10274. (t
  10275. (setq gv `(org-cached-entry-get nil ,pn))))
  10276. (if re-p
  10277. (if (eq po 'org<>)
  10278. `(not (string-match ,pv (or ,gv "")))
  10279. `(string-match ,pv (or ,gv "")))
  10280. (if str-p
  10281. `(,po (or ,gv "") ,pv)
  10282. `(,po (string-to-number (or ,gv ""))
  10283. ,(string-to-number pv) ))))
  10284. (t `(member ,tag tags-list)))
  10285. mm (if minus (list 'not mm) mm)
  10286. term rest)
  10287. (push mm tagsmatcher))
  10288. (push (if (> (length tagsmatcher) 1)
  10289. (cons 'and tagsmatcher)
  10290. (car tagsmatcher))
  10291. orlist)
  10292. (setq tagsmatcher nil))
  10293. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10294. (setq tagsmatcher
  10295. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10296. ;; Make the todo matcher
  10297. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10298. (setq todomatcher t)
  10299. (setq orterms (org-split-string todomatch "|") orlist nil)
  10300. (while (setq term (pop orterms))
  10301. (while (string-match re term)
  10302. (setq minus (and (match-end 1)
  10303. (equal (match-string 1 term) "-"))
  10304. kwd (match-string 2 term)
  10305. re-p (equal (string-to-char kwd) ?{)
  10306. term (substring term (match-end 0))
  10307. mm (if re-p
  10308. `(string-match ,(substring kwd 1 -1) todo)
  10309. (list 'equal 'todo kwd))
  10310. mm (if minus (list 'not mm) mm))
  10311. (push mm todomatcher))
  10312. (push (if (> (length todomatcher) 1)
  10313. (cons 'and todomatcher)
  10314. (car todomatcher))
  10315. orlist)
  10316. (setq todomatcher nil))
  10317. (setq todomatcher (if (> (length orlist) 1)
  10318. (cons 'or orlist) (car orlist))))
  10319. ;; Return the string and lisp forms of the matcher
  10320. (setq matcher (if todomatcher
  10321. (list 'and tagsmatcher todomatcher)
  10322. tagsmatcher))
  10323. (cons match0 matcher)))
  10324. (defun org-op-to-function (op &optional stringp)
  10325. "Turn an operator into the appropriate function."
  10326. (setq op
  10327. (cond
  10328. ((equal op "<" ) '(< string< org-time<))
  10329. ((equal op ">" ) '(> org-string> org-time>))
  10330. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10331. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10332. ((member op '("=" "==")) '(= string= org-time=))
  10333. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10334. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10335. (defun org<> (a b) (not (= a b)))
  10336. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10337. (defun org-string>= (a b) (not (string< a b)))
  10338. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10339. (defun org-string<> (a b) (not (string= a b)))
  10340. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10341. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10342. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10343. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10344. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10345. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10346. (defun org-2ft (s)
  10347. "Convert S to a floating point time.
  10348. If S is already a number, just return it. If it is a string, parse
  10349. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10350. (cond
  10351. ((numberp s) s)
  10352. ((stringp s)
  10353. (condition-case nil
  10354. (float-time (apply 'encode-time (org-parse-time-string s)))
  10355. (error 0.)))
  10356. (t 0.)))
  10357. (defun org-time-today ()
  10358. "Time in seconds today at 0:00.
  10359. Returns the float number of seconds since the beginning of the
  10360. epoch to the beginning of today (00:00)."
  10361. (float-time (apply 'encode-time
  10362. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10363. (defun org-matcher-time (s)
  10364. "Interpret a time comparison value."
  10365. (save-match-data
  10366. (cond
  10367. ((string= s "<now>") (float-time))
  10368. ((string= s "<today>") (org-time-today))
  10369. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10370. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10371. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10372. (+ (org-time-today)
  10373. (* (string-to-number (match-string 1 s))
  10374. (cdr (assoc (match-string 2 s)
  10375. '(("d" . 86400.0) ("w" . 604800.0)
  10376. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10377. (t (org-2ft s)))))
  10378. (defun org-match-any-p (re list)
  10379. "Does re match any element of list?"
  10380. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10381. (delq nil list))
  10382. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10383. (defvar org-tags-overlay (org-make-overlay 1 1))
  10384. (org-detach-overlay org-tags-overlay)
  10385. (defun org-get-local-tags-at (&optional pos)
  10386. "Get a list of tags defined in the current headline."
  10387. (org-get-tags-at pos 'local))
  10388. (defun org-get-local-tags ()
  10389. "Get a list of tags defined in the current headline."
  10390. (org-get-tags-at nil 'local))
  10391. (defun org-get-tags-at (&optional pos local)
  10392. "Get a list of all headline tags applicable at POS.
  10393. POS defaults to point. If tags are inherited, the list contains
  10394. the targets in the same sequence as the headlines appear, i.e.
  10395. the tags of the current headline come last.
  10396. When LOCAL is non-nil, only return tags from the current headline,
  10397. ignore inherited ones."
  10398. (interactive)
  10399. (if (and org-trust-scanner-tags
  10400. (or (not pos) (equal pos (point)))
  10401. (not local))
  10402. org-scanner-tags
  10403. (let (tags ltags lastpos parent)
  10404. (save-excursion
  10405. (save-restriction
  10406. (widen)
  10407. (goto-char (or pos (point)))
  10408. (save-match-data
  10409. (catch 'done
  10410. (condition-case nil
  10411. (progn
  10412. (org-back-to-heading t)
  10413. (while (not (equal lastpos (point)))
  10414. (setq lastpos (point))
  10415. (when (looking-at
  10416. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10417. (setq ltags (org-split-string
  10418. (org-match-string-no-properties 1) ":"))
  10419. (when parent
  10420. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10421. (setq tags (append
  10422. (if parent
  10423. (org-remove-uniherited-tags ltags)
  10424. ltags)
  10425. tags)))
  10426. (or org-use-tag-inheritance (throw 'done t))
  10427. (if local (throw 'done t))
  10428. (or (org-up-heading-safe) (error nil))
  10429. (setq parent t)))
  10430. (error nil)))))
  10431. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10432. (defun org-add-prop-inherited (s)
  10433. (add-text-properties 0 (length s) '(inherited t) s)
  10434. s)
  10435. (defun org-toggle-tag (tag &optional onoff)
  10436. "Toggle the tag TAG for the current line.
  10437. If ONOFF is `on' or `off', don't toggle but set to this state."
  10438. (let (res current)
  10439. (save-excursion
  10440. (org-back-to-heading t)
  10441. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10442. (point-at-eol) t)
  10443. (progn
  10444. (setq current (match-string 1))
  10445. (replace-match ""))
  10446. (setq current ""))
  10447. (setq current (nreverse (org-split-string current ":")))
  10448. (cond
  10449. ((eq onoff 'on)
  10450. (setq res t)
  10451. (or (member tag current) (push tag current)))
  10452. ((eq onoff 'off)
  10453. (or (not (member tag current)) (setq current (delete tag current))))
  10454. (t (if (member tag current)
  10455. (setq current (delete tag current))
  10456. (setq res t)
  10457. (push tag current))))
  10458. (end-of-line 1)
  10459. (if current
  10460. (progn
  10461. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10462. (org-set-tags nil t))
  10463. (delete-horizontal-space))
  10464. (run-hooks 'org-after-tags-change-hook))
  10465. res))
  10466. (defun org-align-tags-here (to-col)
  10467. ;; Assumes that this is a headline
  10468. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10469. (beginning-of-line 1)
  10470. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10471. (< pos (match-beginning 2)))
  10472. (progn
  10473. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10474. (goto-char (match-beginning 1))
  10475. (insert " ")
  10476. (delete-region (point) (1+ (match-beginning 2)))
  10477. (setq ncol (max (1+ (current-column))
  10478. (1+ col)
  10479. (if (> to-col 0)
  10480. to-col
  10481. (- (abs to-col) tags-l))))
  10482. (setq p (point))
  10483. (insert (make-string (- ncol (current-column)) ?\ ))
  10484. (setq ncol (current-column))
  10485. (when indent-tabs-mode (tabify p (point-at-eol)))
  10486. (org-move-to-column (min ncol col) t))
  10487. (goto-char pos))))
  10488. (defun org-set-tags-command (&optional arg just-align)
  10489. "Call the set-tags command for the current entry."
  10490. (interactive "P")
  10491. (if (org-on-heading-p)
  10492. (org-set-tags arg just-align)
  10493. (save-excursion
  10494. (org-back-to-heading t)
  10495. (org-set-tags arg just-align))))
  10496. (defun org-set-tags-to (data)
  10497. "Set the tags of the current entry to DATA, replacing the current tags.
  10498. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10499. If DATA is nil or the empty string, any tags will be removed."
  10500. (interactive "sTags: ")
  10501. (setq data
  10502. (cond
  10503. ((eq data nil) "")
  10504. ((equal data "") "")
  10505. ((stringp data)
  10506. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10507. ":"))
  10508. ((listp data)
  10509. (concat ":" (mapconcat 'identity data ":") ":"))
  10510. (t nil)))
  10511. (when data
  10512. (save-excursion
  10513. (org-back-to-heading t)
  10514. (when (looking-at org-complex-heading-regexp)
  10515. (if (match-end 5)
  10516. (progn
  10517. (goto-char (match-beginning 5))
  10518. (insert data)
  10519. (delete-region (point) (point-at-eol))
  10520. (org-set-tags nil 'align))
  10521. (goto-char (point-at-eol))
  10522. (insert " " data)
  10523. (org-set-tags nil 'align)))
  10524. (beginning-of-line 1)
  10525. (if (looking-at ".*?\\([ \t]+\\)$")
  10526. (delete-region (match-beginning 1) (match-end 1))))))
  10527. (defun org-set-tags (&optional arg just-align)
  10528. "Set the tags for the current headline.
  10529. With prefix ARG, realign all tags in headings in the current buffer."
  10530. (interactive "P")
  10531. (let* ((re (concat "^" outline-regexp))
  10532. (current (org-get-tags-string))
  10533. (col (current-column))
  10534. (org-setting-tags t)
  10535. table current-tags inherited-tags ; computed below when needed
  10536. tags p0 c0 c1 rpl)
  10537. (if arg
  10538. (save-excursion
  10539. (goto-char (point-min))
  10540. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10541. (while (re-search-forward re nil t)
  10542. (org-set-tags nil t)
  10543. (end-of-line 1)))
  10544. (message "All tags realigned to column %d" org-tags-column))
  10545. (if just-align
  10546. (setq tags current)
  10547. ;; Get a new set of tags from the user
  10548. (save-excursion
  10549. (setq table (append org-tag-persistent-alist
  10550. (or org-tag-alist (org-get-buffer-tags)))
  10551. org-last-tags-completion-table table
  10552. current-tags (org-split-string current ":")
  10553. inherited-tags (nreverse
  10554. (nthcdr (length current-tags)
  10555. (nreverse (org-get-tags-at))))
  10556. tags
  10557. (if (or (eq t org-use-fast-tag-selection)
  10558. (and org-use-fast-tag-selection
  10559. (delq nil (mapcar 'cdr table))))
  10560. (org-fast-tag-selection
  10561. current-tags inherited-tags table
  10562. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10563. (let ((org-add-colon-after-tag-completion t))
  10564. (org-trim
  10565. (org-without-partial-completion
  10566. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10567. nil nil current 'org-tags-history)))))))
  10568. (while (string-match "[-+&]+" tags)
  10569. ;; No boolean logic, just a list
  10570. (setq tags (replace-match ":" t t tags))))
  10571. (if org-tags-sort-function
  10572. (setq tags (mapconcat 'identity
  10573. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10574. org-tags-sort-function) ":")))
  10575. (if (string-match "\\`[\t ]*\\'" tags)
  10576. (setq tags "")
  10577. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10578. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10579. ;; Insert new tags at the correct column
  10580. (beginning-of-line 1)
  10581. (cond
  10582. ((and (equal current "") (equal tags "")))
  10583. ((re-search-forward
  10584. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10585. (point-at-eol) t)
  10586. (if (equal tags "")
  10587. (setq rpl "")
  10588. (goto-char (match-beginning 0))
  10589. (setq c0 (current-column) p0 (point)
  10590. c1 (max (1+ c0) (if (> org-tags-column 0)
  10591. org-tags-column
  10592. (- (- org-tags-column) (length tags))))
  10593. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10594. (replace-match rpl t t)
  10595. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10596. tags)
  10597. (t (error "Tags alignment failed")))
  10598. (org-move-to-column col)
  10599. (unless just-align
  10600. (run-hooks 'org-after-tags-change-hook)))))
  10601. (defun org-change-tag-in-region (beg end tag off)
  10602. "Add or remove TAG for each entry in the region.
  10603. This works in the agenda, and also in an org-mode buffer."
  10604. (interactive
  10605. (list (region-beginning) (region-end)
  10606. (let ((org-last-tags-completion-table
  10607. (if (org-mode-p)
  10608. (org-get-buffer-tags)
  10609. (org-global-tags-completion-table))))
  10610. (org-icompleting-read
  10611. "Tag: " 'org-tags-completion-function nil nil nil
  10612. 'org-tags-history))
  10613. (progn
  10614. (message "[s]et or [r]emove? ")
  10615. (equal (read-char-exclusive) ?r))))
  10616. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10617. (let ((agendap (equal major-mode 'org-agenda-mode))
  10618. l1 l2 m buf pos newhead (cnt 0))
  10619. (goto-char end)
  10620. (setq l2 (1- (org-current-line)))
  10621. (goto-char beg)
  10622. (setq l1 (org-current-line))
  10623. (loop for l from l1 to l2 do
  10624. (org-goto-line l)
  10625. (setq m (get-text-property (point) 'org-hd-marker))
  10626. (when (or (and (org-mode-p) (org-on-heading-p))
  10627. (and agendap m))
  10628. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10629. pos (if agendap m (point)))
  10630. (with-current-buffer buf
  10631. (save-excursion
  10632. (save-restriction
  10633. (goto-char pos)
  10634. (setq cnt (1+ cnt))
  10635. (org-toggle-tag tag (if off 'off 'on))
  10636. (setq newhead (org-get-heading)))))
  10637. (and agendap (org-agenda-change-all-lines newhead m))))
  10638. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10639. (defun org-tags-completion-function (string predicate &optional flag)
  10640. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10641. (confirm (lambda (x) (stringp (car x)))))
  10642. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10643. (setq s1 (match-string 1 string)
  10644. s2 (match-string 2 string))
  10645. (setq s1 "" s2 string))
  10646. (cond
  10647. ((eq flag nil)
  10648. ;; try completion
  10649. (setq rtn (try-completion s2 ctable confirm))
  10650. (if (stringp rtn)
  10651. (setq rtn
  10652. (concat s1 s2 (substring rtn (length s2))
  10653. (if (and org-add-colon-after-tag-completion
  10654. (assoc rtn ctable))
  10655. ":" ""))))
  10656. rtn)
  10657. ((eq flag t)
  10658. ;; all-completions
  10659. (all-completions s2 ctable confirm)
  10660. )
  10661. ((eq flag 'lambda)
  10662. ;; exact match?
  10663. (assoc s2 ctable)))
  10664. ))
  10665. (defun org-fast-tag-insert (kwd tags face &optional end)
  10666. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10667. (insert (format "%-12s" (concat kwd ":"))
  10668. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10669. (or end "")))
  10670. (defun org-fast-tag-show-exit (flag)
  10671. (save-excursion
  10672. (org-goto-line 3)
  10673. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10674. (replace-match ""))
  10675. (when flag
  10676. (end-of-line 1)
  10677. (org-move-to-column (- (window-width) 19) t)
  10678. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10679. (defun org-set-current-tags-overlay (current prefix)
  10680. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10681. (if (featurep 'xemacs)
  10682. (org-overlay-display org-tags-overlay (concat prefix s)
  10683. 'secondary-selection)
  10684. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10685. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10686. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10687. "Fast tag selection with single keys.
  10688. CURRENT is the current list of tags in the headline, INHERITED is the
  10689. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10690. possibly with grouping information. TODO-TABLE is a similar table with
  10691. TODO keywords, should these have keys assigned to them.
  10692. If the keys are nil, a-z are automatically assigned.
  10693. Returns the new tags string, or nil to not change the current settings."
  10694. (let* ((fulltable (append table todo-table))
  10695. (maxlen (apply 'max (mapcar
  10696. (lambda (x)
  10697. (if (stringp (car x)) (string-width (car x)) 0))
  10698. fulltable)))
  10699. (buf (current-buffer))
  10700. (expert (eq org-fast-tag-selection-single-key 'expert))
  10701. (buffer-tags nil)
  10702. (fwidth (+ maxlen 3 1 3))
  10703. (ncol (/ (- (window-width) 4) fwidth))
  10704. (i-face 'org-done)
  10705. (c-face 'org-todo)
  10706. tg cnt e c char c1 c2 ntable tbl rtn
  10707. ov-start ov-end ov-prefix
  10708. (exit-after-next org-fast-tag-selection-single-key)
  10709. (done-keywords org-done-keywords)
  10710. groups ingroup)
  10711. (save-excursion
  10712. (beginning-of-line 1)
  10713. (if (looking-at
  10714. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10715. (setq ov-start (match-beginning 1)
  10716. ov-end (match-end 1)
  10717. ov-prefix "")
  10718. (setq ov-start (1- (point-at-eol))
  10719. ov-end (1+ ov-start))
  10720. (skip-chars-forward "^\n\r")
  10721. (setq ov-prefix
  10722. (concat
  10723. (buffer-substring (1- (point)) (point))
  10724. (if (> (current-column) org-tags-column)
  10725. " "
  10726. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10727. (org-move-overlay org-tags-overlay ov-start ov-end)
  10728. (save-window-excursion
  10729. (if expert
  10730. (set-buffer (get-buffer-create " *Org tags*"))
  10731. (delete-other-windows)
  10732. (split-window-vertically)
  10733. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10734. (erase-buffer)
  10735. (org-set-local 'org-done-keywords done-keywords)
  10736. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10737. (org-fast-tag-insert "Current" current c-face "\n\n")
  10738. (org-fast-tag-show-exit exit-after-next)
  10739. (org-set-current-tags-overlay current ov-prefix)
  10740. (setq tbl fulltable char ?a cnt 0)
  10741. (while (setq e (pop tbl))
  10742. (cond
  10743. ((equal (car e) :startgroup)
  10744. (push '() groups) (setq ingroup t)
  10745. (when (not (= cnt 0))
  10746. (setq cnt 0)
  10747. (insert "\n"))
  10748. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10749. ((equal (car e) :endgroup)
  10750. (setq ingroup nil cnt 0)
  10751. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10752. ((equal e '(:newline))
  10753. (when (not (= cnt 0))
  10754. (setq cnt 0)
  10755. (insert "\n")
  10756. (setq e (car tbl))
  10757. (while (equal (car tbl) '(:newline))
  10758. (insert "\n")
  10759. (setq tbl (cdr tbl)))))
  10760. (t
  10761. (setq tg (copy-sequence (car e)) c2 nil)
  10762. (if (cdr e)
  10763. (setq c (cdr e))
  10764. ;; automatically assign a character.
  10765. (setq c1 (string-to-char
  10766. (downcase (substring
  10767. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10768. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10769. (while (or (rassoc char ntable) (rassoc char table))
  10770. (setq char (1+ char)))
  10771. (setq c2 c1))
  10772. (setq c (or c2 char)))
  10773. (if ingroup (push tg (car groups)))
  10774. (setq tg (org-add-props tg nil 'face
  10775. (cond
  10776. ((not (assoc tg table))
  10777. (org-get-todo-face tg))
  10778. ((member tg current) c-face)
  10779. ((member tg inherited) i-face)
  10780. (t nil))))
  10781. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10782. (insert "[" c "] " tg (make-string
  10783. (- fwidth 4 (length tg)) ?\ ))
  10784. (push (cons tg c) ntable)
  10785. (when (= (setq cnt (1+ cnt)) ncol)
  10786. (insert "\n")
  10787. (if ingroup (insert " "))
  10788. (setq cnt 0)))))
  10789. (setq ntable (nreverse ntable))
  10790. (insert "\n")
  10791. (goto-char (point-min))
  10792. (if (not expert) (org-fit-window-to-buffer))
  10793. (setq rtn
  10794. (catch 'exit
  10795. (while t
  10796. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10797. (if (not groups) "no " "")
  10798. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10799. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10800. (cond
  10801. ((= c ?\r) (throw 'exit t))
  10802. ((= c ?!)
  10803. (setq groups (not groups))
  10804. (goto-char (point-min))
  10805. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10806. ((= c ?\C-c)
  10807. (if (not expert)
  10808. (org-fast-tag-show-exit
  10809. (setq exit-after-next (not exit-after-next)))
  10810. (setq expert nil)
  10811. (delete-other-windows)
  10812. (split-window-vertically)
  10813. (org-switch-to-buffer-other-window " *Org tags*")
  10814. (org-fit-window-to-buffer)))
  10815. ((or (= c ?\C-g)
  10816. (and (= c ?q) (not (rassoc c ntable))))
  10817. (org-detach-overlay org-tags-overlay)
  10818. (setq quit-flag t))
  10819. ((= c ?\ )
  10820. (setq current nil)
  10821. (if exit-after-next (setq exit-after-next 'now)))
  10822. ((= c ?\t)
  10823. (condition-case nil
  10824. (setq tg (org-icompleting-read
  10825. "Tag: "
  10826. (or buffer-tags
  10827. (with-current-buffer buf
  10828. (org-get-buffer-tags)))))
  10829. (quit (setq tg "")))
  10830. (when (string-match "\\S-" tg)
  10831. (add-to-list 'buffer-tags (list tg))
  10832. (if (member tg current)
  10833. (setq current (delete tg current))
  10834. (push tg current)))
  10835. (if exit-after-next (setq exit-after-next 'now)))
  10836. ((setq e (rassoc c todo-table) tg (car e))
  10837. (with-current-buffer buf
  10838. (save-excursion (org-todo tg)))
  10839. (if exit-after-next (setq exit-after-next 'now)))
  10840. ((setq e (rassoc c ntable) tg (car e))
  10841. (if (member tg current)
  10842. (setq current (delete tg current))
  10843. (loop for g in groups do
  10844. (if (member tg g)
  10845. (mapc (lambda (x)
  10846. (setq current (delete x current)))
  10847. g)))
  10848. (push tg current))
  10849. (if exit-after-next (setq exit-after-next 'now))))
  10850. ;; Create a sorted list
  10851. (setq current
  10852. (sort current
  10853. (lambda (a b)
  10854. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10855. (if (eq exit-after-next 'now) (throw 'exit t))
  10856. (goto-char (point-min))
  10857. (beginning-of-line 2)
  10858. (delete-region (point) (point-at-eol))
  10859. (org-fast-tag-insert "Current" current c-face)
  10860. (org-set-current-tags-overlay current ov-prefix)
  10861. (while (re-search-forward
  10862. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10863. (setq tg (match-string 1))
  10864. (add-text-properties
  10865. (match-beginning 1) (match-end 1)
  10866. (list 'face
  10867. (cond
  10868. ((member tg current) c-face)
  10869. ((member tg inherited) i-face)
  10870. (t (get-text-property (match-beginning 1) 'face))))))
  10871. (goto-char (point-min)))))
  10872. (org-detach-overlay org-tags-overlay)
  10873. (if rtn
  10874. (mapconcat 'identity current ":")
  10875. nil))))
  10876. (defun org-get-tags-string ()
  10877. "Get the TAGS string in the current headline."
  10878. (unless (org-on-heading-p t)
  10879. (error "Not on a heading"))
  10880. (save-excursion
  10881. (beginning-of-line 1)
  10882. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10883. (org-match-string-no-properties 1)
  10884. "")))
  10885. (defun org-get-tags ()
  10886. "Get the list of tags specified in the current headline."
  10887. (org-split-string (org-get-tags-string) ":"))
  10888. (defun org-get-buffer-tags ()
  10889. "Get a table of all tags used in the buffer, for completion."
  10890. (let (tags)
  10891. (save-excursion
  10892. (goto-char (point-min))
  10893. (while (re-search-forward
  10894. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10895. (when (equal (char-after (point-at-bol 0)) ?*)
  10896. (mapc (lambda (x) (add-to-list 'tags x))
  10897. (org-split-string (org-match-string-no-properties 1) ":")))))
  10898. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10899. (mapcar 'list tags)))
  10900. ;;;; The mapping API
  10901. ;;;###autoload
  10902. (defun org-map-entries (func &optional match scope &rest skip)
  10903. "Call FUNC at each headline selected by MATCH in SCOPE.
  10904. FUNC is a function or a lisp form. The function will be called without
  10905. arguments, with the cursor positioned at the beginning of the headline.
  10906. The return values of all calls to the function will be collected and
  10907. returned as a list.
  10908. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10909. does not need to preserve point. After evaluation, the cursor will be
  10910. moved to the end of the line (presumably of the headline of the
  10911. processed entry) and search continues from there. Under some
  10912. circumstances, this may not produce the wanted results. For example,
  10913. if you have removed (e.g. archived) the current (sub)tree it could
  10914. mean that the next entry will be skipped entirely. In such cases, you
  10915. can specify the position from where search should continue by making
  10916. FUNC set the variable `org-map-continue-from' to the desired buffer
  10917. position.
  10918. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10919. Only headlines that are matched by this query will be considered during
  10920. the iteration. When MATCH is nil or t, all headlines will be
  10921. visited by the iteration.
  10922. SCOPE determines the scope of this command. It can be any of:
  10923. nil The current buffer, respecting the restriction if any
  10924. tree The subtree started with the entry at point
  10925. file The current buffer, without restriction
  10926. file-with-archives
  10927. The current buffer, and any archives associated with it
  10928. agenda All agenda files
  10929. agenda-with-archives
  10930. All agenda files with any archive files associated with them
  10931. \(file1 file2 ...)
  10932. If this is a list, all files in the list will be scanned
  10933. The remaining args are treated as settings for the skipping facilities of
  10934. the scanner. The following items can be given here:
  10935. archive skip trees with the archive tag.
  10936. comment skip trees with the COMMENT keyword
  10937. function or Emacs Lisp form:
  10938. will be used as value for `org-agenda-skip-function', so whenever
  10939. the function returns t, FUNC will not be called for that
  10940. entry and search will continue from the point where the
  10941. function leaves it.
  10942. If your function needs to retrieve the tags including inherited tags
  10943. at the *current* entry, you can use the value of the variable
  10944. `org-scanner-tags' which will be much faster than getting the value
  10945. with `org-get-tags-at'. If your function gets properties with
  10946. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10947. to t around the call to `org-entry-properties' to get the same speedup.
  10948. Note that if your function moves around to retrieve tags and properties at
  10949. a *different* entry, you cannot use these techniques."
  10950. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10951. (org-agenda-skip-archived-trees (memq 'archive skip))
  10952. (org-agenda-skip-comment-trees (memq 'comment skip))
  10953. (org-agenda-skip-function
  10954. (car (org-delete-all '(comment archive) skip)))
  10955. (org-tags-match-list-sublevels t)
  10956. matcher file res
  10957. org-todo-keywords-for-agenda
  10958. org-done-keywords-for-agenda
  10959. org-todo-keyword-alist-for-agenda
  10960. org-drawers-for-agenda
  10961. org-tag-alist-for-agenda)
  10962. (cond
  10963. ((eq match t) (setq matcher t))
  10964. ((eq match nil) (setq matcher t))
  10965. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10966. (save-excursion
  10967. (save-restriction
  10968. (when (eq scope 'tree)
  10969. (org-back-to-heading t)
  10970. (org-narrow-to-subtree)
  10971. (setq scope nil))
  10972. (if (not scope)
  10973. (progn
  10974. (org-prepare-agenda-buffers
  10975. (list (buffer-file-name (current-buffer))))
  10976. (setq res (org-scan-tags func matcher)))
  10977. ;; Get the right scope
  10978. (cond
  10979. ((and scope (listp scope) (symbolp (car scope)))
  10980. (setq scope (eval scope)))
  10981. ((eq scope 'agenda)
  10982. (setq scope (org-agenda-files t)))
  10983. ((eq scope 'agenda-with-archives)
  10984. (setq scope (org-agenda-files t))
  10985. (setq scope (org-add-archive-files scope)))
  10986. ((eq scope 'file)
  10987. (setq scope (list (buffer-file-name))))
  10988. ((eq scope 'file-with-archives)
  10989. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10990. (org-prepare-agenda-buffers scope)
  10991. (while (setq file (pop scope))
  10992. (with-current-buffer (org-find-base-buffer-visiting file)
  10993. (save-excursion
  10994. (save-restriction
  10995. (widen)
  10996. (goto-char (point-min))
  10997. (setq res (append res (org-scan-tags func matcher))))))))))
  10998. res))
  10999. ;;;; Properties
  11000. ;;; Setting and retrieving properties
  11001. (defconst org-special-properties
  11002. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11003. "TIMESTAMP" "TIMESTAMP_IA")
  11004. "The special properties valid in Org-mode.
  11005. These are properties that are not defined in the property drawer,
  11006. but in some other way.")
  11007. (defconst org-default-properties
  11008. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11009. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11010. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11011. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11012. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11013. "CLOCK_MODELINE_TOTAL" "STYLE")
  11014. "Some properties that are used by Org-mode for various purposes.
  11015. Being in this list makes sure that they are offered for completion.")
  11016. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11017. "Regular expression matching the first line of a property drawer.")
  11018. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11019. "Regular expression matching the first line of a property drawer.")
  11020. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11021. "Regular expression matching the first line of a property drawer.")
  11022. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11023. "Regular expression matching the first line of a property drawer.")
  11024. (defconst org-property-drawer-re
  11025. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11026. org-property-end-re "\\)\n?")
  11027. "Matches an entire property drawer.")
  11028. (defconst org-clock-drawer-re
  11029. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11030. org-property-end-re "\\)\n?")
  11031. "Matches an entire clock drawer.")
  11032. (defun org-property-action ()
  11033. "Do an action on properties."
  11034. (interactive)
  11035. (let (c)
  11036. (org-at-property-p)
  11037. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11038. (setq c (read-char-exclusive))
  11039. (cond
  11040. ((equal c ?s)
  11041. (call-interactively 'org-set-property))
  11042. ((equal c ?d)
  11043. (call-interactively 'org-delete-property))
  11044. ((equal c ?D)
  11045. (call-interactively 'org-delete-property-globally))
  11046. ((equal c ?c)
  11047. (call-interactively 'org-compute-property-at-point))
  11048. (t (error "No such property action %c" c)))))
  11049. (defun org-set-effort (&optional value)
  11050. "Set the effort property of the current entry.
  11051. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11052. allowed value."
  11053. (interactive "P")
  11054. (if (equal value 0) (setq value 10))
  11055. (let* ((completion-ignore-case t)
  11056. (prop org-effort-property)
  11057. (cur (org-entry-get nil prop))
  11058. (allowed (org-property-get-allowed-values nil prop 'table))
  11059. (existing (mapcar 'list (org-property-values prop)))
  11060. rpl
  11061. (val (cond
  11062. ((stringp value) value)
  11063. ((and allowed (integerp value))
  11064. (or (car (nth (1- value) allowed))
  11065. (car (org-last allowed))))
  11066. (allowed
  11067. (message "Select 1-9,0, [RET%s]: %s"
  11068. (if cur (concat "=" cur) "")
  11069. (mapconcat 'car allowed " "))
  11070. (setq rpl (read-char-exclusive))
  11071. (if (equal rpl ?\r)
  11072. cur
  11073. (setq rpl (- rpl ?0))
  11074. (if (equal rpl 0) (setq rpl 10))
  11075. (if (and (> rpl 0) (<= rpl (length allowed)))
  11076. (car (nth (1- rpl) allowed))
  11077. (org-completing-read "Value: " allowed nil))))
  11078. (t
  11079. (let (org-completion-use-ido org-completion-use-iswitchb)
  11080. (org-completing-read
  11081. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11082. (concat "[" cur "]") "")
  11083. ": ")
  11084. existing nil nil "" nil cur))))))
  11085. (unless (equal (org-entry-get nil prop) val)
  11086. (org-entry-put nil prop val))
  11087. (message "%s is now %s" prop val)))
  11088. (defun org-at-property-p ()
  11089. "Is the cursor in a property line?"
  11090. ;; FIXME: Does not check if we are actually in the drawer.
  11091. ;; FIXME: also returns true on any drawers.....
  11092. ;; This is used by C-c C-c for property action.
  11093. (save-excursion
  11094. (beginning-of-line 1)
  11095. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11096. (defun org-get-property-block (&optional beg end force)
  11097. "Return the (beg . end) range of the body of the property drawer.
  11098. BEG and END can be beginning and end of subtree, if not given
  11099. they will be found.
  11100. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11101. (catch 'exit
  11102. (save-excursion
  11103. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11104. (end (or end (progn (outline-next-heading) (point)))))
  11105. (goto-char beg)
  11106. (if (re-search-forward org-property-start-re end t)
  11107. (setq beg (1+ (match-end 0)))
  11108. (if force
  11109. (save-excursion
  11110. (org-insert-property-drawer)
  11111. (setq end (progn (outline-next-heading) (point))))
  11112. (throw 'exit nil))
  11113. (goto-char beg)
  11114. (if (re-search-forward org-property-start-re end t)
  11115. (setq beg (1+ (match-end 0)))))
  11116. (if (re-search-forward org-property-end-re end t)
  11117. (setq end (match-beginning 0))
  11118. (or force (throw 'exit nil))
  11119. (goto-char beg)
  11120. (setq end beg)
  11121. (org-indent-line-function)
  11122. (insert ":END:\n"))
  11123. (cons beg end)))))
  11124. (defun org-entry-properties (&optional pom which)
  11125. "Get all properties of the entry at point-or-marker POM.
  11126. This includes the TODO keyword, the tags, time strings for deadline,
  11127. scheduled, and clocking, and any additional properties defined in the
  11128. entry. The return value is an alist, keys may occur multiple times
  11129. if the property key was used several times.
  11130. POM may also be nil, in which case the current entry is used.
  11131. If WHICH is nil or `all', get all properties. If WHICH is
  11132. `special' or `standard', only get that subclass."
  11133. (setq which (or which 'all))
  11134. (org-with-point-at pom
  11135. (let ((clockstr (substring org-clock-string 0 -1))
  11136. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  11137. beg end range props sum-props key value string clocksum)
  11138. (save-excursion
  11139. (when (condition-case nil
  11140. (and (org-mode-p) (org-back-to-heading t))
  11141. (error nil))
  11142. (setq beg (point))
  11143. (setq sum-props (get-text-property (point) 'org-summaries))
  11144. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11145. (outline-next-heading)
  11146. (setq end (point))
  11147. (when (memq which '(all special))
  11148. ;; Get the special properties, like TODO and tags
  11149. (goto-char beg)
  11150. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  11151. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11152. (when (looking-at org-priority-regexp)
  11153. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11154. (when (and (setq value (org-get-tags-string))
  11155. (string-match "\\S-" value))
  11156. (push (cons "TAGS" value) props))
  11157. (when (setq value (org-get-tags-at))
  11158. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  11159. props))
  11160. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11161. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11162. string (if (equal key clockstr)
  11163. (org-no-properties
  11164. (org-trim
  11165. (buffer-substring
  11166. (match-beginning 3) (goto-char (point-at-eol)))))
  11167. (substring (org-match-string-no-properties 3) 1 -1)))
  11168. (unless key
  11169. (if (= (char-after (match-beginning 3)) ?\[)
  11170. (setq key "TIMESTAMP_IA")
  11171. (setq key "TIMESTAMP")))
  11172. (when (or (equal key clockstr) (not (assoc key props)))
  11173. (push (cons key string) props)))
  11174. )
  11175. (when (memq which '(all standard))
  11176. ;; Get the standard properties, like :PROP: ...
  11177. (setq range (org-get-property-block beg end))
  11178. (when range
  11179. (goto-char (car range))
  11180. (while (re-search-forward
  11181. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11182. (cdr range) t)
  11183. (setq key (org-match-string-no-properties 1)
  11184. value (org-trim (or (org-match-string-no-properties 2) "")))
  11185. (unless (member key excluded)
  11186. (push (cons key (or value "")) props)))))
  11187. (if clocksum
  11188. (push (cons "CLOCKSUM"
  11189. (org-columns-number-to-string (/ (float clocksum) 60.)
  11190. 'add_times))
  11191. props))
  11192. (unless (assoc "CATEGORY" props)
  11193. (setq value (or (org-get-category)
  11194. (progn (org-refresh-category-properties)
  11195. (org-get-category))))
  11196. (push (cons "CATEGORY" value) props))
  11197. (append sum-props (nreverse props)))))))
  11198. (defun org-entry-get (pom property &optional inherit)
  11199. "Get value of PROPERTY for entry at point-or-marker POM.
  11200. If INHERIT is non-nil and the entry does not have the property,
  11201. then also check higher levels of the hierarchy.
  11202. If INHERIT is the symbol `selective', use inheritance only if the setting
  11203. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11204. If the property is present but empty, the return value is the empty string.
  11205. If the property is not present at all, nil is returned."
  11206. (org-with-point-at pom
  11207. (if (and inherit (if (eq inherit 'selective)
  11208. (org-property-inherit-p property)
  11209. t))
  11210. (org-entry-get-with-inheritance property)
  11211. (if (member property org-special-properties)
  11212. ;; We need a special property. Use brute force, get all properties.
  11213. (cdr (assoc property (org-entry-properties nil 'special)))
  11214. (let ((range (org-get-property-block)))
  11215. (if (and range
  11216. (goto-char (car range))
  11217. (re-search-forward
  11218. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11219. (cdr range) t))
  11220. ;; Found the property, return it.
  11221. (if (match-end 1)
  11222. (org-match-string-no-properties 1)
  11223. "")))))))
  11224. (defun org-property-or-variable-value (var &optional inherit)
  11225. "Check if there is a property fixing the value of VAR.
  11226. If yes, return this value. If not, return the current value of the variable."
  11227. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11228. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11229. (read prop)
  11230. (symbol-value var))))
  11231. (defun org-entry-delete (pom property)
  11232. "Delete the property PROPERTY from entry at point-or-marker POM."
  11233. (org-with-point-at pom
  11234. (if (member property org-special-properties)
  11235. nil ; cannot delete these properties.
  11236. (let ((range (org-get-property-block)))
  11237. (if (and range
  11238. (goto-char (car range))
  11239. (re-search-forward
  11240. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11241. (cdr range) t))
  11242. (progn
  11243. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11244. t)
  11245. nil)))))
  11246. ;; Multi-values properties are properties that contain multiple values
  11247. ;; These values are assumed to be single words, separated by whitespace.
  11248. (defun org-entry-add-to-multivalued-property (pom property value)
  11249. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11250. (let* ((old (org-entry-get pom property))
  11251. (values (and old (org-split-string old "[ \t]"))))
  11252. (setq value (org-entry-protect-space value))
  11253. (unless (member value values)
  11254. (setq values (cons value values))
  11255. (org-entry-put pom property
  11256. (mapconcat 'identity values " ")))))
  11257. (defun org-entry-remove-from-multivalued-property (pom property value)
  11258. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11259. (let* ((old (org-entry-get pom property))
  11260. (values (and old (org-split-string old "[ \t]"))))
  11261. (setq value (org-entry-protect-space value))
  11262. (when (member value values)
  11263. (setq values (delete value values))
  11264. (org-entry-put pom property
  11265. (mapconcat 'identity values " ")))))
  11266. (defun org-entry-member-in-multivalued-property (pom property value)
  11267. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11268. (let* ((old (org-entry-get pom property))
  11269. (values (and old (org-split-string old "[ \t]"))))
  11270. (setq value (org-entry-protect-space value))
  11271. (member value values)))
  11272. (defun org-entry-get-multivalued-property (pom property)
  11273. "Return a list of values in a multivalued property."
  11274. (let* ((value (org-entry-get pom property))
  11275. (values (and value (org-split-string value "[ \t]"))))
  11276. (mapcar 'org-entry-restore-space values)))
  11277. (defun org-entry-put-multivalued-property (pom property &rest values)
  11278. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11279. VALUES should be a list of strings. Spaces will be protected."
  11280. (org-entry-put pom property
  11281. (mapconcat 'org-entry-protect-space values " "))
  11282. (let* ((value (org-entry-get pom property))
  11283. (values (and value (org-split-string value "[ \t]"))))
  11284. (mapcar 'org-entry-restore-space values)))
  11285. (defun org-entry-protect-space (s)
  11286. "Protect spaces and newline in string S."
  11287. (while (string-match " " s)
  11288. (setq s (replace-match "%20" t t s)))
  11289. (while (string-match "\n" s)
  11290. (setq s (replace-match "%0A" t t s)))
  11291. s)
  11292. (defun org-entry-restore-space (s)
  11293. "Restore spaces and newline in string S."
  11294. (while (string-match "%20" s)
  11295. (setq s (replace-match " " t t s)))
  11296. (while (string-match "%0A" s)
  11297. (setq s (replace-match "\n" t t s)))
  11298. s)
  11299. (defvar org-entry-property-inherited-from (make-marker)
  11300. "Marker pointing to the entry from where a property was inherited.
  11301. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11302. location of the entry where the inheritance search matched. If there was
  11303. no match, the marker will point nowhere.
  11304. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11305. is set.")
  11306. (defun org-entry-get-with-inheritance (property)
  11307. "Get entry property, and search higher levels if not present."
  11308. (move-marker org-entry-property-inherited-from nil)
  11309. (let (tmp)
  11310. (save-excursion
  11311. (save-restriction
  11312. (widen)
  11313. (catch 'ex
  11314. (while t
  11315. (when (setq tmp (org-entry-get nil property))
  11316. (org-back-to-heading t)
  11317. (move-marker org-entry-property-inherited-from (point))
  11318. (throw 'ex tmp))
  11319. (or (org-up-heading-safe) (throw 'ex nil)))))
  11320. (or tmp
  11321. (cdr (assoc property org-file-properties))
  11322. (cdr (assoc property org-global-properties))
  11323. (cdr (assoc property org-global-properties-fixed))))))
  11324. (defun org-entry-put (pom property value)
  11325. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11326. (org-with-point-at pom
  11327. (org-back-to-heading t)
  11328. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11329. range)
  11330. (cond
  11331. ((equal property "TODO")
  11332. (when (and (stringp value) (string-match "\\S-" value)
  11333. (not (member value org-todo-keywords-1)))
  11334. (error "\"%s\" is not a valid TODO state" value))
  11335. (if (or (not value)
  11336. (not (string-match "\\S-" value)))
  11337. (setq value 'none))
  11338. (org-todo value)
  11339. (org-set-tags nil 'align))
  11340. ((equal property "PRIORITY")
  11341. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11342. (string-to-char value) ?\ ))
  11343. (org-set-tags nil 'align))
  11344. ((equal property "SCHEDULED")
  11345. (if (re-search-forward org-scheduled-time-regexp end t)
  11346. (cond
  11347. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11348. ((eq value 'later) (org-timestamp-change 1 'day))
  11349. (t (call-interactively 'org-schedule)))
  11350. (call-interactively 'org-schedule)))
  11351. ((equal property "DEADLINE")
  11352. (if (re-search-forward org-deadline-time-regexp end t)
  11353. (cond
  11354. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11355. ((eq value 'later) (org-timestamp-change 1 'day))
  11356. (t (call-interactively 'org-deadline)))
  11357. (call-interactively 'org-deadline)))
  11358. ((member property org-special-properties)
  11359. (error "The %s property can not yet be set with `org-entry-put'"
  11360. property))
  11361. (t ; a non-special property
  11362. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11363. (setq range (org-get-property-block beg end 'force))
  11364. (goto-char (car range))
  11365. (if (re-search-forward
  11366. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11367. (progn
  11368. (delete-region (match-beginning 1) (match-end 1))
  11369. (goto-char (match-beginning 1)))
  11370. (goto-char (cdr range))
  11371. (insert "\n")
  11372. (backward-char 1)
  11373. (org-indent-line-function)
  11374. (insert ":" property ":"))
  11375. (and value (insert " " value))
  11376. (org-indent-line-function)))))))
  11377. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11378. "Get all property keys in the current buffer.
  11379. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11380. like tags and TODO state.
  11381. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11382. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11383. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11384. formats in the current buffer."
  11385. (let (rtn range cfmt s p)
  11386. (save-excursion
  11387. (save-restriction
  11388. (widen)
  11389. (goto-char (point-min))
  11390. (while (re-search-forward org-property-start-re nil t)
  11391. (setq range (org-get-property-block))
  11392. (goto-char (car range))
  11393. (while (re-search-forward
  11394. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11395. (cdr range) t)
  11396. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11397. (outline-next-heading))))
  11398. (when include-specials
  11399. (setq rtn (append org-special-properties rtn)))
  11400. (when include-defaults
  11401. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11402. (add-to-list 'rtn org-effort-property))
  11403. (when include-columns
  11404. (save-excursion
  11405. (save-restriction
  11406. (widen)
  11407. (goto-char (point-min))
  11408. (while (re-search-forward
  11409. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11410. nil t)
  11411. (setq cfmt (match-string 2) s 0)
  11412. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11413. cfmt s)
  11414. (setq s (match-end 0)
  11415. p (match-string 1 cfmt))
  11416. (unless (or (equal p "ITEM")
  11417. (member p org-special-properties))
  11418. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11419. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11420. (defun org-property-values (key)
  11421. "Return a list of all values of property KEY."
  11422. (save-excursion
  11423. (save-restriction
  11424. (widen)
  11425. (goto-char (point-min))
  11426. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11427. values)
  11428. (while (re-search-forward re nil t)
  11429. (add-to-list 'values (org-trim (match-string 1))))
  11430. (delete "" values)))))
  11431. (defun org-insert-property-drawer ()
  11432. "Insert a property drawer into the current entry."
  11433. (interactive)
  11434. (org-back-to-heading t)
  11435. (looking-at outline-regexp)
  11436. (let ((indent (if org-adapt-indentation
  11437. (- (match-end 0)(match-beginning 0))
  11438. 0))
  11439. (beg (point))
  11440. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11441. end hiddenp)
  11442. (outline-next-heading)
  11443. (setq end (point))
  11444. (goto-char beg)
  11445. (while (re-search-forward re end t))
  11446. (setq hiddenp (org-invisible-p))
  11447. (end-of-line 1)
  11448. (and (equal (char-after) ?\n) (forward-char 1))
  11449. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11450. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11451. ;; just skip this line
  11452. (beginning-of-line 2)
  11453. ;; Drawer start, find the end
  11454. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11455. (beginning-of-line 1)))
  11456. (org-skip-over-state-notes)
  11457. (skip-chars-backward " \t\n\r")
  11458. (if (eq (char-before) ?*) (forward-char 1))
  11459. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11460. (beginning-of-line 0)
  11461. (org-indent-to-column indent)
  11462. (beginning-of-line 2)
  11463. (org-indent-to-column indent)
  11464. (beginning-of-line 0)
  11465. (if hiddenp
  11466. (save-excursion
  11467. (org-back-to-heading t)
  11468. (hide-entry))
  11469. (org-flag-drawer t))))
  11470. (defun org-set-property (property value)
  11471. "In the current entry, set PROPERTY to VALUE.
  11472. When called interactively, this will prompt for a property name, offering
  11473. completion on existing and default properties. And then it will prompt
  11474. for a value, offering completion either on allowed values (via an inherited
  11475. xxx_ALL property) or on existing values in other instances of this property
  11476. in the current file."
  11477. (interactive
  11478. (let* ((completion-ignore-case t)
  11479. (keys (org-buffer-property-keys nil t t))
  11480. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11481. (prop (if (member prop0 keys)
  11482. prop0
  11483. (or (cdr (assoc (downcase prop0)
  11484. (mapcar (lambda (x) (cons (downcase x) x))
  11485. keys)))
  11486. prop0)))
  11487. (cur (org-entry-get nil prop))
  11488. (allowed (org-property-get-allowed-values nil prop 'table))
  11489. (existing (mapcar 'list (org-property-values prop)))
  11490. (val (if allowed
  11491. (org-completing-read "Value: " allowed nil 'req-match)
  11492. (let (org-completion-use-ido org-completion-use-iswitchb)
  11493. (org-completing-read
  11494. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11495. (concat "[" cur "]") "")
  11496. ": ")
  11497. existing nil nil "" nil cur)))))
  11498. (list prop (if (equal val "") cur val))))
  11499. (unless (equal (org-entry-get nil property) value)
  11500. (org-entry-put nil property value)))
  11501. (defun org-delete-property (property)
  11502. "In the current entry, delete PROPERTY."
  11503. (interactive
  11504. (let* ((completion-ignore-case t)
  11505. (prop (org-icompleting-read
  11506. "Property: " (org-entry-properties nil 'standard))))
  11507. (list prop)))
  11508. (message "Property %s %s" property
  11509. (if (org-entry-delete nil property)
  11510. "deleted"
  11511. "was not present in the entry")))
  11512. (defun org-delete-property-globally (property)
  11513. "Remove PROPERTY globally, from all entries."
  11514. (interactive
  11515. (let* ((completion-ignore-case t)
  11516. (prop (org-icompleting-read
  11517. "Globally remove property: "
  11518. (mapcar 'list (org-buffer-property-keys)))))
  11519. (list prop)))
  11520. (save-excursion
  11521. (save-restriction
  11522. (widen)
  11523. (goto-char (point-min))
  11524. (let ((cnt 0))
  11525. (while (re-search-forward
  11526. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11527. nil t)
  11528. (setq cnt (1+ cnt))
  11529. (replace-match ""))
  11530. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11531. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11532. (defun org-compute-property-at-point ()
  11533. "Compute the property at point.
  11534. This looks for an enclosing column format, extracts the operator and
  11535. then applies it to the property in the column format's scope."
  11536. (interactive)
  11537. (unless (org-at-property-p)
  11538. (error "Not at a property"))
  11539. (let ((prop (org-match-string-no-properties 2)))
  11540. (org-columns-get-format-and-top-level)
  11541. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11542. (error "No operator defined for property %s" prop))
  11543. (org-columns-compute prop)))
  11544. (defun org-property-get-allowed-values (pom property &optional table)
  11545. "Get allowed values for the property PROPERTY.
  11546. When TABLE is non-nil, return an alist that can directly be used for
  11547. completion."
  11548. (let (vals)
  11549. (cond
  11550. ((equal property "TODO")
  11551. (setq vals (org-with-point-at pom
  11552. (append org-todo-keywords-1 '("")))))
  11553. ((equal property "PRIORITY")
  11554. (let ((n org-lowest-priority))
  11555. (while (>= n org-highest-priority)
  11556. (push (char-to-string n) vals)
  11557. (setq n (1- n)))))
  11558. ((member property org-special-properties))
  11559. (t
  11560. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11561. (when (and vals (string-match "\\S-" vals))
  11562. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11563. (setq vals (mapcar (lambda (x)
  11564. (cond ((stringp x) x)
  11565. ((numberp x) (number-to-string x))
  11566. ((symbolp x) (symbol-name x))
  11567. (t "???")))
  11568. vals)))))
  11569. (if table (mapcar 'list vals) vals)))
  11570. (defun org-property-previous-allowed-value (&optional previous)
  11571. "Switch to the next allowed value for this property."
  11572. (interactive)
  11573. (org-property-next-allowed-value t))
  11574. (defun org-property-next-allowed-value (&optional previous)
  11575. "Switch to the next allowed value for this property."
  11576. (interactive)
  11577. (unless (org-at-property-p)
  11578. (error "Not at a property"))
  11579. (let* ((key (match-string 2))
  11580. (value (match-string 3))
  11581. (allowed (or (org-property-get-allowed-values (point) key)
  11582. (and (member value '("[ ]" "[-]" "[X]"))
  11583. '("[ ]" "[X]"))))
  11584. nval)
  11585. (unless allowed
  11586. (error "Allowed values for this property have not been defined"))
  11587. (if previous (setq allowed (reverse allowed)))
  11588. (if (member value allowed)
  11589. (setq nval (car (cdr (member value allowed)))))
  11590. (setq nval (or nval (car allowed)))
  11591. (if (equal nval value)
  11592. (error "Only one allowed value for this property"))
  11593. (org-at-property-p)
  11594. (replace-match (concat " :" key ": " nval) t t)
  11595. (org-indent-line-function)
  11596. (beginning-of-line 1)
  11597. (skip-chars-forward " \t")))
  11598. (defun org-find-entry-with-id (ident)
  11599. "Locate the entry that contains the ID property with exact value IDENT.
  11600. IDENT can be a string, a symbol or a number, this function will search for
  11601. the string representation of it.
  11602. Return the position where this entry starts, or nil if there is no such entry."
  11603. (interactive "sID: ")
  11604. (let ((id (cond
  11605. ((stringp ident) ident)
  11606. ((symbol-name ident) (symbol-name ident))
  11607. ((numberp ident) (number-to-string ident))
  11608. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11609. (case-fold-search nil))
  11610. (save-excursion
  11611. (save-restriction
  11612. (widen)
  11613. (goto-char (point-min))
  11614. (when (re-search-forward
  11615. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11616. nil t)
  11617. (org-back-to-heading t)
  11618. (point))))))
  11619. ;;;; Timestamps
  11620. (defvar org-last-changed-timestamp nil)
  11621. (defvar org-last-inserted-timestamp nil
  11622. "The last time stamp inserted with `org-insert-time-stamp'.")
  11623. (defvar org-time-was-given) ; dynamically scoped parameter
  11624. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11625. (defvar org-ts-what) ; dynamically scoped parameter
  11626. (defun org-time-stamp (arg &optional inactive)
  11627. "Prompt for a date/time and insert a time stamp.
  11628. If the user specifies a time like HH:MM, or if this command is called
  11629. with a prefix argument, the time stamp will contain date and time.
  11630. Otherwise, only the date will be included. All parts of a date not
  11631. specified by the user will be filled in from the current date/time.
  11632. So if you press just return without typing anything, the time stamp
  11633. will represent the current date/time. If there is already a timestamp
  11634. at the cursor, it will be modified."
  11635. (interactive "P")
  11636. (let* ((ts nil)
  11637. (default-time
  11638. ;; Default time is either today, or, when entering a range,
  11639. ;; the range start.
  11640. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11641. (save-excursion
  11642. (re-search-backward
  11643. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11644. (- (point) 20) t)))
  11645. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11646. (current-time)))
  11647. (default-input (and ts (org-get-compact-tod ts)))
  11648. org-time-was-given org-end-time-was-given time)
  11649. (cond
  11650. ((and (org-at-timestamp-p t)
  11651. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11652. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11653. (insert "--")
  11654. (setq time (let ((this-command this-command))
  11655. (org-read-date arg 'totime nil nil
  11656. default-time default-input)))
  11657. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11658. ((org-at-timestamp-p t)
  11659. (setq time (let ((this-command this-command))
  11660. (org-read-date arg 'totime nil nil default-time default-input)))
  11661. (when (org-at-timestamp-p t) ; just to get the match data
  11662. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11663. (replace-match "")
  11664. (setq org-last-changed-timestamp
  11665. (org-insert-time-stamp
  11666. time (or org-time-was-given arg)
  11667. inactive nil nil (list org-end-time-was-given))))
  11668. (message "Timestamp updated"))
  11669. (t
  11670. (setq time (let ((this-command this-command))
  11671. (org-read-date arg 'totime nil nil default-time default-input)))
  11672. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11673. nil nil (list org-end-time-was-given))))))
  11674. ;; FIXME: can we use this for something else, like computing time differences?
  11675. (defun org-get-compact-tod (s)
  11676. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11677. (let* ((t1 (match-string 1 s))
  11678. (h1 (string-to-number (match-string 2 s)))
  11679. (m1 (string-to-number (match-string 3 s)))
  11680. (t2 (and (match-end 4) (match-string 5 s)))
  11681. (h2 (and t2 (string-to-number (match-string 6 s))))
  11682. (m2 (and t2 (string-to-number (match-string 7 s))))
  11683. dh dm)
  11684. (if (not t2)
  11685. t1
  11686. (setq dh (- h2 h1) dm (- m2 m1))
  11687. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11688. (concat t1 "+" (number-to-string dh)
  11689. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11690. (defun org-time-stamp-inactive (&optional arg)
  11691. "Insert an inactive time stamp.
  11692. An inactive time stamp is enclosed in square brackets instead of angle
  11693. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11694. does not link to the calendar and cannot be changed with the S-cursor keys.
  11695. So these are more for recording a certain time/date."
  11696. (interactive "P")
  11697. (org-time-stamp arg 'inactive))
  11698. (defvar org-date-ovl (org-make-overlay 1 1))
  11699. (org-overlay-put org-date-ovl 'face 'org-warning)
  11700. (org-detach-overlay org-date-ovl)
  11701. (defvar org-ans1) ; dynamically scoped parameter
  11702. (defvar org-ans2) ; dynamically scoped parameter
  11703. (defvar org-plain-time-of-day-regexp) ; defined below
  11704. (defvar org-overriding-default-time nil) ; dynamically scoped
  11705. (defvar org-read-date-overlay nil)
  11706. (defvar org-dcst nil) ; dynamically scoped
  11707. (defvar org-read-date-history nil)
  11708. (defvar org-read-date-final-answer nil)
  11709. (defun org-read-date (&optional with-time to-time from-string prompt
  11710. default-time default-input)
  11711. "Read a date, possibly a time, and make things smooth for the user.
  11712. The prompt will suggest to enter an ISO date, but you can also enter anything
  11713. which will at least partially be understood by `parse-time-string'.
  11714. Unrecognized parts of the date will default to the current day, month, year,
  11715. hour and minute. If this command is called to replace a timestamp at point,
  11716. of to enter the second timestamp of a range, the default time is taken from the
  11717. existing stamp. For example,
  11718. 3-2-5 --> 2003-02-05
  11719. feb 15 --> currentyear-02-15
  11720. sep 12 9 --> 2009-09-12
  11721. 12:45 --> today 12:45
  11722. 22 sept 0:34 --> currentyear-09-22 0:34
  11723. 12 --> currentyear-currentmonth-12
  11724. Fri --> nearest Friday (today or later)
  11725. etc.
  11726. Furthermore you can specify a relative date by giving, as the *first* thing
  11727. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11728. change in days weeks, months, years.
  11729. With a single plus or minus, the date is relative to today. With a double
  11730. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11731. +4d --> four days from today
  11732. +4 --> same as above
  11733. +2w --> two weeks from today
  11734. ++5 --> five days from default date
  11735. The function understands only English month and weekday abbreviations,
  11736. but this can be configured with the variables `parse-time-months' and
  11737. `parse-time-weekdays'.
  11738. While prompting, a calendar is popped up - you can also select the
  11739. date with the mouse (button 1). The calendar shows a period of three
  11740. months. To scroll it to other months, use the keys `>' and `<'.
  11741. If you don't like the calendar, turn it off with
  11742. \(setq org-read-date-popup-calendar nil)
  11743. With optional argument TO-TIME, the date will immediately be converted
  11744. to an internal time.
  11745. With an optional argument WITH-TIME, the prompt will suggest to also
  11746. insert a time. Note that when WITH-TIME is not set, you can still
  11747. enter a time, and this function will inform the calling routine about
  11748. this change. The calling routine may then choose to change the format
  11749. used to insert the time stamp into the buffer to include the time.
  11750. With optional argument FROM-STRING, read from this string instead from
  11751. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11752. the time/date that is used for everything that is not specified by the
  11753. user."
  11754. (require 'parse-time)
  11755. (let* ((org-time-stamp-rounding-minutes
  11756. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11757. (org-dcst org-display-custom-times)
  11758. (ct (org-current-time))
  11759. (def (or org-overriding-default-time default-time ct))
  11760. (defdecode (decode-time def))
  11761. (dummy (progn
  11762. (when (< (nth 2 defdecode) org-extend-today-until)
  11763. (setcar (nthcdr 2 defdecode) -1)
  11764. (setcar (nthcdr 1 defdecode) 59)
  11765. (setq def (apply 'encode-time defdecode)
  11766. defdecode (decode-time def)))))
  11767. (calendar-frame-setup nil)
  11768. (calendar-move-hook nil)
  11769. (calendar-view-diary-initially-flag nil)
  11770. (view-diary-entries-initially nil)
  11771. (calendar-view-holidays-initially-flag nil)
  11772. (view-calendar-holidays-initially nil)
  11773. (timestr (format-time-string
  11774. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11775. (prompt (concat (if prompt (concat prompt " ") "")
  11776. (format "Date+time [%s]: " timestr)))
  11777. ans (org-ans0 "") org-ans1 org-ans2 final)
  11778. (cond
  11779. (from-string (setq ans from-string))
  11780. (org-read-date-popup-calendar
  11781. (save-excursion
  11782. (save-window-excursion
  11783. (calendar)
  11784. (calendar-forward-day (- (time-to-days def)
  11785. (calendar-absolute-from-gregorian
  11786. (calendar-current-date))))
  11787. (org-eval-in-calendar nil t)
  11788. (let* ((old-map (current-local-map))
  11789. (map (copy-keymap calendar-mode-map))
  11790. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11791. (org-defkey map (kbd "RET") 'org-calendar-select)
  11792. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11793. 'org-calendar-select-mouse)
  11794. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11795. 'org-calendar-select-mouse)
  11796. (org-defkey minibuffer-local-map [(meta shift left)]
  11797. (lambda () (interactive)
  11798. (org-eval-in-calendar '(calendar-backward-month 1))))
  11799. (org-defkey minibuffer-local-map [(meta shift right)]
  11800. (lambda () (interactive)
  11801. (org-eval-in-calendar '(calendar-forward-month 1))))
  11802. (org-defkey minibuffer-local-map [(meta shift up)]
  11803. (lambda () (interactive)
  11804. (org-eval-in-calendar '(calendar-backward-year 1))))
  11805. (org-defkey minibuffer-local-map [(meta shift down)]
  11806. (lambda () (interactive)
  11807. (org-eval-in-calendar '(calendar-forward-year 1))))
  11808. (org-defkey minibuffer-local-map [?\e (shift left)]
  11809. (lambda () (interactive)
  11810. (org-eval-in-calendar '(calendar-backward-month 1))))
  11811. (org-defkey minibuffer-local-map [?\e (shift right)]
  11812. (lambda () (interactive)
  11813. (org-eval-in-calendar '(calendar-forward-month 1))))
  11814. (org-defkey minibuffer-local-map [?\e (shift up)]
  11815. (lambda () (interactive)
  11816. (org-eval-in-calendar '(calendar-backward-year 1))))
  11817. (org-defkey minibuffer-local-map [?\e (shift down)]
  11818. (lambda () (interactive)
  11819. (org-eval-in-calendar '(calendar-forward-year 1))))
  11820. (org-defkey minibuffer-local-map [(shift up)]
  11821. (lambda () (interactive)
  11822. (org-eval-in-calendar '(calendar-backward-week 1))))
  11823. (org-defkey minibuffer-local-map [(shift down)]
  11824. (lambda () (interactive)
  11825. (org-eval-in-calendar '(calendar-forward-week 1))))
  11826. (org-defkey minibuffer-local-map [(shift left)]
  11827. (lambda () (interactive)
  11828. (org-eval-in-calendar '(calendar-backward-day 1))))
  11829. (org-defkey minibuffer-local-map [(shift right)]
  11830. (lambda () (interactive)
  11831. (org-eval-in-calendar '(calendar-forward-day 1))))
  11832. (org-defkey minibuffer-local-map ">"
  11833. (lambda () (interactive)
  11834. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11835. (org-defkey minibuffer-local-map "<"
  11836. (lambda () (interactive)
  11837. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11838. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11839. (unwind-protect
  11840. (progn
  11841. (use-local-map map)
  11842. (add-hook 'post-command-hook 'org-read-date-display)
  11843. (setq org-ans0 (read-string prompt default-input
  11844. 'org-read-date-history nil))
  11845. ;; org-ans0: from prompt
  11846. ;; org-ans1: from mouse click
  11847. ;; org-ans2: from calendar motion
  11848. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11849. (remove-hook 'post-command-hook 'org-read-date-display)
  11850. (use-local-map old-map)
  11851. (when org-read-date-overlay
  11852. (org-delete-overlay org-read-date-overlay)
  11853. (setq org-read-date-overlay nil)))))))
  11854. (t ; Naked prompt only
  11855. (unwind-protect
  11856. (setq ans (read-string prompt default-input
  11857. 'org-read-date-history timestr))
  11858. (when org-read-date-overlay
  11859. (org-delete-overlay org-read-date-overlay)
  11860. (setq org-read-date-overlay nil)))))
  11861. (setq final (org-read-date-analyze ans def defdecode))
  11862. (setq org-read-date-final-answer ans)
  11863. (if to-time
  11864. (apply 'encode-time final)
  11865. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11866. (format "%04d-%02d-%02d %02d:%02d"
  11867. (nth 5 final) (nth 4 final) (nth 3 final)
  11868. (nth 2 final) (nth 1 final))
  11869. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11870. (defvar def)
  11871. (defvar defdecode)
  11872. (defvar with-time)
  11873. (defvar org-read-date-analyze-futurep nil)
  11874. (defun org-read-date-display ()
  11875. "Display the current date prompt interpretation in the minibuffer."
  11876. (when org-read-date-display-live
  11877. (when org-read-date-overlay
  11878. (org-delete-overlay org-read-date-overlay))
  11879. (let ((p (point)))
  11880. (end-of-line 1)
  11881. (while (not (equal (buffer-substring
  11882. (max (point-min) (- (point) 4)) (point))
  11883. " "))
  11884. (insert " "))
  11885. (goto-char p))
  11886. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11887. " " (or org-ans1 org-ans2)))
  11888. (org-end-time-was-given nil)
  11889. (f (org-read-date-analyze ans def defdecode))
  11890. (fmts (if org-dcst
  11891. org-time-stamp-custom-formats
  11892. org-time-stamp-formats))
  11893. (fmt (if (or with-time
  11894. (and (boundp 'org-time-was-given) org-time-was-given))
  11895. (cdr fmts)
  11896. (car fmts)))
  11897. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11898. (when (and org-end-time-was-given
  11899. (string-match org-plain-time-of-day-regexp txt))
  11900. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11901. org-end-time-was-given
  11902. (substring txt (match-end 0)))))
  11903. (when org-read-date-analyze-futurep
  11904. (setq txt (concat txt " (=>F)")))
  11905. (setq org-read-date-overlay
  11906. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11907. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11908. (defun org-read-date-analyze (ans def defdecode)
  11909. "Analyse the combined answer of the date prompt."
  11910. ;; FIXME: cleanup and comment
  11911. (let (delta deltan deltaw deltadef year month day
  11912. hour minute second wday pm h2 m2 tl wday1
  11913. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11914. (setq org-read-date-analyze-futurep nil)
  11915. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11916. (setq ans "+0"))
  11917. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11918. (setq ans (replace-match "" t t ans)
  11919. deltan (car delta)
  11920. deltaw (nth 1 delta)
  11921. deltadef (nth 2 delta)))
  11922. ;; Check if there is an iso week date in there
  11923. ;; If yes, sore the info and postpone interpreting it until the rest
  11924. ;; of the parsing is done
  11925. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11926. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11927. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11928. iso-week (string-to-number (match-string 2 ans)))
  11929. (setq ans (replace-match "" t t ans)))
  11930. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11931. (when (string-match
  11932. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11933. (setq year (if (match-end 2)
  11934. (string-to-number (match-string 2 ans))
  11935. (string-to-number (format-time-string "%Y")))
  11936. month (string-to-number (match-string 3 ans))
  11937. day (string-to-number (match-string 4 ans)))
  11938. (if (< year 100) (setq year (+ 2000 year)))
  11939. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11940. t nil ans)))
  11941. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11942. ;; If there is a time with am/pm, and *no* time without it, we convert
  11943. ;; so that matching will be successful.
  11944. (loop for i from 1 to 2 do ; twice, for end time as well
  11945. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11946. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11947. (setq hour (string-to-number (match-string 1 ans))
  11948. minute (if (match-end 3)
  11949. (string-to-number (match-string 3 ans))
  11950. 0)
  11951. pm (equal ?p
  11952. (string-to-char (downcase (match-string 4 ans)))))
  11953. (if (and (= hour 12) (not pm))
  11954. (setq hour 0)
  11955. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11956. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11957. t t ans))))
  11958. ;; Check if a time range is given as a duration
  11959. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11960. (setq hour (string-to-number (match-string 1 ans))
  11961. h2 (+ hour (string-to-number (match-string 3 ans)))
  11962. minute (string-to-number (match-string 2 ans))
  11963. m2 (+ minute (if (match-end 5) (string-to-number
  11964. (match-string 5 ans))0)))
  11965. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11966. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11967. t t ans)))
  11968. ;; Check if there is a time range
  11969. (when (boundp 'org-end-time-was-given)
  11970. (setq org-time-was-given nil)
  11971. (when (and (string-match org-plain-time-of-day-regexp ans)
  11972. (match-end 8))
  11973. (setq org-end-time-was-given (match-string 8 ans))
  11974. (setq ans (concat (substring ans 0 (match-beginning 7))
  11975. (substring ans (match-end 7))))))
  11976. (setq tl (parse-time-string ans)
  11977. day (or (nth 3 tl) (nth 3 defdecode))
  11978. month (or (nth 4 tl)
  11979. (if (and org-read-date-prefer-future
  11980. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11981. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  11982. (nth 4 defdecode)))
  11983. year (or (nth 5 tl)
  11984. (if (and org-read-date-prefer-future
  11985. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11986. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  11987. (nth 5 defdecode)))
  11988. hour (or (nth 2 tl) (nth 2 defdecode))
  11989. minute (or (nth 1 tl) (nth 1 defdecode))
  11990. second (or (nth 0 tl) 0)
  11991. wday (nth 6 tl))
  11992. (when (and (eq org-read-date-prefer-future 'time)
  11993. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  11994. (equal day (nth 3 defdecode))
  11995. (equal month (nth 4 defdecode))
  11996. (equal year (nth 5 defdecode))
  11997. (nth 2 tl)
  11998. (or (< (nth 2 tl) (nth 2 defdecode))
  11999. (and (= (nth 2 tl) (nth 2 defdecode))
  12000. (nth 1 tl)
  12001. (< (nth 1 tl) (nth 1 defdecode)))))
  12002. (setq day (1+ day)
  12003. futurep t))
  12004. ;; Special date definitions below
  12005. (cond
  12006. (iso-week
  12007. ;; There was an iso week
  12008. (setq futurep nil)
  12009. (setq year (or iso-year year)
  12010. day (or iso-weekday wday 1)
  12011. wday nil ; to make sure that the trigger below does not match
  12012. iso-date (calendar-gregorian-from-absolute
  12013. (calendar-absolute-from-iso
  12014. (list iso-week day year))))
  12015. ; FIXME: Should we also push ISO weeks into the future?
  12016. ; (when (and org-read-date-prefer-future
  12017. ; (not iso-year)
  12018. ; (< (calendar-absolute-from-gregorian iso-date)
  12019. ; (time-to-days (current-time))))
  12020. ; (setq year (1+ year)
  12021. ; iso-date (calendar-gregorian-from-absolute
  12022. ; (calendar-absolute-from-iso
  12023. ; (list iso-week day year)))))
  12024. (setq month (car iso-date)
  12025. year (nth 2 iso-date)
  12026. day (nth 1 iso-date)))
  12027. (deltan
  12028. (setq futurep nil)
  12029. (unless deltadef
  12030. (let ((now (decode-time (current-time))))
  12031. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12032. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12033. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12034. ((equal deltaw "m") (setq month (+ month deltan)))
  12035. ((equal deltaw "y") (setq year (+ year deltan)))))
  12036. ((and wday (not (nth 3 tl)))
  12037. (setq futurep nil)
  12038. ;; Weekday was given, but no day, so pick that day in the week
  12039. ;; on or after the derived date.
  12040. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12041. (unless (equal wday wday1)
  12042. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12043. (if (and (boundp 'org-time-was-given)
  12044. (nth 2 tl))
  12045. (setq org-time-was-given t))
  12046. (if (< year 100) (setq year (+ 2000 year)))
  12047. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12048. (setq org-read-date-analyze-futurep futurep)
  12049. (list second minute hour day month year)))
  12050. (defvar parse-time-weekdays)
  12051. (defun org-read-date-get-relative (s today default)
  12052. "Check string S for special relative date string.
  12053. TODAY and DEFAULT are internal times, for today and for a default.
  12054. Return shift list (N what def-flag)
  12055. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12056. N is the number of WHATs to shift.
  12057. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12058. the DEFAULT date rather than TODAY."
  12059. (when (and
  12060. (string-match
  12061. (concat
  12062. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12063. "\\([0-9]+\\)?"
  12064. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12065. "\\([ \t]\\|$\\)") s)
  12066. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12067. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12068. (string-to-char (substring (match-string 1 s) -1))
  12069. ?+))
  12070. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12071. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12072. (what (if (match-end 3) (match-string 3 s) "d"))
  12073. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12074. (date (if rel default today))
  12075. (wday (nth 6 (decode-time date)))
  12076. delta)
  12077. (if wday1
  12078. (progn
  12079. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12080. (if (= dir ?-) (setq delta (- delta 7)))
  12081. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12082. (list delta "d" rel))
  12083. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12084. (defun org-eval-in-calendar (form &optional keepdate)
  12085. "Eval FORM in the calendar window and return to current window.
  12086. Also, store the cursor date in variable org-ans2."
  12087. (let ((sf (selected-frame))
  12088. (sw (selected-window)))
  12089. (select-window (get-buffer-window "*Calendar*" t))
  12090. (eval form)
  12091. (when (and (not keepdate) (calendar-cursor-to-date))
  12092. (let* ((date (calendar-cursor-to-date))
  12093. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12094. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12095. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12096. (select-window sw)
  12097. (org-select-frame-set-input-focus sf)))
  12098. (defun org-calendar-select ()
  12099. "Return to `org-read-date' with the date currently selected.
  12100. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12101. (interactive)
  12102. (when (calendar-cursor-to-date)
  12103. (let* ((date (calendar-cursor-to-date))
  12104. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12105. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12106. (if (active-minibuffer-window) (exit-minibuffer))))
  12107. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12108. "Insert a date stamp for the date given by the internal TIME.
  12109. WITH-HM means, use the stamp format that includes the time of the day.
  12110. INACTIVE means use square brackets instead of angular ones, so that the
  12111. stamp will not contribute to the agenda.
  12112. PRE and POST are optional strings to be inserted before and after the
  12113. stamp.
  12114. The command returns the inserted time stamp."
  12115. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12116. stamp)
  12117. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12118. (insert-before-markers (or pre ""))
  12119. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12120. (when (listp extra)
  12121. (setq extra (car extra))
  12122. (if (and (stringp extra)
  12123. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12124. (setq extra (format "-%02d:%02d"
  12125. (string-to-number (match-string 1 extra))
  12126. (string-to-number (match-string 2 extra))))
  12127. (setq extra nil)))
  12128. (when extra
  12129. (backward-char 1)
  12130. (insert-before-markers extra)
  12131. (forward-char 1))
  12132. (insert-before-markers (or post ""))
  12133. (setq org-last-inserted-timestamp stamp)))
  12134. (defun org-toggle-time-stamp-overlays ()
  12135. "Toggle the use of custom time stamp formats."
  12136. (interactive)
  12137. (setq org-display-custom-times (not org-display-custom-times))
  12138. (unless org-display-custom-times
  12139. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12140. (while (setq p (next-single-property-change p 'display))
  12141. (if (and (get-text-property p 'display)
  12142. (eq (get-text-property p 'face) 'org-date))
  12143. (remove-text-properties
  12144. p (setq p (next-single-property-change p 'display))
  12145. '(display t))))
  12146. (set-buffer-modified-p bmp)))
  12147. (if (featurep 'xemacs)
  12148. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12149. (org-restart-font-lock)
  12150. (setq org-table-may-need-update t)
  12151. (if org-display-custom-times
  12152. (message "Time stamps are overlayed with custom format")
  12153. (message "Time stamp overlays removed")))
  12154. (defun org-display-custom-time (beg end)
  12155. "Overlay modified time stamp format over timestamp between BEG and END."
  12156. (let* ((ts (buffer-substring beg end))
  12157. t1 w1 with-hm tf time str w2 (off 0))
  12158. (save-match-data
  12159. (setq t1 (org-parse-time-string ts t))
  12160. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12161. (setq off (- (match-end 0) (match-beginning 0)))))
  12162. (setq end (- end off))
  12163. (setq w1 (- end beg)
  12164. with-hm (and (nth 1 t1) (nth 2 t1))
  12165. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12166. time (org-fix-decoded-time t1)
  12167. str (org-add-props
  12168. (format-time-string
  12169. (substring tf 1 -1) (apply 'encode-time time))
  12170. nil 'mouse-face 'highlight)
  12171. w2 (length str))
  12172. (if (not (= w2 w1))
  12173. (add-text-properties (1+ beg) (+ 2 beg)
  12174. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12175. (if (featurep 'xemacs)
  12176. (progn
  12177. (put-text-property beg end 'invisible t)
  12178. (put-text-property beg end 'end-glyph (make-glyph str)))
  12179. (put-text-property beg end 'display str))))
  12180. (defun org-translate-time (string)
  12181. "Translate all timestamps in STRING to custom format.
  12182. But do this only if the variable `org-display-custom-times' is set."
  12183. (when org-display-custom-times
  12184. (save-match-data
  12185. (let* ((start 0)
  12186. (re org-ts-regexp-both)
  12187. t1 with-hm inactive tf time str beg end)
  12188. (while (setq start (string-match re string start))
  12189. (setq beg (match-beginning 0)
  12190. end (match-end 0)
  12191. t1 (save-match-data
  12192. (org-parse-time-string (substring string beg end) t))
  12193. with-hm (and (nth 1 t1) (nth 2 t1))
  12194. inactive (equal (substring string beg (1+ beg)) "[")
  12195. tf (funcall (if with-hm 'cdr 'car)
  12196. org-time-stamp-custom-formats)
  12197. time (org-fix-decoded-time t1)
  12198. str (format-time-string
  12199. (concat
  12200. (if inactive "[" "<") (substring tf 1 -1)
  12201. (if inactive "]" ">"))
  12202. (apply 'encode-time time))
  12203. string (replace-match str t t string)
  12204. start (+ start (length str)))))))
  12205. string)
  12206. (defun org-fix-decoded-time (time)
  12207. "Set 0 instead of nil for the first 6 elements of time.
  12208. Don't touch the rest."
  12209. (let ((n 0))
  12210. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12211. (defun org-days-to-time (timestamp-string)
  12212. "Difference between TIMESTAMP-STRING and now in days."
  12213. (- (time-to-days (org-time-string-to-time timestamp-string))
  12214. (time-to-days (current-time))))
  12215. (defun org-deadline-close (timestamp-string &optional ndays)
  12216. "Is the time in TIMESTAMP-STRING close to the current date?"
  12217. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12218. (and (< (org-days-to-time timestamp-string) ndays)
  12219. (not (org-entry-is-done-p))))
  12220. (defun org-get-wdays (ts)
  12221. "Get the deadline lead time appropriate for timestring TS."
  12222. (cond
  12223. ((<= org-deadline-warning-days 0)
  12224. ;; 0 or negative, enforce this value no matter what
  12225. (- org-deadline-warning-days))
  12226. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12227. ;; lead time is specified.
  12228. (floor (* (string-to-number (match-string 1 ts))
  12229. (cdr (assoc (match-string 2 ts)
  12230. '(("d" . 1) ("w" . 7)
  12231. ("m" . 30.4) ("y" . 365.25)))))))
  12232. ;; go for the default.
  12233. (t org-deadline-warning-days)))
  12234. (defun org-calendar-select-mouse (ev)
  12235. "Return to `org-read-date' with the date currently selected.
  12236. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12237. (interactive "e")
  12238. (mouse-set-point ev)
  12239. (when (calendar-cursor-to-date)
  12240. (let* ((date (calendar-cursor-to-date))
  12241. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12242. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12243. (if (active-minibuffer-window) (exit-minibuffer))))
  12244. (defun org-check-deadlines (ndays)
  12245. "Check if there are any deadlines due or past due.
  12246. A deadline is considered due if it happens within `org-deadline-warning-days'
  12247. days from today's date. If the deadline appears in an entry marked DONE,
  12248. it is not shown. The prefix arg NDAYS can be used to test that many
  12249. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12250. (interactive "P")
  12251. (let* ((org-warn-days
  12252. (cond
  12253. ((equal ndays '(4)) 100000)
  12254. (ndays (prefix-numeric-value ndays))
  12255. (t (abs org-deadline-warning-days))))
  12256. (case-fold-search nil)
  12257. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12258. (callback
  12259. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12260. (message "%d deadlines past-due or due within %d days"
  12261. (org-occur regexp nil callback)
  12262. org-warn-days)))
  12263. (defun org-check-before-date (date)
  12264. "Check if there are deadlines or scheduled entries before DATE."
  12265. (interactive (list (org-read-date)))
  12266. (let ((case-fold-search nil)
  12267. (regexp (concat "\\<\\(" org-deadline-string
  12268. "\\|" org-scheduled-string
  12269. "\\) *<\\([^>]+\\)>"))
  12270. (callback
  12271. (lambda () (time-less-p
  12272. (org-time-string-to-time (match-string 2))
  12273. (org-time-string-to-time date)))))
  12274. (message "%d entries before %s"
  12275. (org-occur regexp nil callback) date)))
  12276. (defun org-check-after-date (date)
  12277. "Check if there are deadlines or scheduled entries after DATE."
  12278. (interactive (list (org-read-date)))
  12279. (let ((case-fold-search nil)
  12280. (regexp (concat "\\<\\(" org-deadline-string
  12281. "\\|" org-scheduled-string
  12282. "\\) *<\\([^>]+\\)>"))
  12283. (callback
  12284. (lambda () (not
  12285. (time-less-p
  12286. (org-time-string-to-time (match-string 2))
  12287. (org-time-string-to-time date))))))
  12288. (message "%d entries after %s"
  12289. (org-occur regexp nil callback) date)))
  12290. (defun org-evaluate-time-range (&optional to-buffer)
  12291. "Evaluate a time range by computing the difference between start and end.
  12292. Normally the result is just printed in the echo area, but with prefix arg
  12293. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12294. If the time range is actually in a table, the result is inserted into the
  12295. next column.
  12296. For time difference computation, a year is assumed to be exactly 365
  12297. days in order to avoid rounding problems."
  12298. (interactive "P")
  12299. (or
  12300. (org-clock-update-time-maybe)
  12301. (save-excursion
  12302. (unless (org-at-date-range-p t)
  12303. (goto-char (point-at-bol))
  12304. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12305. (if (not (org-at-date-range-p t))
  12306. (error "Not at a time-stamp range, and none found in current line")))
  12307. (let* ((ts1 (match-string 1))
  12308. (ts2 (match-string 2))
  12309. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12310. (match-end (match-end 0))
  12311. (time1 (org-time-string-to-time ts1))
  12312. (time2 (org-time-string-to-time ts2))
  12313. (t1 (org-float-time time1))
  12314. (t2 (org-float-time time2))
  12315. (diff (abs (- t2 t1)))
  12316. (negative (< (- t2 t1) 0))
  12317. ;; (ys (floor (* 365 24 60 60)))
  12318. (ds (* 24 60 60))
  12319. (hs (* 60 60))
  12320. (fy "%dy %dd %02d:%02d")
  12321. (fy1 "%dy %dd")
  12322. (fd "%dd %02d:%02d")
  12323. (fd1 "%dd")
  12324. (fh "%02d:%02d")
  12325. y d h m align)
  12326. (if havetime
  12327. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12328. y 0
  12329. d (floor (/ diff ds)) diff (mod diff ds)
  12330. h (floor (/ diff hs)) diff (mod diff hs)
  12331. m (floor (/ diff 60)))
  12332. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12333. y 0
  12334. d (floor (+ (/ diff ds) 0.5))
  12335. h 0 m 0))
  12336. (if (not to-buffer)
  12337. (message "%s" (org-make-tdiff-string y d h m))
  12338. (if (org-at-table-p)
  12339. (progn
  12340. (goto-char match-end)
  12341. (setq align t)
  12342. (and (looking-at " *|") (goto-char (match-end 0))))
  12343. (goto-char match-end))
  12344. (if (looking-at
  12345. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12346. (replace-match ""))
  12347. (if negative (insert " -"))
  12348. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12349. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12350. (insert " " (format fh h m))))
  12351. (if align (org-table-align))
  12352. (message "Time difference inserted")))))
  12353. (defun org-make-tdiff-string (y d h m)
  12354. (let ((fmt "")
  12355. (l nil))
  12356. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12357. l (push y l)))
  12358. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12359. l (push d l)))
  12360. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12361. l (push h l)))
  12362. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12363. l (push m l)))
  12364. (apply 'format fmt (nreverse l))))
  12365. (defun org-time-string-to-time (s)
  12366. (apply 'encode-time (org-parse-time-string s)))
  12367. (defun org-time-string-to-seconds (s)
  12368. (org-float-time (org-time-string-to-time s)))
  12369. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12370. "Convert a time stamp to an absolute day number.
  12371. If there is a specifyer for a cyclic time stamp, get the closest date to
  12372. DAYNR.
  12373. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12374. the variable date is bound by the calendar when this is called."
  12375. (cond
  12376. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12377. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12378. daynr
  12379. (+ daynr 1000)))
  12380. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12381. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12382. (time-to-days (current-time))) (match-string 0 s)
  12383. prefer show-all))
  12384. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12385. (defun org-days-to-iso-week (days)
  12386. "Return the iso week number."
  12387. (require 'cal-iso)
  12388. (car (calendar-iso-from-absolute days)))
  12389. (defun org-small-year-to-year (year)
  12390. "Convert 2-digit years into 4-digit years.
  12391. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12392. The year 2000 cannot be abbreviated. Any year larger than 99
  12393. is returned unchanged."
  12394. (if (< year 38)
  12395. (setq year (+ 2000 year))
  12396. (if (< year 100)
  12397. (setq year (+ 1900 year))))
  12398. year)
  12399. (defun org-time-from-absolute (d)
  12400. "Return the time corresponding to date D.
  12401. D may be an absolute day number, or a calendar-type list (month day year)."
  12402. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12403. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12404. (defun org-calendar-holiday ()
  12405. "List of holidays, for Diary display in Org-mode."
  12406. (require 'holidays)
  12407. (let ((hl (funcall
  12408. (if (fboundp 'calendar-check-holidays)
  12409. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12410. (if hl (mapconcat 'identity hl "; "))))
  12411. (defun org-diary-sexp-entry (sexp entry date)
  12412. "Process a SEXP diary ENTRY for DATE."
  12413. (require 'diary-lib)
  12414. (let ((result (if calendar-debug-sexp
  12415. (let ((stack-trace-on-error t))
  12416. (eval (car (read-from-string sexp))))
  12417. (condition-case nil
  12418. (eval (car (read-from-string sexp)))
  12419. (error
  12420. (beep)
  12421. (message "Bad sexp at line %d in %s: %s"
  12422. (org-current-line)
  12423. (buffer-file-name) sexp)
  12424. (sleep-for 2))))))
  12425. (cond ((stringp result) result)
  12426. ((and (consp result)
  12427. (stringp (cdr result))) (cdr result))
  12428. (result entry)
  12429. (t nil))))
  12430. (defun org-diary-to-ical-string (frombuf)
  12431. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12432. This uses the icalendar.el library."
  12433. (let* ((tmpdir (if (featurep 'xemacs)
  12434. (temp-directory)
  12435. temporary-file-directory))
  12436. (tmpfile (make-temp-name
  12437. (expand-file-name "orgics" tmpdir)))
  12438. buf rtn b e)
  12439. (with-current-buffer frombuf
  12440. (icalendar-export-region (point-min) (point-max) tmpfile)
  12441. (setq buf (find-buffer-visiting tmpfile))
  12442. (set-buffer buf)
  12443. (goto-char (point-min))
  12444. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12445. (setq b (match-beginning 0)))
  12446. (goto-char (point-max))
  12447. (if (re-search-backward "^END:VEVENT" nil t)
  12448. (setq e (match-end 0)))
  12449. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12450. (kill-buffer buf)
  12451. (delete-file tmpfile)
  12452. rtn))
  12453. (defun org-closest-date (start current change prefer show-all)
  12454. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12455. When PREFER is `past' return a date that is either CURRENT or past.
  12456. When PREFER is `future', return a date that is either CURRENT or future.
  12457. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12458. ;; Make the proper lists from the dates
  12459. (catch 'exit
  12460. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12461. dn dw sday cday n1 n2 n0
  12462. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12463. (setq start (org-date-to-gregorian start)
  12464. current (org-date-to-gregorian
  12465. (if show-all
  12466. current
  12467. (time-to-days (current-time))))
  12468. sday (calendar-absolute-from-gregorian start)
  12469. cday (calendar-absolute-from-gregorian current))
  12470. (if (<= cday sday) (throw 'exit sday))
  12471. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12472. (setq dn (string-to-number (match-string 1 change))
  12473. dw (cdr (assoc (match-string 2 change) a1)))
  12474. (error "Invalid change specifyer: %s" change))
  12475. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12476. (cond
  12477. ((eq dw 'day)
  12478. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12479. n2 (+ n1 dn)))
  12480. ((eq dw 'year)
  12481. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12482. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12483. (setq date1 (list m d y1)
  12484. n1 (calendar-absolute-from-gregorian date1)
  12485. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12486. n2 (calendar-absolute-from-gregorian date2)))
  12487. ((eq dw 'month)
  12488. ;; approx number of month between the two dates
  12489. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12490. ;; How often does dn fit in there?
  12491. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12492. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12493. m (+ m nm)
  12494. ny (floor (/ m 12))
  12495. y (+ y ny)
  12496. m (- m (* ny 12)))
  12497. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12498. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12499. (setq m2 (+ m dn) y2 y)
  12500. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12501. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12502. (while (<= n2 cday)
  12503. (setq n1 n2 m m2 y y2)
  12504. (setq m2 (+ m dn) y2 y)
  12505. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12506. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12507. ;; Make sure n1 is the earlier date
  12508. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12509. (if show-all
  12510. (cond
  12511. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12512. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12513. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12514. (cond
  12515. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12516. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12517. (t (if (= cday n1) n1 n2)))))))
  12518. (defun org-date-to-gregorian (date)
  12519. "Turn any specification of DATE into a gregorian date for the calendar."
  12520. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12521. ((and (listp date) (= (length date) 3)) date)
  12522. ((stringp date)
  12523. (setq date (org-parse-time-string date))
  12524. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12525. ((listp date)
  12526. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12527. (defun org-parse-time-string (s &optional nodefault)
  12528. "Parse the standard Org-mode time string.
  12529. This should be a lot faster than the normal `parse-time-string'.
  12530. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12531. hour and minute fields will be nil if not given."
  12532. (if (string-match org-ts-regexp0 s)
  12533. (list 0
  12534. (if (or (match-beginning 8) (not nodefault))
  12535. (string-to-number (or (match-string 8 s) "0")))
  12536. (if (or (match-beginning 7) (not nodefault))
  12537. (string-to-number (or (match-string 7 s) "0")))
  12538. (string-to-number (match-string 4 s))
  12539. (string-to-number (match-string 3 s))
  12540. (string-to-number (match-string 2 s))
  12541. nil nil nil)
  12542. (error "Not a standard Org-mode time string: %s" s)))
  12543. (defun org-timestamp-up (&optional arg)
  12544. "Increase the date item at the cursor by one.
  12545. If the cursor is on the year, change the year. If it is on the month or
  12546. the day, change that.
  12547. With prefix ARG, change by that many units."
  12548. (interactive "p")
  12549. (org-timestamp-change (prefix-numeric-value arg)))
  12550. (defun org-timestamp-down (&optional arg)
  12551. "Decrease the date item at the cursor by one.
  12552. If the cursor is on the year, change the year. If it is on the month or
  12553. the day, change that.
  12554. With prefix ARG, change by that many units."
  12555. (interactive "p")
  12556. (org-timestamp-change (- (prefix-numeric-value arg))))
  12557. (defun org-timestamp-up-day (&optional arg)
  12558. "Increase the date in the time stamp by one day.
  12559. With prefix ARG, change that many days."
  12560. (interactive "p")
  12561. (if (and (not (org-at-timestamp-p t))
  12562. (org-on-heading-p))
  12563. (org-todo 'up)
  12564. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12565. (defun org-timestamp-down-day (&optional arg)
  12566. "Decrease the date in the time stamp by one day.
  12567. With prefix ARG, change that many days."
  12568. (interactive "p")
  12569. (if (and (not (org-at-timestamp-p t))
  12570. (org-on-heading-p))
  12571. (org-todo 'down)
  12572. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12573. (defun org-at-timestamp-p (&optional inactive-ok)
  12574. "Determine if the cursor is in or at a timestamp."
  12575. (interactive)
  12576. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12577. (pos (point))
  12578. (ans (or (looking-at tsr)
  12579. (save-excursion
  12580. (skip-chars-backward "^[<\n\r\t")
  12581. (if (> (point) (point-min)) (backward-char 1))
  12582. (and (looking-at tsr)
  12583. (> (- (match-end 0) pos) -1))))))
  12584. (and ans
  12585. (boundp 'org-ts-what)
  12586. (setq org-ts-what
  12587. (cond
  12588. ((= pos (match-beginning 0)) 'bracket)
  12589. ((= pos (1- (match-end 0))) 'bracket)
  12590. ((org-pos-in-match-range pos 2) 'year)
  12591. ((org-pos-in-match-range pos 3) 'month)
  12592. ((org-pos-in-match-range pos 7) 'hour)
  12593. ((org-pos-in-match-range pos 8) 'minute)
  12594. ((or (org-pos-in-match-range pos 4)
  12595. (org-pos-in-match-range pos 5)) 'day)
  12596. ((and (> pos (or (match-end 8) (match-end 5)))
  12597. (< pos (match-end 0)))
  12598. (- pos (or (match-end 8) (match-end 5))))
  12599. (t 'day))))
  12600. ans))
  12601. (defun org-toggle-timestamp-type ()
  12602. "Toggle the type (<active> or [inactive]) of a time stamp."
  12603. (interactive)
  12604. (when (org-at-timestamp-p t)
  12605. (let ((beg (match-beginning 0)) (end (match-end 0))
  12606. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12607. (save-excursion
  12608. (goto-char beg)
  12609. (while (re-search-forward "[][<>]" end t)
  12610. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12611. t t)))
  12612. (message "Timestamp is now %sactive"
  12613. (if (equal (char-after beg) ?<) "" "in")))))
  12614. (defun org-timestamp-change (n &optional what)
  12615. "Change the date in the time stamp at point.
  12616. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12617. `year', `minute', `second'. If WHAT is not given, the cursor position
  12618. in the timestamp determines what will be changed."
  12619. (let ((pos (point))
  12620. with-hm inactive
  12621. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12622. org-ts-what
  12623. extra rem
  12624. ts time time0)
  12625. (if (not (org-at-timestamp-p t))
  12626. (error "Not at a timestamp"))
  12627. (if (and (not what) (eq org-ts-what 'bracket))
  12628. (org-toggle-timestamp-type)
  12629. (if (and (not what) (not (eq org-ts-what 'day))
  12630. org-display-custom-times
  12631. (get-text-property (point) 'display)
  12632. (not (get-text-property (1- (point)) 'display)))
  12633. (setq org-ts-what 'day))
  12634. (setq org-ts-what (or what org-ts-what)
  12635. inactive (= (char-after (match-beginning 0)) ?\[)
  12636. ts (match-string 0))
  12637. (replace-match "")
  12638. (if (string-match
  12639. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12640. ts)
  12641. (setq extra (match-string 1 ts)))
  12642. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12643. (setq with-hm t))
  12644. (setq time0 (org-parse-time-string ts))
  12645. (when (and (eq org-ts-what 'minute)
  12646. (eq current-prefix-arg nil))
  12647. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12648. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12649. (setcar (cdr time0) (+ (nth 1 time0)
  12650. (if (> n 0) (- rem) (- dm rem))))))
  12651. (setq time
  12652. (encode-time (or (car time0) 0)
  12653. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12654. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12655. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12656. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12657. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12658. (nthcdr 6 time0)))
  12659. (when (and (member org-ts-what '(hour minute))
  12660. extra
  12661. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12662. (setq extra (org-modify-ts-extra
  12663. extra
  12664. (if (eq org-ts-what 'hour) 2 5)
  12665. n dm)))
  12666. (when (integerp org-ts-what)
  12667. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12668. (if (eq what 'calendar)
  12669. (let ((cal-date (org-get-date-from-calendar)))
  12670. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12671. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12672. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12673. (setcar time0 (or (car time0) 0))
  12674. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12675. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12676. (setq time (apply 'encode-time time0))))
  12677. (setq org-last-changed-timestamp
  12678. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12679. (org-clock-update-time-maybe)
  12680. (goto-char pos)
  12681. ;; Try to recenter the calendar window, if any
  12682. (if (and org-calendar-follow-timestamp-change
  12683. (get-buffer-window "*Calendar*" t)
  12684. (memq org-ts-what '(day month year)))
  12685. (org-recenter-calendar (time-to-days time))))))
  12686. (defun org-modify-ts-extra (s pos n dm)
  12687. "Change the different parts of the lead-time and repeat fields in timestamp."
  12688. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12689. ng h m new rem)
  12690. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12691. (cond
  12692. ((or (org-pos-in-match-range pos 2)
  12693. (org-pos-in-match-range pos 3))
  12694. (setq m (string-to-number (match-string 3 s))
  12695. h (string-to-number (match-string 2 s)))
  12696. (if (org-pos-in-match-range pos 2)
  12697. (setq h (+ h n))
  12698. (setq n (* dm (org-no-warnings (signum n))))
  12699. (when (not (= 0 (setq rem (% m dm))))
  12700. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12701. (setq m (+ m n)))
  12702. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12703. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12704. (setq h (min 24 (max 0 h)))
  12705. (setq ng 1 new (format "-%02d:%02d" h m)))
  12706. ((org-pos-in-match-range pos 6)
  12707. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12708. ((org-pos-in-match-range pos 5)
  12709. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12710. ((org-pos-in-match-range pos 9)
  12711. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12712. ((org-pos-in-match-range pos 8)
  12713. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12714. (when ng
  12715. (setq s (concat
  12716. (substring s 0 (match-beginning ng))
  12717. new
  12718. (substring s (match-end ng))))))
  12719. s))
  12720. (defun org-recenter-calendar (date)
  12721. "If the calendar is visible, recenter it to DATE."
  12722. (let* ((win (selected-window))
  12723. (cwin (get-buffer-window "*Calendar*" t))
  12724. (calendar-move-hook nil))
  12725. (when cwin
  12726. (select-window cwin)
  12727. (calendar-goto-date (if (listp date) date
  12728. (calendar-gregorian-from-absolute date)))
  12729. (select-window win))))
  12730. (defun org-goto-calendar (&optional arg)
  12731. "Go to the Emacs calendar at the current date.
  12732. If there is a time stamp in the current line, go to that date.
  12733. A prefix ARG can be used to force the current date."
  12734. (interactive "P")
  12735. (let ((tsr org-ts-regexp) diff
  12736. (calendar-move-hook nil)
  12737. (calendar-view-holidays-initially-flag nil)
  12738. (view-calendar-holidays-initially nil)
  12739. (calendar-view-diary-initially-flag nil)
  12740. (view-diary-entries-initially nil))
  12741. (if (or (org-at-timestamp-p)
  12742. (save-excursion
  12743. (beginning-of-line 1)
  12744. (looking-at (concat ".*" tsr))))
  12745. (let ((d1 (time-to-days (current-time)))
  12746. (d2 (time-to-days
  12747. (org-time-string-to-time (match-string 1)))))
  12748. (setq diff (- d2 d1))))
  12749. (calendar)
  12750. (calendar-goto-today)
  12751. (if (and diff (not arg)) (calendar-forward-day diff))))
  12752. (defun org-get-date-from-calendar ()
  12753. "Return a list (month day year) of date at point in calendar."
  12754. (with-current-buffer "*Calendar*"
  12755. (save-match-data
  12756. (calendar-cursor-to-date))))
  12757. (defun org-date-from-calendar ()
  12758. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12759. If there is already a time stamp at the cursor position, update it."
  12760. (interactive)
  12761. (if (org-at-timestamp-p t)
  12762. (org-timestamp-change 0 'calendar)
  12763. (let ((cal-date (org-get-date-from-calendar)))
  12764. (org-insert-time-stamp
  12765. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12766. (defun org-minutes-to-hh:mm-string (m)
  12767. "Compute H:MM from a number of minutes."
  12768. (let ((h (/ m 60)))
  12769. (setq m (- m (* 60 h)))
  12770. (format org-time-clocksum-format h m)))
  12771. (defun org-hh:mm-string-to-minutes (s)
  12772. "Convert a string H:MM to a number of minutes.
  12773. If the string is just a number, interpret it as minutes.
  12774. In fact, the first hh:mm or number in the string will be taken,
  12775. there can be extra stuff in the string.
  12776. If no number is found, the return value is 0."
  12777. (cond
  12778. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12779. (+ (* (string-to-number (match-string 1 s)) 60)
  12780. (string-to-number (match-string 2 s))))
  12781. ((string-match "\\([0-9]+\\)" s)
  12782. (string-to-number (match-string 1 s)))
  12783. (t 0)))
  12784. ;;;; Files
  12785. (defun org-save-all-org-buffers ()
  12786. "Save all Org-mode buffers without user confirmation."
  12787. (interactive)
  12788. (message "Saving all Org-mode buffers...")
  12789. (save-some-buffers t 'org-mode-p)
  12790. (when (featurep 'org-id) (org-id-locations-save))
  12791. (message "Saving all Org-mode buffers... done"))
  12792. (defun org-revert-all-org-buffers ()
  12793. "Revert all Org-mode buffers.
  12794. Prompt for confirmation when there are unsaved changes.
  12795. Be sure you know what you are doing before letting this function
  12796. overwrite your changes.
  12797. This function is useful in a setup where one tracks org files
  12798. with a version control system, to revert on one machine after pulling
  12799. changes from another. I believe the procedure must be like this:
  12800. 1. M-x org-save-all-org-buffers
  12801. 2. Pull changes from the other machine, resolve conflicts
  12802. 3. M-x org-revert-all-org-buffers"
  12803. (interactive)
  12804. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12805. (error "Abort"))
  12806. (save-excursion
  12807. (save-window-excursion
  12808. (mapc
  12809. (lambda (b)
  12810. (when (and (with-current-buffer b (org-mode-p))
  12811. (with-current-buffer b buffer-file-name))
  12812. (switch-to-buffer b)
  12813. (revert-buffer t 'no-confirm)))
  12814. (buffer-list))
  12815. (when (and (featurep 'org-id) org-id-track-globally)
  12816. (org-id-locations-load)))))
  12817. ;;;; Agenda files
  12818. ;;;###autoload
  12819. (defun org-iswitchb (&optional arg)
  12820. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12821. With a prefix argument, restrict available to files.
  12822. With two prefix arguments, restrict available buffers to agenda files."
  12823. (interactive "P")
  12824. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12825. ((equal arg '(16)) (org-buffer-list 'agenda))
  12826. (t (org-buffer-list)))))
  12827. (switch-to-buffer
  12828. (org-icompleting-read "Org buffer: "
  12829. (mapcar 'list (mapcar 'buffer-name blist))
  12830. nil t))))
  12831. ;;;###autoload
  12832. (defalias 'org-ido-switchb 'org-iswitchb)
  12833. (defun org-buffer-list (&optional predicate exclude-tmp)
  12834. "Return a list of Org buffers.
  12835. PREDICATE can be `export', `files' or `agenda'.
  12836. export restrict the list to Export buffers.
  12837. files restrict the list to buffers visiting Org files.
  12838. agenda restrict the list to buffers visiting agenda files.
  12839. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12840. (let* ((bfn nil)
  12841. (agenda-files (and (eq predicate 'agenda)
  12842. (mapcar 'file-truename (org-agenda-files t))))
  12843. (filter
  12844. (cond
  12845. ((eq predicate 'files)
  12846. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12847. ((eq predicate 'export)
  12848. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12849. ((eq predicate 'agenda)
  12850. (lambda (b)
  12851. (with-current-buffer b
  12852. (and (eq major-mode 'org-mode)
  12853. (setq bfn (buffer-file-name b))
  12854. (member (file-truename bfn) agenda-files)))))
  12855. (t (lambda (b) (with-current-buffer b
  12856. (or (eq major-mode 'org-mode)
  12857. (string-match "\*Org .*Export"
  12858. (buffer-name b)))))))))
  12859. (delq nil
  12860. (mapcar
  12861. (lambda(b)
  12862. (if (and (funcall filter b)
  12863. (or (not exclude-tmp)
  12864. (not (string-match "tmp" (buffer-name b)))))
  12865. b
  12866. nil))
  12867. (buffer-list)))))
  12868. (defun org-agenda-files (&optional unrestricted archives)
  12869. "Get the list of agenda files.
  12870. Optional UNRESTRICTED means return the full list even if a restriction
  12871. is currently in place.
  12872. When ARCHIVES is t, include all archive files hat are really being
  12873. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12874. `org-agenda-archives-mode' is t."
  12875. (let ((files
  12876. (cond
  12877. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12878. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12879. ((listp org-agenda-files) org-agenda-files)
  12880. (t (error "Invalid value of `org-agenda-files'")))))
  12881. (setq files (apply 'append
  12882. (mapcar (lambda (f)
  12883. (if (file-directory-p f)
  12884. (directory-files
  12885. f t org-agenda-file-regexp)
  12886. (list f)))
  12887. files)))
  12888. (when org-agenda-skip-unavailable-files
  12889. (setq files (delq nil
  12890. (mapcar (function
  12891. (lambda (file)
  12892. (and (file-readable-p file) file)))
  12893. files))))
  12894. (when (or (eq archives t)
  12895. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12896. (setq files (org-add-archive-files files)))
  12897. files))
  12898. (defun org-edit-agenda-file-list ()
  12899. "Edit the list of agenda files.
  12900. Depending on setup, this either uses customize to edit the variable
  12901. `org-agenda-files', or it visits the file that is holding the list. In the
  12902. latter case, the buffer is set up in a way that saving it automatically kills
  12903. the buffer and restores the previous window configuration."
  12904. (interactive)
  12905. (if (stringp org-agenda-files)
  12906. (let ((cw (current-window-configuration)))
  12907. (find-file org-agenda-files)
  12908. (org-set-local 'org-window-configuration cw)
  12909. (org-add-hook 'after-save-hook
  12910. (lambda ()
  12911. (set-window-configuration
  12912. (prog1 org-window-configuration
  12913. (kill-buffer (current-buffer))))
  12914. (org-install-agenda-files-menu)
  12915. (message "New agenda file list installed"))
  12916. nil 'local)
  12917. (message "%s" (substitute-command-keys
  12918. "Edit list and finish with \\[save-buffer]")))
  12919. (customize-variable 'org-agenda-files)))
  12920. (defun org-store-new-agenda-file-list (list)
  12921. "Set new value for the agenda file list and save it correctly."
  12922. (if (stringp org-agenda-files)
  12923. (let ((f org-agenda-files) b)
  12924. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12925. (with-temp-file f
  12926. (insert (mapconcat 'identity list "\n") "\n")))
  12927. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12928. (org-insert-mode-line-in-empty-file nil))
  12929. (setq org-agenda-files list)
  12930. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12931. (defun org-read-agenda-file-list ()
  12932. "Read the list of agenda files from a file."
  12933. (when (file-directory-p org-agenda-files)
  12934. (error "`org-agenda-files' cannot be a single directory"))
  12935. (when (stringp org-agenda-files)
  12936. (with-temp-buffer
  12937. (insert-file-contents org-agenda-files)
  12938. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12939. ;;;###autoload
  12940. (defun org-cycle-agenda-files ()
  12941. "Cycle through the files in `org-agenda-files'.
  12942. If the current buffer visits an agenda file, find the next one in the list.
  12943. If the current buffer does not, find the first agenda file."
  12944. (interactive)
  12945. (let* ((fs (org-agenda-files t))
  12946. (files (append fs (list (car fs))))
  12947. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12948. file)
  12949. (unless files (error "No agenda files"))
  12950. (catch 'exit
  12951. (while (setq file (pop files))
  12952. (if (equal (file-truename file) tcf)
  12953. (when (car files)
  12954. (find-file (car files))
  12955. (throw 'exit t))))
  12956. (find-file (car fs)))
  12957. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12958. (defun org-agenda-file-to-front (&optional to-end)
  12959. "Move/add the current file to the top of the agenda file list.
  12960. If the file is not present in the list, it is added to the front. If it is
  12961. present, it is moved there. With optional argument TO-END, add/move to the
  12962. end of the list."
  12963. (interactive "P")
  12964. (let ((org-agenda-skip-unavailable-files nil)
  12965. (file-alist (mapcar (lambda (x)
  12966. (cons (file-truename x) x))
  12967. (org-agenda-files t)))
  12968. (ctf (file-truename buffer-file-name))
  12969. x had)
  12970. (setq x (assoc ctf file-alist) had x)
  12971. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12972. (if to-end
  12973. (setq file-alist (append (delq x file-alist) (list x)))
  12974. (setq file-alist (cons x (delq x file-alist))))
  12975. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12976. (org-install-agenda-files-menu)
  12977. (message "File %s to %s of agenda file list"
  12978. (if had "moved" "added") (if to-end "end" "front"))))
  12979. (defun org-remove-file (&optional file)
  12980. "Remove current file from the list of files in variable `org-agenda-files'.
  12981. These are the files which are being checked for agenda entries.
  12982. Optional argument FILE means, use this file instead of the current."
  12983. (interactive)
  12984. (let* ((org-agenda-skip-unavailable-files nil)
  12985. (file (or file buffer-file-name))
  12986. (true-file (file-truename file))
  12987. (afile (abbreviate-file-name file))
  12988. (files (delq nil (mapcar
  12989. (lambda (x)
  12990. (if (equal true-file
  12991. (file-truename x))
  12992. nil x))
  12993. (org-agenda-files t)))))
  12994. (if (not (= (length files) (length (org-agenda-files t))))
  12995. (progn
  12996. (org-store-new-agenda-file-list files)
  12997. (org-install-agenda-files-menu)
  12998. (message "Removed file: %s" afile))
  12999. (message "File was not in list: %s (not removed)" afile))))
  13000. (defun org-file-menu-entry (file)
  13001. (vector file (list 'find-file file) t))
  13002. (defun org-check-agenda-file (file)
  13003. "Make sure FILE exists. If not, ask user what to do."
  13004. (when (not (file-exists-p file))
  13005. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13006. (abbreviate-file-name file))
  13007. (let ((r (downcase (read-char-exclusive))))
  13008. (cond
  13009. ((equal r ?r)
  13010. (org-remove-file file)
  13011. (throw 'nextfile t))
  13012. (t (error "Abort"))))))
  13013. (defun org-get-agenda-file-buffer (file)
  13014. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13015. it to the list of buffers which might be released later."
  13016. (let ((buf (org-find-base-buffer-visiting file)))
  13017. (if buf
  13018. buf ; just return it
  13019. ;; Make a new buffer and remember it
  13020. (setq buf (find-file-noselect file))
  13021. (if buf (push buf org-agenda-new-buffers))
  13022. buf)))
  13023. (defun org-release-buffers (blist)
  13024. "Release all buffers in list, asking the user for confirmation when needed.
  13025. When a buffer is unmodified, it is just killed. When modified, it is saved
  13026. \(if the user agrees) and then killed."
  13027. (let (buf file)
  13028. (while (setq buf (pop blist))
  13029. (setq file (buffer-file-name buf))
  13030. (when (and (buffer-modified-p buf)
  13031. file
  13032. (y-or-n-p (format "Save file %s? " file)))
  13033. (with-current-buffer buf (save-buffer)))
  13034. (kill-buffer buf))))
  13035. (defun org-prepare-agenda-buffers (files)
  13036. "Create buffers for all agenda files, protect archived trees and comments."
  13037. (interactive)
  13038. (let ((pa '(:org-archived t))
  13039. (pc '(:org-comment t))
  13040. (pall '(:org-archived t :org-comment t))
  13041. (inhibit-read-only t)
  13042. (rea (concat ":" org-archive-tag ":"))
  13043. bmp file re)
  13044. (save-excursion
  13045. (save-restriction
  13046. (while (setq file (pop files))
  13047. (catch 'nextfile
  13048. (if (bufferp file)
  13049. (set-buffer file)
  13050. (org-check-agenda-file file)
  13051. (set-buffer (org-get-agenda-file-buffer file)))
  13052. (widen)
  13053. (setq bmp (buffer-modified-p))
  13054. (org-refresh-category-properties)
  13055. (setq org-todo-keywords-for-agenda
  13056. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13057. (setq org-done-keywords-for-agenda
  13058. (append org-done-keywords-for-agenda org-done-keywords))
  13059. (setq org-todo-keyword-alist-for-agenda
  13060. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13061. (setq org-drawers-for-agenda
  13062. (append org-drawers-for-agenda org-drawers))
  13063. (setq org-tag-alist-for-agenda
  13064. (append org-tag-alist-for-agenda org-tag-alist))
  13065. (save-excursion
  13066. (remove-text-properties (point-min) (point-max) pall)
  13067. (when org-agenda-skip-archived-trees
  13068. (goto-char (point-min))
  13069. (while (re-search-forward rea nil t)
  13070. (if (org-on-heading-p t)
  13071. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13072. (goto-char (point-min))
  13073. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13074. (while (re-search-forward re nil t)
  13075. (add-text-properties
  13076. (match-beginning 0) (org-end-of-subtree t) pc)))
  13077. (set-buffer-modified-p bmp)))))
  13078. (setq org-todo-keyword-alist-for-agenda
  13079. (org-uniquify org-todo-keyword-alist-for-agenda)
  13080. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13081. ;;;; Embedded LaTeX
  13082. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13083. "Keymap for the minor `org-cdlatex-mode'.")
  13084. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13085. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13086. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13087. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13088. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13089. (defvar org-cdlatex-texmathp-advice-is-done nil
  13090. "Flag remembering if we have applied the advice to texmathp already.")
  13091. (define-minor-mode org-cdlatex-mode
  13092. "Toggle the minor `org-cdlatex-mode'.
  13093. This mode supports entering LaTeX environment and math in LaTeX fragments
  13094. in Org-mode.
  13095. \\{org-cdlatex-mode-map}"
  13096. nil " OCDL" nil
  13097. (when org-cdlatex-mode (require 'cdlatex))
  13098. (unless org-cdlatex-texmathp-advice-is-done
  13099. (setq org-cdlatex-texmathp-advice-is-done t)
  13100. (defadvice texmathp (around org-math-always-on activate)
  13101. "Always return t in org-mode buffers.
  13102. This is because we want to insert math symbols without dollars even outside
  13103. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13104. an embedded LaTeX fragment, let texmathp do its job.
  13105. \\[org-cdlatex-mode-map]"
  13106. (interactive)
  13107. (let (p)
  13108. (cond
  13109. ((not (org-mode-p)) ad-do-it)
  13110. ((eq this-command 'cdlatex-math-symbol)
  13111. (setq ad-return-value t
  13112. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13113. (t
  13114. (let ((p (org-inside-LaTeX-fragment-p)))
  13115. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13116. (setq ad-return-value t
  13117. texmathp-why '("Org-mode embedded math" . 0))
  13118. (if p ad-do-it)))))))))
  13119. (defun turn-on-org-cdlatex ()
  13120. "Unconditionally turn on `org-cdlatex-mode'."
  13121. (org-cdlatex-mode 1))
  13122. (defun org-inside-LaTeX-fragment-p ()
  13123. "Test if point is inside a LaTeX fragment.
  13124. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13125. sequence appearing also before point.
  13126. Even though the matchers for math are configurable, this function assumes
  13127. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13128. delimiters are skipped when they have been removed by customization.
  13129. The return value is nil, or a cons cell with the delimiter and
  13130. and the position of this delimiter.
  13131. This function does a reasonably good job, but can locally be fooled by
  13132. for example currency specifications. For example it will assume being in
  13133. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13134. fragments that are properly closed, but during editing, we have to live
  13135. with the uncertainty caused by missing closing delimiters. This function
  13136. looks only before point, not after."
  13137. (catch 'exit
  13138. (let ((pos (point))
  13139. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13140. (lim (progn
  13141. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13142. (point)))
  13143. dd-on str (start 0) m re)
  13144. (goto-char pos)
  13145. (when dodollar
  13146. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13147. re (nth 1 (assoc "$" org-latex-regexps)))
  13148. (while (string-match re str start)
  13149. (cond
  13150. ((= (match-end 0) (length str))
  13151. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13152. ((= (match-end 0) (- (length str) 5))
  13153. (throw 'exit nil))
  13154. (t (setq start (match-end 0))))))
  13155. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13156. (goto-char pos)
  13157. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13158. (and (match-beginning 2) (throw 'exit nil))
  13159. ;; count $$
  13160. (while (re-search-backward "\\$\\$" lim t)
  13161. (setq dd-on (not dd-on)))
  13162. (goto-char pos)
  13163. (if dd-on (cons "$$" m))))))
  13164. (defun org-try-cdlatex-tab ()
  13165. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13166. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13167. - inside a LaTeX fragment, or
  13168. - after the first word in a line, where an abbreviation expansion could
  13169. insert a LaTeX environment."
  13170. (when org-cdlatex-mode
  13171. (cond
  13172. ((save-excursion
  13173. (skip-chars-backward "a-zA-Z0-9*")
  13174. (skip-chars-backward " \t")
  13175. (bolp))
  13176. (cdlatex-tab) t)
  13177. ((org-inside-LaTeX-fragment-p)
  13178. (cdlatex-tab) t)
  13179. (t nil))))
  13180. (defun org-cdlatex-underscore-caret (&optional arg)
  13181. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13182. Revert to the normal definition outside of these fragments."
  13183. (interactive "P")
  13184. (if (org-inside-LaTeX-fragment-p)
  13185. (call-interactively 'cdlatex-sub-superscript)
  13186. (let (org-cdlatex-mode)
  13187. (call-interactively (key-binding (vector last-input-event))))))
  13188. (defun org-cdlatex-math-modify (&optional arg)
  13189. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13190. Revert to the normal definition outside of these fragments."
  13191. (interactive "P")
  13192. (if (org-inside-LaTeX-fragment-p)
  13193. (call-interactively 'cdlatex-math-modify)
  13194. (let (org-cdlatex-mode)
  13195. (call-interactively (key-binding (vector last-input-event))))))
  13196. (defvar org-latex-fragment-image-overlays nil
  13197. "List of overlays carrying the images of latex fragments.")
  13198. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13199. (defun org-remove-latex-fragment-image-overlays ()
  13200. "Remove all overlays with LaTeX fragment images in current buffer."
  13201. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13202. (setq org-latex-fragment-image-overlays nil))
  13203. (defun org-preview-latex-fragment (&optional subtree)
  13204. "Preview the LaTeX fragment at point, or all locally or globally.
  13205. If the cursor is in a LaTeX fragment, create the image and overlay
  13206. it over the source code. If there is no fragment at point, display
  13207. all fragments in the current text, from one headline to the next. With
  13208. prefix SUBTREE, display all fragments in the current subtree. With a
  13209. double prefix `C-u C-u', or when the cursor is before the first headline,
  13210. display all fragments in the buffer.
  13211. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13212. (interactive "P")
  13213. (org-remove-latex-fragment-image-overlays)
  13214. (save-excursion
  13215. (save-restriction
  13216. (let (beg end at msg)
  13217. (cond
  13218. ((or (equal subtree '(16))
  13219. (not (save-excursion
  13220. (re-search-backward (concat "^" outline-regexp) nil t))))
  13221. (setq beg (point-min) end (point-max)
  13222. msg "Creating images for buffer...%s"))
  13223. ((equal subtree '(4))
  13224. (org-back-to-heading)
  13225. (setq beg (point) end (org-end-of-subtree t)
  13226. msg "Creating images for subtree...%s"))
  13227. (t
  13228. (if (setq at (org-inside-LaTeX-fragment-p))
  13229. (goto-char (max (point-min) (- (cdr at) 2)))
  13230. (org-back-to-heading))
  13231. (setq beg (point) end (progn (outline-next-heading) (point))
  13232. msg (if at "Creating image...%s"
  13233. "Creating images for entry...%s"))))
  13234. (message msg "")
  13235. (narrow-to-region beg end)
  13236. (goto-char beg)
  13237. (org-format-latex
  13238. (concat "ltxpng/" (file-name-sans-extension
  13239. (file-name-nondirectory
  13240. buffer-file-name)))
  13241. default-directory 'overlays msg at 'forbuffer)
  13242. (message msg "done. Use `C-c C-c' to remove images.")))))
  13243. (defvar org-latex-regexps
  13244. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13245. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13246. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13247. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13248. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13249. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13250. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13251. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13252. "Regular expressions for matching embedded LaTeX.")
  13253. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13254. "Replace LaTeX fragments with links to an image, and produce images.
  13255. Some of the options can be changed using the variable
  13256. `org-format-latex-options'."
  13257. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13258. (let* ((prefixnodir (file-name-nondirectory prefix))
  13259. (absprefix (expand-file-name prefix dir))
  13260. (todir (file-name-directory absprefix))
  13261. (opt org-format-latex-options)
  13262. (matchers (plist-get opt :matchers))
  13263. (re-list org-latex-regexps)
  13264. (cnt 0) txt link beg end re e checkdir
  13265. executables-checked
  13266. m n block linkfile movefile ov)
  13267. ;; Check if there are old images files with this prefix, and remove them
  13268. (when (file-directory-p todir)
  13269. (mapc 'delete-file
  13270. (directory-files
  13271. todir 'full
  13272. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13273. ;; Check the different regular expressions
  13274. (while (setq e (pop re-list))
  13275. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13276. block (if (nth 3 e) "\n\n" ""))
  13277. (when (member m matchers)
  13278. (goto-char (point-min))
  13279. (while (re-search-forward re nil t)
  13280. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13281. (not (get-text-property (match-beginning n)
  13282. 'org-protected))
  13283. (or (not overlays)
  13284. (not (eq (get-char-property (match-beginning n)
  13285. 'org-overlay-type)
  13286. 'org-latex-overlay))))
  13287. (setq txt (match-string n)
  13288. beg (match-beginning n) end (match-end n)
  13289. cnt (1+ cnt)
  13290. linkfile (format "%s_%04d.png" prefix cnt)
  13291. movefile (format "%s_%04d.png" absprefix cnt)
  13292. link (concat block "[[file:" linkfile "]]" block))
  13293. (if msg (message msg cnt))
  13294. (goto-char beg)
  13295. (unless checkdir ; make sure the directory exists
  13296. (setq checkdir t)
  13297. (or (file-directory-p todir) (make-directory todir)))
  13298. (unless executables-checked
  13299. (org-check-external-command
  13300. "latex" "needed to convert LaTeX fragments to images")
  13301. (org-check-external-command
  13302. "dvipng" "needed to convert LaTeX fragments to images")
  13303. (setq executables-checked t))
  13304. (org-create-formula-image
  13305. txt movefile opt forbuffer)
  13306. (if overlays
  13307. (progn
  13308. (mapc (lambda (o)
  13309. (if (eq (org-overlay-get o 'org-overlay-type)
  13310. 'org-latex-overlay)
  13311. (org-delete-overlay o)))
  13312. (org-overlays-in beg end))
  13313. (setq ov (org-make-overlay beg end))
  13314. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13315. (if (featurep 'xemacs)
  13316. (progn
  13317. (org-overlay-put ov 'invisible t)
  13318. (org-overlay-put
  13319. ov 'end-glyph
  13320. (make-glyph (vector 'png :file movefile))))
  13321. (org-overlay-put
  13322. ov 'display
  13323. (list 'image :type 'png :file movefile :ascent 'center)))
  13324. (push ov org-latex-fragment-image-overlays)
  13325. (goto-char end))
  13326. (delete-region beg end)
  13327. (insert link))))))))
  13328. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13329. ;; This function borrows from Ganesh Swami's latex2png.el
  13330. (defun org-create-formula-image (string tofile options buffer)
  13331. "This calls dvipng."
  13332. (require 'org-latex)
  13333. (let* ((tmpdir (if (featurep 'xemacs)
  13334. (temp-directory)
  13335. temporary-file-directory))
  13336. (texfilebase (make-temp-name
  13337. (expand-file-name "orgtex" tmpdir)))
  13338. (texfile (concat texfilebase ".tex"))
  13339. (dvifile (concat texfilebase ".dvi"))
  13340. (pngfile (concat texfilebase ".png"))
  13341. (fnh (if (featurep 'xemacs)
  13342. (font-height (get-face-font 'default))
  13343. (face-attribute 'default :height nil)))
  13344. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13345. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13346. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13347. "Black"))
  13348. (bg (or (plist-get options (if buffer :background :html-background))
  13349. "Transparent")))
  13350. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13351. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13352. (with-temp-file texfile
  13353. (insert org-format-latex-header
  13354. (if org-export-latex-packages-alist
  13355. (concat "\n"
  13356. (mapconcat (lambda(p)
  13357. (if (equal "" (car p))
  13358. (format "\\usepackage{%s}" (cadr p))
  13359. (format "\\usepackage[%s]{%s}"
  13360. (car p) (cadr p))))
  13361. org-export-latex-packages-alist "\n"))
  13362. "")
  13363. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13364. (let ((dir default-directory))
  13365. (condition-case nil
  13366. (progn
  13367. (cd tmpdir)
  13368. (call-process "latex" nil nil nil texfile))
  13369. (error nil))
  13370. (cd dir))
  13371. (if (not (file-exists-p dvifile))
  13372. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13373. (condition-case nil
  13374. (call-process "dvipng" nil nil nil
  13375. "-fg" fg "-bg" bg
  13376. "-D" dpi
  13377. ;;"-x" scale "-y" scale
  13378. "-T" "tight"
  13379. "-o" pngfile
  13380. dvifile)
  13381. (error nil))
  13382. (if (not (file-exists-p pngfile))
  13383. (progn (message "Failed to create png file from %s" texfile) nil)
  13384. ;; Use the requested file name and clean up
  13385. (copy-file pngfile tofile 'replace)
  13386. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13387. (delete-file (concat texfilebase e)))
  13388. pngfile))))
  13389. (defun org-dvipng-color (attr)
  13390. "Return an rgb color specification for dvipng."
  13391. (apply 'format "rgb %s %s %s"
  13392. (mapcar 'org-normalize-color
  13393. (color-values (face-attribute 'default attr nil)))))
  13394. (defun org-normalize-color (value)
  13395. "Return string to be used as color value for an RGB component."
  13396. (format "%g" (/ value 65535.0)))
  13397. ;;;; Key bindings
  13398. ;; Make `C-c C-x' a prefix key
  13399. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13400. ;; TAB key with modifiers
  13401. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13402. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13403. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13404. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13405. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13406. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13407. ;; The following line is necessary under Suse GNU/Linux
  13408. (unless (featurep 'xemacs)
  13409. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13410. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13411. (define-key org-mode-map [backtab] 'org-shifttab)
  13412. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13413. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13414. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13415. ;; Cursor keys with modifiers
  13416. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13417. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13418. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13419. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13420. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13421. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13422. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13423. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13424. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13425. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13426. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13427. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13428. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13429. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13430. ;;; Extra keys for tty access.
  13431. ;; We only set them when really needed because otherwise the
  13432. ;; menus don't show the simple keys
  13433. (when (or org-use-extra-keys
  13434. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13435. (not window-system))
  13436. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13437. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13438. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13439. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13440. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13441. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13442. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13443. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13444. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13445. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13446. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13447. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13448. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13449. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13450. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13451. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13452. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13453. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13454. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13455. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13456. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13457. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13458. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13459. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13460. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13461. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13462. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13463. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13464. ;; All the other keys
  13465. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13466. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13467. (if (boundp 'narrow-map)
  13468. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13469. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13470. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13471. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13472. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13473. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13474. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13475. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13476. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13477. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13478. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13479. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13480. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13481. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13482. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13483. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13484. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13485. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13486. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13487. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13488. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13489. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13490. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13491. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13492. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13493. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13494. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13495. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13496. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13497. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13498. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13499. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13500. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13501. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13502. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13503. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13504. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13505. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13506. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13507. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13508. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13509. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13510. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13511. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13512. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13513. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13514. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13515. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13516. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13517. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13518. (org-defkey org-mode-map "\C-m" 'org-return)
  13519. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13520. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13521. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13522. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13523. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13524. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13525. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13526. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13527. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13528. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13529. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13530. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13531. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13532. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13533. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13534. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13535. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13536. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13537. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13538. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13539. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13540. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13541. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13542. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13543. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13544. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13545. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13546. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13547. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13548. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13549. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13550. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13551. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13552. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13553. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13554. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13555. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13556. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13557. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13558. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13559. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13560. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13561. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13562. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13563. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13564. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13565. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13566. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13567. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13568. (when (featurep 'xemacs)
  13569. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13570. (defconst org-speed-commands-default
  13571. '(
  13572. ("n" . outline-next-visible-heading)
  13573. ("p" . outline-previous-visible-heading)
  13574. ("f" . org-forward-same-level)
  13575. ("b" . org-backward-same-level)
  13576. ("u" . outline-up-heading)
  13577. ("c" . org-cycle)
  13578. ("C" . org-shifttab)
  13579. ("U" . org-shiftmetaup)
  13580. ("D" . org-shiftmetadown)
  13581. ("r" . org-metaright)
  13582. ("l" . org-metaleft)
  13583. ("R" . org-shiftmetaright)
  13584. ("L" . org-shiftmetaleft)
  13585. ("i" . (progn (forward-char 1) (call-interactively
  13586. 'org-insert-heading-respect-content)))
  13587. ("a" . org-agenda)
  13588. ("/" . org-sparse-tree)
  13589. (";" . org-set-tags-command)
  13590. ("I" . org-clock-in)
  13591. ("O" . org-clock-out)
  13592. ("o" . org-open-at-point)
  13593. ("t" . org-todo)
  13594. ("j" . org-goto)
  13595. ("g" . (org-refile t))
  13596. ("e" . org-set-effort)
  13597. ("0" . (org-priority ?\ ))
  13598. ("1" . (org-priority ?A))
  13599. ("2" . (org-priority ?B))
  13600. ("3" . (org-priority ?C))
  13601. ("." . outline-mark-subtree)
  13602. ("^" . org-sort)
  13603. ("w" . org-refile)
  13604. ("a" . org-archive-subtree-default-with-confirmation)
  13605. ("/" . org-sparse-tree)
  13606. ("?" . org-speed-command-help)
  13607. )
  13608. "The default speed commands.")
  13609. (defun org-print-speed-command (e)
  13610. (princ (car e))
  13611. (princ " ")
  13612. (if (symbolp (cdr e))
  13613. (princ (symbol-name (cdr e)))
  13614. (prin1 (cdr e)))
  13615. (princ "\n"))
  13616. (defun org-speed-command-help ()
  13617. "Show the available speed commands."
  13618. (interactive)
  13619. (if (not org-use-speed-commands)
  13620. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13621. (with-output-to-temp-buffer "*Help*"
  13622. (princ "Speed commands\n==============\n")
  13623. (mapc 'org-print-speed-command org-speed-commands-user)
  13624. (princ "\n")
  13625. (mapc 'org-print-speed-command org-speed-commands-default))))
  13626. (defvar org-self-insert-command-undo-counter 0)
  13627. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13628. (defvar org-speed-command nil)
  13629. (defun org-self-insert-command (N)
  13630. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13631. If the cursor is in a table looking at whitespace, the whitespace is
  13632. overwritten, and the table is not marked as requiring realignment."
  13633. (interactive "p")
  13634. (cond
  13635. ((and org-use-speed-commands
  13636. (bolp)
  13637. (looking-at outline-regexp)
  13638. (setq
  13639. org-speed-command
  13640. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13641. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13642. (cond
  13643. ((commandp org-speed-command)
  13644. (setq this-command org-speed-command)
  13645. (call-interactively org-speed-command))
  13646. ((functionp org-speed-command)
  13647. (funcall org-speed-command))
  13648. ((and org-speed-command (listp org-speed-command))
  13649. (eval org-speed-command))
  13650. (t (let (org-use-speed-commands)
  13651. (call-interactively 'org-self-insert-command)))))
  13652. ((and
  13653. (org-table-p)
  13654. (progn
  13655. ;; check if we blank the field, and if that triggers align
  13656. (and (featurep 'org-table) org-table-auto-blank-field
  13657. (member last-command
  13658. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13659. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13660. ;; got extra space, this field does not determine column width
  13661. (let (org-table-may-need-update) (org-table-blank-field))
  13662. ;; no extra space, this field may determine column width
  13663. (org-table-blank-field)))
  13664. t)
  13665. (eq N 1)
  13666. (looking-at "[^|\n]* |"))
  13667. (let (org-table-may-need-update)
  13668. (goto-char (1- (match-end 0)))
  13669. (delete-backward-char 1)
  13670. (goto-char (match-beginning 0))
  13671. (self-insert-command N)))
  13672. (t
  13673. (setq org-table-may-need-update t)
  13674. (self-insert-command N)
  13675. (org-fix-tags-on-the-fly)
  13676. (if org-self-insert-cluster-for-undo
  13677. (if (not (eq last-command 'org-self-insert-command))
  13678. (setq org-self-insert-command-undo-counter 1)
  13679. (if (>= org-self-insert-command-undo-counter 20)
  13680. (setq org-self-insert-command-undo-counter 1)
  13681. (and (> org-self-insert-command-undo-counter 0)
  13682. buffer-undo-list
  13683. (not (cadr buffer-undo-list)) ; remove nil entry
  13684. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13685. (setq org-self-insert-command-undo-counter
  13686. (1+ org-self-insert-command-undo-counter))))))))
  13687. (defun org-fix-tags-on-the-fly ()
  13688. (when (and (equal (char-after (point-at-bol)) ?*)
  13689. (org-on-heading-p))
  13690. (org-align-tags-here org-tags-column)))
  13691. (defun org-delete-backward-char (N)
  13692. "Like `delete-backward-char', insert whitespace at field end in tables.
  13693. When deleting backwards, in tables this function will insert whitespace in
  13694. front of the next \"|\" separator, to keep the table aligned. The table will
  13695. still be marked for re-alignment if the field did fill the entire column,
  13696. because, in this case the deletion might narrow the column."
  13697. (interactive "p")
  13698. (if (and (org-table-p)
  13699. (eq N 1)
  13700. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13701. (looking-at ".*?|"))
  13702. (let ((pos (point))
  13703. (noalign (looking-at "[^|\n\r]* |"))
  13704. (c org-table-may-need-update))
  13705. (backward-delete-char N)
  13706. (skip-chars-forward "^|")
  13707. (insert " ")
  13708. (goto-char (1- pos))
  13709. ;; noalign: if there were two spaces at the end, this field
  13710. ;; does not determine the width of the column.
  13711. (if noalign (setq org-table-may-need-update c)))
  13712. (backward-delete-char N)
  13713. (org-fix-tags-on-the-fly)))
  13714. (defun org-delete-char (N)
  13715. "Like `delete-char', but insert whitespace at field end in tables.
  13716. When deleting characters, in tables this function will insert whitespace in
  13717. front of the next \"|\" separator, to keep the table aligned. The table will
  13718. still be marked for re-alignment if the field did fill the entire column,
  13719. because, in this case the deletion might narrow the column."
  13720. (interactive "p")
  13721. (if (and (org-table-p)
  13722. (not (bolp))
  13723. (not (= (char-after) ?|))
  13724. (eq N 1))
  13725. (if (looking-at ".*?|")
  13726. (let ((pos (point))
  13727. (noalign (looking-at "[^|\n\r]* |"))
  13728. (c org-table-may-need-update))
  13729. (replace-match (concat
  13730. (substring (match-string 0) 1 -1)
  13731. " |"))
  13732. (goto-char pos)
  13733. ;; noalign: if there were two spaces at the end, this field
  13734. ;; does not determine the width of the column.
  13735. (if noalign (setq org-table-may-need-update c)))
  13736. (delete-char N))
  13737. (delete-char N)
  13738. (org-fix-tags-on-the-fly)))
  13739. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13740. (put 'org-self-insert-command 'delete-selection t)
  13741. (put 'orgtbl-self-insert-command 'delete-selection t)
  13742. (put 'org-delete-char 'delete-selection 'supersede)
  13743. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13744. (put 'org-yank 'delete-selection 'yank)
  13745. ;; Make `flyspell-mode' delay after some commands
  13746. (put 'org-self-insert-command 'flyspell-delayed t)
  13747. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13748. (put 'org-delete-char 'flyspell-delayed t)
  13749. (put 'org-delete-backward-char 'flyspell-delayed t)
  13750. ;; Make pabbrev-mode expand after org-mode commands
  13751. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13752. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13753. ;; How to do this: Measure non-white length of current string
  13754. ;; If equal to column width, we should realign.
  13755. (defun org-remap (map &rest commands)
  13756. "In MAP, remap the functions given in COMMANDS.
  13757. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13758. (let (new old)
  13759. (while commands
  13760. (setq old (pop commands) new (pop commands))
  13761. (if (fboundp 'command-remapping)
  13762. (org-defkey map (vector 'remap old) new)
  13763. (substitute-key-definition old new map global-map)))))
  13764. (when (eq org-enable-table-editor 'optimized)
  13765. ;; If the user wants maximum table support, we need to hijack
  13766. ;; some standard editing functions
  13767. (org-remap org-mode-map
  13768. 'self-insert-command 'org-self-insert-command
  13769. 'delete-char 'org-delete-char
  13770. 'delete-backward-char 'org-delete-backward-char)
  13771. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13772. (defvar org-ctrl-c-ctrl-c-hook nil
  13773. "Hook for functions attaching themselves to `C-c C-c'.
  13774. This can be used to add additional functionality to the C-c C-c key which
  13775. executes context-dependent commands.
  13776. Each function will be called with no arguments. The function must check
  13777. if the context is appropriate for it to act. If yes, it should do its
  13778. thing and then return a non-nil value. If the context is wrong,
  13779. just do nothing and return nil.")
  13780. (defvar org-tab-first-hook nil
  13781. "Hook for functions to attach themselves to TAB.
  13782. See `org-ctrl-c-ctrl-c-hook' for more information.
  13783. This hook runs as the first action when TAB is pressed, even before
  13784. `org-cycle' messes around with the `outline-regexp' to cater for
  13785. inline tasks and plain list item folding.
  13786. If any function in this hook returns t, not other actions like table
  13787. field motion visibility cycling will be done.")
  13788. (defvar org-tab-after-check-for-table-hook nil
  13789. "Hook for functions to attach themselves to TAB.
  13790. See `org-ctrl-c-ctrl-c-hook' for more information.
  13791. This hook runs after it has been established that the cursor is not in a
  13792. table, but before checking if the cursor is in a headline or if global cycling
  13793. should be done.
  13794. If any function in this hook returns t, not other actions like visibility
  13795. cycling will be done.")
  13796. (defvar org-tab-after-check-for-cycling-hook nil
  13797. "Hook for functions to attach themselves to TAB.
  13798. See `org-ctrl-c-ctrl-c-hook' for more information.
  13799. This hook runs after it has been established that not table field motion and
  13800. not visibility should be done because of current context. This is probably
  13801. the place where a package like yasnippets can hook in.")
  13802. (defvar org-tab-before-tab-emulation-hook nil
  13803. "Hook for functions to attach themselves to TAB.
  13804. See `org-ctrl-c-ctrl-c-hook' for more information.
  13805. This hook runs after every other options for TAB have been exhausted, but
  13806. before indentation and \t insertion takes place.")
  13807. (defvar org-metaleft-hook nil
  13808. "Hook for functions attaching themselves to `M-left'.
  13809. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13810. (defvar org-metaright-hook nil
  13811. "Hook for functions attaching themselves to `M-right'.
  13812. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13813. (defvar org-metaup-hook nil
  13814. "Hook for functions attaching themselves to `M-up'.
  13815. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13816. (defvar org-metadown-hook nil
  13817. "Hook for functions attaching themselves to `M-down'.
  13818. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13819. (defvar org-shiftmetaleft-hook nil
  13820. "Hook for functions attaching themselves to `M-S-left'.
  13821. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13822. (defvar org-shiftmetaright-hook nil
  13823. "Hook for functions attaching themselves to `M-S-right'.
  13824. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13825. (defvar org-shiftmetaup-hook nil
  13826. "Hook for functions attaching themselves to `M-S-up'.
  13827. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13828. (defvar org-shiftmetadown-hook nil
  13829. "Hook for functions attaching themselves to `M-S-down'.
  13830. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13831. (defvar org-metareturn-hook nil
  13832. "Hook for functions attaching themselves to `M-RET'.
  13833. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13834. (defun org-modifier-cursor-error ()
  13835. "Throw an error, a modified cursor command was applied in wrong context."
  13836. (error "This command is active in special context like tables, headlines or items"))
  13837. (defun org-shiftselect-error ()
  13838. "Throw an error because Shift-Cursor command was applied in wrong context."
  13839. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13840. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13841. (error "This command works only in special context like headlines or timestamps")))
  13842. (defun org-call-for-shift-select (cmd)
  13843. (let ((this-command-keys-shift-translated t))
  13844. (call-interactively cmd)))
  13845. (defun org-shifttab (&optional arg)
  13846. "Global visibility cycling or move to previous table field.
  13847. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13848. on context.
  13849. See the individual commands for more information."
  13850. (interactive "P")
  13851. (cond
  13852. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13853. ((integerp arg)
  13854. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13855. (message "Content view to level: %d" arg)
  13856. (org-content (prefix-numeric-value arg2))
  13857. (setq org-cycle-global-status 'overview)))
  13858. (t (call-interactively 'org-global-cycle))))
  13859. (defun org-shiftmetaleft ()
  13860. "Promote subtree or delete table column.
  13861. Calls `org-promote-subtree', `org-outdent-item',
  13862. or `org-table-delete-column', depending on context.
  13863. See the individual commands for more information."
  13864. (interactive)
  13865. (cond
  13866. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13867. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13868. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13869. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13870. (t (org-modifier-cursor-error))))
  13871. (defun org-shiftmetaright ()
  13872. "Demote subtree or insert table column.
  13873. Calls `org-demote-subtree', `org-indent-item',
  13874. or `org-table-insert-column', depending on context.
  13875. See the individual commands for more information."
  13876. (interactive)
  13877. (cond
  13878. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13879. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13880. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13881. ((org-at-item-p) (call-interactively 'org-indent-item))
  13882. (t (org-modifier-cursor-error))))
  13883. (defun org-shiftmetaup (&optional arg)
  13884. "Move subtree up or kill table row.
  13885. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13886. `org-move-item-up' depending on context. See the individual commands
  13887. for more information."
  13888. (interactive "P")
  13889. (cond
  13890. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13891. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13892. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13893. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13894. (t (org-modifier-cursor-error))))
  13895. (defun org-shiftmetadown (&optional arg)
  13896. "Move subtree down or insert table row.
  13897. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13898. `org-move-item-down', depending on context. See the individual
  13899. commands for more information."
  13900. (interactive "P")
  13901. (cond
  13902. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13903. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13904. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13905. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13906. (t (org-modifier-cursor-error))))
  13907. (defun org-metaleft (&optional arg)
  13908. "Promote heading or move table column to left.
  13909. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13910. With no specific context, calls the Emacs default `backward-word'.
  13911. See the individual commands for more information."
  13912. (interactive "P")
  13913. (cond
  13914. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13915. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13916. ((or (org-on-heading-p)
  13917. (and (org-region-active-p)
  13918. (save-excursion
  13919. (goto-char (region-beginning))
  13920. (org-on-heading-p))))
  13921. (call-interactively 'org-do-promote))
  13922. ((or (org-at-item-p)
  13923. (and (org-region-active-p)
  13924. (save-excursion
  13925. (goto-char (region-beginning))
  13926. (org-at-item-p))))
  13927. (call-interactively 'org-outdent-item))
  13928. (t (call-interactively 'backward-word))))
  13929. (defun org-metaright (&optional arg)
  13930. "Demote subtree or move table column to right.
  13931. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13932. With no specific context, calls the Emacs default `forward-word'.
  13933. See the individual commands for more information."
  13934. (interactive "P")
  13935. (cond
  13936. ((run-hook-with-args-until-success 'org-metaright-hook))
  13937. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13938. ((or (org-on-heading-p)
  13939. (and (org-region-active-p)
  13940. (save-excursion
  13941. (goto-char (region-beginning))
  13942. (org-on-heading-p))))
  13943. (call-interactively 'org-do-demote))
  13944. ((or (org-at-item-p)
  13945. (and (org-region-active-p)
  13946. (save-excursion
  13947. (goto-char (region-beginning))
  13948. (org-at-item-p))))
  13949. (call-interactively 'org-indent-item))
  13950. (t (call-interactively 'forward-word))))
  13951. (defun org-metaup (&optional arg)
  13952. "Move subtree up or move table row up.
  13953. Calls `org-move-subtree-up' or `org-table-move-row' or
  13954. `org-move-item-up', depending on context. See the individual commands
  13955. for more information."
  13956. (interactive "P")
  13957. (cond
  13958. ((run-hook-with-args-until-success 'org-metaup-hook))
  13959. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13960. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13961. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13962. (t (transpose-lines 1) (beginning-of-line -1))))
  13963. (defun org-metadown (&optional arg)
  13964. "Move subtree down or move table row down.
  13965. Calls `org-move-subtree-down' or `org-table-move-row' or
  13966. `org-move-item-down', depending on context. See the individual
  13967. commands for more information."
  13968. (interactive "P")
  13969. (cond
  13970. ((run-hook-with-args-until-success 'org-metadown-hook))
  13971. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13972. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13973. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13974. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13975. (defun org-shiftup (&optional arg)
  13976. "Increase item in timestamp or increase priority of current headline.
  13977. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13978. depending on context. See the individual commands for more information."
  13979. (interactive "P")
  13980. (cond
  13981. ((and org-support-shift-select (org-region-active-p))
  13982. (org-call-for-shift-select 'previous-line))
  13983. ((org-at-timestamp-p t)
  13984. (call-interactively (if org-edit-timestamp-down-means-later
  13985. 'org-timestamp-down 'org-timestamp-up)))
  13986. ((and (not (eq org-support-shift-select 'always))
  13987. org-enable-priority-commands
  13988. (org-on-heading-p))
  13989. (call-interactively 'org-priority-up))
  13990. ((and (not org-support-shift-select) (org-at-item-p))
  13991. (call-interactively 'org-previous-item))
  13992. ((org-clocktable-try-shift 'up arg))
  13993. (org-support-shift-select
  13994. (org-call-for-shift-select 'previous-line))
  13995. (t (org-shiftselect-error))))
  13996. (defun org-shiftdown (&optional arg)
  13997. "Decrease item in timestamp or decrease priority of current headline.
  13998. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13999. depending on context. See the individual commands for more information."
  14000. (interactive "P")
  14001. (cond
  14002. ((and org-support-shift-select (org-region-active-p))
  14003. (org-call-for-shift-select 'next-line))
  14004. ((org-at-timestamp-p t)
  14005. (call-interactively (if org-edit-timestamp-down-means-later
  14006. 'org-timestamp-up 'org-timestamp-down)))
  14007. ((and (not (eq org-support-shift-select 'always))
  14008. org-enable-priority-commands
  14009. (org-on-heading-p))
  14010. (call-interactively 'org-priority-down))
  14011. ((and (not org-support-shift-select) (org-at-item-p))
  14012. (call-interactively 'org-next-item))
  14013. ((org-clocktable-try-shift 'down arg))
  14014. (org-support-shift-select
  14015. (org-call-for-shift-select 'next-line))
  14016. (t (org-shiftselect-error))))
  14017. (defun org-shiftright (&optional arg)
  14018. "Cycle the thing at point or in the current line, depending on context.
  14019. Depending on context, this does one of the following:
  14020. - switch a timestamp at point one day into the future
  14021. - on a headline, switch to the next TODO keyword.
  14022. - on an item, switch entire list to the next bullet type
  14023. - on a property line, switch to the next allowed value
  14024. - on a clocktable definition line, move time block into the future"
  14025. (interactive "P")
  14026. (cond
  14027. ((and org-support-shift-select (org-region-active-p))
  14028. (org-call-for-shift-select 'forward-char))
  14029. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14030. ((and (not (eq org-support-shift-select 'always))
  14031. (org-on-heading-p))
  14032. (let ((org-inhibit-logging
  14033. (not org-treat-S-cursor-todo-selection-as-state-change))
  14034. (org-inhibit-blocking
  14035. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14036. (org-call-with-arg 'org-todo 'right)))
  14037. ((or (and org-support-shift-select
  14038. (not (eq org-support-shift-select 'always))
  14039. (org-at-item-bullet-p))
  14040. (and (not org-support-shift-select) (org-at-item-p)))
  14041. (org-call-with-arg 'org-cycle-list-bullet nil))
  14042. ((and (not (eq org-support-shift-select 'always))
  14043. (org-at-property-p))
  14044. (call-interactively 'org-property-next-allowed-value))
  14045. ((org-clocktable-try-shift 'right arg))
  14046. (org-support-shift-select
  14047. (org-call-for-shift-select 'forward-char))
  14048. (t (org-shiftselect-error))))
  14049. (defun org-shiftleft (&optional arg)
  14050. "Cycle the thing at point or in the current line, depending on context.
  14051. Depending on context, this does one of the following:
  14052. - switch a timestamp at point one day into the past
  14053. - on a headline, switch to the previous TODO keyword.
  14054. - on an item, switch entire list to the previous bullet type
  14055. - on a property line, switch to the previous allowed value
  14056. - on a clocktable definition line, move time block into the past"
  14057. (interactive "P")
  14058. (cond
  14059. ((and org-support-shift-select (org-region-active-p))
  14060. (org-call-for-shift-select 'backward-char))
  14061. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14062. ((and (not (eq org-support-shift-select 'always))
  14063. (org-on-heading-p))
  14064. (let ((org-inhibit-logging
  14065. (not org-treat-S-cursor-todo-selection-as-state-change))
  14066. (org-inhibit-blocking
  14067. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14068. (org-call-with-arg 'org-todo 'left)))
  14069. ((or (and org-support-shift-select
  14070. (not (eq org-support-shift-select 'always))
  14071. (org-at-item-bullet-p))
  14072. (and (not org-support-shift-select) (org-at-item-p)))
  14073. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14074. ((and (not (eq org-support-shift-select 'always))
  14075. (org-at-property-p))
  14076. (call-interactively 'org-property-previous-allowed-value))
  14077. ((org-clocktable-try-shift 'left arg))
  14078. (org-support-shift-select
  14079. (org-call-for-shift-select 'backward-char))
  14080. (t (org-shiftselect-error))))
  14081. (defun org-shiftcontrolright ()
  14082. "Switch to next TODO set."
  14083. (interactive)
  14084. (cond
  14085. ((and org-support-shift-select (org-region-active-p))
  14086. (org-call-for-shift-select 'forward-word))
  14087. ((and (not (eq org-support-shift-select 'always))
  14088. (org-on-heading-p))
  14089. (org-call-with-arg 'org-todo 'nextset))
  14090. (org-support-shift-select
  14091. (org-call-for-shift-select 'forward-word))
  14092. (t (org-shiftselect-error))))
  14093. (defun org-shiftcontrolleft ()
  14094. "Switch to previous TODO set."
  14095. (interactive)
  14096. (cond
  14097. ((and org-support-shift-select (org-region-active-p))
  14098. (org-call-for-shift-select 'backward-word))
  14099. ((and (not (eq org-support-shift-select 'always))
  14100. (org-on-heading-p))
  14101. (org-call-with-arg 'org-todo 'previousset))
  14102. (org-support-shift-select
  14103. (org-call-for-shift-select 'backward-word))
  14104. (t (org-shiftselect-error))))
  14105. (defun org-ctrl-c-ret ()
  14106. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14107. (interactive)
  14108. (cond
  14109. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14110. (t (call-interactively 'org-insert-heading))))
  14111. (defun org-copy-special ()
  14112. "Copy region in table or copy current subtree.
  14113. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14114. See the individual commands for more information."
  14115. (interactive)
  14116. (call-interactively
  14117. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14118. (defun org-cut-special ()
  14119. "Cut region in table or cut current subtree.
  14120. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14121. See the individual commands for more information."
  14122. (interactive)
  14123. (call-interactively
  14124. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14125. (defun org-paste-special (arg)
  14126. "Paste rectangular region into table, or past subtree relative to level.
  14127. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14128. See the individual commands for more information."
  14129. (interactive "P")
  14130. (if (org-at-table-p)
  14131. (org-table-paste-rectangle)
  14132. (org-paste-subtree arg)))
  14133. (defun org-edit-special ()
  14134. "Call a special editor for the stuff at point.
  14135. When at a table, call the formula editor with `org-table-edit-formulas'.
  14136. When at the first line of an src example, call `org-edit-src-code'.
  14137. When in an #+include line, visit the include file. Otherwise call
  14138. `ffap' to visit the file at point."
  14139. (interactive)
  14140. (cond
  14141. ((org-at-table-p)
  14142. (call-interactively 'org-table-edit-formulas))
  14143. ((save-excursion
  14144. (beginning-of-line 1)
  14145. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14146. (find-file (org-trim (match-string 1))))
  14147. ((org-edit-src-code))
  14148. ((org-edit-fixed-width-region))
  14149. (t (call-interactively 'ffap))))
  14150. (defun org-ctrl-c-ctrl-c (&optional arg)
  14151. "Set tags in headline, or update according to changed information at point.
  14152. This command does many different things, depending on context:
  14153. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14154. this is what we do.
  14155. - If the cursor is on a statistics cookie, update it.
  14156. - If the cursor is in a headline, prompt for tags and insert them
  14157. into the current line, aligned to `org-tags-column'. When called
  14158. with prefix arg, realign all tags in the current buffer.
  14159. - If the cursor is in one of the special #+KEYWORD lines, this
  14160. triggers scanning the buffer for these lines and updating the
  14161. information.
  14162. - If the cursor is inside a table, realign the table. This command
  14163. works even if the automatic table editor has been turned off.
  14164. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14165. the entire table.
  14166. - If the cursor is at a footnote reference or definition, jump to
  14167. the corresponding definition or references, respectively.
  14168. - If the cursor is a the beginning of a dynamic block, update it.
  14169. - If the cursor is inside a table created by the table.el package,
  14170. activate that table.
  14171. - If the current buffer is a remember buffer, close note and file
  14172. it. A prefix argument of 1 files to the default location
  14173. without further interaction. A prefix argument of 2 files to
  14174. the currently clocking task.
  14175. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14176. links in this buffer.
  14177. - If the cursor is on a numbered item in a plain list, renumber the
  14178. ordered list.
  14179. - If the cursor is on a checkbox, toggle it."
  14180. (interactive "P")
  14181. (let ((org-enable-table-editor t))
  14182. (cond
  14183. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14184. org-occur-highlights
  14185. org-latex-fragment-image-overlays)
  14186. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14187. (org-remove-occur-highlights)
  14188. (org-remove-latex-fragment-image-overlays)
  14189. (message "Temporary highlights/overlays removed from current buffer"))
  14190. ((and (local-variable-p 'org-finish-function (current-buffer))
  14191. (fboundp org-finish-function))
  14192. (funcall org-finish-function))
  14193. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14194. ((org-at-property-p)
  14195. (call-interactively 'org-property-action))
  14196. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14197. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14198. (or (org-on-heading-p) (org-at-item-p)))
  14199. (call-interactively 'org-update-statistics-cookies))
  14200. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14201. ((org-at-table.el-p)
  14202. (require 'table)
  14203. (beginning-of-line 1)
  14204. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14205. (call-interactively 'table-recognize-table))
  14206. ((org-at-table-p)
  14207. (org-table-maybe-eval-formula)
  14208. (if arg
  14209. (call-interactively 'org-table-recalculate)
  14210. (org-table-maybe-recalculate-line))
  14211. (call-interactively 'org-table-align))
  14212. ((or (org-footnote-at-reference-p)
  14213. (org-footnote-at-definition-p))
  14214. (call-interactively 'org-footnote-action))
  14215. ((org-at-item-checkbox-p)
  14216. (call-interactively 'org-toggle-checkbox))
  14217. ((org-at-item-p)
  14218. (if arg
  14219. (call-interactively 'org-toggle-checkbox)
  14220. (call-interactively 'org-maybe-renumber-ordered-list)))
  14221. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14222. ;; Dynamic block
  14223. (beginning-of-line 1)
  14224. (save-excursion (org-update-dblock)))
  14225. ((save-excursion
  14226. (beginning-of-line 1)
  14227. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14228. (cond
  14229. ((equal (match-string 1) "TBLFM")
  14230. ;; Recalculate the table before this line
  14231. (save-excursion
  14232. (beginning-of-line 1)
  14233. (skip-chars-backward " \r\n\t")
  14234. (if (org-at-table-p)
  14235. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14236. (t
  14237. ; (org-set-regexps-and-options)
  14238. ; (org-restart-font-lock)
  14239. (let ((org-inhibit-startup t)) (org-mode-restart))
  14240. (message "Local setup has been refreshed"))))
  14241. ((org-clock-update-time-maybe))
  14242. (t (error "C-c C-c can do nothing useful at this location")))))
  14243. (defun org-mode-restart ()
  14244. "Restart Org-mode, to scan again for special lines.
  14245. Also updates the keyword regular expressions."
  14246. (interactive)
  14247. (org-mode)
  14248. (message "Org-mode restarted"))
  14249. (defun org-kill-note-or-show-branches ()
  14250. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14251. (interactive)
  14252. (if (not org-finish-function)
  14253. (call-interactively 'show-branches)
  14254. (let ((org-note-abort t))
  14255. (funcall org-finish-function))))
  14256. (defun org-return (&optional indent)
  14257. "Goto next table row or insert a newline.
  14258. Calls `org-table-next-row' or `newline', depending on context.
  14259. See the individual commands for more information."
  14260. (interactive)
  14261. (cond
  14262. ((bobp) (if indent (newline-and-indent) (newline)))
  14263. ((org-at-table-p)
  14264. (org-table-justify-field-maybe)
  14265. (call-interactively 'org-table-next-row))
  14266. ((and org-return-follows-link
  14267. (eq (get-text-property (point) 'face) 'org-link))
  14268. (call-interactively 'org-open-at-point))
  14269. ((and (org-at-heading-p)
  14270. (looking-at
  14271. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14272. (org-show-entry)
  14273. (end-of-line 1)
  14274. (newline))
  14275. (t (if indent (newline-and-indent) (newline)))))
  14276. (defun org-return-indent ()
  14277. "Goto next table row or insert a newline and indent.
  14278. Calls `org-table-next-row' or `newline-and-indent', depending on
  14279. context. See the individual commands for more information."
  14280. (interactive)
  14281. (org-return t))
  14282. (defun org-ctrl-c-star ()
  14283. "Compute table, or change heading status of lines.
  14284. Calls `org-table-recalculate' or `org-toggle-heading',
  14285. depending on context."
  14286. (interactive)
  14287. (cond
  14288. ((org-at-table-p)
  14289. (call-interactively 'org-table-recalculate))
  14290. (t
  14291. ;; Convert all lines in region to list items
  14292. (call-interactively 'org-toggle-heading))))
  14293. (defun org-ctrl-c-minus ()
  14294. "Insert separator line in table or modify bullet status of line.
  14295. Also turns a plain line or a region of lines into list items.
  14296. Calls `org-table-insert-hline', `org-toggle-item', or
  14297. `org-cycle-list-bullet', depending on context."
  14298. (interactive)
  14299. (cond
  14300. ((org-at-table-p)
  14301. (call-interactively 'org-table-insert-hline))
  14302. ((org-region-active-p)
  14303. (call-interactively 'org-toggle-item))
  14304. ((org-in-item-p)
  14305. (call-interactively 'org-cycle-list-bullet))
  14306. (t
  14307. (call-interactively 'org-toggle-item))))
  14308. (defun org-toggle-item ()
  14309. "Convert headings or normal lines to items, items to normal lines.
  14310. If there is no active region, only the current line is considered.
  14311. If the first line in the region is a headline, convert all headlines to items.
  14312. If the first line in the region is an item, convert all items to normal lines.
  14313. If the first line is normal text, add an item bullet to each line."
  14314. (interactive)
  14315. (let (l2 l beg end)
  14316. (if (org-region-active-p)
  14317. (setq beg (region-beginning) end (region-end))
  14318. (setq beg (point-at-bol)
  14319. end (min (1+ (point-at-eol)) (point-max))))
  14320. (save-excursion
  14321. (goto-char end)
  14322. (setq l2 (org-current-line))
  14323. (goto-char beg)
  14324. (beginning-of-line 1)
  14325. (setq l (1- (org-current-line)))
  14326. (if (org-at-item-p)
  14327. ;; We already have items, de-itemize
  14328. (while (< (setq l (1+ l)) l2)
  14329. (when (org-at-item-p)
  14330. (goto-char (match-beginning 2))
  14331. (delete-region (match-beginning 2) (match-end 2))
  14332. (and (looking-at "[ \t]+") (replace-match "")))
  14333. (beginning-of-line 2))
  14334. (if (org-on-heading-p)
  14335. ;; Headings, convert to items
  14336. (while (< (setq l (1+ l)) l2)
  14337. (if (looking-at org-outline-regexp)
  14338. (replace-match "- " t t))
  14339. (beginning-of-line 2))
  14340. ;; normal lines, turn them into items
  14341. (while (< (setq l (1+ l)) l2)
  14342. (unless (org-at-item-p)
  14343. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14344. (replace-match "\\1- \\2")))
  14345. (beginning-of-line 2)))))))
  14346. (defun org-toggle-heading (&optional nstars)
  14347. "Convert headings to normal text, or items or text to headings.
  14348. If there is no active region, only the current line is considered.
  14349. If the first line is a heading, remove the stars from all headlines
  14350. in the region.
  14351. If the first line is a plain list item, turn all plain list items
  14352. into headings.
  14353. If the first line is a normal line, turn each and every line in the
  14354. region into a heading.
  14355. When converting a line into a heading, the number of stars is chosen
  14356. such that the lines become children of the current entry. However,
  14357. when a prefix argument is given, its value determines the number of
  14358. stars to add."
  14359. (interactive "P")
  14360. (let (l2 l itemp beg end)
  14361. (if (org-region-active-p)
  14362. (setq beg (region-beginning) end (region-end))
  14363. (setq beg (point-at-bol)
  14364. end (min (1+ (point-at-eol)) (point-max))))
  14365. (save-excursion
  14366. (goto-char end)
  14367. (setq l2 (org-current-line))
  14368. (goto-char beg)
  14369. (beginning-of-line 1)
  14370. (setq l (1- (org-current-line)))
  14371. (if (org-on-heading-p)
  14372. ;; We already have headlines, de-star them
  14373. (while (< (setq l (1+ l)) l2)
  14374. (when (org-on-heading-p t)
  14375. (and (looking-at outline-regexp) (replace-match "")))
  14376. (beginning-of-line 2))
  14377. (setq itemp (org-at-item-p))
  14378. (let* ((stars
  14379. (if nstars
  14380. (make-string (prefix-numeric-value current-prefix-arg)
  14381. ?*)
  14382. (save-excursion
  14383. (if (re-search-backward org-complex-heading-regexp nil t)
  14384. (match-string 1) ""))))
  14385. (add-stars (cond (nstars "")
  14386. ((equal stars "") "*")
  14387. (org-odd-levels-only "**")
  14388. (t "*")))
  14389. (rpl (concat stars add-stars " ")))
  14390. (while (< (setq l (1+ l)) l2)
  14391. (if itemp
  14392. (and (org-at-item-p) (replace-match rpl t t))
  14393. (unless (org-on-heading-p)
  14394. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14395. (replace-match (concat rpl (match-string 2))))))
  14396. (beginning-of-line 2)))))))
  14397. (defun org-meta-return (&optional arg)
  14398. "Insert a new heading or wrap a region in a table.
  14399. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14400. See the individual commands for more information."
  14401. (interactive "P")
  14402. (cond
  14403. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14404. ((org-at-table-p)
  14405. (call-interactively 'org-table-wrap-region))
  14406. (t (call-interactively 'org-insert-heading))))
  14407. ;;; Menu entries
  14408. ;; Define the Org-mode menus
  14409. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14410. '("Tbl"
  14411. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14412. ["Next Field" org-cycle (org-at-table-p)]
  14413. ["Previous Field" org-shifttab (org-at-table-p)]
  14414. ["Next Row" org-return (org-at-table-p)]
  14415. "--"
  14416. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14417. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14418. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14419. "--"
  14420. ("Column"
  14421. ["Move Column Left" org-metaleft (org-at-table-p)]
  14422. ["Move Column Right" org-metaright (org-at-table-p)]
  14423. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14424. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14425. ("Row"
  14426. ["Move Row Up" org-metaup (org-at-table-p)]
  14427. ["Move Row Down" org-metadown (org-at-table-p)]
  14428. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14429. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14430. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14431. "--"
  14432. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14433. ("Rectangle"
  14434. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14435. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14436. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14437. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14438. "--"
  14439. ("Calculate"
  14440. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14441. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14442. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14443. "--"
  14444. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14445. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14446. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14447. "--"
  14448. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14449. "--"
  14450. ["Sum Column/Rectangle" org-table-sum
  14451. (or (org-at-table-p) (org-region-active-p))]
  14452. ["Which Column?" org-table-current-column (org-at-table-p)])
  14453. ["Debug Formulas"
  14454. org-table-toggle-formula-debugger
  14455. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14456. ["Show Col/Row Numbers"
  14457. org-table-toggle-coordinate-overlays
  14458. :style toggle
  14459. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14460. "--"
  14461. ["Create" org-table-create (and (not (org-at-table-p))
  14462. org-enable-table-editor)]
  14463. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14464. ["Import from File" org-table-import (not (org-at-table-p))]
  14465. ["Export to File" org-table-export (org-at-table-p)]
  14466. "--"
  14467. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14468. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14469. '("Org"
  14470. ("Show/Hide"
  14471. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14472. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14473. ["Sparse Tree..." org-sparse-tree t]
  14474. ["Reveal Context" org-reveal t]
  14475. ["Show All" show-all t]
  14476. "--"
  14477. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14478. "--"
  14479. ["New Heading" org-insert-heading t]
  14480. ("Navigate Headings"
  14481. ["Up" outline-up-heading t]
  14482. ["Next" outline-next-visible-heading t]
  14483. ["Previous" outline-previous-visible-heading t]
  14484. ["Next Same Level" outline-forward-same-level t]
  14485. ["Previous Same Level" outline-backward-same-level t]
  14486. "--"
  14487. ["Jump" org-goto t])
  14488. ("Edit Structure"
  14489. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14490. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14491. "--"
  14492. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14493. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14494. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14495. "--"
  14496. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14497. "--"
  14498. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14499. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14500. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14501. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14502. "--"
  14503. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14504. "--"
  14505. ["Convert to odd levels" org-convert-to-odd-levels t]
  14506. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14507. ("Editing"
  14508. ["Emphasis..." org-emphasize t]
  14509. ["Edit Source Example" org-edit-special t]
  14510. "--"
  14511. ["Footnote new/jump" org-footnote-action t]
  14512. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14513. ("Archive"
  14514. ["Archive (default method)" org-archive-subtree-default t]
  14515. "--"
  14516. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14517. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14518. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14519. )
  14520. "--"
  14521. ("Hyperlinks"
  14522. ["Store Link (Global)" org-store-link t]
  14523. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14524. ["Insert Link" org-insert-link t]
  14525. ["Follow Link" org-open-at-point t]
  14526. "--"
  14527. ["Next link" org-next-link t]
  14528. ["Previous link" org-previous-link t]
  14529. "--"
  14530. ["Descriptive Links"
  14531. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14532. :style radio
  14533. :selected (member '(org-link) buffer-invisibility-spec)]
  14534. ["Literal Links"
  14535. (progn
  14536. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14537. :style radio
  14538. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14539. "--"
  14540. ("TODO Lists"
  14541. ["TODO/DONE/-" org-todo t]
  14542. ("Select keyword"
  14543. ["Next keyword" org-shiftright (org-on-heading-p)]
  14544. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14545. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14546. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14547. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14548. ["Show TODO Tree" org-show-todo-tree t]
  14549. ["Global TODO list" org-todo-list t]
  14550. "--"
  14551. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14552. :selected org-enforce-todo-dependencies :style toggle :active t]
  14553. "Settings for tree at point"
  14554. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14555. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14556. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14557. ["Do Children parallel" org-toggle-ordered-property :style radio
  14558. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14559. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14560. "--"
  14561. ["Set Priority" org-priority t]
  14562. ["Priority Up" org-shiftup t]
  14563. ["Priority Down" org-shiftdown t]
  14564. "--"
  14565. ["Get news from all feeds" org-feed-update-all t]
  14566. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14567. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14568. ("TAGS and Properties"
  14569. ["Set Tags" org-set-tags-command t]
  14570. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14571. "--"
  14572. ["Set property" org-set-property t]
  14573. ["Column view of properties" org-columns t]
  14574. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14575. ("Dates and Scheduling"
  14576. ["Timestamp" org-time-stamp t]
  14577. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14578. ("Change Date"
  14579. ["1 Day Later" org-shiftright t]
  14580. ["1 Day Earlier" org-shiftleft t]
  14581. ["1 ... Later" org-shiftup t]
  14582. ["1 ... Earlier" org-shiftdown t])
  14583. ["Compute Time Range" org-evaluate-time-range t]
  14584. ["Schedule Item" org-schedule t]
  14585. ["Deadline" org-deadline t]
  14586. "--"
  14587. ["Custom time format" org-toggle-time-stamp-overlays
  14588. :style radio :selected org-display-custom-times]
  14589. "--"
  14590. ["Goto Calendar" org-goto-calendar t]
  14591. ["Date from Calendar" org-date-from-calendar t]
  14592. "--"
  14593. ["Start/Restart Timer" org-timer-start t]
  14594. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14595. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14596. ["Insert Timer String" org-timer t]
  14597. ["Insert Timer Item" org-timer-item t])
  14598. ("Logging work"
  14599. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14600. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14601. ["Clock out" org-clock-out t]
  14602. ["Clock cancel" org-clock-cancel t]
  14603. "--"
  14604. ["Mark as default task" org-clock-mark-default-task t]
  14605. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14606. ["Goto running clock" org-clock-goto t]
  14607. "--"
  14608. ["Display times" org-clock-display t]
  14609. ["Create clock table" org-clock-report t]
  14610. "--"
  14611. ["Record DONE time"
  14612. (progn (setq org-log-done (not org-log-done))
  14613. (message "Switching to %s will %s record a timestamp"
  14614. (car org-done-keywords)
  14615. (if org-log-done "automatically" "not")))
  14616. :style toggle :selected org-log-done])
  14617. "--"
  14618. ["Agenda Command..." org-agenda t]
  14619. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14620. ("File List for Agenda")
  14621. ("Special views current file"
  14622. ["TODO Tree" org-show-todo-tree t]
  14623. ["Check Deadlines" org-check-deadlines t]
  14624. ["Timeline" org-timeline t]
  14625. ["Tags/Property tree" org-match-sparse-tree t])
  14626. "--"
  14627. ["Export/Publish..." org-export t]
  14628. ("LaTeX"
  14629. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14630. :selected org-cdlatex-mode]
  14631. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14632. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14633. ["Modify math symbol" org-cdlatex-math-modify
  14634. (org-inside-LaTeX-fragment-p)]
  14635. ["Insert citation" org-reftex-citation t]
  14636. "--"
  14637. ["Export LaTeX fragments as images"
  14638. (if (featurep 'org-exp)
  14639. (setq org-export-with-LaTeX-fragments
  14640. (not org-export-with-LaTeX-fragments))
  14641. (require 'org-exp))
  14642. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14643. org-export-with-LaTeX-fragments)])
  14644. "--"
  14645. ("MobileOrg"
  14646. ["Push Files and Views" org-mobile-push t]
  14647. ["Get Captured and Flagged" org-mobile-pull t]
  14648. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14649. "--"
  14650. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14651. "--"
  14652. ("Documentation"
  14653. ["Show Version" org-version t]
  14654. ["Info Documentation" org-info t])
  14655. ("Customize"
  14656. ["Browse Org Group" org-customize t]
  14657. "--"
  14658. ["Expand This Menu" org-create-customize-menu
  14659. (fboundp 'customize-menu-create)])
  14660. ["Send bug report" org-submit-bug-report t]
  14661. "--"
  14662. ("Refresh/Reload"
  14663. ["Refresh setup current buffer" org-mode-restart t]
  14664. ["Reload Org (after update)" org-reload t]
  14665. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14666. ))
  14667. (defun org-info (&optional node)
  14668. "Read documentation for Org-mode in the info system.
  14669. With optional NODE, go directly to that node."
  14670. (interactive)
  14671. (info (format "(org)%s" (or node ""))))
  14672. ;;;###autoload
  14673. (defun org-submit-bug-report ()
  14674. "Submit a bug report on Org-mode via mail.
  14675. Don't hesitate to report any problems or inaccurate documentation.
  14676. If you don't have setup sending mail from (X)Emacs, please copy the
  14677. output buffer into your mail program, as it gives us important
  14678. information about your Org-mode version and configuration."
  14679. (interactive)
  14680. (require 'reporter)
  14681. (org-load-modules-maybe)
  14682. (org-require-autoloaded-modules)
  14683. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14684. (reporter-submit-bug-report
  14685. "emacs-orgmode@gnu.org"
  14686. (org-version)
  14687. (let (list)
  14688. (save-window-excursion
  14689. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14690. (delete-other-windows)
  14691. (erase-buffer)
  14692. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14693. We would like to add your full Org-mode and Outline configuration to the
  14694. bug report. This greatly simplifies the work of the maintainer and
  14695. other experts on the mailing list.
  14696. HOWEVER, some variables you have customized may contain private
  14697. information. The names of customers, colleagues, or friends, might
  14698. appear in the form of file names, tags, todo states, or search strings.
  14699. If you answer yes to the prompt, you might want to check and remove
  14700. such private information before sending the email.")
  14701. (add-text-properties (point-min) (point-max) '(face org-warning))
  14702. (when (yes-or-no-p "Include your Org-mode configuration ")
  14703. (mapatoms
  14704. (lambda (v)
  14705. (and (boundp v)
  14706. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14707. (or (and (symbol-value v)
  14708. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14709. (and
  14710. (get v 'custom-type) (get v 'standard-value)
  14711. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14712. (push v list)))))
  14713. (kill-buffer (get-buffer "*Warn about privacy*"))
  14714. list))
  14715. nil nil
  14716. "Remember to cover the basics, that is, what you expected to happen and
  14717. what in fact did happen. You don't know how to make a good report? See
  14718. http://orgmode.org/manual/Feedback.html#Feedback
  14719. Your bug report will be posted to the Org-mode mailing list.
  14720. ------------------------------------------------------------------------")))
  14721. (defun org-install-agenda-files-menu ()
  14722. (let ((bl (buffer-list)))
  14723. (save-excursion
  14724. (while bl
  14725. (set-buffer (pop bl))
  14726. (if (org-mode-p) (setq bl nil)))
  14727. (when (org-mode-p)
  14728. (easy-menu-change
  14729. '("Org") "File List for Agenda"
  14730. (append
  14731. (list
  14732. ["Edit File List" (org-edit-agenda-file-list) t]
  14733. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14734. ["Remove Current File from List" org-remove-file t]
  14735. ["Cycle through agenda files" org-cycle-agenda-files t]
  14736. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14737. "--")
  14738. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14739. ;;;; Documentation
  14740. ;;;###autoload
  14741. (defun org-require-autoloaded-modules ()
  14742. (interactive)
  14743. (mapc 'require
  14744. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14745. org-docbook org-exp org-html org-icalendar
  14746. org-id org-latex
  14747. org-publish org-remember org-table
  14748. org-timer org-xoxo)))
  14749. ;;;###autoload
  14750. (defun org-reload (&optional uncompiled)
  14751. "Reload all org lisp files.
  14752. With prefix arg UNCOMPILED, load the uncompiled versions."
  14753. (interactive "P")
  14754. (require 'find-func)
  14755. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14756. (dir-org (file-name-directory (org-find-library-name "org")))
  14757. (dir-org-contrib (ignore-errors
  14758. (file-name-directory
  14759. (org-find-library-name "org-contribdir"))))
  14760. (files
  14761. (append (directory-files dir-org t file-re)
  14762. (and dir-org-contrib
  14763. (directory-files dir-org-contrib t file-re))))
  14764. (remove-re (concat (if (featurep 'xemacs)
  14765. "org-colview" "org-colview-xemacs")
  14766. "\\'")))
  14767. (setq files (mapcar 'file-name-sans-extension files))
  14768. (setq files (mapcar
  14769. (lambda (x) (if (string-match remove-re x) nil x))
  14770. files))
  14771. (setq files (delq nil files))
  14772. (mapc
  14773. (lambda (f)
  14774. (when (featurep (intern (file-name-nondirectory f)))
  14775. (if (and (not uncompiled)
  14776. (file-exists-p (concat f ".elc")))
  14777. (load (concat f ".elc") nil nil t)
  14778. (load (concat f ".el") nil nil t))))
  14779. files))
  14780. (org-version))
  14781. ;;;###autoload
  14782. (defun org-customize ()
  14783. "Call the customize function with org as argument."
  14784. (interactive)
  14785. (org-load-modules-maybe)
  14786. (org-require-autoloaded-modules)
  14787. (customize-browse 'org))
  14788. (defun org-create-customize-menu ()
  14789. "Create a full customization menu for Org-mode, insert it into the menu."
  14790. (interactive)
  14791. (org-load-modules-maybe)
  14792. (org-require-autoloaded-modules)
  14793. (if (fboundp 'customize-menu-create)
  14794. (progn
  14795. (easy-menu-change
  14796. '("Org") "Customize"
  14797. `(["Browse Org group" org-customize t]
  14798. "--"
  14799. ,(customize-menu-create 'org)
  14800. ["Set" Custom-set t]
  14801. ["Save" Custom-save t]
  14802. ["Reset to Current" Custom-reset-current t]
  14803. ["Reset to Saved" Custom-reset-saved t]
  14804. ["Reset to Standard Settings" Custom-reset-standard t]))
  14805. (message "\"Org\"-menu now contains full customization menu"))
  14806. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14807. ;;;; Miscellaneous stuff
  14808. ;;; Generally useful functions
  14809. (defun org-get-at-bol (property)
  14810. "Get text property PROPERTY at beginning of line."
  14811. (get-text-property (point-at-bol) property))
  14812. (defun org-find-text-property-in-string (prop s)
  14813. "Return the first non-nil value of property PROP in string S."
  14814. (or (get-text-property 0 prop s)
  14815. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14816. prop s)))
  14817. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14818. "Display the given MESSAGE as a warning."
  14819. (if (fboundp 'display-warning)
  14820. (display-warning 'org message
  14821. (if (featurep 'xemacs)
  14822. 'warning
  14823. :warning))
  14824. (let ((buf (get-buffer-create "*Org warnings*")))
  14825. (with-current-buffer buf
  14826. (goto-char (point-max))
  14827. (insert "Warning (Org): " message)
  14828. (unless (bolp)
  14829. (newline)))
  14830. (display-buffer buf)
  14831. (sit-for 0))))
  14832. (defun org-in-commented-line ()
  14833. "Is point in a line starting with `#'?"
  14834. (equal (char-after (point-at-bol)) ?#))
  14835. (defun org-in-verbatim-emphasis ()
  14836. (save-match-data
  14837. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14838. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14839. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14840. (if (and marker (marker-buffer marker)
  14841. (buffer-live-p (marker-buffer marker)))
  14842. (progn
  14843. (switch-to-buffer (marker-buffer marker))
  14844. (if (or (> marker (point-max)) (< marker (point-min)))
  14845. (widen))
  14846. (goto-char marker)
  14847. (org-show-context 'org-goto))
  14848. (if bookmark
  14849. (bookmark-jump bookmark)
  14850. (error "Cannot find location"))))
  14851. (defun org-quote-csv-field (s)
  14852. "Quote field for inclusion in CSV material."
  14853. (if (string-match "[\",]" s)
  14854. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14855. s))
  14856. (defun org-plist-delete (plist property)
  14857. "Delete PROPERTY from PLIST.
  14858. This is in contrast to merely setting it to 0."
  14859. (let (p)
  14860. (while plist
  14861. (if (not (eq property (car plist)))
  14862. (setq p (plist-put p (car plist) (nth 1 plist))))
  14863. (setq plist (cddr plist)))
  14864. p))
  14865. (defun org-force-self-insert (N)
  14866. "Needed to enforce self-insert under remapping."
  14867. (interactive "p")
  14868. (self-insert-command N))
  14869. (defun org-string-width (s)
  14870. "Compute width of string, ignoring invisible characters.
  14871. This ignores character with invisibility property `org-link', and also
  14872. characters with property `org-cwidth', because these will become invisible
  14873. upon the next fontification round."
  14874. (let (b l)
  14875. (when (or (eq t buffer-invisibility-spec)
  14876. (assq 'org-link buffer-invisibility-spec))
  14877. (while (setq b (text-property-any 0 (length s)
  14878. 'invisible 'org-link s))
  14879. (setq s (concat (substring s 0 b)
  14880. (substring s (or (next-single-property-change
  14881. b 'invisible s) (length s)))))))
  14882. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14883. (setq s (concat (substring s 0 b)
  14884. (substring s (or (next-single-property-change
  14885. b 'org-cwidth s) (length s))))))
  14886. (setq l (string-width s) b -1)
  14887. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14888. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14889. l))
  14890. (defun org-get-indentation (&optional line)
  14891. "Get the indentation of the current line, interpreting tabs.
  14892. When LINE is given, assume it represents a line and compute its indentation."
  14893. (if line
  14894. (if (string-match "^ *" (org-remove-tabs line))
  14895. (match-end 0))
  14896. (save-excursion
  14897. (beginning-of-line 1)
  14898. (skip-chars-forward " \t")
  14899. (current-column))))
  14900. (defun org-remove-tabs (s &optional width)
  14901. "Replace tabulators in S with spaces.
  14902. Assumes that s is a single line, starting in column 0."
  14903. (setq width (or width tab-width))
  14904. (while (string-match "\t" s)
  14905. (setq s (replace-match
  14906. (make-string
  14907. (- (* width (/ (+ (match-beginning 0) width) width))
  14908. (match-beginning 0)) ?\ )
  14909. t t s)))
  14910. s)
  14911. (defun org-fix-indentation (line ind)
  14912. "Fix indentation in LINE.
  14913. IND is a cons cell with target and minimum indentation.
  14914. If the current indentation in LINE is smaller than the minimum,
  14915. leave it alone. If it is larger than ind, set it to the target."
  14916. (let* ((l (org-remove-tabs line))
  14917. (i (org-get-indentation l))
  14918. (i1 (car ind)) (i2 (cdr ind)))
  14919. (if (>= i i2) (setq l (substring line i2)))
  14920. (if (> i1 0)
  14921. (concat (make-string i1 ?\ ) l)
  14922. l)))
  14923. (defun org-remove-indentation (code &optional n)
  14924. "Remove the maximum common indentation from the lines in CODE.
  14925. N may optionally be the number of spaces to remove."
  14926. (with-temp-buffer
  14927. (insert code)
  14928. (org-do-remove-indentation n)
  14929. (buffer-string)))
  14930. (defun org-do-remove-indentation (&optional n)
  14931. "Remove the maximum common indentation from the buffer."
  14932. (untabify (point-min) (point-max))
  14933. (let ((min 10000) re)
  14934. (if n
  14935. (setq min n)
  14936. (goto-char (point-min))
  14937. (while (re-search-forward "^ *[^ \n]" nil t)
  14938. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14939. (unless (or (= min 0) (= min 10000))
  14940. (setq re (format "^ \\{%d\\}" min))
  14941. (goto-char (point-min))
  14942. (while (re-search-forward re nil t)
  14943. (replace-match "")
  14944. (end-of-line 1))
  14945. min)))
  14946. (defun org-fill-template (template alist)
  14947. "Find each %key of ALIST in TEMPLATE and replace it."
  14948. (let (entry key value)
  14949. (setq alist (sort (copy-sequence alist)
  14950. (lambda (a b) (< (length (car a)) (length (car b))))))
  14951. (while (setq entry (pop alist))
  14952. (setq template
  14953. (replace-regexp-in-string
  14954. (concat "%" (regexp-quote (car entry)))
  14955. (cdr entry) template t t)))
  14956. template))
  14957. (defun org-base-buffer (buffer)
  14958. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14959. (if (not buffer)
  14960. buffer
  14961. (or (buffer-base-buffer buffer)
  14962. buffer)))
  14963. (defun org-trim (s)
  14964. "Remove whitespace at beginning and end of string."
  14965. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14966. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14967. s)
  14968. (defun org-wrap (string &optional width lines)
  14969. "Wrap string to either a number of lines, or a width in characters.
  14970. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14971. that costs. If there is a word longer than WIDTH, the text is actually
  14972. wrapped to the length of that word.
  14973. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14974. many lines, whatever width that takes.
  14975. The return value is a list of lines, without newlines at the end."
  14976. (let* ((words (org-split-string string "[ \t\n]+"))
  14977. (maxword (apply 'max (mapcar 'org-string-width words)))
  14978. w ll)
  14979. (cond (width
  14980. (org-do-wrap words (max maxword width)))
  14981. (lines
  14982. (setq w maxword)
  14983. (setq ll (org-do-wrap words maxword))
  14984. (if (<= (length ll) lines)
  14985. ll
  14986. (setq ll words)
  14987. (while (> (length ll) lines)
  14988. (setq w (1+ w))
  14989. (setq ll (org-do-wrap words w)))
  14990. ll))
  14991. (t (error "Cannot wrap this")))))
  14992. (defun org-do-wrap (words width)
  14993. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14994. (let (lines line)
  14995. (while words
  14996. (setq line (pop words))
  14997. (while (and words (< (+ (length line) (length (car words))) width))
  14998. (setq line (concat line " " (pop words))))
  14999. (setq lines (push line lines)))
  15000. (nreverse lines)))
  15001. (defun org-split-string (string &optional separators)
  15002. "Splits STRING into substrings at SEPARATORS.
  15003. No empty strings are returned if there are matches at the beginning
  15004. and end of string."
  15005. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15006. (start 0)
  15007. notfirst
  15008. (list nil))
  15009. (while (and (string-match rexp string
  15010. (if (and notfirst
  15011. (= start (match-beginning 0))
  15012. (< start (length string)))
  15013. (1+ start) start))
  15014. (< (match-beginning 0) (length string)))
  15015. (setq notfirst t)
  15016. (or (eq (match-beginning 0) 0)
  15017. (and (eq (match-beginning 0) (match-end 0))
  15018. (eq (match-beginning 0) start))
  15019. (setq list
  15020. (cons (substring string start (match-beginning 0))
  15021. list)))
  15022. (setq start (match-end 0)))
  15023. (or (eq start (length string))
  15024. (setq list
  15025. (cons (substring string start)
  15026. list)))
  15027. (nreverse list)))
  15028. (defun org-quote-vert (s)
  15029. "Replace \"|\" with \"\\vert\"."
  15030. (while (string-match "|" s)
  15031. (setq s (replace-match "\\vert" t t s)))
  15032. s)
  15033. (defun org-uuidgen-p (s)
  15034. "Is S an ID created by UUIDGEN?"
  15035. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15036. (defun org-context ()
  15037. "Return a list of contexts of the current cursor position.
  15038. If several contexts apply, all are returned.
  15039. Each context entry is a list with a symbol naming the context, and
  15040. two positions indicating start and end of the context. Possible
  15041. contexts are:
  15042. :headline anywhere in a headline
  15043. :headline-stars on the leading stars in a headline
  15044. :todo-keyword on a TODO keyword (including DONE) in a headline
  15045. :tags on the TAGS in a headline
  15046. :priority on the priority cookie in a headline
  15047. :item on the first line of a plain list item
  15048. :item-bullet on the bullet/number of a plain list item
  15049. :checkbox on the checkbox in a plain list item
  15050. :table in an org-mode table
  15051. :table-special on a special filed in a table
  15052. :table-table in a table.el table
  15053. :link on a hyperlink
  15054. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15055. :target on a <<target>>
  15056. :radio-target on a <<<radio-target>>>
  15057. :latex-fragment on a LaTeX fragment
  15058. :latex-preview on a LaTeX fragment with overlayed preview image
  15059. This function expects the position to be visible because it uses font-lock
  15060. faces as a help to recognize the following contexts: :table-special, :link,
  15061. and :keyword."
  15062. (let* ((f (get-text-property (point) 'face))
  15063. (faces (if (listp f) f (list f)))
  15064. (p (point)) clist o)
  15065. ;; First the large context
  15066. (cond
  15067. ((org-on-heading-p t)
  15068. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15069. (when (progn
  15070. (beginning-of-line 1)
  15071. (looking-at org-todo-line-tags-regexp))
  15072. (push (org-point-in-group p 1 :headline-stars) clist)
  15073. (push (org-point-in-group p 2 :todo-keyword) clist)
  15074. (push (org-point-in-group p 4 :tags) clist))
  15075. (goto-char p)
  15076. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15077. (if (looking-at "\\[#[A-Z0-9]\\]")
  15078. (push (org-point-in-group p 0 :priority) clist)))
  15079. ((org-at-item-p)
  15080. (push (org-point-in-group p 2 :item-bullet) clist)
  15081. (push (list :item (point-at-bol)
  15082. (save-excursion (org-end-of-item) (point)))
  15083. clist)
  15084. (and (org-at-item-checkbox-p)
  15085. (push (org-point-in-group p 0 :checkbox) clist)))
  15086. ((org-at-table-p)
  15087. (push (list :table (org-table-begin) (org-table-end)) clist)
  15088. (if (memq 'org-formula faces)
  15089. (push (list :table-special
  15090. (previous-single-property-change p 'face)
  15091. (next-single-property-change p 'face)) clist)))
  15092. ((org-at-table-p 'any)
  15093. (push (list :table-table) clist)))
  15094. (goto-char p)
  15095. ;; Now the small context
  15096. (cond
  15097. ((org-at-timestamp-p)
  15098. (push (org-point-in-group p 0 :timestamp) clist))
  15099. ((memq 'org-link faces)
  15100. (push (list :link
  15101. (previous-single-property-change p 'face)
  15102. (next-single-property-change p 'face)) clist))
  15103. ((memq 'org-special-keyword faces)
  15104. (push (list :keyword
  15105. (previous-single-property-change p 'face)
  15106. (next-single-property-change p 'face)) clist))
  15107. ((org-on-target-p)
  15108. (push (org-point-in-group p 0 :target) clist)
  15109. (goto-char (1- (match-beginning 0)))
  15110. (if (looking-at org-radio-target-regexp)
  15111. (push (org-point-in-group p 0 :radio-target) clist))
  15112. (goto-char p))
  15113. ((setq o (car (delq nil
  15114. (mapcar
  15115. (lambda (x)
  15116. (if (memq x org-latex-fragment-image-overlays) x))
  15117. (org-overlays-at (point))))))
  15118. (push (list :latex-fragment
  15119. (org-overlay-start o) (org-overlay-end o)) clist)
  15120. (push (list :latex-preview
  15121. (org-overlay-start o) (org-overlay-end o)) clist))
  15122. ((org-inside-LaTeX-fragment-p)
  15123. ;; FIXME: positions wrong.
  15124. (push (list :latex-fragment (point) (point)) clist)))
  15125. (setq clist (nreverse (delq nil clist)))
  15126. clist))
  15127. ;; FIXME: Compare with at-regexp-p Do we need both?
  15128. (defun org-in-regexp (re &optional nlines visually)
  15129. "Check if point is inside a match of regexp.
  15130. Normally only the current line is checked, but you can include NLINES extra
  15131. lines both before and after point into the search.
  15132. If VISUALLY is set, require that the cursor is not after the match but
  15133. really on, so that the block visually is on the match."
  15134. (catch 'exit
  15135. (let ((pos (point))
  15136. (eol (point-at-eol (+ 1 (or nlines 0))))
  15137. (inc (if visually 1 0)))
  15138. (save-excursion
  15139. (beginning-of-line (- 1 (or nlines 0)))
  15140. (while (re-search-forward re eol t)
  15141. (if (and (<= (match-beginning 0) pos)
  15142. (>= (+ inc (match-end 0)) pos))
  15143. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15144. (defun org-at-regexp-p (regexp)
  15145. "Is point inside a match of REGEXP in the current line?"
  15146. (catch 'exit
  15147. (save-excursion
  15148. (let ((pos (point)) (end (point-at-eol)))
  15149. (beginning-of-line 1)
  15150. (while (re-search-forward regexp end t)
  15151. (if (and (<= (match-beginning 0) pos)
  15152. (>= (match-end 0) pos))
  15153. (throw 'exit t)))
  15154. nil))))
  15155. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15156. "Call `multi-occur' with buffers for all agenda files."
  15157. (interactive "sOrg-files matching: \np")
  15158. (let* ((files (org-agenda-files))
  15159. (tnames (mapcar 'file-truename files))
  15160. (extra org-agenda-text-search-extra-files)
  15161. f)
  15162. (when (eq (car extra) 'agenda-archives)
  15163. (setq extra (cdr extra))
  15164. (setq files (org-add-archive-files files)))
  15165. (while (setq f (pop extra))
  15166. (unless (member (file-truename f) tnames)
  15167. (add-to-list 'files f 'append)
  15168. (add-to-list 'tnames (file-truename f) 'append)))
  15169. (multi-occur
  15170. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  15171. regexp)))
  15172. (if (boundp 'occur-mode-find-occurrence-hook)
  15173. ;; Emacs 23
  15174. (add-hook 'occur-mode-find-occurrence-hook
  15175. (lambda ()
  15176. (when (org-mode-p)
  15177. (org-reveal))))
  15178. ;; Emacs 22
  15179. (defadvice occur-mode-goto-occurrence
  15180. (after org-occur-reveal activate)
  15181. (and (org-mode-p) (org-reveal)))
  15182. (defadvice occur-mode-goto-occurrence-other-window
  15183. (after org-occur-reveal activate)
  15184. (and (org-mode-p) (org-reveal)))
  15185. (defadvice occur-mode-display-occurrence
  15186. (after org-occur-reveal activate)
  15187. (when (org-mode-p)
  15188. (let ((pos (occur-mode-find-occurrence)))
  15189. (with-current-buffer (marker-buffer pos)
  15190. (save-excursion
  15191. (goto-char pos)
  15192. (org-reveal)))))))
  15193. (defun org-occur-link-in-agenda-files ()
  15194. "Create a link and search for it in the agendas.
  15195. The link is not stored in `org-stored-links', it is just created
  15196. for the search purpose."
  15197. (interactive)
  15198. (let ((link (condition-case nil
  15199. (org-store-link nil)
  15200. (error "Unable to create a link to here"))))
  15201. (org-occur-in-agenda-files (regexp-quote link))))
  15202. (defun org-uniquify (list)
  15203. "Remove duplicate elements from LIST."
  15204. (let (res)
  15205. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15206. res))
  15207. (defun org-delete-all (elts list)
  15208. "Remove all elements in ELTS from LIST."
  15209. (while elts
  15210. (setq list (delete (pop elts) list)))
  15211. list)
  15212. (defun org-back-over-empty-lines ()
  15213. "Move backwards over whitespace, to the beginning of the first empty line.
  15214. Returns the number of empty lines passed."
  15215. (let ((pos (point)))
  15216. (skip-chars-backward " \t\n\r")
  15217. (beginning-of-line 2)
  15218. (goto-char (min (point) pos))
  15219. (count-lines (point) pos)))
  15220. (defun org-skip-whitespace ()
  15221. (skip-chars-forward " \t\n\r"))
  15222. (defun org-point-in-group (point group &optional context)
  15223. "Check if POINT is in match-group GROUP.
  15224. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15225. match. If the match group does ot exist or point is not inside it,
  15226. return nil."
  15227. (and (match-beginning group)
  15228. (>= point (match-beginning group))
  15229. (<= point (match-end group))
  15230. (if context
  15231. (list context (match-beginning group) (match-end group))
  15232. t)))
  15233. (defun org-switch-to-buffer-other-window (&rest args)
  15234. "Switch to buffer in a second window on the current frame.
  15235. In particular, do not allow pop-up frames."
  15236. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15237. special-display-function)
  15238. (apply 'switch-to-buffer-other-window args)))
  15239. (defun org-combine-plists (&rest plists)
  15240. "Create a single property list from all plists in PLISTS.
  15241. The process starts by copying the first list, and then setting properties
  15242. from the other lists. Settings in the last list are the most significant
  15243. ones and overrule settings in the other lists."
  15244. (let ((rtn (copy-sequence (pop plists)))
  15245. p v ls)
  15246. (while plists
  15247. (setq ls (pop plists))
  15248. (while ls
  15249. (setq p (pop ls) v (pop ls))
  15250. (setq rtn (plist-put rtn p v))))
  15251. rtn))
  15252. (defun org-move-line-down (arg)
  15253. "Move the current line down. With prefix argument, move it past ARG lines."
  15254. (interactive "p")
  15255. (let ((col (current-column))
  15256. beg end pos)
  15257. (beginning-of-line 1) (setq beg (point))
  15258. (beginning-of-line 2) (setq end (point))
  15259. (beginning-of-line (+ 1 arg))
  15260. (setq pos (move-marker (make-marker) (point)))
  15261. (insert (delete-and-extract-region beg end))
  15262. (goto-char pos)
  15263. (org-move-to-column col)))
  15264. (defun org-move-line-up (arg)
  15265. "Move the current line up. With prefix argument, move it past ARG lines."
  15266. (interactive "p")
  15267. (let ((col (current-column))
  15268. beg end pos)
  15269. (beginning-of-line 1) (setq beg (point))
  15270. (beginning-of-line 2) (setq end (point))
  15271. (beginning-of-line (- arg))
  15272. (setq pos (move-marker (make-marker) (point)))
  15273. (insert (delete-and-extract-region beg end))
  15274. (goto-char pos)
  15275. (org-move-to-column col)))
  15276. (defun org-replace-escapes (string table)
  15277. "Replace %-escapes in STRING with values in TABLE.
  15278. TABLE is an association list with keys like \"%a\" and string values.
  15279. The sequences in STRING may contain normal field width and padding information,
  15280. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15281. so values can contain further %-escapes if they are define later in TABLE."
  15282. (let ((case-fold-search nil)
  15283. e re rpl)
  15284. (while (setq e (pop table))
  15285. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15286. (while (string-match re string)
  15287. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15288. (cdr e)))
  15289. (setq string (replace-match rpl t t string))))
  15290. string))
  15291. (defun org-sublist (list start end)
  15292. "Return a section of LIST, from START to END.
  15293. Counting starts at 1."
  15294. (let (rtn (c start))
  15295. (setq list (nthcdr (1- start) list))
  15296. (while (and list (<= c end))
  15297. (push (pop list) rtn)
  15298. (setq c (1+ c)))
  15299. (nreverse rtn)))
  15300. (defun org-find-base-buffer-visiting (file)
  15301. "Like `find-buffer-visiting' but always return the base buffer and
  15302. not an indirect buffer."
  15303. (let ((buf (or (get-file-buffer file)
  15304. (find-buffer-visiting file))))
  15305. (if buf
  15306. (or (buffer-base-buffer buf) buf)
  15307. nil)))
  15308. (defun org-image-file-name-regexp (&optional extensions)
  15309. "Return regexp matching the file names of images.
  15310. If EXTENSIONS is given, only match these."
  15311. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15312. (image-file-name-regexp)
  15313. (let ((image-file-name-extensions
  15314. (or extensions
  15315. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15316. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15317. (concat "\\."
  15318. (regexp-opt (nconc (mapcar 'upcase
  15319. image-file-name-extensions)
  15320. image-file-name-extensions)
  15321. t)
  15322. "\\'"))))
  15323. (defun org-file-image-p (file &optional extensions)
  15324. "Return non-nil if FILE is an image."
  15325. (save-match-data
  15326. (string-match (org-image-file-name-regexp extensions) file)))
  15327. (defun org-get-cursor-date ()
  15328. "Return the date at cursor in as a time.
  15329. This works in the calendar and in the agenda, anywhere else it just
  15330. returns the current time."
  15331. (let (date day defd)
  15332. (cond
  15333. ((eq major-mode 'calendar-mode)
  15334. (setq date (calendar-cursor-to-date)
  15335. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15336. ((eq major-mode 'org-agenda-mode)
  15337. (setq day (get-text-property (point) 'day))
  15338. (if day
  15339. (setq date (calendar-gregorian-from-absolute day)
  15340. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15341. (nth 2 date))))))
  15342. (or defd (current-time))))
  15343. (defvar org-agenda-action-marker (make-marker)
  15344. "Marker pointing to the entry for the next agenda action.")
  15345. (defun org-mark-entry-for-agenda-action ()
  15346. "Mark the current entry as target of an agenda action.
  15347. Agenda actions are actions executed from the agenda with the key `k',
  15348. which make use of the date at the cursor."
  15349. (interactive)
  15350. (move-marker org-agenda-action-marker
  15351. (save-excursion (org-back-to-heading t) (point))
  15352. (current-buffer))
  15353. (message
  15354. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15355. ;;; Paragraph filling stuff.
  15356. ;; We want this to be just right, so use the full arsenal.
  15357. (defun org-indent-line-function ()
  15358. "Indent line like previous, but further if previous was headline or item."
  15359. (interactive)
  15360. (let* ((pos (point))
  15361. (itemp (org-at-item-p))
  15362. (case-fold-search t)
  15363. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15364. column bpos bcol tpos tcol bullet btype bullet-type)
  15365. ;; Find the previous relevant line
  15366. (beginning-of-line 1)
  15367. (cond
  15368. ((looking-at "#") (setq column 0))
  15369. ((looking-at "\\*+ ") (setq column 0))
  15370. ((and (looking-at "[ \t]*:END:")
  15371. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15372. (save-excursion
  15373. (goto-char (1- (match-beginning 1)))
  15374. (setq column (current-column))))
  15375. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15376. (save-excursion
  15377. (re-search-backward
  15378. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15379. (setq column (org-get-indentation (match-string 0))))
  15380. (t
  15381. (beginning-of-line 0)
  15382. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15383. (not (looking-at "[ \t]*:END:"))
  15384. (not (looking-at org-drawer-regexp)))
  15385. (beginning-of-line 0))
  15386. (cond
  15387. ((looking-at "\\*+[ \t]+")
  15388. (if (not org-adapt-indentation)
  15389. (setq column 0)
  15390. (goto-char (match-end 0))
  15391. (setq column (current-column))))
  15392. ((looking-at org-drawer-regexp)
  15393. (goto-char (1- (match-beginning 1)))
  15394. (setq column (current-column)))
  15395. ((looking-at "\\([ \t]*\\):END:")
  15396. (goto-char (match-end 1))
  15397. (setq column (current-column)))
  15398. ((org-in-item-p)
  15399. (org-beginning-of-item)
  15400. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15401. (setq bpos (match-beginning 1) tpos (match-end 0)
  15402. bcol (progn (goto-char bpos) (current-column))
  15403. tcol (progn (goto-char tpos) (current-column))
  15404. bullet (match-string 1)
  15405. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15406. (if (> tcol (+ bcol org-description-max-indent))
  15407. (setq tcol (+ bcol 5)))
  15408. (if (not itemp)
  15409. (setq column tcol)
  15410. (goto-char pos)
  15411. (beginning-of-line 1)
  15412. (if (looking-at "\\S-")
  15413. (progn
  15414. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15415. (setq bullet (match-string 1)
  15416. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15417. (setq column (if (equal btype bullet-type) bcol tcol)))
  15418. (setq column (org-get-indentation)))))
  15419. (t (setq column (org-get-indentation))))))
  15420. (goto-char pos)
  15421. (if (<= (current-column) (current-indentation))
  15422. (org-indent-line-to column)
  15423. (save-excursion (org-indent-line-to column)))
  15424. (setq column (current-column))
  15425. (beginning-of-line 1)
  15426. (if (looking-at
  15427. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15428. (replace-match (concat (match-string 1)
  15429. (format org-property-format
  15430. (match-string 2) (match-string 3)))
  15431. t t))
  15432. (org-move-to-column column)))
  15433. (defun org-set-autofill-regexps ()
  15434. (interactive)
  15435. ;; In the paragraph separator we include headlines, because filling
  15436. ;; text in a line directly attached to a headline would otherwise
  15437. ;; fill the headline as well.
  15438. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15439. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15440. ;; The paragraph starter includes hand-formatted lists.
  15441. (org-set-local
  15442. 'paragraph-start
  15443. (concat
  15444. "\f" "\\|"
  15445. "[ ]*$" "\\|"
  15446. "\\*+ " "\\|"
  15447. "[ \t]*#" "\\|"
  15448. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15449. "[ \t]*[:|]" "\\|"
  15450. "\\$\\$" "\\|"
  15451. "\\\\\\(begin\\|end\\|[][]\\)"))
  15452. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15453. ;; But only if the user has not turned off tables or fixed-width regions
  15454. (org-set-local
  15455. 'auto-fill-inhibit-regexp
  15456. (concat "\\*+ \\|#\\+"
  15457. "\\|[ \t]*" org-keyword-time-regexp
  15458. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15459. (concat
  15460. "\\|[ \t]*["
  15461. (if org-enable-table-editor "|" "")
  15462. (if org-enable-fixed-width-editor ":" "")
  15463. "]"))))
  15464. ;; We use our own fill-paragraph function, to make sure that tables
  15465. ;; and fixed-width regions are not wrapped. That function will pass
  15466. ;; through to `fill-paragraph' when appropriate.
  15467. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15468. ; Adaptive filling: To get full control, first make sure that
  15469. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15470. (org-set-local 'adaptive-fill-regexp "\000")
  15471. (org-set-local 'adaptive-fill-function
  15472. 'org-adaptive-fill-function)
  15473. (org-set-local
  15474. 'align-mode-rules-list
  15475. '((org-in-buffer-settings
  15476. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15477. (modes . '(org-mode))))))
  15478. (defun org-fill-paragraph (&optional justify)
  15479. "Re-align a table, pass through to fill-paragraph if no table."
  15480. (let ((table-p (org-at-table-p))
  15481. (table.el-p (org-at-table.el-p)))
  15482. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15483. (save-excursion (goto-char (point-at-bol))
  15484. (looking-at outline-regexp)))
  15485. t) ; skip headlines
  15486. (table.el-p t) ; skip table.el tables
  15487. (table-p (org-table-align) t) ; align org-mode tables
  15488. (t nil)))) ; call paragraph-fill
  15489. ;; For reference, this is the default value of adaptive-fill-regexp
  15490. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15491. (defun org-adaptive-fill-function ()
  15492. "Return a fill prefix for org-mode files.
  15493. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15494. work correctly."
  15495. (cond ((looking-at "#[ \t]+")
  15496. (match-string 0))
  15497. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15498. (save-excursion
  15499. (if (> (match-end 1) (+ (match-beginning 1)
  15500. org-description-max-indent))
  15501. (goto-char (+ (match-beginning 1) 5))
  15502. (goto-char (match-end 0)))
  15503. (make-string (current-column) ?\ )))
  15504. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15505. (save-excursion
  15506. (goto-char (match-end 0))
  15507. (make-string (current-column) ?\ )))
  15508. (t nil)))
  15509. ;;; Other stuff.
  15510. (defun org-toggle-fixed-width-section (arg)
  15511. "Toggle the fixed-width export.
  15512. If there is no active region, the QUOTE keyword at the current headline is
  15513. inserted or removed. When present, it causes the text between this headline
  15514. and the next to be exported as fixed-width text, and unmodified.
  15515. If there is an active region, this command adds or removes a colon as the
  15516. first character of this line. If the first character of a line is a colon,
  15517. this line is also exported in fixed-width font."
  15518. (interactive "P")
  15519. (let* ((cc 0)
  15520. (regionp (org-region-active-p))
  15521. (beg (if regionp (region-beginning) (point)))
  15522. (end (if regionp (region-end)))
  15523. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15524. (case-fold-search nil)
  15525. (re "[ \t]*\\(: \\)")
  15526. off)
  15527. (if regionp
  15528. (save-excursion
  15529. (goto-char beg)
  15530. (setq cc (current-column))
  15531. (beginning-of-line 1)
  15532. (setq off (looking-at re))
  15533. (while (> nlines 0)
  15534. (setq nlines (1- nlines))
  15535. (beginning-of-line 1)
  15536. (cond
  15537. (arg
  15538. (org-move-to-column cc t)
  15539. (insert ": \n")
  15540. (forward-line -1))
  15541. ((and off (looking-at re))
  15542. (replace-match "" t t nil 1))
  15543. ((not off) (org-move-to-column cc t) (insert ": ")))
  15544. (forward-line 1)))
  15545. (save-excursion
  15546. (org-back-to-heading)
  15547. (if (looking-at (concat outline-regexp
  15548. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15549. (replace-match "" t t nil 1)
  15550. (if (looking-at outline-regexp)
  15551. (progn
  15552. (goto-char (match-end 0))
  15553. (insert org-quote-string " "))))))))
  15554. (defun org-reftex-citation ()
  15555. "Use reftex-citation to insert a citation into the buffer.
  15556. This looks for a line like
  15557. #+BIBLIOGRAPHY: foo plain option:-d
  15558. and derives from it that foo.bib is the bibliography file relevant
  15559. for this document. It then installs the necessary environment for RefTeX
  15560. to work in this buffer and calls `reftex-citation' to insert a citation
  15561. into the buffer.
  15562. Export of such citations to both LaTeX and HTML is handled by the contributed
  15563. package org-exp-bibtex by Taru Karttunen."
  15564. (interactive)
  15565. (let ((reftex-docstruct-symbol 'rds)
  15566. (reftex-cite-format "\\cite{%l}")
  15567. rds bib)
  15568. (save-excursion
  15569. (save-restriction
  15570. (widen)
  15571. (let ((case-fold-search t)
  15572. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15573. (if (not (save-excursion
  15574. (or (re-search-forward re nil t)
  15575. (re-search-backward re nil t))))
  15576. (error "No bibliography defined in file")
  15577. (setq bib (concat (match-string 1) ".bib")
  15578. rds (list (list 'bib bib)))))))
  15579. (call-interactively 'reftex-citation)))
  15580. ;;;; Functions extending outline functionality
  15581. (defun org-beginning-of-line (&optional arg)
  15582. "Go to the beginning of the current line. If that is invisible, continue
  15583. to a visible line beginning. This makes the function of C-a more intuitive.
  15584. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15585. first attempt, and only move to after the tags when the cursor is already
  15586. beyond the end of the headline."
  15587. (interactive "P")
  15588. (let ((pos (point))
  15589. (special (if (consp org-special-ctrl-a/e)
  15590. (car org-special-ctrl-a/e)
  15591. org-special-ctrl-a/e))
  15592. refpos)
  15593. (if (org-bound-and-true-p line-move-visual)
  15594. (beginning-of-visual-line 1)
  15595. (beginning-of-line 1))
  15596. (if (and arg (fboundp 'move-beginning-of-line))
  15597. (call-interactively 'move-beginning-of-line)
  15598. (if (bobp)
  15599. nil
  15600. (backward-char 1)
  15601. (if (org-invisible-p)
  15602. (while (and (not (bobp)) (org-invisible-p))
  15603. (backward-char 1)
  15604. (beginning-of-line 1))
  15605. (forward-char 1))))
  15606. (when special
  15607. (cond
  15608. ((and (looking-at org-complex-heading-regexp)
  15609. (= (char-after (match-end 1)) ?\ ))
  15610. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15611. (point-at-eol)))
  15612. (goto-char
  15613. (if (eq special t)
  15614. (cond ((> pos refpos) refpos)
  15615. ((= pos (point)) refpos)
  15616. (t (point)))
  15617. (cond ((> pos (point)) (point))
  15618. ((not (eq last-command this-command)) (point))
  15619. (t refpos)))))
  15620. ((org-at-item-p)
  15621. (goto-char
  15622. (if (eq special t)
  15623. (cond ((> pos (match-end 4)) (match-end 4))
  15624. ((= pos (point)) (match-end 4))
  15625. (t (point)))
  15626. (cond ((> pos (point)) (point))
  15627. ((not (eq last-command this-command)) (point))
  15628. (t (match-end 4))))))))
  15629. (org-no-warnings
  15630. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15631. (defun org-end-of-line (&optional arg)
  15632. "Go to the end of the line.
  15633. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15634. first attempt, and only move to after the tags when the cursor is already
  15635. beyond the end of the headline."
  15636. (interactive "P")
  15637. (let ((special (if (consp org-special-ctrl-a/e)
  15638. (cdr org-special-ctrl-a/e)
  15639. org-special-ctrl-a/e)))
  15640. (if (or (not special)
  15641. (not (org-on-heading-p))
  15642. arg)
  15643. (call-interactively
  15644. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15645. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15646. (t 'end-of-line)))
  15647. (let ((pos (point)))
  15648. (beginning-of-line 1)
  15649. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15650. (if (eq special t)
  15651. (if (or (< pos (match-beginning 1))
  15652. (= pos (match-end 0)))
  15653. (goto-char (match-beginning 1))
  15654. (goto-char (match-end 0)))
  15655. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15656. (goto-char (match-end 0))
  15657. (goto-char (match-beginning 1))))
  15658. (call-interactively (if (fboundp 'move-end-of-line)
  15659. 'move-end-of-line
  15660. 'end-of-line)))))
  15661. (org-no-warnings
  15662. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15663. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15664. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15665. (define-key org-mode-map [home] 'org-beginning-of-line)
  15666. (define-key org-mode-map [end] 'org-end-of-line)
  15667. (defun org-backward-sentence (&optional arg)
  15668. "Go to beginning of sentence, or beginning of table field.
  15669. This will call `backward-sentence' or `org-table-beginning-of-field',
  15670. depending on context."
  15671. (interactive "P")
  15672. (cond
  15673. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15674. (t (call-interactively 'backward-sentence))))
  15675. (defun org-forward-sentence (&optional arg)
  15676. "Go to end of sentence, or end of table field.
  15677. This will call `forward-sentence' or `org-table-end-of-field',
  15678. depending on context."
  15679. (interactive "P")
  15680. (cond
  15681. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15682. (t (call-interactively 'forward-sentence))))
  15683. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15684. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15685. (defun org-kill-line (&optional arg)
  15686. "Kill line, to tags or end of line."
  15687. (interactive "P")
  15688. (cond
  15689. ((or (not org-special-ctrl-k)
  15690. (bolp)
  15691. (not (org-on-heading-p)))
  15692. (call-interactively 'kill-line))
  15693. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15694. (kill-region (point) (match-beginning 1))
  15695. (org-set-tags nil t))
  15696. (t (kill-region (point) (point-at-eol)))))
  15697. (define-key org-mode-map "\C-k" 'org-kill-line)
  15698. (defun org-yank (&optional arg)
  15699. "Yank. If the kill is a subtree, treat it specially.
  15700. This command will look at the current kill and check if is a single
  15701. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15702. cursor is at the beginning of a line or after the stars of a currently
  15703. empty headline, then the yank is handled specially. How exactly depends
  15704. on the value of the following variables, both set by default.
  15705. org-yank-folded-subtrees
  15706. When set, the subtree(s) will be folded after insertion, but only
  15707. if doing so would now swallow text after the yanked text.
  15708. org-yank-adjusted-subtrees
  15709. When set, the subtree will be promoted or demoted in order to
  15710. fit into the local outline tree structure, which means that the level
  15711. will be adjusted so that it becomes the smaller one of the two
  15712. *visible* surrounding headings.
  15713. Any prefix to this command will cause `yank' to be called directly with
  15714. no special treatment. In particular, a simple `C-u' prefix will just
  15715. plainly yank the text as it is.
  15716. \[1] The test checks if the first non-white line is a heading
  15717. and if there are no other headings with fewer stars."
  15718. (interactive "P")
  15719. (org-yank-generic 'yank arg))
  15720. (defun org-yank-generic (command arg)
  15721. "Perform some yank-like command.
  15722. This function implements the behavior described in the `org-yank'
  15723. documentation. However, it has been generalized to work for any
  15724. interactive command with similar behavior."
  15725. ;; pretend to be command COMMAND
  15726. (setq this-command command)
  15727. (if arg
  15728. (call-interactively command)
  15729. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15730. (and (org-kill-is-subtree-p)
  15731. (or (bolp)
  15732. (and (looking-at "[ \t]*$")
  15733. (string-match
  15734. "\\`\\*+\\'"
  15735. (buffer-substring (point-at-bol) (point)))))))
  15736. swallowp)
  15737. (cond
  15738. ((and subtreep org-yank-folded-subtrees)
  15739. (let ((beg (point))
  15740. end)
  15741. (if (and subtreep org-yank-adjusted-subtrees)
  15742. (org-paste-subtree nil nil 'for-yank)
  15743. (call-interactively command))
  15744. (setq end (point))
  15745. (goto-char beg)
  15746. (when (and (bolp) subtreep
  15747. (not (setq swallowp
  15748. (org-yank-folding-would-swallow-text beg end))))
  15749. (or (looking-at outline-regexp)
  15750. (re-search-forward (concat "^" outline-regexp) end t))
  15751. (while (and (< (point) end) (looking-at outline-regexp))
  15752. (hide-subtree)
  15753. (org-cycle-show-empty-lines 'folded)
  15754. (condition-case nil
  15755. (outline-forward-same-level 1)
  15756. (error (goto-char end)))))
  15757. (when swallowp
  15758. (message
  15759. "Inserted text not folded because that would swallow text"))
  15760. (goto-char end)
  15761. (skip-chars-forward " \t\n\r")
  15762. (beginning-of-line 1)
  15763. (push-mark beg 'nomsg)))
  15764. ((and subtreep org-yank-adjusted-subtrees)
  15765. (let ((beg (point-at-bol)))
  15766. (org-paste-subtree nil nil 'for-yank)
  15767. (push-mark beg 'nomsg)))
  15768. (t
  15769. (call-interactively command))))))
  15770. (defun org-yank-folding-would-swallow-text (beg end)
  15771. "Would hide-subtree at BEG swallow any text after END?"
  15772. (let (level)
  15773. (save-excursion
  15774. (goto-char beg)
  15775. (when (or (looking-at outline-regexp)
  15776. (re-search-forward (concat "^" outline-regexp) end t))
  15777. (setq level (org-outline-level)))
  15778. (goto-char end)
  15779. (skip-chars-forward " \t\r\n\v\f")
  15780. (if (or (eobp)
  15781. (and (bolp) (looking-at org-outline-regexp)
  15782. (<= (org-outline-level) level)))
  15783. nil ; Nothing would be swallowed
  15784. t)))) ; something would swallow
  15785. (define-key org-mode-map "\C-y" 'org-yank)
  15786. (defun org-invisible-p ()
  15787. "Check if point is at a character currently not visible."
  15788. ;; Early versions of noutline don't have `outline-invisible-p'.
  15789. (if (fboundp 'outline-invisible-p)
  15790. (outline-invisible-p)
  15791. (get-char-property (point) 'invisible)))
  15792. (defun org-invisible-p2 ()
  15793. "Check if point is at a character currently not visible."
  15794. (save-excursion
  15795. (if (and (eolp) (not (bobp))) (backward-char 1))
  15796. ;; Early versions of noutline don't have `outline-invisible-p'.
  15797. (if (fboundp 'outline-invisible-p)
  15798. (outline-invisible-p)
  15799. (get-char-property (point) 'invisible))))
  15800. (defun org-back-to-heading (&optional invisible-ok)
  15801. "Call `outline-back-to-heading', but provide a better error message."
  15802. (condition-case nil
  15803. (outline-back-to-heading invisible-ok)
  15804. (error (error "Before first headline at position %d in buffer %s"
  15805. (point) (current-buffer)))))
  15806. (defun org-before-first-heading-p ()
  15807. "Before first heading?"
  15808. (save-excursion
  15809. (null (re-search-backward "^\\*+ " nil t))))
  15810. (defun org-on-heading-p (&optional ignored)
  15811. (outline-on-heading-p t))
  15812. (defun org-at-heading-p (&optional ignored)
  15813. (outline-on-heading-p t))
  15814. (defun org-at-heading-or-item-p ()
  15815. (or (org-on-heading-p) (org-at-item-p)))
  15816. (defun org-on-target-p ()
  15817. (or (org-in-regexp org-radio-target-regexp)
  15818. (org-in-regexp org-target-regexp)))
  15819. (defun org-up-heading-all (arg)
  15820. "Move to the heading line of which the present line is a subheading.
  15821. This function considers both visible and invisible heading lines.
  15822. With argument, move up ARG levels."
  15823. (if (fboundp 'outline-up-heading-all)
  15824. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15825. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15826. (defun org-up-heading-safe ()
  15827. "Move to the heading line of which the present line is a subheading.
  15828. This version will not throw an error. It will return the level of the
  15829. headline found, or nil if no higher level is found.
  15830. Also, this function will be a lot faster than `outline-up-heading',
  15831. because it relies on stars being the outline starters. This can really
  15832. make a significant difference in outlines with very many siblings."
  15833. (let (start-level re)
  15834. (org-back-to-heading t)
  15835. (setq start-level (funcall outline-level))
  15836. (if (equal start-level 1)
  15837. nil
  15838. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15839. (if (re-search-backward re nil t)
  15840. (funcall outline-level)))))
  15841. (defun org-first-sibling-p ()
  15842. "Is this heading the first child of its parents?"
  15843. (interactive)
  15844. (let ((re (concat "^" outline-regexp))
  15845. level l)
  15846. (unless (org-at-heading-p t)
  15847. (error "Not at a heading"))
  15848. (setq level (funcall outline-level))
  15849. (save-excursion
  15850. (if (not (re-search-backward re nil t))
  15851. t
  15852. (setq l (funcall outline-level))
  15853. (< l level)))))
  15854. (defun org-goto-sibling (&optional previous)
  15855. "Goto the next sibling, even if it is invisible.
  15856. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15857. when a sibling was found. When none is found, return nil and don't
  15858. move point."
  15859. (let ((fun (if previous 're-search-backward 're-search-forward))
  15860. (pos (point))
  15861. (re (concat "^" outline-regexp))
  15862. level l)
  15863. (when (condition-case nil (org-back-to-heading t) (error nil))
  15864. (setq level (funcall outline-level))
  15865. (catch 'exit
  15866. (or previous (forward-char 1))
  15867. (while (funcall fun re nil t)
  15868. (setq l (funcall outline-level))
  15869. (when (< l level) (goto-char pos) (throw 'exit nil))
  15870. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15871. (goto-char pos)
  15872. nil))))
  15873. (defun org-show-siblings ()
  15874. "Show all siblings of the current headline."
  15875. (save-excursion
  15876. (while (org-goto-sibling) (org-flag-heading nil)))
  15877. (save-excursion
  15878. (while (org-goto-sibling 'previous)
  15879. (org-flag-heading nil))))
  15880. (defun org-show-hidden-entry ()
  15881. "Show an entry where even the heading is hidden."
  15882. (save-excursion
  15883. (org-show-entry)))
  15884. (defun org-flag-heading (flag &optional entry)
  15885. "Flag the current heading. FLAG non-nil means make invisible.
  15886. When ENTRY is non-nil, show the entire entry."
  15887. (save-excursion
  15888. (org-back-to-heading t)
  15889. ;; Check if we should show the entire entry
  15890. (if entry
  15891. (progn
  15892. (org-show-entry)
  15893. (save-excursion
  15894. (and (outline-next-heading)
  15895. (org-flag-heading nil))))
  15896. (outline-flag-region (max (point-min) (1- (point)))
  15897. (save-excursion (outline-end-of-heading) (point))
  15898. flag))))
  15899. (defun org-get-next-sibling ()
  15900. "Move to next heading of the same level, and return point.
  15901. If there is no such heading, return nil.
  15902. This is like outline-next-sibling, but invisible headings are ok."
  15903. (let ((level (funcall outline-level)))
  15904. (outline-next-heading)
  15905. (while (and (not (eobp)) (> (funcall outline-level) level))
  15906. (outline-next-heading))
  15907. (if (or (eobp) (< (funcall outline-level) level))
  15908. nil
  15909. (point))))
  15910. (defun org-get-last-sibling ()
  15911. "Move to previous heading of the same level, and return point.
  15912. If there is no such heading, return nil."
  15913. (let ((opoint (point))
  15914. (level (funcall outline-level)))
  15915. (outline-previous-heading)
  15916. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15917. (while (and (> (funcall outline-level) level)
  15918. (not (bobp)))
  15919. (outline-previous-heading))
  15920. (if (< (funcall outline-level) level)
  15921. nil
  15922. (point)))))
  15923. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15924. ;; This contains an exact copy of the original function, but it uses
  15925. ;; `org-back-to-heading', to make it work also in invisible
  15926. ;; trees. And is uses an invisible-OK argument.
  15927. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15928. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15929. ;; with many children and grandchildren etc, this can be much faster
  15930. ;; than the outline version.
  15931. (org-back-to-heading invisible-OK)
  15932. (let ((first t)
  15933. (level (funcall outline-level)))
  15934. (if (and (org-mode-p) (< level 1000))
  15935. ;; A true heading (not a plain list item), in Org-mode
  15936. ;; This means we can easily find the end by looking
  15937. ;; only for the right number of stars. Using a regexp to do
  15938. ;; this is so much faster than using a Lisp loop.
  15939. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15940. (forward-char 1)
  15941. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15942. ;; something else, do it the slow way
  15943. (while (and (not (eobp))
  15944. (or first (> (funcall outline-level) level)))
  15945. (setq first nil)
  15946. (outline-next-heading)))
  15947. (unless to-heading
  15948. (if (memq (preceding-char) '(?\n ?\^M))
  15949. (progn
  15950. ;; Go to end of line before heading
  15951. (forward-char -1)
  15952. (if (memq (preceding-char) '(?\n ?\^M))
  15953. ;; leave blank line before heading
  15954. (forward-char -1))))))
  15955. (point))
  15956. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15957. "Use Org version in org-mode, for dramatic speed-up."
  15958. (if (eq major-mode 'org-mode)
  15959. (progn
  15960. (org-end-of-subtree nil t)
  15961. (unless (eobp) (backward-char 1)))
  15962. ad-do-it))
  15963. (defun org-forward-same-level (arg &optional invisible-ok)
  15964. "Move forward to the arg'th subheading at same level as this one.
  15965. Stop at the first and last subheadings of a superior heading."
  15966. (interactive "p")
  15967. (org-back-to-heading invisible-ok)
  15968. (org-on-heading-p)
  15969. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15970. (re (format "^\\*\\{1,%d\\} " level))
  15971. l)
  15972. (forward-char 1)
  15973. (while (> arg 0)
  15974. (while (and (re-search-forward re nil 'move)
  15975. (setq l (- (match-end 0) (match-beginning 0) 1))
  15976. (= l level)
  15977. (not invisible-ok)
  15978. (org-invisible-p))
  15979. (if (< l level) (setq arg 1)))
  15980. (setq arg (1- arg)))
  15981. (beginning-of-line 1)))
  15982. (defun org-backward-same-level (arg &optional invisible-ok)
  15983. "Move backward to the arg'th subheading at same level as this one.
  15984. Stop at the first and last subheadings of a superior heading."
  15985. (interactive "p")
  15986. (org-back-to-heading)
  15987. (org-on-heading-p)
  15988. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15989. (re (format "^\\*\\{1,%d\\} " level))
  15990. l)
  15991. (while (> arg 0)
  15992. (while (and (re-search-backward re nil 'move)
  15993. (setq l (- (match-end 0) (match-beginning 0) 1))
  15994. (= l level)
  15995. (not invisible-ok)
  15996. (org-invisible-p))
  15997. (if (< l level) (setq arg 1)))
  15998. (setq arg (1- arg)))))
  15999. (defun org-show-subtree ()
  16000. "Show everything after this heading at deeper levels."
  16001. (outline-flag-region
  16002. (point)
  16003. (save-excursion
  16004. (org-end-of-subtree t t))
  16005. nil))
  16006. (defun org-show-entry ()
  16007. "Show the body directly following this heading.
  16008. Show the heading too, if it is currently invisible."
  16009. (interactive)
  16010. (save-excursion
  16011. (condition-case nil
  16012. (progn
  16013. (org-back-to-heading t)
  16014. (outline-flag-region
  16015. (max (point-min) (1- (point)))
  16016. (save-excursion
  16017. (if (re-search-forward
  16018. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16019. (match-beginning 1)
  16020. (point-max)))
  16021. nil)
  16022. (org-cycle-hide-drawers 'children))
  16023. (error nil))))
  16024. (defun org-make-options-regexp (kwds &optional extra)
  16025. "Make a regular expression for keyword lines."
  16026. (concat
  16027. "^"
  16028. "#?[ \t]*\\+\\("
  16029. (mapconcat 'regexp-quote kwds "\\|")
  16030. (if extra (concat "\\|" extra))
  16031. "\\):[ \t]*"
  16032. "\\(.*\\)"))
  16033. ;; Make isearch reveal the necessary context
  16034. (defun org-isearch-end ()
  16035. "Reveal context after isearch exits."
  16036. (when isearch-success ; only if search was successful
  16037. (if (featurep 'xemacs)
  16038. ;; Under XEmacs, the hook is run in the correct place,
  16039. ;; we directly show the context.
  16040. (org-show-context 'isearch)
  16041. ;; In Emacs the hook runs *before* restoring the overlays.
  16042. ;; So we have to use a one-time post-command-hook to do this.
  16043. ;; (Emacs 22 has a special variable, see function `org-mode')
  16044. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16045. isearch-mode-end-hook-quit)
  16046. ;; Only when the isearch was not quitted.
  16047. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16048. 'append 'local)))))
  16049. (defun org-isearch-post-command ()
  16050. "Remove self from hook, and show context."
  16051. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16052. (org-show-context 'isearch))
  16053. ;;;; Integration with and fixes for other packages
  16054. ;;; Imenu support
  16055. (defvar org-imenu-markers nil
  16056. "All markers currently used by Imenu.")
  16057. (make-variable-buffer-local 'org-imenu-markers)
  16058. (defun org-imenu-new-marker (&optional pos)
  16059. "Return a new marker for use by Imenu, and remember the marker."
  16060. (let ((m (make-marker)))
  16061. (move-marker m (or pos (point)))
  16062. (push m org-imenu-markers)
  16063. m))
  16064. (defun org-imenu-get-tree ()
  16065. "Produce the index for Imenu."
  16066. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16067. (setq org-imenu-markers nil)
  16068. (let* ((n org-imenu-depth)
  16069. (re (concat "^" outline-regexp))
  16070. (subs (make-vector (1+ n) nil))
  16071. (last-level 0)
  16072. m level head)
  16073. (save-excursion
  16074. (save-restriction
  16075. (widen)
  16076. (goto-char (point-max))
  16077. (while (re-search-backward re nil t)
  16078. (setq level (org-reduced-level (funcall outline-level)))
  16079. (when (<= level n)
  16080. (looking-at org-complex-heading-regexp)
  16081. (setq head (org-link-display-format
  16082. (org-match-string-no-properties 4))
  16083. m (org-imenu-new-marker))
  16084. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16085. (if (>= level last-level)
  16086. (push (cons head m) (aref subs level))
  16087. (push (cons head (aref subs (1+ level))) (aref subs level))
  16088. (loop for i from (1+ level) to n do (aset subs i nil)))
  16089. (setq last-level level)))))
  16090. (aref subs 1)))
  16091. (eval-after-load "imenu"
  16092. '(progn
  16093. (add-hook 'imenu-after-jump-hook
  16094. (lambda ()
  16095. (if (eq major-mode 'org-mode)
  16096. (org-show-context 'org-goto))))))
  16097. (defun org-link-display-format (link)
  16098. "Replace a link with either the description, or the link target
  16099. if no description is present"
  16100. (save-match-data
  16101. (if (string-match org-bracket-link-analytic-regexp link)
  16102. (replace-match (if (match-end 5)
  16103. (match-string 5 link)
  16104. (concat (match-string 1 link)
  16105. (match-string 3 link)))
  16106. nil t link)
  16107. link)))
  16108. ;; Speedbar support
  16109. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16110. "Overlay marking the agenda restriction line in speedbar.")
  16111. (org-overlay-put org-speedbar-restriction-lock-overlay
  16112. 'face 'org-agenda-restriction-lock)
  16113. (org-overlay-put org-speedbar-restriction-lock-overlay
  16114. 'help-echo "Agendas are currently limited to this item.")
  16115. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16116. (defun org-speedbar-set-agenda-restriction ()
  16117. "Restrict future agenda commands to the location at point in speedbar.
  16118. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16119. (interactive)
  16120. (require 'org-agenda)
  16121. (let (p m tp np dir txt)
  16122. (cond
  16123. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16124. 'org-imenu t))
  16125. (setq m (get-text-property p 'org-imenu-marker))
  16126. (with-current-buffer (marker-buffer m)
  16127. (goto-char m)
  16128. (org-agenda-set-restriction-lock 'subtree)))
  16129. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16130. 'speedbar-function 'speedbar-find-file))
  16131. (setq tp (previous-single-property-change
  16132. (1+ p) 'speedbar-function)
  16133. np (next-single-property-change
  16134. tp 'speedbar-function)
  16135. dir (speedbar-line-directory)
  16136. txt (buffer-substring-no-properties (or tp (point-min))
  16137. (or np (point-max))))
  16138. (with-current-buffer (find-file-noselect
  16139. (let ((default-directory dir))
  16140. (expand-file-name txt)))
  16141. (unless (org-mode-p)
  16142. (error "Cannot restrict to non-Org-mode file"))
  16143. (org-agenda-set-restriction-lock 'file)))
  16144. (t (error "Don't know how to restrict Org-mode's agenda")))
  16145. (org-move-overlay org-speedbar-restriction-lock-overlay
  16146. (point-at-bol) (point-at-eol))
  16147. (setq current-prefix-arg nil)
  16148. (org-agenda-maybe-redo)))
  16149. (eval-after-load "speedbar"
  16150. '(progn
  16151. (speedbar-add-supported-extension ".org")
  16152. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16153. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16154. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16155. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16156. (add-hook 'speedbar-visiting-tag-hook
  16157. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16158. ;;; Fixes and Hacks for problems with other packages
  16159. ;; Make flyspell not check words in links, to not mess up our keymap
  16160. (defun org-mode-flyspell-verify ()
  16161. "Don't let flyspell put overlays at active buttons."
  16162. (and (not (get-text-property (point) 'keymap))
  16163. (not (get-text-property (point) 'org-no-flyspell))))
  16164. (defun org-remove-flyspell-overlays-in (beg end)
  16165. "Remove flyspell overlays in region."
  16166. (and (org-bound-and-true-p flyspell-mode)
  16167. (fboundp 'flyspell-delete-region-overlays)
  16168. (flyspell-delete-region-overlays beg end))
  16169. (add-text-properties beg end '(org-no-flyspell t)))
  16170. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16171. (eval-after-load "bookmark"
  16172. '(if (boundp 'bookmark-after-jump-hook)
  16173. ;; We can use the hook
  16174. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16175. ;; Hook not available, use advice
  16176. (defadvice bookmark-jump (after org-make-visible activate)
  16177. "Make the position visible."
  16178. (org-bookmark-jump-unhide))))
  16179. ;; Make sure saveplace shows the location if it was hidden
  16180. (eval-after-load "saveplace"
  16181. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16182. "Make the position visible."
  16183. (org-bookmark-jump-unhide)))
  16184. ;; Make sure ecb shows the location if it was hidden
  16185. (eval-after-load "ecb"
  16186. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16187. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16188. (if (eq major-mode 'org-mode)
  16189. (org-show-context))))
  16190. (defun org-bookmark-jump-unhide ()
  16191. "Unhide the current position, to show the bookmark location."
  16192. (and (org-mode-p)
  16193. (or (org-invisible-p)
  16194. (save-excursion (goto-char (max (point-min) (1- (point))))
  16195. (org-invisible-p)))
  16196. (org-show-context 'bookmark-jump)))
  16197. ;; Make session.el ignore our circular variable
  16198. (eval-after-load "session"
  16199. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16200. ;;;; Experimental code
  16201. (defun org-closed-in-range ()
  16202. "Sparse tree of items closed in a certain time range.
  16203. Still experimental, may disappear in the future."
  16204. (interactive)
  16205. ;; Get the time interval from the user.
  16206. (let* ((time1 (org-float-time
  16207. (org-read-date nil 'to-time nil "Starting date: ")))
  16208. (time2 (org-float-time
  16209. (org-read-date nil 'to-time nil "End date:")))
  16210. ;; callback function
  16211. (callback (lambda ()
  16212. (let ((time
  16213. (org-float-time
  16214. (apply 'encode-time
  16215. (org-parse-time-string
  16216. (match-string 1))))))
  16217. ;; check if time in interval
  16218. (and (>= time time1) (<= time time2))))))
  16219. ;; make tree, check each match with the callback
  16220. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16221. ;;;; Finish up
  16222. (provide 'org)
  16223. (run-hooks 'org-load-hook)
  16224. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16225. ;;; org.el ends here