org.el 560 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096
  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.19b
  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-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.19b"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (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)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  163. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  164. (const :tag "C eval: Include command output as text" org-eval)
  165. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  166. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  167. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  168. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  169. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  170. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  171. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  172. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  173. (const :tag "C registry: A registry for Org links" org-registry)
  174. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  175. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  176. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  177. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  178. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  179. (defcustom org-support-shift-select
  180. (and (boundp 'shift-select-mode) shift-select-mode t)
  181. "Non-nil means, make shift-cursor commands select text when possible.
  182. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  183. selecting a region, or enlarge thusly started regions. In Org-mode,
  184. in special contexts, these same keys are used for other purposes, important
  185. enough to compete with shift selection. Org tries to balance these needs
  186. by supporting `shift-select-mode' outside these special contexts, under
  187. control of this variable.
  188. When this variable is t and the cursor is not in a special context,
  189. Org-mode will support shift-selection for making and enlarging regions.
  190. Shift-cursor keys have special meanings at the following locations:
  191. - on a headline (changing TODO state and priority)
  192. - on a plain list item bullet (cycling the bullet type)
  193. - on a time stamp (changing the time)
  194. - in the BEGIN line of a clock table (changing the time block).
  195. If you set this variable to the symbol `even-in-headlines', then the
  196. keys will not change TODO states and priorities in headlines, to
  197. make shift selection work there as well. This is not really a problem,
  198. because there are alternative commands `C-c C-t' and `C-c ,' to change
  199. TODO state and priority.
  200. However, when the cursor is on a timestamp, headline or not, shift-cursor
  201. commands will still edit the time stamp - this is just too good to give up.
  202. XEmacs user should have this variable set to nil."
  203. :group 'org
  204. :type '(choice
  205. (const :tag "Not at all" nil)
  206. (const :tag "When context allows" t)
  207. (const :tag "Even in headlines" even-in-headlines)))
  208. (defgroup org-startup nil
  209. "Options concerning startup of Org-mode."
  210. :tag "Org Startup"
  211. :group 'org)
  212. (defcustom org-startup-folded t
  213. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  214. This can also be configured on a per-file basis by adding one of
  215. the following lines anywhere in the buffer:
  216. #+STARTUP: fold
  217. #+STARTUP: nofold
  218. #+STARTUP: content"
  219. :group 'org-startup
  220. :type '(choice
  221. (const :tag "nofold: show all" nil)
  222. (const :tag "fold: overview" t)
  223. (const :tag "content: all headlines" content)))
  224. (defcustom org-startup-truncated t
  225. "Non-nil means, entering Org-mode will set `truncate-lines'.
  226. This is useful since some lines containing links can be very long and
  227. uninteresting. Also tables look terrible when wrapped."
  228. :group 'org-startup
  229. :type 'boolean)
  230. (defcustom org-startup-align-all-tables nil
  231. "Non-nil means, align all tables when visiting a file.
  232. This is useful when the column width in tables is forced with <N> cookies
  233. in table fields. Such tables will look correct only after the first re-align.
  234. This can also be configured on a per-file basis by adding one of
  235. the following lines anywhere in the buffer:
  236. #+STARTUP: align
  237. #+STARTUP: noalign"
  238. :group 'org-startup
  239. :type 'boolean)
  240. (defcustom org-insert-mode-line-in-empty-file nil
  241. "Non-nil means insert the first line setting Org-mode in empty files.
  242. When the function `org-mode' is called interactively in an empty file, this
  243. normally means that the file name does not automatically trigger Org-mode.
  244. To ensure that the file will always be in Org-mode in the future, a
  245. line enforcing Org-mode will be inserted into the buffer, if this option
  246. has been set."
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defcustom org-replace-disputed-keys nil
  250. "Non-nil means use alternative key bindings for some keys.
  251. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  252. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  253. If you want to use Org-mode together with one of these other modes,
  254. or more generally if you would like to move some Org-mode commands to
  255. other keys, set this variable and configure the keys with the variable
  256. `org-disputed-keys'.
  257. This option is only relevant at load-time of Org-mode, and must be set
  258. *before* org.el is loaded. Changing it requires a restart of Emacs to
  259. become effective."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-use-extra-keys nil
  263. "Non-nil means use extra key sequence definitions for certain
  264. commands. This happens automatically if you run XEmacs or if
  265. window-system is nil. This variable lets you do the same
  266. manually. You must set it before loading org.
  267. Example: on Carbon Emacs 22 running graphically, with an external
  268. keyboard on a Powerbook, the default way of setting M-left might
  269. not work for either Alt or ESC. Setting this variable will make
  270. it work for ESC."
  271. :group 'org-startup
  272. :type 'boolean)
  273. (if (fboundp 'defvaralias)
  274. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  275. (defcustom org-disputed-keys
  276. '(([(shift up)] . [(meta p)])
  277. ([(shift down)] . [(meta n)])
  278. ([(shift left)] . [(meta -)])
  279. ([(shift right)] . [(meta +)])
  280. ([(control shift right)] . [(meta shift +)])
  281. ([(control shift left)] . [(meta shift -)]))
  282. "Keys for which Org-mode and other modes compete.
  283. This is an alist, cars are the default keys, second element specifies
  284. the alternative to use when `org-replace-disputed-keys' is t.
  285. Keys can be specified in any syntax supported by `define-key'.
  286. The value of this option takes effect only at Org-mode's startup,
  287. therefore you'll have to restart Emacs to apply it after changing."
  288. :group 'org-startup
  289. :type 'alist)
  290. (defun org-key (key)
  291. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  292. Or return the original if not disputed."
  293. (if org-replace-disputed-keys
  294. (let* ((nkey (key-description key))
  295. (x (org-find-if (lambda (x)
  296. (equal (key-description (car x)) nkey))
  297. org-disputed-keys)))
  298. (if x (cdr x) key))
  299. key))
  300. (defun org-find-if (predicate seq)
  301. (catch 'exit
  302. (while seq
  303. (if (funcall predicate (car seq))
  304. (throw 'exit (car seq))
  305. (pop seq)))))
  306. (defun org-defkey (keymap key def)
  307. "Define a key, possibly translated, as returned by `org-key'."
  308. (define-key keymap (org-key key) def))
  309. (defcustom org-ellipsis nil
  310. "The ellipsis to use in the Org-mode outline.
  311. When nil, just use the standard three dots. When a string, use that instead,
  312. When a face, use the standard 3 dots, but with the specified face.
  313. The change affects only Org-mode (which will then use its own display table).
  314. Changing this requires executing `M-x org-mode' in a buffer to become
  315. effective."
  316. :group 'org-startup
  317. :type '(choice (const :tag "Default" nil)
  318. (face :tag "Face" :value org-warning)
  319. (string :tag "String" :value "...#")))
  320. (defvar org-display-table nil
  321. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  322. (defgroup org-keywords nil
  323. "Keywords in Org-mode."
  324. :tag "Org Keywords"
  325. :group 'org)
  326. (defcustom org-deadline-string "DEADLINE:"
  327. "String to mark deadline entries.
  328. A deadline is this string, followed by a time stamp. Should be a word,
  329. terminated by a colon. You can insert a schedule keyword and
  330. a timestamp with \\[org-deadline].
  331. Changes become only effective after restarting Emacs."
  332. :group 'org-keywords
  333. :type 'string)
  334. (defcustom org-scheduled-string "SCHEDULED:"
  335. "String to mark scheduled TODO entries.
  336. A schedule is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-schedule].
  339. Changes become only effective after restarting Emacs."
  340. :group 'org-keywords
  341. :type 'string)
  342. (defcustom org-closed-string "CLOSED:"
  343. "String used as the prefix for timestamps logging closing a TODO entry."
  344. :group 'org-keywords
  345. :type 'string)
  346. (defcustom org-clock-string "CLOCK:"
  347. "String used as prefix for timestamps clocking work hours on an item."
  348. :group 'org-keywords
  349. :type 'string)
  350. (defcustom org-comment-string "COMMENT"
  351. "Entries starting with this keyword will never be exported.
  352. An entry can be toggled between COMMENT and normal with
  353. \\[org-toggle-comment].
  354. Changes become only effective after restarting Emacs."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-quote-string "QUOTE"
  358. "Entries starting with this keyword will be exported in fixed-width font.
  359. Quoting applies only to the text in the entry following the headline, and does
  360. not extend beyond the next headline, even if that is lower level.
  361. An entry can be toggled between QUOTE and normal with
  362. \\[org-toggle-fixed-width-section]."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defconst org-repeat-re
  366. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  367. "Regular expression for specifying repeated events.
  368. After a match, group 1 contains the repeat expression.")
  369. (defgroup org-structure nil
  370. "Options concerning the general structure of Org-mode files."
  371. :tag "Org Structure"
  372. :group 'org)
  373. (defgroup org-reveal-location nil
  374. "Options about how to make context of a location visible."
  375. :tag "Org Reveal Location"
  376. :group 'org-structure)
  377. (defconst org-context-choice
  378. '(choice
  379. (const :tag "Always" t)
  380. (const :tag "Never" nil)
  381. (repeat :greedy t :tag "Individual contexts"
  382. (cons
  383. (choice :tag "Context"
  384. (const agenda)
  385. (const org-goto)
  386. (const occur-tree)
  387. (const tags-tree)
  388. (const link-search)
  389. (const mark-goto)
  390. (const bookmark-jump)
  391. (const isearch)
  392. (const default))
  393. (boolean))))
  394. "Contexts for the reveal options.")
  395. (defcustom org-show-hierarchy-above '((default . t))
  396. "Non-nil means, show full hierarchy when revealing a location.
  397. Org-mode often shows locations in an org-mode file which might have
  398. been invisible before. When this is set, the hierarchy of headings
  399. above the exposed location is shown.
  400. Turning this off for example for sparse trees makes them very compact.
  401. Instead of t, this can also be an alist specifying this option for different
  402. contexts. Valid contexts are
  403. agenda when exposing an entry from the agenda
  404. org-goto when using the command `org-goto' on key C-c C-j
  405. occur-tree when using the command `org-occur' on key C-c /
  406. tags-tree when constructing a sparse tree based on tags matches
  407. link-search when exposing search matches associated with a link
  408. mark-goto when exposing the jump goal of a mark
  409. bookmark-jump when exposing a bookmark location
  410. isearch when exiting from an incremental search
  411. default default for all contexts not set explicitly"
  412. :group 'org-reveal-location
  413. :type org-context-choice)
  414. (defcustom org-show-following-heading '((default . nil))
  415. "Non-nil means, show following heading when revealing a location.
  416. Org-mode often shows locations in an org-mode file which might have
  417. been invisible before. When this is set, the heading following the
  418. match is shown.
  419. Turning this off for example for sparse trees makes them very compact,
  420. but makes it harder to edit the location of the match. In such a case,
  421. use the command \\[org-reveal] to show more context.
  422. Instead of t, this can also be an alist specifying this option for different
  423. contexts. See `org-show-hierarchy-above' for valid contexts."
  424. :group 'org-reveal-location
  425. :type org-context-choice)
  426. (defcustom org-show-siblings '((default . nil) (isearch t))
  427. "Non-nil means, show all sibling heading when revealing a location.
  428. Org-mode often shows locations in an org-mode file which might have
  429. been invisible before. When this is set, the sibling of the current entry
  430. heading are all made visible. If `org-show-hierarchy-above' is t,
  431. the same happens on each level of the hierarchy above the current entry.
  432. By default this is on for the isearch context, off for all other contexts.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-entry-below '((default . nil))
  441. "Non-nil means, show the entry below a headline when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the text below the headline that is
  444. exposed is also shown.
  445. By default this is off for all contexts.
  446. Instead of t, this can also be an alist specifying this option for different
  447. contexts. See `org-show-hierarchy-above' for valid contexts."
  448. :group 'org-reveal-location
  449. :type org-context-choice)
  450. (defcustom org-indirect-buffer-display 'other-window
  451. "How should indirect tree buffers be displayed?
  452. This applies to indirect buffers created with the commands
  453. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  454. Valid values are:
  455. current-window Display in the current window
  456. other-window Just display in another window.
  457. dedicated-frame Create one new frame, and re-use it each time.
  458. new-frame Make a new frame each time. Note that in this case
  459. previously-made indirect buffers are kept, and you need to
  460. kill these buffers yourself."
  461. :group 'org-structure
  462. :group 'org-agenda-windows
  463. :type '(choice
  464. (const :tag "In current window" current-window)
  465. (const :tag "In current frame, other window" other-window)
  466. (const :tag "Each time a new frame" new-frame)
  467. (const :tag "One dedicated frame" dedicated-frame)))
  468. (defgroup org-cycle nil
  469. "Options concerning visibility cycling in Org-mode."
  470. :tag "Org Cycle"
  471. :group 'org-structure)
  472. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  473. "Names of drawers. Drawers are not opened by cycling on the headline above.
  474. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  475. this:
  476. :DRAWERNAME:
  477. .....
  478. :END:
  479. The drawer \"PROPERTIES\" is special for capturing properties through
  480. the property API.
  481. Drawers can be defined on the per-file basis with a line like:
  482. #+DRAWERS: HIDDEN STATE PROPERTIES"
  483. :group 'org-structure
  484. :type '(repeat (string :tag "Drawer Name")))
  485. (defcustom org-cycle-global-at-bob nil
  486. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  487. This makes it possible to do global cycling without having to use S-TAB or
  488. C-u TAB. For this special case to work, the first line of the buffer
  489. must not be a headline - it may be empty ot some other text. When used in
  490. this way, `org-cycle-hook' is disables temporarily, to make sure the
  491. cursor stays at the beginning of the buffer.
  492. When this option is nil, don't do anything special at the beginning
  493. of the buffer."
  494. :group 'org-cycle
  495. :type 'boolean)
  496. (defcustom org-cycle-emulate-tab t
  497. "Where should `org-cycle' emulate TAB.
  498. nil Never
  499. white Only in completely white lines
  500. whitestart Only at the beginning of lines, before the first non-white char
  501. t Everywhere except in headlines
  502. exc-hl-bol Everywhere except at the start of a headline
  503. If TAB is used in a place where it does not emulate TAB, the current subtree
  504. visibility is cycled."
  505. :group 'org-cycle
  506. :type '(choice (const :tag "Never" nil)
  507. (const :tag "Only in completely white lines" white)
  508. (const :tag "Before first char in a line" whitestart)
  509. (const :tag "Everywhere except in headlines" t)
  510. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  511. ))
  512. (defcustom org-cycle-separator-lines 2
  513. "Number of empty lines needed to keep an empty line between collapsed trees.
  514. If you leave an empty line between the end of a subtree and the following
  515. headline, this empty line is hidden when the subtree is folded.
  516. Org-mode will leave (exactly) one empty line visible if the number of
  517. empty lines is equal or larger to the number given in this variable.
  518. So the default 2 means, at least 2 empty lines after the end of a subtree
  519. are needed to produce free space between a collapsed subtree and the
  520. following headline.
  521. Special case: when 0, never leave empty lines in collapsed view."
  522. :group 'org-cycle
  523. :type 'integer)
  524. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  525. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  526. org-cycle-hide-drawers
  527. org-cycle-show-empty-lines
  528. org-optimize-window-after-visibility-change)
  529. "Hook that is run after `org-cycle' has changed the buffer visibility.
  530. The function(s) in this hook must accept a single argument which indicates
  531. the new state that was set by the most recent `org-cycle' command. The
  532. argument is a symbol. After a global state change, it can have the values
  533. `overview', `content', or `all'. After a local state change, it can have
  534. the values `folded', `children', or `subtree'."
  535. :group 'org-cycle
  536. :type 'hook)
  537. (defgroup org-edit-structure nil
  538. "Options concerning structure editing in Org-mode."
  539. :tag "Org Edit Structure"
  540. :group 'org-structure)
  541. (defcustom org-odd-levels-only nil
  542. "Non-nil means, skip even levels and only use odd levels for the outline.
  543. This has the effect that two stars are being added/taken away in
  544. promotion/demotion commands. It also influences how levels are
  545. handled by the exporters.
  546. Changing it requires restart of `font-lock-mode' to become effective
  547. for fontification also in regions already fontified.
  548. You may also set this on a per-file basis by adding one of the following
  549. lines to the buffer:
  550. #+STARTUP: odd
  551. #+STARTUP: oddeven"
  552. :group 'org-edit-structure
  553. :group 'org-font-lock
  554. :type 'boolean)
  555. (defcustom org-adapt-indentation t
  556. "Non-nil means, adapt indentation when promoting and demoting.
  557. When this is set and the *entire* text in an entry is indented, the
  558. indentation is increased by one space in a demotion command, and
  559. decreased by one in a promotion command. If any line in the entry
  560. body starts at column 0, indentation is not changed at all."
  561. :group 'org-edit-structure
  562. :type 'boolean)
  563. (defcustom org-special-ctrl-a/e nil
  564. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  565. When t, `C-a' will bring back the cursor to the beginning of the
  566. headline text, i.e. after the stars and after a possible TODO keyword.
  567. In an item, this will be the position after the bullet.
  568. When the cursor is already at that position, another `C-a' will bring
  569. it to the beginning of the line.
  570. `C-e' will jump to the end of the headline, ignoring the presence of tags
  571. in the headline. A second `C-e' will then jump to the true end of the
  572. line, after any tags.
  573. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  574. and only a directly following, identical keypress will bring the cursor
  575. to the special positions."
  576. :group 'org-edit-structure
  577. :type '(choice
  578. (const :tag "off" nil)
  579. (const :tag "after bullet first" t)
  580. (const :tag "border first" reversed)))
  581. (if (fboundp 'defvaralias)
  582. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  583. (defcustom org-special-ctrl-k nil
  584. "Non-nil means `C-k' will behave specially in headlines.
  585. When nil, `C-k' will call the default `kill-line' command.
  586. When t, the following will happen while the cursor is in the headline:
  587. - When the cursor is at the beginning of a headline, kill the entire
  588. line and possible the folded subtree below the line.
  589. - When in the middle of the headline text, kill the headline up to the tags.
  590. - When after the headline text, kill the tags."
  591. :group 'org-edit-structure
  592. :type 'boolean)
  593. (defcustom org-yank-folded-subtrees t
  594. "Non-nil means, when yanking subtrees, fold them.
  595. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  596. it starts with a heading and all other headings in it are either children
  597. or siblings, then fold all the subtrees. However, do this only if no
  598. text after the yank would be swallowed into a folded tree by this action."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-yank-adjusted-subtrees t
  602. "Non-nil means, when yanking subtrees, adjust the level.
  603. With this setting, `org-paste-subtree' is used to insert the subtree, see
  604. this function for details."
  605. :group 'org-edit-structure
  606. :type 'boolean)
  607. (defcustom org-M-RET-may-split-line '((default . t))
  608. "Non-nil means, M-RET will split the line at the cursor position.
  609. When nil, it will go to the end of the line before making a
  610. new line.
  611. You may also set this option in a different way for different
  612. contexts. Valid contexts are:
  613. headline when creating a new headline
  614. item when creating a new item
  615. table in a table field
  616. default the value to be used for all contexts not explicitly
  617. customized"
  618. :group 'org-structure
  619. :group 'org-table
  620. :type '(choice
  621. (const :tag "Always" t)
  622. (const :tag "Never" nil)
  623. (repeat :greedy t :tag "Individual contexts"
  624. (cons
  625. (choice :tag "Context"
  626. (const headline)
  627. (const item)
  628. (const table)
  629. (const default))
  630. (boolean)))))
  631. (defcustom org-insert-heading-respect-content nil
  632. "Non-nil means, insert new headings after the current subtree.
  633. When nil, the new heading is created directly after the current line.
  634. The commands \\[org-insert-heading-respect-content] and
  635. \\[org-insert-todo-heading-respect-content] turn this variable on
  636. for the duration of the command."
  637. :group 'org-structure
  638. :type 'boolean)
  639. (defcustom org-blank-before-new-entry '((heading . auto)
  640. (plain-list-item . auto))
  641. "Should `org-insert-heading' leave a blank line before new heading/item?
  642. The value is an alist, with `heading' and `plain-list-item' as car,
  643. and a boolean flag as cdr."
  644. :group 'org-edit-structure
  645. :type '(list
  646. (cons (const heading)
  647. (choice (const :tag "Never" nil)
  648. (const :tag "Always" t)
  649. (const :tag "Auto" auto)))
  650. (cons (const plain-list-item)
  651. (choice (const :tag "Never" nil)
  652. (const :tag "Always" t)
  653. (const :tag "Auto" auto)))))
  654. (defcustom org-insert-heading-hook nil
  655. "Hook being run after inserting a new heading."
  656. :group 'org-edit-structure
  657. :type 'hook)
  658. (defcustom org-enable-fixed-width-editor t
  659. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  660. This currently only means, they are never auto-wrapped.
  661. When nil, such lines will be treated like ordinary lines.
  662. See also the QUOTE keyword."
  663. :group 'org-edit-structure
  664. :type 'boolean)
  665. (defcustom org-edit-src-region-extra nil
  666. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  667. For examples see the function `org-edit-src-find-region-and-lang'.
  668. The regular expression identifying the begin marker should end with a newline,
  669. and the regexp marking the end line should start with a newline, to make sure
  670. there are kept outside the narrowed region."
  671. :group 'org-edit-structure
  672. :type '(repeat
  673. (list
  674. (regexp :tag "begin regexp")
  675. (regexp :tag "end regexp")
  676. (choice :tag "language"
  677. (string :tag "specify")
  678. (integer :tag "from match group")
  679. (const :tag "from `lang' element")
  680. (const :tag "from `style' element")))))
  681. (defcustom org-coderef-label-format "(ref:%s)"
  682. "The default coderef format.
  683. This format string will be used to search for coderef labels in literal
  684. examples (EXAMPLE and SRC blocks). The format can be overwritten
  685. an individual literal example with the -f option, like
  686. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  687. ...
  688. #+END_SRC
  689. If you want to use this for HTML export, make sure that the format does
  690. not introduce special font-locking, and avoid the HTML special
  691. characters `<', `>', and `&'. The reason for this restriction is that
  692. the labels are searched for only after htmlize has done its job."
  693. :group 'org-edit-structure ; FIXME this is not in the right group
  694. :type 'string)
  695. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  696. "The mode that should be used to edit fixed-width regions.
  697. These are the regions where each line starts with a colon."
  698. :group 'org-edit-structure
  699. :type '(choice
  700. (const artist-mode)
  701. (const picture-mode)
  702. (const fundamental-mode)
  703. (function :tag "Other (specify)")))
  704. (defcustom org-goto-auto-isearch t
  705. "Non-nil means, typing characters in org-goto starts incremental search."
  706. :group 'org-edit-structure
  707. :type 'boolean)
  708. (defgroup org-sparse-trees nil
  709. "Options concerning sparse trees in Org-mode."
  710. :tag "Org Sparse Trees"
  711. :group 'org-structure)
  712. (defcustom org-highlight-sparse-tree-matches t
  713. "Non-nil means, highlight all matches that define a sparse tree.
  714. The highlights will automatically disappear the next time the buffer is
  715. changed by an edit command."
  716. :group 'org-sparse-trees
  717. :type 'boolean)
  718. (defcustom org-remove-highlights-with-change t
  719. "Non-nil means, any change to the buffer will remove temporary highlights.
  720. Such highlights are created by `org-occur' and `org-clock-display'.
  721. When nil, `C-c C-c needs to be used to get rid of the highlights.
  722. The highlights created by `org-preview-latex-fragment' always need
  723. `C-c C-c' to be removed."
  724. :group 'org-sparse-trees
  725. :group 'org-time
  726. :type 'boolean)
  727. (defcustom org-occur-hook '(org-first-headline-recenter)
  728. "Hook that is run after `org-occur' has constructed a sparse tree.
  729. This can be used to recenter the window to show as much of the structure
  730. as possible."
  731. :group 'org-sparse-trees
  732. :type 'hook)
  733. (defgroup org-imenu-and-speedbar nil
  734. "Options concerning imenu and speedbar in Org-mode."
  735. :tag "Org Imenu and Speedbar"
  736. :group 'org-structure)
  737. (defcustom org-imenu-depth 2
  738. "The maximum level for Imenu access to Org-mode headlines.
  739. This also applied for speedbar access."
  740. :group 'org-imenu-and-speedbar
  741. :type 'number)
  742. (defgroup org-table nil
  743. "Options concerning tables in Org-mode."
  744. :tag "Org Table"
  745. :group 'org)
  746. (defcustom org-enable-table-editor 'optimized
  747. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  748. When nil, such lines will be treated like ordinary lines.
  749. When equal to the symbol `optimized', the table editor will be optimized to
  750. do the following:
  751. - Automatic overwrite mode in front of whitespace in table fields.
  752. This makes the structure of the table stay in tact as long as the edited
  753. field does not exceed the column width.
  754. - Minimize the number of realigns. Normally, the table is aligned each time
  755. TAB or RET are pressed to move to another field. With optimization this
  756. happens only if changes to a field might have changed the column width.
  757. Optimization requires replacing the functions `self-insert-command',
  758. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  759. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  760. very good at guessing when a re-align will be necessary, but you can always
  761. force one with \\[org-ctrl-c-ctrl-c].
  762. If you would like to use the optimized version in Org-mode, but the
  763. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  764. This variable can be used to turn on and off the table editor during a session,
  765. but in order to toggle optimization, a restart is required.
  766. See also the variable `org-table-auto-blank-field'."
  767. :group 'org-table
  768. :type '(choice
  769. (const :tag "off" nil)
  770. (const :tag "on" t)
  771. (const :tag "on, optimized" optimized)))
  772. (defcustom org-table-tab-recognizes-table.el t
  773. "Non-nil means, TAB will automatically notice a table.el table.
  774. When it sees such a table, it moves point into it and - if necessary -
  775. calls `table-recognize-table'."
  776. :group 'org-table-editing
  777. :type 'boolean)
  778. (defgroup org-link nil
  779. "Options concerning links in Org-mode."
  780. :tag "Org Link"
  781. :group 'org)
  782. (defvar org-link-abbrev-alist-local nil
  783. "Buffer-local version of `org-link-abbrev-alist', which see.
  784. The value of this is taken from the #+LINK lines.")
  785. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  786. (defcustom org-link-abbrev-alist nil
  787. "Alist of link abbreviations.
  788. The car of each element is a string, to be replaced at the start of a link.
  789. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  790. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  791. [[linkkey:tag][description]]
  792. If REPLACE is a string, the tag will simply be appended to create the link.
  793. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  794. the placeholder \"%h\" will cause a url-encoded version of the tag to
  795. be inserted at that point (see the function `url-hexify-string').
  796. REPLACE may also be a function that will be called with the tag as the
  797. only argument to create the link, which should be returned as a string.
  798. See the manual for examples."
  799. :group 'org-link
  800. :type '(repeat
  801. (cons
  802. (string :tag "Protocol")
  803. (choice
  804. (string :tag "Format")
  805. (function)))))
  806. (defcustom org-descriptive-links t
  807. "Non-nil means, hide link part and only show description of bracket links.
  808. Bracket links are like [[link][description]]. This variable sets the initial
  809. state in new org-mode buffers. The setting can then be toggled on a
  810. per-buffer basis from the Org->Hyperlinks menu."
  811. :group 'org-link
  812. :type 'boolean)
  813. (defcustom org-link-file-path-type 'adaptive
  814. "How the path name in file links should be stored.
  815. Valid values are:
  816. relative Relative to the current directory, i.e. the directory of the file
  817. into which the link is being inserted.
  818. absolute Absolute path, if possible with ~ for home directory.
  819. noabbrev Absolute path, no abbreviation of home directory.
  820. adaptive Use relative path for files in the current directory and sub-
  821. directories of it. For other files, use an absolute path."
  822. :group 'org-link
  823. :type '(choice
  824. (const relative)
  825. (const absolute)
  826. (const noabbrev)
  827. (const adaptive)))
  828. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  829. "Types of links that should be activated in Org-mode files.
  830. This is a list of symbols, each leading to the activation of a certain link
  831. type. In principle, it does not hurt to turn on most link types - there may
  832. be a small gain when turning off unused link types. The types are:
  833. bracket The recommended [[link][description]] or [[link]] links with hiding.
  834. angular Links in angular brackets that may contain whitespace like
  835. <bbdb:Carsten Dominik>.
  836. plain Plain links in normal text, no whitespace, like http://google.com.
  837. radio Text that is matched by a radio target, see manual for details.
  838. tag Tag settings in a headline (link to tag search).
  839. date Time stamps (link to calendar).
  840. footnote Footnote labels.
  841. Changing this variable requires a restart of Emacs to become effective."
  842. :group 'org-link
  843. :type '(set :greedy t
  844. (const :tag "Double bracket links (new style)" bracket)
  845. (const :tag "Angular bracket links (old style)" angular)
  846. (const :tag "Plain text links" plain)
  847. (const :tag "Radio target matches" radio)
  848. (const :tag "Tags" tag)
  849. (const :tag "Timestamps" date)
  850. (const :tag "Footnotes" footnote)))
  851. (defcustom org-make-link-description-function nil
  852. "Function to use to generate link descriptions from links. If
  853. nil the link location will be used. This function must take two
  854. parameters; the first is the link and the second the description
  855. org-insert-link has generated, and should return the description
  856. to use."
  857. :group 'org-link
  858. :type 'function)
  859. (defgroup org-link-store nil
  860. "Options concerning storing links in Org-mode."
  861. :tag "Org Store Link"
  862. :group 'org-link)
  863. (defcustom org-email-link-description-format "Email %c: %.30s"
  864. "Format of the description part of a link to an email or usenet message.
  865. The following %-escapes will be replaced by corresponding information:
  866. %F full \"From\" field
  867. %f name, taken from \"From\" field, address if no name
  868. %T full \"To\" field
  869. %t first name in \"To\" field, address if no name
  870. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  871. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  872. %s subject
  873. %m message-id.
  874. You may use normal field width specification between the % and the letter.
  875. This is for example useful to limit the length of the subject.
  876. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  877. :group 'org-link-store
  878. :type 'string)
  879. (defcustom org-from-is-user-regexp
  880. (let (r1 r2)
  881. (when (and user-mail-address (not (string= user-mail-address "")))
  882. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  883. (when (and user-full-name (not (string= user-full-name "")))
  884. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  885. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  886. "Regexp matched against the \"From:\" header of an email or usenet message.
  887. It should match if the message is from the user him/herself."
  888. :group 'org-link-store
  889. :type 'regexp)
  890. (defcustom org-link-to-org-use-id 'create-if-interactive
  891. "Non-nil means, storing a link to an Org file will use entry IDs.
  892. Note that before this variable is even considered, org-id must be loaded,
  893. to please customize `org-modules' and turn it on.
  894. The variable can have the following values:
  895. t Create an ID if needed to make a link to the current entry.
  896. create-if-interactive
  897. If `org-store-link' is called directly (interactively, as a user
  898. command), do create an ID to support the link. But when doing the
  899. job for remember, only use the ID if it already exists. The
  900. purpose of this setting is to avoid proliferation of unwanted
  901. IDs, just because you happen to be in an Org file when you
  902. call `org-remember' that automatically and preemptively
  903. creates a link. If you do want to get an ID link in a remember
  904. template to an entry not having an ID, create it first by
  905. explicitly creating a link to it, using `C-c C-l' first.
  906. use-existing
  907. Use existing ID, do not create one.
  908. nil Never use an ID to make a link, instead link using a text search for
  909. the headline text."
  910. :group 'org-link-store
  911. :type '(choice
  912. (const :tag "Create ID to make link" t)
  913. (const :tag "Create if string link interactively"
  914. 'create-if-interactive)
  915. (const :tag "Only use existing" 'use-existing)
  916. (const :tag "Do not use ID to create link" nil)))
  917. (defcustom org-context-in-file-links t
  918. "Non-nil means, file links from `org-store-link' contain context.
  919. A search string will be added to the file name with :: as separator and
  920. used to find the context when the link is activated by the command
  921. `org-open-at-point'.
  922. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  923. negates this setting for the duration of the command."
  924. :group 'org-link-store
  925. :type 'boolean)
  926. (defcustom org-keep-stored-link-after-insertion nil
  927. "Non-nil means, keep link in list for entire session.
  928. The command `org-store-link' adds a link pointing to the current
  929. location to an internal list. These links accumulate during a session.
  930. The command `org-insert-link' can be used to insert links into any
  931. Org-mode file (offering completion for all stored links). When this
  932. option is nil, every link which has been inserted once using \\[org-insert-link]
  933. will be removed from the list, to make completing the unused links
  934. more efficient."
  935. :group 'org-link-store
  936. :type 'boolean)
  937. (defgroup org-link-follow nil
  938. "Options concerning following links in Org-mode."
  939. :tag "Org Follow Link"
  940. :group 'org-link)
  941. (defcustom org-link-translation-function nil
  942. "Function to translate links with different syntax to Org syntax.
  943. This can be used to translate links created for example by the Planner
  944. or emacs-wiki packages to Org syntax.
  945. The function must accept two parameters, a TYPE containing the link
  946. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  947. which is everything after the link protocol. It should return a cons
  948. with possibly modified values of type and path.
  949. Org contains a function for this, so if you set this variable to
  950. `org-translate-link-from-planner', you should be able follow many
  951. links created by planner."
  952. :group 'org-link-follow
  953. :type 'function)
  954. (defcustom org-follow-link-hook nil
  955. "Hook that is run after a link has been followed."
  956. :group 'org-link-follow
  957. :type 'hook)
  958. (defcustom org-tab-follows-link nil
  959. "Non-nil means, on links TAB will follow the link.
  960. Needs to be set before org.el is loaded."
  961. :group 'org-link-follow
  962. :type 'boolean)
  963. (defcustom org-return-follows-link nil
  964. "Non-nil means, on links RET will follow the link.
  965. Needs to be set before org.el is loaded."
  966. :group 'org-link-follow
  967. :type 'boolean)
  968. (defcustom org-mouse-1-follows-link
  969. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  970. "Non-nil means, mouse-1 on a link will follow the link.
  971. A longer mouse click will still set point. Does not work on XEmacs.
  972. Needs to be set before org.el is loaded."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-mark-ring-length 4
  976. "Number of different positions to be recorded in the ring
  977. Changing this requires a restart of Emacs to work correctly."
  978. :group 'org-link-follow
  979. :type 'integer)
  980. (defcustom org-link-frame-setup
  981. '((vm . vm-visit-folder-other-frame)
  982. (gnus . gnus-other-frame)
  983. (file . find-file-other-window))
  984. "Setup the frame configuration for following links.
  985. When following a link with Emacs, it may often be useful to display
  986. this link in another window or frame. This variable can be used to
  987. set this up for the different types of links.
  988. For VM, use any of
  989. `vm-visit-folder'
  990. `vm-visit-folder-other-frame'
  991. For Gnus, use any of
  992. `gnus'
  993. `gnus-other-frame'
  994. `org-gnus-no-new-news'
  995. For FILE, use any of
  996. `find-file'
  997. `find-file-other-window'
  998. `find-file-other-frame'
  999. For the calendar, use the variable `calendar-setup'.
  1000. For BBDB, it is currently only possible to display the matches in
  1001. another window."
  1002. :group 'org-link-follow
  1003. :type '(list
  1004. (cons (const vm)
  1005. (choice
  1006. (const vm-visit-folder)
  1007. (const vm-visit-folder-other-window)
  1008. (const vm-visit-folder-other-frame)))
  1009. (cons (const gnus)
  1010. (choice
  1011. (const gnus)
  1012. (const gnus-other-frame)
  1013. (const org-gnus-no-new-news)))
  1014. (cons (const file)
  1015. (choice
  1016. (const find-file)
  1017. (const find-file-other-window)
  1018. (const find-file-other-frame)))))
  1019. (defcustom org-display-internal-link-with-indirect-buffer nil
  1020. "Non-nil means, use indirect buffer to display infile links.
  1021. Activating internal links (from one location in a file to another location
  1022. in the same file) normally just jumps to the location. When the link is
  1023. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1024. another window. When this option is set, the other window actually displays
  1025. an indirect buffer clone of the current buffer, to avoid any visibility
  1026. changes to the current buffer."
  1027. :group 'org-link-follow
  1028. :type 'boolean)
  1029. (defcustom org-open-non-existing-files nil
  1030. "Non-nil means, `org-open-file' will open non-existing files.
  1031. When nil, an error will be generated."
  1032. :group 'org-link-follow
  1033. :type 'boolean)
  1034. (defcustom org-open-directory-means-index-dot-org nil
  1035. "Non-nil means, a link to a directory really means to index.org.
  1036. When nil, following a directory link will run dired or open a finder/explorer
  1037. window on that directory."
  1038. :group 'org-link-follow
  1039. :type 'boolean)
  1040. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1041. "Function and arguments to call for following mailto links.
  1042. This is a list with the first element being a lisp function, and the
  1043. remaining elements being arguments to the function. In string arguments,
  1044. %a will be replaced by the address, and %s will be replaced by the subject
  1045. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1046. :group 'org-link-follow
  1047. :type '(choice
  1048. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1049. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1050. (const :tag "message-mail" (message-mail "%a" "%s"))
  1051. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1052. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1053. "Non-nil means, ask for confirmation before executing shell links.
  1054. Shell links can be dangerous: just think about a link
  1055. [[shell:rm -rf ~/*][Google Search]]
  1056. This link would show up in your Org-mode document as \"Google Search\",
  1057. but really it would remove your entire home directory.
  1058. Therefore we advise against setting this variable to nil.
  1059. Just change it to `y-or-n-p' of you want to confirm with a
  1060. single keystroke rather than having to type \"yes\"."
  1061. :group 'org-link-follow
  1062. :type '(choice
  1063. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1064. (const :tag "with y-or-n (faster)" y-or-n-p)
  1065. (const :tag "no confirmation (dangerous)" nil)))
  1066. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1067. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1068. Elisp links can be dangerous: just think about a link
  1069. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1070. This link would show up in your Org-mode document as \"Google Search\",
  1071. but really it would remove your entire home directory.
  1072. Therefore we advise against setting this variable to nil.
  1073. Just change it to `y-or-n-p' of you want to confirm with a
  1074. single keystroke rather than having to type \"yes\"."
  1075. :group 'org-link-follow
  1076. :type '(choice
  1077. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1078. (const :tag "with y-or-n (faster)" y-or-n-p)
  1079. (const :tag "no confirmation (dangerous)" nil)))
  1080. (defconst org-file-apps-defaults-gnu
  1081. '((remote . emacs)
  1082. (system . mailcap)
  1083. (t . mailcap))
  1084. "Default file applications on a UNIX or GNU/Linux system.
  1085. See `org-file-apps'.")
  1086. (defconst org-file-apps-defaults-macosx
  1087. '((remote . emacs)
  1088. (t . "open %s")
  1089. (system . "open %s")
  1090. ("ps.gz" . "gv %s")
  1091. ("eps.gz" . "gv %s")
  1092. ("dvi" . "xdvi %s")
  1093. ("fig" . "xfig %s"))
  1094. "Default file applications on a MacOS X system.
  1095. The system \"open\" is known as a default, but we use X11 applications
  1096. for some files for which the OS does not have a good default.
  1097. See `org-file-apps'.")
  1098. (defconst org-file-apps-defaults-windowsnt
  1099. (list
  1100. '(remote . emacs)
  1101. (cons t
  1102. (list (if (featurep 'xemacs)
  1103. 'mswindows-shell-execute
  1104. 'w32-shell-execute)
  1105. "open" 'file))
  1106. (cons 'system
  1107. (list (if (featurep 'xemacs)
  1108. 'mswindows-shell-execute
  1109. 'w32-shell-execute)
  1110. "open" 'file)))
  1111. "Default file applications on a Windows NT system.
  1112. The system \"open\" is used for most files.
  1113. See `org-file-apps'.")
  1114. (defcustom org-file-apps
  1115. '(
  1116. (auto-mode . emacs)
  1117. ("\\.x?html?\\'" . default)
  1118. ("\\.pdf\\'" . default)
  1119. )
  1120. "External applications for opening `file:path' items in a document.
  1121. Org-mode uses system defaults for different file types, but
  1122. you can use this variable to set the application for a given file
  1123. extension. The entries in this list are cons cells where the car identifies
  1124. files and the cdr the corresponding command. Possible values for the
  1125. file identifier are
  1126. \"regex\" Regular expression matched against the file name. For backward
  1127. compatibility, this can also be a string with only alphanumeric
  1128. characters, which is then interpreted as an extension.
  1129. `directory' Matches a directory
  1130. `remote' Matches a remote file, accessible through tramp or efs.
  1131. Remote files most likely should be visited through Emacs
  1132. because external applications cannot handle such paths.
  1133. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1134. so all files Emacs knows how to handle. Using this with
  1135. command `emacs' will open most files in Emacs. Beware that this
  1136. will also open html files inside Emacs, unless you add
  1137. (\"html\" . default) to the list as well.
  1138. t Default for files not matched by any of the other options.
  1139. `system' The system command to open files, like `open' on Windows
  1140. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1141. that will be selected if you call `C-c C-o' with a double
  1142. `C-u C-u' prefix.
  1143. Possible values for the command are:
  1144. `emacs' The file will be visited by the current Emacs process.
  1145. `default' Use the default application for this file type, which is the
  1146. association for t in the list, most likely in the system-specific
  1147. part.
  1148. This can be used to overrule an unwanted setting in the
  1149. system-specific variable.
  1150. `system' Use the system command for opening files, like \"open\".
  1151. This command is specified by the entry whose car is `system'.
  1152. Most likely, the system-specific version of this variable
  1153. does define this command, but you can overrule/replace it
  1154. here.
  1155. string A command to be executed by a shell; %s will be replaced
  1156. by the path to the file.
  1157. sexp A Lisp form which will be evaluated. The file path will
  1158. be available in the Lisp variable `file'.
  1159. For more examples, see the system specific constants
  1160. `org-file-apps-defaults-macosx'
  1161. `org-file-apps-defaults-windowsnt'
  1162. `org-file-apps-defaults-gnu'."
  1163. :group 'org-link-follow
  1164. :type '(repeat
  1165. (cons (choice :value ""
  1166. (string :tag "Extension")
  1167. (const :tag "System command to open files" system)
  1168. (const :tag "Default for unrecognized files" t)
  1169. (const :tag "Remote file" remote)
  1170. (const :tag "Links to a directory" directory)
  1171. (const :tag "Any files that have Emacs modes"
  1172. auto-mode))
  1173. (choice :value ""
  1174. (const :tag "Visit with Emacs" emacs)
  1175. (const :tag "Use default" default)
  1176. (const :tag "Use the system command" system)
  1177. (string :tag "Command")
  1178. (sexp :tag "Lisp form")))))
  1179. (defgroup org-refile nil
  1180. "Options concerning refiling entries in Org-mode."
  1181. :tag "Org Refile"
  1182. :group 'org)
  1183. (defcustom org-directory "~/org"
  1184. "Directory with org files.
  1185. This directory will be used as default to prompt for org files.
  1186. Used by the hooks for remember.el."
  1187. :group 'org-refile
  1188. :group 'org-remember
  1189. :type 'directory)
  1190. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1191. "Default target for storing notes.
  1192. Used by the hooks for remember.el. This can be a string, or nil to mean
  1193. the value of `remember-data-file'.
  1194. You can set this on a per-template basis with the variable
  1195. `org-remember-templates'."
  1196. :group 'org-refile
  1197. :group 'org-remember
  1198. :type '(choice
  1199. (const :tag "Default from remember-data-file" nil)
  1200. file))
  1201. (defcustom org-goto-interface 'outline
  1202. "The default interface to be used for `org-goto'.
  1203. Allowed values are:
  1204. outline The interface shows an outline of the relevant file
  1205. and the correct heading is found by moving through
  1206. the outline or by searching with incremental search.
  1207. outline-path-completion Headlines in the current buffer are offered via
  1208. completion. This is the interface also used by
  1209. the refile command."
  1210. :group 'org-refile
  1211. :type '(choice
  1212. (const :tag "Outline" outline)
  1213. (const :tag "Outline-path-completion" outline-path-completion)))
  1214. (defcustom org-goto-max-level 5
  1215. "Maximum level to be considered when running org-goto with refile interface."
  1216. :group 'org-refile
  1217. :type 'number)
  1218. (defcustom org-reverse-note-order nil
  1219. "Non-nil means, store new notes at the beginning of a file or entry.
  1220. When nil, new notes will be filed to the end of a file or entry.
  1221. This can also be a list with cons cells of regular expressions that
  1222. are matched against file names, and values."
  1223. :group 'org-remember
  1224. :group 'org-refile
  1225. :type '(choice
  1226. (const :tag "Reverse always" t)
  1227. (const :tag "Reverse never" nil)
  1228. (repeat :tag "By file name regexp"
  1229. (cons regexp boolean))))
  1230. (defcustom org-refile-targets nil
  1231. "Targets for refiling entries with \\[org-refile].
  1232. This is list of cons cells. Each cell contains:
  1233. - a specification of the files to be considered, either a list of files,
  1234. or a symbol whose function or variable value will be used to retrieve
  1235. a file name or a list of file names. If you use `org-agenda-files' for
  1236. that, all agenda files will be scanned for targets. Nil means, consider
  1237. headings in the current buffer.
  1238. - A specification of how to select find candidate refile targets. This
  1239. may be any of
  1240. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1241. This tag has to be present in all target headlines, inheritance will
  1242. not be considered.
  1243. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1244. todo keyword.
  1245. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1246. headlines that are refiling targets.
  1247. - a cons cell (:level . N). Any headline of level N is considered a target.
  1248. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1249. When this variable is nil, all top-level headlines in the current buffer
  1250. are used, equivalent to the value `((nil . (:level . 1))'."
  1251. :group 'org-refile
  1252. :type '(repeat
  1253. (cons
  1254. (choice :value org-agenda-files
  1255. (const :tag "All agenda files" org-agenda-files)
  1256. (const :tag "Current buffer" nil)
  1257. (function) (variable) (file))
  1258. (choice :tag "Identify target headline by"
  1259. (cons :tag "Specific tag" (const :value :tag) (string))
  1260. (cons :tag "TODO keyword" (const :value :todo) (string))
  1261. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1262. (cons :tag "Level number" (const :value :level) (integer))
  1263. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1264. (defcustom org-refile-use-outline-path nil
  1265. "Non-nil means, provide refile targets as paths.
  1266. So a level 3 headline will be available as level1/level2/level3.
  1267. When the value is `file', also include the file name (without directory)
  1268. into the path. When `full-file-path', include the full file path."
  1269. :group 'org-refile
  1270. :type '(choice
  1271. (const :tag "Not" nil)
  1272. (const :tag "Yes" t)
  1273. (const :tag "Start with file name" file)
  1274. (const :tag "Start with full file path" full-file-path)))
  1275. (defcustom org-outline-path-complete-in-steps t
  1276. "Non-nil means, complete the outline path in hierarchical steps.
  1277. When Org-mode uses the refile interface to select an outline path
  1278. \(see variable `org-refile-use-outline-path'), the completion of
  1279. the path can be done is a single go, or if can be done in steps down
  1280. the headline hierarchy. Going in steps is probably the best if you
  1281. do not use a special completion package like `ido' or `icicles'.
  1282. However, when using these packages, going in one step can be very
  1283. fast, while still showing the whole path to the entry."
  1284. :group 'org-refile
  1285. :type 'boolean)
  1286. (defgroup org-todo nil
  1287. "Options concerning TODO items in Org-mode."
  1288. :tag "Org TODO"
  1289. :group 'org)
  1290. (defgroup org-progress nil
  1291. "Options concerning Progress logging in Org-mode."
  1292. :tag "Org Progress"
  1293. :group 'org-time)
  1294. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1295. "List of TODO entry keyword sequences and their interpretation.
  1296. \\<org-mode-map>This is a list of sequences.
  1297. Each sequence starts with a symbol, either `sequence' or `type',
  1298. indicating if the keywords should be interpreted as a sequence of
  1299. action steps, or as different types of TODO items. The first
  1300. keywords are states requiring action - these states will select a headline
  1301. for inclusion into the global TODO list Org-mode produces. If one of
  1302. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1303. signify that no further action is necessary. If \"|\" is not found,
  1304. the last keyword is treated as the only DONE state of the sequence.
  1305. The command \\[org-todo] cycles an entry through these states, and one
  1306. additional state where no keyword is present. For details about this
  1307. cycling, see the manual.
  1308. TODO keywords and interpretation can also be set on a per-file basis with
  1309. the special #+SEQ_TODO and #+TYP_TODO lines.
  1310. Each keyword can optionally specify a character for fast state selection
  1311. \(in combination with the variable `org-use-fast-todo-selection')
  1312. and specifiers for state change logging, using the same syntax
  1313. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1314. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1315. indicates to record a time stamp each time this state is selected.
  1316. Each keyword may also specify if a timestamp or a note should be
  1317. recorded when entering or leaving the state, by adding additional
  1318. characters in the parenthesis after the keyword. This looks like this:
  1319. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1320. record only the time of the state change. With X and Y being either
  1321. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1322. Y when leaving the state if and only if the *target* state does not
  1323. define X. You may omit any of the fast-selection key or X or /Y,
  1324. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1325. For backward compatibility, this variable may also be just a list
  1326. of keywords - in this case the interpretation (sequence or type) will be
  1327. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1328. :group 'org-todo
  1329. :group 'org-keywords
  1330. :type '(choice
  1331. (repeat :tag "Old syntax, just keywords"
  1332. (string :tag "Keyword"))
  1333. (repeat :tag "New syntax"
  1334. (cons
  1335. (choice
  1336. :tag "Interpretation"
  1337. (const :tag "Sequence (cycling hits every state)" sequence)
  1338. (const :tag "Type (cycling directly to DONE)" type))
  1339. (repeat
  1340. (string :tag "Keyword"))))))
  1341. (defvar org-todo-keywords-1 nil
  1342. "All TODO and DONE keywords active in a buffer.")
  1343. (make-variable-buffer-local 'org-todo-keywords-1)
  1344. (defvar org-todo-keywords-for-agenda nil)
  1345. (defvar org-done-keywords-for-agenda nil)
  1346. (defvar org-todo-keyword-alist-for-agenda nil)
  1347. (defvar org-tag-alist-for-agenda nil)
  1348. (defvar org-agenda-contributing-files nil)
  1349. (defvar org-not-done-keywords nil)
  1350. (make-variable-buffer-local 'org-not-done-keywords)
  1351. (defvar org-done-keywords nil)
  1352. (make-variable-buffer-local 'org-done-keywords)
  1353. (defvar org-todo-heads nil)
  1354. (make-variable-buffer-local 'org-todo-heads)
  1355. (defvar org-todo-sets nil)
  1356. (make-variable-buffer-local 'org-todo-sets)
  1357. (defvar org-todo-log-states nil)
  1358. (make-variable-buffer-local 'org-todo-log-states)
  1359. (defvar org-todo-kwd-alist nil)
  1360. (make-variable-buffer-local 'org-todo-kwd-alist)
  1361. (defvar org-todo-key-alist nil)
  1362. (make-variable-buffer-local 'org-todo-key-alist)
  1363. (defvar org-todo-key-trigger nil)
  1364. (make-variable-buffer-local 'org-todo-key-trigger)
  1365. (defcustom org-todo-interpretation 'sequence
  1366. "Controls how TODO keywords are interpreted.
  1367. This variable is in principle obsolete and is only used for
  1368. backward compatibility, if the interpretation of todo keywords is
  1369. not given already in `org-todo-keywords'. See that variable for
  1370. more information."
  1371. :group 'org-todo
  1372. :group 'org-keywords
  1373. :type '(choice (const sequence)
  1374. (const type)))
  1375. (defcustom org-use-fast-todo-selection 'prefix
  1376. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1377. This variable describes if and under what circumstances the cycling
  1378. mechanism for TODO keywords will be replaced by a single-key, direct
  1379. selection scheme.
  1380. When nil, fast selection is never used.
  1381. When the symbol `prefix', it will be used when `org-todo' is called with
  1382. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1383. in an agenda buffer.
  1384. When t, fast selection is used by default. In this case, the prefix
  1385. argument forces cycling instead.
  1386. In all cases, the special interface is only used if access keys have actually
  1387. been assigned by the user, i.e. if keywords in the configuration are followed
  1388. by a letter in parenthesis, like TODO(t)."
  1389. :group 'org-todo
  1390. :type '(choice
  1391. (const :tag "Never" nil)
  1392. (const :tag "By default" t)
  1393. (const :tag "Only with C-u C-c C-t" prefix)))
  1394. (defcustom org-provide-todo-statistics t
  1395. "Non-nil means, update todo statistics after insert and toggle.
  1396. When this is set, todo statistics is updated in the parent of the current
  1397. entry each time a todo state is changed."
  1398. :group 'org-todo
  1399. :type 'boolean)
  1400. (defcustom org-after-todo-state-change-hook nil
  1401. "Hook which is run after the state of a TODO item was changed.
  1402. The new state (a string with a TODO keyword, or nil) is available in the
  1403. Lisp variable `state'."
  1404. :group 'org-todo
  1405. :type 'hook)
  1406. (defcustom org-todo-state-tags-triggers nil
  1407. "Tag changes that should be triggered by TODO state changes.
  1408. This is a list. Each entry is
  1409. (state-change (tag . flag) .......)
  1410. State-change can be a string with a state, and empty string to indicate the
  1411. state that has no TODO keyword, or it can be one of the symbols `todo'
  1412. or `done', meaning any not-done or done state, respectively."
  1413. :group 'org-todo
  1414. :group 'org-tags
  1415. :type '(repeat
  1416. (cons (choice :tag "When changing to"
  1417. (const :tag "Not-done state" todo)
  1418. (const :tag "Done state" done)
  1419. (string :tag "State"))
  1420. (repeat
  1421. (cons :tag "Tag action"
  1422. (string :tag "Tag")
  1423. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1424. (defcustom org-log-done nil
  1425. "Information to record when a task moves to the DONE state.
  1426. Possible values are:
  1427. nil Don't add anything, just change the keyword
  1428. time Add a time stamp to the task
  1429. note Prompt a closing note and add it with template `org-log-note-headings'
  1430. This option can also be set with on a per-file-basis with
  1431. #+STARTUP: nologdone
  1432. #+STARTUP: logdone
  1433. #+STARTUP: lognotedone
  1434. You can have local logging settings for a subtree by setting the LOGGING
  1435. property to one or more of these keywords."
  1436. :group 'org-todo
  1437. :group 'org-progress
  1438. :type '(choice
  1439. (const :tag "No logging" nil)
  1440. (const :tag "Record CLOSED timestamp" time)
  1441. (const :tag "Record CLOSED timestamp with closing note." note)))
  1442. ;; Normalize old uses of org-log-done.
  1443. (cond
  1444. ((eq org-log-done t) (setq org-log-done 'time))
  1445. ((and (listp org-log-done) (memq 'done org-log-done))
  1446. (setq org-log-done 'note)))
  1447. (defcustom org-log-note-clock-out nil
  1448. "Non-nil means, record a note when clocking out of an item.
  1449. This can also be configured on a per-file basis by adding one of
  1450. the following lines anywhere in the buffer:
  1451. #+STARTUP: lognoteclock-out
  1452. #+STARTUP: nolognoteclock-out"
  1453. :group 'org-todo
  1454. :group 'org-progress
  1455. :type 'boolean)
  1456. (defcustom org-log-done-with-time t
  1457. "Non-nil means, the CLOSED time stamp will contain date and time.
  1458. When nil, only the date will be recorded."
  1459. :group 'org-progress
  1460. :type 'boolean)
  1461. (defcustom org-log-note-headings
  1462. '((done . "CLOSING NOTE %t")
  1463. (state . "State %-12s %t")
  1464. (note . "Note taken on %t")
  1465. (clock-out . ""))
  1466. "Headings for notes added to entries.
  1467. The value is an alist, with the car being a symbol indicating the note
  1468. context, and the cdr is the heading to be used. The heading may also be the
  1469. empty string.
  1470. %t in the heading will be replaced by a time stamp.
  1471. %s will be replaced by the new TODO state, in double quotes.
  1472. %u will be replaced by the user name.
  1473. %U will be replaced by the full user name."
  1474. :group 'org-todo
  1475. :group 'org-progress
  1476. :type '(list :greedy t
  1477. (cons (const :tag "Heading when closing an item" done) string)
  1478. (cons (const :tag
  1479. "Heading when changing todo state (todo sequence only)"
  1480. state) string)
  1481. (cons (const :tag "Heading when just taking a note" note) string)
  1482. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1483. (unless (assq 'note org-log-note-headings)
  1484. (push '(note . "%t") org-log-note-headings))
  1485. (defcustom org-log-state-notes-insert-after-drawers nil
  1486. "Non-nil means, insert state change notes after any drawers in entry.
  1487. Only the drawers that *immediately* follow the headline and the
  1488. deadline/scheduled line are skipped.
  1489. When nil, insert notes right after the heading and perhaps the line
  1490. with deadline/scheduling if present."
  1491. :group 'org-todo
  1492. :group 'org-progress
  1493. :type 'boolean)
  1494. (defcustom org-log-states-order-reversed t
  1495. "Non-nil means, the latest state change note will be directly after heading.
  1496. When nil, the notes will be orderer according to time."
  1497. :group 'org-todo
  1498. :group 'org-progress
  1499. :type 'boolean)
  1500. (defcustom org-log-repeat 'time
  1501. "Non-nil means, record moving through the DONE state when triggering repeat.
  1502. An auto-repeating tasks is immediately switched back to TODO when marked
  1503. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1504. the TODO keyword definition, or recording a closing note by setting
  1505. `org-log-done', there will be no record of the task moving through DONE.
  1506. This variable forces taking a note anyway. Possible values are:
  1507. nil Don't force a record
  1508. time Record a time stamp
  1509. note Record a note
  1510. This option can also be set with on a per-file-basis with
  1511. #+STARTUP: logrepeat
  1512. #+STARTUP: lognoterepeat
  1513. #+STARTUP: nologrepeat
  1514. You can have local logging settings for a subtree by setting the LOGGING
  1515. property to one or more of these keywords."
  1516. :group 'org-todo
  1517. :group 'org-progress
  1518. :type '(choice
  1519. (const :tag "Don't force a record" nil)
  1520. (const :tag "Force recording the DONE state" time)
  1521. (const :tag "Force recording a note with the DONE state" note)))
  1522. (defgroup org-priorities nil
  1523. "Priorities in Org-mode."
  1524. :tag "Org Priorities"
  1525. :group 'org-todo)
  1526. (defcustom org-highest-priority ?A
  1527. "The highest priority of TODO items. A character like ?A, ?B etc.
  1528. Must have a smaller ASCII number than `org-lowest-priority'."
  1529. :group 'org-priorities
  1530. :type 'character)
  1531. (defcustom org-lowest-priority ?C
  1532. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1533. Must have a larger ASCII number than `org-highest-priority'."
  1534. :group 'org-priorities
  1535. :type 'character)
  1536. (defcustom org-default-priority ?B
  1537. "The default priority of TODO items.
  1538. This is the priority an item get if no explicit priority is given."
  1539. :group 'org-priorities
  1540. :type 'character)
  1541. (defcustom org-priority-start-cycle-with-default t
  1542. "Non-nil means, start with default priority when starting to cycle.
  1543. When this is nil, the first step in the cycle will be (depending on the
  1544. command used) one higher or lower that the default priority."
  1545. :group 'org-priorities
  1546. :type 'boolean)
  1547. (defgroup org-time nil
  1548. "Options concerning time stamps and deadlines in Org-mode."
  1549. :tag "Org Time"
  1550. :group 'org)
  1551. (defcustom org-insert-labeled-timestamps-at-point nil
  1552. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1553. When nil, these labeled time stamps are forces into the second line of an
  1554. entry, just after the headline. When scheduling from the global TODO list,
  1555. the time stamp will always be forced into the second line."
  1556. :group 'org-time
  1557. :type 'boolean)
  1558. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1559. "Formats for `format-time-string' which are used for time stamps.
  1560. It is not recommended to change this constant.")
  1561. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1562. "Number of minutes to round time stamps to.
  1563. These are two values, the first applies when first creating a time stamp.
  1564. The second applies when changing it with the commands `S-up' and `S-down'.
  1565. When changing the time stamp, this means that it will change in steps
  1566. of N minutes, as given by the second value.
  1567. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1568. numbers should be factors of 60, so for example 5, 10, 15.
  1569. When this is larger than 1, you can still force an exact time-stamp by using
  1570. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1571. and by using a prefix arg to `S-up/down' to specify the exact number
  1572. of minutes to shift."
  1573. :group 'org-time
  1574. :get '(lambda (var) ; Make sure all entries have 5 elements
  1575. (if (integerp (default-value var))
  1576. (list (default-value var) 5)
  1577. (default-value var)))
  1578. :type '(list
  1579. (integer :tag "when inserting times")
  1580. (integer :tag "when modifying times")))
  1581. ;; Normalize old customizations of this variable.
  1582. (when (integerp org-time-stamp-rounding-minutes)
  1583. (setq org-time-stamp-rounding-minutes
  1584. (list org-time-stamp-rounding-minutes
  1585. org-time-stamp-rounding-minutes)))
  1586. (defcustom org-display-custom-times nil
  1587. "Non-nil means, overlay custom formats over all time stamps.
  1588. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1589. To turn this on on a per-file basis, insert anywhere in the file:
  1590. #+STARTUP: customtime"
  1591. :group 'org-time
  1592. :set 'set-default
  1593. :type 'sexp)
  1594. (make-variable-buffer-local 'org-display-custom-times)
  1595. (defcustom org-time-stamp-custom-formats
  1596. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1597. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1598. These are overlayed over the default ISO format if the variable
  1599. `org-display-custom-times' is set. Time like %H:%M should be at the
  1600. end of the second format."
  1601. :group 'org-time
  1602. :type 'sexp)
  1603. (defun org-time-stamp-format (&optional long inactive)
  1604. "Get the right format for a time string."
  1605. (let ((f (if long (cdr org-time-stamp-formats)
  1606. (car org-time-stamp-formats))))
  1607. (if inactive
  1608. (concat "[" (substring f 1 -1) "]")
  1609. f)))
  1610. (defcustom org-time-clocksum-format "%d:%02d"
  1611. "The format string used when creating CLOCKSUM lines, or when
  1612. org-mode generates a time duration."
  1613. :group 'org-time
  1614. :type 'string)
  1615. (defcustom org-deadline-warning-days 14
  1616. "No. of days before expiration during which a deadline becomes active.
  1617. This variable governs the display in sparse trees and in the agenda.
  1618. When 0 or negative, it means use this number (the absolute value of it)
  1619. even if a deadline has a different individual lead time specified."
  1620. :group 'org-time
  1621. :group 'org-agenda-daily/weekly
  1622. :type 'number)
  1623. (defcustom org-read-date-prefer-future t
  1624. "Non-nil means, assume future for incomplete date input from user.
  1625. This affects the following situations:
  1626. 1. The user gives a day, but no month.
  1627. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1628. read this as the third of *next* month. However, if you enter \"17\",
  1629. it will be considered as *this* month.
  1630. 2. The user gives a month but not a year.
  1631. For example, if it is april and you enter \"feb 2\", this will be read
  1632. as feb 2, *next* year. \"May 5\", however, will be this year.
  1633. Currently this does not work for ISO week specifications.
  1634. When this option is nil, the current month and year will always be used
  1635. as defaults."
  1636. :group 'org-time
  1637. :type 'boolean)
  1638. (defcustom org-read-date-display-live t
  1639. "Non-nil means, display current interpretation of date prompt live.
  1640. This display will be in an overlay, in the minibuffer."
  1641. :group 'org-time
  1642. :type 'boolean)
  1643. (defcustom org-read-date-popup-calendar t
  1644. "Non-nil means, pop up a calendar when prompting for a date.
  1645. In the calendar, the date can be selected with mouse-1. However, the
  1646. minibuffer will also be active, and you can simply enter the date as well.
  1647. When nil, only the minibuffer will be available."
  1648. :group 'org-time
  1649. :type 'boolean)
  1650. (if (fboundp 'defvaralias)
  1651. (defvaralias 'org-popup-calendar-for-date-prompt
  1652. 'org-read-date-popup-calendar))
  1653. (defcustom org-extend-today-until 0
  1654. "The hour when your day really ends. Must be an integer.
  1655. This has influence for the following applications:
  1656. - When switching the agenda to \"today\". It it is still earlier than
  1657. the time given here, the day recognized as TODAY is actually yesterday.
  1658. - When a date is read from the user and it is still before the time given
  1659. here, the current date and time will be assumed to be yesterday, 23:59.
  1660. Also, timestamps inserted in remember templates follow this rule.
  1661. IMPORTANT: This is a feature whose implementation is and likely will
  1662. remain incomplete. Really, it is only here because past midnight seems to
  1663. be the favorite working time of John Wiegley :-)"
  1664. :group 'org-time
  1665. :type 'number)
  1666. (defcustom org-edit-timestamp-down-means-later nil
  1667. "Non-nil means, S-down will increase the time in a time stamp.
  1668. When nil, S-up will increase."
  1669. :group 'org-time
  1670. :type 'boolean)
  1671. (defcustom org-calendar-follow-timestamp-change t
  1672. "Non-nil means, make the calendar window follow timestamp changes.
  1673. When a timestamp is modified and the calendar window is visible, it will be
  1674. moved to the new date."
  1675. :group 'org-time
  1676. :type 'boolean)
  1677. (defgroup org-tags nil
  1678. "Options concerning tags in Org-mode."
  1679. :tag "Org Tags"
  1680. :group 'org)
  1681. (defcustom org-tag-alist nil
  1682. "List of tags allowed in Org-mode files.
  1683. When this list is nil, Org-mode will base TAG input on what is already in the
  1684. buffer.
  1685. The value of this variable is an alist, the car of each entry must be a
  1686. keyword as a string, the cdr may be a character that is used to select
  1687. that tag through the fast-tag-selection interface.
  1688. See the manual for details."
  1689. :group 'org-tags
  1690. :type '(repeat
  1691. (choice
  1692. (cons (string :tag "Tag name")
  1693. (character :tag "Access char"))
  1694. (const :tag "Start radio group" (:startgroup))
  1695. (const :tag "End radio group" (:endgroup)))))
  1696. (defvar org-file-tags nil
  1697. "List of tags that can be inherited by all entries in the file.
  1698. The tags will be inherited if the variable `org-use-tag-inheritance'
  1699. says they should be.
  1700. This variable is populated from #+TAG lines.")
  1701. (defcustom org-use-fast-tag-selection 'auto
  1702. "Non-nil means, use fast tag selection scheme.
  1703. This is a special interface to select and deselect tags with single keys.
  1704. When nil, fast selection is never used.
  1705. When the symbol `auto', fast selection is used if and only if selection
  1706. characters for tags have been configured, either through the variable
  1707. `org-tag-alist' or through a #+TAGS line in the buffer.
  1708. When t, fast selection is always used and selection keys are assigned
  1709. automatically if necessary."
  1710. :group 'org-tags
  1711. :type '(choice
  1712. (const :tag "Always" t)
  1713. (const :tag "Never" nil)
  1714. (const :tag "When selection characters are configured" 'auto)))
  1715. (defcustom org-fast-tag-selection-single-key nil
  1716. "Non-nil means, fast tag selection exits after first change.
  1717. When nil, you have to press RET to exit it.
  1718. During fast tag selection, you can toggle this flag with `C-c'.
  1719. This variable can also have the value `expert'. In this case, the window
  1720. displaying the tags menu is not even shown, until you press C-c again."
  1721. :group 'org-tags
  1722. :type '(choice
  1723. (const :tag "No" nil)
  1724. (const :tag "Yes" t)
  1725. (const :tag "Expert" expert)))
  1726. (defvar org-fast-tag-selection-include-todo nil
  1727. "Non-nil means, fast tags selection interface will also offer TODO states.
  1728. This is an undocumented feature, you should not rely on it.")
  1729. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1730. "The column to which tags should be indented in a headline.
  1731. If this number is positive, it specifies the column. If it is negative,
  1732. it means that the tags should be flushright to that column. For example,
  1733. -80 works well for a normal 80 character screen."
  1734. :group 'org-tags
  1735. :type 'integer)
  1736. (defcustom org-auto-align-tags t
  1737. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1738. These operations change the length of a headline and therefore shift
  1739. the tags around. With this options turned on, after each such operation
  1740. the tags are again aligned to `org-tags-column'."
  1741. :group 'org-tags
  1742. :type 'boolean)
  1743. (defcustom org-use-tag-inheritance t
  1744. "Non-nil means, tags in levels apply also for sublevels.
  1745. When nil, only the tags directly given in a specific line apply there.
  1746. This may also be a list of tags that should be inherited, or a regexp that
  1747. matches tags that should be inherited. Additional control is possible
  1748. with the variable `org-tags-exclude-from-inheritance' which gives an
  1749. explicit list of tags to be excluded from inheritance., even if the value of
  1750. `org-use-tag-inheritance' would select it for inheritance.
  1751. If this option is t, a match early-on in a tree can lead to a large
  1752. number of matches in the subtree when constructing the agenda or creating
  1753. a sparse tree. If you only want to see the first match in a tree during
  1754. a search, check out the variable `org-tags-match-list-sublevels'."
  1755. :group 'org-tags
  1756. :type '(choice
  1757. (const :tag "Not" nil)
  1758. (const :tag "Always" t)
  1759. (repeat :tag "Specific tags" (string :tag "Tag"))
  1760. (regexp :tag "Tags matched by regexp")))
  1761. (defcustom org-tags-exclude-from-inheritance nil
  1762. "List of tags that should never be inherited.
  1763. This is a way to exclude a few tags from inheritance. For way to do
  1764. the opposite, to actively allow inheritance for selected tags,
  1765. see the variable `org-use-tag-inheritance'."
  1766. :group 'org-tags
  1767. :type '(repeat (string :tag "Tag")))
  1768. (defun org-tag-inherit-p (tag)
  1769. "Check if TAG is one that should be inherited."
  1770. (cond
  1771. ((member tag org-tags-exclude-from-inheritance) nil)
  1772. ((eq org-use-tag-inheritance t) t)
  1773. ((not org-use-tag-inheritance) nil)
  1774. ((stringp org-use-tag-inheritance)
  1775. (string-match org-use-tag-inheritance tag))
  1776. ((listp org-use-tag-inheritance)
  1777. (member tag org-use-tag-inheritance))
  1778. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1779. (defcustom org-tags-match-list-sublevels t
  1780. "Non-nil means list also sublevels of headlines matching tag search.
  1781. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1782. the sublevels of a headline matching a tag search often also match
  1783. the same search. Listing all of them can create very long lists.
  1784. Setting this variable to nil causes subtrees of a match to be skipped.
  1785. This option is off by default, because inheritance in on. If you turn
  1786. inheritance off, you very likely want to turn this option on.
  1787. As a special case, if the tag search is restricted to TODO items, the
  1788. value of this variable is ignored and sublevels are always checked, to
  1789. make sure all corresponding TODO items find their way into the list.
  1790. This variable is semi-obsolete and probably should always be true. It
  1791. is better to limit inheritance to certain tags using the variables
  1792. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1793. :group 'org-tags
  1794. :type 'boolean)
  1795. (defvar org-tags-history nil
  1796. "History of minibuffer reads for tags.")
  1797. (defvar org-last-tags-completion-table nil
  1798. "The last used completion table for tags.")
  1799. (defvar org-after-tags-change-hook nil
  1800. "Hook that is run after the tags in a line have changed.")
  1801. (defgroup org-properties nil
  1802. "Options concerning properties in Org-mode."
  1803. :tag "Org Properties"
  1804. :group 'org)
  1805. (defcustom org-property-format "%-10s %s"
  1806. "How property key/value pairs should be formatted by `indent-line'.
  1807. When `indent-line' hits a property definition, it will format the line
  1808. according to this format, mainly to make sure that the values are
  1809. lined-up with respect to each other."
  1810. :group 'org-properties
  1811. :type 'string)
  1812. (defcustom org-use-property-inheritance nil
  1813. "Non-nil means, properties apply also for sublevels.
  1814. This setting is chiefly used during property searches. Turning it on can
  1815. cause significant overhead when doing a search, which is why it is not
  1816. on by default.
  1817. When nil, only the properties directly given in the current entry count.
  1818. When t, every property is inherited. The value may also be a list of
  1819. properties that should have inheritance, or a regular expression matching
  1820. properties that should be inherited.
  1821. However, note that some special properties use inheritance under special
  1822. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1823. and the properties ending in \"_ALL\" when they are used as descriptor
  1824. for valid values of a property.
  1825. Note for programmers:
  1826. When querying an entry with `org-entry-get', you can control if inheritance
  1827. should be used. By default, `org-entry-get' looks only at the local
  1828. properties. You can request inheritance by setting the inherit argument
  1829. to t (to force inheritance) or to `selective' (to respect the setting
  1830. in this variable)."
  1831. :group 'org-properties
  1832. :type '(choice
  1833. (const :tag "Not" nil)
  1834. (const :tag "Always" t)
  1835. (repeat :tag "Specific properties" (string :tag "Property"))
  1836. (regexp :tag "Properties matched by regexp")))
  1837. (defun org-property-inherit-p (property)
  1838. "Check if PROPERTY is one that should be inherited."
  1839. (cond
  1840. ((eq org-use-property-inheritance t) t)
  1841. ((not org-use-property-inheritance) nil)
  1842. ((stringp org-use-property-inheritance)
  1843. (string-match org-use-property-inheritance property))
  1844. ((listp org-use-property-inheritance)
  1845. (member property org-use-property-inheritance))
  1846. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1847. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1848. "The default column format, if no other format has been defined.
  1849. This variable can be set on the per-file basis by inserting a line
  1850. #+COLUMNS: %25ITEM ....."
  1851. :group 'org-properties
  1852. :type 'string)
  1853. (defcustom org-columns-ellipses ".."
  1854. "The ellipses to be used when a field in column view is truncated.
  1855. When this is the empty string, as many characters as possible are shown,
  1856. but then there will be no visual indication that the field has been truncated.
  1857. When this is a string of length N, the last N characters of a truncated
  1858. field are replaced by this string. If the column is narrower than the
  1859. ellipses string, only part of the ellipses string will be shown."
  1860. :group 'org-properties
  1861. :type 'string)
  1862. (defcustom org-columns-modify-value-for-display-function nil
  1863. "Function that modifies values for display in column view.
  1864. For example, it can be used to cut out a certain part from a time stamp.
  1865. The function must take 2 arguments:
  1866. column-title The title of the column (*not* the property name)
  1867. value The value that should be modified.
  1868. The function should return the value that should be displayed,
  1869. or nil if the normal value should be used."
  1870. :group 'org-properties
  1871. :type 'function)
  1872. (defcustom org-effort-property "Effort"
  1873. "The property that is being used to keep track of effort estimates.
  1874. Effort estimates given in this property need to have the format H:MM."
  1875. :group 'org-properties
  1876. :group 'org-progress
  1877. :type '(string :tag "Property"))
  1878. (defconst org-global-properties-fixed
  1879. '(("VISIBILITY_ALL" . "folded children content all"))
  1880. "List of property/value pairs that can be inherited by any entry.
  1881. These are fixed values, for the preset properties.")
  1882. (defcustom org-global-properties nil
  1883. "List of property/value pairs that can be inherited by any entry.
  1884. You can set buffer-local values for the same purpose in the variable
  1885. `org-file-properties' this by adding lines like
  1886. #+PROPERTY: NAME VALUE"
  1887. :group 'org-properties
  1888. :type '(repeat
  1889. (cons (string :tag "Property")
  1890. (string :tag "Value"))))
  1891. (defvar org-file-properties nil
  1892. "List of property/value pairs that can be inherited by any entry.
  1893. Valid for the current buffer.
  1894. This variable is populated from #+PROPERTY lines.")
  1895. (make-variable-buffer-local 'org-file-properties)
  1896. (defgroup org-agenda nil
  1897. "Options concerning agenda views in Org-mode."
  1898. :tag "Org Agenda"
  1899. :group 'org)
  1900. (defvar org-category nil
  1901. "Variable used by org files to set a category for agenda display.
  1902. Such files should use a file variable to set it, for example
  1903. # -*- mode: org; org-category: \"ELisp\"
  1904. or contain a special line
  1905. #+CATEGORY: ELisp
  1906. If the file does not specify a category, then file's base name
  1907. is used instead.")
  1908. (make-variable-buffer-local 'org-category)
  1909. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1910. (defcustom org-agenda-files nil
  1911. "The files to be used for agenda display.
  1912. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1913. \\[org-remove-file]. You can also use customize to edit the list.
  1914. If an entry is a directory, all files in that directory that are matched by
  1915. `org-agenda-file-regexp' will be part of the file list.
  1916. If the value of the variable is not a list but a single file name, then
  1917. the list of agenda files is actually stored and maintained in that file, one
  1918. agenda file per line."
  1919. :group 'org-agenda
  1920. :type '(choice
  1921. (repeat :tag "List of files and directories" file)
  1922. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1923. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1924. "Regular expression to match files for `org-agenda-files'.
  1925. If any element in the list in that variable contains a directory instead
  1926. of a normal file, all files in that directory that are matched by this
  1927. regular expression will be included."
  1928. :group 'org-agenda
  1929. :type 'regexp)
  1930. (defcustom org-agenda-text-search-extra-files nil
  1931. "List of extra files to be searched by text search commands.
  1932. These files will be search in addition to the agenda files by the
  1933. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1934. Note that these files will only be searched for text search commands,
  1935. not for the other agenda views like todo lists, tag searches or the weekly
  1936. agenda. This variable is intended to list notes and possibly archive files
  1937. that should also be searched by these two commands.
  1938. In fact, if the first element in the list is the symbol `agenda-archives',
  1939. than all archive files of all agenda files will be added to the search
  1940. scope."
  1941. :group 'org-agenda
  1942. :type '(set :greedy t
  1943. (const :tag "Agenda Archives" agenda-archives)
  1944. (repeat :inline t (file))))
  1945. (if (fboundp 'defvaralias)
  1946. (defvaralias 'org-agenda-multi-occur-extra-files
  1947. 'org-agenda-text-search-extra-files))
  1948. (defcustom org-agenda-skip-unavailable-files nil
  1949. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1950. A nil value means to remove them, after a query, from the list."
  1951. :group 'org-agenda
  1952. :type 'boolean)
  1953. (defcustom org-calendar-to-agenda-key [?c]
  1954. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1955. The command `org-calendar-goto-agenda' will be bound to this key. The
  1956. default is the character `c' because then `c' can be used to switch back and
  1957. forth between agenda and calendar."
  1958. :group 'org-agenda
  1959. :type 'sexp)
  1960. (defcustom org-calendar-agenda-action-key [?k]
  1961. "The key to be installed in `calendar-mode-map' for agenda-action.
  1962. The command `org-agenda-action' will be bound to this key. The
  1963. default is the character `k' because we use the same key in the agenda."
  1964. :group 'org-agenda
  1965. :type 'sexp)
  1966. (eval-after-load "calendar"
  1967. '(progn
  1968. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1969. 'org-calendar-goto-agenda)
  1970. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1971. 'org-agenda-action)))
  1972. (defgroup org-latex nil
  1973. "Options for embedding LaTeX code into Org-mode."
  1974. :tag "Org LaTeX"
  1975. :group 'org)
  1976. (defcustom org-format-latex-options
  1977. '(:foreground default :background default :scale 1.0
  1978. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1979. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  1980. "Options for creating images from LaTeX fragments.
  1981. This is a property list with the following properties:
  1982. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1983. `default' means use the foreground of the default face.
  1984. :background the background color, or \"Transparent\".
  1985. `default' means use the background of the default face.
  1986. :scale a scaling factor for the size of the images.
  1987. :html-foreground, :html-background, :html-scale
  1988. the same numbers for HTML export.
  1989. :matchers a list indicating which matchers should be used to
  1990. find LaTeX fragments. Valid members of this list are:
  1991. \"begin\" find environments
  1992. \"$1\" find single characters surrounded by $.$
  1993. \"$\" find math expressions surrounded by $...$
  1994. \"$$\" find math expressions surrounded by $$....$$
  1995. \"\\(\" find math expressions surrounded by \\(...\\)
  1996. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1997. :group 'org-latex
  1998. :type 'plist)
  1999. (defcustom org-format-latex-header "\\documentclass{article}
  2000. \\usepackage{fullpage} % do not remove
  2001. \\usepackage{amssymb}
  2002. \\usepackage[usenames]{color}
  2003. \\usepackage{amsmath}
  2004. \\usepackage{latexsym}
  2005. \\usepackage[mathscr]{eucal}
  2006. \\pagestyle{empty} % do not remove"
  2007. "The document header used for processing LaTeX fragments."
  2008. :group 'org-latex
  2009. :type 'string)
  2010. (defgroup org-font-lock nil
  2011. "Font-lock settings for highlighting in Org-mode."
  2012. :tag "Org Font Lock"
  2013. :group 'org)
  2014. (defcustom org-level-color-stars-only nil
  2015. "Non-nil means fontify only the stars in each headline.
  2016. When nil, the entire headline is fontified.
  2017. Changing it requires restart of `font-lock-mode' to become effective
  2018. also in regions already fontified."
  2019. :group 'org-font-lock
  2020. :type 'boolean)
  2021. (defcustom org-hide-leading-stars nil
  2022. "Non-nil means, hide the first N-1 stars in a headline.
  2023. This works by using the face `org-hide' for these stars. This
  2024. face is white for a light background, and black for a dark
  2025. background. You may have to customize the face `org-hide' to
  2026. make this work.
  2027. Changing it requires restart of `font-lock-mode' to become effective
  2028. also in regions already fontified.
  2029. You may also set this on a per-file basis by adding one of the following
  2030. lines to the buffer:
  2031. #+STARTUP: hidestars
  2032. #+STARTUP: showstars"
  2033. :group 'org-font-lock
  2034. :type 'boolean)
  2035. (defcustom org-fontify-done-headline nil
  2036. "Non-nil means, change the face of a headline if it is marked DONE.
  2037. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2038. When this is non-nil, the headline after the keyword is set to the
  2039. `org-headline-done' as an additional indication."
  2040. :group 'org-font-lock
  2041. :type 'boolean)
  2042. (defcustom org-fontify-emphasized-text t
  2043. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2044. Changing this variable requires a restart of Emacs to take effect."
  2045. :group 'org-font-lock
  2046. :type 'boolean)
  2047. (defcustom org-highlight-latex-fragments-and-specials nil
  2048. "Non-nil means, fontify what is treated specially by the exporters."
  2049. :group 'org-font-lock
  2050. :type 'boolean)
  2051. (defcustom org-hide-emphasis-markers nil
  2052. "Non-nil mean font-lock should hide the emphasis marker characters."
  2053. :group 'org-font-lock
  2054. :type 'boolean)
  2055. (defvar org-emph-re nil
  2056. "Regular expression for matching emphasis.")
  2057. (defvar org-verbatim-re nil
  2058. "Regular expression for matching verbatim text.")
  2059. (defvar org-emphasis-regexp-components) ; defined just below
  2060. (defvar org-emphasis-alist) ; defined just below
  2061. (defun org-set-emph-re (var val)
  2062. "Set variable and compute the emphasis regular expression."
  2063. (set var val)
  2064. (when (and (boundp 'org-emphasis-alist)
  2065. (boundp 'org-emphasis-regexp-components)
  2066. org-emphasis-alist org-emphasis-regexp-components)
  2067. (let* ((e org-emphasis-regexp-components)
  2068. (pre (car e))
  2069. (post (nth 1 e))
  2070. (border (nth 2 e))
  2071. (body (nth 3 e))
  2072. (nl (nth 4 e))
  2073. (body1 (concat body "*?"))
  2074. (markers (mapconcat 'car org-emphasis-alist ""))
  2075. (vmarkers (mapconcat
  2076. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2077. org-emphasis-alist "")))
  2078. ;; make sure special characters appear at the right position in the class
  2079. (if (string-match "\\^" markers)
  2080. (setq markers (concat (replace-match "" t t markers) "^")))
  2081. (if (string-match "-" markers)
  2082. (setq markers (concat (replace-match "" t t markers) "-")))
  2083. (if (string-match "\\^" vmarkers)
  2084. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2085. (if (string-match "-" vmarkers)
  2086. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2087. (if (> nl 0)
  2088. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2089. (int-to-string nl) "\\}")))
  2090. ;; Make the regexp
  2091. (setq org-emph-re
  2092. (concat "\\([" pre "]\\|^\\)"
  2093. "\\("
  2094. "\\([" markers "]\\)"
  2095. "\\("
  2096. "[^" border "]\\|"
  2097. "[^" border "]"
  2098. body1
  2099. "[^" border "]"
  2100. "\\)"
  2101. "\\3\\)"
  2102. "\\([" post "]\\|$\\)"))
  2103. (setq org-verbatim-re
  2104. (concat "\\([" pre "]\\|^\\)"
  2105. "\\("
  2106. "\\([" vmarkers "]\\)"
  2107. "\\("
  2108. "[^" border "]\\|"
  2109. "[^" border "]"
  2110. body1
  2111. "[^" border "]"
  2112. "\\)"
  2113. "\\3\\)"
  2114. "\\([" post "]\\|$\\)")))))
  2115. (defcustom org-emphasis-regexp-components
  2116. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2117. "Components used to build the regular expression for emphasis.
  2118. This is a list with 6 entries. Terminology: In an emphasis string
  2119. like \" *strong word* \", we call the initial space PREMATCH, the final
  2120. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2121. and \"trong wor\" is the body. The different components in this variable
  2122. specify what is allowed/forbidden in each part:
  2123. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2124. post Chars allowed as postmatch. End of line will be allowed too.
  2125. border The chars *forbidden* as border characters.
  2126. body-regexp A regexp like \".\" to match a body character. Don't use
  2127. non-shy groups here, and don't allow newline here.
  2128. newline The maximum number of newlines allowed in an emphasis exp.
  2129. Use customize to modify this, or restart Emacs after changing it."
  2130. :group 'org-font-lock
  2131. :set 'org-set-emph-re
  2132. :type '(list
  2133. (sexp :tag "Allowed chars in pre ")
  2134. (sexp :tag "Allowed chars in post ")
  2135. (sexp :tag "Forbidden chars in border ")
  2136. (sexp :tag "Regexp for body ")
  2137. (integer :tag "number of newlines allowed")
  2138. (option (boolean :tag "Please ignore this button"))))
  2139. (defcustom org-emphasis-alist
  2140. `(("*" bold "<b>" "</b>")
  2141. ("/" italic "<i>" "</i>")
  2142. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2143. ("=" org-code "<code>" "</code>" verbatim)
  2144. ("~" org-verbatim "<code>" "</code>" verbatim)
  2145. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2146. "<del>" "</del>")
  2147. )
  2148. "Special syntax for emphasized text.
  2149. Text starting and ending with a special character will be emphasized, for
  2150. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2151. characters, the face to be used by font-lock for highlighting in Org-mode
  2152. Emacs buffers, and the HTML tags to be used for this.
  2153. Use customize to modify this, or restart Emacs after changing it."
  2154. :group 'org-font-lock
  2155. :set 'org-set-emph-re
  2156. :type '(repeat
  2157. (list
  2158. (string :tag "Marker character")
  2159. (choice
  2160. (face :tag "Font-lock-face")
  2161. (plist :tag "Face property list"))
  2162. (string :tag "HTML start tag")
  2163. (string :tag "HTML end tag")
  2164. (option (const verbatim)))))
  2165. ;;; Miscellaneous options
  2166. (defgroup org-completion nil
  2167. "Completion in Org-mode."
  2168. :tag "Org Completion"
  2169. :group 'org)
  2170. (defcustom org-completion-use-ido nil
  2171. "Non-nil means, use ido completion wherever possible.
  2172. Note that `ido-mode' must be active for this variable to be relevant.
  2173. If you decide to turn this variable on, you might well want to turn off
  2174. `org-outline-path-complete-in-steps'."
  2175. :group 'org-completion
  2176. :type 'boolean)
  2177. (defcustom org-completion-fallback-command 'hippie-expand
  2178. "The expansion command called by \\[org-complete] in normal context.
  2179. Normal means, no org-mode-specific context."
  2180. :group 'org-completion
  2181. :type 'function)
  2182. ;;; Functions and variables from ther packages
  2183. ;; Declared here to avoid compiler warnings
  2184. ;; XEmacs only
  2185. (defvar outline-mode-menu-heading)
  2186. (defvar outline-mode-menu-show)
  2187. (defvar outline-mode-menu-hide)
  2188. (defvar zmacs-regions) ; XEmacs regions
  2189. ;; Emacs only
  2190. (defvar mark-active)
  2191. ;; Various packages
  2192. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2193. (declare-function calendar-forward-day "cal-move" (arg))
  2194. (declare-function calendar-goto-date "cal-move" (date))
  2195. (declare-function calendar-goto-today "cal-move" ())
  2196. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2197. (defvar calc-embedded-close-formula)
  2198. (defvar calc-embedded-open-formula)
  2199. (declare-function cdlatex-tab "ext:cdlatex" ())
  2200. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2201. (defvar font-lock-unfontify-region-function)
  2202. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2203. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2204. (defvar iswitchb-temp-buflist)
  2205. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2206. (defvar org-agenda-tags-todo-honor-ignore-options)
  2207. (declare-function org-agenda-skip "org-agenda" ())
  2208. (declare-function org-format-agenda-item "org-agenda"
  2209. (extra txt &optional category tags dotime noprefix remove-re))
  2210. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2211. (declare-function org-agenda-change-all-lines "org-agenda"
  2212. (newhead hdmarker &optional fixface just-this))
  2213. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2214. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2215. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2216. (beg end))
  2217. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2218. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2219. "org-agenda" (&optional end))
  2220. (declare-function parse-time-string "parse-time" (string))
  2221. (declare-function remember "remember" (&optional initial))
  2222. (declare-function remember-buffer-desc "remember" ())
  2223. (declare-function remember-finalize "remember" ())
  2224. (defvar remember-save-after-remembering)
  2225. (defvar remember-data-file)
  2226. (defvar remember-register)
  2227. (defvar remember-buffer)
  2228. (defvar remember-handler-functions)
  2229. (defvar remember-annotation-functions)
  2230. (defvar texmathp-why)
  2231. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2232. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2233. (defvar w3m-current-url)
  2234. (defvar w3m-current-title)
  2235. (defvar org-latex-regexps)
  2236. ;;; Autoload and prepare some org modules
  2237. ;; Some table stuff that needs to be defined here, because it is used
  2238. ;; by the functions setting up org-mode or checking for table context.
  2239. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2240. "Detects an org-type or table-type table.")
  2241. (defconst org-table-line-regexp "^[ \t]*|"
  2242. "Detects an org-type table line.")
  2243. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2244. "Detects an org-type table line.")
  2245. (defconst org-table-hline-regexp "^[ \t]*|-"
  2246. "Detects an org-type table hline.")
  2247. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2248. "Detects a table-type table hline.")
  2249. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2250. "Searching from within a table (any type) this finds the first line
  2251. outside the table.")
  2252. ;; Autoload the functions in org-table.el that are needed by functions here.
  2253. (eval-and-compile
  2254. (org-autoload "org-table"
  2255. '(org-table-align org-table-begin org-table-blank-field
  2256. org-table-convert org-table-convert-region org-table-copy-down
  2257. org-table-copy-region org-table-create
  2258. org-table-create-or-convert-from-region
  2259. org-table-create-with-table.el org-table-current-dline
  2260. org-table-cut-region org-table-delete-column org-table-edit-field
  2261. org-table-edit-formulas org-table-end org-table-eval-formula
  2262. org-table-export org-table-field-info
  2263. org-table-get-stored-formulas org-table-goto-column
  2264. org-table-hline-and-move org-table-import org-table-insert-column
  2265. org-table-insert-hline org-table-insert-row org-table-iterate
  2266. org-table-justify-field-maybe org-table-kill-row
  2267. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2268. org-table-move-column org-table-move-column-left
  2269. org-table-move-column-right org-table-move-row
  2270. org-table-move-row-down org-table-move-row-up
  2271. org-table-next-field org-table-next-row org-table-paste-rectangle
  2272. org-table-previous-field org-table-recalculate
  2273. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2274. org-table-toggle-coordinate-overlays
  2275. org-table-toggle-formula-debugger org-table-wrap-region
  2276. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2277. (defun org-at-table-p (&optional table-type)
  2278. "Return t if the cursor is inside an org-type table.
  2279. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2280. (if org-enable-table-editor
  2281. (save-excursion
  2282. (beginning-of-line 1)
  2283. (looking-at (if table-type org-table-any-line-regexp
  2284. org-table-line-regexp)))
  2285. nil))
  2286. (defsubst org-table-p () (org-at-table-p))
  2287. (defun org-at-table.el-p ()
  2288. "Return t if and only if we are at a table.el table."
  2289. (and (org-at-table-p 'any)
  2290. (save-excursion
  2291. (goto-char (org-table-begin 'any))
  2292. (looking-at org-table1-hline-regexp))))
  2293. (defun org-table-recognize-table.el ()
  2294. "If there is a table.el table nearby, recognize it and move into it."
  2295. (if org-table-tab-recognizes-table.el
  2296. (if (org-at-table.el-p)
  2297. (progn
  2298. (beginning-of-line 1)
  2299. (if (looking-at org-table-dataline-regexp)
  2300. nil
  2301. (if (looking-at org-table1-hline-regexp)
  2302. (progn
  2303. (beginning-of-line 2)
  2304. (if (looking-at org-table-any-border-regexp)
  2305. (beginning-of-line -1)))))
  2306. (if (re-search-forward "|" (org-table-end t) t)
  2307. (progn
  2308. (require 'table)
  2309. (if (table--at-cell-p (point))
  2310. t
  2311. (message "recognizing table.el table...")
  2312. (table-recognize-table)
  2313. (message "recognizing table.el table...done")))
  2314. (error "This should not happen..."))
  2315. t)
  2316. nil)
  2317. nil))
  2318. (defun org-at-table-hline-p ()
  2319. "Return t if the cursor is inside a hline in a table."
  2320. (if org-enable-table-editor
  2321. (save-excursion
  2322. (beginning-of-line 1)
  2323. (looking-at org-table-hline-regexp))
  2324. nil))
  2325. (defvar org-table-clean-did-remove-column nil)
  2326. (defun org-table-map-tables (function)
  2327. "Apply FUNCTION to the start of all tables in the buffer."
  2328. (save-excursion
  2329. (save-restriction
  2330. (widen)
  2331. (goto-char (point-min))
  2332. (while (re-search-forward org-table-any-line-regexp nil t)
  2333. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2334. (beginning-of-line 1)
  2335. (if (looking-at org-table-line-regexp)
  2336. (save-excursion (funcall function)))
  2337. (re-search-forward org-table-any-border-regexp nil 1))))
  2338. (message "Mapping tables: done"))
  2339. ;; Declare and autoload functions from org-exp.el
  2340. (declare-function org-default-export-plist "org-exp")
  2341. (declare-function org-infile-export-plist "org-exp")
  2342. (declare-function org-get-current-options "org-exp")
  2343. (eval-and-compile
  2344. (org-autoload "org-exp"
  2345. '(org-export org-export-as-ascii org-export-visible
  2346. org-insert-export-options-template org-export-as-html-and-open
  2347. org-export-as-html-batch org-export-as-html-to-buffer
  2348. org-replace-region-by-html org-export-region-as-html
  2349. org-export-as-html org-export-icalendar-this-file
  2350. org-export-icalendar-all-agenda-files
  2351. org-table-clean-before-export
  2352. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2353. ;; Declare and autoload functions from org-agenda.el
  2354. (eval-and-compile
  2355. (org-autoload "org-agenda"
  2356. '(org-agenda org-agenda-list org-search-view
  2357. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2358. org-diary org-agenda-to-appt
  2359. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2360. ;; Autoload org-remember
  2361. (eval-and-compile
  2362. (org-autoload "org-remember"
  2363. '(org-remember-insinuate org-remember-annotation
  2364. org-remember-apply-template org-remember org-remember-handler)))
  2365. ;; Autoload org-clock.el
  2366. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2367. (beg end))
  2368. (declare-function org-clock-update-mode-line "org-clock" ())
  2369. (defvar org-clock-start-time)
  2370. (defvar org-clock-marker (make-marker)
  2371. "Marker recording the last clock-in.")
  2372. (eval-and-compile
  2373. (org-autoload
  2374. "org-clock"
  2375. '(org-clock-in org-clock-out org-clock-cancel
  2376. org-clock-goto org-clock-sum org-clock-display
  2377. org-clock-remove-overlays org-clock-report
  2378. org-clocktable-shift org-dblock-write:clocktable
  2379. org-get-clocktable)))
  2380. (defun org-clock-update-time-maybe ()
  2381. "If this is a CLOCK line, update it and return t.
  2382. Otherwise, return nil."
  2383. (interactive)
  2384. (save-excursion
  2385. (beginning-of-line 1)
  2386. (skip-chars-forward " \t")
  2387. (when (looking-at org-clock-string)
  2388. (let ((re (concat "[ \t]*" org-clock-string
  2389. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2390. "\\([ \t]*=>.*\\)?\\)?"))
  2391. ts te h m s neg)
  2392. (cond
  2393. ((not (looking-at re))
  2394. nil)
  2395. ((not (match-end 2))
  2396. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2397. (> org-clock-marker (point))
  2398. (<= org-clock-marker (point-at-eol)))
  2399. ;; The clock is running here
  2400. (setq org-clock-start-time
  2401. (apply 'encode-time
  2402. (org-parse-time-string (match-string 1))))
  2403. (org-clock-update-mode-line)))
  2404. (t
  2405. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2406. (end-of-line 1)
  2407. (setq ts (match-string 1)
  2408. te (match-string 3))
  2409. (setq s (- (time-to-seconds
  2410. (apply 'encode-time (org-parse-time-string te)))
  2411. (time-to-seconds
  2412. (apply 'encode-time (org-parse-time-string ts))))
  2413. neg (< s 0)
  2414. s (abs s)
  2415. h (floor (/ s 3600))
  2416. s (- s (* 3600 h))
  2417. m (floor (/ s 60))
  2418. s (- s (* 60 s)))
  2419. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2420. t))))))
  2421. (defun org-check-running-clock ()
  2422. "Check if the current buffer contains the running clock.
  2423. If yes, offer to stop it and to save the buffer with the changes."
  2424. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2425. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2426. (buffer-name))))
  2427. (org-clock-out)
  2428. (when (y-or-n-p "Save changed buffer?")
  2429. (save-buffer))))
  2430. (defun org-clocktable-try-shift (dir n)
  2431. "Check if this line starts a clock table, if yes, shift the time block."
  2432. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2433. (org-clocktable-shift dir n)))
  2434. ;; Autoload org-timer.el
  2435. ;(declare-function org-timer "org-timer")
  2436. (eval-and-compile
  2437. (org-autoload
  2438. "org-timer"
  2439. '(org-timer-start org-timer org-timer-item
  2440. org-timer-change-times-in-region)))
  2441. ;; Autoload archiving code
  2442. ;; The stuff that is needed for cycling and tags has to be defined here.
  2443. (defgroup org-archive nil
  2444. "Options concerning archiving in Org-mode."
  2445. :tag "Org Archive"
  2446. :group 'org-structure)
  2447. (defcustom org-archive-location "%s_archive::"
  2448. "The location where subtrees should be archived.
  2449. The value of this variable is a string, consisting of two parts,
  2450. separated by a double-colon. The first part is a filename and
  2451. the second part is a headline.
  2452. When the filename is omitted, archiving happens in the same file.
  2453. %s in the filename will be replaced by the current file
  2454. name (without the directory part). Archiving to a different file
  2455. is useful to keep archived entries from contributing to the
  2456. Org-mode Agenda.
  2457. The archived entries will be filed as subtrees of the specified
  2458. headline. When the headline is omitted, the subtrees are simply
  2459. filed away at the end of the file, as top-level entries. Also in
  2460. the heading you can use %s to represent the file name, this can be
  2461. useful when using the same archive for a number of different files.
  2462. Here are a few examples:
  2463. \"%s_archive::\"
  2464. If the current file is Projects.org, archive in file
  2465. Projects.org_archive, as top-level trees. This is the default.
  2466. \"::* Archived Tasks\"
  2467. Archive in the current file, under the top-level headline
  2468. \"* Archived Tasks\".
  2469. \"~/org/archive.org::\"
  2470. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2471. \"~/org/archive.org::From %s\"
  2472. Archive in file ~/org/archive.org (absolute path), und headlines
  2473. \"From FILENAME\" where file name is the current file name.
  2474. \"basement::** Finished Tasks\"
  2475. Archive in file ./basement (relative path), as level 3 trees
  2476. below the level 2 heading \"** Finished Tasks\".
  2477. You may set this option on a per-file basis by adding to the buffer a
  2478. line like
  2479. #+ARCHIVE: basement::** Finished Tasks
  2480. You may also define it locally for a subtree by setting an ARCHIVE property
  2481. in the entry. If such a property is found in an entry, or anywhere up
  2482. the hierarchy, it will be used."
  2483. :group 'org-archive
  2484. :type 'string)
  2485. (defcustom org-archive-tag "ARCHIVE"
  2486. "The tag that marks a subtree as archived.
  2487. An archived subtree does not open during visibility cycling, and does
  2488. not contribute to the agenda listings.
  2489. After changing this, font-lock must be restarted in the relevant buffers to
  2490. get the proper fontification."
  2491. :group 'org-archive
  2492. :group 'org-keywords
  2493. :type 'string)
  2494. (defcustom org-agenda-skip-archived-trees t
  2495. "Non-nil means, the agenda will skip any items located in archived trees.
  2496. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2497. variable is no longer recommended, you should leave it at the value t.
  2498. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2499. :group 'org-archive
  2500. :group 'org-agenda-skip
  2501. :type 'boolean)
  2502. (defcustom org-cycle-open-archived-trees nil
  2503. "Non-nil means, `org-cycle' will open archived trees.
  2504. An archived tree is a tree marked with the tag ARCHIVE.
  2505. When nil, archived trees will stay folded. You can still open them with
  2506. normal outline commands like `show-all', but not with the cycling commands."
  2507. :group 'org-archive
  2508. :group 'org-cycle
  2509. :type 'boolean)
  2510. (defcustom org-sparse-tree-open-archived-trees nil
  2511. "Non-nil means sparse tree construction shows matches in archived trees.
  2512. When nil, matches in these trees are highlighted, but the trees are kept in
  2513. collapsed state."
  2514. :group 'org-archive
  2515. :group 'org-sparse-trees
  2516. :type 'boolean)
  2517. (defun org-cycle-hide-archived-subtrees (state)
  2518. "Re-hide all archived subtrees after a visibility state change."
  2519. (when (and (not org-cycle-open-archived-trees)
  2520. (not (memq state '(overview folded))))
  2521. (save-excursion
  2522. (let* ((globalp (memq state '(contents all)))
  2523. (beg (if globalp (point-min) (point)))
  2524. (end (if globalp (point-max) (org-end-of-subtree t))))
  2525. (org-hide-archived-subtrees beg end)
  2526. (goto-char beg)
  2527. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2528. (message "%s" (substitute-command-keys
  2529. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2530. (defun org-force-cycle-archived ()
  2531. "Cycle subtree even if it is archived."
  2532. (interactive)
  2533. (setq this-command 'org-cycle)
  2534. (let ((org-cycle-open-archived-trees t))
  2535. (call-interactively 'org-cycle)))
  2536. (defun org-hide-archived-subtrees (beg end)
  2537. "Re-hide all archived subtrees after a visibility state change."
  2538. (save-excursion
  2539. (let* ((re (concat ":" org-archive-tag ":")))
  2540. (goto-char beg)
  2541. (while (re-search-forward re end t)
  2542. (and (org-on-heading-p) (hide-subtree))
  2543. (org-end-of-subtree t)))))
  2544. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2545. (eval-and-compile
  2546. (org-autoload "org-archive"
  2547. '(org-add-archive-files org-archive-subtree
  2548. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2549. ;; Autoload Column View Code
  2550. (declare-function org-columns-number-to-string "org-colview")
  2551. (declare-function org-columns-get-format-and-top-level "org-colview")
  2552. (declare-function org-columns-compute "org-colview")
  2553. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2554. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2555. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2556. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2557. ;; Autoload ID code
  2558. (declare-function org-id-store-link "org-id")
  2559. (org-autoload "org-id"
  2560. '(org-id-get-create org-id-new org-id-copy org-id-get
  2561. org-id-get-with-outline-path-completion
  2562. org-id-get-with-outline-drilling
  2563. org-id-goto org-id-find org-id-store-link))
  2564. ;;; Variables for pre-computed regular expressions, all buffer local
  2565. (defvar org-drawer-regexp nil
  2566. "Matches first line of a hidden block.")
  2567. (make-variable-buffer-local 'org-drawer-regexp)
  2568. (defvar org-todo-regexp nil
  2569. "Matches any of the TODO state keywords.")
  2570. (make-variable-buffer-local 'org-todo-regexp)
  2571. (defvar org-not-done-regexp nil
  2572. "Matches any of the TODO state keywords except the last one.")
  2573. (make-variable-buffer-local 'org-not-done-regexp)
  2574. (defvar org-todo-line-regexp nil
  2575. "Matches a headline and puts TODO state into group 2 if present.")
  2576. (make-variable-buffer-local 'org-todo-line-regexp)
  2577. (defvar org-complex-heading-regexp nil
  2578. "Matches a headline and puts everything into groups:
  2579. group 1: the stars
  2580. group 2: The todo keyword, maybe
  2581. group 3: Priority cookie
  2582. group 4: True headline
  2583. group 5: Tags")
  2584. (make-variable-buffer-local 'org-complex-heading-regexp)
  2585. (defvar org-todo-line-tags-regexp nil
  2586. "Matches a headline and puts TODO state into group 2 if present.
  2587. Also put tags into group 4 if tags are present.")
  2588. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2589. (defvar org-nl-done-regexp nil
  2590. "Matches newline followed by a headline with the DONE keyword.")
  2591. (make-variable-buffer-local 'org-nl-done-regexp)
  2592. (defvar org-looking-at-done-regexp nil
  2593. "Matches the DONE keyword a point.")
  2594. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2595. (defvar org-ds-keyword-length 12
  2596. "Maximum length of the Deadline and SCHEDULED keywords.")
  2597. (make-variable-buffer-local 'org-ds-keyword-length)
  2598. (defvar org-deadline-regexp nil
  2599. "Matches the DEADLINE keyword.")
  2600. (make-variable-buffer-local 'org-deadline-regexp)
  2601. (defvar org-deadline-time-regexp nil
  2602. "Matches the DEADLINE keyword together with a time stamp.")
  2603. (make-variable-buffer-local 'org-deadline-time-regexp)
  2604. (defvar org-deadline-line-regexp nil
  2605. "Matches the DEADLINE keyword and the rest of the line.")
  2606. (make-variable-buffer-local 'org-deadline-line-regexp)
  2607. (defvar org-scheduled-regexp nil
  2608. "Matches the SCHEDULED keyword.")
  2609. (make-variable-buffer-local 'org-scheduled-regexp)
  2610. (defvar org-scheduled-time-regexp nil
  2611. "Matches the SCHEDULED keyword together with a time stamp.")
  2612. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2613. (defvar org-closed-time-regexp nil
  2614. "Matches the CLOSED keyword together with a time stamp.")
  2615. (make-variable-buffer-local 'org-closed-time-regexp)
  2616. (defvar org-keyword-time-regexp nil
  2617. "Matches any of the 4 keywords, together with the time stamp.")
  2618. (make-variable-buffer-local 'org-keyword-time-regexp)
  2619. (defvar org-keyword-time-not-clock-regexp nil
  2620. "Matches any of the 3 keywords, together with the time stamp.")
  2621. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2622. (defvar org-maybe-keyword-time-regexp nil
  2623. "Matches a timestamp, possibly preceeded by a keyword.")
  2624. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2625. (defvar org-planning-or-clock-line-re nil
  2626. "Matches a line with planning or clock info.")
  2627. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2628. (defconst org-plain-time-of-day-regexp
  2629. (concat
  2630. "\\(\\<[012]?[0-9]"
  2631. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2632. "\\(--?"
  2633. "\\(\\<[012]?[0-9]"
  2634. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2635. "\\)?")
  2636. "Regular expression to match a plain time or time range.
  2637. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2638. groups carry important information:
  2639. 0 the full match
  2640. 1 the first time, range or not
  2641. 8 the second time, if it is a range.")
  2642. (defconst org-plain-time-extension-regexp
  2643. (concat
  2644. "\\(\\<[012]?[0-9]"
  2645. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2646. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2647. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2648. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2649. groups carry important information:
  2650. 0 the full match
  2651. 7 hours of duration
  2652. 9 minutes of duration")
  2653. (defconst org-stamp-time-of-day-regexp
  2654. (concat
  2655. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2656. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2657. "\\(--?"
  2658. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2659. "Regular expression to match a timestamp time or time range.
  2660. After a match, the following groups carry important information:
  2661. 0 the full match
  2662. 1 date plus weekday, for backreferencing to make sure both times on same day
  2663. 2 the first time, range or not
  2664. 4 the second time, if it is a range.")
  2665. (defconst org-startup-options
  2666. '(("fold" org-startup-folded t)
  2667. ("overview" org-startup-folded t)
  2668. ("nofold" org-startup-folded nil)
  2669. ("showall" org-startup-folded nil)
  2670. ("content" org-startup-folded content)
  2671. ("hidestars" org-hide-leading-stars t)
  2672. ("showstars" org-hide-leading-stars nil)
  2673. ("odd" org-odd-levels-only t)
  2674. ("oddeven" org-odd-levels-only nil)
  2675. ("align" org-startup-align-all-tables t)
  2676. ("noalign" org-startup-align-all-tables nil)
  2677. ("customtime" org-display-custom-times t)
  2678. ("logdone" org-log-done time)
  2679. ("lognotedone" org-log-done note)
  2680. ("nologdone" org-log-done nil)
  2681. ("lognoteclock-out" org-log-note-clock-out t)
  2682. ("nolognoteclock-out" org-log-note-clock-out nil)
  2683. ("logrepeat" org-log-repeat state)
  2684. ("lognoterepeat" org-log-repeat note)
  2685. ("nologrepeat" org-log-repeat nil)
  2686. ("fninline" org-footnote-define-inline t)
  2687. ("nofninline" org-footnote-define-inline nil)
  2688. ("fnlocal" org-footnote-section nil)
  2689. ("fnauto" org-footnote-auto-label t)
  2690. ("fnprompt" org-footnote-auto-label nil)
  2691. ("fnconfirm" org-footnote-auto-label confirm)
  2692. ("fnplain" org-footnote-auto-label plain)
  2693. ("constcgs" constants-unit-system cgs)
  2694. ("constSI" constants-unit-system SI))
  2695. "Variable associated with STARTUP options for org-mode.
  2696. Each element is a list of three items: The startup options as written
  2697. in the #+STARTUP line, the corresponding variable, and the value to
  2698. set this variable to if the option is found. An optional forth element PUSH
  2699. means to push this value onto the list in the variable.")
  2700. (defun org-set-regexps-and-options ()
  2701. "Precompute regular expressions for current buffer."
  2702. (when (org-mode-p)
  2703. (org-set-local 'org-todo-kwd-alist nil)
  2704. (org-set-local 'org-todo-key-alist nil)
  2705. (org-set-local 'org-todo-key-trigger nil)
  2706. (org-set-local 'org-todo-keywords-1 nil)
  2707. (org-set-local 'org-done-keywords nil)
  2708. (org-set-local 'org-todo-heads nil)
  2709. (org-set-local 'org-todo-sets nil)
  2710. (org-set-local 'org-todo-log-states nil)
  2711. (org-set-local 'org-file-properties nil)
  2712. (org-set-local 'org-file-tags nil)
  2713. (let ((re (org-make-options-regexp
  2714. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2715. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2716. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2717. (splitre "[ \t]+")
  2718. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2719. tail sep kws1 prio props ftags drawers
  2720. ext-setup-or-nil setup-contents (start 0))
  2721. (save-excursion
  2722. (save-restriction
  2723. (widen)
  2724. (goto-char (point-min))
  2725. (while (or (and ext-setup-or-nil
  2726. (string-match re ext-setup-or-nil start)
  2727. (setq start (match-end 0)))
  2728. (and (setq ext-setup-or-nil nil start 0)
  2729. (re-search-forward re nil t)))
  2730. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2731. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2732. (cond
  2733. ((equal key "CATEGORY")
  2734. (if (string-match "[ \t]+$" value)
  2735. (setq value (replace-match "" t t value)))
  2736. (setq cat value))
  2737. ((member key '("SEQ_TODO" "TODO"))
  2738. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2739. ((equal key "TYP_TODO")
  2740. (push (cons 'type (org-split-string value splitre)) kwds))
  2741. ((equal key "TAGS")
  2742. (setq tags (append tags (org-split-string value splitre))))
  2743. ((equal key "COLUMNS")
  2744. (org-set-local 'org-columns-default-format value))
  2745. ((equal key "LINK")
  2746. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2747. (push (cons (match-string 1 value)
  2748. (org-trim (match-string 2 value)))
  2749. links)))
  2750. ((equal key "PRIORITIES")
  2751. (setq prio (org-split-string value " +")))
  2752. ((equal key "PROPERTY")
  2753. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2754. (push (cons (match-string 1 value) (match-string 2 value))
  2755. props)))
  2756. ((equal key "FILETAGS")
  2757. (when (string-match "\\S-" value)
  2758. (setq ftags
  2759. (append
  2760. ftags
  2761. (apply 'append
  2762. (mapcar (lambda (x) (org-split-string x ":"))
  2763. (org-split-string value)))))))
  2764. ((equal key "DRAWERS")
  2765. (setq drawers (org-split-string value splitre)))
  2766. ((equal key "CONSTANTS")
  2767. (setq const (append const (org-split-string value splitre))))
  2768. ((equal key "STARTUP")
  2769. (let ((opts (org-split-string value splitre))
  2770. l var val)
  2771. (while (setq l (pop opts))
  2772. (when (setq l (assoc l org-startup-options))
  2773. (setq var (nth 1 l) val (nth 2 l))
  2774. (if (not (nth 3 l))
  2775. (set (make-local-variable var) val)
  2776. (if (not (listp (symbol-value var)))
  2777. (set (make-local-variable var) nil))
  2778. (set (make-local-variable var) (symbol-value var))
  2779. (add-to-list var val))))))
  2780. ((equal key "ARCHIVE")
  2781. (string-match " *$" value)
  2782. (setq arch (replace-match "" t t value))
  2783. (remove-text-properties 0 (length arch)
  2784. '(face t fontified t) arch))
  2785. ((equal key "SETUPFILE")
  2786. (setq setup-contents (org-file-contents
  2787. (expand-file-name
  2788. (org-remove-double-quotes value))
  2789. 'noerror))
  2790. (if (not ext-setup-or-nil)
  2791. (setq ext-setup-or-nil setup-contents start 0)
  2792. (setq ext-setup-or-nil
  2793. (concat (substring ext-setup-or-nil 0 start)
  2794. "\n" setup-contents "\n"
  2795. (substring ext-setup-or-nil start)))))
  2796. ))))
  2797. (when cat
  2798. (org-set-local 'org-category (intern cat))
  2799. (push (cons "CATEGORY" cat) props))
  2800. (when prio
  2801. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2802. (setq prio (mapcar 'string-to-char prio))
  2803. (org-set-local 'org-highest-priority (nth 0 prio))
  2804. (org-set-local 'org-lowest-priority (nth 1 prio))
  2805. (org-set-local 'org-default-priority (nth 2 prio)))
  2806. (and props (org-set-local 'org-file-properties (nreverse props)))
  2807. (and ftags (org-set-local 'org-file-tags ftags))
  2808. (and drawers (org-set-local 'org-drawers drawers))
  2809. (and arch (org-set-local 'org-archive-location arch))
  2810. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2811. ;; Process the TODO keywords
  2812. (unless kwds
  2813. ;; Use the global values as if they had been given locally.
  2814. (setq kwds (default-value 'org-todo-keywords))
  2815. (if (stringp (car kwds))
  2816. (setq kwds (list (cons org-todo-interpretation
  2817. (default-value 'org-todo-keywords)))))
  2818. (setq kwds (reverse kwds)))
  2819. (setq kwds (nreverse kwds))
  2820. (let (inter kws kw)
  2821. (while (setq kws (pop kwds))
  2822. (setq inter (pop kws) sep (member "|" kws)
  2823. kws0 (delete "|" (copy-sequence kws))
  2824. kwsa nil
  2825. kws1 (mapcar
  2826. (lambda (x)
  2827. ;; 1 2
  2828. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2829. (progn
  2830. (setq kw (match-string 1 x)
  2831. key (and (match-end 2) (match-string 2 x))
  2832. log (org-extract-log-state-settings x))
  2833. (push (cons kw (and key (string-to-char key))) kwsa)
  2834. (and log (push log org-todo-log-states))
  2835. kw)
  2836. (error "Invalid TODO keyword %s" x)))
  2837. kws0)
  2838. kwsa (if kwsa (append '((:startgroup))
  2839. (nreverse kwsa)
  2840. '((:endgroup))))
  2841. hw (car kws1)
  2842. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2843. tail (list inter hw (car dws) (org-last dws)))
  2844. (add-to-list 'org-todo-heads hw 'append)
  2845. (push kws1 org-todo-sets)
  2846. (setq org-done-keywords (append org-done-keywords dws nil))
  2847. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2848. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2849. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2850. (setq org-todo-sets (nreverse org-todo-sets)
  2851. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2852. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2853. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2854. ;; Process the constants
  2855. (when const
  2856. (let (e cst)
  2857. (while (setq e (pop const))
  2858. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2859. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2860. (setq org-table-formula-constants-local cst)))
  2861. ;; Process the tags.
  2862. (when tags
  2863. (let (e tgs)
  2864. (while (setq e (pop tags))
  2865. (cond
  2866. ((equal e "{") (push '(:startgroup) tgs))
  2867. ((equal e "}") (push '(:endgroup) tgs))
  2868. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2869. (push (cons (match-string 1 e)
  2870. (string-to-char (match-string 2 e)))
  2871. tgs))
  2872. (t (push (list e) tgs))))
  2873. (org-set-local 'org-tag-alist nil)
  2874. (while (setq e (pop tgs))
  2875. (or (and (stringp (car e))
  2876. (assoc (car e) org-tag-alist))
  2877. (push e org-tag-alist)))))
  2878. ;; Compute the regular expressions and other local variables
  2879. (if (not org-done-keywords)
  2880. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2881. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2882. (length org-scheduled-string)
  2883. (length org-clock-string)
  2884. (length org-closed-string)))
  2885. org-drawer-regexp
  2886. (concat "^[ \t]*:\\("
  2887. (mapconcat 'regexp-quote org-drawers "\\|")
  2888. "\\):[ \t]*$")
  2889. org-not-done-keywords
  2890. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2891. org-todo-regexp
  2892. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2893. "\\|") "\\)\\>")
  2894. org-not-done-regexp
  2895. (concat "\\<\\("
  2896. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2897. "\\)\\>")
  2898. org-todo-line-regexp
  2899. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2900. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2901. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2902. org-complex-heading-regexp
  2903. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2904. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2905. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2906. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2907. org-nl-done-regexp
  2908. (concat "\n\\*+[ \t]+"
  2909. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2910. "\\)" "\\>")
  2911. org-todo-line-tags-regexp
  2912. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2913. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2914. (org-re
  2915. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2916. org-looking-at-done-regexp
  2917. (concat "^" "\\(?:"
  2918. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2919. "\\>")
  2920. org-deadline-regexp (concat "\\<" org-deadline-string)
  2921. org-deadline-time-regexp
  2922. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2923. org-deadline-line-regexp
  2924. (concat "\\<\\(" org-deadline-string "\\).*")
  2925. org-scheduled-regexp
  2926. (concat "\\<" org-scheduled-string)
  2927. org-scheduled-time-regexp
  2928. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2929. org-closed-time-regexp
  2930. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2931. org-keyword-time-regexp
  2932. (concat "\\<\\(" org-scheduled-string
  2933. "\\|" org-deadline-string
  2934. "\\|" org-closed-string
  2935. "\\|" org-clock-string "\\)"
  2936. " *[[<]\\([^]>]+\\)[]>]")
  2937. org-keyword-time-not-clock-regexp
  2938. (concat "\\<\\(" org-scheduled-string
  2939. "\\|" org-deadline-string
  2940. "\\|" org-closed-string
  2941. "\\)"
  2942. " *[[<]\\([^]>]+\\)[]>]")
  2943. org-maybe-keyword-time-regexp
  2944. (concat "\\(\\<\\(" org-scheduled-string
  2945. "\\|" org-deadline-string
  2946. "\\|" org-closed-string
  2947. "\\|" org-clock-string "\\)\\)?"
  2948. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2949. org-planning-or-clock-line-re
  2950. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2951. "\\|" org-deadline-string
  2952. "\\|" org-closed-string "\\|" org-clock-string
  2953. "\\)\\>\\)")
  2954. )
  2955. (org-compute-latex-and-specials-regexp)
  2956. (org-set-font-lock-defaults))))
  2957. (defun org-file-contents (file &optional noerror)
  2958. "Return the contents of FILE, as a string."
  2959. (if (or (not file)
  2960. (not (file-readable-p file)))
  2961. (if noerror
  2962. (progn
  2963. (message "Cannot read file %s" file)
  2964. (ding) (sit-for 2)
  2965. "")
  2966. (error "Cannot read file %s" file))
  2967. (with-temp-buffer
  2968. (insert-file-contents file)
  2969. (buffer-string))))
  2970. (defun org-extract-log-state-settings (x)
  2971. "Extract the log state setting from a TODO keyword string.
  2972. This will extract info from a string like \"WAIT(w@/!)\"."
  2973. (let (kw key log1 log2)
  2974. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2975. (setq kw (match-string 1 x)
  2976. key (and (match-end 2) (match-string 2 x))
  2977. log1 (and (match-end 3) (match-string 3 x))
  2978. log2 (and (match-end 4) (match-string 4 x)))
  2979. (and (or log1 log2)
  2980. (list kw
  2981. (and log1 (if (equal log1 "!") 'time 'note))
  2982. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2983. (defun org-remove-keyword-keys (list)
  2984. "Remove a pair of parenthesis at the end of each string in LIST."
  2985. (mapcar (lambda (x)
  2986. (if (string-match "(.*)$" x)
  2987. (substring x 0 (match-beginning 0))
  2988. x))
  2989. list))
  2990. ;; FIXME: this could be done much better, using second characters etc.
  2991. (defun org-assign-fast-keys (alist)
  2992. "Assign fast keys to a keyword-key alist.
  2993. Respect keys that are already there."
  2994. (let (new e k c c1 c2 (char ?a))
  2995. (while (setq e (pop alist))
  2996. (cond
  2997. ((equal e '(:startgroup)) (push e new))
  2998. ((equal e '(:endgroup)) (push e new))
  2999. (t
  3000. (setq k (car e) c2 nil)
  3001. (if (cdr e)
  3002. (setq c (cdr e))
  3003. ;; automatically assign a character.
  3004. (setq c1 (string-to-char
  3005. (downcase (substring
  3006. k (if (= (string-to-char k) ?@) 1 0)))))
  3007. (if (or (rassoc c1 new) (rassoc c1 alist))
  3008. (while (or (rassoc char new) (rassoc char alist))
  3009. (setq char (1+ char)))
  3010. (setq c2 c1))
  3011. (setq c (or c2 char)))
  3012. (push (cons k c) new))))
  3013. (nreverse new)))
  3014. ;;; Some variables used in various places
  3015. (defvar org-window-configuration nil
  3016. "Used in various places to store a window configuration.")
  3017. (defvar org-finish-function nil
  3018. "Function to be called when `C-c C-c' is used.
  3019. This is for getting out of special buffers like remember.")
  3020. ;; FIXME: Occasionally check by commenting these, to make sure
  3021. ;; no other functions uses these, forgetting to let-bind them.
  3022. (defvar entry)
  3023. (defvar state)
  3024. (defvar last-state)
  3025. (defvar date)
  3026. (defvar description)
  3027. ;; Defined somewhere in this file, but used before definition.
  3028. (defvar org-html-entities)
  3029. (defvar org-struct-menu)
  3030. (defvar org-org-menu)
  3031. (defvar org-tbl-menu)
  3032. (defvar org-agenda-keymap)
  3033. ;;;; Define the Org-mode
  3034. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3035. (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."))
  3036. ;; We use a before-change function to check if a table might need
  3037. ;; an update.
  3038. (defvar org-table-may-need-update t
  3039. "Indicates that a table might need an update.
  3040. This variable is set by `org-before-change-function'.
  3041. `org-table-align' sets it back to nil.")
  3042. (defun org-before-change-function (beg end)
  3043. "Every change indicates that a table might need an update."
  3044. (setq org-table-may-need-update t))
  3045. (defvar org-mode-map)
  3046. (defvar org-mode-hook nil
  3047. "Mode hook for Org-mode, run after the mode was turned on.")
  3048. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3049. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3050. (defvar org-table-buffer-is-an nil)
  3051. (defconst org-outline-regexp "\\*+ ")
  3052. ;;;###autoload
  3053. (define-derived-mode org-mode outline-mode "Org"
  3054. "Outline-based notes management and organizer, alias
  3055. \"Carsten's outline-mode for keeping track of everything.\"
  3056. Org-mode develops organizational tasks around a NOTES file which
  3057. contains information about projects as plain text. Org-mode is
  3058. implemented on top of outline-mode, which is ideal to keep the content
  3059. of large files well structured. It supports ToDo items, deadlines and
  3060. time stamps, which magically appear in the diary listing of the Emacs
  3061. calendar. Tables are easily created with a built-in table editor.
  3062. Plain text URL-like links connect to websites, emails (VM), Usenet
  3063. messages (Gnus), BBDB entries, and any files related to the project.
  3064. For printing and sharing of notes, an Org-mode file (or a part of it)
  3065. can be exported as a structured ASCII or HTML file.
  3066. The following commands are available:
  3067. \\{org-mode-map}"
  3068. ;; Get rid of Outline menus, they are not needed
  3069. ;; Need to do this here because define-derived-mode sets up
  3070. ;; the keymap so late. Still, it is a waste to call this each time
  3071. ;; we switch another buffer into org-mode.
  3072. (if (featurep 'xemacs)
  3073. (when (boundp 'outline-mode-menu-heading)
  3074. ;; Assume this is Greg's port, it used easymenu
  3075. (easy-menu-remove outline-mode-menu-heading)
  3076. (easy-menu-remove outline-mode-menu-show)
  3077. (easy-menu-remove outline-mode-menu-hide))
  3078. (define-key org-mode-map [menu-bar headings] 'undefined)
  3079. (define-key org-mode-map [menu-bar hide] 'undefined)
  3080. (define-key org-mode-map [menu-bar show] 'undefined))
  3081. (org-load-modules-maybe)
  3082. (easy-menu-add org-org-menu)
  3083. (easy-menu-add org-tbl-menu)
  3084. (org-install-agenda-files-menu)
  3085. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3086. (org-add-to-invisibility-spec '(org-cwidth))
  3087. (when (featurep 'xemacs)
  3088. (org-set-local 'line-move-ignore-invisible t))
  3089. (org-set-local 'outline-regexp org-outline-regexp)
  3090. (org-set-local 'outline-level 'org-outline-level)
  3091. (when (and org-ellipsis
  3092. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3093. (fboundp 'make-glyph-code))
  3094. (unless org-display-table
  3095. (setq org-display-table (make-display-table)))
  3096. (set-display-table-slot
  3097. org-display-table 4
  3098. (vconcat (mapcar
  3099. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3100. org-ellipsis)))
  3101. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3102. (setq buffer-display-table org-display-table))
  3103. (org-set-regexps-and-options)
  3104. (when (and org-tag-faces (not org-tags-special-faces-re))
  3105. ;; tag faces set outside customize.... force initialization.
  3106. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3107. ;; Calc embedded
  3108. (org-set-local 'calc-embedded-open-mode "# ")
  3109. (modify-syntax-entry ?# "<")
  3110. (modify-syntax-entry ?@ "w")
  3111. (if org-startup-truncated (setq truncate-lines t))
  3112. (org-set-local 'font-lock-unfontify-region-function
  3113. 'org-unfontify-region)
  3114. ;; Activate before-change-function
  3115. (org-set-local 'org-table-may-need-update t)
  3116. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3117. 'local)
  3118. ;; Check for running clock before killing a buffer
  3119. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3120. ;; Paragraphs and auto-filling
  3121. (org-set-autofill-regexps)
  3122. (setq indent-line-function 'org-indent-line-function)
  3123. (org-update-radio-target-regexp)
  3124. ;; Comment characters
  3125. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3126. (org-set-local 'comment-padding " ")
  3127. ;; Align options lines
  3128. (org-set-local
  3129. 'align-mode-rules-list
  3130. '((org-in-buffer-settings
  3131. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3132. (modes . '(org-mode)))))
  3133. ;; Imenu
  3134. (org-set-local 'imenu-create-index-function
  3135. 'org-imenu-get-tree)
  3136. ;; Make isearch reveal context
  3137. (if (or (featurep 'xemacs)
  3138. (not (boundp 'outline-isearch-open-invisible-function)))
  3139. ;; Emacs 21 and XEmacs make use of the hook
  3140. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3141. ;; Emacs 22 deals with this through a special variable
  3142. (org-set-local 'outline-isearch-open-invisible-function
  3143. (lambda (&rest ignore) (org-show-context 'isearch))))
  3144. ;; If empty file that did not turn on org-mode automatically, make it to.
  3145. (if (and org-insert-mode-line-in-empty-file
  3146. (interactive-p)
  3147. (= (point-min) (point-max)))
  3148. (insert "# -*- mode: org -*-\n\n"))
  3149. (unless org-inhibit-startup
  3150. (when org-startup-align-all-tables
  3151. (let ((bmp (buffer-modified-p)))
  3152. (org-table-map-tables 'org-table-align)
  3153. (set-buffer-modified-p bmp)))
  3154. (org-set-startup-visibility)))
  3155. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3156. (defun org-current-time ()
  3157. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3158. (if (> (car org-time-stamp-rounding-minutes) 1)
  3159. (let ((r (car org-time-stamp-rounding-minutes))
  3160. (time (decode-time)))
  3161. (apply 'encode-time
  3162. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3163. (nthcdr 2 time))))
  3164. (current-time)))
  3165. ;;;; Font-Lock stuff, including the activators
  3166. (defvar org-mouse-map (make-sparse-keymap))
  3167. (org-defkey org-mouse-map
  3168. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3169. (org-defkey org-mouse-map
  3170. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3171. (when org-mouse-1-follows-link
  3172. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3173. (when org-tab-follows-link
  3174. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3175. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3176. (when org-return-follows-link
  3177. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3178. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3179. (require 'font-lock)
  3180. (defconst org-non-link-chars "]\t\n\r<>")
  3181. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3182. "shell" "elisp"))
  3183. (defvar org-link-types-re nil
  3184. "Matches a link that has a url-like prefix like \"http:\"")
  3185. (defvar org-link-re-with-space nil
  3186. "Matches a link with spaces, optional angular brackets around it.")
  3187. (defvar org-link-re-with-space2 nil
  3188. "Matches a link with spaces, optional angular brackets around it.")
  3189. (defvar org-link-re-with-space3 nil
  3190. "Matches a link with spaces, only for internal part in bracket links.")
  3191. (defvar org-angle-link-re nil
  3192. "Matches link with angular brackets, spaces are allowed.")
  3193. (defvar org-plain-link-re nil
  3194. "Matches plain link, without spaces.")
  3195. (defvar org-bracket-link-regexp nil
  3196. "Matches a link in double brackets.")
  3197. (defvar org-bracket-link-analytic-regexp nil
  3198. "Regular expression used to analyze links.
  3199. Here is what the match groups contain after a match:
  3200. 1: http:
  3201. 2: http
  3202. 3: path
  3203. 4: [desc]
  3204. 5: desc")
  3205. (defvar org-bracket-link-analytic-regexp++ nil
  3206. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3207. (defvar org-any-link-re nil
  3208. "Regular expression matching any link.")
  3209. (defun org-make-link-regexps ()
  3210. "Update the link regular expressions.
  3211. This should be called after the variable `org-link-types' has changed."
  3212. (setq org-link-types-re
  3213. (concat
  3214. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3215. org-link-re-with-space
  3216. (concat
  3217. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3218. "\\([^" org-non-link-chars " ]"
  3219. "[^" org-non-link-chars "]*"
  3220. "[^" org-non-link-chars " ]\\)>?")
  3221. org-link-re-with-space2
  3222. (concat
  3223. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3224. "\\([^" org-non-link-chars " ]"
  3225. "[^\t\n\r]*"
  3226. "[^" org-non-link-chars " ]\\)>?")
  3227. org-link-re-with-space3
  3228. (concat
  3229. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3230. "\\([^" org-non-link-chars " ]"
  3231. "[^\t\n\r]*\\)")
  3232. org-angle-link-re
  3233. (concat
  3234. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3235. "\\([^" org-non-link-chars " ]"
  3236. "[^" org-non-link-chars "]*"
  3237. "\\)>")
  3238. org-plain-link-re
  3239. (concat
  3240. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3241. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3242. org-bracket-link-regexp
  3243. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3244. org-bracket-link-analytic-regexp
  3245. (concat
  3246. "\\[\\["
  3247. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3248. "\\([^]]+\\)"
  3249. "\\]"
  3250. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3251. "\\]")
  3252. org-bracket-link-analytic-regexp++
  3253. (concat
  3254. "\\[\\["
  3255. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3256. "\\([^]]+\\)"
  3257. "\\]"
  3258. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3259. "\\]")
  3260. org-any-link-re
  3261. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3262. org-angle-link-re "\\)\\|\\("
  3263. org-plain-link-re "\\)")))
  3264. (org-make-link-regexps)
  3265. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3266. "Regular expression for fast time stamp matching.")
  3267. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3268. "Regular expression for fast time stamp matching.")
  3269. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3270. "Regular expression matching time strings for analysis.
  3271. This one does not require the space after the date, so it can be used
  3272. on a string that terminates immediately after the date.")
  3273. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3274. "Regular expression matching time strings for analysis.")
  3275. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3276. "Regular expression matching time stamps, with groups.")
  3277. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3278. "Regular expression matching time stamps (also [..]), with groups.")
  3279. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3280. "Regular expression matching a time stamp range.")
  3281. (defconst org-tr-regexp-both
  3282. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3283. "Regular expression matching a time stamp range.")
  3284. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3285. org-ts-regexp "\\)?")
  3286. "Regular expression matching a time stamp or time stamp range.")
  3287. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3288. org-ts-regexp-both "\\)?")
  3289. "Regular expression matching a time stamp or time stamp range.
  3290. The time stamps may be either active or inactive.")
  3291. (defvar org-emph-face nil)
  3292. (defun org-do-emphasis-faces (limit)
  3293. "Run through the buffer and add overlays to links."
  3294. (let (rtn)
  3295. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3296. (if (not (= (char-after (match-beginning 3))
  3297. (char-after (match-beginning 4))))
  3298. (progn
  3299. (setq rtn t)
  3300. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3301. 'face
  3302. (nth 1 (assoc (match-string 3)
  3303. org-emphasis-alist)))
  3304. (add-text-properties (match-beginning 2) (match-end 2)
  3305. '(font-lock-multiline t))
  3306. (when org-hide-emphasis-markers
  3307. (add-text-properties (match-end 4) (match-beginning 5)
  3308. '(invisible org-link))
  3309. (add-text-properties (match-beginning 3) (match-end 3)
  3310. '(invisible org-link)))))
  3311. (backward-char 1))
  3312. rtn))
  3313. (defun org-emphasize (&optional char)
  3314. "Insert or change an emphasis, i.e. a font like bold or italic.
  3315. If there is an active region, change that region to a new emphasis.
  3316. If there is no region, just insert the marker characters and position
  3317. the cursor between them.
  3318. CHAR should be either the marker character, or the first character of the
  3319. HTML tag associated with that emphasis. If CHAR is a space, the means
  3320. to remove the emphasis of the selected region.
  3321. If char is not given (for example in an interactive call) it
  3322. will be prompted for."
  3323. (interactive)
  3324. (let ((eal org-emphasis-alist) e det
  3325. (erc org-emphasis-regexp-components)
  3326. (prompt "")
  3327. (string "") beg end move tag c s)
  3328. (if (org-region-active-p)
  3329. (setq beg (region-beginning) end (region-end)
  3330. string (buffer-substring beg end))
  3331. (setq move t))
  3332. (while (setq e (pop eal))
  3333. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3334. c (aref tag 0))
  3335. (push (cons c (string-to-char (car e))) det)
  3336. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3337. (substring tag 1)))))
  3338. (setq det (nreverse det))
  3339. (unless char
  3340. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3341. (setq char (read-char-exclusive)))
  3342. (setq char (or (cdr (assoc char det)) char))
  3343. (if (equal char ?\ )
  3344. (setq s "" move nil)
  3345. (unless (assoc (char-to-string char) org-emphasis-alist)
  3346. (error "No such emphasis marker: \"%c\"" char))
  3347. (setq s (char-to-string char)))
  3348. (while (and (> (length string) 1)
  3349. (equal (substring string 0 1) (substring string -1))
  3350. (assoc (substring string 0 1) org-emphasis-alist))
  3351. (setq string (substring string 1 -1)))
  3352. (setq string (concat s string s))
  3353. (if beg (delete-region beg end))
  3354. (unless (or (bolp)
  3355. (string-match (concat "[" (nth 0 erc) "\n]")
  3356. (char-to-string (char-before (point)))))
  3357. (insert " "))
  3358. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3359. (char-to-string (char-after (point))))
  3360. (insert " ") (backward-char 1))
  3361. (insert string)
  3362. (and move (backward-char 1))))
  3363. (defconst org-nonsticky-props
  3364. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3365. (defun org-activate-plain-links (limit)
  3366. "Run through the buffer and add overlays to links."
  3367. (catch 'exit
  3368. (let (f)
  3369. (while (re-search-forward org-plain-link-re limit t)
  3370. (setq f (get-text-property (match-beginning 0) 'face))
  3371. (if (or (eq f 'org-tag)
  3372. (and (listp f) (memq 'org-tag f)))
  3373. nil
  3374. (add-text-properties (match-beginning 0) (match-end 0)
  3375. (list 'mouse-face 'highlight
  3376. 'rear-nonsticky org-nonsticky-props
  3377. 'keymap org-mouse-map
  3378. ))
  3379. (throw 'exit t))))))
  3380. (defun org-activate-code (limit)
  3381. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3382. (progn
  3383. (remove-text-properties (match-beginning 0) (match-end 0)
  3384. '(display t invisible t intangible t))
  3385. t)))
  3386. (defun org-activate-angle-links (limit)
  3387. "Run through the buffer and add overlays to links."
  3388. (if (re-search-forward org-angle-link-re limit t)
  3389. (progn
  3390. (add-text-properties (match-beginning 0) (match-end 0)
  3391. (list 'mouse-face 'highlight
  3392. 'rear-nonsticky org-nonsticky-props
  3393. 'keymap org-mouse-map
  3394. ))
  3395. t)))
  3396. (defun org-activate-footnote-links (limit)
  3397. "Run through the buffer and add overlays to links."
  3398. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3399. limit t)
  3400. (progn
  3401. (add-text-properties (match-beginning 2) (match-end 2)
  3402. (list 'mouse-face 'highlight
  3403. 'rear-nonsticky org-nonsticky-props
  3404. 'keymap org-mouse-map
  3405. 'help-echo
  3406. (if (= (point-at-bol) (match-beginning 2))
  3407. "Footnote definition"
  3408. "Footnote reference")
  3409. ))
  3410. t)))
  3411. (defun org-activate-bracket-links (limit)
  3412. "Run through the buffer and add overlays to bracketed links."
  3413. (if (re-search-forward org-bracket-link-regexp limit t)
  3414. (let* ((help (concat "LINK: "
  3415. (org-match-string-no-properties 1)))
  3416. ;; FIXME: above we should remove the escapes.
  3417. ;; but that requires another match, protecting match data,
  3418. ;; a lot of overhead for font-lock.
  3419. (ip (org-maybe-intangible
  3420. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3421. 'keymap org-mouse-map 'mouse-face 'highlight
  3422. 'font-lock-multiline t 'help-echo help)))
  3423. (vp (list 'rear-nonsticky org-nonsticky-props
  3424. 'keymap org-mouse-map 'mouse-face 'highlight
  3425. ' font-lock-multiline t 'help-echo help)))
  3426. ;; We need to remove the invisible property here. Table narrowing
  3427. ;; may have made some of this invisible.
  3428. (remove-text-properties (match-beginning 0) (match-end 0)
  3429. '(invisible nil))
  3430. (if (match-end 3)
  3431. (progn
  3432. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3433. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3434. (add-text-properties (match-end 3) (match-end 0) ip))
  3435. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3436. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3437. (add-text-properties (match-end 1) (match-end 0) ip))
  3438. t)))
  3439. (defun org-activate-dates (limit)
  3440. "Run through the buffer and add overlays to dates."
  3441. (if (re-search-forward org-tsr-regexp-both limit t)
  3442. (progn
  3443. (add-text-properties (match-beginning 0) (match-end 0)
  3444. (list 'mouse-face 'highlight
  3445. 'rear-nonsticky org-nonsticky-props
  3446. 'keymap org-mouse-map))
  3447. (when org-display-custom-times
  3448. (if (match-end 3)
  3449. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3450. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3451. t)))
  3452. (defvar org-target-link-regexp nil
  3453. "Regular expression matching radio targets in plain text.")
  3454. (make-variable-buffer-local 'org-target-link-regexp)
  3455. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3456. "Regular expression matching a link target.")
  3457. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3458. "Regular expression matching a radio target.")
  3459. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3460. "Regular expression matching any target.")
  3461. (defun org-activate-target-links (limit)
  3462. "Run through the buffer and add overlays to target matches."
  3463. (when org-target-link-regexp
  3464. (let ((case-fold-search t))
  3465. (if (re-search-forward org-target-link-regexp limit t)
  3466. (progn
  3467. (add-text-properties (match-beginning 0) (match-end 0)
  3468. (list 'mouse-face 'highlight
  3469. 'rear-nonsticky org-nonsticky-props
  3470. 'keymap org-mouse-map
  3471. 'help-echo "Radio target link"
  3472. 'org-linked-text t))
  3473. t)))))
  3474. (defun org-update-radio-target-regexp ()
  3475. "Find all radio targets in this file and update the regular expression."
  3476. (interactive)
  3477. (when (memq 'radio org-activate-links)
  3478. (setq org-target-link-regexp
  3479. (org-make-target-link-regexp (org-all-targets 'radio)))
  3480. (org-restart-font-lock)))
  3481. (defun org-hide-wide-columns (limit)
  3482. (let (s e)
  3483. (setq s (text-property-any (point) (or limit (point-max))
  3484. 'org-cwidth t))
  3485. (when s
  3486. (setq e (next-single-property-change s 'org-cwidth))
  3487. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3488. (goto-char e)
  3489. t)))
  3490. (defvar org-latex-and-specials-regexp nil
  3491. "Regular expression for highlighting export special stuff.")
  3492. (defvar org-match-substring-regexp)
  3493. (defvar org-match-substring-with-braces-regexp)
  3494. (defvar org-export-html-special-string-regexps)
  3495. (defun org-compute-latex-and-specials-regexp ()
  3496. "Compute regular expression for stuff treated specially by exporters."
  3497. (if (not org-highlight-latex-fragments-and-specials)
  3498. (org-set-local 'org-latex-and-specials-regexp nil)
  3499. (require 'org-exp)
  3500. (let*
  3501. ((matchers (plist-get org-format-latex-options :matchers))
  3502. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3503. org-latex-regexps)))
  3504. (options (org-combine-plists (org-default-export-plist)
  3505. (org-infile-export-plist)))
  3506. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3507. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3508. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3509. (org-export-html-expand (plist-get options :expand-quoted-html))
  3510. (org-export-with-special-strings (plist-get options :special-strings))
  3511. (re-sub
  3512. (cond
  3513. ((equal org-export-with-sub-superscripts '{})
  3514. (list org-match-substring-with-braces-regexp))
  3515. (org-export-with-sub-superscripts
  3516. (list org-match-substring-regexp))
  3517. (t nil)))
  3518. (re-latex
  3519. (if org-export-with-LaTeX-fragments
  3520. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3521. (re-macros
  3522. (if org-export-with-TeX-macros
  3523. (list (concat "\\\\"
  3524. (regexp-opt
  3525. (append (mapcar 'car org-html-entities)
  3526. (if (boundp 'org-latex-entities)
  3527. org-latex-entities nil))
  3528. 'words))) ; FIXME
  3529. ))
  3530. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3531. (re-special (if org-export-with-special-strings
  3532. (mapcar (lambda (x) (car x))
  3533. org-export-html-special-string-regexps)))
  3534. (re-rest
  3535. (delq nil
  3536. (list
  3537. (if org-export-html-expand "@<[^>\n]+>")
  3538. ))))
  3539. (org-set-local
  3540. 'org-latex-and-specials-regexp
  3541. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3542. re-rest) "\\|")))))
  3543. (defun org-do-latex-and-special-faces (limit)
  3544. "Run through the buffer and add overlays to links."
  3545. (when org-latex-and-specials-regexp
  3546. (let (rtn d)
  3547. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3548. limit t))
  3549. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3550. 'face))
  3551. '(org-code org-verbatim underline)))
  3552. (progn
  3553. (setq rtn t
  3554. d (cond ((member (char-after (1+ (match-beginning 0)))
  3555. '(?_ ?^)) 1)
  3556. (t 0)))
  3557. (font-lock-prepend-text-property
  3558. (+ d (match-beginning 0)) (match-end 0)
  3559. 'face 'org-latex-and-export-specials)
  3560. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3561. '(font-lock-multiline t)))))
  3562. rtn)))
  3563. (defun org-restart-font-lock ()
  3564. "Restart font-lock-mode, to force refontification."
  3565. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3566. (font-lock-mode -1)
  3567. (font-lock-mode 1)))
  3568. (defun org-all-targets (&optional radio)
  3569. "Return a list of all targets in this file.
  3570. With optional argument RADIO, only find radio targets."
  3571. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3572. rtn)
  3573. (save-excursion
  3574. (goto-char (point-min))
  3575. (while (re-search-forward re nil t)
  3576. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3577. rtn)))
  3578. (defun org-make-target-link-regexp (targets)
  3579. "Make regular expression matching all strings in TARGETS.
  3580. The regular expression finds the targets also if there is a line break
  3581. between words."
  3582. (and targets
  3583. (concat
  3584. "\\<\\("
  3585. (mapconcat
  3586. (lambda (x)
  3587. (while (string-match " +" x)
  3588. (setq x (replace-match "\\s-+" t t x)))
  3589. x)
  3590. targets
  3591. "\\|")
  3592. "\\)\\>")))
  3593. (defun org-activate-tags (limit)
  3594. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3595. (progn
  3596. (add-text-properties (match-beginning 1) (match-end 1)
  3597. (list 'mouse-face 'highlight
  3598. 'rear-nonsticky org-nonsticky-props
  3599. 'keymap org-mouse-map))
  3600. t)))
  3601. (defun org-outline-level ()
  3602. (save-excursion
  3603. (looking-at outline-regexp)
  3604. (if (match-beginning 1)
  3605. (+ (org-get-string-indentation (match-string 1)) 1000)
  3606. (1- (- (match-end 0) (match-beginning 0))))))
  3607. (defvar org-font-lock-keywords nil)
  3608. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3609. "Regular expression matching a property line.")
  3610. (defvar org-font-lock-hook nil
  3611. "Functions to be called for special font lock stuff.")
  3612. (defun org-font-lock-hook (limit)
  3613. (run-hook-with-args 'org-font-lock-hook limit))
  3614. (defun org-set-font-lock-defaults ()
  3615. (let* ((em org-fontify-emphasized-text)
  3616. (lk org-activate-links)
  3617. (org-font-lock-extra-keywords
  3618. (list
  3619. ;; Call the hook
  3620. '(org-font-lock-hook)
  3621. ;; Headlines
  3622. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3623. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3624. ;; Table lines
  3625. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3626. (1 'org-table t))
  3627. ;; Table internals
  3628. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3629. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3630. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3631. ;; Drawers
  3632. (list org-drawer-regexp '(0 'org-special-keyword t))
  3633. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3634. ;; Properties
  3635. (list org-property-re
  3636. '(1 'org-special-keyword t)
  3637. '(3 'org-property-value t))
  3638. (if org-format-transports-properties-p
  3639. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3640. ;; Links
  3641. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3642. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3643. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3644. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3645. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3646. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3647. (if (memq 'footnote lk) '(org-activate-footnote-links
  3648. (2 'org-footnote t)))
  3649. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3650. '(org-hide-wide-columns (0 nil append))
  3651. ;; TODO lines
  3652. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3653. '(1 (org-get-todo-face 1) t))
  3654. ;; DONE
  3655. (if org-fontify-done-headline
  3656. (list (concat "^[*]+ +\\<\\("
  3657. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3658. "\\)\\(.*\\)")
  3659. '(2 'org-headline-done t))
  3660. nil)
  3661. ;; Priorities
  3662. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3663. ;; Tags
  3664. '(org-font-lock-add-tag-faces)
  3665. ;; Special keywords
  3666. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3667. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3668. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3669. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3670. ;; Emphasis
  3671. (if em
  3672. (if (featurep 'xemacs)
  3673. '(org-do-emphasis-faces (0 nil append))
  3674. '(org-do-emphasis-faces)))
  3675. ;; Checkboxes
  3676. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3677. 2 'bold prepend)
  3678. (if org-provide-checkbox-statistics
  3679. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3680. (0 (org-get-checkbox-statistics-face) t)))
  3681. ;; Description list items
  3682. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3683. 2 'bold prepend)
  3684. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3685. '(1 'org-archived prepend))
  3686. ;; Specials
  3687. '(org-do-latex-and-special-faces)
  3688. ;; Code
  3689. '(org-activate-code (1 'org-code t))
  3690. ;; COMMENT
  3691. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3692. "\\|" org-quote-string "\\)\\>")
  3693. '(1 'org-special-keyword t))
  3694. '("^#.*" (0 'font-lock-comment-face t))
  3695. )))
  3696. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3697. ;; Now set the full font-lock-keywords
  3698. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3699. (org-set-local 'font-lock-defaults
  3700. '(org-font-lock-keywords t nil nil backward-paragraph))
  3701. (kill-local-variable 'font-lock-keywords) nil))
  3702. (defvar org-m nil)
  3703. (defvar org-l nil)
  3704. (defvar org-f nil)
  3705. (defun org-get-level-face (n)
  3706. "Get the right face for match N in font-lock matching of headlines."
  3707. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3708. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3709. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3710. (cond
  3711. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3712. ((eq n 2) org-f)
  3713. (t (if org-level-color-stars-only nil org-f))))
  3714. (defun org-get-todo-face (kwd)
  3715. "Get the right face for a TODO keyword KWD.
  3716. If KWD is a number, get the corresponding match group."
  3717. (if (numberp kwd) (setq kwd (match-string kwd)))
  3718. (or (cdr (assoc kwd org-todo-keyword-faces))
  3719. (and (member kwd org-done-keywords) 'org-done)
  3720. 'org-todo))
  3721. (defun org-font-lock-add-tag-faces (limit)
  3722. "Add the special tag faces."
  3723. (when (and org-tag-faces org-tags-special-faces-re)
  3724. (while (re-search-forward org-tags-special-faces-re limit t)
  3725. (add-text-properties (match-beginning 1) (match-end 1)
  3726. (list 'face (org-get-tag-face 1)
  3727. 'font-lock-fontified t))
  3728. (backward-char 1))))
  3729. (defun org-get-tag-face (kwd)
  3730. "Get the right face for a TODO keyword KWD.
  3731. If KWD is a number, get the corresponding match group."
  3732. (if (numberp kwd) (setq kwd (match-string kwd)))
  3733. (or (cdr (assoc kwd org-tag-faces))
  3734. 'org-tag))
  3735. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3736. "Remove fontification and activation overlays from links."
  3737. (font-lock-default-unfontify-region beg end)
  3738. (let* ((buffer-undo-list t)
  3739. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3740. (inhibit-modification-hooks t)
  3741. deactivate-mark buffer-file-name buffer-file-truename)
  3742. (remove-text-properties beg end
  3743. '(mouse-face t keymap t org-linked-text t
  3744. invisible t intangible t))))
  3745. ;;;; Visibility cycling, including org-goto and indirect buffer
  3746. ;;; Cycling
  3747. (defvar org-cycle-global-status nil)
  3748. (make-variable-buffer-local 'org-cycle-global-status)
  3749. (defvar org-cycle-subtree-status nil)
  3750. (make-variable-buffer-local 'org-cycle-subtree-status)
  3751. ;;;###autoload
  3752. (defun org-cycle (&optional arg)
  3753. "Visibility cycling for Org-mode.
  3754. - When this function is called with a prefix argument, rotate the entire
  3755. buffer through 3 states (global cycling)
  3756. 1. OVERVIEW: Show only top-level headlines.
  3757. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3758. 3. SHOW ALL: Show everything.
  3759. When called with two C-u C-u prefixes, switch to the startup visibility,
  3760. determined by the variable `org-startup-folded', and by any VISIBILITY
  3761. properties in the buffer.
  3762. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3763. including drawers.
  3764. - When point is at the beginning of a headline, rotate the subtree started
  3765. by this line through 3 different states (local cycling)
  3766. 1. FOLDED: Only the main headline is shown.
  3767. 2. CHILDREN: The main headline and the direct children are shown.
  3768. From this state, you can move to one of the children
  3769. and zoom in further.
  3770. 3. SUBTREE: Show the entire subtree, including body text.
  3771. - When there is a numeric prefix, go up to a heading with level ARG, do
  3772. a `show-subtree' and return to the previous cursor position. If ARG
  3773. is negative, go up that many levels.
  3774. - When point is not at the beginning of a headline, execute the global
  3775. binding for TAB, which is re-indenting the line. See the option
  3776. `org-cycle-emulate-tab' for details.
  3777. - Special case: if point is at the beginning of the buffer and there is
  3778. no headline in line 1, this function will act as if called with prefix arg.
  3779. But only if also the variable `org-cycle-global-at-bob' is t."
  3780. (interactive "P")
  3781. (org-load-modules-maybe)
  3782. (let* ((outline-regexp
  3783. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3784. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3785. outline-regexp))
  3786. (bob-special (and org-cycle-global-at-bob (bobp)
  3787. (not (looking-at outline-regexp))))
  3788. (org-cycle-hook
  3789. (if bob-special
  3790. (delq 'org-optimize-window-after-visibility-change
  3791. (copy-sequence org-cycle-hook))
  3792. org-cycle-hook))
  3793. (pos (point)))
  3794. (if (or bob-special (equal arg '(4)))
  3795. ;; special case: use global cycling
  3796. (setq arg t))
  3797. (cond
  3798. ((equal arg '(16))
  3799. (org-set-startup-visibility)
  3800. (message "Startup visibility, plus VISIBILITY properties"))
  3801. ((equal arg '(64))
  3802. (show-all)
  3803. (message "Entire buffer visible, including drawers"))
  3804. ((org-at-table-p 'any)
  3805. ;; Enter the table or move to the next field in the table
  3806. (or (org-table-recognize-table.el)
  3807. (progn
  3808. (if arg (org-table-edit-field t)
  3809. (org-table-justify-field-maybe)
  3810. (call-interactively 'org-table-next-field)))))
  3811. ((eq arg t) ;; Global cycling
  3812. (cond
  3813. ((and (eq last-command this-command)
  3814. (eq org-cycle-global-status 'overview))
  3815. ;; We just created the overview - now do table of contents
  3816. ;; This can be slow in very large buffers, so indicate action
  3817. (message "CONTENTS...")
  3818. (org-content)
  3819. (message "CONTENTS...done")
  3820. (setq org-cycle-global-status 'contents)
  3821. (run-hook-with-args 'org-cycle-hook 'contents))
  3822. ((and (eq last-command this-command)
  3823. (eq org-cycle-global-status 'contents))
  3824. ;; We just showed the table of contents - now show everything
  3825. (show-all)
  3826. (message "SHOW ALL")
  3827. (setq org-cycle-global-status 'all)
  3828. (run-hook-with-args 'org-cycle-hook 'all))
  3829. (t
  3830. ;; Default action: go to overview
  3831. (org-overview)
  3832. (message "OVERVIEW")
  3833. (setq org-cycle-global-status 'overview)
  3834. (run-hook-with-args 'org-cycle-hook 'overview))))
  3835. ((and org-drawers org-drawer-regexp
  3836. (save-excursion
  3837. (beginning-of-line 1)
  3838. (looking-at org-drawer-regexp)))
  3839. ;; Toggle block visibility
  3840. (org-flag-drawer
  3841. (not (get-char-property (match-end 0) 'invisible))))
  3842. ((integerp arg)
  3843. ;; Show-subtree, ARG levels up from here.
  3844. (save-excursion
  3845. (org-back-to-heading)
  3846. (outline-up-heading (if (< arg 0) (- arg)
  3847. (- (funcall outline-level) arg)))
  3848. (org-show-subtree)))
  3849. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3850. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3851. ;; At a heading: rotate between three different views
  3852. (org-back-to-heading)
  3853. (let ((goal-column 0) eoh eol eos)
  3854. ;; First, some boundaries
  3855. (save-excursion
  3856. (org-back-to-heading)
  3857. (save-excursion
  3858. (beginning-of-line 2)
  3859. (while (and (not (eobp)) ;; this is like `next-line'
  3860. (get-char-property (1- (point)) 'invisible))
  3861. (beginning-of-line 2)) (setq eol (point)))
  3862. (outline-end-of-heading) (setq eoh (point))
  3863. (org-end-of-subtree t)
  3864. (unless (eobp)
  3865. (skip-chars-forward " \t\n")
  3866. (beginning-of-line 1) ; in case this is an item
  3867. )
  3868. (setq eos (1- (point))))
  3869. ;; Find out what to do next and set `this-command'
  3870. (cond
  3871. ((= eos eoh)
  3872. ;; Nothing is hidden behind this heading
  3873. (message "EMPTY ENTRY")
  3874. (setq org-cycle-subtree-status nil)
  3875. (save-excursion
  3876. (goto-char eos)
  3877. (outline-next-heading)
  3878. (if (org-invisible-p) (org-flag-heading nil))))
  3879. ((or (>= eol eos)
  3880. (not (string-match "\\S-" (buffer-substring eol eos))))
  3881. ;; Entire subtree is hidden in one line: open it
  3882. (org-show-entry)
  3883. (show-children)
  3884. (message "CHILDREN")
  3885. (save-excursion
  3886. (goto-char eos)
  3887. (outline-next-heading)
  3888. (if (org-invisible-p) (org-flag-heading nil)))
  3889. (setq org-cycle-subtree-status 'children)
  3890. (run-hook-with-args 'org-cycle-hook 'children))
  3891. ((and (eq last-command this-command)
  3892. (eq org-cycle-subtree-status 'children))
  3893. ;; We just showed the children, now show everything.
  3894. (org-show-subtree)
  3895. (message "SUBTREE")
  3896. (setq org-cycle-subtree-status 'subtree)
  3897. (run-hook-with-args 'org-cycle-hook 'subtree))
  3898. (t
  3899. ;; Default action: hide the subtree.
  3900. (hide-subtree)
  3901. (message "FOLDED")
  3902. (setq org-cycle-subtree-status 'folded)
  3903. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3904. ;; TAB emulation and template completion
  3905. (buffer-read-only (org-back-to-heading))
  3906. ((org-try-structure-completion))
  3907. ((org-try-cdlatex-tab))
  3908. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3909. (or (not (bolp))
  3910. (not (looking-at outline-regexp))))
  3911. (call-interactively (global-key-binding "\t")))
  3912. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3913. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3914. (or (and (eq org-cycle-emulate-tab 'white)
  3915. (= (match-end 0) (point-at-eol)))
  3916. (and (eq org-cycle-emulate-tab 'whitestart)
  3917. (>= (match-end 0) pos))))
  3918. t
  3919. (eq org-cycle-emulate-tab t))
  3920. (call-interactively (global-key-binding "\t")))
  3921. (t (save-excursion
  3922. (org-back-to-heading)
  3923. (org-cycle))))))
  3924. ;;;###autoload
  3925. (defun org-global-cycle (&optional arg)
  3926. "Cycle the global visibility. For details see `org-cycle'.
  3927. With C-u prefix arg, switch to startup visibility.
  3928. With a numeric prefix, show all headlines up to that level."
  3929. (interactive "P")
  3930. (let ((org-cycle-include-plain-lists
  3931. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3932. (cond
  3933. ((integerp arg)
  3934. (show-all)
  3935. (hide-sublevels arg)
  3936. (setq org-cycle-global-status 'contents))
  3937. ((equal arg '(4))
  3938. (org-set-startup-visibility)
  3939. (message "Startup visibility, plus VISIBILITY properties."))
  3940. (t
  3941. (org-cycle '(4))))))
  3942. (defun org-set-startup-visibility ()
  3943. "Set the visibility required by startup options and properties."
  3944. (cond
  3945. ((eq org-startup-folded t)
  3946. (org-cycle '(4)))
  3947. ((eq org-startup-folded 'content)
  3948. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3949. (org-cycle '(4)) (org-cycle '(4)))))
  3950. (org-set-visibility-according-to-property 'no-cleanup)
  3951. (org-cycle-hide-archived-subtrees 'all)
  3952. (org-cycle-hide-drawers 'all)
  3953. (org-cycle-show-empty-lines 'all))
  3954. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3955. "Switch subtree visibilities according to :VISIBILITY: property."
  3956. (interactive)
  3957. (let (state)
  3958. (save-excursion
  3959. (goto-char (point-min))
  3960. (while (re-search-forward
  3961. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3962. nil t)
  3963. (setq state (match-string 1))
  3964. (save-excursion
  3965. (org-back-to-heading t)
  3966. (hide-subtree)
  3967. (org-reveal)
  3968. (cond
  3969. ((equal state '("fold" "folded"))
  3970. (hide-subtree))
  3971. ((equal state "children")
  3972. (org-show-hidden-entry)
  3973. (show-children))
  3974. ((equal state "content")
  3975. (save-excursion
  3976. (save-restriction
  3977. (org-narrow-to-subtree)
  3978. (org-content))))
  3979. ((member state '("all" "showall"))
  3980. (show-subtree)))))
  3981. (unless no-cleanup
  3982. (org-cycle-hide-archived-subtrees 'all)
  3983. (org-cycle-hide-drawers 'all)
  3984. (org-cycle-show-empty-lines 'all)))))
  3985. (defun org-overview ()
  3986. "Switch to overview mode, showing only top-level headlines.
  3987. Really, this shows all headlines with level equal or greater than the level
  3988. of the first headline in the buffer. This is important, because if the
  3989. first headline is not level one, then (hide-sublevels 1) gives confusing
  3990. results."
  3991. (interactive)
  3992. (let ((level (save-excursion
  3993. (goto-char (point-min))
  3994. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3995. (progn
  3996. (goto-char (match-beginning 0))
  3997. (funcall outline-level))))))
  3998. (and level (hide-sublevels level))))
  3999. (defun org-content (&optional arg)
  4000. "Show all headlines in the buffer, like a table of contents.
  4001. With numerical argument N, show content up to level N."
  4002. (interactive "P")
  4003. (save-excursion
  4004. ;; Visit all headings and show their offspring
  4005. (and (integerp arg) (org-overview))
  4006. (goto-char (point-max))
  4007. (catch 'exit
  4008. (while (and (progn (condition-case nil
  4009. (outline-previous-visible-heading 1)
  4010. (error (goto-char (point-min))))
  4011. t)
  4012. (looking-at outline-regexp))
  4013. (if (integerp arg)
  4014. (show-children (1- arg))
  4015. (show-branches))
  4016. (if (bobp) (throw 'exit nil))))))
  4017. (defun org-optimize-window-after-visibility-change (state)
  4018. "Adjust the window after a change in outline visibility.
  4019. This function is the default value of the hook `org-cycle-hook'."
  4020. (when (get-buffer-window (current-buffer))
  4021. (cond
  4022. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4023. ; ((eq state 'overview) (org-beginning-of-line))
  4024. ((eq state 'content) nil)
  4025. ((eq state 'all) nil)
  4026. ((eq state 'folded) nil)
  4027. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4028. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4029. (defun org-compact-display-after-subtree-move ()
  4030. "Show a compacter version of the tree of the entry's parent."
  4031. (save-excursion
  4032. (if (org-up-heading-safe)
  4033. (progn
  4034. (hide-subtree)
  4035. (show-entry)
  4036. (show-children)
  4037. (org-cycle-show-empty-lines 'children)
  4038. (org-cycle-hide-drawers 'children))
  4039. (org-overview))))
  4040. (defun org-cycle-show-empty-lines (state)
  4041. "Show empty lines above all visible headlines.
  4042. The region to be covered depends on STATE when called through
  4043. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4044. entire buffer covered. Note that an empty line is only shown if there
  4045. are at least `org-cycle-separator-lines' empty lines before the headline."
  4046. (when (> org-cycle-separator-lines 0)
  4047. (save-excursion
  4048. (let* ((n org-cycle-separator-lines)
  4049. (re (cond
  4050. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4051. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4052. (t (let ((ns (number-to-string (- n 2))))
  4053. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4054. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4055. beg end)
  4056. (cond
  4057. ((memq state '(overview contents t))
  4058. (setq beg (point-min) end (point-max)))
  4059. ((memq state '(children folded))
  4060. (setq beg (point) end (progn (org-end-of-subtree t t)
  4061. (beginning-of-line 2)
  4062. (point)))))
  4063. (when beg
  4064. (goto-char beg)
  4065. (while (re-search-forward re end t)
  4066. (if (not (get-char-property (match-end 1) 'invisible))
  4067. (outline-flag-region
  4068. (match-beginning 1) (match-end 1) nil)))))))
  4069. ;; Never hide empty lines at the end of the file.
  4070. (save-excursion
  4071. (goto-char (point-max))
  4072. (outline-previous-heading)
  4073. (outline-end-of-heading)
  4074. (if (and (looking-at "[ \t\n]+")
  4075. (= (match-end 0) (point-max)))
  4076. (outline-flag-region (point) (match-end 0) nil))))
  4077. (defun org-show-empty-lines-in-parent ()
  4078. "Move to the parent and re-show empty lines before visible headlines."
  4079. (save-excursion
  4080. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4081. (org-cycle-show-empty-lines context))))
  4082. (defun org-cycle-hide-drawers (state)
  4083. "Re-hide all drawers after a visibility state change."
  4084. (when (and (org-mode-p)
  4085. (not (memq state '(overview folded))))
  4086. (save-excursion
  4087. (let* ((globalp (memq state '(contents all)))
  4088. (beg (if globalp (point-min) (point)))
  4089. (end (if globalp (point-max) (org-end-of-subtree t))))
  4090. (goto-char beg)
  4091. (while (re-search-forward org-drawer-regexp end t)
  4092. (org-flag-drawer t))))))
  4093. (defun org-flag-drawer (flag)
  4094. (save-excursion
  4095. (beginning-of-line 1)
  4096. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4097. (let ((b (match-end 0))
  4098. (outline-regexp org-outline-regexp))
  4099. (if (re-search-forward
  4100. "^[ \t]*:END:"
  4101. (save-excursion (outline-next-heading) (point)) t)
  4102. (outline-flag-region b (point-at-eol) flag)
  4103. (error ":END: line missing"))))))
  4104. (defun org-subtree-end-visible-p ()
  4105. "Is the end of the current subtree visible?"
  4106. (pos-visible-in-window-p
  4107. (save-excursion (org-end-of-subtree t) (point))))
  4108. (defun org-first-headline-recenter (&optional N)
  4109. "Move cursor to the first headline and recenter the headline.
  4110. Optional argument N means, put the headline into the Nth line of the window."
  4111. (goto-char (point-min))
  4112. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4113. (beginning-of-line)
  4114. (recenter (prefix-numeric-value N))))
  4115. ;;; Org-goto
  4116. (defvar org-goto-window-configuration nil)
  4117. (defvar org-goto-marker nil)
  4118. (defvar org-goto-map
  4119. (let ((map (make-sparse-keymap)))
  4120. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4121. (while (setq cmd (pop cmds))
  4122. (substitute-key-definition cmd cmd map global-map)))
  4123. (suppress-keymap map)
  4124. (org-defkey map "\C-m" 'org-goto-ret)
  4125. (org-defkey map [(return)] 'org-goto-ret)
  4126. (org-defkey map [(left)] 'org-goto-left)
  4127. (org-defkey map [(right)] 'org-goto-right)
  4128. (org-defkey map [(control ?g)] 'org-goto-quit)
  4129. (org-defkey map "\C-i" 'org-cycle)
  4130. (org-defkey map [(tab)] 'org-cycle)
  4131. (org-defkey map [(down)] 'outline-next-visible-heading)
  4132. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4133. (if org-goto-auto-isearch
  4134. (if (fboundp 'define-key-after)
  4135. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4136. nil)
  4137. (org-defkey map "q" 'org-goto-quit)
  4138. (org-defkey map "n" 'outline-next-visible-heading)
  4139. (org-defkey map "p" 'outline-previous-visible-heading)
  4140. (org-defkey map "f" 'outline-forward-same-level)
  4141. (org-defkey map "b" 'outline-backward-same-level)
  4142. (org-defkey map "u" 'outline-up-heading))
  4143. (org-defkey map "/" 'org-occur)
  4144. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4145. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4146. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4147. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4148. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4149. map))
  4150. (defconst org-goto-help
  4151. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4152. RET=jump to location [Q]uit and return to previous location
  4153. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4154. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4155. ;; FIXME: Docstring doe not mention both interfaces
  4156. (defun org-goto (&optional alternative-interface)
  4157. "Look up a different location in the current file, keeping current visibility.
  4158. When you want look-up or go to a different location in a document, the
  4159. fastest way is often to fold the entire buffer and then dive into the tree.
  4160. This method has the disadvantage, that the previous location will be folded,
  4161. which may not be what you want.
  4162. This command works around this by showing a copy of the current buffer
  4163. in an indirect buffer, in overview mode. You can dive into the tree in
  4164. that copy, use org-occur and incremental search to find a location.
  4165. When pressing RET or `Q', the command returns to the original buffer in
  4166. which the visibility is still unchanged. After RET is will also jump to
  4167. the location selected in the indirect buffer and expose the
  4168. the headline hierarchy above."
  4169. (interactive "P")
  4170. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4171. (org-refile-use-outline-path t)
  4172. (interface
  4173. (if (not alternative-interface)
  4174. org-goto-interface
  4175. (if (eq org-goto-interface 'outline)
  4176. 'outline-path-completion
  4177. 'outline)))
  4178. (org-goto-start-pos (point))
  4179. (selected-point
  4180. (if (eq interface 'outline)
  4181. (car (org-get-location (current-buffer) org-goto-help))
  4182. (nth 3 (org-refile-get-location "Goto: ")))))
  4183. (if selected-point
  4184. (progn
  4185. (org-mark-ring-push org-goto-start-pos)
  4186. (goto-char selected-point)
  4187. (if (or (org-invisible-p) (org-invisible-p2))
  4188. (org-show-context 'org-goto)))
  4189. (message "Quit"))))
  4190. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4191. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4192. (defvar org-goto-local-auto-isearch-map) ; defined below
  4193. (defun org-get-location (buf help)
  4194. "Let the user select a location in the Org-mode buffer BUF.
  4195. This function uses a recursive edit. It returns the selected position
  4196. or nil."
  4197. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4198. (isearch-hide-immediately nil)
  4199. (isearch-search-fun-function
  4200. (lambda () 'org-goto-local-search-headings))
  4201. (org-goto-selected-point org-goto-exit-command))
  4202. (save-excursion
  4203. (save-window-excursion
  4204. (delete-other-windows)
  4205. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4206. (switch-to-buffer
  4207. (condition-case nil
  4208. (make-indirect-buffer (current-buffer) "*org-goto*")
  4209. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4210. (with-output-to-temp-buffer "*Help*"
  4211. (princ help))
  4212. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4213. (setq buffer-read-only nil)
  4214. (let ((org-startup-truncated t)
  4215. (org-startup-folded nil)
  4216. (org-startup-align-all-tables nil))
  4217. (org-mode)
  4218. (org-overview))
  4219. (setq buffer-read-only t)
  4220. (if (and (boundp 'org-goto-start-pos)
  4221. (integer-or-marker-p org-goto-start-pos))
  4222. (let ((org-show-hierarchy-above t)
  4223. (org-show-siblings t)
  4224. (org-show-following-heading t))
  4225. (goto-char org-goto-start-pos)
  4226. (and (org-invisible-p) (org-show-context)))
  4227. (goto-char (point-min)))
  4228. (org-beginning-of-line)
  4229. (message "Select location and press RET")
  4230. (use-local-map org-goto-map)
  4231. (recursive-edit)
  4232. ))
  4233. (kill-buffer "*org-goto*")
  4234. (cons org-goto-selected-point org-goto-exit-command)))
  4235. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4236. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4237. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4238. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4239. (defun org-goto-local-search-headings (string bound noerror)
  4240. "Search and make sure that any matches are in headlines."
  4241. (catch 'return
  4242. (while (if isearch-forward
  4243. (search-forward string bound noerror)
  4244. (search-backward string bound noerror))
  4245. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4246. (and (member :headline context)
  4247. (not (member :tags context))))
  4248. (throw 'return (point))))))
  4249. (defun org-goto-local-auto-isearch ()
  4250. "Start isearch."
  4251. (interactive)
  4252. (goto-char (point-min))
  4253. (let ((keys (this-command-keys)))
  4254. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4255. (isearch-mode t)
  4256. (isearch-process-search-char (string-to-char keys)))))
  4257. (defun org-goto-ret (&optional arg)
  4258. "Finish `org-goto' by going to the new location."
  4259. (interactive "P")
  4260. (setq org-goto-selected-point (point)
  4261. org-goto-exit-command 'return)
  4262. (throw 'exit nil))
  4263. (defun org-goto-left ()
  4264. "Finish `org-goto' by going to the new location."
  4265. (interactive)
  4266. (if (org-on-heading-p)
  4267. (progn
  4268. (beginning-of-line 1)
  4269. (setq org-goto-selected-point (point)
  4270. org-goto-exit-command 'left)
  4271. (throw 'exit nil))
  4272. (error "Not on a heading")))
  4273. (defun org-goto-right ()
  4274. "Finish `org-goto' by going to the new location."
  4275. (interactive)
  4276. (if (org-on-heading-p)
  4277. (progn
  4278. (setq org-goto-selected-point (point)
  4279. org-goto-exit-command 'right)
  4280. (throw 'exit nil))
  4281. (error "Not on a heading")))
  4282. (defun org-goto-quit ()
  4283. "Finish `org-goto' without cursor motion."
  4284. (interactive)
  4285. (setq org-goto-selected-point nil)
  4286. (setq org-goto-exit-command 'quit)
  4287. (throw 'exit nil))
  4288. ;;; Indirect buffer display of subtrees
  4289. (defvar org-indirect-dedicated-frame nil
  4290. "This is the frame being used for indirect tree display.")
  4291. (defvar org-last-indirect-buffer nil)
  4292. (defun org-tree-to-indirect-buffer (&optional arg)
  4293. "Create indirect buffer and narrow it to current subtree.
  4294. With numerical prefix ARG, go up to this level and then take that tree.
  4295. If ARG is negative, go up that many levels.
  4296. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4297. indirect buffer previously made with this command, to avoid proliferation of
  4298. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4299. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4300. is kept so that you can work with several indirect buffers at the same time.
  4301. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4302. requests that a new frame be made for the new buffer, so that the dedicated
  4303. frame is not changed."
  4304. (interactive "P")
  4305. (let ((cbuf (current-buffer))
  4306. (cwin (selected-window))
  4307. (pos (point))
  4308. beg end level heading ibuf)
  4309. (save-excursion
  4310. (org-back-to-heading t)
  4311. (when (numberp arg)
  4312. (setq level (org-outline-level))
  4313. (if (< arg 0) (setq arg (+ level arg)))
  4314. (while (> (setq level (org-outline-level)) arg)
  4315. (outline-up-heading 1 t)))
  4316. (setq beg (point)
  4317. heading (org-get-heading))
  4318. (org-end-of-subtree t) (setq end (point)))
  4319. (if (and (buffer-live-p org-last-indirect-buffer)
  4320. (not (eq org-indirect-buffer-display 'new-frame))
  4321. (not arg))
  4322. (kill-buffer org-last-indirect-buffer))
  4323. (setq ibuf (org-get-indirect-buffer cbuf)
  4324. org-last-indirect-buffer ibuf)
  4325. (cond
  4326. ((or (eq org-indirect-buffer-display 'new-frame)
  4327. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4328. (select-frame (make-frame))
  4329. (delete-other-windows)
  4330. (switch-to-buffer ibuf)
  4331. (org-set-frame-title heading))
  4332. ((eq org-indirect-buffer-display 'dedicated-frame)
  4333. (raise-frame
  4334. (select-frame (or (and org-indirect-dedicated-frame
  4335. (frame-live-p org-indirect-dedicated-frame)
  4336. org-indirect-dedicated-frame)
  4337. (setq org-indirect-dedicated-frame (make-frame)))))
  4338. (delete-other-windows)
  4339. (switch-to-buffer ibuf)
  4340. (org-set-frame-title (concat "Indirect: " heading)))
  4341. ((eq org-indirect-buffer-display 'current-window)
  4342. (switch-to-buffer ibuf))
  4343. ((eq org-indirect-buffer-display 'other-window)
  4344. (pop-to-buffer ibuf))
  4345. (t (error "Invalid value.")))
  4346. (if (featurep 'xemacs)
  4347. (save-excursion (org-mode) (turn-on-font-lock)))
  4348. (narrow-to-region beg end)
  4349. (show-all)
  4350. (goto-char pos)
  4351. (and (window-live-p cwin) (select-window cwin))))
  4352. (defun org-get-indirect-buffer (&optional buffer)
  4353. (setq buffer (or buffer (current-buffer)))
  4354. (let ((n 1) (base (buffer-name buffer)) bname)
  4355. (while (buffer-live-p
  4356. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4357. (setq n (1+ n)))
  4358. (condition-case nil
  4359. (make-indirect-buffer buffer bname 'clone)
  4360. (error (make-indirect-buffer buffer bname)))))
  4361. (defun org-set-frame-title (title)
  4362. "Set the title of the current frame to the string TITLE."
  4363. ;; FIXME: how to name a single frame in XEmacs???
  4364. (unless (featurep 'xemacs)
  4365. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4366. ;;;; Structure editing
  4367. ;;; Inserting headlines
  4368. (defun org-previous-line-empty-p ()
  4369. (save-excursion
  4370. (and (not (bobp))
  4371. (or (beginning-of-line 0) t)
  4372. (save-match-data
  4373. (looking-at "[ \t]*$")))))
  4374. (defun org-insert-heading (&optional force-heading)
  4375. "Insert a new heading or item with same depth at point.
  4376. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4377. If point is at the beginning of a headline, insert a sibling before the
  4378. current headline. If point is not at the beginning, do not split the line,
  4379. but create the new headline after the current line."
  4380. (interactive "P")
  4381. (if (= (buffer-size) 0)
  4382. (insert "\n* ")
  4383. (when (or force-heading (not (org-insert-item)))
  4384. (let* ((empty-line-p nil)
  4385. (head (save-excursion
  4386. (condition-case nil
  4387. (progn
  4388. (org-back-to-heading)
  4389. (setq empty-line-p (org-previous-line-empty-p))
  4390. (match-string 0))
  4391. (error "*"))))
  4392. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4393. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4394. pos hide-previous previous-pos)
  4395. (cond
  4396. ((and (org-on-heading-p) (bolp)
  4397. (or (bobp)
  4398. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4399. ;; insert before the current line
  4400. (open-line (if blank 2 1)))
  4401. ((and (bolp)
  4402. (or (bobp)
  4403. (save-excursion
  4404. (backward-char 1) (not (org-invisible-p)))))
  4405. ;; insert right here
  4406. nil)
  4407. (t
  4408. ;; somewhere in the line
  4409. (save-excursion
  4410. (setq previous-pos (point-at-bol))
  4411. (end-of-line)
  4412. (setq hide-previous (org-invisible-p)))
  4413. (and org-insert-heading-respect-content (org-show-subtree))
  4414. (let ((split
  4415. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4416. (save-excursion
  4417. (let ((p (point)))
  4418. (goto-char (point-at-bol))
  4419. (and (looking-at org-complex-heading-regexp)
  4420. (> p (match-beginning 4)))))))
  4421. tags pos)
  4422. (cond
  4423. (org-insert-heading-respect-content
  4424. (org-end-of-subtree nil t)
  4425. (or (bolp) (newline))
  4426. (or (org-previous-line-empty-p)
  4427. (and blank (newline)))
  4428. (open-line 1))
  4429. ((org-on-heading-p)
  4430. (when hide-previous
  4431. (show-children)
  4432. (org-show-entry))
  4433. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4434. (setq tags (and (match-end 2) (match-string 2)))
  4435. (and (match-end 1)
  4436. (delete-region (match-beginning 1) (match-end 1)))
  4437. (setq pos (point-at-bol))
  4438. (or split (end-of-line 1))
  4439. (delete-horizontal-space)
  4440. (newline (if blank 2 1))
  4441. (when tags
  4442. (save-excursion
  4443. (goto-char pos)
  4444. (end-of-line 1)
  4445. (insert " " tags)
  4446. (org-set-tags nil 'align))))
  4447. (t
  4448. (or split (end-of-line 1))
  4449. (newline (if blank 2 1)))))))
  4450. (insert head) (just-one-space)
  4451. (setq pos (point))
  4452. (end-of-line 1)
  4453. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4454. (when (and org-insert-heading-respect-content hide-previous)
  4455. (save-excursion
  4456. (goto-char previous-pos)
  4457. (hide-subtree)))
  4458. (run-hooks 'org-insert-heading-hook)))))
  4459. (defun org-get-heading (&optional no-tags)
  4460. "Return the heading of the current entry, without the stars."
  4461. (save-excursion
  4462. (org-back-to-heading t)
  4463. (if (looking-at
  4464. (if no-tags
  4465. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4466. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4467. (match-string 1) "")))
  4468. (defun org-heading-components ()
  4469. "Return the components of the current heading.
  4470. This is a list with the following elements:
  4471. - the level as an integer
  4472. - the reduced level, different if `org-odd-levels-only' is set.
  4473. - the TODO keyword, or nil
  4474. - the priority character, like ?A, or nil if no priority is given
  4475. - the headline text itself, or the tags string if no headline text
  4476. - the tags string, or nil."
  4477. (save-excursion
  4478. (org-back-to-heading t)
  4479. (if (looking-at org-complex-heading-regexp)
  4480. (list (length (match-string 1))
  4481. (org-reduced-level (length (match-string 1)))
  4482. (org-match-string-no-properties 2)
  4483. (and (match-end 3) (aref (match-string 3) 2))
  4484. (org-match-string-no-properties 4)
  4485. (org-match-string-no-properties 5)))))
  4486. (defun org-insert-heading-after-current ()
  4487. "Insert a new heading with same level as current, after current subtree."
  4488. (interactive)
  4489. (org-back-to-heading)
  4490. (org-insert-heading)
  4491. (org-move-subtree-down)
  4492. (end-of-line 1))
  4493. (defun org-insert-heading-respect-content ()
  4494. (interactive)
  4495. (let ((org-insert-heading-respect-content t))
  4496. (org-insert-heading t)))
  4497. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4498. (interactive "P")
  4499. (let ((org-insert-heading-respect-content t))
  4500. (org-insert-todo-heading force-state t)))
  4501. (defun org-insert-todo-heading (arg &optional force-heading)
  4502. "Insert a new heading with the same level and TODO state as current heading.
  4503. If the heading has no TODO state, or if the state is DONE, use the first
  4504. state (TODO by default). Also with prefix arg, force first state."
  4505. (interactive "P")
  4506. (when (or force-heading (not (org-insert-item 'checkbox)))
  4507. (org-insert-heading force-heading)
  4508. (save-excursion
  4509. (org-back-to-heading)
  4510. (outline-previous-heading)
  4511. (looking-at org-todo-line-regexp))
  4512. (if (or arg
  4513. (not (match-beginning 2))
  4514. (member (match-string 2) org-done-keywords))
  4515. (insert (car org-todo-keywords-1) " ")
  4516. (insert (match-string 2) " "))
  4517. (when org-provide-todo-statistics
  4518. (org-update-parent-todo-statistics))))
  4519. (defun org-insert-subheading (arg)
  4520. "Insert a new subheading and demote it.
  4521. Works for outline headings and for plain lists alike."
  4522. (interactive "P")
  4523. (org-insert-heading arg)
  4524. (cond
  4525. ((org-on-heading-p) (org-do-demote))
  4526. ((org-at-item-p) (org-indent-item 1))))
  4527. (defun org-insert-todo-subheading (arg)
  4528. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4529. Works for outline headings and for plain lists alike."
  4530. (interactive "P")
  4531. (org-insert-todo-heading arg)
  4532. (cond
  4533. ((org-on-heading-p) (org-do-demote))
  4534. ((org-at-item-p) (org-indent-item 1))))
  4535. ;;; Promotion and Demotion
  4536. (defun org-promote-subtree ()
  4537. "Promote the entire subtree.
  4538. See also `org-promote'."
  4539. (interactive)
  4540. (save-excursion
  4541. (org-map-tree 'org-promote))
  4542. (org-fix-position-after-promote))
  4543. (defun org-demote-subtree ()
  4544. "Demote the entire subtree. See `org-demote'.
  4545. See also `org-promote'."
  4546. (interactive)
  4547. (save-excursion
  4548. (org-map-tree 'org-demote))
  4549. (org-fix-position-after-promote))
  4550. (defun org-do-promote ()
  4551. "Promote the current heading higher up the tree.
  4552. If the region is active in `transient-mark-mode', promote all headings
  4553. in the region."
  4554. (interactive)
  4555. (save-excursion
  4556. (if (org-region-active-p)
  4557. (org-map-region 'org-promote (region-beginning) (region-end))
  4558. (org-promote)))
  4559. (org-fix-position-after-promote))
  4560. (defun org-do-demote ()
  4561. "Demote the current heading lower down the tree.
  4562. If the region is active in `transient-mark-mode', demote all headings
  4563. in the region."
  4564. (interactive)
  4565. (save-excursion
  4566. (if (org-region-active-p)
  4567. (org-map-region 'org-demote (region-beginning) (region-end))
  4568. (org-demote)))
  4569. (org-fix-position-after-promote))
  4570. (defun org-fix-position-after-promote ()
  4571. "Make sure that after pro/demotion cursor position is right."
  4572. (let ((pos (point)))
  4573. (when (save-excursion
  4574. (beginning-of-line 1)
  4575. (looking-at org-todo-line-regexp)
  4576. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4577. (cond ((eobp) (insert " "))
  4578. ((eolp) (insert " "))
  4579. ((equal (char-after) ?\ ) (forward-char 1))))))
  4580. (defun org-reduced-level (l)
  4581. "Compute the effective level of a heading.
  4582. This takes into account the setting of `org-odd-levels-only'."
  4583. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4584. (defun org-get-valid-level (level &optional change)
  4585. "Rectify a level change under the influence of `org-odd-levels-only'
  4586. LEVEL is a current level, CHANGE is by how much the level should be
  4587. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4588. even level numbers will become the next higher odd number."
  4589. (if org-odd-levels-only
  4590. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4591. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4592. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4593. (max 1 (+ level change))))
  4594. (if (boundp 'define-obsolete-function-alias)
  4595. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4596. (define-obsolete-function-alias 'org-get-legal-level
  4597. 'org-get-valid-level)
  4598. (define-obsolete-function-alias 'org-get-legal-level
  4599. 'org-get-valid-level "23.1")))
  4600. (defun org-promote ()
  4601. "Promote the current heading higher up the tree.
  4602. If the region is active in `transient-mark-mode', promote all headings
  4603. in the region."
  4604. (org-back-to-heading t)
  4605. (let* ((level (save-match-data (funcall outline-level)))
  4606. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4607. (diff (abs (- level (length up-head) -1))))
  4608. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4609. (replace-match up-head nil t)
  4610. ;; Fixup tag positioning
  4611. (and org-auto-align-tags (org-set-tags nil t))
  4612. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4613. (defun org-demote ()
  4614. "Demote the current heading lower down the tree.
  4615. If the region is active in `transient-mark-mode', demote all headings
  4616. in the region."
  4617. (org-back-to-heading t)
  4618. (let* ((level (save-match-data (funcall outline-level)))
  4619. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4620. (diff (abs (- level (length down-head) -1))))
  4621. (replace-match down-head nil t)
  4622. ;; Fixup tag positioning
  4623. (and org-auto-align-tags (org-set-tags nil t))
  4624. (if org-adapt-indentation (org-fixup-indentation diff))))
  4625. (defun org-map-tree (fun)
  4626. "Call FUN for every heading underneath the current one."
  4627. (org-back-to-heading)
  4628. (let ((level (funcall outline-level)))
  4629. (save-excursion
  4630. (funcall fun)
  4631. (while (and (progn
  4632. (outline-next-heading)
  4633. (> (funcall outline-level) level))
  4634. (not (eobp)))
  4635. (funcall fun)))))
  4636. (defun org-map-region (fun beg end)
  4637. "Call FUN for every heading between BEG and END."
  4638. (let ((org-ignore-region t))
  4639. (save-excursion
  4640. (setq end (copy-marker end))
  4641. (goto-char beg)
  4642. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4643. (< (point) end))
  4644. (funcall fun))
  4645. (while (and (progn
  4646. (outline-next-heading)
  4647. (< (point) end))
  4648. (not (eobp)))
  4649. (funcall fun)))))
  4650. (defun org-fixup-indentation (diff)
  4651. "Change the indentation in the current entry by DIFF
  4652. However, if any line in the current entry has no indentation, or if it
  4653. would end up with no indentation after the change, nothing at all is done."
  4654. (save-excursion
  4655. (let ((end (save-excursion (outline-next-heading)
  4656. (point-marker)))
  4657. (prohibit (if (> diff 0)
  4658. "^\\S-"
  4659. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4660. col)
  4661. (unless (save-excursion (end-of-line 1)
  4662. (re-search-forward prohibit end t))
  4663. (while (and (< (point) end)
  4664. (re-search-forward "^[ \t]+" end t))
  4665. (goto-char (match-end 0))
  4666. (setq col (current-column))
  4667. (if (< diff 0) (replace-match ""))
  4668. (org-indent-to-column (+ diff col))))
  4669. (move-marker end nil))))
  4670. (defun org-convert-to-odd-levels ()
  4671. "Convert an org-mode file with all levels allowed to one with odd levels.
  4672. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4673. level 5 etc."
  4674. (interactive)
  4675. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4676. (let ((org-odd-levels-only nil) n)
  4677. (save-excursion
  4678. (goto-char (point-min))
  4679. (while (re-search-forward "^\\*\\*+ " nil t)
  4680. (setq n (- (length (match-string 0)) 2))
  4681. (while (>= (setq n (1- n)) 0)
  4682. (org-demote))
  4683. (end-of-line 1))))))
  4684. (defun org-convert-to-oddeven-levels ()
  4685. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4686. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4687. section with an even level, conversion would destroy the structure of the file. An error
  4688. is signaled in this case."
  4689. (interactive)
  4690. (goto-char (point-min))
  4691. ;; First check if there are no even levels
  4692. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4693. (org-show-context t)
  4694. (error "Not all levels are odd in this file. Conversion not possible."))
  4695. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4696. (let ((org-odd-levels-only nil) n)
  4697. (save-excursion
  4698. (goto-char (point-min))
  4699. (while (re-search-forward "^\\*\\*+ " nil t)
  4700. (setq n (/ (1- (length (match-string 0))) 2))
  4701. (while (>= (setq n (1- n)) 0)
  4702. (org-promote))
  4703. (end-of-line 1))))))
  4704. (defun org-tr-level (n)
  4705. "Make N odd if required."
  4706. (if org-odd-levels-only (1+ (/ n 2)) n))
  4707. ;;; Vertical tree motion, cutting and pasting of subtrees
  4708. (defun org-move-subtree-up (&optional arg)
  4709. "Move the current subtree up past ARG headlines of the same level."
  4710. (interactive "p")
  4711. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4712. (defun org-move-subtree-down (&optional arg)
  4713. "Move the current subtree down past ARG headlines of the same level."
  4714. (interactive "p")
  4715. (setq arg (prefix-numeric-value arg))
  4716. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4717. 'outline-get-last-sibling))
  4718. (ins-point (make-marker))
  4719. (cnt (abs arg))
  4720. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4721. ;; Select the tree
  4722. (org-back-to-heading)
  4723. (setq beg0 (point))
  4724. (save-excursion
  4725. (setq ne-beg (org-back-over-empty-lines))
  4726. (setq beg (point)))
  4727. (save-match-data
  4728. (save-excursion (outline-end-of-heading)
  4729. (setq folded (org-invisible-p)))
  4730. (outline-end-of-subtree))
  4731. (outline-next-heading)
  4732. (setq ne-end (org-back-over-empty-lines))
  4733. (setq end (point))
  4734. (goto-char beg0)
  4735. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4736. ;; include less whitespace
  4737. (save-excursion
  4738. (goto-char beg)
  4739. (forward-line (- ne-beg ne-end))
  4740. (setq beg (point))))
  4741. ;; Find insertion point, with error handling
  4742. (while (> cnt 0)
  4743. (or (and (funcall movfunc) (looking-at outline-regexp))
  4744. (progn (goto-char beg0)
  4745. (error "Cannot move past superior level or buffer limit")))
  4746. (setq cnt (1- cnt)))
  4747. (if (> arg 0)
  4748. ;; Moving forward - still need to move over subtree
  4749. (progn (org-end-of-subtree t t)
  4750. (save-excursion
  4751. (org-back-over-empty-lines)
  4752. (or (bolp) (newline)))))
  4753. (setq ne-ins (org-back-over-empty-lines))
  4754. (move-marker ins-point (point))
  4755. (setq txt (buffer-substring beg end))
  4756. (org-save-markers-in-region beg end)
  4757. (delete-region beg end)
  4758. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4759. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4760. (let ((bbb (point)))
  4761. (insert-before-markers txt)
  4762. (org-reinstall-markers-in-region bbb)
  4763. (move-marker ins-point bbb))
  4764. (or (bolp) (insert "\n"))
  4765. (setq ins-end (point))
  4766. (goto-char ins-point)
  4767. (org-skip-whitespace)
  4768. (when (and (< arg 0)
  4769. (org-first-sibling-p)
  4770. (> ne-ins ne-beg))
  4771. ;; Move whitespace back to beginning
  4772. (save-excursion
  4773. (goto-char ins-end)
  4774. (let ((kill-whole-line t))
  4775. (kill-line (- ne-ins ne-beg)) (point)))
  4776. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4777. (move-marker ins-point nil)
  4778. (org-compact-display-after-subtree-move)
  4779. (org-show-empty-lines-in-parent)
  4780. (unless folded
  4781. (org-show-entry)
  4782. (show-children)
  4783. (org-cycle-hide-drawers 'children))))
  4784. (defvar org-subtree-clip ""
  4785. "Clipboard for cut and paste of subtrees.
  4786. This is actually only a copy of the kill, because we use the normal kill
  4787. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4788. (defvar org-subtree-clip-folded nil
  4789. "Was the last copied subtree folded?
  4790. This is used to fold the tree back after pasting.")
  4791. (defun org-cut-subtree (&optional n)
  4792. "Cut the current subtree into the clipboard.
  4793. With prefix arg N, cut this many sequential subtrees.
  4794. This is a short-hand for marking the subtree and then cutting it."
  4795. (interactive "p")
  4796. (org-copy-subtree n 'cut))
  4797. (defun org-copy-subtree (&optional n cut force-store-markers)
  4798. "Cut the current subtree into the clipboard.
  4799. With prefix arg N, cut this many sequential subtrees.
  4800. This is a short-hand for marking the subtree and then copying it.
  4801. If CUT is non-nil, actually cut the subtree.
  4802. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4803. of some markers in the region, even if CUT is non-nil. This is
  4804. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4805. (interactive "p")
  4806. (let (beg end folded (beg0 (point)))
  4807. (if (interactive-p)
  4808. (org-back-to-heading nil) ; take what looks like a subtree
  4809. (org-back-to-heading t)) ; take what is really there
  4810. (org-back-over-empty-lines)
  4811. (setq beg (point))
  4812. (skip-chars-forward " \t\r\n")
  4813. (save-match-data
  4814. (save-excursion (outline-end-of-heading)
  4815. (setq folded (org-invisible-p)))
  4816. (condition-case nil
  4817. (outline-forward-same-level (1- n))
  4818. (error nil))
  4819. (org-end-of-subtree t t))
  4820. (org-back-over-empty-lines)
  4821. (setq end (point))
  4822. (goto-char beg0)
  4823. (when (> end beg)
  4824. (setq org-subtree-clip-folded folded)
  4825. (when (or cut force-store-markers)
  4826. (org-save-markers-in-region beg end))
  4827. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4828. (setq org-subtree-clip (current-kill 0))
  4829. (message "%s: Subtree(s) with %d characters"
  4830. (if cut "Cut" "Copied")
  4831. (length org-subtree-clip)))))
  4832. (defun org-paste-subtree (&optional level tree for-yank)
  4833. "Paste the clipboard as a subtree, with modification of headline level.
  4834. The entire subtree is promoted or demoted in order to match a new headline
  4835. level.
  4836. If the cursor is at the beginning of a headline, the same level as
  4837. that headline is used to paste the tree
  4838. If not, the new level is derived from the *visible* headings
  4839. before and after the insertion point, and taken to be the inferior headline
  4840. level of the two. So if the previous visible heading is level 3 and the
  4841. next is level 4 (or vice versa), level 4 will be used for insertion.
  4842. This makes sure that the subtree remains an independent subtree and does
  4843. not swallow low level entries.
  4844. You can also force a different level, either by using a numeric prefix
  4845. argument, or by inserting the heading marker by hand. For example, if the
  4846. cursor is after \"*****\", then the tree will be shifted to level 5.
  4847. If optional TREE is given, use this text instead of the kill ring.
  4848. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4849. move back over whitespace before inserting, and move point to the end of
  4850. the inserted text when done."
  4851. (interactive "P")
  4852. (unless (org-kill-is-subtree-p tree)
  4853. (error "%s"
  4854. (substitute-command-keys
  4855. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4856. (let* ((visp (not (org-invisible-p)))
  4857. (txt (or tree (and kill-ring (current-kill 0))))
  4858. (^re (concat "^\\(" outline-regexp "\\)"))
  4859. (re (concat "\\(" outline-regexp "\\)"))
  4860. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4861. (old-level (if (string-match ^re txt)
  4862. (- (match-end 0) (match-beginning 0) 1)
  4863. -1))
  4864. (force-level (cond (level (prefix-numeric-value level))
  4865. ((and (looking-at "[ \t]*$")
  4866. (string-match
  4867. ^re_ (buffer-substring
  4868. (point-at-bol) (point))))
  4869. (- (match-end 1) (match-beginning 1)))
  4870. ((and (bolp)
  4871. (looking-at org-outline-regexp))
  4872. (- (match-end 0) (point) 1))
  4873. (t nil)))
  4874. (previous-level (save-excursion
  4875. (condition-case nil
  4876. (progn
  4877. (outline-previous-visible-heading 1)
  4878. (if (looking-at re)
  4879. (- (match-end 0) (match-beginning 0) 1)
  4880. 1))
  4881. (error 1))))
  4882. (next-level (save-excursion
  4883. (condition-case nil
  4884. (progn
  4885. (or (looking-at outline-regexp)
  4886. (outline-next-visible-heading 1))
  4887. (if (looking-at re)
  4888. (- (match-end 0) (match-beginning 0) 1)
  4889. 1))
  4890. (error 1))))
  4891. (new-level (or force-level (max previous-level next-level)))
  4892. (shift (if (or (= old-level -1)
  4893. (= new-level -1)
  4894. (= old-level new-level))
  4895. 0
  4896. (- new-level old-level)))
  4897. (delta (if (> shift 0) -1 1))
  4898. (func (if (> shift 0) 'org-demote 'org-promote))
  4899. (org-odd-levels-only nil)
  4900. beg end newend)
  4901. ;; Remove the forced level indicator
  4902. (if force-level
  4903. (delete-region (point-at-bol) (point)))
  4904. ;; Paste
  4905. (beginning-of-line 1)
  4906. (unless for-yank (org-back-over-empty-lines))
  4907. (setq beg (point))
  4908. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4909. (insert-before-markers txt)
  4910. (unless (string-match "\n\\'" txt) (insert "\n"))
  4911. (setq newend (point))
  4912. (org-reinstall-markers-in-region beg)
  4913. (setq end (point))
  4914. (goto-char beg)
  4915. (skip-chars-forward " \t\n\r")
  4916. (setq beg (point))
  4917. (if (and (org-invisible-p) visp)
  4918. (save-excursion (outline-show-heading)))
  4919. ;; Shift if necessary
  4920. (unless (= shift 0)
  4921. (save-restriction
  4922. (narrow-to-region beg end)
  4923. (while (not (= shift 0))
  4924. (org-map-region func (point-min) (point-max))
  4925. (setq shift (+ delta shift)))
  4926. (goto-char (point-min))
  4927. (setq newend (point-max))))
  4928. (when (or (interactive-p) for-yank)
  4929. (message "Clipboard pasted as level %d subtree" new-level))
  4930. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4931. kill-ring
  4932. (eq org-subtree-clip (current-kill 0))
  4933. org-subtree-clip-folded)
  4934. ;; The tree was folded before it was killed/copied
  4935. (hide-subtree))
  4936. (and for-yank (goto-char newend))))
  4937. (defun org-kill-is-subtree-p (&optional txt)
  4938. "Check if the current kill is an outline subtree, or a set of trees.
  4939. Returns nil if kill does not start with a headline, or if the first
  4940. headline level is not the largest headline level in the tree.
  4941. So this will actually accept several entries of equal levels as well,
  4942. which is OK for `org-paste-subtree'.
  4943. If optional TXT is given, check this string instead of the current kill."
  4944. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4945. (start-level (and kill
  4946. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4947. org-outline-regexp "\\)")
  4948. kill)
  4949. (- (match-end 2) (match-beginning 2) 1)))
  4950. (re (concat "^" org-outline-regexp))
  4951. (start (1+ (or (match-beginning 2) -1))))
  4952. (if (not start-level)
  4953. (progn
  4954. nil) ;; does not even start with a heading
  4955. (catch 'exit
  4956. (while (setq start (string-match re kill (1+ start)))
  4957. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4958. (throw 'exit nil)))
  4959. t))))
  4960. (defvar org-markers-to-move nil
  4961. "Markers that should be moved with a cut-and-paste operation.
  4962. Those markers are stored together with their positions relative to
  4963. the start of the region.")
  4964. (defun org-save-markers-in-region (beg end)
  4965. "Check markers in region.
  4966. If these markers are between BEG and END, record their position relative
  4967. to BEG, so that after moving the block of text, we can put the markers back
  4968. into place.
  4969. This function gets called just before an entry or tree gets cut from the
  4970. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4971. called immediately, to move the markers with the entries."
  4972. (setq org-markers-to-move nil)
  4973. (when (featurep 'org-clock)
  4974. (org-clock-save-markers-for-cut-and-paste beg end))
  4975. (when (featurep 'org-agenda)
  4976. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4977. (defun org-check-and-save-marker (marker beg end)
  4978. "Check if MARKER is between BEG and END.
  4979. If yes, remember the marker and the distance to BEG."
  4980. (when (and (marker-buffer marker)
  4981. (equal (marker-buffer marker) (current-buffer)))
  4982. (if (and (>= marker beg) (< marker end))
  4983. (push (cons marker (- marker beg)) org-markers-to-move))))
  4984. (defun org-reinstall-markers-in-region (beg)
  4985. "Move all remembered markers to their position relative to BEG."
  4986. (mapc (lambda (x)
  4987. (move-marker (car x) (+ beg (cdr x))))
  4988. org-markers-to-move)
  4989. (setq org-markers-to-move nil))
  4990. (defun org-narrow-to-subtree ()
  4991. "Narrow buffer to the current subtree."
  4992. (interactive)
  4993. (save-excursion
  4994. (save-match-data
  4995. (narrow-to-region
  4996. (progn (org-back-to-heading) (point))
  4997. (progn (org-end-of-subtree t) (point))))))
  4998. ;;; Outline Sorting
  4999. (defun org-sort (with-case)
  5000. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5001. Optional argument WITH-CASE means sort case-sensitively."
  5002. (interactive "P")
  5003. (if (org-at-table-p)
  5004. (org-call-with-arg 'org-table-sort-lines with-case)
  5005. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5006. (defun org-sort-remove-invisible (s)
  5007. (remove-text-properties 0 (length s) org-rm-props s)
  5008. (while (string-match org-bracket-link-regexp s)
  5009. (setq s (replace-match (if (match-end 2)
  5010. (match-string 3 s)
  5011. (match-string 1 s)) t t s)))
  5012. s)
  5013. (defvar org-priority-regexp) ; defined later in the file
  5014. (defun org-sort-entries-or-items
  5015. (&optional with-case sorting-type getkey-func compare-func property)
  5016. "Sort entries on a certain level of an outline tree.
  5017. If there is an active region, the entries in the region are sorted.
  5018. Else, if the cursor is before the first entry, sort the top-level items.
  5019. Else, the children of the entry at point are sorted.
  5020. Sorting can be alphabetically, numerically, and by date/time as given by
  5021. the first time stamp in the entry. The command prompts for the sorting
  5022. type unless it has been given to the function through the SORTING-TYPE
  5023. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5024. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5025. called with point at the beginning of the record. It must return either
  5026. a string or a number that should serve as the sorting key for that record.
  5027. Comparing entries ignores case by default. However, with an optional argument
  5028. WITH-CASE, the sorting considers case as well."
  5029. (interactive "P")
  5030. (let ((case-func (if with-case 'identity 'downcase))
  5031. start beg end stars re re2
  5032. txt what tmp plain-list-p)
  5033. ;; Find beginning and end of region to sort
  5034. (cond
  5035. ((org-region-active-p)
  5036. ;; we will sort the region
  5037. (setq end (region-end)
  5038. what "region")
  5039. (goto-char (region-beginning))
  5040. (if (not (org-on-heading-p)) (outline-next-heading))
  5041. (setq start (point)))
  5042. ((org-at-item-p)
  5043. ;; we will sort this plain list
  5044. (org-beginning-of-item-list) (setq start (point))
  5045. (org-end-of-item-list) (setq end (point))
  5046. (goto-char start)
  5047. (setq plain-list-p t
  5048. what "plain list"))
  5049. ((or (org-on-heading-p)
  5050. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5051. ;; we will sort the children of the current headline
  5052. (org-back-to-heading)
  5053. (setq start (point)
  5054. end (progn (org-end-of-subtree t t)
  5055. (org-back-over-empty-lines)
  5056. (point))
  5057. what "children")
  5058. (goto-char start)
  5059. (show-subtree)
  5060. (outline-next-heading))
  5061. (t
  5062. ;; we will sort the top-level entries in this file
  5063. (goto-char (point-min))
  5064. (or (org-on-heading-p) (outline-next-heading))
  5065. (setq start (point) end (point-max) what "top-level")
  5066. (goto-char start)
  5067. (show-all)))
  5068. (setq beg (point))
  5069. (if (>= beg end) (error "Nothing to sort"))
  5070. (unless plain-list-p
  5071. (looking-at "\\(\\*+\\)")
  5072. (setq stars (match-string 1)
  5073. re (concat "^" (regexp-quote stars) " +")
  5074. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5075. txt (buffer-substring beg end))
  5076. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5077. (if (and (not (equal stars "*")) (string-match re2 txt))
  5078. (error "Region to sort contains a level above the first entry")))
  5079. (unless sorting-type
  5080. (message
  5081. (if plain-list-p
  5082. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5083. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5084. what)
  5085. (setq sorting-type (read-char-exclusive))
  5086. (and (= (downcase sorting-type) ?f)
  5087. (setq getkey-func
  5088. (org-ido-completing-read "Sort using function: "
  5089. obarray 'fboundp t nil nil))
  5090. (setq getkey-func (intern getkey-func)))
  5091. (and (= (downcase sorting-type) ?r)
  5092. (setq property
  5093. (org-ido-completing-read "Property: "
  5094. (mapcar 'list (org-buffer-property-keys t))
  5095. nil t))))
  5096. (message "Sorting entries...")
  5097. (save-restriction
  5098. (narrow-to-region start end)
  5099. (let ((dcst (downcase sorting-type))
  5100. (now (current-time)))
  5101. (sort-subr
  5102. (/= dcst sorting-type)
  5103. ;; This function moves to the beginning character of the "record" to
  5104. ;; be sorted.
  5105. (if plain-list-p
  5106. (lambda nil
  5107. (if (org-at-item-p) t (goto-char (point-max))))
  5108. (lambda nil
  5109. (if (re-search-forward re nil t)
  5110. (goto-char (match-beginning 0))
  5111. (goto-char (point-max)))))
  5112. ;; This function moves to the last character of the "record" being
  5113. ;; sorted.
  5114. (if plain-list-p
  5115. 'org-end-of-item
  5116. (lambda nil
  5117. (save-match-data
  5118. (condition-case nil
  5119. (outline-forward-same-level 1)
  5120. (error
  5121. (goto-char (point-max)))))))
  5122. ;; This function returns the value that gets sorted against.
  5123. (if plain-list-p
  5124. (lambda nil
  5125. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5126. (cond
  5127. ((= dcst ?n)
  5128. (string-to-number (buffer-substring (match-end 0)
  5129. (point-at-eol))))
  5130. ((= dcst ?a)
  5131. (buffer-substring (match-end 0) (point-at-eol)))
  5132. ((= dcst ?t)
  5133. (if (re-search-forward org-ts-regexp
  5134. (point-at-eol) t)
  5135. (org-time-string-to-time (match-string 0))
  5136. now))
  5137. ((= dcst ?f)
  5138. (if getkey-func
  5139. (progn
  5140. (setq tmp (funcall getkey-func))
  5141. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5142. tmp)
  5143. (error "Invalid key function `%s'" getkey-func)))
  5144. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5145. (lambda nil
  5146. (cond
  5147. ((= dcst ?n)
  5148. (if (looking-at org-complex-heading-regexp)
  5149. (string-to-number (match-string 4))
  5150. nil))
  5151. ((= dcst ?a)
  5152. (if (looking-at org-complex-heading-regexp)
  5153. (funcall case-func (match-string 4))
  5154. nil))
  5155. ((= dcst ?t)
  5156. (if (re-search-forward org-ts-regexp
  5157. (save-excursion
  5158. (forward-line 2)
  5159. (point)) t)
  5160. (org-time-string-to-time (match-string 0))
  5161. now))
  5162. ((= dcst ?p)
  5163. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5164. (string-to-char (match-string 2))
  5165. org-default-priority))
  5166. ((= dcst ?r)
  5167. (or (org-entry-get nil property) ""))
  5168. ((= dcst ?o)
  5169. (if (looking-at org-complex-heading-regexp)
  5170. (- 9999 (length (member (match-string 2)
  5171. org-todo-keywords-1)))))
  5172. ((= dcst ?f)
  5173. (if getkey-func
  5174. (progn
  5175. (setq tmp (funcall getkey-func))
  5176. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5177. tmp)
  5178. (error "Invalid key function `%s'" getkey-func)))
  5179. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5180. nil
  5181. (cond
  5182. ((= dcst ?a) 'string<)
  5183. ((= dcst ?t) 'time-less-p)
  5184. ((= dcst ?f) compare-func)
  5185. (t nil)))))
  5186. (message "Sorting entries...done")))
  5187. (defun org-do-sort (table what &optional with-case sorting-type)
  5188. "Sort TABLE of WHAT according to SORTING-TYPE.
  5189. The user will be prompted for the SORTING-TYPE if the call to this
  5190. function does not specify it. WHAT is only for the prompt, to indicate
  5191. what is being sorted. The sorting key will be extracted from
  5192. the car of the elements of the table.
  5193. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5194. (unless sorting-type
  5195. (message
  5196. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5197. what)
  5198. (setq sorting-type (read-char-exclusive)))
  5199. (let ((dcst (downcase sorting-type))
  5200. extractfun comparefun)
  5201. ;; Define the appropriate functions
  5202. (cond
  5203. ((= dcst ?n)
  5204. (setq extractfun 'string-to-number
  5205. comparefun (if (= dcst sorting-type) '< '>)))
  5206. ((= dcst ?a)
  5207. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5208. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5209. comparefun (if (= dcst sorting-type)
  5210. 'string<
  5211. (lambda (a b) (and (not (string< a b))
  5212. (not (string= a b)))))))
  5213. ((= dcst ?t)
  5214. (setq extractfun
  5215. (lambda (x)
  5216. (if (string-match org-ts-regexp x)
  5217. (time-to-seconds
  5218. (org-time-string-to-time (match-string 0 x)))
  5219. 0))
  5220. comparefun (if (= dcst sorting-type) '< '>)))
  5221. (t (error "Invalid sorting type `%c'" sorting-type)))
  5222. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5223. table)
  5224. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5225. ;;; Editing source examples
  5226. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5227. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5228. (defvar org-edit-src-force-single-line nil)
  5229. (defvar org-edit-src-from-org-mode nil)
  5230. (defvar org-edit-src-picture nil)
  5231. (define-minor-mode org-exit-edit-mode
  5232. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5233. (defun org-edit-src-code ()
  5234. "Edit the source code example at point.
  5235. An indirect buffer is created, and that buffer is then narrowed to the
  5236. example at point and switched to the correct language mode. When done,
  5237. exit by killing the buffer with \\[org-edit-src-exit]."
  5238. (interactive)
  5239. (let ((line (org-current-line))
  5240. (case-fold-search t)
  5241. (msg (substitute-command-keys
  5242. "Edit, then exit with C-c ' (C-c and single quote)"))
  5243. (info (org-edit-src-find-region-and-lang))
  5244. (org-mode-p (eq major-mode 'org-mode))
  5245. beg end lang lang-f single lfmt)
  5246. (if (not info)
  5247. nil
  5248. (setq beg (nth 0 info)
  5249. end (nth 1 info)
  5250. lang (nth 2 info)
  5251. single (nth 3 info)
  5252. lfmt (nth 4 info)
  5253. lang-f (intern (concat lang "-mode")))
  5254. (unless (functionp lang-f)
  5255. (error "No such language mode: %s" lang-f))
  5256. (goto-line line)
  5257. (if (get-buffer "*Org Edit Src Example*")
  5258. (kill-buffer "*Org Edit Src Example*"))
  5259. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5260. "*Org Edit Src Example*"))
  5261. (narrow-to-region beg end)
  5262. (remove-text-properties beg end '(display nil invisible nil
  5263. intangible nil))
  5264. (let ((org-inhibit-startup t))
  5265. (funcall lang-f))
  5266. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5267. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5268. (when lfmt
  5269. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5270. (when org-mode-p
  5271. (goto-char (point-min))
  5272. (while (re-search-forward "^," nil t)
  5273. (replace-match "")))
  5274. (goto-line line)
  5275. (org-exit-edit-mode)
  5276. (org-set-local 'header-line-format msg)
  5277. (message "%s" msg)
  5278. t)))
  5279. (defun org-edit-fixed-width-region ()
  5280. "Edit the fixed-width ascii drawing at point.
  5281. This must be a region where each line starts with a colon followed by
  5282. a space character.
  5283. An indirect buffer is created, and that buffer is then narrowed to the
  5284. example at point and switched to artist-mode. When done,
  5285. exit by killing the buffer with \\[org-edit-src-exit]."
  5286. (interactive)
  5287. (let ((line (org-current-line))
  5288. (case-fold-search t)
  5289. (msg (substitute-command-keys
  5290. "Edit, then exit with C-c ' (C-c and single quote)"))
  5291. (org-mode-p (eq major-mode 'org-mode))
  5292. beg end)
  5293. (beginning-of-line 1)
  5294. (if (looking-at "[ \t]*[^:\n \t]")
  5295. nil
  5296. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5297. (setq beg (point) end beg)
  5298. (save-excursion
  5299. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5300. (setq beg (point-at-bol 2))
  5301. (setq beg (point))))
  5302. (save-excursion
  5303. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5304. (setq end (1- (match-beginning 0)))
  5305. (setq end (point))))
  5306. (goto-line line))
  5307. (if (get-buffer "*Org Edit Picture*")
  5308. (kill-buffer "*Org Edit Picture*"))
  5309. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5310. "*Org Edit Picture*"))
  5311. (narrow-to-region beg end)
  5312. (remove-text-properties beg end '(display nil invisible nil
  5313. intangible nil))
  5314. (when (fboundp 'font-lock-unfontify-region)
  5315. (font-lock-unfontify-region (point-min) (point-max)))
  5316. (cond
  5317. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5318. (fundamental-mode)
  5319. (artist-mode 1))
  5320. (t (funcall org-edit-fixed-width-region-mode)))
  5321. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5322. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5323. (set (make-local-variable 'org-edit-src-picture) t)
  5324. (goto-char (point-min))
  5325. (while (re-search-forward "^[ \t]*: ?" nil t)
  5326. (replace-match ""))
  5327. (goto-line line)
  5328. (org-exit-edit-mode)
  5329. (org-set-local 'header-line-format msg)
  5330. (message "%s" msg)
  5331. t)))
  5332. (defun org-edit-src-find-region-and-lang ()
  5333. "Find the region and language for a local edit.
  5334. Return a list with beginning and end of the region, a string representing
  5335. the language, a switch telling of the content should be in a single line."
  5336. (let ((re-list
  5337. (append
  5338. org-edit-src-region-extra
  5339. '(
  5340. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5341. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5342. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5343. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5344. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5345. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5346. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5347. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5348. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5349. ("^#\\+html:" "\n" "html" single-line)
  5350. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5351. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5352. ("^#\\+latex:" "\n" "latex" single-line)
  5353. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5354. ("^#\\+ascii:" "\n" "ascii" single-line)
  5355. )))
  5356. (pos (point))
  5357. re1 re2 single beg end lang lfmt match-re1)
  5358. (catch 'exit
  5359. (while (setq entry (pop re-list))
  5360. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5361. single (nth 3 entry))
  5362. (save-excursion
  5363. (if (or (looking-at re1)
  5364. (re-search-backward re1 nil t))
  5365. (progn
  5366. (setq match-re1 (match-string 0))
  5367. (setq beg (match-end 0)
  5368. lang (org-edit-src-get-lang lang)
  5369. lfmt (org-edit-src-get-label-format match-re1))
  5370. (if (and (re-search-forward re2 nil t)
  5371. (>= (match-end 0) pos))
  5372. (throw 'exit (list beg (match-beginning 0)
  5373. lang single lfmt))))
  5374. (if (or (looking-at re2)
  5375. (re-search-forward re2 nil t))
  5376. (progn
  5377. (setq end (match-beginning 0))
  5378. (if (and (re-search-backward re1 nil t)
  5379. (<= (match-beginning 0) pos))
  5380. (progn
  5381. (setq lfmt (org-edit-src-get-label-format
  5382. (match-string 0)))
  5383. (throw 'exit
  5384. (list (match-end 0) end
  5385. (org-edit-src-get-lang lang)
  5386. single lfmt))))))))))))
  5387. (defun org-edit-src-get-lang (lang)
  5388. "Extract the src language."
  5389. (let ((m (match-string 0)))
  5390. (cond
  5391. ((stringp lang) lang)
  5392. ((integerp lang) (match-string lang))
  5393. ((and (eq lang 'lang)
  5394. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5395. (match-string 1 m))
  5396. ((and (eq lang 'style)
  5397. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5398. (match-string 1 m))
  5399. (t "fundamental"))))
  5400. (defun org-edit-src-get-label-format (s)
  5401. "Extract the label format."
  5402. (save-match-data
  5403. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5404. (match-string 1 s))))
  5405. (defun org-edit-src-exit ()
  5406. "Exit special edit and protect problematic lines."
  5407. (interactive)
  5408. (unless (buffer-base-buffer (current-buffer))
  5409. (error "This is not an indirect buffer, something is wrong..."))
  5410. (unless (> (point-min) 1)
  5411. (error "This buffer is not narrowed, something is wrong..."))
  5412. (goto-char (point-min))
  5413. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5414. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5415. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5416. (goto-char (point-min))
  5417. (while (re-search-forward "\n" nil t)
  5418. (replace-match " "))
  5419. (goto-char (point-min))
  5420. (if (looking-at "\\s-*") (replace-match " "))
  5421. (if (re-search-forward "\\s-+\\'" nil t)
  5422. (replace-match "")))
  5423. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5424. (goto-char (point-min))
  5425. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5426. (replace-match ",\\1"))
  5427. (when font-lock-mode
  5428. (font-lock-unfontify-region (point-min) (point-max)))
  5429. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5430. (when (org-bound-and-true-p org-edit-src-picture)
  5431. (untabify (point-min) (point-max))
  5432. (goto-char (point-min))
  5433. (while (re-search-forward "^" nil t)
  5434. (replace-match ": "))
  5435. (when font-lock-mode
  5436. (font-lock-unfontify-region (point-min) (point-max)))
  5437. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5438. (kill-buffer (current-buffer))
  5439. (and (org-mode-p) (org-restart-font-lock)))
  5440. ;;; The orgstruct minor mode
  5441. ;; Define a minor mode which can be used in other modes in order to
  5442. ;; integrate the org-mode structure editing commands.
  5443. ;; This is really a hack, because the org-mode structure commands use
  5444. ;; keys which normally belong to the major mode. Here is how it
  5445. ;; works: The minor mode defines all the keys necessary to operate the
  5446. ;; structure commands, but wraps the commands into a function which
  5447. ;; tests if the cursor is currently at a headline or a plain list
  5448. ;; item. If that is the case, the structure command is used,
  5449. ;; temporarily setting many Org-mode variables like regular
  5450. ;; expressions for filling etc. However, when any of those keys is
  5451. ;; used at a different location, function uses `key-binding' to look
  5452. ;; up if the key has an associated command in another currently active
  5453. ;; keymap (minor modes, major mode, global), and executes that
  5454. ;; command. There might be problems if any of the keys is otherwise
  5455. ;; used as a prefix key.
  5456. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5457. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5458. ;; addresses this by checking explicitly for both bindings.
  5459. (defvar orgstruct-mode-map (make-sparse-keymap)
  5460. "Keymap for the minor `orgstruct-mode'.")
  5461. (defvar org-local-vars nil
  5462. "List of local variables, for use by `orgstruct-mode'")
  5463. ;;;###autoload
  5464. (define-minor-mode orgstruct-mode
  5465. "Toggle the minor more `orgstruct-mode'.
  5466. This mode is for using Org-mode structure commands in other modes.
  5467. The following key behave as if Org-mode was active, if the cursor
  5468. is on a headline, or on a plain list item (both in the definition
  5469. of Org-mode).
  5470. M-up Move entry/item up
  5471. M-down Move entry/item down
  5472. M-left Promote
  5473. M-right Demote
  5474. M-S-up Move entry/item up
  5475. M-S-down Move entry/item down
  5476. M-S-left Promote subtree
  5477. M-S-right Demote subtree
  5478. M-q Fill paragraph and items like in Org-mode
  5479. C-c ^ Sort entries
  5480. C-c - Cycle list bullet
  5481. TAB Cycle item visibility
  5482. M-RET Insert new heading/item
  5483. S-M-RET Insert new TODO heading / Checkbox item
  5484. C-c C-c Set tags / toggle checkbox"
  5485. nil " OrgStruct" nil
  5486. (org-load-modules-maybe)
  5487. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5488. ;;;###autoload
  5489. (defun turn-on-orgstruct ()
  5490. "Unconditionally turn on `orgstruct-mode'."
  5491. (orgstruct-mode 1))
  5492. ;;;###autoload
  5493. (defun turn-on-orgstruct++ ()
  5494. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5495. In addition to setting orgstruct-mode, this also exports all indentation and
  5496. autofilling variables from org-mode into the buffer. Note that turning
  5497. off orgstruct-mode will *not* remove these additional settings."
  5498. (orgstruct-mode 1)
  5499. (let (var val)
  5500. (mapc
  5501. (lambda (x)
  5502. (when (string-match
  5503. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5504. (symbol-name (car x)))
  5505. (setq var (car x) val (nth 1 x))
  5506. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5507. org-local-vars)))
  5508. (defun orgstruct-error ()
  5509. "Error when there is no default binding for a structure key."
  5510. (interactive)
  5511. (error "This key has no function outside structure elements"))
  5512. (defun orgstruct-setup ()
  5513. "Setup orgstruct keymaps."
  5514. (let ((nfunc 0)
  5515. (bindings
  5516. (list
  5517. '([(meta up)] org-metaup)
  5518. '([(meta down)] org-metadown)
  5519. '([(meta left)] org-metaleft)
  5520. '([(meta right)] org-metaright)
  5521. '([(meta shift up)] org-shiftmetaup)
  5522. '([(meta shift down)] org-shiftmetadown)
  5523. '([(meta shift left)] org-shiftmetaleft)
  5524. '([(meta shift right)] org-shiftmetaright)
  5525. '([(shift up)] org-shiftup)
  5526. '([(shift down)] org-shiftdown)
  5527. '([(shift left)] org-shiftleft)
  5528. '([(shift right)] org-shiftright)
  5529. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5530. '("\M-q" fill-paragraph)
  5531. '("\C-c^" org-sort)
  5532. '("\C-c-" org-cycle-list-bullet)))
  5533. elt key fun cmd)
  5534. (while (setq elt (pop bindings))
  5535. (setq nfunc (1+ nfunc))
  5536. (setq key (org-key (car elt))
  5537. fun (nth 1 elt)
  5538. cmd (orgstruct-make-binding fun nfunc key))
  5539. (org-defkey orgstruct-mode-map key cmd))
  5540. ;; Special treatment needed for TAB and RET
  5541. (org-defkey orgstruct-mode-map [(tab)]
  5542. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5543. (org-defkey orgstruct-mode-map "\C-i"
  5544. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5545. (org-defkey orgstruct-mode-map "\M-\C-m"
  5546. (orgstruct-make-binding 'org-insert-heading 105
  5547. "\M-\C-m" [(meta return)]))
  5548. (org-defkey orgstruct-mode-map [(meta return)]
  5549. (orgstruct-make-binding 'org-insert-heading 106
  5550. [(meta return)] "\M-\C-m"))
  5551. (org-defkey orgstruct-mode-map [(shift meta return)]
  5552. (orgstruct-make-binding 'org-insert-todo-heading 107
  5553. [(meta return)] "\M-\C-m"))
  5554. (unless org-local-vars
  5555. (setq org-local-vars (org-get-local-variables)))
  5556. t))
  5557. (defun orgstruct-make-binding (fun n &rest keys)
  5558. "Create a function for binding in the structure minor mode.
  5559. FUN is the command to call inside a table. N is used to create a unique
  5560. command name. KEYS are keys that should be checked in for a command
  5561. to execute outside of tables."
  5562. (eval
  5563. (list 'defun
  5564. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5565. '(arg)
  5566. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5567. "Outside of structure, run the binding of `"
  5568. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5569. "'.")
  5570. '(interactive "p")
  5571. (list 'if
  5572. '(org-context-p 'headline 'item)
  5573. (list 'org-run-like-in-org-mode (list 'quote fun))
  5574. (list 'let '(orgstruct-mode)
  5575. (list 'call-interactively
  5576. (append '(or)
  5577. (mapcar (lambda (k)
  5578. (list 'key-binding k))
  5579. keys)
  5580. '('orgstruct-error))))))))
  5581. (defun org-context-p (&rest contexts)
  5582. "Check if local context is any of CONTEXTS.
  5583. Possible values in the list of contexts are `table', `headline', and `item'."
  5584. (let ((pos (point)))
  5585. (goto-char (point-at-bol))
  5586. (prog1 (or (and (memq 'table contexts)
  5587. (looking-at "[ \t]*|"))
  5588. (and (memq 'headline contexts)
  5589. ;;????????? (looking-at "\\*+"))
  5590. (looking-at outline-regexp))
  5591. (and (memq 'item contexts)
  5592. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5593. (goto-char pos))))
  5594. (defun org-get-local-variables ()
  5595. "Return a list of all local variables in an org-mode buffer."
  5596. (let (varlist)
  5597. (with-current-buffer (get-buffer-create "*Org tmp*")
  5598. (erase-buffer)
  5599. (org-mode)
  5600. (setq varlist (buffer-local-variables)))
  5601. (kill-buffer "*Org tmp*")
  5602. (delq nil
  5603. (mapcar
  5604. (lambda (x)
  5605. (setq x
  5606. (if (symbolp x)
  5607. (list x)
  5608. (list (car x) (list 'quote (cdr x)))))
  5609. (if (string-match
  5610. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5611. (symbol-name (car x)))
  5612. x nil))
  5613. varlist))))
  5614. ;;;###autoload
  5615. (defun org-run-like-in-org-mode (cmd)
  5616. (org-load-modules-maybe)
  5617. (unless org-local-vars
  5618. (setq org-local-vars (org-get-local-variables)))
  5619. (eval (list 'let org-local-vars
  5620. (list 'call-interactively (list 'quote cmd)))))
  5621. ;;;; Archiving
  5622. (defun org-get-category (&optional pos)
  5623. "Get the category applying to position POS."
  5624. (get-text-property (or pos (point)) 'org-category))
  5625. (defun org-refresh-category-properties ()
  5626. "Refresh category text properties in the buffer."
  5627. (let ((def-cat (cond
  5628. ((null org-category)
  5629. (if buffer-file-name
  5630. (file-name-sans-extension
  5631. (file-name-nondirectory buffer-file-name))
  5632. "???"))
  5633. ((symbolp org-category) (symbol-name org-category))
  5634. (t org-category)))
  5635. beg end cat pos optionp)
  5636. (org-unmodified
  5637. (save-excursion
  5638. (save-restriction
  5639. (widen)
  5640. (goto-char (point-min))
  5641. (put-text-property (point) (point-max) 'org-category def-cat)
  5642. (while (re-search-forward
  5643. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5644. (setq pos (match-end 0)
  5645. optionp (equal (char-after (match-beginning 0)) ?#)
  5646. cat (org-trim (match-string 2)))
  5647. (if optionp
  5648. (setq beg (point-at-bol) end (point-max))
  5649. (org-back-to-heading t)
  5650. (setq beg (point) end (org-end-of-subtree t t)))
  5651. (put-text-property beg end 'org-category cat)
  5652. (goto-char pos)))))))
  5653. ;;;; Link Stuff
  5654. ;;; Link abbreviations
  5655. (defun org-link-expand-abbrev (link)
  5656. "Apply replacements as defined in `org-link-abbrev-alist."
  5657. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5658. (let* ((key (match-string 1 link))
  5659. (as (or (assoc key org-link-abbrev-alist-local)
  5660. (assoc key org-link-abbrev-alist)))
  5661. (tag (and (match-end 2) (match-string 3 link)))
  5662. rpl)
  5663. (if (not as)
  5664. link
  5665. (setq rpl (cdr as))
  5666. (cond
  5667. ((symbolp rpl) (funcall rpl tag))
  5668. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5669. ((string-match "%h" rpl)
  5670. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5671. (t (concat rpl tag)))))
  5672. link))
  5673. ;;; Storing and inserting links
  5674. (defvar org-insert-link-history nil
  5675. "Minibuffer history for links inserted with `org-insert-link'.")
  5676. (defvar org-stored-links nil
  5677. "Contains the links stored with `org-store-link'.")
  5678. (defvar org-store-link-plist nil
  5679. "Plist with info about the most recently link created with `org-store-link'.")
  5680. (defvar org-link-protocols nil
  5681. "Link protocols added to Org-mode using `org-add-link-type'.")
  5682. (defvar org-store-link-functions nil
  5683. "List of functions that are called to create and store a link.
  5684. Each function will be called in turn until one returns a non-nil
  5685. value. Each function should check if it is responsible for creating
  5686. this link (for example by looking at the major mode).
  5687. If not, it must exit and return nil.
  5688. If yes, it should return a non-nil value after a calling
  5689. `org-store-link-props' with a list of properties and values.
  5690. Special properties are:
  5691. :type The link prefix. like \"http\". This must be given.
  5692. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5693. This is obligatory as well.
  5694. :description Optional default description for the second pair
  5695. of brackets in an Org-mode link. The user can still change
  5696. this when inserting this link into an Org-mode buffer.
  5697. In addition to these, any additional properties can be specified
  5698. and then used in remember templates.")
  5699. (defun org-add-link-type (type &optional follow export)
  5700. "Add TYPE to the list of `org-link-types'.
  5701. Re-compute all regular expressions depending on `org-link-types'
  5702. FOLLOW and EXPORT are two functions.
  5703. FOLLOW should take the link path as the single argument and do whatever
  5704. is necessary to follow the link, for example find a file or display
  5705. a mail message.
  5706. EXPORT should format the link path for export to one of the export formats.
  5707. It should be a function accepting three arguments:
  5708. path the path of the link, the text after the prefix (like \"http:\")
  5709. desc the description of the link, if any, nil if there was no description
  5710. format the export format, a symbol like `html' or `latex'.
  5711. The function may use the FORMAT information to return different values
  5712. depending on the format. The return value will be put literally into
  5713. the exported file.
  5714. Org-mode has a built-in default for exporting links. If you are happy with
  5715. this default, there is no need to define an export function for the link
  5716. type. For a simple example of an export function, see `org-bbdb.el'."
  5717. (add-to-list 'org-link-types type t)
  5718. (org-make-link-regexps)
  5719. (if (assoc type org-link-protocols)
  5720. (setcdr (assoc type org-link-protocols) (list follow export))
  5721. (push (list type follow export) org-link-protocols)))
  5722. ;;;###autoload
  5723. (defun org-store-link (arg)
  5724. "\\<org-mode-map>Store an org-link to the current location.
  5725. This link is added to `org-stored-links' and can later be inserted
  5726. into an org-buffer with \\[org-insert-link].
  5727. For some link types, a prefix arg is interpreted:
  5728. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5729. For file links, arg negates `org-context-in-file-links'."
  5730. (interactive "P")
  5731. (org-load-modules-maybe)
  5732. (setq org-store-link-plist nil) ; reset
  5733. (let (link cpltxt desc description search txt)
  5734. (cond
  5735. ((run-hook-with-args-until-success 'org-store-link-functions)
  5736. (setq link (plist-get org-store-link-plist :link)
  5737. desc (or (plist-get org-store-link-plist :description) link)))
  5738. ((equal (buffer-name) "*Org Edit Src Example*")
  5739. (let (label gc)
  5740. (while (or (not label)
  5741. (save-excursion
  5742. (save-restriction
  5743. (widen)
  5744. (goto-char (point-min))
  5745. (re-search-forward
  5746. (regexp-quote (format org-coderef-label-format label))
  5747. nil t))))
  5748. (when label (message "Label exists already") (sit-for 2))
  5749. (setq label (read-string "Code line label: " label)))
  5750. (end-of-line 1)
  5751. (setq link (format org-coderef-label-format label))
  5752. (setq gc (- 79 (length link)))
  5753. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5754. (insert link)
  5755. (setq link (concat "(" label ")") desc nil)))
  5756. ((eq major-mode 'calendar-mode)
  5757. (let ((cd (calendar-cursor-to-date)))
  5758. (setq link
  5759. (format-time-string
  5760. (car org-time-stamp-formats)
  5761. (apply 'encode-time
  5762. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5763. nil nil nil))))
  5764. (org-store-link-props :type "calendar" :date cd)))
  5765. ((eq major-mode 'w3-mode)
  5766. (setq cpltxt (url-view-url t)
  5767. link (org-make-link cpltxt))
  5768. (org-store-link-props :type "w3" :url (url-view-url t)))
  5769. ((eq major-mode 'w3m-mode)
  5770. (setq cpltxt (or w3m-current-title w3m-current-url)
  5771. link (org-make-link w3m-current-url))
  5772. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5773. ((setq search (run-hook-with-args-until-success
  5774. 'org-create-file-search-functions))
  5775. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5776. "::" search))
  5777. (setq cpltxt (or description link)))
  5778. ((eq major-mode 'image-mode)
  5779. (setq cpltxt (concat "file:"
  5780. (abbreviate-file-name buffer-file-name))
  5781. link (org-make-link cpltxt))
  5782. (org-store-link-props :type "image" :file buffer-file-name))
  5783. ((eq major-mode 'dired-mode)
  5784. ;; link to the file in the current line
  5785. (setq cpltxt (concat "file:"
  5786. (abbreviate-file-name
  5787. (expand-file-name
  5788. (dired-get-filename nil t))))
  5789. link (org-make-link cpltxt)))
  5790. ((and buffer-file-name (org-mode-p))
  5791. (cond
  5792. ((org-in-regexp "<<\\(.*?\\)>>")
  5793. (setq cpltxt
  5794. (concat "file:"
  5795. (abbreviate-file-name buffer-file-name)
  5796. "::" (match-string 1))
  5797. link (org-make-link cpltxt)))
  5798. ((and (featurep 'org-id)
  5799. (or (eq org-link-to-org-use-id t)
  5800. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5801. (interactive-p))
  5802. (and org-link-to-org-use-id
  5803. (condition-case nil
  5804. (org-entry-get nil "ID")
  5805. (error nil)))))
  5806. ;; We can make a link using the ID.
  5807. (setq link (condition-case nil
  5808. (prog1 (org-id-store-link)
  5809. (setq desc (plist-get org-store-link-plist
  5810. :description)))
  5811. (error
  5812. ;; probably before first headline, link to file only
  5813. (concat "file:"
  5814. (abbreviate-file-name buffer-file-name))))))
  5815. (t
  5816. ;; Just link to current headline
  5817. (setq cpltxt (concat "file:"
  5818. (abbreviate-file-name buffer-file-name)))
  5819. ;; Add a context search string
  5820. (when (org-xor org-context-in-file-links arg)
  5821. (setq txt (cond
  5822. ((org-on-heading-p) nil)
  5823. ((org-region-active-p)
  5824. (buffer-substring (region-beginning) (region-end)))
  5825. (t nil)))
  5826. (when (or (null txt) (string-match "\\S-" txt))
  5827. (setq cpltxt
  5828. (concat cpltxt "::"
  5829. (condition-case nil
  5830. (org-make-org-heading-search-string txt)
  5831. (error "")))
  5832. desc "NONE")))
  5833. (if (string-match "::\\'" cpltxt)
  5834. (setq cpltxt (substring cpltxt 0 -2)))
  5835. (setq link (org-make-link cpltxt)))))
  5836. ((buffer-file-name (buffer-base-buffer))
  5837. ;; Just link to this file here.
  5838. (setq cpltxt (concat "file:"
  5839. (abbreviate-file-name
  5840. (buffer-file-name (buffer-base-buffer)))))
  5841. ;; Add a context string
  5842. (when (org-xor org-context-in-file-links arg)
  5843. (setq txt (if (org-region-active-p)
  5844. (buffer-substring (region-beginning) (region-end))
  5845. (buffer-substring (point-at-bol) (point-at-eol))))
  5846. ;; Only use search option if there is some text.
  5847. (when (string-match "\\S-" txt)
  5848. (setq cpltxt
  5849. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5850. desc "NONE")))
  5851. (setq link (org-make-link cpltxt)))
  5852. ((interactive-p)
  5853. (error "Cannot link to a buffer which is not visiting a file"))
  5854. (t (setq link nil)))
  5855. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5856. (setq link (or link cpltxt)
  5857. desc (or desc cpltxt))
  5858. (if (equal desc "NONE") (setq desc nil))
  5859. (if (and (interactive-p) link)
  5860. (progn
  5861. (setq org-stored-links
  5862. (cons (list link desc) org-stored-links))
  5863. (message "Stored: %s" (or desc link)))
  5864. (and link (org-make-link-string link desc)))))
  5865. (defun org-store-link-props (&rest plist)
  5866. "Store link properties, extract names and addresses."
  5867. (let (x adr)
  5868. (when (setq x (plist-get plist :from))
  5869. (setq adr (mail-extract-address-components x))
  5870. (setq plist (plist-put plist :fromname (car adr)))
  5871. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5872. (when (setq x (plist-get plist :to))
  5873. (setq adr (mail-extract-address-components x))
  5874. (setq plist (plist-put plist :toname (car adr)))
  5875. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5876. (let ((from (plist-get plist :from))
  5877. (to (plist-get plist :to)))
  5878. (when (and from to org-from-is-user-regexp)
  5879. (setq plist
  5880. (plist-put plist :fromto
  5881. (if (string-match org-from-is-user-regexp from)
  5882. (concat "to %t")
  5883. (concat "from %f"))))))
  5884. (setq org-store-link-plist plist))
  5885. (defun org-add-link-props (&rest plist)
  5886. "Add these properties to the link property list."
  5887. (let (key value)
  5888. (while plist
  5889. (setq key (pop plist) value (pop plist))
  5890. (setq org-store-link-plist
  5891. (plist-put org-store-link-plist key value)))))
  5892. (defun org-email-link-description (&optional fmt)
  5893. "Return the description part of an email link.
  5894. This takes information from `org-store-link-plist' and formats it
  5895. according to FMT (default from `org-email-link-description-format')."
  5896. (setq fmt (or fmt org-email-link-description-format))
  5897. (let* ((p org-store-link-plist)
  5898. (to (plist-get p :toaddress))
  5899. (from (plist-get p :fromaddress))
  5900. (table
  5901. (list
  5902. (cons "%c" (plist-get p :fromto))
  5903. (cons "%F" (plist-get p :from))
  5904. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5905. (cons "%T" (plist-get p :to))
  5906. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5907. (cons "%s" (plist-get p :subject))
  5908. (cons "%m" (plist-get p :message-id)))))
  5909. (when (string-match "%c" fmt)
  5910. ;; Check if the user wrote this message
  5911. (if (and org-from-is-user-regexp from to
  5912. (save-match-data (string-match org-from-is-user-regexp from)))
  5913. (setq fmt (replace-match "to %t" t t fmt))
  5914. (setq fmt (replace-match "from %f" t t fmt))))
  5915. (org-replace-escapes fmt table)))
  5916. (defun org-make-org-heading-search-string (&optional string heading)
  5917. "Make search string for STRING or current headline."
  5918. (interactive)
  5919. (let ((s (or string (org-get-heading))))
  5920. (unless (and string (not heading))
  5921. ;; We are using a headline, clean up garbage in there.
  5922. (if (string-match org-todo-regexp s)
  5923. (setq s (replace-match "" t t s)))
  5924. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5925. (setq s (replace-match "" t t s)))
  5926. (setq s (org-trim s))
  5927. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5928. org-comment-string "\\)") s)
  5929. (setq s (replace-match "" t t s)))
  5930. (while (string-match org-ts-regexp s)
  5931. (setq s (replace-match "" t t s))))
  5932. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5933. (setq s (replace-match " " t t s)))
  5934. (or string (setq s (concat "*" s))) ; Add * for headlines
  5935. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5936. (defun org-make-link (&rest strings)
  5937. "Concatenate STRINGS."
  5938. (apply 'concat strings))
  5939. (defun org-make-link-string (link &optional description)
  5940. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5941. (unless (string-match "\\S-" link)
  5942. (error "Empty link"))
  5943. (when (stringp description)
  5944. ;; Remove brackets from the description, they are fatal.
  5945. (while (string-match "\\[" description)
  5946. (setq description (replace-match "{" t t description)))
  5947. (while (string-match "\\]" description)
  5948. (setq description (replace-match "}" t t description))))
  5949. (when (equal (org-link-escape link) description)
  5950. ;; No description needed, it is identical
  5951. (setq description nil))
  5952. (when (and (not description)
  5953. (not (equal link (org-link-escape link))))
  5954. (setq description (org-extract-attributes link)))
  5955. (concat "[[" (org-link-escape link) "]"
  5956. (if description (concat "[" description "]") "")
  5957. "]"))
  5958. (defconst org-link-escape-chars
  5959. '((?\ . "%20")
  5960. (?\[ . "%5B")
  5961. (?\] . "%5D")
  5962. (?\340 . "%E0") ; `a
  5963. (?\342 . "%E2") ; ^a
  5964. (?\347 . "%E7") ; ,c
  5965. (?\350 . "%E8") ; `e
  5966. (?\351 . "%E9") ; 'e
  5967. (?\352 . "%EA") ; ^e
  5968. (?\356 . "%EE") ; ^i
  5969. (?\364 . "%F4") ; ^o
  5970. (?\371 . "%F9") ; `u
  5971. (?\373 . "%FB") ; ^u
  5972. (?\; . "%3B")
  5973. (?? . "%3F")
  5974. (?= . "%3D")
  5975. (?+ . "%2B")
  5976. )
  5977. "Association list of escapes for some characters problematic in links.
  5978. This is the list that is used for internal purposes.")
  5979. (defconst org-link-escape-chars-browser
  5980. '((?\ . "%20")) ; 32 for the SPC char
  5981. "Association list of escapes for some characters problematic in links.
  5982. This is the list that is used before handing over to the browser.")
  5983. (defun org-link-escape (text &optional table)
  5984. "Escape characters in TEXT that are problematic for links."
  5985. (setq table (or table org-link-escape-chars))
  5986. (when text
  5987. (let ((re (mapconcat (lambda (x) (regexp-quote
  5988. (char-to-string (car x))))
  5989. table "\\|")))
  5990. (while (string-match re text)
  5991. (setq text
  5992. (replace-match
  5993. (cdr (assoc (string-to-char (match-string 0 text))
  5994. table))
  5995. t t text)))
  5996. text)))
  5997. (defun org-link-unescape (text &optional table)
  5998. "Reverse the action of `org-link-escape'."
  5999. (setq table (or table org-link-escape-chars))
  6000. (when text
  6001. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6002. table "\\|")))
  6003. (while (string-match re text)
  6004. (setq text
  6005. (replace-match
  6006. (char-to-string (car (rassoc (match-string 0 text) table)))
  6007. t t text)))
  6008. text)))
  6009. (defun org-xor (a b)
  6010. "Exclusive or."
  6011. (if a (not b) b))
  6012. (defun org-fixup-message-id-for-http (s)
  6013. "Replace special characters in a message id, so it can be used in an http query."
  6014. (while (string-match "<" s)
  6015. (setq s (replace-match "%3C" t t s)))
  6016. (while (string-match ">" s)
  6017. (setq s (replace-match "%3E" t t s)))
  6018. (while (string-match "@" s)
  6019. (setq s (replace-match "%40" t t s)))
  6020. s)
  6021. ;;;###autoload
  6022. (defun org-insert-link-global ()
  6023. "Insert a link like Org-mode does.
  6024. This command can be called in any mode to insert a link in Org-mode syntax."
  6025. (interactive)
  6026. (org-load-modules-maybe)
  6027. (org-run-like-in-org-mode 'org-insert-link))
  6028. (defun org-insert-link (&optional complete-file link-location)
  6029. "Insert a link. At the prompt, enter the link.
  6030. Completion can be used to insert any of the link protocol prefixes like
  6031. http or ftp in use.
  6032. The history can be used to select a link previously stored with
  6033. `org-store-link'. When the empty string is entered (i.e. if you just
  6034. press RET at the prompt), the link defaults to the most recently
  6035. stored link. As SPC triggers completion in the minibuffer, you need to
  6036. use M-SPC or C-q SPC to force the insertion of a space character.
  6037. You will also be prompted for a description, and if one is given, it will
  6038. be displayed in the buffer instead of the link.
  6039. If there is already a link at point, this command will allow you to edit link
  6040. and description parts.
  6041. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6042. be selected using completion. The path to the file will be relative to the
  6043. current directory if the file is in the current directory or a subdirectory.
  6044. Otherwise, the link will be the absolute path as completed in the minibuffer
  6045. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6046. option `org-link-file-path-type'.
  6047. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6048. the current directory or below.
  6049. With three \\[universal-argument] prefixes, negate the meaning of
  6050. `org-keep-stored-link-after-insertion'.
  6051. If `org-make-link-description-function' is non-nil, this function will be
  6052. called with the link target, and the result will be the default
  6053. link description.
  6054. If the LINK-LOCATION parameter is non-nil, this value will be
  6055. used as the link location instead of reading one interactively."
  6056. (interactive "P")
  6057. (let* ((wcf (current-window-configuration))
  6058. (region (if (org-region-active-p)
  6059. (buffer-substring (region-beginning) (region-end))))
  6060. (remove (and region (list (region-beginning) (region-end))))
  6061. (desc region)
  6062. tmphist ; byte-compile incorrectly complains about this
  6063. (link link-location)
  6064. entry file)
  6065. (cond
  6066. (link-location) ; specified by arg, just use it.
  6067. ((org-in-regexp org-bracket-link-regexp 1)
  6068. ;; We do have a link at point, and we are going to edit it.
  6069. (setq remove (list (match-beginning 0) (match-end 0)))
  6070. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6071. (setq link (read-string "Link: "
  6072. (org-link-unescape
  6073. (org-match-string-no-properties 1)))))
  6074. ((or (org-in-regexp org-angle-link-re)
  6075. (org-in-regexp org-plain-link-re))
  6076. ;; Convert to bracket link
  6077. (setq remove (list (match-beginning 0) (match-end 0))
  6078. link (read-string "Link: "
  6079. (org-remove-angle-brackets (match-string 0)))))
  6080. ((member complete-file '((4) (16)))
  6081. ;; Completing read for file names.
  6082. (setq file (read-file-name "File: "))
  6083. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6084. (pwd1 (file-name-as-directory (abbreviate-file-name
  6085. (expand-file-name ".")))))
  6086. (cond
  6087. ((equal complete-file '(16))
  6088. (setq link (org-make-link
  6089. "file:"
  6090. (abbreviate-file-name (expand-file-name file)))))
  6091. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6092. (setq link (org-make-link "file:" (match-string 1 file))))
  6093. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6094. (expand-file-name file))
  6095. (setq link (org-make-link
  6096. "file:" (match-string 1 (expand-file-name file)))))
  6097. (t (setq link (org-make-link "file:" file))))))
  6098. (t
  6099. ;; Read link, with completion for stored links.
  6100. (with-output-to-temp-buffer "*Org Links*"
  6101. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6102. (when org-stored-links
  6103. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6104. (princ (mapconcat
  6105. (lambda (x)
  6106. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6107. (reverse org-stored-links) "\n"))))
  6108. (let ((cw (selected-window)))
  6109. (select-window (get-buffer-window "*Org Links*"))
  6110. (setq truncate-lines t)
  6111. (org-fit-window-to-buffer)
  6112. (select-window cw))
  6113. ;; Fake a link history, containing the stored links.
  6114. (setq tmphist (append (mapcar 'car org-stored-links)
  6115. org-insert-link-history))
  6116. (unwind-protect
  6117. (setq link
  6118. (let ((org-completion-use-ido nil))
  6119. (org-completing-read
  6120. "Link: "
  6121. (append
  6122. (mapcar (lambda (x) (list (concat (car x) ":")))
  6123. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6124. (mapcar (lambda (x) (list (concat x ":")))
  6125. org-link-types))
  6126. nil nil nil
  6127. 'tmphist
  6128. (or (car (car org-stored-links))))))
  6129. (set-window-configuration wcf)
  6130. (kill-buffer "*Org Links*"))
  6131. (setq entry (assoc link org-stored-links))
  6132. (or entry (push link org-insert-link-history))
  6133. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6134. (not org-keep-stored-link-after-insertion))
  6135. (setq org-stored-links (delq (assoc link org-stored-links)
  6136. org-stored-links)))
  6137. (setq desc (or desc (nth 1 entry)))))
  6138. (if (string-match org-plain-link-re link)
  6139. ;; URL-like link, normalize the use of angular brackets.
  6140. (setq link (org-make-link (org-remove-angle-brackets link))))
  6141. ;; Check if we are linking to the current file with a search option
  6142. ;; If yes, simplify the link by using only the search option.
  6143. (when (and buffer-file-name
  6144. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6145. (let* ((path (match-string 1 link))
  6146. (case-fold-search nil)
  6147. (search (match-string 2 link)))
  6148. (save-match-data
  6149. (if (equal (file-truename buffer-file-name) (file-truename path))
  6150. ;; We are linking to this same file, with a search option
  6151. (setq link search)))))
  6152. ;; Check if we can/should use a relative path. If yes, simplify the link
  6153. (when (string-match "^file:\\(.*\\)" link)
  6154. (let* ((path (match-string 1 link))
  6155. (origpath path)
  6156. (case-fold-search nil))
  6157. (cond
  6158. ((or (eq org-link-file-path-type 'absolute)
  6159. (equal complete-file '(16)))
  6160. (setq path (abbreviate-file-name (expand-file-name path))))
  6161. ((eq org-link-file-path-type 'noabbrev)
  6162. (setq path (expand-file-name path)))
  6163. ((eq org-link-file-path-type 'relative)
  6164. (setq path (file-relative-name path)))
  6165. (t
  6166. (save-match-data
  6167. (if (string-match (concat "^" (regexp-quote
  6168. (file-name-as-directory
  6169. (expand-file-name "."))))
  6170. (expand-file-name path))
  6171. ;; We are linking a file with relative path name.
  6172. (setq path (substring (expand-file-name path)
  6173. (match-end 0)))
  6174. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6175. (setq link (concat "file:" path))
  6176. (if (equal desc origpath)
  6177. (setq desc path))))
  6178. (if org-make-link-description-function
  6179. (setq desc (funcall org-make-link-description-function link desc)))
  6180. (setq desc (read-string "Description: " desc))
  6181. (unless (string-match "\\S-" desc) (setq desc nil))
  6182. (if remove (apply 'delete-region remove))
  6183. (insert (org-make-link-string link desc))))
  6184. (defun org-completing-read (&rest args)
  6185. "Completing-read with SPACE being a normal character."
  6186. (let ((minibuffer-local-completion-map
  6187. (copy-keymap minibuffer-local-completion-map)))
  6188. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6189. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6190. (apply 'org-ido-completing-read args)))
  6191. (defun org-ido-completing-read (&rest args)
  6192. "Completing-read using `ido-mode' speedups if available"
  6193. (if (and org-completion-use-ido
  6194. (fboundp 'ido-completing-read)
  6195. (boundp 'ido-mode) ido-mode
  6196. (listp (second args)))
  6197. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6198. (apply 'completing-read args)))
  6199. (defun org-extract-attributes (s)
  6200. "Extract the attributes cookie from a string and set as text property."
  6201. (let (a attr (start 0) key value)
  6202. (save-match-data
  6203. (when (string-match "{{\\([^}]+\\)}}$" s)
  6204. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6205. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6206. (setq key (match-string 1 a) value (match-string 2 a)
  6207. start (match-end 0)
  6208. attr (plist-put attr (intern key) value))))
  6209. (org-add-props s nil 'org-attr attr))
  6210. s))
  6211. (defun org-attributes-to-string (plist)
  6212. "Format a property list into an HTML attribute list."
  6213. (let ((s "") key value)
  6214. (while plist
  6215. (setq key (pop plist) value (pop plist))
  6216. (and value
  6217. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6218. s))
  6219. ;;; Opening/following a link
  6220. (defvar org-link-search-failed nil)
  6221. (defun org-next-link ()
  6222. "Move forward to the next link.
  6223. If the link is in hidden text, expose it."
  6224. (interactive)
  6225. (when (and org-link-search-failed (eq this-command last-command))
  6226. (goto-char (point-min))
  6227. (message "Link search wrapped back to beginning of buffer"))
  6228. (setq org-link-search-failed nil)
  6229. (let* ((pos (point))
  6230. (ct (org-context))
  6231. (a (assoc :link ct)))
  6232. (if a (goto-char (nth 2 a)))
  6233. (if (re-search-forward org-any-link-re nil t)
  6234. (progn
  6235. (goto-char (match-beginning 0))
  6236. (if (org-invisible-p) (org-show-context)))
  6237. (goto-char pos)
  6238. (setq org-link-search-failed t)
  6239. (error "No further link found"))))
  6240. (defun org-previous-link ()
  6241. "Move backward to the previous link.
  6242. If the link is in hidden text, expose it."
  6243. (interactive)
  6244. (when (and org-link-search-failed (eq this-command last-command))
  6245. (goto-char (point-max))
  6246. (message "Link search wrapped back to end of buffer"))
  6247. (setq org-link-search-failed nil)
  6248. (let* ((pos (point))
  6249. (ct (org-context))
  6250. (a (assoc :link ct)))
  6251. (if a (goto-char (nth 1 a)))
  6252. (if (re-search-backward org-any-link-re nil t)
  6253. (progn
  6254. (goto-char (match-beginning 0))
  6255. (if (org-invisible-p) (org-show-context)))
  6256. (goto-char pos)
  6257. (setq org-link-search-failed t)
  6258. (error "No further link found"))))
  6259. (defun org-translate-link (s)
  6260. "Translate a link string if a translation function has been defined."
  6261. (if (and org-link-translation-function
  6262. (fboundp org-link-translation-function)
  6263. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6264. (progn
  6265. (setq s (funcall org-link-translation-function
  6266. (match-string 1) (match-string 2)))
  6267. (concat (car s) ":" (cdr s)))
  6268. s))
  6269. (defun org-translate-link-from-planner (type path)
  6270. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6271. This is still an experimental function, your mileage may vary."
  6272. (cond
  6273. ((member type '("http" "https" "news" "ftp"))
  6274. ;; standard Internet links are the same.
  6275. nil)
  6276. ((and (equal type "irc") (string-match "^//" path))
  6277. ;; Planner has two / at the beginning of an irc link, we have 1.
  6278. ;; We should have zero, actually....
  6279. (setq path (substring path 1)))
  6280. ((and (equal type "lisp") (string-match "^/" path))
  6281. ;; Planner has a slash, we do not.
  6282. (setq type "elisp" path (substring path 1)))
  6283. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6284. ;; A typical message link. Planner has the id after the fina slash,
  6285. ;; we separate it with a hash mark
  6286. (setq path (concat (match-string 1 path) "#"
  6287. (org-remove-angle-brackets (match-string 2 path)))))
  6288. )
  6289. (cons type path))
  6290. (defun org-find-file-at-mouse (ev)
  6291. "Open file link or URL at mouse."
  6292. (interactive "e")
  6293. (mouse-set-point ev)
  6294. (org-open-at-point 'in-emacs))
  6295. (defun org-open-at-mouse (ev)
  6296. "Open file link or URL at mouse."
  6297. (interactive "e")
  6298. (mouse-set-point ev)
  6299. (if (eq major-mode 'org-agenda-mode)
  6300. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6301. (org-open-at-point))
  6302. (defvar org-window-config-before-follow-link nil
  6303. "The window configuration before following a link.
  6304. This is saved in case the need arises to restore it.")
  6305. (defvar org-open-link-marker (make-marker)
  6306. "Marker pointing to the location where `org-open-at-point; was called.")
  6307. ;;;###autoload
  6308. (defun org-open-at-point-global ()
  6309. "Follow a link like Org-mode does.
  6310. This command can be called in any mode to follow a link that has
  6311. Org-mode syntax."
  6312. (interactive)
  6313. (org-run-like-in-org-mode 'org-open-at-point))
  6314. ;;;###autoload
  6315. (defun org-open-link-from-string (s &optional arg)
  6316. "Open a link in the string S, as if it was in Org-mode."
  6317. (interactive "sLink: \nP")
  6318. (with-temp-buffer
  6319. (let ((org-inhibit-startup t))
  6320. (org-mode)
  6321. (insert s)
  6322. (goto-char (point-min))
  6323. (org-open-at-point arg))))
  6324. (defun org-open-at-point (&optional in-emacs)
  6325. "Open link at or after point.
  6326. If there is no link at point, this function will search forward up to
  6327. the end of the current subtree.
  6328. Normally, files will be opened by an appropriate application. If the
  6329. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6330. With a double prefix argument, try to open outside of Emacs, in the
  6331. application the system uses for this file type."
  6332. (interactive "P")
  6333. (org-load-modules-maybe)
  6334. (move-marker org-open-link-marker (point))
  6335. (setq org-window-config-before-follow-link (current-window-configuration))
  6336. (org-remove-occur-highlights nil nil t)
  6337. (cond
  6338. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6339. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6340. (org-footnote-action))
  6341. (t
  6342. (let (type path link line search (pos (point)))
  6343. (catch 'match
  6344. (save-excursion
  6345. (skip-chars-forward "^]\n\r")
  6346. (when (org-in-regexp org-bracket-link-regexp)
  6347. (setq link (org-extract-attributes
  6348. (org-link-unescape (org-match-string-no-properties 1))))
  6349. (while (string-match " *\n *" link)
  6350. (setq link (replace-match " " t t link)))
  6351. (setq link (org-link-expand-abbrev link))
  6352. (cond
  6353. ((or (file-name-absolute-p link)
  6354. (string-match "^\\.\\.?/" link))
  6355. (setq type "file" path link))
  6356. ((string-match org-link-re-with-space3 link)
  6357. (setq type (match-string 1 link) path (match-string 2 link)))
  6358. (t (setq type "thisfile" path link)))
  6359. (throw 'match t)))
  6360. (when (get-text-property (point) 'org-linked-text)
  6361. (setq type "thisfile"
  6362. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6363. (1+ (point)) (point))
  6364. path (buffer-substring
  6365. (previous-single-property-change pos 'org-linked-text)
  6366. (next-single-property-change pos 'org-linked-text)))
  6367. (throw 'match t))
  6368. (save-excursion
  6369. (when (or (org-in-regexp org-angle-link-re)
  6370. (org-in-regexp org-plain-link-re))
  6371. (setq type (match-string 1) path (match-string 2))
  6372. (throw 'match t)))
  6373. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6374. (setq type "tree-match"
  6375. path (match-string 1))
  6376. (throw 'match t))
  6377. (save-excursion
  6378. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6379. (setq type "tags"
  6380. path (match-string 1))
  6381. (while (string-match ":" path)
  6382. (setq path (replace-match "+" t t path)))
  6383. (throw 'match t))))
  6384. (unless path
  6385. (error "No link found"))
  6386. ;; Remove any trailing spaces in path
  6387. (if (string-match " +\\'" path)
  6388. (setq path (replace-match "" t t path)))
  6389. (if (and org-link-translation-function
  6390. (fboundp org-link-translation-function))
  6391. ;; Check if we need to translate the link
  6392. (let ((tmp (funcall org-link-translation-function type path)))
  6393. (setq type (car tmp) path (cdr tmp))))
  6394. (cond
  6395. ((assoc type org-link-protocols)
  6396. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6397. ((equal type "mailto")
  6398. (let ((cmd (car org-link-mailto-program))
  6399. (args (cdr org-link-mailto-program)) args1
  6400. (address path) (subject "") a)
  6401. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6402. (setq address (match-string 1 path)
  6403. subject (org-link-escape (match-string 2 path))))
  6404. (while args
  6405. (cond
  6406. ((not (stringp (car args))) (push (pop args) args1))
  6407. (t (setq a (pop args))
  6408. (if (string-match "%a" a)
  6409. (setq a (replace-match address t t a)))
  6410. (if (string-match "%s" a)
  6411. (setq a (replace-match subject t t a)))
  6412. (push a args1))))
  6413. (apply cmd (nreverse args1))))
  6414. ((member type '("http" "https" "ftp" "news"))
  6415. (browse-url (concat type ":" (org-link-escape
  6416. path org-link-escape-chars-browser))))
  6417. ((member type '("message"))
  6418. (browse-url (concat type ":" path)))
  6419. ((string= type "tags")
  6420. (org-tags-view in-emacs path))
  6421. ((string= type "thisfile")
  6422. (if in-emacs
  6423. (switch-to-buffer-other-window
  6424. (org-get-buffer-for-internal-link (current-buffer)))
  6425. (org-mark-ring-push))
  6426. (let ((cmd `(org-link-search
  6427. ,path
  6428. ,(cond ((equal in-emacs '(4)) 'occur)
  6429. ((equal in-emacs '(16)) 'org-occur)
  6430. (t nil))
  6431. ,pos)))
  6432. (condition-case nil (eval cmd)
  6433. (error (progn (widen) (eval cmd))))))
  6434. ((string= type "tree-match")
  6435. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6436. ((string= type "file")
  6437. (if (string-match "::\\([0-9]+\\)\\'" path)
  6438. (setq line (string-to-number (match-string 1 path))
  6439. path (substring path 0 (match-beginning 0)))
  6440. (if (string-match "::\\(.+\\)\\'" path)
  6441. (setq search (match-string 1 path)
  6442. path (substring path 0 (match-beginning 0)))))
  6443. (if (string-match "[*?{]" (file-name-nondirectory path))
  6444. (dired path)
  6445. (org-open-file path in-emacs line search)))
  6446. ((string= type "news")
  6447. (require 'org-gnus)
  6448. (org-gnus-follow-link path))
  6449. ((string= type "shell")
  6450. (let ((cmd path))
  6451. (if (or (not org-confirm-shell-link-function)
  6452. (funcall org-confirm-shell-link-function
  6453. (format "Execute \"%s\" in shell? "
  6454. (org-add-props cmd nil
  6455. 'face 'org-warning))))
  6456. (progn
  6457. (message "Executing %s" cmd)
  6458. (shell-command cmd))
  6459. (error "Abort"))))
  6460. ((string= type "elisp")
  6461. (let ((cmd path))
  6462. (if (or (not org-confirm-elisp-link-function)
  6463. (funcall org-confirm-elisp-link-function
  6464. (format "Execute \"%s\" as elisp? "
  6465. (org-add-props cmd nil
  6466. 'face 'org-warning))))
  6467. (message "%s => %s" cmd
  6468. (if (equal (string-to-char cmd) ?\()
  6469. (eval (read cmd))
  6470. (call-interactively (read cmd))))
  6471. (error "Abort"))))
  6472. (t
  6473. (browse-url-at-point))))))
  6474. (move-marker org-open-link-marker nil)
  6475. (run-hook-with-args 'org-follow-link-hook))
  6476. ;;;; Time estimates
  6477. (defun org-get-effort (&optional pom)
  6478. "Get the effort estimate for the current entry."
  6479. (org-entry-get pom org-effort-property))
  6480. ;;; File search
  6481. (defvar org-create-file-search-functions nil
  6482. "List of functions to construct the right search string for a file link.
  6483. These functions are called in turn with point at the location to
  6484. which the link should point.
  6485. A function in the hook should first test if it would like to
  6486. handle this file type, for example by checking the major-mode or
  6487. the file extension. If it decides not to handle this file, it
  6488. should just return nil to give other functions a chance. If it
  6489. does handle the file, it must return the search string to be used
  6490. when following the link. The search string will be part of the
  6491. file link, given after a double colon, and `org-open-at-point'
  6492. will automatically search for it. If special measures must be
  6493. taken to make the search successful, another function should be
  6494. added to the companion hook `org-execute-file-search-functions',
  6495. which see.
  6496. A function in this hook may also use `setq' to set the variable
  6497. `description' to provide a suggestion for the descriptive text to
  6498. be used for this link when it gets inserted into an Org-mode
  6499. buffer with \\[org-insert-link].")
  6500. (defvar org-execute-file-search-functions nil
  6501. "List of functions to execute a file search triggered by a link.
  6502. Functions added to this hook must accept a single argument, the
  6503. search string that was part of the file link, the part after the
  6504. double colon. The function must first check if it would like to
  6505. handle this search, for example by checking the major-mode or the
  6506. file extension. If it decides not to handle this search, it
  6507. should just return nil to give other functions a chance. If it
  6508. does handle the search, it must return a non-nil value to keep
  6509. other functions from trying.
  6510. Each function can access the current prefix argument through the
  6511. variable `current-prefix-argument'. Note that a single prefix is
  6512. used to force opening a link in Emacs, so it may be good to only
  6513. use a numeric or double prefix to guide the search function.
  6514. In case this is needed, a function in this hook can also restore
  6515. the window configuration before `org-open-at-point' was called using:
  6516. (set-window-configuration org-window-config-before-follow-link)")
  6517. (defun org-link-search (s &optional type avoid-pos)
  6518. "Search for a link search option.
  6519. If S is surrounded by forward slashes, it is interpreted as a
  6520. regular expression. In org-mode files, this will create an `org-occur'
  6521. sparse tree. In ordinary files, `occur' will be used to list matches.
  6522. If the current buffer is in `dired-mode', grep will be used to search
  6523. in all files. If AVOID-POS is given, ignore matches near that position."
  6524. (let ((case-fold-search t)
  6525. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6526. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6527. (append '(("") (" ") ("\t") ("\n"))
  6528. org-emphasis-alist)
  6529. "\\|") "\\)"))
  6530. (pos (point))
  6531. (pre nil) (post nil)
  6532. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6533. (cond
  6534. ;; First check if there are any special
  6535. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6536. ;; Now try the builtin stuff
  6537. ((save-excursion
  6538. (goto-char (point-min))
  6539. (and
  6540. (re-search-forward
  6541. (concat "<<" (regexp-quote s0) ">>") nil t)
  6542. (setq type 'dedicated
  6543. pos (match-beginning 0))))
  6544. ;; There is an exact target for this
  6545. (goto-char pos))
  6546. ((and (string-match "^(\\(.*\\))$" s0)
  6547. (save-excursion
  6548. (goto-char (point-min))
  6549. (and
  6550. (re-search-forward
  6551. (concat "[^[]" (regexp-quote
  6552. (format org-coderef-label-format
  6553. (match-string 1 s0))))
  6554. nil t)
  6555. (setq type 'dedicated
  6556. pos (1+ (match-beginning 0))))))
  6557. ;; There is a coderef target for this
  6558. (goto-char pos))
  6559. ((string-match "^/\\(.*\\)/$" s)
  6560. ;; A regular expression
  6561. (cond
  6562. ((org-mode-p)
  6563. (org-occur (match-string 1 s)))
  6564. ;;((eq major-mode 'dired-mode)
  6565. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6566. (t (org-do-occur (match-string 1 s)))))
  6567. (t
  6568. ;; A normal search strings
  6569. (when (equal (string-to-char s) ?*)
  6570. ;; Anchor on headlines, post may include tags.
  6571. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6572. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6573. s (substring s 1)))
  6574. (remove-text-properties
  6575. 0 (length s)
  6576. '(face nil mouse-face nil keymap nil fontified nil) s)
  6577. ;; Make a series of regular expressions to find a match
  6578. (setq words (org-split-string s "[ \n\r\t]+")
  6579. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6580. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6581. "\\)" markers)
  6582. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6583. re2a (concat "[ \t\r\n]" re2a_)
  6584. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6585. re4 (concat "[^a-zA-Z_]" re4_)
  6586. re1 (concat pre re2 post)
  6587. re3 (concat pre (if pre re4_ re4) post)
  6588. re5 (concat pre ".*" re4)
  6589. re2 (concat pre re2)
  6590. re2a (concat pre (if pre re2a_ re2a))
  6591. re4 (concat pre (if pre re4_ re4))
  6592. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6593. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6594. re5 "\\)"
  6595. ))
  6596. (cond
  6597. ((eq type 'org-occur) (org-occur reall))
  6598. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6599. (t (goto-char (point-min))
  6600. (setq type 'fuzzy)
  6601. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6602. (org-search-not-self 1 re1 nil t)
  6603. (org-search-not-self 1 re2 nil t)
  6604. (org-search-not-self 1 re2a nil t)
  6605. (org-search-not-self 1 re3 nil t)
  6606. (org-search-not-self 1 re4 nil t)
  6607. (org-search-not-self 1 re5 nil t)
  6608. )
  6609. (goto-char (match-beginning 1))
  6610. (goto-char pos)
  6611. (error "No match")))))
  6612. (t
  6613. ;; Normal string-search
  6614. (goto-char (point-min))
  6615. (if (search-forward s nil t)
  6616. (goto-char (match-beginning 0))
  6617. (error "No match"))))
  6618. (and (org-mode-p) (org-show-context 'link-search))
  6619. type))
  6620. (defun org-search-not-self (group &rest args)
  6621. "Execute `re-search-forward', but only accept matches that do not
  6622. enclose the position of `org-open-link-marker'."
  6623. (let ((m org-open-link-marker))
  6624. (catch 'exit
  6625. (while (apply 're-search-forward args)
  6626. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6627. (goto-char (match-end group))
  6628. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6629. (> (match-beginning 0) (marker-position m))
  6630. (< (match-end 0) (marker-position m)))
  6631. (save-match-data
  6632. (or (not (org-in-regexp
  6633. org-bracket-link-analytic-regexp 1))
  6634. (not (match-end 4)) ; no description
  6635. (and (<= (match-beginning 4) (point))
  6636. (>= (match-end 4) (point))))))
  6637. (throw 'exit (point))))))))
  6638. (defun org-get-buffer-for-internal-link (buffer)
  6639. "Return a buffer to be used for displaying the link target of internal links."
  6640. (cond
  6641. ((not org-display-internal-link-with-indirect-buffer)
  6642. buffer)
  6643. ((string-match "(Clone)$" (buffer-name buffer))
  6644. (message "Buffer is already a clone, not making another one")
  6645. ;; we also do not modify visibility in this case
  6646. buffer)
  6647. (t ; make a new indirect buffer for displaying the link
  6648. (let* ((bn (buffer-name buffer))
  6649. (ibn (concat bn "(Clone)"))
  6650. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6651. (with-current-buffer ib (org-overview))
  6652. ib))))
  6653. (defun org-do-occur (regexp &optional cleanup)
  6654. "Call the Emacs command `occur'.
  6655. If CLEANUP is non-nil, remove the printout of the regular expression
  6656. in the *Occur* buffer. This is useful if the regex is long and not useful
  6657. to read."
  6658. (occur regexp)
  6659. (when cleanup
  6660. (let ((cwin (selected-window)) win beg end)
  6661. (when (setq win (get-buffer-window "*Occur*"))
  6662. (select-window win))
  6663. (goto-char (point-min))
  6664. (when (re-search-forward "match[a-z]+" nil t)
  6665. (setq beg (match-end 0))
  6666. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6667. (setq end (1- (match-beginning 0)))))
  6668. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6669. (goto-char (point-min))
  6670. (select-window cwin))))
  6671. ;;; The mark ring for links jumps
  6672. (defvar org-mark-ring nil
  6673. "Mark ring for positions before jumps in Org-mode.")
  6674. (defvar org-mark-ring-last-goto nil
  6675. "Last position in the mark ring used to go back.")
  6676. ;; Fill and close the ring
  6677. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6678. (loop for i from 1 to org-mark-ring-length do
  6679. (push (make-marker) org-mark-ring))
  6680. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6681. org-mark-ring)
  6682. (defun org-mark-ring-push (&optional pos buffer)
  6683. "Put the current position or POS into the mark ring and rotate it."
  6684. (interactive)
  6685. (setq pos (or pos (point)))
  6686. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6687. (move-marker (car org-mark-ring)
  6688. (or pos (point))
  6689. (or buffer (current-buffer)))
  6690. (message "%s"
  6691. (substitute-command-keys
  6692. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6693. (defun org-mark-ring-goto (&optional n)
  6694. "Jump to the previous position in the mark ring.
  6695. With prefix arg N, jump back that many stored positions. When
  6696. called several times in succession, walk through the entire ring.
  6697. Org-mode commands jumping to a different position in the current file,
  6698. or to another Org-mode file, automatically push the old position
  6699. onto the ring."
  6700. (interactive "p")
  6701. (let (p m)
  6702. (if (eq last-command this-command)
  6703. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6704. (setq p org-mark-ring))
  6705. (setq org-mark-ring-last-goto p)
  6706. (setq m (car p))
  6707. (switch-to-buffer (marker-buffer m))
  6708. (goto-char m)
  6709. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6710. (defun org-remove-angle-brackets (s)
  6711. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6712. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6713. s)
  6714. (defun org-add-angle-brackets (s)
  6715. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6716. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6717. s)
  6718. (defun org-remove-double-quotes (s)
  6719. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6720. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6721. s)
  6722. ;;; Following specific links
  6723. (defun org-follow-timestamp-link ()
  6724. (cond
  6725. ((org-at-date-range-p t)
  6726. (let ((org-agenda-start-on-weekday)
  6727. (t1 (match-string 1))
  6728. (t2 (match-string 2)))
  6729. (setq t1 (time-to-days (org-time-string-to-time t1))
  6730. t2 (time-to-days (org-time-string-to-time t2)))
  6731. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6732. ((org-at-timestamp-p t)
  6733. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6734. (substring (match-string 1) 0 10)))
  6735. 1))
  6736. (t (error "This should not happen"))))
  6737. ;;; Following file links
  6738. (defvar org-wait nil)
  6739. (defun org-open-file (path &optional in-emacs line search)
  6740. "Open the file at PATH.
  6741. First, this expands any special file name abbreviations. Then the
  6742. configuration variable `org-file-apps' is checked if it contains an
  6743. entry for this file type, and if yes, the corresponding command is launched.
  6744. If no application is found, Emacs simply visits the file.
  6745. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6746. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6747. and o use an external application to visit the file.
  6748. Optional LINE specifies a line to go to, optional SEARCH a string to
  6749. search for. If LINE or SEARCH is given, the file will always be
  6750. opened in Emacs.
  6751. If the file does not exist, an error is thrown."
  6752. (setq in-emacs (or in-emacs line search))
  6753. (let* ((file (if (equal path "")
  6754. buffer-file-name
  6755. (substitute-in-file-name (expand-file-name path))))
  6756. (apps (append org-file-apps (org-default-apps)))
  6757. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6758. (dirp (if remp nil (file-directory-p file)))
  6759. (file (if (and dirp org-open-directory-means-index-dot-org)
  6760. (concat (file-name-as-directory file) "index.org")
  6761. file))
  6762. (a-m-a-p (assq 'auto-mode apps))
  6763. (dfile (downcase file))
  6764. (old-buffer (current-buffer))
  6765. (old-pos (point))
  6766. (old-mode major-mode)
  6767. ext cmd)
  6768. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6769. (setq ext (match-string 1 dfile))
  6770. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6771. (setq ext (match-string 1 dfile))))
  6772. (cond
  6773. ((equal in-emacs '(16))
  6774. (setq cmd (cdr (assoc 'system apps))))
  6775. (in-emacs (setq cmd 'emacs))
  6776. (t
  6777. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6778. (and dirp (cdr (assoc 'directory apps)))
  6779. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6780. 'string-match)
  6781. (cdr (assoc ext apps))
  6782. (cdr (assoc t apps))))))
  6783. (when (eq cmd 'system)
  6784. (setq cmd (cdr (assoc 'system apps))))
  6785. (when (eq cmd 'default)
  6786. (setq cmd (cdr (assoc t apps))))
  6787. (when (eq cmd 'mailcap)
  6788. (require 'mailcap)
  6789. (mailcap-parse-mailcaps)
  6790. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6791. (command (mailcap-mime-info mime-type)))
  6792. (if (stringp command)
  6793. (setq cmd command)
  6794. (setq cmd 'emacs))))
  6795. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6796. (not (file-exists-p file))
  6797. (not org-open-non-existing-files))
  6798. (error "No such file: %s" file))
  6799. (cond
  6800. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6801. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6802. (while (string-match "['\"]%s['\"]" cmd)
  6803. (setq cmd (replace-match "%s" t t cmd)))
  6804. (while (string-match "%s" cmd)
  6805. (setq cmd (replace-match
  6806. (save-match-data
  6807. (shell-quote-argument
  6808. (convert-standard-filename file)))
  6809. t t cmd)))
  6810. (save-window-excursion
  6811. (start-process-shell-command cmd nil cmd)
  6812. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6813. ))
  6814. ((or (stringp cmd)
  6815. (eq cmd 'emacs))
  6816. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6817. (widen)
  6818. (if line (goto-line line)
  6819. (if search (org-link-search search))))
  6820. ((consp cmd)
  6821. (let ((file (convert-standard-filename file)))
  6822. (eval cmd)))
  6823. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6824. (and (org-mode-p) (eq old-mode 'org-mode)
  6825. (or (not (equal old-buffer (current-buffer)))
  6826. (not (equal old-pos (point))))
  6827. (org-mark-ring-push old-pos old-buffer))))
  6828. (defun org-default-apps ()
  6829. "Return the default applications for this operating system."
  6830. (cond
  6831. ((eq system-type 'darwin)
  6832. org-file-apps-defaults-macosx)
  6833. ((eq system-type 'windows-nt)
  6834. org-file-apps-defaults-windowsnt)
  6835. (t org-file-apps-defaults-gnu)))
  6836. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6837. "Convert extensions to regular expressions in the cars of LIST.
  6838. Also, weed out any non-string entries, because the return value is used
  6839. only for regexp matching.
  6840. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6841. point to the symbol `emacs', indicating that the file should
  6842. be opened in Emacs."
  6843. (append
  6844. (delq nil
  6845. (mapcar (lambda (x)
  6846. (if (not (stringp (car x)))
  6847. nil
  6848. (if (string-match "\\W" (car x))
  6849. x
  6850. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6851. list))
  6852. (if add-auto-mode
  6853. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6854. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6855. (defun org-file-remote-p (file)
  6856. "Test whether FILE specifies a location on a remote system.
  6857. Return non-nil if the location is indeed remote.
  6858. For example, the filename \"/user@host:/foo\" specifies a location
  6859. on the system \"/user@host:\"."
  6860. (cond ((fboundp 'file-remote-p)
  6861. (file-remote-p file))
  6862. ((fboundp 'tramp-handle-file-remote-p)
  6863. (tramp-handle-file-remote-p file))
  6864. ((and (boundp 'ange-ftp-name-format)
  6865. (string-match (car ange-ftp-name-format) file))
  6866. t)
  6867. (t nil)))
  6868. ;;;; Refiling
  6869. (defun org-get-org-file ()
  6870. "Read a filename, with default directory `org-directory'."
  6871. (let ((default (or org-default-notes-file remember-data-file)))
  6872. (read-file-name (format "File name [%s]: " default)
  6873. (file-name-as-directory org-directory)
  6874. default)))
  6875. (defun org-notes-order-reversed-p ()
  6876. "Check if the current file should receive notes in reversed order."
  6877. (cond
  6878. ((not org-reverse-note-order) nil)
  6879. ((eq t org-reverse-note-order) t)
  6880. ((not (listp org-reverse-note-order)) nil)
  6881. (t (catch 'exit
  6882. (let ((all org-reverse-note-order)
  6883. entry)
  6884. (while (setq entry (pop all))
  6885. (if (string-match (car entry) buffer-file-name)
  6886. (throw 'exit (cdr entry))))
  6887. nil)))))
  6888. (defvar org-refile-target-table nil
  6889. "The list of refile targets, created by `org-refile'.")
  6890. (defvar org-agenda-new-buffers nil
  6891. "Buffers created to visit agenda files.")
  6892. (defun org-get-refile-targets (&optional default-buffer)
  6893. "Produce a table with refile targets."
  6894. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6895. targets txt re files f desc descre fast-path-p level)
  6896. (message "Getting targets...")
  6897. (with-current-buffer (or default-buffer (current-buffer))
  6898. (while (setq entry (pop entries))
  6899. (setq files (car entry) desc (cdr entry))
  6900. (setq fast-path-p nil)
  6901. (cond
  6902. ((null files) (setq files (list (current-buffer))))
  6903. ((eq files 'org-agenda-files)
  6904. (setq files (org-agenda-files 'unrestricted)))
  6905. ((and (symbolp files) (fboundp files))
  6906. (setq files (funcall files)))
  6907. ((and (symbolp files) (boundp files))
  6908. (setq files (symbol-value files))))
  6909. (if (stringp files) (setq files (list files)))
  6910. (cond
  6911. ((eq (car desc) :tag)
  6912. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6913. ((eq (car desc) :todo)
  6914. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6915. ((eq (car desc) :regexp)
  6916. (setq descre (cdr desc)))
  6917. ((eq (car desc) :level)
  6918. (setq descre (concat "^\\*\\{" (number-to-string
  6919. (if org-odd-levels-only
  6920. (1- (* 2 (cdr desc)))
  6921. (cdr desc)))
  6922. "\\}[ \t]")))
  6923. ((eq (car desc) :maxlevel)
  6924. (setq fast-path-p t)
  6925. (setq descre (concat "^\\*\\{1," (number-to-string
  6926. (if org-odd-levels-only
  6927. (1- (* 2 (cdr desc)))
  6928. (cdr desc)))
  6929. "\\}[ \t]")))
  6930. (t (error "Bad refiling target description %s" desc)))
  6931. (while (setq f (pop files))
  6932. (save-excursion
  6933. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6934. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6935. (setq f (expand-file-name f))
  6936. (save-excursion
  6937. (save-restriction
  6938. (widen)
  6939. (goto-char (point-min))
  6940. (while (re-search-forward descre nil t)
  6941. (goto-char (point-at-bol))
  6942. (when (looking-at org-complex-heading-regexp)
  6943. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6944. txt (org-link-display-format (match-string 4))
  6945. re (concat "^" (regexp-quote
  6946. (buffer-substring (match-beginning 1)
  6947. (match-end 4)))))
  6948. (if (match-end 5) (setq re (concat re "[ \t]+"
  6949. (regexp-quote
  6950. (match-string 5)))))
  6951. (setq re (concat re "[ \t]*$"))
  6952. (when org-refile-use-outline-path
  6953. (setq txt (mapconcat 'org-protect-slash
  6954. (append
  6955. (if (eq org-refile-use-outline-path 'file)
  6956. (list (file-name-nondirectory
  6957. (buffer-file-name (buffer-base-buffer))))
  6958. (if (eq org-refile-use-outline-path 'full-file-path)
  6959. (list (buffer-file-name (buffer-base-buffer)))))
  6960. (org-get-outline-path fast-path-p level txt)
  6961. (list txt))
  6962. "/")))
  6963. (push (list txt f re (point)) targets))
  6964. (goto-char (point-at-eol))))))))
  6965. (message "Getting targets...done")
  6966. (nreverse targets))))
  6967. (defun org-protect-slash (s)
  6968. (while (string-match "/" s)
  6969. (setq s (replace-match "\\" t t s)))
  6970. s)
  6971. (defvar org-olpa (make-vector 20 nil))
  6972. (defun org-get-outline-path (&optional fastp level heading)
  6973. "Return the outline path to the current entry, as a list."
  6974. (if fastp
  6975. (progn
  6976. (if (> level 19)
  6977. (error "Outline path failure, more than 19 levels."))
  6978. (loop for i from level upto 19 do
  6979. (aset org-olpa i nil))
  6980. (prog1
  6981. (delq nil (append org-olpa nil))
  6982. (aset org-olpa level heading)))
  6983. (let (rtn)
  6984. (save-excursion
  6985. (while (org-up-heading-safe)
  6986. (when (looking-at org-complex-heading-regexp)
  6987. (push (org-match-string-no-properties 4) rtn)))
  6988. rtn))))
  6989. (defvar org-refile-history nil
  6990. "History for refiling operations.")
  6991. (defun org-refile (&optional goto default-buffer)
  6992. "Move the entry at point to another heading.
  6993. The list of target headings is compiled using the information in
  6994. `org-refile-targets', which see. This list is created before each use
  6995. and will therefore always be up-to-date.
  6996. At the target location, the entry is filed as a subitem of the target heading.
  6997. Depending on `org-reverse-note-order', the new subitem will either be the
  6998. first or the last subitem.
  6999. If there is an active region, all entries in that region will be moved.
  7000. However, the region must fulfil the requirement that the first heading
  7001. is the first one sets the top-level of the moved text - at most siblings
  7002. below it are allowed.
  7003. With prefix arg GOTO, the command will only visit the target location,
  7004. not actually move anything.
  7005. With a double prefix `C-u C-u', go to the location where the last refiling
  7006. operation has put the subtree."
  7007. (interactive "P")
  7008. (let* ((cbuf (current-buffer))
  7009. (regionp (org-region-active-p))
  7010. (region-start (and regionp (region-beginning)))
  7011. (region-end (and regionp (region-end)))
  7012. (region-length (and regionp (- region-end region-start)))
  7013. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7014. pos it nbuf file re level reversed)
  7015. (when regionp (goto-char region-start)
  7016. (unless (org-kill-is-subtree-p
  7017. (buffer-substring region-start region-end))
  7018. (error "The region is not a (sequence of) subtree(s)")))
  7019. (if (equal goto '(16))
  7020. (org-refile-goto-last-stored)
  7021. (when (setq it (org-refile-get-location
  7022. (if goto "Goto: " "Refile to: ") default-buffer))
  7023. (setq file (nth 1 it)
  7024. re (nth 2 it)
  7025. pos (nth 3 it))
  7026. (if (and (equal (buffer-file-name) file)
  7027. (if regionp
  7028. (and (>= pos region-start)
  7029. (<= pos region-end))
  7030. (and (>= pos (point))
  7031. (< pos (save-excursion
  7032. (org-end-of-subtree t t))))))
  7033. (error "Cannot refile to position inside the tree or region"))
  7034. (setq nbuf (or (find-buffer-visiting file)
  7035. (find-file-noselect file)))
  7036. (if goto
  7037. (progn
  7038. (switch-to-buffer nbuf)
  7039. (goto-char pos)
  7040. (org-show-context 'org-goto))
  7041. (if regionp
  7042. (progn
  7043. (kill-new (buffer-substring region-start region-end))
  7044. (org-save-markers-in-region region-start region-end))
  7045. (org-copy-subtree 1 nil t))
  7046. (save-excursion
  7047. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7048. (find-file-noselect file))))
  7049. (setq reversed (org-notes-order-reversed-p))
  7050. (save-excursion
  7051. (save-restriction
  7052. (widen)
  7053. (goto-char pos)
  7054. (looking-at outline-regexp)
  7055. (setq level (org-get-valid-level (funcall outline-level) 1))
  7056. (goto-char
  7057. (if reversed
  7058. (or (outline-next-heading) (point-max))
  7059. (or (save-excursion (outline-get-next-sibling))
  7060. (org-end-of-subtree t t)
  7061. (point-max))))
  7062. (if (not (bolp)) (newline))
  7063. (bookmark-set "org-refile-last-stored")
  7064. (org-paste-subtree level))))
  7065. (if regionp
  7066. (delete-region (point) (+ (point) region-length))
  7067. (org-cut-subtree))
  7068. (setq org-markers-to-move nil)
  7069. (message "Refiled to \"%s\"" (car it)))))))
  7070. (defun org-refile-goto-last-stored ()
  7071. "Go to the location where the last refile was stored."
  7072. (interactive)
  7073. (bookmark-jump "org-refile-last-stored")
  7074. (message "This is the location of the last refile"))
  7075. (defun org-refile-get-location (&optional prompt default-buffer)
  7076. "Prompt the user for a refile location, using PROMPT."
  7077. (let ((org-refile-targets org-refile-targets)
  7078. (org-refile-use-outline-path org-refile-use-outline-path))
  7079. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7080. (unless org-refile-target-table
  7081. (error "No refile targets"))
  7082. (let* ((cbuf (current-buffer))
  7083. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7084. (cfunc (if (and org-refile-use-outline-path
  7085. org-outline-path-complete-in-steps)
  7086. 'org-olpath-completing-read
  7087. 'org-ido-completing-read))
  7088. (extra (if org-refile-use-outline-path "/" ""))
  7089. (filename (and cfn (expand-file-name cfn)))
  7090. (tbl (mapcar
  7091. (lambda (x)
  7092. (if (not (equal filename (nth 1 x)))
  7093. (cons (concat (car x) extra " ("
  7094. (file-name-nondirectory (nth 1 x)) ")")
  7095. (cdr x))
  7096. (cons (concat (car x) extra) (cdr x))))
  7097. org-refile-target-table))
  7098. (completion-ignore-case t))
  7099. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7100. tbl)))
  7101. (defun org-olpath-completing-read (prompt collection &rest args)
  7102. "Read an outline path like a file name."
  7103. (let ((thetable collection))
  7104. (apply
  7105. 'org-ido-completing-read prompt
  7106. (lambda (string predicate &optional flag)
  7107. (let (rtn r f (l (length string)))
  7108. (cond
  7109. ((eq flag nil)
  7110. ;; try completion
  7111. (try-completion string thetable))
  7112. ((eq flag t)
  7113. ;; all-completions
  7114. (setq rtn (all-completions string thetable predicate))
  7115. (mapcar
  7116. (lambda (x)
  7117. (setq r (substring x l))
  7118. (if (string-match " ([^)]*)$" x)
  7119. (setq f (match-string 0 x))
  7120. (setq f ""))
  7121. (if (string-match "/" r)
  7122. (concat string (substring r 0 (match-end 0)) f)
  7123. x))
  7124. rtn))
  7125. ((eq flag 'lambda)
  7126. ;; exact match?
  7127. (assoc string thetable)))
  7128. ))
  7129. args)))
  7130. ;;;; Dynamic blocks
  7131. (defun org-find-dblock (name)
  7132. "Find the first dynamic block with name NAME in the buffer.
  7133. If not found, stay at current position and return nil."
  7134. (let (pos)
  7135. (save-excursion
  7136. (goto-char (point-min))
  7137. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7138. nil t)
  7139. (match-beginning 0))))
  7140. (if pos (goto-char pos))
  7141. pos))
  7142. (defconst org-dblock-start-re
  7143. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7144. "Matches the startline of a dynamic block, with parameters.")
  7145. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7146. "Matches the end of a dynamic block.")
  7147. (defun org-create-dblock (plist)
  7148. "Create a dynamic block section, with parameters taken from PLIST.
  7149. PLIST must contain a :name entry which is used as name of the block."
  7150. (unless (bolp) (newline))
  7151. (let ((name (plist-get plist :name)))
  7152. (insert "#+BEGIN: " name)
  7153. (while plist
  7154. (if (eq (car plist) :name)
  7155. (setq plist (cddr plist))
  7156. (insert " " (prin1-to-string (pop plist)))))
  7157. (insert "\n\n#+END:\n")
  7158. (beginning-of-line -2)))
  7159. (defun org-prepare-dblock ()
  7160. "Prepare dynamic block for refresh.
  7161. This empties the block, puts the cursor at the insert position and returns
  7162. the property list including an extra property :name with the block name."
  7163. (unless (looking-at org-dblock-start-re)
  7164. (error "Not at a dynamic block"))
  7165. (let* ((begdel (1+ (match-end 0)))
  7166. (name (org-no-properties (match-string 1)))
  7167. (params (append (list :name name)
  7168. (read (concat "(" (match-string 3) ")")))))
  7169. (unless (re-search-forward org-dblock-end-re nil t)
  7170. (error "Dynamic block not terminated"))
  7171. (setq params
  7172. (append params
  7173. (list :content (buffer-substring
  7174. begdel (match-beginning 0)))))
  7175. (delete-region begdel (match-beginning 0))
  7176. (goto-char begdel)
  7177. (open-line 1)
  7178. params))
  7179. (defun org-map-dblocks (&optional command)
  7180. "Apply COMMAND to all dynamic blocks in the current buffer.
  7181. If COMMAND is not given, use `org-update-dblock'."
  7182. (let ((cmd (or command 'org-update-dblock))
  7183. pos)
  7184. (save-excursion
  7185. (goto-char (point-min))
  7186. (while (re-search-forward org-dblock-start-re nil t)
  7187. (goto-char (setq pos (match-beginning 0)))
  7188. (condition-case nil
  7189. (funcall cmd)
  7190. (error (message "Error during update of dynamic block")))
  7191. (goto-char pos)
  7192. (unless (re-search-forward org-dblock-end-re nil t)
  7193. (error "Dynamic block not terminated"))))))
  7194. (defun org-dblock-update (&optional arg)
  7195. "User command for updating dynamic blocks.
  7196. Update the dynamic block at point. With prefix ARG, update all dynamic
  7197. blocks in the buffer."
  7198. (interactive "P")
  7199. (if arg
  7200. (org-update-all-dblocks)
  7201. (or (looking-at org-dblock-start-re)
  7202. (org-beginning-of-dblock))
  7203. (org-update-dblock)))
  7204. (defun org-update-dblock ()
  7205. "Update the dynamic block at point
  7206. This means to empty the block, parse for parameters and then call
  7207. the correct writing function."
  7208. (save-window-excursion
  7209. (let* ((pos (point))
  7210. (line (org-current-line))
  7211. (params (org-prepare-dblock))
  7212. (name (plist-get params :name))
  7213. (cmd (intern (concat "org-dblock-write:" name))))
  7214. (message "Updating dynamic block `%s' at line %d..." name line)
  7215. (funcall cmd params)
  7216. (message "Updating dynamic block `%s' at line %d...done" name line)
  7217. (goto-char pos))))
  7218. (defun org-beginning-of-dblock ()
  7219. "Find the beginning of the dynamic block at point.
  7220. Error if there is no such block at point."
  7221. (let ((pos (point))
  7222. beg)
  7223. (end-of-line 1)
  7224. (if (and (re-search-backward org-dblock-start-re nil t)
  7225. (setq beg (match-beginning 0))
  7226. (re-search-forward org-dblock-end-re nil t)
  7227. (> (match-end 0) pos))
  7228. (goto-char beg)
  7229. (goto-char pos)
  7230. (error "Not in a dynamic block"))))
  7231. (defun org-update-all-dblocks ()
  7232. "Update all dynamic blocks in the buffer.
  7233. This function can be used in a hook."
  7234. (when (org-mode-p)
  7235. (org-map-dblocks 'org-update-dblock)))
  7236. ;;;; Completion
  7237. (defconst org-additional-option-like-keywords
  7238. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7239. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7240. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7241. "BEGIN_QUOTE" "END_QUOTE"
  7242. "BEGIN_VERSE" "END_VERSE"
  7243. "BEGIN_SRC" "END_SRC"
  7244. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7245. (defcustom org-structure-template-alist
  7246. '(
  7247. ("s" "#+begin_src ?\n\n#+end_src"
  7248. "<src lang=\"?\">\n\n</src>")
  7249. ("e" "#+begin_example\n?\n#+end_example"
  7250. "<example>\n?\n</example>")
  7251. ("q" "#+begin_quote\n?\n#+end_quote"
  7252. "<quote>\n?\n</quote>")
  7253. ("v" "#+begin_verse\n?\n#+end_verse"
  7254. "<verse>\n?\n/verse>")
  7255. ("l" "#+begin_latex\n?\n#+end_latex"
  7256. "<literal style=\"latex\">\n?\n</literal>")
  7257. ("L" "#+latex: "
  7258. "<literal style=\"latex\">?</literal>")
  7259. ("h" "#+begin_html\n?\n#+end_html"
  7260. "<literal style=\"html\">\n?\n</literal>")
  7261. ("H" "#+html: "
  7262. "<literal style=\"html\">?</literal>")
  7263. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7264. ("A" "#+ascii: ")
  7265. ("i" "#+include %file ?"
  7266. "<include file=%file markup=\"?\">")
  7267. )
  7268. "Structure completion elements.
  7269. This is a list of abbreviation keys and values. The value gets inserted
  7270. it you type @samp{.} followed by the key and then the completion key,
  7271. usually `M-TAB'. %file will be replaced by a file name after prompting
  7272. for the file using completion.
  7273. There are two templates for each key, the first uses the original Org syntax,
  7274. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7275. the default when the /org-mtags.el/ module has been loaded. See also the
  7276. variable `org-mtags-prefer-muse-templates'.
  7277. This is an experimental feature, it is undecided if it is going to stay in."
  7278. :group 'org-completion
  7279. :type '(repeat
  7280. (string :tag "Key")
  7281. (string :tag "Template")
  7282. (string :tag "Muse Template")))
  7283. (defun org-try-structure-completion ()
  7284. "Try to complete a structure template before point.
  7285. This looks for strings like \"<e\" on an otherwise empty line and
  7286. expands them."
  7287. (let ((l (buffer-substring (point-at-bol) (point)))
  7288. a)
  7289. (when (and (looking-at "[ \t]*$")
  7290. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7291. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7292. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7293. (match-beginning 1)) a)
  7294. t)))
  7295. (defun org-complete-expand-structure-template (start cell)
  7296. "Expand a structure template."
  7297. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7298. (rpl (nth (if musep 2 1) cell)))
  7299. (delete-region start (point))
  7300. (when (string-match "\\`#\\+" rpl)
  7301. (cond
  7302. ((bolp))
  7303. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7304. (delete-region (point-at-bol) (point)))
  7305. (t (newline))))
  7306. (setq start (point))
  7307. (if (string-match "%file" rpl)
  7308. (setq rpl (replace-match
  7309. (concat
  7310. "\""
  7311. (save-match-data
  7312. (abbreviate-file-name (read-file-name "Include file: ")))
  7313. "\"")
  7314. t t rpl)))
  7315. (insert rpl)
  7316. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7317. (defun org-complete (&optional arg)
  7318. "Perform completion on word at point.
  7319. At the beginning of a headline, this completes TODO keywords as given in
  7320. `org-todo-keywords'.
  7321. If the current word is preceded by a backslash, completes the TeX symbols
  7322. that are supported for HTML support.
  7323. If the current word is preceded by \"#+\", completes special words for
  7324. setting file options.
  7325. In the line after \"#+STARTUP:, complete valid keywords.\"
  7326. At all other locations, this simply calls the value of
  7327. `org-completion-fallback-command'."
  7328. (interactive "P")
  7329. (org-without-partial-completion
  7330. (catch 'exit
  7331. (let* ((a nil)
  7332. (end (point))
  7333. (beg1 (save-excursion
  7334. (skip-chars-backward (org-re "[:alnum:]_@"))
  7335. (point)))
  7336. (beg (save-excursion
  7337. (skip-chars-backward "a-zA-Z0-9_:$")
  7338. (point)))
  7339. (confirm (lambda (x) (stringp (car x))))
  7340. (searchhead (equal (char-before beg) ?*))
  7341. (struct
  7342. (when (and (member (char-before beg1) '(?. ?<))
  7343. (setq a (assoc (buffer-substring beg1 (point))
  7344. org-structure-template-alist)))
  7345. (org-complete-expand-structure-template (1- beg1) a)
  7346. (throw 'exit t)))
  7347. (tag (and (equal (char-before beg1) ?:)
  7348. (equal (char-after (point-at-bol)) ?*)))
  7349. (prop (and (equal (char-before beg1) ?:)
  7350. (not (equal (char-after (point-at-bol)) ?*))))
  7351. (texp (equal (char-before beg) ?\\))
  7352. (link (equal (char-before beg) ?\[))
  7353. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7354. beg)
  7355. "#+"))
  7356. (startup (string-match "^#\\+STARTUP:.*"
  7357. (buffer-substring (point-at-bol) (point))))
  7358. (completion-ignore-case opt)
  7359. (type nil)
  7360. (tbl nil)
  7361. (table (cond
  7362. (opt
  7363. (setq type :opt)
  7364. (require 'org-exp)
  7365. (append
  7366. (mapcar
  7367. (lambda (x)
  7368. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7369. (cons (match-string 2 x) (match-string 1 x)))
  7370. (org-split-string (org-get-current-options) "\n"))
  7371. (mapcar 'list org-additional-option-like-keywords)))
  7372. (startup
  7373. (setq type :startup)
  7374. org-startup-options)
  7375. (link (append org-link-abbrev-alist-local
  7376. org-link-abbrev-alist))
  7377. (texp
  7378. (setq type :tex)
  7379. org-html-entities)
  7380. ((string-match "\\`\\*+[ \t]+\\'"
  7381. (buffer-substring (point-at-bol) beg))
  7382. (setq type :todo)
  7383. (mapcar 'list org-todo-keywords-1))
  7384. (searchhead
  7385. (setq type :searchhead)
  7386. (save-excursion
  7387. (goto-char (point-min))
  7388. (while (re-search-forward org-todo-line-regexp nil t)
  7389. (push (list
  7390. (org-make-org-heading-search-string
  7391. (match-string 3) t))
  7392. tbl)))
  7393. tbl)
  7394. (tag (setq type :tag beg beg1)
  7395. (or org-tag-alist (org-get-buffer-tags)))
  7396. (prop (setq type :prop beg beg1)
  7397. (mapcar 'list (org-buffer-property-keys nil t t)))
  7398. (t (progn
  7399. (call-interactively org-completion-fallback-command)
  7400. (throw 'exit nil)))))
  7401. (pattern (buffer-substring-no-properties beg end))
  7402. (completion (try-completion pattern table confirm)))
  7403. (cond ((eq completion t)
  7404. (if (not (assoc (upcase pattern) table))
  7405. (message "Already complete")
  7406. (if (and (equal type :opt)
  7407. (not (member (car (assoc (upcase pattern) table))
  7408. org-additional-option-like-keywords)))
  7409. (insert (substring (cdr (assoc (upcase pattern) table))
  7410. (length pattern)))
  7411. (if (memq type '(:tag :prop)) (insert ":")))))
  7412. ((null completion)
  7413. (message "Can't find completion for \"%s\"" pattern)
  7414. (ding))
  7415. ((not (string= pattern completion))
  7416. (delete-region beg end)
  7417. (if (string-match " +$" completion)
  7418. (setq completion (replace-match "" t t completion)))
  7419. (insert completion)
  7420. (if (get-buffer-window "*Completions*")
  7421. (delete-window (get-buffer-window "*Completions*")))
  7422. (if (assoc completion table)
  7423. (if (eq type :todo) (insert " ")
  7424. (if (memq type '(:tag :prop)) (insert ":"))))
  7425. (if (and (equal type :opt) (assoc completion table))
  7426. (message "%s" (substitute-command-keys
  7427. "Press \\[org-complete] again to insert example settings"))))
  7428. (t
  7429. (message "Making completion list...")
  7430. (let ((list (sort (all-completions pattern table confirm)
  7431. 'string<)))
  7432. (with-output-to-temp-buffer "*Completions*"
  7433. (condition-case nil
  7434. ;; Protection needed for XEmacs and emacs 21
  7435. (display-completion-list list pattern)
  7436. (error (display-completion-list list)))))
  7437. (message "Making completion list...%s" "done")))))))
  7438. ;;;; TODO, DEADLINE, Comments
  7439. (defun org-toggle-comment ()
  7440. "Change the COMMENT state of an entry."
  7441. (interactive)
  7442. (save-excursion
  7443. (org-back-to-heading)
  7444. (let (case-fold-search)
  7445. (if (looking-at (concat outline-regexp
  7446. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7447. (replace-match "" t t nil 1)
  7448. (if (looking-at outline-regexp)
  7449. (progn
  7450. (goto-char (match-end 0))
  7451. (insert org-comment-string " ")))))))
  7452. (defvar org-last-todo-state-is-todo nil
  7453. "This is non-nil when the last TODO state change led to a TODO state.
  7454. If the last change removed the TODO tag or switched to DONE, then
  7455. this is nil.")
  7456. (defvar org-setting-tags nil) ; dynamically skipped
  7457. (defun org-parse-local-options (string var)
  7458. "Parse STRING for startup setting relevant for variable VAR."
  7459. (let ((rtn (symbol-value var))
  7460. e opts)
  7461. (save-match-data
  7462. (if (or (not string) (not (string-match "\\S-" string)))
  7463. rtn
  7464. (setq opts (delq nil (mapcar (lambda (x)
  7465. (setq e (assoc x org-startup-options))
  7466. (if (eq (nth 1 e) var) e nil))
  7467. (org-split-string string "[ \t]+"))))
  7468. (if (not opts)
  7469. rtn
  7470. (setq rtn nil)
  7471. (while (setq e (pop opts))
  7472. (if (not (nth 3 e))
  7473. (setq rtn (nth 2 e))
  7474. (if (not (listp rtn)) (setq rtn nil))
  7475. (push (nth 2 e) rtn)))
  7476. rtn)))))
  7477. (defvar org-blocker-hook nil
  7478. "Hook for functions that are allowed to block a state change.
  7479. Each function gets as its single argument a property list, see
  7480. `org-trigger-hook' for more information about this list.
  7481. If any of the functions in this hook returns nil, the state change
  7482. is blocked.")
  7483. (defvar org-trigger-hook nil
  7484. "Hook for functions that are triggered by a state change.
  7485. Each function gets as its single argument a property list with at least
  7486. the following elements:
  7487. (:type type-of-change :position pos-at-entry-start
  7488. :from old-state :to new-state)
  7489. Depending on the type, more properties may be present.
  7490. This mechanism is currently implemented for:
  7491. TODO state changes
  7492. ------------------
  7493. :type todo-state-change
  7494. :from previous state (keyword as a string), or nil
  7495. :to new state (keyword as a string), or nil")
  7496. (defvar org-agenda-headline-snapshot-before-repeat)
  7497. (defun org-todo (&optional arg)
  7498. "Change the TODO state of an item.
  7499. The state of an item is given by a keyword at the start of the heading,
  7500. like
  7501. *** TODO Write paper
  7502. *** DONE Call mom
  7503. The different keywords are specified in the variable `org-todo-keywords'.
  7504. By default the available states are \"TODO\" and \"DONE\".
  7505. So for this example: when the item starts with TODO, it is changed to DONE.
  7506. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7507. DONE are present, add TODO at the beginning of the heading.
  7508. With C-u prefix arg, use completion to determine the new state.
  7509. With numeric prefix arg, switch to that state.
  7510. For calling through lisp, arg is also interpreted in the following way:
  7511. 'none -> empty state
  7512. \"\"(empty string) -> switch to empty state
  7513. 'done -> switch to DONE
  7514. 'nextset -> switch to the next set of keywords
  7515. 'previousset -> switch to the previous set of keywords
  7516. \"WAITING\" -> switch to the specified keyword, but only if it
  7517. really is a member of `org-todo-keywords'."
  7518. (interactive "P")
  7519. (save-excursion
  7520. (catch 'exit
  7521. (org-back-to-heading)
  7522. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7523. (or (looking-at (concat " +" org-todo-regexp " *"))
  7524. (looking-at " *"))
  7525. (let* ((match-data (match-data))
  7526. (startpos (point-at-bol))
  7527. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7528. (org-log-done org-log-done)
  7529. (org-log-repeat org-log-repeat)
  7530. (org-todo-log-states org-todo-log-states)
  7531. (this (match-string 1))
  7532. (hl-pos (match-beginning 0))
  7533. (head (org-get-todo-sequence-head this))
  7534. (ass (assoc head org-todo-kwd-alist))
  7535. (interpret (nth 1 ass))
  7536. (done-word (nth 3 ass))
  7537. (final-done-word (nth 4 ass))
  7538. (last-state (or this ""))
  7539. (completion-ignore-case t)
  7540. (member (member this org-todo-keywords-1))
  7541. (tail (cdr member))
  7542. (state (cond
  7543. ((and org-todo-key-trigger
  7544. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7545. (and (not arg) org-use-fast-todo-selection
  7546. (not (eq org-use-fast-todo-selection 'prefix)))))
  7547. ;; Use fast selection
  7548. (org-fast-todo-selection))
  7549. ((and (equal arg '(4))
  7550. (or (not org-use-fast-todo-selection)
  7551. (not org-todo-key-trigger)))
  7552. ;; Read a state with completion
  7553. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7554. org-todo-keywords-1)
  7555. nil t))
  7556. ((eq arg 'right)
  7557. (if this
  7558. (if tail (car tail) nil)
  7559. (car org-todo-keywords-1)))
  7560. ((eq arg 'left)
  7561. (if (equal member org-todo-keywords-1)
  7562. nil
  7563. (if this
  7564. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7565. org-todo-keywords-1)
  7566. (org-last org-todo-keywords-1))))
  7567. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7568. (setq arg nil))) ; hack to fall back to cycling
  7569. (arg
  7570. ;; user or caller requests a specific state
  7571. (cond
  7572. ((equal arg "") nil)
  7573. ((eq arg 'none) nil)
  7574. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7575. ((eq arg 'nextset)
  7576. (or (car (cdr (member head org-todo-heads)))
  7577. (car org-todo-heads)))
  7578. ((eq arg 'previousset)
  7579. (let ((org-todo-heads (reverse org-todo-heads)))
  7580. (or (car (cdr (member head org-todo-heads)))
  7581. (car org-todo-heads))))
  7582. ((car (member arg org-todo-keywords-1)))
  7583. ((nth (1- (prefix-numeric-value arg))
  7584. org-todo-keywords-1))))
  7585. ((null member) (or head (car org-todo-keywords-1)))
  7586. ((equal this final-done-word) nil) ;; -> make empty
  7587. ((null tail) nil) ;; -> first entry
  7588. ((eq interpret 'sequence)
  7589. (car tail))
  7590. ((memq interpret '(type priority))
  7591. (if (eq this-command last-command)
  7592. (car tail)
  7593. (if (> (length tail) 0)
  7594. (or done-word (car org-done-keywords))
  7595. nil)))
  7596. (t nil)))
  7597. (next (if state (concat " " state " ") " "))
  7598. (change-plist (list :type 'todo-state-change :from this :to state
  7599. :position startpos))
  7600. dolog now-done-p)
  7601. (when org-blocker-hook
  7602. (unless (save-excursion
  7603. (save-match-data
  7604. (run-hook-with-args-until-failure
  7605. 'org-blocker-hook change-plist)))
  7606. (if (interactive-p)
  7607. (error "TODO state change from %s to %s blocked" this state)
  7608. ;; fail silently
  7609. (message "TODO state change from %s to %s blocked" this state)
  7610. (throw 'exit nil))))
  7611. (store-match-data match-data)
  7612. (replace-match next t t)
  7613. (unless (pos-visible-in-window-p hl-pos)
  7614. (message "TODO state changed to %s" (org-trim next)))
  7615. (unless head
  7616. (setq head (org-get-todo-sequence-head state)
  7617. ass (assoc head org-todo-kwd-alist)
  7618. interpret (nth 1 ass)
  7619. done-word (nth 3 ass)
  7620. final-done-word (nth 4 ass)))
  7621. (when (memq arg '(nextset previousset))
  7622. (message "Keyword-Set %d/%d: %s"
  7623. (- (length org-todo-sets) -1
  7624. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7625. (length org-todo-sets)
  7626. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7627. (setq org-last-todo-state-is-todo
  7628. (not (member state org-done-keywords)))
  7629. (setq now-done-p (and (member state org-done-keywords)
  7630. (not (member this org-done-keywords))))
  7631. (and logging (org-local-logging logging))
  7632. (when (and (or org-todo-log-states org-log-done)
  7633. (not (memq arg '(nextset previousset))))
  7634. ;; we need to look at recording a time and note
  7635. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7636. (nth 2 (assoc this org-todo-log-states))))
  7637. (when (and state
  7638. (member state org-not-done-keywords)
  7639. (not (member this org-not-done-keywords)))
  7640. ;; This is now a todo state and was not one before
  7641. ;; If there was a CLOSED time stamp, get rid of it.
  7642. (org-add-planning-info nil nil 'closed))
  7643. (when (and now-done-p org-log-done)
  7644. ;; It is now done, and it was not done before
  7645. (org-add-planning-info 'closed (org-current-time))
  7646. (if (and (not dolog) (eq 'note org-log-done))
  7647. (org-add-log-setup 'done state 'findpos 'note)))
  7648. (when (and state dolog)
  7649. ;; This is a non-nil state, and we need to log it
  7650. (org-add-log-setup 'state state 'findpos dolog)))
  7651. ;; Fixup tag positioning
  7652. (org-todo-trigger-tag-changes state)
  7653. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7654. (when org-provide-todo-statistics
  7655. (org-update-parent-todo-statistics))
  7656. (run-hooks 'org-after-todo-state-change-hook)
  7657. (if (and arg (not (member state org-done-keywords)))
  7658. (setq head (org-get-todo-sequence-head state)))
  7659. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7660. ;; Do we need to trigger a repeat?
  7661. (when now-done-p
  7662. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7663. ;; This is for the agenda, take a snapshot of the headline.
  7664. (save-match-data
  7665. (setq org-agenda-headline-snapshot-before-repeat
  7666. (org-get-heading))))
  7667. (org-auto-repeat-maybe state))
  7668. ;; Fixup cursor location if close to the keyword
  7669. (if (and (outline-on-heading-p)
  7670. (not (bolp))
  7671. (save-excursion (beginning-of-line 1)
  7672. (looking-at org-todo-line-regexp))
  7673. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7674. (progn
  7675. (goto-char (or (match-end 2) (match-end 1)))
  7676. (just-one-space)))
  7677. (when org-trigger-hook
  7678. (save-excursion
  7679. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7680. (defun org-update-parent-todo-statistics ()
  7681. "Update any statistics cookie in the parent of the current headline."
  7682. (interactive)
  7683. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7684. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7685. (catch 'exit
  7686. (save-excursion
  7687. (setq level (org-up-heading-safe))
  7688. (unless (and level
  7689. (re-search-forward box-re (point-at-eol) t))
  7690. (throw 'exit nil))
  7691. (setq is-percent (match-end 2))
  7692. (save-match-data
  7693. (unless (outline-next-heading) (throw 'exit nil))
  7694. (while (looking-at org-todo-line-regexp)
  7695. (setq kwd (match-string 2))
  7696. (and kwd (setq cnt-all (1+ cnt-all)))
  7697. (and (member kwd org-done-keywords)
  7698. (setq cnt-done (1+ cnt-done)))
  7699. (condition-case nil
  7700. (org-forward-same-level 1)
  7701. (error (end-of-line 1)))))
  7702. (replace-match
  7703. (if is-percent
  7704. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7705. (format "[%d/%d]" cnt-done cnt-all)))
  7706. (run-hook-with-args 'org-after-todo-statistics-hook
  7707. cnt-done (- cnt-all cnt-done))))))
  7708. (defvar org-after-todo-statistics-hook nil
  7709. "Hook that is called after a TODO statistics cookie has been updated.
  7710. Each function is called with two arguments: the number of not-done entries
  7711. and the number of done entries.
  7712. For example, the following function, when added to this hook, will switch
  7713. an entry to DONE when all children are done, and back to TODO when new
  7714. entries are set to a TODO status. Note that this hook is only called
  7715. when there is a statistics cookie in the headline!
  7716. (defun org-summary-todo (n-done n-not-done)
  7717. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7718. (let (org-log-done org-log-states) ; turn off logging
  7719. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7720. ")
  7721. (defun org-todo-trigger-tag-changes (state)
  7722. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7723. (let ((l org-todo-state-tags-triggers)
  7724. changes)
  7725. (when (or (not state) (equal state ""))
  7726. (setq changes (append changes (cdr (assoc "" l)))))
  7727. (when (and (stringp state) (> (length state) 0))
  7728. (setq changes (append changes (cdr (assoc state l)))))
  7729. (when (member state org-not-done-keywords)
  7730. (setq changes (append changes (cdr (assoc 'todo l)))))
  7731. (when (member state org-done-keywords)
  7732. (setq changes (append changes (cdr (assoc 'done l)))))
  7733. (dolist (c changes)
  7734. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7735. (defun org-local-logging (value)
  7736. "Get logging settings from a property VALUE."
  7737. (let* (words w a)
  7738. ;; directly set the variables, they are already local.
  7739. (setq org-log-done nil
  7740. org-log-repeat nil
  7741. org-todo-log-states nil)
  7742. (setq words (org-split-string value))
  7743. (while (setq w (pop words))
  7744. (cond
  7745. ((setq a (assoc w org-startup-options))
  7746. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7747. (set (nth 1 a) (nth 2 a))))
  7748. ((setq a (org-extract-log-state-settings w))
  7749. (and (member (car a) org-todo-keywords-1)
  7750. (push a org-todo-log-states)))))))
  7751. (defun org-get-todo-sequence-head (kwd)
  7752. "Return the head of the TODO sequence to which KWD belongs.
  7753. If KWD is not set, check if there is a text property remembering the
  7754. right sequence."
  7755. (let (p)
  7756. (cond
  7757. ((not kwd)
  7758. (or (get-text-property (point-at-bol) 'org-todo-head)
  7759. (progn
  7760. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7761. nil (point-at-eol)))
  7762. (get-text-property p 'org-todo-head))))
  7763. ((not (member kwd org-todo-keywords-1))
  7764. (car org-todo-keywords-1))
  7765. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7766. (defun org-fast-todo-selection ()
  7767. "Fast TODO keyword selection with single keys.
  7768. Returns the new TODO keyword, or nil if no state change should occur."
  7769. (let* ((fulltable org-todo-key-alist)
  7770. (done-keywords org-done-keywords) ;; needed for the faces.
  7771. (maxlen (apply 'max (mapcar
  7772. (lambda (x)
  7773. (if (stringp (car x)) (string-width (car x)) 0))
  7774. fulltable)))
  7775. (expert nil)
  7776. (fwidth (+ maxlen 3 1 3))
  7777. (ncol (/ (- (window-width) 4) fwidth))
  7778. tg cnt e c tbl
  7779. groups ingroup)
  7780. (save-window-excursion
  7781. (if expert
  7782. (set-buffer (get-buffer-create " *Org todo*"))
  7783. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7784. (erase-buffer)
  7785. (org-set-local 'org-done-keywords done-keywords)
  7786. (setq tbl fulltable cnt 0)
  7787. (while (setq e (pop tbl))
  7788. (cond
  7789. ((equal e '(:startgroup))
  7790. (push '() groups) (setq ingroup t)
  7791. (when (not (= cnt 0))
  7792. (setq cnt 0)
  7793. (insert "\n"))
  7794. (insert "{ "))
  7795. ((equal e '(:endgroup))
  7796. (setq ingroup nil cnt 0)
  7797. (insert "}\n"))
  7798. (t
  7799. (setq tg (car e) c (cdr e))
  7800. (if ingroup (push tg (car groups)))
  7801. (setq tg (org-add-props tg nil 'face
  7802. (org-get-todo-face tg)))
  7803. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7804. (insert "[" c "] " tg (make-string
  7805. (- fwidth 4 (length tg)) ?\ ))
  7806. (when (= (setq cnt (1+ cnt)) ncol)
  7807. (insert "\n")
  7808. (if ingroup (insert " "))
  7809. (setq cnt 0)))))
  7810. (insert "\n")
  7811. (goto-char (point-min))
  7812. (if (not expert) (org-fit-window-to-buffer))
  7813. (message "[a-z..]:Set [SPC]:clear")
  7814. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7815. (cond
  7816. ((or (= c ?\C-g)
  7817. (and (= c ?q) (not (rassoc c fulltable))))
  7818. (setq quit-flag t))
  7819. ((= c ?\ ) nil)
  7820. ((setq e (rassoc c fulltable) tg (car e))
  7821. tg)
  7822. (t (setq quit-flag t))))))
  7823. (defun org-entry-is-todo-p ()
  7824. (member (org-get-todo-state) org-not-done-keywords))
  7825. (defun org-entry-is-done-p ()
  7826. (member (org-get-todo-state) org-done-keywords))
  7827. (defun org-get-todo-state ()
  7828. (save-excursion
  7829. (org-back-to-heading t)
  7830. (and (looking-at org-todo-line-regexp)
  7831. (match-end 2)
  7832. (match-string 2))))
  7833. (defun org-at-date-range-p (&optional inactive-ok)
  7834. "Is the cursor inside a date range?"
  7835. (interactive)
  7836. (save-excursion
  7837. (catch 'exit
  7838. (let ((pos (point)))
  7839. (skip-chars-backward "^[<\r\n")
  7840. (skip-chars-backward "<[")
  7841. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7842. (>= (match-end 0) pos)
  7843. (throw 'exit t))
  7844. (skip-chars-backward "^<[\r\n")
  7845. (skip-chars-backward "<[")
  7846. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7847. (>= (match-end 0) pos)
  7848. (throw 'exit t)))
  7849. nil)))
  7850. (defun org-get-repeat ()
  7851. "Check if there is a deadline/schedule with repeater in this entry."
  7852. (save-match-data
  7853. (save-excursion
  7854. (org-back-to-heading t)
  7855. (if (re-search-forward
  7856. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7857. (match-string 1)))))
  7858. (defvar org-last-changed-timestamp)
  7859. (defvar org-last-inserted-timestamp)
  7860. (defvar org-log-post-message)
  7861. (defvar org-log-note-purpose)
  7862. (defvar org-log-note-how)
  7863. (defvar org-log-note-extra)
  7864. (defun org-auto-repeat-maybe (done-word)
  7865. "Check if the current headline contains a repeated deadline/schedule.
  7866. If yes, set TODO state back to what it was and change the base date
  7867. of repeating deadline/scheduled time stamps to new date.
  7868. This function is run automatically after each state change to a DONE state."
  7869. ;; last-state is dynamically scoped into this function
  7870. (let* ((repeat (org-get-repeat))
  7871. (aa (assoc last-state org-todo-kwd-alist))
  7872. (interpret (nth 1 aa))
  7873. (head (nth 2 aa))
  7874. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7875. (msg "Entry repeats: ")
  7876. (org-log-done nil)
  7877. (org-todo-log-states nil)
  7878. (nshiftmax 10) (nshift 0)
  7879. re type n what ts time)
  7880. (when repeat
  7881. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7882. (org-todo (if (eq interpret 'type) last-state head))
  7883. (when org-log-repeat
  7884. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7885. (memq 'org-add-log-note post-command-hook))
  7886. ;; OK, we are already setup for some record
  7887. (if (eq org-log-repeat 'note)
  7888. ;; make sure we take a note, not only a time stamp
  7889. (setq org-log-note-how 'note))
  7890. ;; Set up for taking a record
  7891. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7892. 'findpos org-log-repeat)))
  7893. (org-back-to-heading t)
  7894. (org-add-planning-info nil nil 'closed)
  7895. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7896. org-deadline-time-regexp "\\)\\|\\("
  7897. org-ts-regexp "\\)"))
  7898. (while (re-search-forward
  7899. re (save-excursion (outline-next-heading) (point)) t)
  7900. (setq type (if (match-end 1) org-scheduled-string
  7901. (if (match-end 3) org-deadline-string "Plain:"))
  7902. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  7903. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7904. (setq n (string-to-number (match-string 2 ts))
  7905. what (match-string 3 ts))
  7906. (if (equal what "w") (setq n (* n 7) what "d"))
  7907. ;; Preparation, see if we need to modify the start date for the change
  7908. (when (match-end 1)
  7909. (setq time (save-match-data (org-time-string-to-time ts)))
  7910. (cond
  7911. ((equal (match-string 1 ts) ".")
  7912. ;; Shift starting date to today
  7913. (org-timestamp-change
  7914. (- (time-to-days (current-time)) (time-to-days time))
  7915. 'day))
  7916. ((equal (match-string 1 ts) "+")
  7917. (while (or (= nshift 0)
  7918. (<= (time-to-days time) (time-to-days (current-time))))
  7919. (when (= (incf nshift) nshiftmax)
  7920. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7921. (error "Abort")))
  7922. (org-timestamp-change n (cdr (assoc what whata)))
  7923. (org-at-timestamp-p t)
  7924. (setq ts (match-string 1))
  7925. (setq time (save-match-data (org-time-string-to-time ts))))
  7926. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7927. ;; rematch, so that we have everything in place for the real shift
  7928. (org-at-timestamp-p t)
  7929. (setq ts (match-string 1))
  7930. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7931. (org-timestamp-change n (cdr (assoc what whata)))
  7932. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7933. (setq org-log-post-message msg)
  7934. (message "%s" msg))))
  7935. (defun org-show-todo-tree (arg)
  7936. "Make a compact tree which shows all headlines marked with TODO.
  7937. The tree will show the lines where the regexp matches, and all higher
  7938. headlines above the match.
  7939. With a \\[universal-argument] prefix, also show the DONE entries.
  7940. With a numeric prefix N, construct a sparse tree for the Nth element
  7941. of `org-todo-keywords-1'."
  7942. (interactive "P")
  7943. (let ((case-fold-search nil)
  7944. (kwd-re
  7945. (cond ((null arg) org-not-done-regexp)
  7946. ((equal arg '(4))
  7947. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7948. (mapcar 'list org-todo-keywords-1))))
  7949. (concat "\\("
  7950. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7951. "\\)\\>")))
  7952. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7953. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7954. org-todo-keywords-1)))
  7955. (t (error "Invalid prefix argument: %s" arg)))))
  7956. (message "%d TODO entries found"
  7957. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7958. (defun org-deadline (&optional remove time)
  7959. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7960. With argument REMOVE, remove any deadline from the item.
  7961. When TIME is set, it should be an internal time specification, and the
  7962. scheduling will use the corresponding date."
  7963. (interactive "P")
  7964. (if remove
  7965. (progn
  7966. (org-remove-timestamp-with-keyword org-deadline-string)
  7967. (message "Item no longer has a deadline."))
  7968. (if (org-get-repeat)
  7969. (error "Cannot change deadline on task with repeater, please do that by hand")
  7970. (org-add-planning-info 'deadline time 'closed)
  7971. (message "Deadline on %s" org-last-inserted-timestamp))))
  7972. (defun org-schedule (&optional remove time)
  7973. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7974. With argument REMOVE, remove any scheduling date from the item.
  7975. When TIME is set, it should be an internal time specification, and the
  7976. scheduling will use the corresponding date."
  7977. (interactive "P")
  7978. (if remove
  7979. (progn
  7980. (org-remove-timestamp-with-keyword org-scheduled-string)
  7981. (message "Item is no longer scheduled."))
  7982. (if (org-get-repeat)
  7983. (error "Cannot reschedule task with repeater, please do that by hand")
  7984. (org-add-planning-info 'scheduled time 'closed)
  7985. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7986. (defun org-remove-timestamp-with-keyword (keyword)
  7987. "Remove all time stamps with KEYWORD in the current entry."
  7988. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7989. beg)
  7990. (save-excursion
  7991. (org-back-to-heading t)
  7992. (setq beg (point))
  7993. (org-end-of-subtree t t)
  7994. (while (re-search-backward re beg t)
  7995. (replace-match "")
  7996. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7997. (equal (char-before) ?\ ))
  7998. (backward-delete-char 1)
  7999. (if (string-match "^[ \t]*$" (buffer-substring
  8000. (point-at-bol) (point-at-eol)))
  8001. (delete-region (point-at-bol)
  8002. (min (point-max) (1+ (point-at-eol))))))))))
  8003. (defun org-add-planning-info (what &optional time &rest remove)
  8004. "Insert new timestamp with keyword in the line directly after the headline.
  8005. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8006. If non is given, the user is prompted for a date.
  8007. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8008. be removed."
  8009. (interactive)
  8010. (let (org-time-was-given org-end-time-was-given ts
  8011. end default-time default-input)
  8012. (when (and (not time) (memq what '(scheduled deadline)))
  8013. ;; Try to get a default date/time from existing timestamp
  8014. (save-excursion
  8015. (org-back-to-heading t)
  8016. (setq end (save-excursion (outline-next-heading) (point)))
  8017. (when (re-search-forward (if (eq what 'scheduled)
  8018. org-scheduled-time-regexp
  8019. org-deadline-time-regexp)
  8020. end t)
  8021. (setq ts (match-string 1)
  8022. default-time
  8023. (apply 'encode-time (org-parse-time-string ts))
  8024. default-input (and ts (org-get-compact-tod ts))))))
  8025. (when what
  8026. ;; If necessary, get the time from the user
  8027. (setq time (or time (org-read-date nil 'to-time nil nil
  8028. default-time default-input))))
  8029. (when (and org-insert-labeled-timestamps-at-point
  8030. (member what '(scheduled deadline)))
  8031. (insert
  8032. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8033. (org-insert-time-stamp time org-time-was-given
  8034. nil nil nil (list org-end-time-was-given))
  8035. (setq what nil))
  8036. (save-excursion
  8037. (save-restriction
  8038. (let (col list elt ts buffer-invisibility-spec)
  8039. (org-back-to-heading t)
  8040. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8041. (goto-char (match-end 1))
  8042. (setq col (current-column))
  8043. (goto-char (match-end 0))
  8044. (if (eobp) (insert "\n") (forward-char 1))
  8045. (if (and (not (looking-at outline-regexp))
  8046. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8047. "[^\r\n]*"))
  8048. (not (equal (match-string 1) org-clock-string)))
  8049. (narrow-to-region (match-beginning 0) (match-end 0))
  8050. (insert-before-markers "\n")
  8051. (backward-char 1)
  8052. (narrow-to-region (point) (point))
  8053. (and org-adapt-indentation (org-indent-to-column col)))
  8054. ;; Check if we have to remove something.
  8055. (setq list (cons what remove))
  8056. (while list
  8057. (setq elt (pop list))
  8058. (goto-char (point-min))
  8059. (when (or (and (eq elt 'scheduled)
  8060. (re-search-forward org-scheduled-time-regexp nil t))
  8061. (and (eq elt 'deadline)
  8062. (re-search-forward org-deadline-time-regexp nil t))
  8063. (and (eq elt 'closed)
  8064. (re-search-forward org-closed-time-regexp nil t)))
  8065. (replace-match "")
  8066. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8067. (if (looking-at " +") (replace-match ""))))
  8068. (goto-char (point-max))
  8069. (when what
  8070. (insert
  8071. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8072. (cond ((eq what 'scheduled) org-scheduled-string)
  8073. ((eq what 'deadline) org-deadline-string)
  8074. ((eq what 'closed) org-closed-string))
  8075. " ")
  8076. (setq ts (org-insert-time-stamp
  8077. time
  8078. (or org-time-was-given
  8079. (and (eq what 'closed) org-log-done-with-time))
  8080. (eq what 'closed)
  8081. nil nil (list org-end-time-was-given)))
  8082. (end-of-line 1))
  8083. (goto-char (point-min))
  8084. (widen)
  8085. (if (and (looking-at "[ \t]+\n")
  8086. (equal (char-before) ?\n))
  8087. (delete-region (1- (point)) (point-at-eol)))
  8088. ts)))))
  8089. (defvar org-log-note-marker (make-marker))
  8090. (defvar org-log-note-purpose nil)
  8091. (defvar org-log-note-state nil)
  8092. (defvar org-log-note-how nil)
  8093. (defvar org-log-note-extra nil)
  8094. (defvar org-log-note-window-configuration nil)
  8095. (defvar org-log-note-return-to (make-marker))
  8096. (defvar org-log-post-message nil
  8097. "Message to be displayed after a log note has been stored.
  8098. The auto-repeater uses this.")
  8099. (defun org-add-note ()
  8100. "Add a note to the current entry.
  8101. This is done in the same way as adding a state change note."
  8102. (interactive)
  8103. (org-add-log-setup 'note nil 'findpos nil))
  8104. (defvar org-property-end-re)
  8105. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8106. "Set up the post command hook to take a note.
  8107. If this is about to TODO state change, the new state is expected in STATE.
  8108. When FINDPOS is non-nil, find the correct position for the note in
  8109. the current entry. If not, assume that it can be inserted at point.
  8110. HOW is an indicator what kind of note should be created.
  8111. EXTRA is additional text that will be inserted into the notes buffer."
  8112. (save-restriction
  8113. (save-excursion
  8114. (when findpos
  8115. (org-back-to-heading t)
  8116. (narrow-to-region (point) (save-excursion
  8117. (outline-next-heading) (point)))
  8118. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8119. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8120. "[^\r\n]*\\)?"))
  8121. (goto-char (match-end 0))
  8122. (when (and org-log-state-notes-insert-after-drawers
  8123. (save-excursion
  8124. (forward-line) (looking-at org-drawer-regexp)))
  8125. (progn (forward-line)
  8126. (while (looking-at org-drawer-regexp)
  8127. (goto-char (match-end 0))
  8128. (re-search-forward org-property-end-re (point-max) t)
  8129. (forward-line))
  8130. (forward-line -1)))
  8131. (unless org-log-states-order-reversed
  8132. (and (= (char-after) ?\n) (forward-char 1))
  8133. (org-skip-over-state-notes)
  8134. (skip-chars-backward " \t\n\r")))
  8135. (move-marker org-log-note-marker (point))
  8136. (setq org-log-note-purpose purpose
  8137. org-log-note-state state
  8138. org-log-note-how how
  8139. org-log-note-extra extra)
  8140. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8141. (defun org-skip-over-state-notes ()
  8142. "Skip past the list of State notes in an entry."
  8143. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8144. (while (looking-at "[ \t]*- State")
  8145. (condition-case nil
  8146. (org-next-item)
  8147. (error (org-end-of-item)))))
  8148. (defun org-add-log-note (&optional purpose)
  8149. "Pop up a window for taking a note, and add this note later at point."
  8150. (remove-hook 'post-command-hook 'org-add-log-note)
  8151. (setq org-log-note-window-configuration (current-window-configuration))
  8152. (delete-other-windows)
  8153. (move-marker org-log-note-return-to (point))
  8154. (switch-to-buffer (marker-buffer org-log-note-marker))
  8155. (goto-char org-log-note-marker)
  8156. (org-switch-to-buffer-other-window "*Org Note*")
  8157. (erase-buffer)
  8158. (if (memq org-log-note-how '(time state))
  8159. (let (current-prefix-arg) (org-store-log-note))
  8160. (let ((org-inhibit-startup t)) (org-mode))
  8161. (insert (format "# Insert note for %s.
  8162. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8163. (cond
  8164. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8165. ((eq org-log-note-purpose 'done) "closed todo item")
  8166. ((eq org-log-note-purpose 'state)
  8167. (format "state change to \"%s\"" org-log-note-state))
  8168. ((eq org-log-note-purpose 'note)
  8169. "this entry")
  8170. (t (error "This should not happen")))))
  8171. (if org-log-note-extra (insert org-log-note-extra))
  8172. (org-set-local 'org-finish-function 'org-store-log-note)))
  8173. (defvar org-note-abort nil) ; dynamically scoped
  8174. (defun org-store-log-note ()
  8175. "Finish taking a log note, and insert it to where it belongs."
  8176. (let ((txt (buffer-string))
  8177. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8178. lines ind)
  8179. (kill-buffer (current-buffer))
  8180. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8181. (setq txt (replace-match "" t t txt)))
  8182. (if (string-match "\\s-+\\'" txt)
  8183. (setq txt (replace-match "" t t txt)))
  8184. (setq lines (org-split-string txt "\n"))
  8185. (when (and note (string-match "\\S-" note))
  8186. (setq note
  8187. (org-replace-escapes
  8188. note
  8189. (list (cons "%u" (user-login-name))
  8190. (cons "%U" user-full-name)
  8191. (cons "%t" (format-time-string
  8192. (org-time-stamp-format 'long 'inactive)
  8193. (current-time)))
  8194. (cons "%s" (if org-log-note-state
  8195. (concat "\"" org-log-note-state "\"")
  8196. "")))))
  8197. (if lines (setq note (concat note " \\\\")))
  8198. (push note lines))
  8199. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8200. (when lines
  8201. (save-excursion
  8202. (set-buffer (marker-buffer org-log-note-marker))
  8203. (save-excursion
  8204. (goto-char org-log-note-marker)
  8205. (move-marker org-log-note-marker nil)
  8206. (end-of-line 1)
  8207. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8208. (indent-relative nil)
  8209. (insert "- " (pop lines))
  8210. (org-indent-line-function)
  8211. (beginning-of-line 1)
  8212. (looking-at "[ \t]*")
  8213. (setq ind (concat (match-string 0) " "))
  8214. (end-of-line 1)
  8215. (while lines (insert "\n" ind (pop lines)))))))
  8216. (set-window-configuration org-log-note-window-configuration)
  8217. (with-current-buffer (marker-buffer org-log-note-return-to)
  8218. (goto-char org-log-note-return-to))
  8219. (move-marker org-log-note-return-to nil)
  8220. (and org-log-post-message (message "%s" org-log-post-message)))
  8221. (defun org-sparse-tree (&optional arg)
  8222. "Create a sparse tree, prompt for the details.
  8223. This command can create sparse trees. You first need to select the type
  8224. of match used to create the tree:
  8225. t Show entries with a specific TODO keyword.
  8226. T Show entries selected by a tags match.
  8227. p Enter a property name and its value (both with completion on existing
  8228. names/values) and show entries with that property.
  8229. r Show entries matching a regular expression
  8230. d Show deadlines due within `org-deadline-warning-days'."
  8231. (interactive "P")
  8232. (let (ans kwd value)
  8233. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8234. (setq ans (read-char-exclusive))
  8235. (cond
  8236. ((equal ans ?d)
  8237. (call-interactively 'org-check-deadlines))
  8238. ((equal ans ?b)
  8239. (call-interactively 'org-check-before-date))
  8240. ((equal ans ?t)
  8241. (org-show-todo-tree '(4)))
  8242. ((equal ans ?T)
  8243. (call-interactively 'org-tags-sparse-tree))
  8244. ((member ans '(?p ?P))
  8245. (setq kwd (org-ido-completing-read "Property: "
  8246. (mapcar 'list (org-buffer-property-keys))))
  8247. (setq value (org-ido-completing-read "Value: "
  8248. (mapcar 'list (org-property-values kwd))))
  8249. (unless (string-match "\\`{.*}\\'" value)
  8250. (setq value (concat "\"" value "\"")))
  8251. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8252. ((member ans '(?r ?R ?/))
  8253. (call-interactively 'org-occur))
  8254. (t (error "No such sparse tree command \"%c\"" ans)))))
  8255. (defvar org-occur-highlights nil
  8256. "List of overlays used for occur matches.")
  8257. (make-variable-buffer-local 'org-occur-highlights)
  8258. (defvar org-occur-parameters nil
  8259. "Parameters of the active org-occur calls.
  8260. This is a list, each call to org-occur pushes as cons cell,
  8261. containing the regular expression and the callback, onto the list.
  8262. The list can contain several entries if `org-occur' has been called
  8263. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8264. will only contain one set of parameters. When the highlights are
  8265. removed (for example with `C-c C-c', or with the next edit (depending
  8266. on `org-remove-highlights-with-change'), this variable is emptied
  8267. as well.")
  8268. (make-variable-buffer-local 'org-occur-parameters)
  8269. (defun org-occur (regexp &optional keep-previous callback)
  8270. "Make a compact tree which shows all matches of REGEXP.
  8271. The tree will show the lines where the regexp matches, and all higher
  8272. headlines above the match. It will also show the heading after the match,
  8273. to make sure editing the matching entry is easy.
  8274. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8275. call to `org-occur' will be kept, to allow stacking of calls to this
  8276. command.
  8277. If CALLBACK is non-nil, it is a function which is called to confirm
  8278. that the match should indeed be shown."
  8279. (interactive "sRegexp: \nP")
  8280. (unless keep-previous
  8281. (org-remove-occur-highlights nil nil t))
  8282. (push (cons regexp callback) org-occur-parameters)
  8283. (let ((cnt 0))
  8284. (save-excursion
  8285. (goto-char (point-min))
  8286. (if (or (not keep-previous) ; do not want to keep
  8287. (not org-occur-highlights)) ; no previous matches
  8288. ;; hide everything
  8289. (org-overview))
  8290. (while (re-search-forward regexp nil t)
  8291. (when (or (not callback)
  8292. (save-match-data (funcall callback)))
  8293. (setq cnt (1+ cnt))
  8294. (when org-highlight-sparse-tree-matches
  8295. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8296. (org-show-context 'occur-tree))))
  8297. (when org-remove-highlights-with-change
  8298. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8299. nil 'local))
  8300. (unless org-sparse-tree-open-archived-trees
  8301. (org-hide-archived-subtrees (point-min) (point-max)))
  8302. (run-hooks 'org-occur-hook)
  8303. (if (interactive-p)
  8304. (message "%d match(es) for regexp %s" cnt regexp))
  8305. cnt))
  8306. (defun org-show-context (&optional key)
  8307. "Make sure point and context and visible.
  8308. How much context is shown depends upon the variables
  8309. `org-show-hierarchy-above', `org-show-following-heading'. and
  8310. `org-show-siblings'."
  8311. (let ((heading-p (org-on-heading-p t))
  8312. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8313. (following-p (org-get-alist-option org-show-following-heading key))
  8314. (entry-p (org-get-alist-option org-show-entry-below key))
  8315. (siblings-p (org-get-alist-option org-show-siblings key)))
  8316. (catch 'exit
  8317. ;; Show heading or entry text
  8318. (if (and heading-p (not entry-p))
  8319. (org-flag-heading nil) ; only show the heading
  8320. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8321. (org-show-hidden-entry))) ; show entire entry
  8322. (when following-p
  8323. ;; Show next sibling, or heading below text
  8324. (save-excursion
  8325. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8326. (org-flag-heading nil))))
  8327. (when siblings-p (org-show-siblings))
  8328. (when hierarchy-p
  8329. ;; show all higher headings, possibly with siblings
  8330. (save-excursion
  8331. (while (and (condition-case nil
  8332. (progn (org-up-heading-all 1) t)
  8333. (error nil))
  8334. (not (bobp)))
  8335. (org-flag-heading nil)
  8336. (when siblings-p (org-show-siblings))))))))
  8337. (defun org-reveal (&optional siblings)
  8338. "Show current entry, hierarchy above it, and the following headline.
  8339. This can be used to show a consistent set of context around locations
  8340. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8341. not t for the search context.
  8342. With optional argument SIBLINGS, on each level of the hierarchy all
  8343. siblings are shown. This repairs the tree structure to what it would
  8344. look like when opened with hierarchical calls to `org-cycle'."
  8345. (interactive "P")
  8346. (let ((org-show-hierarchy-above t)
  8347. (org-show-following-heading t)
  8348. (org-show-siblings (if siblings t org-show-siblings)))
  8349. (org-show-context nil)))
  8350. (defun org-highlight-new-match (beg end)
  8351. "Highlight from BEG to END and mark the highlight is an occur headline."
  8352. (let ((ov (org-make-overlay beg end)))
  8353. (org-overlay-put ov 'face 'secondary-selection)
  8354. (push ov org-occur-highlights)))
  8355. (defun org-remove-occur-highlights (&optional beg end noremove)
  8356. "Remove the occur highlights from the buffer.
  8357. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8358. from the `before-change-functions' in the current buffer."
  8359. (interactive)
  8360. (unless org-inhibit-highlight-removal
  8361. (mapc 'org-delete-overlay org-occur-highlights)
  8362. (setq org-occur-highlights nil)
  8363. (setq org-occur-parameters nil)
  8364. (unless noremove
  8365. (remove-hook 'before-change-functions
  8366. 'org-remove-occur-highlights 'local))))
  8367. ;;;; Priorities
  8368. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8369. "Regular expression matching the priority indicator.")
  8370. (defvar org-remove-priority-next-time nil)
  8371. (defun org-priority-up ()
  8372. "Increase the priority of the current item."
  8373. (interactive)
  8374. (org-priority 'up))
  8375. (defun org-priority-down ()
  8376. "Decrease the priority of the current item."
  8377. (interactive)
  8378. (org-priority 'down))
  8379. (defun org-priority (&optional action)
  8380. "Change the priority of an item by ARG.
  8381. ACTION can be `set', `up', `down', or a character."
  8382. (interactive)
  8383. (setq action (or action 'set))
  8384. (let (current new news have remove)
  8385. (save-excursion
  8386. (org-back-to-heading)
  8387. (if (looking-at org-priority-regexp)
  8388. (setq current (string-to-char (match-string 2))
  8389. have t)
  8390. (setq current org-default-priority))
  8391. (cond
  8392. ((or (eq action 'set)
  8393. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8394. (if (not (eq action 'set))
  8395. (setq new action)
  8396. (message "Priority %c-%c, SPC to remove: "
  8397. org-highest-priority org-lowest-priority)
  8398. (setq new (read-char-exclusive)))
  8399. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8400. (= (upcase org-lowest-priority) org-lowest-priority))
  8401. (setq new (upcase new)))
  8402. (cond ((equal new ?\ ) (setq remove t))
  8403. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8404. (error "Priority must be between `%c' and `%c'"
  8405. org-highest-priority org-lowest-priority))))
  8406. ((eq action 'up)
  8407. (if (and (not have) (eq last-command this-command))
  8408. (setq new org-lowest-priority)
  8409. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8410. org-default-priority (1- current)))))
  8411. ((eq action 'down)
  8412. (if (and (not have) (eq last-command this-command))
  8413. (setq new org-highest-priority)
  8414. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8415. org-default-priority (1+ current)))))
  8416. (t (error "Invalid action")))
  8417. (if (or (< (upcase new) org-highest-priority)
  8418. (> (upcase new) org-lowest-priority))
  8419. (setq remove t))
  8420. (setq news (format "%c" new))
  8421. (if have
  8422. (if remove
  8423. (replace-match "" t t nil 1)
  8424. (replace-match news t t nil 2))
  8425. (if remove
  8426. (error "No priority cookie found in line")
  8427. (looking-at org-todo-line-regexp)
  8428. (if (match-end 2)
  8429. (progn
  8430. (goto-char (match-end 2))
  8431. (insert " [#" news "]"))
  8432. (goto-char (match-beginning 3))
  8433. (insert "[#" news "] ")))))
  8434. (org-preserve-lc (org-set-tags nil 'align))
  8435. (if remove
  8436. (message "Priority removed")
  8437. (message "Priority of current item set to %s" news))))
  8438. (defun org-get-priority (s)
  8439. "Find priority cookie and return priority."
  8440. (save-match-data
  8441. (if (not (string-match org-priority-regexp s))
  8442. (* 1000 (- org-lowest-priority org-default-priority))
  8443. (* 1000 (- org-lowest-priority
  8444. (string-to-char (match-string 2 s)))))))
  8445. ;;;; Tags
  8446. (defvar org-agenda-archives-mode)
  8447. (defun org-scan-tags (action matcher &optional todo-only)
  8448. "Scan headline tags with inheritance and produce output ACTION.
  8449. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8450. or `agenda' to produce an entry list for an agenda view. It can also be
  8451. a Lisp form or a function that should be called at each matched headline, in
  8452. this case the return value is a list of all return values from these calls.
  8453. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8454. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8455. only lines with a TODO keyword are included in the output."
  8456. (require 'org-agenda)
  8457. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8458. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8459. (org-re
  8460. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8461. (props (list 'face 'default
  8462. 'done-face 'org-done
  8463. 'undone-face 'default
  8464. 'mouse-face 'highlight
  8465. 'org-not-done-regexp org-not-done-regexp
  8466. 'org-todo-regexp org-todo-regexp
  8467. 'keymap org-agenda-keymap
  8468. 'help-echo
  8469. (format "mouse-2 or RET jump to org file %s"
  8470. (abbreviate-file-name
  8471. (or (buffer-file-name (buffer-base-buffer))
  8472. (buffer-name (buffer-base-buffer)))))))
  8473. (case-fold-search nil)
  8474. lspos tags tags-list
  8475. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8476. (llast 0) rtn rtn1 level category i txt
  8477. todo marker entry priority)
  8478. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8479. (setq action (list 'lambda nil action)))
  8480. (save-excursion
  8481. (goto-char (point-min))
  8482. (when (eq action 'sparse-tree)
  8483. (org-overview)
  8484. (org-remove-occur-highlights))
  8485. (while (re-search-forward re nil t)
  8486. (catch :skip
  8487. (setq todo (if (match-end 1) (match-string 2))
  8488. tags (if (match-end 4) (match-string 4)))
  8489. (goto-char (setq lspos (1+ (match-beginning 0))))
  8490. (setq level (org-reduced-level (funcall outline-level))
  8491. category (org-get-category))
  8492. (setq i llast llast level)
  8493. ;; remove tag lists from same and sublevels
  8494. (while (>= i level)
  8495. (when (setq entry (assoc i tags-alist))
  8496. (setq tags-alist (delete entry tags-alist)))
  8497. (setq i (1- i)))
  8498. ;; add the next tags
  8499. (when tags
  8500. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8501. tags-alist
  8502. (cons (cons level tags) tags-alist)))
  8503. ;; compile tags for current headline
  8504. (setq tags-list
  8505. (if org-use-tag-inheritance
  8506. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8507. tags))
  8508. (when org-use-tag-inheritance
  8509. (setcdr (car tags-alist)
  8510. (mapcar (lambda (x)
  8511. (setq x (copy-sequence x))
  8512. (org-add-prop-inherited x))
  8513. (cdar tags-alist))))
  8514. (when (and tags org-use-tag-inheritance
  8515. (not (eq t org-use-tag-inheritance)))
  8516. ;; selective inheritance, remove uninherited ones
  8517. (setcdr (car tags-alist)
  8518. (org-remove-uniherited-tags (cdar tags-alist))))
  8519. (when (and (or (not todo-only)
  8520. (and (member todo org-not-done-keywords)
  8521. (or (not org-agenda-tags-todo-honor-ignore-options)
  8522. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8523. (let ((case-fold-search t)) (eval matcher))
  8524. (or
  8525. (not (member org-archive-tag tags-list))
  8526. ;; we have an archive tag, should we use this anyway?
  8527. (or (not org-agenda-skip-archived-trees)
  8528. (and (eq action 'agenda) org-agenda-archives-mode))))
  8529. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8530. ;; select this headline
  8531. (cond
  8532. ((eq action 'sparse-tree)
  8533. (and org-highlight-sparse-tree-matches
  8534. (org-get-heading) (match-end 0)
  8535. (org-highlight-new-match
  8536. (match-beginning 0) (match-beginning 1)))
  8537. (org-show-context 'tags-tree))
  8538. ((eq action 'agenda)
  8539. (setq txt (org-format-agenda-item
  8540. ""
  8541. (concat
  8542. (if org-tags-match-list-sublevels
  8543. (make-string (1- level) ?.) "")
  8544. (org-get-heading))
  8545. category (org-get-tags-at))
  8546. priority (org-get-priority txt))
  8547. (goto-char lspos)
  8548. (setq marker (org-agenda-new-marker))
  8549. (org-add-props txt props
  8550. 'org-marker marker 'org-hd-marker marker 'org-category category
  8551. 'priority priority 'type "tagsmatch")
  8552. (push txt rtn))
  8553. ((functionp action)
  8554. (save-excursion
  8555. (setq rtn1 (funcall action))
  8556. (push rtn1 rtn))
  8557. (goto-char (point-at-eol)))
  8558. (t (error "Invalid action")))
  8559. ;; if we are to skip sublevels, jump to end of subtree
  8560. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8561. (when (and (eq action 'sparse-tree)
  8562. (not org-sparse-tree-open-archived-trees))
  8563. (org-hide-archived-subtrees (point-min) (point-max)))
  8564. (nreverse rtn)))
  8565. (defun org-remove-uniherited-tags (tags)
  8566. "Remove all tags that are not inherited from the list TAGS."
  8567. (cond
  8568. ((eq org-use-tag-inheritance t)
  8569. (if org-tags-exclude-from-inheritance
  8570. (org-delete-all org-tags-exclude-from-inheritance tags)
  8571. tags))
  8572. ((not org-use-tag-inheritance) nil)
  8573. ((stringp org-use-tag-inheritance)
  8574. (delq nil (mapcar
  8575. (lambda (x)
  8576. (if (and (string-match org-use-tag-inheritance x)
  8577. (not (member x org-tags-exclude-from-inheritance)))
  8578. x nil))
  8579. tags)))
  8580. ((listp org-use-tag-inheritance)
  8581. (delq nil (mapcar
  8582. (lambda (x)
  8583. (if (member x org-use-tag-inheritance) x nil))
  8584. tags)))))
  8585. (defvar todo-only) ;; dynamically scoped
  8586. (defun org-tags-sparse-tree (&optional todo-only match)
  8587. "Create a sparse tree according to tags string MATCH.
  8588. MATCH can contain positive and negative selection of tags, like
  8589. \"+WORK+URGENT-WITHBOSS\".
  8590. If optional argument TODO-ONLY is non-nil, only select lines that are
  8591. also TODO lines."
  8592. (interactive "P")
  8593. (org-prepare-agenda-buffers (list (current-buffer)))
  8594. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8595. (defvar org-cached-props nil)
  8596. (defun org-cached-entry-get (pom property)
  8597. (if (or (eq t org-use-property-inheritance)
  8598. (and (stringp org-use-property-inheritance)
  8599. (string-match org-use-property-inheritance property))
  8600. (and (listp org-use-property-inheritance)
  8601. (member property org-use-property-inheritance)))
  8602. ;; Caching is not possible, check it directly
  8603. (org-entry-get pom property 'inherit)
  8604. ;; Get all properties, so that we can do complicated checks easily
  8605. (cdr (assoc property (or org-cached-props
  8606. (setq org-cached-props
  8607. (org-entry-properties pom)))))))
  8608. (defun org-global-tags-completion-table (&optional files)
  8609. "Return the list of all tags in all agenda buffer/files."
  8610. (save-excursion
  8611. (org-uniquify
  8612. (delq nil
  8613. (apply 'append
  8614. (mapcar
  8615. (lambda (file)
  8616. (set-buffer (find-file-noselect file))
  8617. (append (org-get-buffer-tags)
  8618. (mapcar (lambda (x) (if (stringp (car-safe x))
  8619. (list (car-safe x)) nil))
  8620. org-tag-alist)))
  8621. (if (and files (car files))
  8622. files
  8623. (org-agenda-files))))))))
  8624. (defun org-make-tags-matcher (match)
  8625. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8626. ;; todo-only is scoped dynamically into this function, and the function
  8627. ;; may change it if the matcher asks for it.
  8628. (unless match
  8629. ;; Get a new match request, with completion
  8630. (let ((org-last-tags-completion-table
  8631. (org-global-tags-completion-table)))
  8632. (setq match (org-completing-read
  8633. "Match: " 'org-tags-completion-function nil nil nil
  8634. 'org-tags-history))))
  8635. ;; Parse the string and create a lisp form
  8636. (let ((match0 match)
  8637. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8638. minus tag mm
  8639. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8640. orterms term orlist re-p str-p level-p level-op time-p
  8641. prop-p pn pv po cat-p gv rest)
  8642. (if (string-match "/+" match)
  8643. ;; match contains also a todo-matching request
  8644. (progn
  8645. (setq tagsmatch (substring match 0 (match-beginning 0))
  8646. todomatch (substring match (match-end 0)))
  8647. (if (string-match "^!" todomatch)
  8648. (setq todo-only t todomatch (substring todomatch 1)))
  8649. (if (string-match "^\\s-*$" todomatch)
  8650. (setq todomatch nil)))
  8651. ;; only matching tags
  8652. (setq tagsmatch match todomatch nil))
  8653. ;; Make the tags matcher
  8654. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8655. (setq tagsmatcher t)
  8656. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8657. (while (setq term (pop orterms))
  8658. (while (and (equal (substring term -1) "\\") orterms)
  8659. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8660. (while (string-match re term)
  8661. (setq rest (substring term (match-end 0))
  8662. minus (and (match-end 1)
  8663. (equal (match-string 1 term) "-"))
  8664. tag (match-string 2 term)
  8665. re-p (equal (string-to-char tag) ?{)
  8666. level-p (match-end 4)
  8667. prop-p (match-end 5)
  8668. mm (cond
  8669. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8670. (level-p
  8671. (setq level-op (org-op-to-function (match-string 3 term)))
  8672. `(,level-op level ,(string-to-number
  8673. (match-string 4 term))))
  8674. (prop-p
  8675. (setq pn (match-string 5 term)
  8676. po (match-string 6 term)
  8677. pv (match-string 7 term)
  8678. cat-p (equal pn "CATEGORY")
  8679. re-p (equal (string-to-char pv) ?{)
  8680. str-p (equal (string-to-char pv) ?\")
  8681. time-p (save-match-data
  8682. (string-match "^\"[[<].*[]>]\"$" pv))
  8683. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8684. (if time-p (setq pv (org-matcher-time pv)))
  8685. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8686. (cond
  8687. ((equal pn "CATEGORY")
  8688. (setq gv '(get-text-property (point) 'org-category)))
  8689. ((equal pn "TODO")
  8690. (setq gv 'todo))
  8691. (t
  8692. (setq gv `(org-cached-entry-get nil ,pn))))
  8693. (if re-p
  8694. (if (eq po 'org<>)
  8695. `(not (string-match ,pv (or ,gv "")))
  8696. `(string-match ,pv (or ,gv "")))
  8697. (if str-p
  8698. `(,po (or ,gv "") ,pv)
  8699. `(,po (string-to-number (or ,gv ""))
  8700. ,(string-to-number pv) ))))
  8701. (t `(member ,(downcase tag) tags-list)))
  8702. mm (if minus (list 'not mm) mm)
  8703. term rest)
  8704. (push mm tagsmatcher))
  8705. (push (if (> (length tagsmatcher) 1)
  8706. (cons 'and tagsmatcher)
  8707. (car tagsmatcher))
  8708. orlist)
  8709. (setq tagsmatcher nil))
  8710. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8711. (setq tagsmatcher
  8712. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8713. ;; Make the todo matcher
  8714. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8715. (setq todomatcher t)
  8716. (setq orterms (org-split-string todomatch "|") orlist nil)
  8717. (while (setq term (pop orterms))
  8718. (while (string-match re term)
  8719. (setq minus (and (match-end 1)
  8720. (equal (match-string 1 term) "-"))
  8721. kwd (match-string 2 term)
  8722. re-p (equal (string-to-char kwd) ?{)
  8723. term (substring term (match-end 0))
  8724. mm (if re-p
  8725. `(string-match ,(substring kwd 1 -1) todo)
  8726. (list 'equal 'todo kwd))
  8727. mm (if minus (list 'not mm) mm))
  8728. (push mm todomatcher))
  8729. (push (if (> (length todomatcher) 1)
  8730. (cons 'and todomatcher)
  8731. (car todomatcher))
  8732. orlist)
  8733. (setq todomatcher nil))
  8734. (setq todomatcher (if (> (length orlist) 1)
  8735. (cons 'or orlist) (car orlist))))
  8736. ;; Return the string and lisp forms of the matcher
  8737. (setq matcher (if todomatcher
  8738. (list 'and tagsmatcher todomatcher)
  8739. tagsmatcher))
  8740. (cons match0 matcher)))
  8741. (defun org-op-to-function (op &optional stringp)
  8742. "Turn an operator into the appropriate function."
  8743. (setq op
  8744. (cond
  8745. ((equal op "<" ) '(< string< org-time<))
  8746. ((equal op ">" ) '(> org-string> org-time>))
  8747. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8748. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8749. ((member op '("=" "==")) '(= string= org-time=))
  8750. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8751. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8752. (defun org<> (a b) (not (= a b)))
  8753. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8754. (defun org-string>= (a b) (not (string< a b)))
  8755. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8756. (defun org-string<> (a b) (not (string= a b)))
  8757. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8758. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8759. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8760. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8761. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8762. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8763. (defun org-2ft (s)
  8764. "Convert S to a floating point time.
  8765. If S is already a number, just return it. If it is a string, parse
  8766. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8767. (cond
  8768. ((numberp s) s)
  8769. ((stringp s)
  8770. (condition-case nil
  8771. (float-time (apply 'encode-time (org-parse-time-string s)))
  8772. (error 0.)))
  8773. (t 0.)))
  8774. (defun org-time-today ()
  8775. "Time in seconds today at 0:00.
  8776. Returns the float number of seconds since the beginning of the
  8777. epoch to the beginning of today (00:00)."
  8778. (float-time (apply 'encode-time
  8779. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8780. (defun org-matcher-time (s)
  8781. "Interpret a time comparison value."
  8782. (save-match-data
  8783. (cond
  8784. ((string= s "<now>") (float-time))
  8785. ((string= s "<today>") (org-time-today))
  8786. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8787. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8788. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8789. (+ (org-time-today)
  8790. (* (string-to-number (match-string 1 s))
  8791. (cdr (assoc (match-string 2 s)
  8792. '(("d" . 86400.0) ("w" . 604800.0)
  8793. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8794. (t (org-2ft s)))))
  8795. (defun org-match-any-p (re list)
  8796. "Does re match any element of list?"
  8797. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8798. (delq nil list))
  8799. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8800. (defvar org-tags-overlay (org-make-overlay 1 1))
  8801. (org-detach-overlay org-tags-overlay)
  8802. (defun org-get-local-tags-at (&optional pos)
  8803. "Get a list of tags defined in the current headline."
  8804. (org-get-tags-at pos 'local))
  8805. (defun org-get-local-tags ()
  8806. "Get a list of tags defined in the current headline."
  8807. (org-get-tags-at nil 'local))
  8808. (defun org-get-tags-at (&optional pos local)
  8809. "Get a list of all headline tags applicable at POS.
  8810. POS defaults to point. If tags are inherited, the list contains
  8811. the targets in the same sequence as the headlines appear, i.e.
  8812. the tags of the current headline come last.
  8813. When LOCAL is non-nil, only return tags from the current headline,
  8814. ignore inherited ones."
  8815. (interactive)
  8816. (let (tags ltags lastpos parent)
  8817. (save-excursion
  8818. (save-restriction
  8819. (widen)
  8820. (goto-char (or pos (point)))
  8821. (save-match-data
  8822. (catch 'done
  8823. (condition-case nil
  8824. (progn
  8825. (org-back-to-heading t)
  8826. (while (not (equal lastpos (point)))
  8827. (setq lastpos (point))
  8828. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8829. (setq ltags (org-split-string
  8830. (org-match-string-no-properties 1) ":"))
  8831. (when parent
  8832. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8833. (setq tags (append
  8834. (if parent
  8835. (org-remove-uniherited-tags ltags)
  8836. ltags)
  8837. tags)))
  8838. (or org-use-tag-inheritance (throw 'done t))
  8839. (if local (throw 'done t))
  8840. (org-up-heading-all 1)
  8841. (setq parent t)))
  8842. (error nil)))))
  8843. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8844. (defun org-add-prop-inherited (s)
  8845. (add-text-properties 0 (length s) '(inherited t) s)
  8846. s)
  8847. (defun org-toggle-tag (tag &optional onoff)
  8848. "Toggle the tag TAG for the current line.
  8849. If ONOFF is `on' or `off', don't toggle but set to this state."
  8850. (let (res current)
  8851. (save-excursion
  8852. (org-back-to-heading t)
  8853. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8854. (point-at-eol) t)
  8855. (progn
  8856. (setq current (match-string 1))
  8857. (replace-match ""))
  8858. (setq current ""))
  8859. (setq current (nreverse (org-split-string current ":")))
  8860. (cond
  8861. ((eq onoff 'on)
  8862. (setq res t)
  8863. (or (member tag current) (push tag current)))
  8864. ((eq onoff 'off)
  8865. (or (not (member tag current)) (setq current (delete tag current))))
  8866. (t (if (member tag current)
  8867. (setq current (delete tag current))
  8868. (setq res t)
  8869. (push tag current))))
  8870. (end-of-line 1)
  8871. (if current
  8872. (progn
  8873. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8874. (org-set-tags nil t))
  8875. (delete-horizontal-space))
  8876. (run-hooks 'org-after-tags-change-hook))
  8877. res))
  8878. (defun org-align-tags-here (to-col)
  8879. ;; Assumes that this is a headline
  8880. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8881. (beginning-of-line 1)
  8882. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8883. (< pos (match-beginning 2)))
  8884. (progn
  8885. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8886. (goto-char (match-beginning 1))
  8887. (insert " ")
  8888. (delete-region (point) (1+ (match-beginning 2)))
  8889. (setq ncol (max (1+ (current-column))
  8890. (1+ col)
  8891. (if (> to-col 0)
  8892. to-col
  8893. (- (abs to-col) tags-l))))
  8894. (setq p (point))
  8895. (insert (make-string (- ncol (current-column)) ?\ ))
  8896. (setq ncol (current-column))
  8897. (when indent-tabs-mode (tabify p (point-at-eol)))
  8898. (org-move-to-column (min ncol col) t))
  8899. (goto-char pos))))
  8900. (defun org-set-tags-command (&optional arg just-align)
  8901. "Call the set-tags command for the current entry."
  8902. (interactive "P")
  8903. (if (org-on-heading-p)
  8904. (org-set-tags arg just-align)
  8905. (save-excursion
  8906. (org-back-to-heading t)
  8907. (org-set-tags arg just-align))))
  8908. (defun org-set-tags (&optional arg just-align)
  8909. "Set the tags for the current headline.
  8910. With prefix ARG, realign all tags in headings in the current buffer."
  8911. (interactive "P")
  8912. (let* ((re (concat "^" outline-regexp))
  8913. (current (org-get-tags-string))
  8914. (col (current-column))
  8915. (org-setting-tags t)
  8916. table current-tags inherited-tags ; computed below when needed
  8917. tags p0 c0 c1 rpl)
  8918. (if arg
  8919. (save-excursion
  8920. (goto-char (point-min))
  8921. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8922. (while (re-search-forward re nil t)
  8923. (org-set-tags nil t)
  8924. (end-of-line 1)))
  8925. (message "All tags realigned to column %d" org-tags-column))
  8926. (if just-align
  8927. (setq tags current)
  8928. ;; Get a new set of tags from the user
  8929. (save-excursion
  8930. (setq table (or org-tag-alist (org-get-buffer-tags))
  8931. org-last-tags-completion-table table
  8932. current-tags (org-split-string current ":")
  8933. inherited-tags (nreverse
  8934. (nthcdr (length current-tags)
  8935. (nreverse (org-get-tags-at))))
  8936. tags
  8937. (if (or (eq t org-use-fast-tag-selection)
  8938. (and org-use-fast-tag-selection
  8939. (delq nil (mapcar 'cdr table))))
  8940. (org-fast-tag-selection
  8941. current-tags inherited-tags table
  8942. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8943. (let ((org-add-colon-after-tag-completion t))
  8944. (org-trim
  8945. (org-without-partial-completion
  8946. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8947. nil nil current 'org-tags-history)))))))
  8948. (while (string-match "[-+&]+" tags)
  8949. ;; No boolean logic, just a list
  8950. (setq tags (replace-match ":" t t tags))))
  8951. (if (string-match "\\`[\t ]*\\'" tags)
  8952. (setq tags "")
  8953. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8954. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8955. ;; Insert new tags at the correct column
  8956. (beginning-of-line 1)
  8957. (cond
  8958. ((and (equal current "") (equal tags "")))
  8959. ((re-search-forward
  8960. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8961. (point-at-eol) t)
  8962. (if (equal tags "")
  8963. (setq rpl "")
  8964. (goto-char (match-beginning 0))
  8965. (setq c0 (current-column) p0 (point)
  8966. c1 (max (1+ c0) (if (> org-tags-column 0)
  8967. org-tags-column
  8968. (- (- org-tags-column) (length tags))))
  8969. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8970. (replace-match rpl t t)
  8971. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8972. tags)
  8973. (t (error "Tags alignment failed")))
  8974. (org-move-to-column col)
  8975. (unless just-align
  8976. (run-hooks 'org-after-tags-change-hook)))))
  8977. (defun org-change-tag-in-region (beg end tag off)
  8978. "Add or remove TAG for each entry in the region.
  8979. This works in the agenda, and also in an org-mode buffer."
  8980. (interactive
  8981. (list (region-beginning) (region-end)
  8982. (let ((org-last-tags-completion-table
  8983. (if (org-mode-p)
  8984. (org-get-buffer-tags)
  8985. (org-global-tags-completion-table))))
  8986. (org-ido-completing-read
  8987. "Tag: " 'org-tags-completion-function nil nil nil
  8988. 'org-tags-history))
  8989. (progn
  8990. (message "[s]et or [r]emove? ")
  8991. (equal (read-char-exclusive) ?r))))
  8992. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8993. (let ((agendap (equal major-mode 'org-agenda-mode))
  8994. l1 l2 m buf pos newhead (cnt 0))
  8995. (goto-char end)
  8996. (setq l2 (1- (org-current-line)))
  8997. (goto-char beg)
  8998. (setq l1 (org-current-line))
  8999. (loop for l from l1 to l2 do
  9000. (goto-line l)
  9001. (setq m (get-text-property (point) 'org-hd-marker))
  9002. (when (or (and (org-mode-p) (org-on-heading-p))
  9003. (and agendap m))
  9004. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9005. pos (if agendap m (point)))
  9006. (with-current-buffer buf
  9007. (save-excursion
  9008. (save-restriction
  9009. (goto-char pos)
  9010. (setq cnt (1+ cnt))
  9011. (org-toggle-tag tag (if off 'off 'on))
  9012. (setq newhead (org-get-heading)))))
  9013. (and agendap (org-agenda-change-all-lines newhead m))))
  9014. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9015. (defun org-tags-completion-function (string predicate &optional flag)
  9016. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9017. (confirm (lambda (x) (stringp (car x)))))
  9018. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9019. (setq s1 (match-string 1 string)
  9020. s2 (match-string 2 string))
  9021. (setq s1 "" s2 string))
  9022. (cond
  9023. ((eq flag nil)
  9024. ;; try completion
  9025. (setq rtn (try-completion s2 ctable confirm))
  9026. (if (stringp rtn)
  9027. (setq rtn
  9028. (concat s1 s2 (substring rtn (length s2))
  9029. (if (and org-add-colon-after-tag-completion
  9030. (assoc rtn ctable))
  9031. ":" ""))))
  9032. rtn)
  9033. ((eq flag t)
  9034. ;; all-completions
  9035. (all-completions s2 ctable confirm)
  9036. )
  9037. ((eq flag 'lambda)
  9038. ;; exact match?
  9039. (assoc s2 ctable)))
  9040. ))
  9041. (defun org-fast-tag-insert (kwd tags face &optional end)
  9042. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9043. (insert (format "%-12s" (concat kwd ":"))
  9044. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9045. (or end "")))
  9046. (defun org-fast-tag-show-exit (flag)
  9047. (save-excursion
  9048. (goto-line 3)
  9049. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9050. (replace-match ""))
  9051. (when flag
  9052. (end-of-line 1)
  9053. (org-move-to-column (- (window-width) 19) t)
  9054. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9055. (defun org-set-current-tags-overlay (current prefix)
  9056. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9057. (if (featurep 'xemacs)
  9058. (org-overlay-display org-tags-overlay (concat prefix s)
  9059. 'secondary-selection)
  9060. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9061. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9062. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9063. "Fast tag selection with single keys.
  9064. CURRENT is the current list of tags in the headline, INHERITED is the
  9065. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9066. possibly with grouping information. TODO-TABLE is a similar table with
  9067. TODO keywords, should these have keys assigned to them.
  9068. If the keys are nil, a-z are automatically assigned.
  9069. Returns the new tags string, or nil to not change the current settings."
  9070. (let* ((fulltable (append table todo-table))
  9071. (maxlen (apply 'max (mapcar
  9072. (lambda (x)
  9073. (if (stringp (car x)) (string-width (car x)) 0))
  9074. fulltable)))
  9075. (buf (current-buffer))
  9076. (expert (eq org-fast-tag-selection-single-key 'expert))
  9077. (buffer-tags nil)
  9078. (fwidth (+ maxlen 3 1 3))
  9079. (ncol (/ (- (window-width) 4) fwidth))
  9080. (i-face 'org-done)
  9081. (c-face 'org-todo)
  9082. tg cnt e c char c1 c2 ntable tbl rtn
  9083. ov-start ov-end ov-prefix
  9084. (exit-after-next org-fast-tag-selection-single-key)
  9085. (done-keywords org-done-keywords)
  9086. groups ingroup)
  9087. (save-excursion
  9088. (beginning-of-line 1)
  9089. (if (looking-at
  9090. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9091. (setq ov-start (match-beginning 1)
  9092. ov-end (match-end 1)
  9093. ov-prefix "")
  9094. (setq ov-start (1- (point-at-eol))
  9095. ov-end (1+ ov-start))
  9096. (skip-chars-forward "^\n\r")
  9097. (setq ov-prefix
  9098. (concat
  9099. (buffer-substring (1- (point)) (point))
  9100. (if (> (current-column) org-tags-column)
  9101. " "
  9102. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9103. (org-move-overlay org-tags-overlay ov-start ov-end)
  9104. (save-window-excursion
  9105. (if expert
  9106. (set-buffer (get-buffer-create " *Org tags*"))
  9107. (delete-other-windows)
  9108. (split-window-vertically)
  9109. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9110. (erase-buffer)
  9111. (org-set-local 'org-done-keywords done-keywords)
  9112. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9113. (org-fast-tag-insert "Current" current c-face "\n\n")
  9114. (org-fast-tag-show-exit exit-after-next)
  9115. (org-set-current-tags-overlay current ov-prefix)
  9116. (setq tbl fulltable char ?a cnt 0)
  9117. (while (setq e (pop tbl))
  9118. (cond
  9119. ((equal e '(:startgroup))
  9120. (push '() groups) (setq ingroup t)
  9121. (when (not (= cnt 0))
  9122. (setq cnt 0)
  9123. (insert "\n"))
  9124. (insert "{ "))
  9125. ((equal e '(:endgroup))
  9126. (setq ingroup nil cnt 0)
  9127. (insert "}\n"))
  9128. (t
  9129. (setq tg (car e) c2 nil)
  9130. (if (cdr e)
  9131. (setq c (cdr e))
  9132. ;; automatically assign a character.
  9133. (setq c1 (string-to-char
  9134. (downcase (substring
  9135. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9136. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9137. (while (or (rassoc char ntable) (rassoc char table))
  9138. (setq char (1+ char)))
  9139. (setq c2 c1))
  9140. (setq c (or c2 char)))
  9141. (if ingroup (push tg (car groups)))
  9142. (setq tg (org-add-props tg nil 'face
  9143. (cond
  9144. ((not (assoc tg table))
  9145. (org-get-todo-face tg))
  9146. ((member tg current) c-face)
  9147. ((member tg inherited) i-face)
  9148. (t nil))))
  9149. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9150. (insert "[" c "] " tg (make-string
  9151. (- fwidth 4 (length tg)) ?\ ))
  9152. (push (cons tg c) ntable)
  9153. (when (= (setq cnt (1+ cnt)) ncol)
  9154. (insert "\n")
  9155. (if ingroup (insert " "))
  9156. (setq cnt 0)))))
  9157. (setq ntable (nreverse ntable))
  9158. (insert "\n")
  9159. (goto-char (point-min))
  9160. (if (not expert) (org-fit-window-to-buffer))
  9161. (setq rtn
  9162. (catch 'exit
  9163. (while t
  9164. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9165. (if groups " [!] no groups" " [!]groups")
  9166. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9167. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9168. (cond
  9169. ((= c ?\r) (throw 'exit t))
  9170. ((= c ?!)
  9171. (setq groups (not groups))
  9172. (goto-char (point-min))
  9173. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9174. ((= c ?\C-c)
  9175. (if (not expert)
  9176. (org-fast-tag-show-exit
  9177. (setq exit-after-next (not exit-after-next)))
  9178. (setq expert nil)
  9179. (delete-other-windows)
  9180. (split-window-vertically)
  9181. (org-switch-to-buffer-other-window " *Org tags*")
  9182. (org-fit-window-to-buffer)))
  9183. ((or (= c ?\C-g)
  9184. (and (= c ?q) (not (rassoc c ntable))))
  9185. (org-detach-overlay org-tags-overlay)
  9186. (setq quit-flag t))
  9187. ((= c ?\ )
  9188. (setq current nil)
  9189. (if exit-after-next (setq exit-after-next 'now)))
  9190. ((= c ?\t)
  9191. (condition-case nil
  9192. (setq tg (org-ido-completing-read
  9193. "Tag: "
  9194. (or buffer-tags
  9195. (with-current-buffer buf
  9196. (org-get-buffer-tags)))))
  9197. (quit (setq tg "")))
  9198. (when (string-match "\\S-" tg)
  9199. (add-to-list 'buffer-tags (list tg))
  9200. (if (member tg current)
  9201. (setq current (delete tg current))
  9202. (push tg current)))
  9203. (if exit-after-next (setq exit-after-next 'now)))
  9204. ((setq e (rassoc c todo-table) tg (car e))
  9205. (with-current-buffer buf
  9206. (save-excursion (org-todo tg)))
  9207. (if exit-after-next (setq exit-after-next 'now)))
  9208. ((setq e (rassoc c ntable) tg (car e))
  9209. (if (member tg current)
  9210. (setq current (delete tg current))
  9211. (loop for g in groups do
  9212. (if (member tg g)
  9213. (mapc (lambda (x)
  9214. (setq current (delete x current)))
  9215. g)))
  9216. (push tg current))
  9217. (if exit-after-next (setq exit-after-next 'now))))
  9218. ;; Create a sorted list
  9219. (setq current
  9220. (sort current
  9221. (lambda (a b)
  9222. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9223. (if (eq exit-after-next 'now) (throw 'exit t))
  9224. (goto-char (point-min))
  9225. (beginning-of-line 2)
  9226. (delete-region (point) (point-at-eol))
  9227. (org-fast-tag-insert "Current" current c-face)
  9228. (org-set-current-tags-overlay current ov-prefix)
  9229. (while (re-search-forward
  9230. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9231. (setq tg (match-string 1))
  9232. (add-text-properties
  9233. (match-beginning 1) (match-end 1)
  9234. (list 'face
  9235. (cond
  9236. ((member tg current) c-face)
  9237. ((member tg inherited) i-face)
  9238. (t (get-text-property (match-beginning 1) 'face))))))
  9239. (goto-char (point-min)))))
  9240. (org-detach-overlay org-tags-overlay)
  9241. (if rtn
  9242. (mapconcat 'identity current ":")
  9243. nil))))
  9244. (defun org-get-tags-string ()
  9245. "Get the TAGS string in the current headline."
  9246. (unless (org-on-heading-p t)
  9247. (error "Not on a heading"))
  9248. (save-excursion
  9249. (beginning-of-line 1)
  9250. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9251. (org-match-string-no-properties 1)
  9252. "")))
  9253. (defun org-get-tags ()
  9254. "Get the list of tags specified in the current headline."
  9255. (org-split-string (org-get-tags-string) ":"))
  9256. (defun org-get-buffer-tags ()
  9257. "Get a table of all tags used in the buffer, for completion."
  9258. (let (tags)
  9259. (save-excursion
  9260. (goto-char (point-min))
  9261. (while (re-search-forward
  9262. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9263. (when (equal (char-after (point-at-bol 0)) ?*)
  9264. (mapc (lambda (x) (add-to-list 'tags x))
  9265. (org-split-string (org-match-string-no-properties 1) ":")))))
  9266. (mapcar 'list tags)))
  9267. ;;;; The mapping API
  9268. ;;;###autoload
  9269. (defun org-map-entries (func &optional match scope &rest skip)
  9270. "Call FUNC at each headline selected by MATCH in SCOPE.
  9271. FUNC is a function or a lisp form. The function will be called without
  9272. arguments, with the cursor positioned at the beginning of the headline.
  9273. The return values of all calls to the function will be collected and
  9274. returned as a list.
  9275. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9276. Only headlines that are matched by this query will be considered during
  9277. the iteration. When MATCH is nil or t, all headlines will be
  9278. visited by the iteration.
  9279. SCOPE determines the scope of this command. It can be any of:
  9280. nil The current buffer, respecting the restriction if any
  9281. tree The subtree started with the entry at point
  9282. file The current buffer, without restriction
  9283. file-with-archives
  9284. The current buffer, and any archives associated with it
  9285. agenda All agenda files
  9286. agenda-with-archives
  9287. All agenda files with any archive files associated with them
  9288. \(file1 file2 ...)
  9289. If this is a list, all files in the list will be scanned
  9290. The remaining args are treated as settings for the skipping facilities of
  9291. the scanner. The following items can be given here:
  9292. archive skip trees with the archive tag.
  9293. comment skip trees with the COMMENT keyword
  9294. function or Emacs Lisp form:
  9295. will be used as value for `org-agenda-skip-function', so whenever
  9296. the the function returns t, FUNC will not be called for that
  9297. entry and search will continue from the point where the
  9298. function leaves it."
  9299. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9300. (org-agenda-skip-archived-trees (memq 'archive skip))
  9301. (org-agenda-skip-comment-trees (memq 'comment skip))
  9302. (org-agenda-skip-function
  9303. (car (org-delete-all '(comment archive) skip)))
  9304. (org-tags-match-list-sublevels t)
  9305. matcher file res
  9306. org-todo-keywords-for-agenda
  9307. org-done-keywords-for-agenda
  9308. org-todo-keyword-alist-for-agenda
  9309. org-tag-alist-for-agenda)
  9310. (cond
  9311. ((eq match t) (setq matcher t))
  9312. ((eq match nil) (setq matcher t))
  9313. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9314. (save-excursion
  9315. (save-restriction
  9316. (when (eq scope 'tree)
  9317. (org-back-to-heading t)
  9318. (org-narrow-to-subtree)
  9319. (setq scope nil))
  9320. (if (not scope)
  9321. (progn
  9322. (org-prepare-agenda-buffers
  9323. (list (buffer-file-name (current-buffer))))
  9324. (setq res (org-scan-tags func matcher)))
  9325. ;; Get the right scope
  9326. (cond
  9327. ((and scope (listp scope) (symbolp (car scope)))
  9328. (setq scope (eval scope)))
  9329. ((eq scope 'agenda)
  9330. (setq scope (org-agenda-files t)))
  9331. ((eq scope 'agenda-with-archives)
  9332. (setq scope (org-agenda-files t))
  9333. (setq scope (org-add-archive-files scope)))
  9334. ((eq scope 'file)
  9335. (setq scope (list (buffer-file-name))))
  9336. ((eq scope 'file-with-archives)
  9337. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9338. (org-prepare-agenda-buffers scope)
  9339. (while (setq file (pop scope))
  9340. (with-current-buffer (org-find-base-buffer-visiting file)
  9341. (save-excursion
  9342. (save-restriction
  9343. (widen)
  9344. (goto-char (point-min))
  9345. (setq res (append res (org-scan-tags func matcher))))))))))
  9346. res))
  9347. ;;;; Properties
  9348. ;;; Setting and retrieving properties
  9349. (defconst org-special-properties
  9350. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9351. "TIMESTAMP" "TIMESTAMP_IA")
  9352. "The special properties valid in Org-mode.
  9353. These are properties that are not defined in the property drawer,
  9354. but in some other way.")
  9355. (defconst org-default-properties
  9356. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9357. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9358. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9359. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9360. "Some properties that are used by Org-mode for various purposes.
  9361. Being in this list makes sure that they are offered for completion.")
  9362. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9363. "Regular expression matching the first line of a property drawer.")
  9364. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9365. "Regular expression matching the first line of a property drawer.")
  9366. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9367. "Regular expression matching the first line of a property drawer.")
  9368. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9369. "Regular expression matching the first line of a property drawer.")
  9370. (defconst org-property-drawer-re
  9371. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9372. org-property-end-re "\\)\n?")
  9373. "Matches an entire property drawer.")
  9374. (defconst org-clock-drawer-re
  9375. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9376. org-property-end-re "\\)\n?")
  9377. "Matches an entire clock drawer.")
  9378. (defun org-property-action ()
  9379. "Do an action on properties."
  9380. (interactive)
  9381. (let (c)
  9382. (org-at-property-p)
  9383. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9384. (setq c (read-char-exclusive))
  9385. (cond
  9386. ((equal c ?s)
  9387. (call-interactively 'org-set-property))
  9388. ((equal c ?d)
  9389. (call-interactively 'org-delete-property))
  9390. ((equal c ?D)
  9391. (call-interactively 'org-delete-property-globally))
  9392. ((equal c ?c)
  9393. (call-interactively 'org-compute-property-at-point))
  9394. (t (error "No such property action %c" c)))))
  9395. (defun org-at-property-p ()
  9396. "Is the cursor in a property line?"
  9397. ;; FIXME: Does not check if we are actually in the drawer.
  9398. ;; FIXME: also returns true on any drawers.....
  9399. ;; This is used by C-c C-c for property action.
  9400. (save-excursion
  9401. (beginning-of-line 1)
  9402. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9403. (defun org-get-property-block (&optional beg end force)
  9404. "Return the (beg . end) range of the body of the property drawer.
  9405. BEG and END can be beginning and end of subtree, if not given
  9406. they will be found.
  9407. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9408. (catch 'exit
  9409. (save-excursion
  9410. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9411. (end (or end (progn (outline-next-heading) (point)))))
  9412. (goto-char beg)
  9413. (if (re-search-forward org-property-start-re end t)
  9414. (setq beg (1+ (match-end 0)))
  9415. (if force
  9416. (save-excursion
  9417. (org-insert-property-drawer)
  9418. (setq end (progn (outline-next-heading) (point))))
  9419. (throw 'exit nil))
  9420. (goto-char beg)
  9421. (if (re-search-forward org-property-start-re end t)
  9422. (setq beg (1+ (match-end 0)))))
  9423. (if (re-search-forward org-property-end-re end t)
  9424. (setq end (match-beginning 0))
  9425. (or force (throw 'exit nil))
  9426. (goto-char beg)
  9427. (setq end beg)
  9428. (org-indent-line-function)
  9429. (insert ":END:\n"))
  9430. (cons beg end)))))
  9431. (defun org-entry-properties (&optional pom which)
  9432. "Get all properties of the entry at point-or-marker POM.
  9433. This includes the TODO keyword, the tags, time strings for deadline,
  9434. scheduled, and clocking, and any additional properties defined in the
  9435. entry. The return value is an alist, keys may occur multiple times
  9436. if the property key was used several times.
  9437. POM may also be nil, in which case the current entry is used.
  9438. If WHICH is nil or `all', get all properties. If WHICH is
  9439. `special' or `standard', only get that subclass."
  9440. (setq which (or which 'all))
  9441. (org-with-point-at pom
  9442. (let ((clockstr (substring org-clock-string 0 -1))
  9443. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9444. beg end range props sum-props key value string clocksum)
  9445. (save-excursion
  9446. (when (condition-case nil
  9447. (and (org-mode-p) (org-back-to-heading t))
  9448. (error nil))
  9449. (setq beg (point))
  9450. (setq sum-props (get-text-property (point) 'org-summaries))
  9451. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9452. (outline-next-heading)
  9453. (setq end (point))
  9454. (when (memq which '(all special))
  9455. ;; Get the special properties, like TODO and tags
  9456. (goto-char beg)
  9457. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9458. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9459. (when (looking-at org-priority-regexp)
  9460. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9461. (when (and (setq value (org-get-tags-string))
  9462. (string-match "\\S-" value))
  9463. (push (cons "TAGS" value) props))
  9464. (when (setq value (org-get-tags-at))
  9465. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9466. props))
  9467. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9468. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9469. string (if (equal key clockstr)
  9470. (org-no-properties
  9471. (org-trim
  9472. (buffer-substring
  9473. (match-beginning 3) (goto-char (point-at-eol)))))
  9474. (substring (org-match-string-no-properties 3) 1 -1)))
  9475. (unless key
  9476. (if (= (char-after (match-beginning 3)) ?\[)
  9477. (setq key "TIMESTAMP_IA")
  9478. (setq key "TIMESTAMP")))
  9479. (when (or (equal key clockstr) (not (assoc key props)))
  9480. (push (cons key string) props)))
  9481. )
  9482. (when (memq which '(all standard))
  9483. ;; Get the standard properties, like :PORP: ...
  9484. (setq range (org-get-property-block beg end))
  9485. (when range
  9486. (goto-char (car range))
  9487. (while (re-search-forward
  9488. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9489. (cdr range) t)
  9490. (setq key (org-match-string-no-properties 1)
  9491. value (org-trim (or (org-match-string-no-properties 2) "")))
  9492. (unless (member key excluded)
  9493. (push (cons key (or value "")) props)))))
  9494. (if clocksum
  9495. (push (cons "CLOCKSUM"
  9496. (org-columns-number-to-string (/ (float clocksum) 60.)
  9497. 'add_times))
  9498. props))
  9499. (unless (assoc "CATEGORY" props)
  9500. (setq value (or (org-get-category)
  9501. (progn (org-refresh-category-properties)
  9502. (org-get-category))))
  9503. (push (cons "CATEGORY" value) props))
  9504. (append sum-props (nreverse props)))))))
  9505. (defun org-entry-get (pom property &optional inherit)
  9506. "Get value of PROPERTY for entry at point-or-marker POM.
  9507. If INHERIT is non-nil and the entry does not have the property,
  9508. then also check higher levels of the hierarchy.
  9509. If INHERIT is the symbol `selective', use inheritance only if the setting
  9510. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9511. If the property is present but empty, the return value is the empty string.
  9512. If the property is not present at all, nil is returned."
  9513. (org-with-point-at pom
  9514. (if (and inherit (if (eq inherit 'selective)
  9515. (org-property-inherit-p property)
  9516. t))
  9517. (org-entry-get-with-inheritance property)
  9518. (if (member property org-special-properties)
  9519. ;; We need a special property. Use brute force, get all properties.
  9520. (cdr (assoc property (org-entry-properties nil 'special)))
  9521. (let ((range (org-get-property-block)))
  9522. (if (and range
  9523. (goto-char (car range))
  9524. (re-search-forward
  9525. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9526. (cdr range) t))
  9527. ;; Found the property, return it.
  9528. (if (match-end 1)
  9529. (org-match-string-no-properties 1)
  9530. "")))))))
  9531. (defun org-property-or-variable-value (var &optional inherit)
  9532. "Check if there is a property fixing the value of VAR.
  9533. If yes, return this value. If not, return the current value of the variable."
  9534. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9535. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9536. (read prop)
  9537. (symbol-value var))))
  9538. (defun org-entry-delete (pom property)
  9539. "Delete the property PROPERTY from entry at point-or-marker POM."
  9540. (org-with-point-at pom
  9541. (if (member property org-special-properties)
  9542. nil ; cannot delete these properties.
  9543. (let ((range (org-get-property-block)))
  9544. (if (and range
  9545. (goto-char (car range))
  9546. (re-search-forward
  9547. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9548. (cdr range) t))
  9549. (progn
  9550. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9551. t)
  9552. nil)))))
  9553. ;; Multi-values properties are properties that contain multiple values
  9554. ;; These values are assumed to be single words, separated by whitespace.
  9555. (defun org-entry-add-to-multivalued-property (pom property value)
  9556. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9557. (let* ((old (org-entry-get pom property))
  9558. (values (and old (org-split-string old "[ \t]"))))
  9559. (setq value (org-entry-protect-space value))
  9560. (unless (member value values)
  9561. (setq values (cons value values))
  9562. (org-entry-put pom property
  9563. (mapconcat 'identity values " ")))))
  9564. (defun org-entry-remove-from-multivalued-property (pom property value)
  9565. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9566. (let* ((old (org-entry-get pom property))
  9567. (values (and old (org-split-string old "[ \t]"))))
  9568. (setq value (org-entry-protect-space value))
  9569. (when (member value values)
  9570. (setq values (delete value values))
  9571. (org-entry-put pom property
  9572. (mapconcat 'identity values " ")))))
  9573. (defun org-entry-member-in-multivalued-property (pom property value)
  9574. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9575. (let* ((old (org-entry-get pom property))
  9576. (values (and old (org-split-string old "[ \t]"))))
  9577. (setq value (org-entry-protect-space value))
  9578. (member value values)))
  9579. (defun org-entry-get-multivalued-property (pom property)
  9580. "Return a list of values in a multivalued property."
  9581. (let* ((value (org-entry-get pom property))
  9582. (values (and value (org-split-string value "[ \t]"))))
  9583. (mapcar 'org-entry-restore-space values)))
  9584. (defun org-entry-put-multivalued-property (pom property &rest values)
  9585. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9586. VALUES should be a list of strings. Spaces will be protected."
  9587. (org-entry-put pom property
  9588. (mapconcat 'org-entry-protect-space values " "))
  9589. (let* ((value (org-entry-get pom property))
  9590. (values (and value (org-split-string value "[ \t]"))))
  9591. (mapcar 'org-entry-restore-space values)))
  9592. (defun org-entry-protect-space (s)
  9593. "Protect spaces and newline in string S."
  9594. (while (string-match " " s)
  9595. (setq s (replace-match "%20" t t s)))
  9596. (while (string-match "\n" s)
  9597. (setq s (replace-match "%0A" t t s)))
  9598. s)
  9599. (defun org-entry-restore-space (s)
  9600. "Restore spaces and newline in string S."
  9601. (while (string-match "%20" s)
  9602. (setq s (replace-match " " t t s)))
  9603. (while (string-match "%0A" s)
  9604. (setq s (replace-match "\n" t t s)))
  9605. s)
  9606. (defvar org-entry-property-inherited-from (make-marker)
  9607. "Marker pointing to the entry from where a property was inherited.
  9608. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9609. location of the entry where the inheritance search matched. If there was
  9610. no match, the marker will point nowhere.
  9611. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9612. is set.")
  9613. (defun org-entry-get-with-inheritance (property)
  9614. "Get entry property, and search higher levels if not present."
  9615. (move-marker org-entry-property-inherited-from nil)
  9616. (let (tmp)
  9617. (save-excursion
  9618. (save-restriction
  9619. (widen)
  9620. (catch 'ex
  9621. (while t
  9622. (when (setq tmp (org-entry-get nil property))
  9623. (org-back-to-heading t)
  9624. (move-marker org-entry-property-inherited-from (point))
  9625. (throw 'ex tmp))
  9626. (or (org-up-heading-safe) (throw 'ex nil)))))
  9627. (or tmp
  9628. (cdr (assoc property org-file-properties))
  9629. (cdr (assoc property org-global-properties))
  9630. (cdr (assoc property org-global-properties-fixed))))))
  9631. (defun org-entry-put (pom property value)
  9632. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9633. (org-with-point-at pom
  9634. (org-back-to-heading t)
  9635. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9636. range)
  9637. (cond
  9638. ((equal property "TODO")
  9639. (when (and (stringp value) (string-match "\\S-" value)
  9640. (not (member value org-todo-keywords-1)))
  9641. (error "\"%s\" is not a valid TODO state" value))
  9642. (if (or (not value)
  9643. (not (string-match "\\S-" value)))
  9644. (setq value 'none))
  9645. (org-todo value)
  9646. (org-set-tags nil 'align))
  9647. ((equal property "PRIORITY")
  9648. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9649. (string-to-char value) ?\ ))
  9650. (org-set-tags nil 'align))
  9651. ((equal property "SCHEDULED")
  9652. (if (re-search-forward org-scheduled-time-regexp end t)
  9653. (cond
  9654. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9655. ((eq value 'later) (org-timestamp-change 1 'day))
  9656. (t (call-interactively 'org-schedule)))
  9657. (call-interactively 'org-schedule)))
  9658. ((equal property "DEADLINE")
  9659. (if (re-search-forward org-deadline-time-regexp end t)
  9660. (cond
  9661. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9662. ((eq value 'later) (org-timestamp-change 1 'day))
  9663. (t (call-interactively 'org-deadline)))
  9664. (call-interactively 'org-deadline)))
  9665. ((member property org-special-properties)
  9666. (error "The %s property can not yet be set with `org-entry-put'"
  9667. property))
  9668. (t ; a non-special property
  9669. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9670. (setq range (org-get-property-block beg end 'force))
  9671. (goto-char (car range))
  9672. (if (re-search-forward
  9673. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9674. (progn
  9675. (delete-region (match-beginning 1) (match-end 1))
  9676. (goto-char (match-beginning 1)))
  9677. (goto-char (cdr range))
  9678. (insert "\n")
  9679. (backward-char 1)
  9680. (org-indent-line-function)
  9681. (insert ":" property ":"))
  9682. (and value (insert " " value))
  9683. (org-indent-line-function)))))))
  9684. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9685. "Get all property keys in the current buffer.
  9686. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9687. like tags and TODO state.
  9688. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9689. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9690. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9691. formats in the current buffer."
  9692. (let (rtn range cfmt s p)
  9693. (save-excursion
  9694. (save-restriction
  9695. (widen)
  9696. (goto-char (point-min))
  9697. (while (re-search-forward org-property-start-re nil t)
  9698. (setq range (org-get-property-block))
  9699. (goto-char (car range))
  9700. (while (re-search-forward
  9701. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9702. (cdr range) t)
  9703. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9704. (outline-next-heading))))
  9705. (when include-specials
  9706. (setq rtn (append org-special-properties rtn)))
  9707. (when include-defaults
  9708. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9709. (when include-columns
  9710. (save-excursion
  9711. (save-restriction
  9712. (widen)
  9713. (goto-char (point-min))
  9714. (while (re-search-forward
  9715. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9716. nil t)
  9717. (setq cfmt (match-string 2) s 0)
  9718. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9719. cfmt s)
  9720. (setq s (match-end 0)
  9721. p (match-string 1 cfmt))
  9722. (unless (or (equal p "ITEM")
  9723. (member p org-special-properties))
  9724. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9725. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9726. (defun org-property-values (key)
  9727. "Return a list of all values of property KEY."
  9728. (save-excursion
  9729. (save-restriction
  9730. (widen)
  9731. (goto-char (point-min))
  9732. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9733. values)
  9734. (while (re-search-forward re nil t)
  9735. (add-to-list 'values (org-trim (match-string 1))))
  9736. (delete "" values)))))
  9737. (defun org-insert-property-drawer ()
  9738. "Insert a property drawer into the current entry."
  9739. (interactive)
  9740. (org-back-to-heading t)
  9741. (looking-at outline-regexp)
  9742. (let ((indent (- (match-end 0)(match-beginning 0)))
  9743. (beg (point))
  9744. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9745. end hiddenp)
  9746. (outline-next-heading)
  9747. (setq end (point))
  9748. (goto-char beg)
  9749. (while (re-search-forward re end t))
  9750. (setq hiddenp (org-invisible-p))
  9751. (end-of-line 1)
  9752. (and (equal (char-after) ?\n) (forward-char 1))
  9753. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9754. (beginning-of-line 2))
  9755. (org-skip-over-state-notes)
  9756. (skip-chars-backward " \t\n\r")
  9757. (if (eq (char-before) ?*) (forward-char 1))
  9758. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9759. (beginning-of-line 0)
  9760. (org-indent-to-column indent)
  9761. (beginning-of-line 2)
  9762. (org-indent-to-column indent)
  9763. (beginning-of-line 0)
  9764. (if hiddenp
  9765. (save-excursion
  9766. (org-back-to-heading t)
  9767. (hide-entry))
  9768. (org-flag-drawer t))))
  9769. (defun org-set-property (property value)
  9770. "In the current entry, set PROPERTY to VALUE.
  9771. When called interactively, this will prompt for a property name, offering
  9772. completion on existing and default properties. And then it will prompt
  9773. for a value, offering completion either on allowed values (via an inherited
  9774. xxx_ALL property) or on existing values in other instances of this property
  9775. in the current file."
  9776. (interactive
  9777. (let* ((completion-ignore-case t)
  9778. (keys (org-buffer-property-keys nil t t))
  9779. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9780. (prop (if (member prop0 keys)
  9781. prop0
  9782. (or (cdr (assoc (downcase prop0)
  9783. (mapcar (lambda (x) (cons (downcase x) x))
  9784. keys)))
  9785. prop0)))
  9786. (cur (org-entry-get nil prop))
  9787. (allowed (org-property-get-allowed-values nil prop 'table))
  9788. (existing (mapcar 'list (org-property-values prop)))
  9789. (val (if allowed
  9790. (org-completing-read "Value: " allowed nil 'req-match)
  9791. (org-completing-read
  9792. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9793. (concat "[" cur "]") "")
  9794. ": ")
  9795. existing nil nil "" nil cur))))
  9796. (list prop (if (equal val "") cur val))))
  9797. (unless (equal (org-entry-get nil property) value)
  9798. (org-entry-put nil property value)))
  9799. (defun org-delete-property (property)
  9800. "In the current entry, delete PROPERTY."
  9801. (interactive
  9802. (let* ((completion-ignore-case t)
  9803. (prop (org-ido-completing-read
  9804. "Property: " (org-entry-properties nil 'standard))))
  9805. (list prop)))
  9806. (message "Property %s %s" property
  9807. (if (org-entry-delete nil property)
  9808. "deleted"
  9809. "was not present in the entry")))
  9810. (defun org-delete-property-globally (property)
  9811. "Remove PROPERTY globally, from all entries."
  9812. (interactive
  9813. (let* ((completion-ignore-case t)
  9814. (prop (org-ido-completing-read
  9815. "Globally remove property: "
  9816. (mapcar 'list (org-buffer-property-keys)))))
  9817. (list prop)))
  9818. (save-excursion
  9819. (save-restriction
  9820. (widen)
  9821. (goto-char (point-min))
  9822. (let ((cnt 0))
  9823. (while (re-search-forward
  9824. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9825. nil t)
  9826. (setq cnt (1+ cnt))
  9827. (replace-match ""))
  9828. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9829. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9830. (defun org-compute-property-at-point ()
  9831. "Compute the property at point.
  9832. This looks for an enclosing column format, extracts the operator and
  9833. then applies it to the property in the column format's scope."
  9834. (interactive)
  9835. (unless (org-at-property-p)
  9836. (error "Not at a property"))
  9837. (let ((prop (org-match-string-no-properties 2)))
  9838. (org-columns-get-format-and-top-level)
  9839. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9840. (error "No operator defined for property %s" prop))
  9841. (org-columns-compute prop)))
  9842. (defun org-property-get-allowed-values (pom property &optional table)
  9843. "Get allowed values for the property PROPERTY.
  9844. When TABLE is non-nil, return an alist that can directly be used for
  9845. completion."
  9846. (let (vals)
  9847. (cond
  9848. ((equal property "TODO")
  9849. (setq vals (org-with-point-at pom
  9850. (append org-todo-keywords-1 '("")))))
  9851. ((equal property "PRIORITY")
  9852. (let ((n org-lowest-priority))
  9853. (while (>= n org-highest-priority)
  9854. (push (char-to-string n) vals)
  9855. (setq n (1- n)))))
  9856. ((member property org-special-properties))
  9857. (t
  9858. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9859. (when (and vals (string-match "\\S-" vals))
  9860. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9861. (setq vals (mapcar (lambda (x)
  9862. (cond ((stringp x) x)
  9863. ((numberp x) (number-to-string x))
  9864. ((symbolp x) (symbol-name x))
  9865. (t "???")))
  9866. vals)))))
  9867. (if table (mapcar 'list vals) vals)))
  9868. (defun org-property-previous-allowed-value (&optional previous)
  9869. "Switch to the next allowed value for this property."
  9870. (interactive)
  9871. (org-property-next-allowed-value t))
  9872. (defun org-property-next-allowed-value (&optional previous)
  9873. "Switch to the next allowed value for this property."
  9874. (interactive)
  9875. (unless (org-at-property-p)
  9876. (error "Not at a property"))
  9877. (let* ((key (match-string 2))
  9878. (value (match-string 3))
  9879. (allowed (or (org-property-get-allowed-values (point) key)
  9880. (and (member value '("[ ]" "[-]" "[X]"))
  9881. '("[ ]" "[X]"))))
  9882. nval)
  9883. (unless allowed
  9884. (error "Allowed values for this property have not been defined"))
  9885. (if previous (setq allowed (reverse allowed)))
  9886. (if (member value allowed)
  9887. (setq nval (car (cdr (member value allowed)))))
  9888. (setq nval (or nval (car allowed)))
  9889. (if (equal nval value)
  9890. (error "Only one allowed value for this property"))
  9891. (org-at-property-p)
  9892. (replace-match (concat " :" key ": " nval) t t)
  9893. (org-indent-line-function)
  9894. (beginning-of-line 1)
  9895. (skip-chars-forward " \t")))
  9896. (defun org-find-entry-with-id (ident)
  9897. "Locate the entry that contains the ID property with exact value IDENT.
  9898. IDENT can be a string, a symbol or a number, this function will search for
  9899. the string representation of it.
  9900. Return the position where this entry starts, or nil if there is no such entry."
  9901. (interactive "sID: ")
  9902. (let ((id (cond
  9903. ((stringp ident) ident)
  9904. ((symbol-name ident) (symbol-name ident))
  9905. ((numberp ident) (number-to-string ident))
  9906. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9907. (case-fold-search nil))
  9908. (save-excursion
  9909. (save-restriction
  9910. (widen)
  9911. (goto-char (point-min))
  9912. (when (re-search-forward
  9913. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9914. nil t)
  9915. (org-back-to-heading)
  9916. (point))))))
  9917. ;;;; Timestamps
  9918. (defvar org-last-changed-timestamp nil)
  9919. (defvar org-last-inserted-timestamp nil
  9920. "The last time stamp inserted with `org-insert-time-stamp'.")
  9921. (defvar org-time-was-given) ; dynamically scoped parameter
  9922. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9923. (defvar org-ts-what) ; dynamically scoped parameter
  9924. (defun org-time-stamp (arg &optional inactive)
  9925. "Prompt for a date/time and insert a time stamp.
  9926. If the user specifies a time like HH:MM, or if this command is called
  9927. with a prefix argument, the time stamp will contain date and time.
  9928. Otherwise, only the date will be included. All parts of a date not
  9929. specified by the user will be filled in from the current date/time.
  9930. So if you press just return without typing anything, the time stamp
  9931. will represent the current date/time. If there is already a timestamp
  9932. at the cursor, it will be modified."
  9933. (interactive "P")
  9934. (let* ((ts nil)
  9935. (default-time
  9936. ;; Default time is either today, or, when entering a range,
  9937. ;; the range start.
  9938. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9939. (save-excursion
  9940. (re-search-backward
  9941. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9942. (- (point) 20) t)))
  9943. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9944. (current-time)))
  9945. (default-input (and ts (org-get-compact-tod ts)))
  9946. org-time-was-given org-end-time-was-given time)
  9947. (cond
  9948. ((and (org-at-timestamp-p t)
  9949. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9950. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9951. (insert "--")
  9952. (setq time (let ((this-command this-command))
  9953. (org-read-date arg 'totime nil nil
  9954. default-time default-input)))
  9955. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9956. ((org-at-timestamp-p t)
  9957. (setq time (let ((this-command this-command))
  9958. (org-read-date arg 'totime nil nil default-time default-input)))
  9959. (when (org-at-timestamp-p t) ; just to get the match data
  9960. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9961. (replace-match "")
  9962. (setq org-last-changed-timestamp
  9963. (org-insert-time-stamp
  9964. time (or org-time-was-given arg)
  9965. inactive nil nil (list org-end-time-was-given))))
  9966. (message "Timestamp updated"))
  9967. (t
  9968. (setq time (let ((this-command this-command))
  9969. (org-read-date arg 'totime nil nil default-time default-input)))
  9970. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9971. nil nil (list org-end-time-was-given))))))
  9972. ;; FIXME: can we use this for something else, like computing time differences?
  9973. (defun org-get-compact-tod (s)
  9974. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9975. (let* ((t1 (match-string 1 s))
  9976. (h1 (string-to-number (match-string 2 s)))
  9977. (m1 (string-to-number (match-string 3 s)))
  9978. (t2 (and (match-end 4) (match-string 5 s)))
  9979. (h2 (and t2 (string-to-number (match-string 6 s))))
  9980. (m2 (and t2 (string-to-number (match-string 7 s))))
  9981. dh dm)
  9982. (if (not t2)
  9983. t1
  9984. (setq dh (- h2 h1) dm (- m2 m1))
  9985. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9986. (concat t1 "+" (number-to-string dh)
  9987. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9988. (defun org-time-stamp-inactive (&optional arg)
  9989. "Insert an inactive time stamp.
  9990. An inactive time stamp is enclosed in square brackets instead of angle
  9991. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9992. does not link to the calendar and cannot be changed with the S-cursor keys.
  9993. So these are more for recording a certain time/date."
  9994. (interactive "P")
  9995. (org-time-stamp arg 'inactive))
  9996. (defvar org-date-ovl (org-make-overlay 1 1))
  9997. (org-overlay-put org-date-ovl 'face 'org-warning)
  9998. (org-detach-overlay org-date-ovl)
  9999. (defvar org-ans1) ; dynamically scoped parameter
  10000. (defvar org-ans2) ; dynamically scoped parameter
  10001. (defvar org-plain-time-of-day-regexp) ; defined below
  10002. (defvar org-overriding-default-time nil) ; dynamically scoped
  10003. (defvar org-read-date-overlay nil)
  10004. (defvar org-dcst nil) ; dynamically scoped
  10005. (defun org-read-date (&optional with-time to-time from-string prompt
  10006. default-time default-input)
  10007. "Read a date, possibly a time, and make things smooth for the user.
  10008. The prompt will suggest to enter an ISO date, but you can also enter anything
  10009. which will at least partially be understood by `parse-time-string'.
  10010. Unrecognized parts of the date will default to the current day, month, year,
  10011. hour and minute. If this command is called to replace a timestamp at point,
  10012. of to enter the second timestamp of a range, the default time is taken from the
  10013. existing stamp. For example,
  10014. 3-2-5 --> 2003-02-05
  10015. feb 15 --> currentyear-02-15
  10016. sep 12 9 --> 2009-09-12
  10017. 12:45 --> today 12:45
  10018. 22 sept 0:34 --> currentyear-09-22 0:34
  10019. 12 --> currentyear-currentmonth-12
  10020. Fri --> nearest Friday (today or later)
  10021. etc.
  10022. Furthermore you can specify a relative date by giving, as the *first* thing
  10023. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10024. change in days weeks, months, years.
  10025. With a single plus or minus, the date is relative to today. With a double
  10026. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10027. +4d --> four days from today
  10028. +4 --> same as above
  10029. +2w --> two weeks from today
  10030. ++5 --> five days from default date
  10031. The function understands only English month and weekday abbreviations,
  10032. but this can be configured with the variables `parse-time-months' and
  10033. `parse-time-weekdays'.
  10034. While prompting, a calendar is popped up - you can also select the
  10035. date with the mouse (button 1). The calendar shows a period of three
  10036. months. To scroll it to other months, use the keys `>' and `<'.
  10037. If you don't like the calendar, turn it off with
  10038. \(setq org-read-date-popup-calendar nil)
  10039. With optional argument TO-TIME, the date will immediately be converted
  10040. to an internal time.
  10041. With an optional argument WITH-TIME, the prompt will suggest to also
  10042. insert a time. Note that when WITH-TIME is not set, you can still
  10043. enter a time, and this function will inform the calling routine about
  10044. this change. The calling routine may then choose to change the format
  10045. used to insert the time stamp into the buffer to include the time.
  10046. With optional argument FROM-STRING, read from this string instead from
  10047. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10048. the time/date that is used for everything that is not specified by the
  10049. user."
  10050. (require 'parse-time)
  10051. (let* ((org-time-stamp-rounding-minutes
  10052. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10053. (org-dcst org-display-custom-times)
  10054. (ct (org-current-time))
  10055. (def (or org-overriding-default-time default-time ct))
  10056. (defdecode (decode-time def))
  10057. (dummy (progn
  10058. (when (< (nth 2 defdecode) org-extend-today-until)
  10059. (setcar (nthcdr 2 defdecode) -1)
  10060. (setcar (nthcdr 1 defdecode) 59)
  10061. (setq def (apply 'encode-time defdecode)
  10062. defdecode (decode-time def)))))
  10063. (calendar-move-hook nil)
  10064. (calendar-view-diary-initially-flag nil)
  10065. (view-diary-entries-initially nil)
  10066. (calendar-view-holidays-initially-flag nil)
  10067. (view-calendar-holidays-initially nil)
  10068. (timestr (format-time-string
  10069. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10070. (prompt (concat (if prompt (concat prompt " ") "")
  10071. (format "Date+time [%s]: " timestr)))
  10072. ans (org-ans0 "") org-ans1 org-ans2 final)
  10073. (cond
  10074. (from-string (setq ans from-string))
  10075. (org-read-date-popup-calendar
  10076. (save-excursion
  10077. (save-window-excursion
  10078. (calendar)
  10079. (calendar-forward-day (- (time-to-days def)
  10080. (calendar-absolute-from-gregorian
  10081. (calendar-current-date))))
  10082. (org-eval-in-calendar nil t)
  10083. (let* ((old-map (current-local-map))
  10084. (map (copy-keymap calendar-mode-map))
  10085. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10086. (org-defkey map (kbd "RET") 'org-calendar-select)
  10087. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10088. 'org-calendar-select-mouse)
  10089. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10090. 'org-calendar-select-mouse)
  10091. (org-defkey minibuffer-local-map [(meta shift left)]
  10092. (lambda () (interactive)
  10093. (org-eval-in-calendar '(calendar-backward-month 1))))
  10094. (org-defkey minibuffer-local-map [(meta shift right)]
  10095. (lambda () (interactive)
  10096. (org-eval-in-calendar '(calendar-forward-month 1))))
  10097. (org-defkey minibuffer-local-map [(meta shift up)]
  10098. (lambda () (interactive)
  10099. (org-eval-in-calendar '(calendar-backward-year 1))))
  10100. (org-defkey minibuffer-local-map [(meta shift down)]
  10101. (lambda () (interactive)
  10102. (org-eval-in-calendar '(calendar-forward-year 1))))
  10103. (org-defkey minibuffer-local-map [(shift up)]
  10104. (lambda () (interactive)
  10105. (org-eval-in-calendar '(calendar-backward-week 1))))
  10106. (org-defkey minibuffer-local-map [(shift down)]
  10107. (lambda () (interactive)
  10108. (org-eval-in-calendar '(calendar-forward-week 1))))
  10109. (org-defkey minibuffer-local-map [(shift left)]
  10110. (lambda () (interactive)
  10111. (org-eval-in-calendar '(calendar-backward-day 1))))
  10112. (org-defkey minibuffer-local-map [(shift right)]
  10113. (lambda () (interactive)
  10114. (org-eval-in-calendar '(calendar-forward-day 1))))
  10115. (org-defkey minibuffer-local-map ">"
  10116. (lambda () (interactive)
  10117. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10118. (org-defkey minibuffer-local-map "<"
  10119. (lambda () (interactive)
  10120. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10121. (unwind-protect
  10122. (progn
  10123. (use-local-map map)
  10124. (add-hook 'post-command-hook 'org-read-date-display)
  10125. (setq org-ans0 (read-string prompt default-input nil nil))
  10126. ;; org-ans0: from prompt
  10127. ;; org-ans1: from mouse click
  10128. ;; org-ans2: from calendar motion
  10129. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10130. (remove-hook 'post-command-hook 'org-read-date-display)
  10131. (use-local-map old-map)
  10132. (when org-read-date-overlay
  10133. (org-delete-overlay org-read-date-overlay)
  10134. (setq org-read-date-overlay nil)))))))
  10135. (t ; Naked prompt only
  10136. (unwind-protect
  10137. (setq ans (read-string prompt default-input nil timestr))
  10138. (when org-read-date-overlay
  10139. (org-delete-overlay org-read-date-overlay)
  10140. (setq org-read-date-overlay nil)))))
  10141. (setq final (org-read-date-analyze ans def defdecode))
  10142. (if to-time
  10143. (apply 'encode-time final)
  10144. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10145. (format "%04d-%02d-%02d %02d:%02d"
  10146. (nth 5 final) (nth 4 final) (nth 3 final)
  10147. (nth 2 final) (nth 1 final))
  10148. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10149. (defvar def)
  10150. (defvar defdecode)
  10151. (defvar with-time)
  10152. (defun org-read-date-display ()
  10153. "Display the current date prompt interpretation in the minibuffer."
  10154. (when org-read-date-display-live
  10155. (when org-read-date-overlay
  10156. (org-delete-overlay org-read-date-overlay))
  10157. (let ((p (point)))
  10158. (end-of-line 1)
  10159. (while (not (equal (buffer-substring
  10160. (max (point-min) (- (point) 4)) (point))
  10161. " "))
  10162. (insert " "))
  10163. (goto-char p))
  10164. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10165. " " (or org-ans1 org-ans2)))
  10166. (org-end-time-was-given nil)
  10167. (f (org-read-date-analyze ans def defdecode))
  10168. (fmts (if org-dcst
  10169. org-time-stamp-custom-formats
  10170. org-time-stamp-formats))
  10171. (fmt (if (or with-time
  10172. (and (boundp 'org-time-was-given) org-time-was-given))
  10173. (cdr fmts)
  10174. (car fmts)))
  10175. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10176. (when (and org-end-time-was-given
  10177. (string-match org-plain-time-of-day-regexp txt))
  10178. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10179. org-end-time-was-given
  10180. (substring txt (match-end 0)))))
  10181. (setq org-read-date-overlay
  10182. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10183. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10184. (defun org-read-date-analyze (ans def defdecode)
  10185. "Analyse the combined answer of the date prompt."
  10186. ;; FIXME: cleanup and comment
  10187. (let (delta deltan deltaw deltadef year month day
  10188. hour minute second wday pm h2 m2 tl wday1
  10189. iso-year iso-weekday iso-week iso-year iso-date)
  10190. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10191. (setq ans "+0"))
  10192. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10193. (setq ans (replace-match "" t t ans)
  10194. deltan (car delta)
  10195. deltaw (nth 1 delta)
  10196. deltadef (nth 2 delta)))
  10197. ;; Check if there is an iso week date in there
  10198. ;; If yes, sore the info and postpone interpreting it until the rest
  10199. ;; of the parsing is done
  10200. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10201. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10202. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10203. iso-week (string-to-number (match-string 2 ans)))
  10204. (setq ans (replace-match "" t t ans)))
  10205. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10206. (when (string-match
  10207. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10208. (setq year (if (match-end 2)
  10209. (string-to-number (match-string 2 ans))
  10210. (string-to-number (format-time-string "%Y")))
  10211. month (string-to-number (match-string 3 ans))
  10212. day (string-to-number (match-string 4 ans)))
  10213. (if (< year 100) (setq year (+ 2000 year)))
  10214. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10215. t nil ans)))
  10216. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10217. ;; If there is a time with am/pm, and *no* time without it, we convert
  10218. ;; so that matching will be successful.
  10219. (loop for i from 1 to 2 do ; twice, for end time as well
  10220. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10221. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10222. (setq hour (string-to-number (match-string 1 ans))
  10223. minute (if (match-end 3)
  10224. (string-to-number (match-string 3 ans))
  10225. 0)
  10226. pm (equal ?p
  10227. (string-to-char (downcase (match-string 4 ans)))))
  10228. (if (and (= hour 12) (not pm))
  10229. (setq hour 0)
  10230. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10231. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10232. t t ans))))
  10233. ;; Check if a time range is given as a duration
  10234. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10235. (setq hour (string-to-number (match-string 1 ans))
  10236. h2 (+ hour (string-to-number (match-string 3 ans)))
  10237. minute (string-to-number (match-string 2 ans))
  10238. m2 (+ minute (if (match-end 5) (string-to-number
  10239. (match-string 5 ans))0)))
  10240. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10241. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10242. t t ans)))
  10243. ;; Check if there is a time range
  10244. (when (boundp 'org-end-time-was-given)
  10245. (setq org-time-was-given nil)
  10246. (when (and (string-match org-plain-time-of-day-regexp ans)
  10247. (match-end 8))
  10248. (setq org-end-time-was-given (match-string 8 ans))
  10249. (setq ans (concat (substring ans 0 (match-beginning 7))
  10250. (substring ans (match-end 7))))))
  10251. (setq tl (parse-time-string ans)
  10252. day (or (nth 3 tl) (nth 3 defdecode))
  10253. month (or (nth 4 tl)
  10254. (if (and org-read-date-prefer-future
  10255. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10256. (1+ (nth 4 defdecode))
  10257. (nth 4 defdecode)))
  10258. year (or (nth 5 tl)
  10259. (if (and org-read-date-prefer-future
  10260. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10261. (1+ (nth 5 defdecode))
  10262. (nth 5 defdecode)))
  10263. hour (or (nth 2 tl) (nth 2 defdecode))
  10264. minute (or (nth 1 tl) (nth 1 defdecode))
  10265. second (or (nth 0 tl) 0)
  10266. wday (nth 6 tl))
  10267. ;; Special date definitions below
  10268. (cond
  10269. (iso-week
  10270. ;; There was an iso week
  10271. (setq year (or iso-year year)
  10272. day (or iso-weekday wday 1)
  10273. wday nil ; to make sure that the trigger below does not match
  10274. iso-date (calendar-gregorian-from-absolute
  10275. (calendar-absolute-from-iso
  10276. (list iso-week day year))))
  10277. ; FIXME: Should we also push ISO weeks into the future?
  10278. ; (when (and org-read-date-prefer-future
  10279. ; (not iso-year)
  10280. ; (< (calendar-absolute-from-gregorian iso-date)
  10281. ; (time-to-days (current-time))))
  10282. ; (setq year (1+ year)
  10283. ; iso-date (calendar-gregorian-from-absolute
  10284. ; (calendar-absolute-from-iso
  10285. ; (list iso-week day year)))))
  10286. (setq month (car iso-date)
  10287. year (nth 2 iso-date)
  10288. day (nth 1 iso-date)))
  10289. (deltan
  10290. (unless deltadef
  10291. (let ((now (decode-time (current-time))))
  10292. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10293. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10294. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10295. ((equal deltaw "m") (setq month (+ month deltan)))
  10296. ((equal deltaw "y") (setq year (+ year deltan)))))
  10297. ((and wday (not (nth 3 tl)))
  10298. ;; Weekday was given, but no day, so pick that day in the week
  10299. ;; on or after the derived date.
  10300. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10301. (unless (equal wday wday1)
  10302. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10303. (if (and (boundp 'org-time-was-given)
  10304. (nth 2 tl))
  10305. (setq org-time-was-given t))
  10306. (if (< year 100) (setq year (+ 2000 year)))
  10307. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10308. (list second minute hour day month year)))
  10309. (defvar parse-time-weekdays)
  10310. (defun org-read-date-get-relative (s today default)
  10311. "Check string S for special relative date string.
  10312. TODAY and DEFAULT are internal times, for today and for a default.
  10313. Return shift list (N what def-flag)
  10314. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10315. N is the number of WHATs to shift.
  10316. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10317. the DEFAULT date rather than TODAY."
  10318. (when (and
  10319. (string-match
  10320. (concat
  10321. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10322. "\\([0-9]+\\)?"
  10323. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10324. "\\([ \t]\\|$\\)") s)
  10325. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10326. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10327. (string-to-char (substring (match-string 1 s) -1))
  10328. ?+))
  10329. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10330. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10331. (what (if (match-end 3) (match-string 3 s) "d"))
  10332. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10333. (date (if rel default today))
  10334. (wday (nth 6 (decode-time date)))
  10335. delta)
  10336. (if wday1
  10337. (progn
  10338. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10339. (if (= dir ?-) (setq delta (- delta 7)))
  10340. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10341. (list delta "d" rel))
  10342. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10343. (defun org-eval-in-calendar (form &optional keepdate)
  10344. "Eval FORM in the calendar window and return to current window.
  10345. Also, store the cursor date in variable org-ans2."
  10346. (let ((sw (selected-window)))
  10347. (select-window (get-buffer-window "*Calendar*"))
  10348. (eval form)
  10349. (when (and (not keepdate) (calendar-cursor-to-date))
  10350. (let* ((date (calendar-cursor-to-date))
  10351. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10352. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10353. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10354. (select-window sw)))
  10355. (defun org-calendar-select ()
  10356. "Return to `org-read-date' with the date currently selected.
  10357. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10358. (interactive)
  10359. (when (calendar-cursor-to-date)
  10360. (let* ((date (calendar-cursor-to-date))
  10361. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10362. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10363. (if (active-minibuffer-window) (exit-minibuffer))))
  10364. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10365. "Insert a date stamp for the date given by the internal TIME.
  10366. WITH-HM means, use the stamp format that includes the time of the day.
  10367. INACTIVE means use square brackets instead of angular ones, so that the
  10368. stamp will not contribute to the agenda.
  10369. PRE and POST are optional strings to be inserted before and after the
  10370. stamp.
  10371. The command returns the inserted time stamp."
  10372. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10373. stamp)
  10374. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10375. (insert-before-markers (or pre ""))
  10376. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10377. (when (listp extra)
  10378. (setq extra (car extra))
  10379. (if (and (stringp extra)
  10380. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10381. (setq extra (format "-%02d:%02d"
  10382. (string-to-number (match-string 1 extra))
  10383. (string-to-number (match-string 2 extra))))
  10384. (setq extra nil)))
  10385. (when extra
  10386. (backward-char 1)
  10387. (insert-before-markers extra)
  10388. (forward-char 1))
  10389. (insert-before-markers (or post ""))
  10390. (setq org-last-inserted-timestamp stamp)))
  10391. (defun org-toggle-time-stamp-overlays ()
  10392. "Toggle the use of custom time stamp formats."
  10393. (interactive)
  10394. (setq org-display-custom-times (not org-display-custom-times))
  10395. (unless org-display-custom-times
  10396. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10397. (while (setq p (next-single-property-change p 'display))
  10398. (if (and (get-text-property p 'display)
  10399. (eq (get-text-property p 'face) 'org-date))
  10400. (remove-text-properties
  10401. p (setq p (next-single-property-change p 'display))
  10402. '(display t))))
  10403. (set-buffer-modified-p bmp)))
  10404. (if (featurep 'xemacs)
  10405. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10406. (org-restart-font-lock)
  10407. (setq org-table-may-need-update t)
  10408. (if org-display-custom-times
  10409. (message "Time stamps are overlayed with custom format")
  10410. (message "Time stamp overlays removed")))
  10411. (defun org-display-custom-time (beg end)
  10412. "Overlay modified time stamp format over timestamp between BEG and END."
  10413. (let* ((ts (buffer-substring beg end))
  10414. t1 w1 with-hm tf time str w2 (off 0))
  10415. (save-match-data
  10416. (setq t1 (org-parse-time-string ts t))
  10417. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10418. (setq off (- (match-end 0) (match-beginning 0)))))
  10419. (setq end (- end off))
  10420. (setq w1 (- end beg)
  10421. with-hm (and (nth 1 t1) (nth 2 t1))
  10422. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10423. time (org-fix-decoded-time t1)
  10424. str (org-add-props
  10425. (format-time-string
  10426. (substring tf 1 -1) (apply 'encode-time time))
  10427. nil 'mouse-face 'highlight)
  10428. w2 (length str))
  10429. (if (not (= w2 w1))
  10430. (add-text-properties (1+ beg) (+ 2 beg)
  10431. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10432. (if (featurep 'xemacs)
  10433. (progn
  10434. (put-text-property beg end 'invisible t)
  10435. (put-text-property beg end 'end-glyph (make-glyph str)))
  10436. (put-text-property beg end 'display str))))
  10437. (defun org-translate-time (string)
  10438. "Translate all timestamps in STRING to custom format.
  10439. But do this only if the variable `org-display-custom-times' is set."
  10440. (when org-display-custom-times
  10441. (save-match-data
  10442. (let* ((start 0)
  10443. (re org-ts-regexp-both)
  10444. t1 with-hm inactive tf time str beg end)
  10445. (while (setq start (string-match re string start))
  10446. (setq beg (match-beginning 0)
  10447. end (match-end 0)
  10448. t1 (save-match-data
  10449. (org-parse-time-string (substring string beg end) t))
  10450. with-hm (and (nth 1 t1) (nth 2 t1))
  10451. inactive (equal (substring string beg (1+ beg)) "[")
  10452. tf (funcall (if with-hm 'cdr 'car)
  10453. org-time-stamp-custom-formats)
  10454. time (org-fix-decoded-time t1)
  10455. str (format-time-string
  10456. (concat
  10457. (if inactive "[" "<") (substring tf 1 -1)
  10458. (if inactive "]" ">"))
  10459. (apply 'encode-time time))
  10460. string (replace-match str t t string)
  10461. start (+ start (length str)))))))
  10462. string)
  10463. (defun org-fix-decoded-time (time)
  10464. "Set 0 instead of nil for the first 6 elements of time.
  10465. Don't touch the rest."
  10466. (let ((n 0))
  10467. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10468. (defun org-days-to-time (timestamp-string)
  10469. "Difference between TIMESTAMP-STRING and now in days."
  10470. (- (time-to-days (org-time-string-to-time timestamp-string))
  10471. (time-to-days (current-time))))
  10472. (defun org-deadline-close (timestamp-string &optional ndays)
  10473. "Is the time in TIMESTAMP-STRING close to the current date?"
  10474. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10475. (and (< (org-days-to-time timestamp-string) ndays)
  10476. (not (org-entry-is-done-p))))
  10477. (defun org-get-wdays (ts)
  10478. "Get the deadline lead time appropriate for timestring TS."
  10479. (cond
  10480. ((<= org-deadline-warning-days 0)
  10481. ;; 0 or negative, enforce this value no matter what
  10482. (- org-deadline-warning-days))
  10483. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10484. ;; lead time is specified.
  10485. (floor (* (string-to-number (match-string 1 ts))
  10486. (cdr (assoc (match-string 2 ts)
  10487. '(("d" . 1) ("w" . 7)
  10488. ("m" . 30.4) ("y" . 365.25)))))))
  10489. ;; go for the default.
  10490. (t org-deadline-warning-days)))
  10491. (defun org-calendar-select-mouse (ev)
  10492. "Return to `org-read-date' with the date currently selected.
  10493. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10494. (interactive "e")
  10495. (mouse-set-point ev)
  10496. (when (calendar-cursor-to-date)
  10497. (let* ((date (calendar-cursor-to-date))
  10498. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10499. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10500. (if (active-minibuffer-window) (exit-minibuffer))))
  10501. (defun org-check-deadlines (ndays)
  10502. "Check if there are any deadlines due or past due.
  10503. A deadline is considered due if it happens within `org-deadline-warning-days'
  10504. days from today's date. If the deadline appears in an entry marked DONE,
  10505. it is not shown. The prefix arg NDAYS can be used to test that many
  10506. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10507. (interactive "P")
  10508. (let* ((org-warn-days
  10509. (cond
  10510. ((equal ndays '(4)) 100000)
  10511. (ndays (prefix-numeric-value ndays))
  10512. (t (abs org-deadline-warning-days))))
  10513. (case-fold-search nil)
  10514. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10515. (callback
  10516. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10517. (message "%d deadlines past-due or due within %d days"
  10518. (org-occur regexp nil callback)
  10519. org-warn-days)))
  10520. (defun org-check-before-date (date)
  10521. "Check if there are deadlines or scheduled entries before DATE."
  10522. (interactive (list (org-read-date)))
  10523. (let ((case-fold-search nil)
  10524. (regexp (concat "\\<\\(" org-deadline-string
  10525. "\\|" org-scheduled-string
  10526. "\\) *<\\([^>]+\\)>"))
  10527. (callback
  10528. (lambda () (time-less-p
  10529. (org-time-string-to-time (match-string 2))
  10530. (org-time-string-to-time date)))))
  10531. (message "%d entries before %s"
  10532. (org-occur regexp nil callback) date)))
  10533. (defun org-evaluate-time-range (&optional to-buffer)
  10534. "Evaluate a time range by computing the difference between start and end.
  10535. Normally the result is just printed in the echo area, but with prefix arg
  10536. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10537. If the time range is actually in a table, the result is inserted into the
  10538. next column.
  10539. For time difference computation, a year is assumed to be exactly 365
  10540. days in order to avoid rounding problems."
  10541. (interactive "P")
  10542. (or
  10543. (org-clock-update-time-maybe)
  10544. (save-excursion
  10545. (unless (org-at-date-range-p t)
  10546. (goto-char (point-at-bol))
  10547. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10548. (if (not (org-at-date-range-p t))
  10549. (error "Not at a time-stamp range, and none found in current line")))
  10550. (let* ((ts1 (match-string 1))
  10551. (ts2 (match-string 2))
  10552. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10553. (match-end (match-end 0))
  10554. (time1 (org-time-string-to-time ts1))
  10555. (time2 (org-time-string-to-time ts2))
  10556. (t1 (time-to-seconds time1))
  10557. (t2 (time-to-seconds time2))
  10558. (diff (abs (- t2 t1)))
  10559. (negative (< (- t2 t1) 0))
  10560. ;; (ys (floor (* 365 24 60 60)))
  10561. (ds (* 24 60 60))
  10562. (hs (* 60 60))
  10563. (fy "%dy %dd %02d:%02d")
  10564. (fy1 "%dy %dd")
  10565. (fd "%dd %02d:%02d")
  10566. (fd1 "%dd")
  10567. (fh "%02d:%02d")
  10568. y d h m align)
  10569. (if havetime
  10570. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10571. y 0
  10572. d (floor (/ diff ds)) diff (mod diff ds)
  10573. h (floor (/ diff hs)) diff (mod diff hs)
  10574. m (floor (/ diff 60)))
  10575. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10576. y 0
  10577. d (floor (+ (/ diff ds) 0.5))
  10578. h 0 m 0))
  10579. (if (not to-buffer)
  10580. (message "%s" (org-make-tdiff-string y d h m))
  10581. (if (org-at-table-p)
  10582. (progn
  10583. (goto-char match-end)
  10584. (setq align t)
  10585. (and (looking-at " *|") (goto-char (match-end 0))))
  10586. (goto-char match-end))
  10587. (if (looking-at
  10588. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10589. (replace-match ""))
  10590. (if negative (insert " -"))
  10591. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10592. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10593. (insert " " (format fh h m))))
  10594. (if align (org-table-align))
  10595. (message "Time difference inserted")))))
  10596. (defun org-make-tdiff-string (y d h m)
  10597. (let ((fmt "")
  10598. (l nil))
  10599. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10600. l (push y l)))
  10601. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10602. l (push d l)))
  10603. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10604. l (push h l)))
  10605. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10606. l (push m l)))
  10607. (apply 'format fmt (nreverse l))))
  10608. (defun org-time-string-to-time (s)
  10609. (apply 'encode-time (org-parse-time-string s)))
  10610. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10611. "Convert a time stamp to an absolute day number.
  10612. If there is a specifyer for a cyclic time stamp, get the closest date to
  10613. DAYNR.
  10614. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10615. (cond
  10616. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10617. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10618. daynr
  10619. (+ daynr 1000)))
  10620. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10621. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10622. (time-to-days (current-time))) (match-string 0 s)
  10623. prefer show-all))
  10624. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10625. (defun org-days-to-iso-week (days)
  10626. "Return the iso week number."
  10627. (require 'cal-iso)
  10628. (car (calendar-iso-from-absolute days)))
  10629. (defun org-small-year-to-year (year)
  10630. "Convert 2-digit years into 4-digit years.
  10631. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10632. The year 2000 cannot be abbreviated. Any year larger than 99
  10633. is returned unchanged."
  10634. (if (< year 38)
  10635. (setq year (+ 2000 year))
  10636. (if (< year 100)
  10637. (setq year (+ 1900 year))))
  10638. year)
  10639. (defun org-time-from-absolute (d)
  10640. "Return the time corresponding to date D.
  10641. D may be an absolute day number, or a calendar-type list (month day year)."
  10642. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10643. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10644. (defun org-calendar-holiday ()
  10645. "List of holidays, for Diary display in Org-mode."
  10646. (require 'holidays)
  10647. (let ((hl (funcall
  10648. (if (fboundp 'calendar-check-holidays)
  10649. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10650. (if hl (mapconcat 'identity hl "; "))))
  10651. (defun org-diary-sexp-entry (sexp entry date)
  10652. "Process a SEXP diary ENTRY for DATE."
  10653. (require 'diary-lib)
  10654. (let ((result (if calendar-debug-sexp
  10655. (let ((stack-trace-on-error t))
  10656. (eval (car (read-from-string sexp))))
  10657. (condition-case nil
  10658. (eval (car (read-from-string sexp)))
  10659. (error
  10660. (beep)
  10661. (message "Bad sexp at line %d in %s: %s"
  10662. (org-current-line)
  10663. (buffer-file-name) sexp)
  10664. (sleep-for 2))))))
  10665. (cond ((stringp result) result)
  10666. ((and (consp result)
  10667. (stringp (cdr result))) (cdr result))
  10668. (result entry)
  10669. (t nil))))
  10670. (defun org-diary-to-ical-string (frombuf)
  10671. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10672. This uses the icalendar.el library."
  10673. (let* ((tmpdir (if (featurep 'xemacs)
  10674. (temp-directory)
  10675. temporary-file-directory))
  10676. (tmpfile (make-temp-name
  10677. (expand-file-name "orgics" tmpdir)))
  10678. buf rtn b e)
  10679. (save-excursion
  10680. (set-buffer frombuf)
  10681. (icalendar-export-region (point-min) (point-max) tmpfile)
  10682. (setq buf (find-buffer-visiting tmpfile))
  10683. (set-buffer buf)
  10684. (goto-char (point-min))
  10685. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10686. (setq b (match-beginning 0)))
  10687. (goto-char (point-max))
  10688. (if (re-search-backward "^END:VEVENT" nil t)
  10689. (setq e (match-end 0)))
  10690. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10691. (kill-buffer buf)
  10692. (delete-file tmpfile)
  10693. rtn))
  10694. (defun org-closest-date (start current change prefer show-all)
  10695. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10696. When PREFER is `past' return a date that is either CURRENT or past.
  10697. When PREFER is `future', return a date that is either CURRENT or future.
  10698. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10699. ;; Make the proper lists from the dates
  10700. (catch 'exit
  10701. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10702. dn dw sday cday n1 n2 n0
  10703. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10704. (setq start (org-date-to-gregorian start)
  10705. current (org-date-to-gregorian
  10706. (if show-all
  10707. current
  10708. (time-to-days (current-time))))
  10709. sday (calendar-absolute-from-gregorian start)
  10710. cday (calendar-absolute-from-gregorian current))
  10711. (if (<= cday sday) (throw 'exit sday))
  10712. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10713. (setq dn (string-to-number (match-string 1 change))
  10714. dw (cdr (assoc (match-string 2 change) a1)))
  10715. (error "Invalid change specifyer: %s" change))
  10716. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10717. (cond
  10718. ((eq dw 'day)
  10719. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10720. n2 (+ n1 dn)))
  10721. ((eq dw 'year)
  10722. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10723. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10724. (setq date1 (list m d y1)
  10725. n1 (calendar-absolute-from-gregorian date1)
  10726. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10727. n2 (calendar-absolute-from-gregorian date2)))
  10728. ((eq dw 'month)
  10729. ;; approx number of month between the two dates
  10730. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10731. ;; How often does dn fit in there?
  10732. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10733. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10734. m (+ m nm)
  10735. ny (floor (/ m 12))
  10736. y (+ y ny)
  10737. m (- m (* ny 12)))
  10738. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10739. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10740. (setq m2 (+ m dn) y2 y)
  10741. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10742. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10743. (while (<= n2 cday)
  10744. (setq n1 n2 m m2 y y2)
  10745. (setq m2 (+ m dn) y2 y)
  10746. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10747. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10748. ;; Make sure n1 is the earlier date
  10749. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10750. (if show-all
  10751. (cond
  10752. ((eq prefer 'past) n1)
  10753. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10754. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10755. (cond
  10756. ((eq prefer 'past) n1)
  10757. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10758. (t (if (= cday n1) n1 n2)))))))
  10759. (defun org-date-to-gregorian (date)
  10760. "Turn any specification of DATE into a gregorian date for the calendar."
  10761. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10762. ((and (listp date) (= (length date) 3)) date)
  10763. ((stringp date)
  10764. (setq date (org-parse-time-string date))
  10765. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10766. ((listp date)
  10767. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10768. (defun org-parse-time-string (s &optional nodefault)
  10769. "Parse the standard Org-mode time string.
  10770. This should be a lot faster than the normal `parse-time-string'.
  10771. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10772. hour and minute fields will be nil if not given."
  10773. (if (string-match org-ts-regexp0 s)
  10774. (list 0
  10775. (if (or (match-beginning 8) (not nodefault))
  10776. (string-to-number (or (match-string 8 s) "0")))
  10777. (if (or (match-beginning 7) (not nodefault))
  10778. (string-to-number (or (match-string 7 s) "0")))
  10779. (string-to-number (match-string 4 s))
  10780. (string-to-number (match-string 3 s))
  10781. (string-to-number (match-string 2 s))
  10782. nil nil nil)
  10783. (make-list 9 0)))
  10784. (defun org-timestamp-up (&optional arg)
  10785. "Increase the date item at the cursor by one.
  10786. If the cursor is on the year, change the year. If it is on the month or
  10787. the day, change that.
  10788. With prefix ARG, change by that many units."
  10789. (interactive "p")
  10790. (org-timestamp-change (prefix-numeric-value arg)))
  10791. (defun org-timestamp-down (&optional arg)
  10792. "Decrease the date item at the cursor by one.
  10793. If the cursor is on the year, change the year. If it is on the month or
  10794. the day, change that.
  10795. With prefix ARG, change by that many units."
  10796. (interactive "p")
  10797. (org-timestamp-change (- (prefix-numeric-value arg))))
  10798. (defun org-timestamp-up-day (&optional arg)
  10799. "Increase the date in the time stamp by one day.
  10800. With prefix ARG, change that many days."
  10801. (interactive "p")
  10802. (if (and (not (org-at-timestamp-p t))
  10803. (org-on-heading-p))
  10804. (org-todo 'up)
  10805. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10806. (defun org-timestamp-down-day (&optional arg)
  10807. "Decrease the date in the time stamp by one day.
  10808. With prefix ARG, change that many days."
  10809. (interactive "p")
  10810. (if (and (not (org-at-timestamp-p t))
  10811. (org-on-heading-p))
  10812. (org-todo 'down)
  10813. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10814. (defun org-at-timestamp-p (&optional inactive-ok)
  10815. "Determine if the cursor is in or at a timestamp."
  10816. (interactive)
  10817. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10818. (pos (point))
  10819. (ans (or (looking-at tsr)
  10820. (save-excursion
  10821. (skip-chars-backward "^[<\n\r\t")
  10822. (if (> (point) (point-min)) (backward-char 1))
  10823. (and (looking-at tsr)
  10824. (> (- (match-end 0) pos) -1))))))
  10825. (and ans
  10826. (boundp 'org-ts-what)
  10827. (setq org-ts-what
  10828. (cond
  10829. ((= pos (match-beginning 0)) 'bracket)
  10830. ((= pos (1- (match-end 0))) 'bracket)
  10831. ((org-pos-in-match-range pos 2) 'year)
  10832. ((org-pos-in-match-range pos 3) 'month)
  10833. ((org-pos-in-match-range pos 7) 'hour)
  10834. ((org-pos-in-match-range pos 8) 'minute)
  10835. ((or (org-pos-in-match-range pos 4)
  10836. (org-pos-in-match-range pos 5)) 'day)
  10837. ((and (> pos (or (match-end 8) (match-end 5)))
  10838. (< pos (match-end 0)))
  10839. (- pos (or (match-end 8) (match-end 5))))
  10840. (t 'day))))
  10841. ans))
  10842. (defun org-toggle-timestamp-type ()
  10843. "Toggle the type (<active> or [inactive]) of a time stamp."
  10844. (interactive)
  10845. (when (org-at-timestamp-p t)
  10846. (let ((beg (match-beginning 0)) (end (match-end 0))
  10847. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10848. (save-excursion
  10849. (goto-char beg)
  10850. (while (re-search-forward "[][<>]" end t)
  10851. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10852. t t)))
  10853. (message "Timestamp is now %sactive"
  10854. (if (equal (char-after beg) ?<) "" "in")))))
  10855. (defun org-timestamp-change (n &optional what)
  10856. "Change the date in the time stamp at point.
  10857. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10858. `year', `minute', `second'. If WHAT is not given, the cursor position
  10859. in the timestamp determines what will be changed."
  10860. (let ((pos (point))
  10861. with-hm inactive
  10862. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10863. org-ts-what
  10864. extra rem
  10865. ts time time0)
  10866. (if (not (org-at-timestamp-p t))
  10867. (error "Not at a timestamp"))
  10868. (if (and (not what) (eq org-ts-what 'bracket))
  10869. (org-toggle-timestamp-type)
  10870. (if (and (not what) (not (eq org-ts-what 'day))
  10871. org-display-custom-times
  10872. (get-text-property (point) 'display)
  10873. (not (get-text-property (1- (point)) 'display)))
  10874. (setq org-ts-what 'day))
  10875. (setq org-ts-what (or what org-ts-what)
  10876. inactive (= (char-after (match-beginning 0)) ?\[)
  10877. ts (match-string 0))
  10878. (replace-match "")
  10879. (if (string-match
  10880. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10881. ts)
  10882. (setq extra (match-string 1 ts)))
  10883. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10884. (setq with-hm t))
  10885. (setq time0 (org-parse-time-string ts))
  10886. (when (and (eq org-ts-what 'minute)
  10887. (eq current-prefix-arg nil))
  10888. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10889. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10890. (setcar (cdr time0) (+ (nth 1 time0)
  10891. (if (> n 0) (- rem) (- dm rem))))))
  10892. (setq time
  10893. (encode-time (or (car time0) 0)
  10894. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10895. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10896. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10897. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10898. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10899. (nthcdr 6 time0)))
  10900. (when (integerp org-ts-what)
  10901. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10902. (if (eq what 'calendar)
  10903. (let ((cal-date (org-get-date-from-calendar)))
  10904. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10905. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10906. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10907. (setcar time0 (or (car time0) 0))
  10908. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10909. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10910. (setq time (apply 'encode-time time0))))
  10911. (setq org-last-changed-timestamp
  10912. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10913. (org-clock-update-time-maybe)
  10914. (goto-char pos)
  10915. ;; Try to recenter the calendar window, if any
  10916. (if (and org-calendar-follow-timestamp-change
  10917. (get-buffer-window "*Calendar*" t)
  10918. (memq org-ts-what '(day month year)))
  10919. (org-recenter-calendar (time-to-days time))))))
  10920. (defun org-modify-ts-extra (s pos n dm)
  10921. "Change the different parts of the lead-time and repeat fields in timestamp."
  10922. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10923. ng h m new rem)
  10924. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10925. (cond
  10926. ((or (org-pos-in-match-range pos 2)
  10927. (org-pos-in-match-range pos 3))
  10928. (setq m (string-to-number (match-string 3 s))
  10929. h (string-to-number (match-string 2 s)))
  10930. (if (org-pos-in-match-range pos 2)
  10931. (setq h (+ h n))
  10932. (setq n (* dm (org-no-warnings (signum n))))
  10933. (when (not (= 0 (setq rem (% m dm))))
  10934. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10935. (setq m (+ m n)))
  10936. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10937. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10938. (setq h (min 24 (max 0 h)))
  10939. (setq ng 1 new (format "-%02d:%02d" h m)))
  10940. ((org-pos-in-match-range pos 6)
  10941. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10942. ((org-pos-in-match-range pos 5)
  10943. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10944. ((org-pos-in-match-range pos 9)
  10945. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10946. ((org-pos-in-match-range pos 8)
  10947. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10948. (when ng
  10949. (setq s (concat
  10950. (substring s 0 (match-beginning ng))
  10951. new
  10952. (substring s (match-end ng))))))
  10953. s))
  10954. (defun org-recenter-calendar (date)
  10955. "If the calendar is visible, recenter it to DATE."
  10956. (let* ((win (selected-window))
  10957. (cwin (get-buffer-window "*Calendar*" t))
  10958. (calendar-move-hook nil))
  10959. (when cwin
  10960. (select-window cwin)
  10961. (calendar-goto-date (if (listp date) date
  10962. (calendar-gregorian-from-absolute date)))
  10963. (select-window win))))
  10964. (defun org-goto-calendar (&optional arg)
  10965. "Go to the Emacs calendar at the current date.
  10966. If there is a time stamp in the current line, go to that date.
  10967. A prefix ARG can be used to force the current date."
  10968. (interactive "P")
  10969. (let ((tsr org-ts-regexp) diff
  10970. (calendar-move-hook nil)
  10971. (calendar-view-holidays-initially-flag nil)
  10972. (view-calendar-holidays-initially nil)
  10973. (calendar-view-diary-initially-flag nil)
  10974. (view-diary-entries-initially nil))
  10975. (if (or (org-at-timestamp-p)
  10976. (save-excursion
  10977. (beginning-of-line 1)
  10978. (looking-at (concat ".*" tsr))))
  10979. (let ((d1 (time-to-days (current-time)))
  10980. (d2 (time-to-days
  10981. (org-time-string-to-time (match-string 1)))))
  10982. (setq diff (- d2 d1))))
  10983. (calendar)
  10984. (calendar-goto-today)
  10985. (if (and diff (not arg)) (calendar-forward-day diff))))
  10986. (defun org-get-date-from-calendar ()
  10987. "Return a list (month day year) of date at point in calendar."
  10988. (with-current-buffer "*Calendar*"
  10989. (save-match-data
  10990. (calendar-cursor-to-date))))
  10991. (defun org-date-from-calendar ()
  10992. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10993. If there is already a time stamp at the cursor position, update it."
  10994. (interactive)
  10995. (if (org-at-timestamp-p t)
  10996. (org-timestamp-change 0 'calendar)
  10997. (let ((cal-date (org-get-date-from-calendar)))
  10998. (org-insert-time-stamp
  10999. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11000. (defun org-minutes-to-hh:mm-string (m)
  11001. "Compute H:MM from a number of minutes."
  11002. (let ((h (/ m 60)))
  11003. (setq m (- m (* 60 h)))
  11004. (format org-time-clocksum-format h m)))
  11005. (defun org-hh:mm-string-to-minutes (s)
  11006. "Convert a string H:MM to a number of minutes."
  11007. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11008. (+ (* (string-to-number (match-string 1 s)) 60)
  11009. (string-to-number (match-string 2 s)))
  11010. 0))
  11011. ;;;; Agenda files
  11012. ;;;###autoload
  11013. (defun org-iswitchb (&optional arg)
  11014. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11015. With a prefix argument, restrict available to files.
  11016. With two prefix arguments, restrict available buffers to agenda files.
  11017. Due to some yet unresolved reason, the global function
  11018. `iswitchb-mode' needs to be active for this function to work."
  11019. (interactive "P")
  11020. (require 'iswitchb)
  11021. (let ((enabled iswitchb-mode) blist)
  11022. (or enabled (iswitchb-mode 1))
  11023. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11024. ((equal arg '(16)) (org-buffer-list 'agenda))
  11025. (t (org-buffer-list))))
  11026. (unwind-protect
  11027. (let ((iswitchb-make-buflist-hook
  11028. (lambda ()
  11029. (setq iswitchb-temp-buflist
  11030. (mapcar 'buffer-name blist)))))
  11031. (switch-to-buffer
  11032. (iswitchb-read-buffer
  11033. "Switch-to: " nil t))
  11034. (or enabled (iswitchb-mode -1))))))
  11035. ;;;###autoload
  11036. (defun org-ido-switchb (&optional arg)
  11037. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11038. With a prefix argument, restrict available to files.
  11039. With two prefix arguments, restrict available buffers to agenda files."
  11040. (interactive "P")
  11041. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11042. ((equal arg '(16)) (org-buffer-list 'agenda))
  11043. (t (org-buffer-list)))))
  11044. (switch-to-buffer
  11045. (org-ido-completing-read "Org buffer: "
  11046. (mapcar 'buffer-name blist)
  11047. nil t))))
  11048. (defun org-buffer-list (&optional predicate exclude-tmp)
  11049. "Return a list of Org buffers.
  11050. PREDICATE can be `export', `files' or `agenda'.
  11051. export restrict the list to Export buffers.
  11052. files restrict the list to buffers visiting Org files.
  11053. agenda restrict the list to buffers visiting agenda files.
  11054. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11055. (let* ((bfn nil)
  11056. (agenda-files (and (eq predicate 'agenda)
  11057. (mapcar 'file-truename (org-agenda-files t))))
  11058. (filter
  11059. (cond
  11060. ((eq predicate 'files)
  11061. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11062. ((eq predicate 'export)
  11063. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11064. ((eq predicate 'agenda)
  11065. (lambda (b)
  11066. (with-current-buffer b
  11067. (and (eq major-mode 'org-mode)
  11068. (setq bfn (buffer-file-name b))
  11069. (member (file-truename bfn) agenda-files)))))
  11070. (t (lambda (b) (with-current-buffer b
  11071. (or (eq major-mode 'org-mode)
  11072. (string-match "\*Org .*Export"
  11073. (buffer-name b)))))))))
  11074. (delq nil
  11075. (mapcar
  11076. (lambda(b)
  11077. (if (and (funcall filter b)
  11078. (or (not exclude-tmp)
  11079. (not (string-match "tmp" (buffer-name b)))))
  11080. b
  11081. nil))
  11082. (buffer-list)))))
  11083. (defun org-agenda-files (&optional unrestricted archives)
  11084. "Get the list of agenda files.
  11085. Optional UNRESTRICTED means return the full list even if a restriction
  11086. is currently in place.
  11087. When ARCHIVES is t, include all archive files hat are really being
  11088. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11089. `org-agenda-archives-mode' is t."
  11090. (let ((files
  11091. (cond
  11092. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11093. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11094. ((listp org-agenda-files) org-agenda-files)
  11095. (t (error "Invalid value of `org-agenda-files'")))))
  11096. (setq files (apply 'append
  11097. (mapcar (lambda (f)
  11098. (if (file-directory-p f)
  11099. (directory-files
  11100. f t org-agenda-file-regexp)
  11101. (list f)))
  11102. files)))
  11103. (when org-agenda-skip-unavailable-files
  11104. (setq files (delq nil
  11105. (mapcar (function
  11106. (lambda (file)
  11107. (and (file-readable-p file) file)))
  11108. files))))
  11109. (when (or (eq archives t)
  11110. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11111. (setq files (org-add-archive-files files)))
  11112. files))
  11113. (defun org-edit-agenda-file-list ()
  11114. "Edit the list of agenda files.
  11115. Depending on setup, this either uses customize to edit the variable
  11116. `org-agenda-files', or it visits the file that is holding the list. In the
  11117. latter case, the buffer is set up in a way that saving it automatically kills
  11118. the buffer and restores the previous window configuration."
  11119. (interactive)
  11120. (if (stringp org-agenda-files)
  11121. (let ((cw (current-window-configuration)))
  11122. (find-file org-agenda-files)
  11123. (org-set-local 'org-window-configuration cw)
  11124. (org-add-hook 'after-save-hook
  11125. (lambda ()
  11126. (set-window-configuration
  11127. (prog1 org-window-configuration
  11128. (kill-buffer (current-buffer))))
  11129. (org-install-agenda-files-menu)
  11130. (message "New agenda file list installed"))
  11131. nil 'local)
  11132. (message "%s" (substitute-command-keys
  11133. "Edit list and finish with \\[save-buffer]")))
  11134. (customize-variable 'org-agenda-files)))
  11135. (defun org-store-new-agenda-file-list (list)
  11136. "Set new value for the agenda file list and save it correctly."
  11137. (if (stringp org-agenda-files)
  11138. (let ((f org-agenda-files) b)
  11139. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11140. (with-temp-file f
  11141. (insert (mapconcat 'identity list "\n") "\n")))
  11142. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11143. (setq org-agenda-files list)
  11144. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11145. (defun org-read-agenda-file-list ()
  11146. "Read the list of agenda files from a file."
  11147. (when (file-directory-p org-agenda-files)
  11148. (error "`org-agenda-files' cannot be a single directory"))
  11149. (when (stringp org-agenda-files)
  11150. (with-temp-buffer
  11151. (insert-file-contents org-agenda-files)
  11152. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11153. ;;;###autoload
  11154. (defun org-cycle-agenda-files ()
  11155. "Cycle through the files in `org-agenda-files'.
  11156. If the current buffer visits an agenda file, find the next one in the list.
  11157. If the current buffer does not, find the first agenda file."
  11158. (interactive)
  11159. (let* ((fs (org-agenda-files t))
  11160. (files (append fs (list (car fs))))
  11161. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11162. file)
  11163. (unless files (error "No agenda files"))
  11164. (catch 'exit
  11165. (while (setq file (pop files))
  11166. (if (equal (file-truename file) tcf)
  11167. (when (car files)
  11168. (find-file (car files))
  11169. (throw 'exit t))))
  11170. (find-file (car fs)))
  11171. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11172. (defun org-agenda-file-to-front (&optional to-end)
  11173. "Move/add the current file to the top of the agenda file list.
  11174. If the file is not present in the list, it is added to the front. If it is
  11175. present, it is moved there. With optional argument TO-END, add/move to the
  11176. end of the list."
  11177. (interactive "P")
  11178. (let ((org-agenda-skip-unavailable-files nil)
  11179. (file-alist (mapcar (lambda (x)
  11180. (cons (file-truename x) x))
  11181. (org-agenda-files t)))
  11182. (ctf (file-truename buffer-file-name))
  11183. x had)
  11184. (setq x (assoc ctf file-alist) had x)
  11185. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11186. (if to-end
  11187. (setq file-alist (append (delq x file-alist) (list x)))
  11188. (setq file-alist (cons x (delq x file-alist))))
  11189. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11190. (org-install-agenda-files-menu)
  11191. (message "File %s to %s of agenda file list"
  11192. (if had "moved" "added") (if to-end "end" "front"))))
  11193. (defun org-remove-file (&optional file)
  11194. "Remove current file from the list of files in variable `org-agenda-files'.
  11195. These are the files which are being checked for agenda entries.
  11196. Optional argument FILE means, use this file instead of the current."
  11197. (interactive)
  11198. (let* ((org-agenda-skip-unavailable-files nil)
  11199. (file (or file buffer-file-name))
  11200. (true-file (file-truename file))
  11201. (afile (abbreviate-file-name file))
  11202. (files (delq nil (mapcar
  11203. (lambda (x)
  11204. (if (equal true-file
  11205. (file-truename x))
  11206. nil x))
  11207. (org-agenda-files t)))))
  11208. (if (not (= (length files) (length (org-agenda-files t))))
  11209. (progn
  11210. (org-store-new-agenda-file-list files)
  11211. (org-install-agenda-files-menu)
  11212. (message "Removed file: %s" afile))
  11213. (message "File was not in list: %s (not removed)" afile))))
  11214. (defun org-file-menu-entry (file)
  11215. (vector file (list 'find-file file) t))
  11216. (defun org-check-agenda-file (file)
  11217. "Make sure FILE exists. If not, ask user what to do."
  11218. (when (not (file-exists-p file))
  11219. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11220. (abbreviate-file-name file))
  11221. (let ((r (downcase (read-char-exclusive))))
  11222. (cond
  11223. ((equal r ?r)
  11224. (org-remove-file file)
  11225. (throw 'nextfile t))
  11226. (t (error "Abort"))))))
  11227. (defun org-get-agenda-file-buffer (file)
  11228. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11229. it to the list of buffers which might be released later."
  11230. (let ((buf (org-find-base-buffer-visiting file)))
  11231. (if buf
  11232. buf ; just return it
  11233. ;; Make a new buffer and remember it
  11234. (setq buf (find-file-noselect file))
  11235. (if buf (push buf org-agenda-new-buffers))
  11236. buf)))
  11237. (defun org-release-buffers (blist)
  11238. "Release all buffers in list, asking the user for confirmation when needed.
  11239. When a buffer is unmodified, it is just killed. When modified, it is saved
  11240. \(if the user agrees) and then killed."
  11241. (let (buf file)
  11242. (while (setq buf (pop blist))
  11243. (setq file (buffer-file-name buf))
  11244. (when (and (buffer-modified-p buf)
  11245. file
  11246. (y-or-n-p (format "Save file %s? " file)))
  11247. (with-current-buffer buf (save-buffer)))
  11248. (kill-buffer buf))))
  11249. (defun org-prepare-agenda-buffers (files)
  11250. "Create buffers for all agenda files, protect archived trees and comments."
  11251. (interactive)
  11252. (let ((pa '(:org-archived t))
  11253. (pc '(:org-comment t))
  11254. (pall '(:org-archived t :org-comment t))
  11255. (inhibit-read-only t)
  11256. (rea (concat ":" org-archive-tag ":"))
  11257. bmp file re)
  11258. (save-excursion
  11259. (save-restriction
  11260. (while (setq file (pop files))
  11261. (if (bufferp file)
  11262. (set-buffer file)
  11263. (org-check-agenda-file file)
  11264. (set-buffer (org-get-agenda-file-buffer file)))
  11265. (widen)
  11266. (setq bmp (buffer-modified-p))
  11267. (org-refresh-category-properties)
  11268. (setq org-todo-keywords-for-agenda
  11269. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11270. (setq org-done-keywords-for-agenda
  11271. (append org-done-keywords-for-agenda org-done-keywords))
  11272. (setq org-todo-keyword-alist-for-agenda
  11273. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11274. (setq org-tag-alist-for-agenda
  11275. (append org-tag-alist-for-agenda org-tag-alist))
  11276. (save-excursion
  11277. (remove-text-properties (point-min) (point-max) pall)
  11278. (when org-agenda-skip-archived-trees
  11279. (goto-char (point-min))
  11280. (while (re-search-forward rea nil t)
  11281. (if (org-on-heading-p t)
  11282. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11283. (goto-char (point-min))
  11284. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11285. (while (re-search-forward re nil t)
  11286. (add-text-properties
  11287. (match-beginning 0) (org-end-of-subtree t) pc)))
  11288. (set-buffer-modified-p bmp))))
  11289. (setq org-todo-keyword-alist-for-agenda
  11290. (org-uniquify org-todo-keyword-alist-for-agenda)
  11291. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11292. ;;;; Embedded LaTeX
  11293. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11294. "Keymap for the minor `org-cdlatex-mode'.")
  11295. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11296. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11297. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11298. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11299. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11300. (defvar org-cdlatex-texmathp-advice-is-done nil
  11301. "Flag remembering if we have applied the advice to texmathp already.")
  11302. (define-minor-mode org-cdlatex-mode
  11303. "Toggle the minor `org-cdlatex-mode'.
  11304. This mode supports entering LaTeX environment and math in LaTeX fragments
  11305. in Org-mode.
  11306. \\{org-cdlatex-mode-map}"
  11307. nil " OCDL" nil
  11308. (when org-cdlatex-mode (require 'cdlatex))
  11309. (unless org-cdlatex-texmathp-advice-is-done
  11310. (setq org-cdlatex-texmathp-advice-is-done t)
  11311. (defadvice texmathp (around org-math-always-on activate)
  11312. "Always return t in org-mode buffers.
  11313. This is because we want to insert math symbols without dollars even outside
  11314. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11315. an embedded LaTeX fragment, let texmathp do its job.
  11316. \\[org-cdlatex-mode-map]"
  11317. (interactive)
  11318. (let (p)
  11319. (cond
  11320. ((not (org-mode-p)) ad-do-it)
  11321. ((eq this-command 'cdlatex-math-symbol)
  11322. (setq ad-return-value t
  11323. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11324. (t
  11325. (let ((p (org-inside-LaTeX-fragment-p)))
  11326. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11327. (setq ad-return-value t
  11328. texmathp-why '("Org-mode embedded math" . 0))
  11329. (if p ad-do-it)))))))))
  11330. (defun turn-on-org-cdlatex ()
  11331. "Unconditionally turn on `org-cdlatex-mode'."
  11332. (org-cdlatex-mode 1))
  11333. (defun org-inside-LaTeX-fragment-p ()
  11334. "Test if point is inside a LaTeX fragment.
  11335. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11336. sequence appearing also before point.
  11337. Even though the matchers for math are configurable, this function assumes
  11338. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11339. delimiters are skipped when they have been removed by customization.
  11340. The return value is nil, or a cons cell with the delimiter and
  11341. and the position of this delimiter.
  11342. This function does a reasonably good job, but can locally be fooled by
  11343. for example currency specifications. For example it will assume being in
  11344. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11345. fragments that are properly closed, but during editing, we have to live
  11346. with the uncertainty caused by missing closing delimiters. This function
  11347. looks only before point, not after."
  11348. (catch 'exit
  11349. (let ((pos (point))
  11350. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11351. (lim (progn
  11352. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11353. (point)))
  11354. dd-on str (start 0) m re)
  11355. (goto-char pos)
  11356. (when dodollar
  11357. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11358. re (nth 1 (assoc "$" org-latex-regexps)))
  11359. (while (string-match re str start)
  11360. (cond
  11361. ((= (match-end 0) (length str))
  11362. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11363. ((= (match-end 0) (- (length str) 5))
  11364. (throw 'exit nil))
  11365. (t (setq start (match-end 0))))))
  11366. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11367. (goto-char pos)
  11368. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11369. (and (match-beginning 2) (throw 'exit nil))
  11370. ;; count $$
  11371. (while (re-search-backward "\\$\\$" lim t)
  11372. (setq dd-on (not dd-on)))
  11373. (goto-char pos)
  11374. (if dd-on (cons "$$" m))))))
  11375. (defun org-try-cdlatex-tab ()
  11376. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11377. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11378. - inside a LaTeX fragment, or
  11379. - after the first word in a line, where an abbreviation expansion could
  11380. insert a LaTeX environment."
  11381. (when org-cdlatex-mode
  11382. (cond
  11383. ((save-excursion
  11384. (skip-chars-backward "a-zA-Z0-9*")
  11385. (skip-chars-backward " \t")
  11386. (bolp))
  11387. (cdlatex-tab) t)
  11388. ((org-inside-LaTeX-fragment-p)
  11389. (cdlatex-tab) t)
  11390. (t nil))))
  11391. (defun org-cdlatex-underscore-caret (&optional arg)
  11392. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11393. Revert to the normal definition outside of these fragments."
  11394. (interactive "P")
  11395. (if (org-inside-LaTeX-fragment-p)
  11396. (call-interactively 'cdlatex-sub-superscript)
  11397. (let (org-cdlatex-mode)
  11398. (call-interactively (key-binding (vector last-input-event))))))
  11399. (defun org-cdlatex-math-modify (&optional arg)
  11400. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11401. Revert to the normal definition outside of these fragments."
  11402. (interactive "P")
  11403. (if (org-inside-LaTeX-fragment-p)
  11404. (call-interactively 'cdlatex-math-modify)
  11405. (let (org-cdlatex-mode)
  11406. (call-interactively (key-binding (vector last-input-event))))))
  11407. (defvar org-latex-fragment-image-overlays nil
  11408. "List of overlays carrying the images of latex fragments.")
  11409. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11410. (defun org-remove-latex-fragment-image-overlays ()
  11411. "Remove all overlays with LaTeX fragment images in current buffer."
  11412. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11413. (setq org-latex-fragment-image-overlays nil))
  11414. (defun org-preview-latex-fragment (&optional subtree)
  11415. "Preview the LaTeX fragment at point, or all locally or globally.
  11416. If the cursor is in a LaTeX fragment, create the image and overlay
  11417. it over the source code. If there is no fragment at point, display
  11418. all fragments in the current text, from one headline to the next. With
  11419. prefix SUBTREE, display all fragments in the current subtree. With a
  11420. double prefix `C-u C-u', or when the cursor is before the first headline,
  11421. display all fragments in the buffer.
  11422. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11423. (interactive "P")
  11424. (org-remove-latex-fragment-image-overlays)
  11425. (save-excursion
  11426. (save-restriction
  11427. (let (beg end at msg)
  11428. (cond
  11429. ((or (equal subtree '(16))
  11430. (not (save-excursion
  11431. (re-search-backward (concat "^" outline-regexp) nil t))))
  11432. (setq beg (point-min) end (point-max)
  11433. msg "Creating images for buffer...%s"))
  11434. ((equal subtree '(4))
  11435. (org-back-to-heading)
  11436. (setq beg (point) end (org-end-of-subtree t)
  11437. msg "Creating images for subtree...%s"))
  11438. (t
  11439. (if (setq at (org-inside-LaTeX-fragment-p))
  11440. (goto-char (max (point-min) (- (cdr at) 2)))
  11441. (org-back-to-heading))
  11442. (setq beg (point) end (progn (outline-next-heading) (point))
  11443. msg (if at "Creating image...%s"
  11444. "Creating images for entry...%s"))))
  11445. (message msg "")
  11446. (narrow-to-region beg end)
  11447. (goto-char beg)
  11448. (org-format-latex
  11449. (concat "ltxpng/" (file-name-sans-extension
  11450. (file-name-nondirectory
  11451. buffer-file-name)))
  11452. default-directory 'overlays msg at 'forbuffer)
  11453. (message msg "done. Use `C-c C-c' to remove images.")))))
  11454. (defvar org-latex-regexps
  11455. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11456. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11457. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11458. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11459. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11460. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11461. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11462. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11463. "Regular expressions for matching embedded LaTeX.")
  11464. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11465. "Replace LaTeX fragments with links to an image, and produce images."
  11466. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11467. (let* ((prefixnodir (file-name-nondirectory prefix))
  11468. (absprefix (expand-file-name prefix dir))
  11469. (todir (file-name-directory absprefix))
  11470. (opt org-format-latex-options)
  11471. (matchers (plist-get opt :matchers))
  11472. (re-list org-latex-regexps)
  11473. (cnt 0) txt link beg end re e checkdir
  11474. m n block linkfile movefile ov)
  11475. ;; Check if there are old images files with this prefix, and remove them
  11476. (when (file-directory-p todir)
  11477. (mapc 'delete-file
  11478. (directory-files
  11479. todir 'full
  11480. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11481. ;; Check the different regular expressions
  11482. (while (setq e (pop re-list))
  11483. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11484. block (if (nth 3 e) "\n\n" ""))
  11485. (when (member m matchers)
  11486. (goto-char (point-min))
  11487. (while (re-search-forward re nil t)
  11488. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11489. (setq txt (match-string n)
  11490. beg (match-beginning n) end (match-end n)
  11491. cnt (1+ cnt)
  11492. linkfile (format "%s_%04d.png" prefix cnt)
  11493. movefile (format "%s_%04d.png" absprefix cnt)
  11494. link (concat block "[[file:" linkfile "]]" block))
  11495. (if msg (message msg cnt))
  11496. (goto-char beg)
  11497. (unless checkdir ; make sure the directory exists
  11498. (setq checkdir t)
  11499. (or (file-directory-p todir) (make-directory todir)))
  11500. (org-create-formula-image
  11501. txt movefile opt forbuffer)
  11502. (if overlays
  11503. (progn
  11504. (setq ov (org-make-overlay beg end))
  11505. (if (featurep 'xemacs)
  11506. (progn
  11507. (org-overlay-put ov 'invisible t)
  11508. (org-overlay-put
  11509. ov 'end-glyph
  11510. (make-glyph (vector 'png :file movefile))))
  11511. (org-overlay-put
  11512. ov 'display
  11513. (list 'image :type 'png :file movefile :ascent 'center)))
  11514. (push ov org-latex-fragment-image-overlays)
  11515. (goto-char end))
  11516. (delete-region beg end)
  11517. (insert link))))))))
  11518. ;; This function borrows from Ganesh Swami's latex2png.el
  11519. (defun org-create-formula-image (string tofile options buffer)
  11520. (let* ((tmpdir (if (featurep 'xemacs)
  11521. (temp-directory)
  11522. temporary-file-directory))
  11523. (texfilebase (make-temp-name
  11524. (expand-file-name "orgtex" tmpdir)))
  11525. (texfile (concat texfilebase ".tex"))
  11526. (dvifile (concat texfilebase ".dvi"))
  11527. (pngfile (concat texfilebase ".png"))
  11528. (fnh (if (featurep 'xemacs)
  11529. (font-height (get-face-font 'default))
  11530. (face-attribute 'default :height nil)))
  11531. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11532. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11533. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11534. "Black"))
  11535. (bg (or (plist-get options (if buffer :background :html-background))
  11536. "Transparent")))
  11537. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11538. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11539. (with-temp-file texfile
  11540. (insert org-format-latex-header
  11541. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11542. (let ((dir default-directory))
  11543. (condition-case nil
  11544. (progn
  11545. (cd tmpdir)
  11546. (call-process "latex" nil nil nil texfile))
  11547. (error nil))
  11548. (cd dir))
  11549. (if (not (file-exists-p dvifile))
  11550. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11551. (condition-case nil
  11552. (call-process "dvipng" nil nil nil
  11553. "-E" "-fg" fg "-bg" bg
  11554. "-D" dpi
  11555. ;;"-x" scale "-y" scale
  11556. "-T" "tight"
  11557. "-o" pngfile
  11558. dvifile)
  11559. (error nil))
  11560. (if (not (file-exists-p pngfile))
  11561. (progn (message "Failed to create png file from %s" texfile) nil)
  11562. ;; Use the requested file name and clean up
  11563. (copy-file pngfile tofile 'replace)
  11564. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11565. (delete-file (concat texfilebase e)))
  11566. pngfile))))
  11567. (defun org-dvipng-color (attr)
  11568. "Return an rgb color specification for dvipng."
  11569. (apply 'format "rgb %s %s %s"
  11570. (mapcar 'org-normalize-color
  11571. (color-values (face-attribute 'default attr nil)))))
  11572. (defun org-normalize-color (value)
  11573. "Return string to be used as color value for an RGB component."
  11574. (format "%g" (/ value 65535.0)))
  11575. ;;;; Key bindings
  11576. ;; Make `C-c C-x' a prefix key
  11577. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11578. ;; TAB key with modifiers
  11579. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11580. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11581. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11582. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11583. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11584. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11585. ;; The following line is necessary under Suse GNU/Linux
  11586. (unless (featurep 'xemacs)
  11587. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11588. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11589. (define-key org-mode-map [backtab] 'org-shifttab)
  11590. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11591. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11592. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11593. ;; Cursor keys with modifiers
  11594. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11595. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11596. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11597. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11598. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11599. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11600. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11601. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11602. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11603. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11604. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11605. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11606. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11607. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11608. ;;; Extra keys for tty access.
  11609. ;; We only set them when really needed because otherwise the
  11610. ;; menus don't show the simple keys
  11611. (when (or org-use-extra-keys
  11612. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11613. (not window-system))
  11614. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11615. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11616. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11617. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11618. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11619. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11620. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11621. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11622. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11623. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11624. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11625. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11626. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11627. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11628. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11629. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11630. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11631. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11632. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11633. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11634. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11635. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11636. ;; All the other keys
  11637. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11638. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11639. (if (boundp 'narrow-map)
  11640. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11641. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11642. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11643. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11644. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11645. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11646. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11647. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11648. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11649. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11650. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11651. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11652. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11653. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11654. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11655. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11656. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11657. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11658. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11659. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11660. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11661. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11662. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11663. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11664. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11665. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11666. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11667. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11668. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11669. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11670. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11671. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11672. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11673. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11674. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11675. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11676. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11677. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11678. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11679. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11680. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11681. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11682. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11683. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11684. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11685. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11686. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11687. (org-defkey org-mode-map "\C-m" 'org-return)
  11688. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11689. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11690. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11691. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11692. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11693. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11694. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11695. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11696. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11697. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11698. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11699. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11700. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11701. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11702. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11703. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11704. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11705. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11706. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11707. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11708. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11709. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11710. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11711. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11712. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11713. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11714. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11715. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11716. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11717. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11718. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11719. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11720. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11721. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11722. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11723. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11724. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11725. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11726. (when (featurep 'xemacs)
  11727. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11728. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11729. (defun org-self-insert-command (N)
  11730. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11731. If the cursor is in a table looking at whitespace, the whitespace is
  11732. overwritten, and the table is not marked as requiring realignment."
  11733. (interactive "p")
  11734. (if (and (org-table-p)
  11735. (progn
  11736. ;; check if we blank the field, and if that triggers align
  11737. (and (featurep 'org-table) org-table-auto-blank-field
  11738. (member last-command
  11739. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11740. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11741. ;; got extra space, this field does not determine column width
  11742. (let (org-table-may-need-update) (org-table-blank-field))
  11743. ;; no extra space, this field may determine column width
  11744. (org-table-blank-field)))
  11745. t)
  11746. (eq N 1)
  11747. (looking-at "[^|\n]* |"))
  11748. (let (org-table-may-need-update)
  11749. (goto-char (1- (match-end 0)))
  11750. (delete-backward-char 1)
  11751. (goto-char (match-beginning 0))
  11752. (self-insert-command N))
  11753. (setq org-table-may-need-update t)
  11754. (self-insert-command N)
  11755. (org-fix-tags-on-the-fly)))
  11756. (defun org-fix-tags-on-the-fly ()
  11757. (when (and (equal (char-after (point-at-bol)) ?*)
  11758. (org-on-heading-p))
  11759. (org-align-tags-here org-tags-column)))
  11760. (defun org-delete-backward-char (N)
  11761. "Like `delete-backward-char', insert whitespace at field end in tables.
  11762. When deleting backwards, in tables this function will insert whitespace in
  11763. front of the next \"|\" separator, to keep the table aligned. The table will
  11764. still be marked for re-alignment if the field did fill the entire column,
  11765. because, in this case the deletion might narrow the column."
  11766. (interactive "p")
  11767. (if (and (org-table-p)
  11768. (eq N 1)
  11769. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11770. (looking-at ".*?|"))
  11771. (let ((pos (point))
  11772. (noalign (looking-at "[^|\n\r]* |"))
  11773. (c org-table-may-need-update))
  11774. (backward-delete-char N)
  11775. (skip-chars-forward "^|")
  11776. (insert " ")
  11777. (goto-char (1- pos))
  11778. ;; noalign: if there were two spaces at the end, this field
  11779. ;; does not determine the width of the column.
  11780. (if noalign (setq org-table-may-need-update c)))
  11781. (backward-delete-char N)
  11782. (org-fix-tags-on-the-fly)))
  11783. (defun org-delete-char (N)
  11784. "Like `delete-char', but insert whitespace at field end in tables.
  11785. When deleting characters, in tables this function will insert whitespace in
  11786. front of the next \"|\" separator, to keep the table aligned. The table will
  11787. still be marked for re-alignment if the field did fill the entire column,
  11788. because, in this case the deletion might narrow the column."
  11789. (interactive "p")
  11790. (if (and (org-table-p)
  11791. (not (bolp))
  11792. (not (= (char-after) ?|))
  11793. (eq N 1))
  11794. (if (looking-at ".*?|")
  11795. (let ((pos (point))
  11796. (noalign (looking-at "[^|\n\r]* |"))
  11797. (c org-table-may-need-update))
  11798. (replace-match (concat
  11799. (substring (match-string 0) 1 -1)
  11800. " |"))
  11801. (goto-char pos)
  11802. ;; noalign: if there were two spaces at the end, this field
  11803. ;; does not determine the width of the column.
  11804. (if noalign (setq org-table-may-need-update c)))
  11805. (delete-char N))
  11806. (delete-char N)
  11807. (org-fix-tags-on-the-fly)))
  11808. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11809. (put 'org-self-insert-command 'delete-selection t)
  11810. (put 'orgtbl-self-insert-command 'delete-selection t)
  11811. (put 'org-delete-char 'delete-selection 'supersede)
  11812. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11813. (put 'org-yank 'delete-selection 'yank)
  11814. ;; Make `flyspell-mode' delay after some commands
  11815. (put 'org-self-insert-command 'flyspell-delayed t)
  11816. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11817. (put 'org-delete-char 'flyspell-delayed t)
  11818. (put 'org-delete-backward-char 'flyspell-delayed t)
  11819. ;; Make pabbrev-mode expand after org-mode commands
  11820. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11821. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11822. ;; How to do this: Measure non-white length of current string
  11823. ;; If equal to column width, we should realign.
  11824. (defun org-remap (map &rest commands)
  11825. "In MAP, remap the functions given in COMMANDS.
  11826. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11827. (let (new old)
  11828. (while commands
  11829. (setq old (pop commands) new (pop commands))
  11830. (if (fboundp 'command-remapping)
  11831. (org-defkey map (vector 'remap old) new)
  11832. (substitute-key-definition old new map global-map)))))
  11833. (when (eq org-enable-table-editor 'optimized)
  11834. ;; If the user wants maximum table support, we need to hijack
  11835. ;; some standard editing functions
  11836. (org-remap org-mode-map
  11837. 'self-insert-command 'org-self-insert-command
  11838. 'delete-char 'org-delete-char
  11839. 'delete-backward-char 'org-delete-backward-char)
  11840. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11841. (defun org-shiftcursor-error ()
  11842. "Throw an error because Shift-Cursor command was applied in wrong context."
  11843. (error "This command is active in special context like tables, headlines or timestamps"))
  11844. (defun org-shifttab (&optional arg)
  11845. "Global visibility cycling or move to previous table field.
  11846. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11847. on context.
  11848. See the individual commands for more information."
  11849. (interactive "P")
  11850. (cond
  11851. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11852. ((integerp arg)
  11853. (message "Content view to level: %d" arg)
  11854. (org-content (prefix-numeric-value arg))
  11855. (setq org-cycle-global-status 'overview))
  11856. (t (call-interactively 'org-global-cycle))))
  11857. (defun org-shiftmetaleft ()
  11858. "Promote subtree or delete table column.
  11859. Calls `org-promote-subtree', `org-outdent-item',
  11860. or `org-table-delete-column', depending on context.
  11861. See the individual commands for more information."
  11862. (interactive)
  11863. (cond
  11864. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11865. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11866. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11867. (t (org-shiftcursor-error))))
  11868. (defun org-shiftmetaright ()
  11869. "Demote subtree or insert table column.
  11870. Calls `org-demote-subtree', `org-indent-item',
  11871. or `org-table-insert-column', depending on context.
  11872. See the individual commands for more information."
  11873. (interactive)
  11874. (cond
  11875. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11876. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11877. ((org-at-item-p) (call-interactively 'org-indent-item))
  11878. (t (org-shiftcursor-error))))
  11879. (defun org-shiftmetaup (&optional arg)
  11880. "Move subtree up or kill table row.
  11881. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11882. `org-move-item-up' depending on context. See the individual commands
  11883. for more information."
  11884. (interactive "P")
  11885. (cond
  11886. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11887. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11888. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11889. (t (org-shiftcursor-error))))
  11890. (defun org-shiftmetadown (&optional arg)
  11891. "Move subtree down or insert table row.
  11892. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11893. `org-move-item-down', depending on context. See the individual
  11894. commands for more information."
  11895. (interactive "P")
  11896. (cond
  11897. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11898. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11899. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11900. (t (org-shiftcursor-error))))
  11901. (defun org-metaleft (&optional arg)
  11902. "Promote heading or move table column to left.
  11903. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11904. With no specific context, calls the Emacs default `backward-word'.
  11905. See the individual commands for more information."
  11906. (interactive "P")
  11907. (cond
  11908. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11909. ((or (org-on-heading-p) (org-region-active-p))
  11910. (call-interactively 'org-do-promote))
  11911. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11912. (t (call-interactively 'backward-word))))
  11913. (defun org-metaright (&optional arg)
  11914. "Demote subtree or move table column to right.
  11915. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11916. With no specific context, calls the Emacs default `forward-word'.
  11917. See the individual commands for more information."
  11918. (interactive "P")
  11919. (cond
  11920. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11921. ((or (org-on-heading-p) (org-region-active-p))
  11922. (call-interactively 'org-do-demote))
  11923. ((org-at-item-p) (call-interactively 'org-indent-item))
  11924. (t (call-interactively 'forward-word))))
  11925. (defun org-metaup (&optional arg)
  11926. "Move subtree up or move table row up.
  11927. Calls `org-move-subtree-up' or `org-table-move-row' or
  11928. `org-move-item-up', depending on context. See the individual commands
  11929. for more information."
  11930. (interactive "P")
  11931. (cond
  11932. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11933. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11934. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11935. (t (transpose-lines 1) (beginning-of-line -1))))
  11936. (defun org-metadown (&optional arg)
  11937. "Move subtree down or move table row down.
  11938. Calls `org-move-subtree-down' or `org-table-move-row' or
  11939. `org-move-item-down', depending on context. See the individual
  11940. commands for more information."
  11941. (interactive "P")
  11942. (cond
  11943. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11944. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11945. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11946. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11947. (defun org-shiftup (&optional arg)
  11948. "Increase item in timestamp or increase priority of current headline.
  11949. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11950. depending on context. See the individual commands for more information."
  11951. (interactive "P")
  11952. (cond
  11953. ((and org-support-shift-select (org-region-active-p))
  11954. (let ((this-command-keys-shift-translated t))
  11955. (call-interactively 'previous-line)))
  11956. ((org-at-timestamp-p t)
  11957. (call-interactively (if org-edit-timestamp-down-means-later
  11958. 'org-timestamp-down 'org-timestamp-up)))
  11959. ((and (not (eq org-support-shift-select 'even-in-headlines))
  11960. (org-on-heading-p))
  11961. (call-interactively 'org-priority-up))
  11962. ((and (not org-support-shift-select) (org-at-item-p))
  11963. (call-interactively 'org-previous-item))
  11964. ((org-clocktable-try-shift 'up arg))
  11965. (org-support-shift-select
  11966. (let ((this-command-keys-shift-translated t))
  11967. (call-interactively 'previous-line)))
  11968. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11969. (defun org-shiftdown (&optional arg)
  11970. "Decrease item in timestamp or decrease priority of current headline.
  11971. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11972. depending on context. See the individual commands for more information."
  11973. (interactive "P")
  11974. (cond
  11975. ((and org-support-shift-select (org-region-active-p))
  11976. (let ((this-command-keys-shift-translated t))
  11977. (call-interactively 'next-line)))
  11978. ((org-at-timestamp-p t)
  11979. (call-interactively (if org-edit-timestamp-down-means-later
  11980. 'org-timestamp-up 'org-timestamp-down)))
  11981. ((and (not (eq org-support-shift-select 'even-in-headlines))
  11982. (org-on-heading-p))
  11983. (call-interactively 'org-priority-down))
  11984. ((and (not org-support-shift-select) (org-at-item-p))
  11985. (call-interactively 'org-next-item))
  11986. ((org-clocktable-try-shift 'down arg))
  11987. (org-support-shift-select
  11988. (let ((this-command-keys-shift-translated t))
  11989. (call-interactively 'next-line)))
  11990. (t (call-interactively 'org-next-item))))
  11991. (defun org-shiftright (&optional arg)
  11992. "Cycle the thing at point or in the current line, depending on context.
  11993. Depending on context, this does one of the following:
  11994. - switch a timestamp at point one day into the future
  11995. - on a headline, switch to the next TODO keyword.
  11996. - on an item, switch entire list to the next bullet type
  11997. - on a property line, switch to the next allowed value
  11998. - on a clocktable definition line, move time block into the future"
  11999. (interactive "P")
  12000. (cond
  12001. ((and org-support-shift-select (org-region-active-p))
  12002. (let ((this-command-keys-shift-translated t))
  12003. (call-interactively 'forward-char)))
  12004. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12005. ((and (not (eq org-support-shift-select 'even-in-headlines))
  12006. (org-on-heading-p))
  12007. (org-call-with-arg 'org-todo 'right))
  12008. ((or (and org-support-shift-select (org-at-item-bullet-p))
  12009. (and (not org-support-shift-select) (org-at-item-p)))
  12010. (org-call-with-arg 'org-cycle-list-bullet nil))
  12011. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  12012. ((org-clocktable-try-shift 'right arg))
  12013. (org-support-shift-select
  12014. (let ((this-command-keys-shift-translated t))
  12015. (call-interactively 'forward-char)))
  12016. (t (org-shiftcursor-error))))
  12017. (defun org-shiftleft (&optional arg)
  12018. "Cycle the thing at point or in the current line, depending on context.
  12019. Depending on context, this does one of the following:
  12020. - switch a timestamp at point one day into the past
  12021. - on a headline, switch to the previous TODO keyword.
  12022. - on an item, switch entire list to the previous bullet type
  12023. - on a property line, switch to the previous allowed value
  12024. - on a clocktable definition line, move time block into the past"
  12025. (interactive "P")
  12026. (cond
  12027. ((and org-support-shift-select (org-region-active-p))
  12028. (let ((this-command-keys-shift-translated t))
  12029. (call-interactively 'backward-char)))
  12030. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12031. ((and (not (eq org-support-shift-select 'even-in-headlines))
  12032. (org-on-heading-p))
  12033. (org-call-with-arg 'org-todo 'left))
  12034. ((or (and org-support-shift-select (org-at-item-bullet-p))
  12035. (and (not org-support-shift-select) (org-at-item-p)))
  12036. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12037. ((org-at-property-p)
  12038. (call-interactively 'org-property-previous-allowed-value))
  12039. ((org-clocktable-try-shift 'left arg))
  12040. (org-support-shift-select
  12041. (let ((this-command-keys-shift-translated t))
  12042. (call-interactively 'backward-char)))
  12043. (t (org-shiftcursor-error))))
  12044. (defun org-shiftcontrolright ()
  12045. "Switch to next TODO set."
  12046. (interactive)
  12047. (cond
  12048. ((and org-support-shift-select (org-region-active-p))
  12049. (let ((this-command-keys-shift-translated t))
  12050. (call-interactively 'forward-word)))
  12051. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  12052. (org-support-shift-select
  12053. (let ((this-command-keys-shift-translated t))
  12054. (call-interactively 'forward-word)))
  12055. (t (org-shiftcursor-error))))
  12056. (defun org-shiftcontrolleft ()
  12057. "Switch to previous TODO set."
  12058. (interactive)
  12059. (cond
  12060. ((and org-support-shift-select (org-region-active-p))
  12061. (let ((this-command-keys-shift-translated t))
  12062. (call-interactively 'backward-word)))
  12063. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  12064. (org-support-shift-select
  12065. (let ((this-command-keys-shift-translated t))
  12066. (call-interactively 'backward-word)))
  12067. (t (org-shiftcursor-error))))
  12068. (defun org-ctrl-c-ret ()
  12069. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12070. (interactive)
  12071. (cond
  12072. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12073. (t (call-interactively 'org-insert-heading))))
  12074. (defun org-copy-special ()
  12075. "Copy region in table or copy current subtree.
  12076. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12077. See the individual commands for more information."
  12078. (interactive)
  12079. (call-interactively
  12080. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12081. (defun org-cut-special ()
  12082. "Cut region in table or cut current subtree.
  12083. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12084. See the individual commands for more information."
  12085. (interactive)
  12086. (call-interactively
  12087. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12088. (defun org-paste-special (arg)
  12089. "Paste rectangular region into table, or past subtree relative to level.
  12090. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12091. See the individual commands for more information."
  12092. (interactive "P")
  12093. (if (org-at-table-p)
  12094. (org-table-paste-rectangle)
  12095. (org-paste-subtree arg)))
  12096. (defun org-edit-special ()
  12097. "Call a special editor for the stuff at point.
  12098. When at a table, call the formula editor with `org-table-edit-formulas'.
  12099. When at the first line of an src example, call `org-edit-src-code'.
  12100. When in an #+include line, visit the include file. Otherwise call
  12101. `ffap' to visit the file at point."
  12102. (interactive)
  12103. (cond
  12104. ((org-at-table-p)
  12105. (call-interactively 'org-table-edit-formulas))
  12106. ((save-excursion
  12107. (beginning-of-line 1)
  12108. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12109. (find-file (org-trim (match-string 1))))
  12110. ((org-edit-src-code))
  12111. ((org-edit-fixed-width-region))
  12112. (t (call-interactively 'ffap))))
  12113. (defun org-ctrl-c-ctrl-c (&optional arg)
  12114. "Set tags in headline, or update according to changed information at point.
  12115. This command does many different things, depending on context:
  12116. - If the cursor is in a headline, prompt for tags and insert them
  12117. into the current line, aligned to `org-tags-column'. When called
  12118. with prefix arg, realign all tags in the current buffer.
  12119. - If the cursor is in one of the special #+KEYWORD lines, this
  12120. triggers scanning the buffer for these lines and updating the
  12121. information.
  12122. - If the cursor is inside a table, realign the table. This command
  12123. works even if the automatic table editor has been turned off.
  12124. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12125. the entire table.
  12126. - If the cursor is at a footnote reference or definition, jump to
  12127. the corresponding definition or references, respectively.
  12128. - If the cursor is a the beginning of a dynamic block, update it.
  12129. - If the cursor is inside a table created by the table.el package,
  12130. activate that table.
  12131. - If the current buffer is a remember buffer, close note and file
  12132. it. A prefix argument of 1 files to the default location
  12133. without further interaction. A prefix argument of 2 files to
  12134. the currently clocking task.
  12135. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12136. links in this buffer.
  12137. - If the cursor is on a numbered item in a plain list, renumber the
  12138. ordered list.
  12139. - If the cursor is on a checkbox, toggle it."
  12140. (interactive "P")
  12141. (let ((org-enable-table-editor t))
  12142. (cond
  12143. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12144. org-occur-highlights
  12145. org-latex-fragment-image-overlays)
  12146. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12147. (org-remove-occur-highlights)
  12148. (org-remove-latex-fragment-image-overlays)
  12149. (message "Temporary highlights/overlays removed from current buffer"))
  12150. ((and (local-variable-p 'org-finish-function (current-buffer))
  12151. (fboundp org-finish-function))
  12152. (funcall org-finish-function))
  12153. ((org-at-property-p)
  12154. (call-interactively 'org-property-action))
  12155. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12156. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12157. ((org-at-table.el-p)
  12158. (require 'table)
  12159. (beginning-of-line 1)
  12160. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12161. (call-interactively 'table-recognize-table))
  12162. ((org-at-table-p)
  12163. (org-table-maybe-eval-formula)
  12164. (if arg
  12165. (call-interactively 'org-table-recalculate)
  12166. (org-table-maybe-recalculate-line))
  12167. (call-interactively 'org-table-align))
  12168. ((or (org-footnote-at-reference-p)
  12169. (org-footnote-at-definition-p))
  12170. (call-interactively 'org-footnote-action))
  12171. ((org-at-item-checkbox-p)
  12172. (call-interactively 'org-toggle-checkbox))
  12173. ((org-at-item-p)
  12174. (call-interactively 'org-maybe-renumber-ordered-list))
  12175. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12176. ;; Dynamic block
  12177. (beginning-of-line 1)
  12178. (save-excursion (org-update-dblock)))
  12179. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12180. (cond
  12181. ((equal (match-string 1) "TBLFM")
  12182. ;; Recalculate the table before this line
  12183. (save-excursion
  12184. (beginning-of-line 1)
  12185. (skip-chars-backward " \r\n\t")
  12186. (if (org-at-table-p)
  12187. (org-call-with-arg 'org-table-recalculate t))))
  12188. (t
  12189. ; (org-set-regexps-and-options)
  12190. ; (org-restart-font-lock)
  12191. (let ((org-inhibit-startup t)) (org-mode-restart))
  12192. (message "Local setup has been refreshed"))))
  12193. (t (error "C-c C-c can do nothing useful at this location.")))))
  12194. (defun org-mode-restart ()
  12195. "Restart Org-mode, to scan again for special lines.
  12196. Also updates the keyword regular expressions."
  12197. (interactive)
  12198. (org-mode)
  12199. (message "Org-mode restarted"))
  12200. (defun org-kill-note-or-show-branches ()
  12201. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12202. (interactive)
  12203. (if (not org-finish-function)
  12204. (call-interactively 'show-branches)
  12205. (let ((org-note-abort t))
  12206. (funcall org-finish-function))))
  12207. (defun org-return (&optional indent)
  12208. "Goto next table row or insert a newline.
  12209. Calls `org-table-next-row' or `newline', depending on context.
  12210. See the individual commands for more information."
  12211. (interactive)
  12212. (cond
  12213. ((bobp) (if indent (newline-and-indent) (newline)))
  12214. ((and (org-at-heading-p)
  12215. (looking-at
  12216. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12217. (org-show-entry)
  12218. (end-of-line 1)
  12219. (newline))
  12220. ((org-at-table-p)
  12221. (org-table-justify-field-maybe)
  12222. (call-interactively 'org-table-next-row))
  12223. (t (if indent (newline-and-indent) (newline)))))
  12224. (defun org-return-indent ()
  12225. "Goto next table row or insert a newline and indent.
  12226. Calls `org-table-next-row' or `newline-and-indent', depending on
  12227. context. See the individual commands for more information."
  12228. (interactive)
  12229. (org-return t))
  12230. (defun org-ctrl-c-star ()
  12231. "Compute table, or change heading status of lines.
  12232. Calls `org-table-recalculate' or `org-toggle-heading',
  12233. depending on context."
  12234. (interactive)
  12235. (cond
  12236. ((org-at-table-p)
  12237. (call-interactively 'org-table-recalculate))
  12238. (t
  12239. ;; Convert all lines in region to list items
  12240. (call-interactively 'org-toggle-heading))))
  12241. (defun org-ctrl-c-minus ()
  12242. "Insert separator line in table or modify bullet status of line.
  12243. Also turns a plain line or a region of lines into list items.
  12244. Calls `org-table-insert-hline', `org-toggle-item', or
  12245. `org-cycle-list-bullet', depending on context."
  12246. (interactive)
  12247. (cond
  12248. ((org-at-table-p)
  12249. (call-interactively 'org-table-insert-hline))
  12250. ((org-region-active-p)
  12251. (call-interactively 'org-toggle-item))
  12252. ((org-in-item-p)
  12253. (call-interactively 'org-cycle-list-bullet))
  12254. (t
  12255. (call-interactively 'org-toggle-item))))
  12256. (defun org-toggle-item ()
  12257. "Convert headings or normal lines to items, items to normal lines.
  12258. If there is no active region, only the current line is considered.
  12259. If the first line in the region is a headline, convert all headlines to items.
  12260. If the first line in the region is an item, convert all items to normal lines.
  12261. If the first line is normal text, add an item bullet to each line."
  12262. (interactive)
  12263. (let (l2 l beg end)
  12264. (if (org-region-active-p)
  12265. (setq beg (region-beginning) end (region-end))
  12266. (setq beg (point-at-bol)
  12267. end (min (1+ (point-at-eol)) (point-max))))
  12268. (save-excursion
  12269. (goto-char end)
  12270. (setq l2 (org-current-line))
  12271. (goto-char beg)
  12272. (beginning-of-line 1)
  12273. (setq l (1- (org-current-line)))
  12274. (if (org-at-item-p)
  12275. ;; We already have items, de-itemize
  12276. (while (< (setq l (1+ l)) l2)
  12277. (when (org-at-item-p)
  12278. (goto-char (match-beginning 2))
  12279. (delete-region (match-beginning 2) (match-end 2))
  12280. (and (looking-at "[ \t]+") (replace-match "")))
  12281. (beginning-of-line 2))
  12282. (if (org-on-heading-p)
  12283. ;; Headings, convert to items
  12284. (while (< (setq l (1+ l)) l2)
  12285. (if (looking-at org-outline-regexp)
  12286. (replace-match "- " t t))
  12287. (beginning-of-line 2))
  12288. ;; normal lines, turn them into items
  12289. (while (< (setq l (1+ l)) l2)
  12290. (unless (org-at-item-p)
  12291. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12292. (replace-match "\\1- \\2")))
  12293. (beginning-of-line 2)))))))
  12294. (defun org-toggle-heading (&optional nstars)
  12295. "Convert headings to normal text, or items or text to headings.
  12296. If there is no active region, only the current line is considered.
  12297. If the first line is a heading, remove the stars from all headlines
  12298. in the region.
  12299. If the first line is a plain list item, turn all plain list items into
  12300. headings.
  12301. If the first line is a normal line, turn each and every line in the region
  12302. into a heading.
  12303. When converting a line into a heading, the number of stars is chosen
  12304. such that the lines become children of the current entry. However, when
  12305. a prefix argument is given, its value determines the number of stars to add."
  12306. (interactive "P")
  12307. (let (l2 l itemp beg end)
  12308. (if (org-region-active-p)
  12309. (setq beg (region-beginning) end (region-end))
  12310. (setq beg (point-at-bol)
  12311. end (min (1+ (point-at-eol)) (point-max))))
  12312. (save-excursion
  12313. (goto-char end)
  12314. (setq l2 (org-current-line))
  12315. (goto-char beg)
  12316. (beginning-of-line 1)
  12317. (setq l (1- (org-current-line)))
  12318. (if (org-on-heading-p)
  12319. ;; We already have headlines, de-star them
  12320. (while (< (setq l (1+ l)) l2)
  12321. (when (org-on-heading-p t)
  12322. (and (looking-at outline-regexp) (replace-match "")))
  12323. (beginning-of-line 2))
  12324. (setq itemp (org-at-item-p))
  12325. (let* ((stars
  12326. (if nstars
  12327. (make-string (prefix-numeric-value current-prefix-arg)
  12328. ?*)
  12329. (save-excursion
  12330. (re-search-backward org-complex-heading-regexp nil t)
  12331. (or (match-string 1) "*"))))
  12332. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12333. (rpl (concat stars add-stars " ")))
  12334. (while (< (setq l (1+ l)) l2)
  12335. (if itemp
  12336. (and (org-at-item-p) (replace-match rpl t t))
  12337. (unless (org-on-heading-p)
  12338. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12339. (replace-match (concat rpl (match-string 2))))))
  12340. (beginning-of-line 2)))))))
  12341. (defun org-meta-return (&optional arg)
  12342. "Insert a new heading or wrap a region in a table.
  12343. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12344. See the individual commands for more information."
  12345. (interactive "P")
  12346. (cond
  12347. ((org-at-table-p)
  12348. (call-interactively 'org-table-wrap-region))
  12349. (t (call-interactively 'org-insert-heading))))
  12350. ;;; Menu entries
  12351. ;; Define the Org-mode menus
  12352. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12353. '("Tbl"
  12354. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12355. ["Next Field" org-cycle (org-at-table-p)]
  12356. ["Previous Field" org-shifttab (org-at-table-p)]
  12357. ["Next Row" org-return (org-at-table-p)]
  12358. "--"
  12359. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12360. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12361. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12362. "--"
  12363. ("Column"
  12364. ["Move Column Left" org-metaleft (org-at-table-p)]
  12365. ["Move Column Right" org-metaright (org-at-table-p)]
  12366. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12367. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12368. ("Row"
  12369. ["Move Row Up" org-metaup (org-at-table-p)]
  12370. ["Move Row Down" org-metadown (org-at-table-p)]
  12371. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12372. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12373. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12374. "--"
  12375. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12376. ("Rectangle"
  12377. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12378. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12379. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12380. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12381. "--"
  12382. ("Calculate"
  12383. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12384. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12385. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12386. "--"
  12387. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12388. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12389. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12390. "--"
  12391. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12392. "--"
  12393. ["Sum Column/Rectangle" org-table-sum
  12394. (or (org-at-table-p) (org-region-active-p))]
  12395. ["Which Column?" org-table-current-column (org-at-table-p)])
  12396. ["Debug Formulas"
  12397. org-table-toggle-formula-debugger
  12398. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12399. ["Show Col/Row Numbers"
  12400. org-table-toggle-coordinate-overlays
  12401. :style toggle
  12402. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12403. "--"
  12404. ["Create" org-table-create (and (not (org-at-table-p))
  12405. org-enable-table-editor)]
  12406. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12407. ["Import from File" org-table-import (not (org-at-table-p))]
  12408. ["Export to File" org-table-export (org-at-table-p)]
  12409. "--"
  12410. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12411. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12412. '("Org"
  12413. ("Show/Hide"
  12414. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12415. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12416. ["Sparse Tree..." org-sparse-tree t]
  12417. ["Reveal Context" org-reveal t]
  12418. ["Show All" show-all t]
  12419. "--"
  12420. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12421. "--"
  12422. ["New Heading" org-insert-heading t]
  12423. ("Navigate Headings"
  12424. ["Up" outline-up-heading t]
  12425. ["Next" outline-next-visible-heading t]
  12426. ["Previous" outline-previous-visible-heading t]
  12427. ["Next Same Level" outline-forward-same-level t]
  12428. ["Previous Same Level" outline-backward-same-level t]
  12429. "--"
  12430. ["Jump" org-goto t])
  12431. ("Edit Structure"
  12432. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12433. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12434. "--"
  12435. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12436. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12437. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12438. "--"
  12439. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12440. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12441. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12442. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12443. "--"
  12444. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12445. "--"
  12446. ["Convert to odd levels" org-convert-to-odd-levels t]
  12447. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12448. ("Editing"
  12449. ["Emphasis..." org-emphasize t]
  12450. ["Edit Source Example" org-edit-special t]
  12451. "--"
  12452. ["Footnote new/jump" org-footnote-action t]
  12453. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12454. ("Archive"
  12455. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12456. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12457. ; :active t :keys "C-u C-c C-x C-a"]
  12458. ["Sparse trees open ARCHIVE trees"
  12459. (setq org-sparse-tree-open-archived-trees
  12460. (not org-sparse-tree-open-archived-trees))
  12461. :style toggle :selected org-sparse-tree-open-archived-trees]
  12462. ["Cycling opens ARCHIVE trees"
  12463. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12464. :style toggle :selected org-cycle-open-archived-trees]
  12465. "--"
  12466. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12467. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12468. ; ["Check and Move Children" (org-archive-subtree '(4))
  12469. ; :active t :keys "C-u C-c C-x C-s"]
  12470. )
  12471. "--"
  12472. ("TODO Lists"
  12473. ["TODO/DONE/-" org-todo t]
  12474. ("Select keyword"
  12475. ["Next keyword" org-shiftright (org-on-heading-p)]
  12476. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12477. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12478. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12479. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12480. ["Show TODO Tree" org-show-todo-tree t]
  12481. ["Global TODO list" org-todo-list t]
  12482. "--"
  12483. ["Set Priority" org-priority t]
  12484. ["Priority Up" org-shiftup t]
  12485. ["Priority Down" org-shiftdown t])
  12486. ("TAGS and Properties"
  12487. ["Set Tags" org-set-tags-command t]
  12488. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12489. "--"
  12490. ["Set property" org-set-property t]
  12491. ["Column view of properties" org-columns t]
  12492. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12493. ("Dates and Scheduling"
  12494. ["Timestamp" org-time-stamp t]
  12495. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12496. ("Change Date"
  12497. ["1 Day Later" org-shiftright t]
  12498. ["1 Day Earlier" org-shiftleft t]
  12499. ["1 ... Later" org-shiftup t]
  12500. ["1 ... Earlier" org-shiftdown t])
  12501. ["Compute Time Range" org-evaluate-time-range t]
  12502. ["Schedule Item" org-schedule t]
  12503. ["Deadline" org-deadline t]
  12504. "--"
  12505. ["Custom time format" org-toggle-time-stamp-overlays
  12506. :style radio :selected org-display-custom-times]
  12507. "--"
  12508. ["Goto Calendar" org-goto-calendar t]
  12509. ["Date from Calendar" org-date-from-calendar t]
  12510. "--"
  12511. ["Start/Restart Timer" org-timer-start t]
  12512. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12513. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12514. ["Insert Timer String" org-timer t]
  12515. ["Insert Timer Item" org-timer-item t])
  12516. ("Logging work"
  12517. ["Clock in" org-clock-in t]
  12518. ["Clock out" org-clock-out t]
  12519. ["Clock cancel" org-clock-cancel t]
  12520. ["Goto running clock" org-clock-goto t]
  12521. ["Display times" org-clock-display t]
  12522. ["Create clock table" org-clock-report t]
  12523. "--"
  12524. ["Record DONE time"
  12525. (progn (setq org-log-done (not org-log-done))
  12526. (message "Switching to %s will %s record a timestamp"
  12527. (car org-done-keywords)
  12528. (if org-log-done "automatically" "not")))
  12529. :style toggle :selected org-log-done])
  12530. "--"
  12531. ["Agenda Command..." org-agenda t]
  12532. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12533. ("File List for Agenda")
  12534. ("Special views current file"
  12535. ["TODO Tree" org-show-todo-tree t]
  12536. ["Check Deadlines" org-check-deadlines t]
  12537. ["Timeline" org-timeline t]
  12538. ["Tags Tree" org-tags-sparse-tree t])
  12539. "--"
  12540. ("Hyperlinks"
  12541. ["Store Link (Global)" org-store-link t]
  12542. ["Insert Link" org-insert-link t]
  12543. ["Follow Link" org-open-at-point t]
  12544. "--"
  12545. ["Next link" org-next-link t]
  12546. ["Previous link" org-previous-link t]
  12547. "--"
  12548. ["Descriptive Links"
  12549. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12550. :style radio
  12551. :selected (member '(org-link) buffer-invisibility-spec)]
  12552. ["Literal Links"
  12553. (progn
  12554. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12555. :style radio
  12556. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12557. "--"
  12558. ["Export/Publish..." org-export t]
  12559. ("LaTeX"
  12560. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12561. :selected org-cdlatex-mode]
  12562. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12563. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12564. ["Modify math symbol" org-cdlatex-math-modify
  12565. (org-inside-LaTeX-fragment-p)]
  12566. ["Export LaTeX fragments as images"
  12567. (if (featurep 'org-exp)
  12568. (setq org-export-with-LaTeX-fragments
  12569. (not org-export-with-LaTeX-fragments))
  12570. (require 'org-exp))
  12571. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12572. org-export-with-LaTeX-fragments)])
  12573. "--"
  12574. ("Documentation"
  12575. ["Show Version" org-version t]
  12576. ["Info Documentation" org-info t])
  12577. ("Customize"
  12578. ["Browse Org Group" org-customize t]
  12579. "--"
  12580. ["Expand This Menu" org-create-customize-menu
  12581. (fboundp 'customize-menu-create)])
  12582. "--"
  12583. ["Refresh setup" org-mode-restart t]
  12584. ))
  12585. (defun org-info (&optional node)
  12586. "Read documentation for Org-mode in the info system.
  12587. With optional NODE, go directly to that node."
  12588. (interactive)
  12589. (info (format "(org)%s" (or node ""))))
  12590. (defun org-install-agenda-files-menu ()
  12591. (let ((bl (buffer-list)))
  12592. (save-excursion
  12593. (while bl
  12594. (set-buffer (pop bl))
  12595. (if (org-mode-p) (setq bl nil)))
  12596. (when (org-mode-p)
  12597. (easy-menu-change
  12598. '("Org") "File List for Agenda"
  12599. (append
  12600. (list
  12601. ["Edit File List" (org-edit-agenda-file-list) t]
  12602. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12603. ["Remove Current File from List" org-remove-file t]
  12604. ["Cycle through agenda files" org-cycle-agenda-files t]
  12605. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12606. "--")
  12607. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12608. ;;;; Documentation
  12609. ;;;###autoload
  12610. (defun org-require-autoloaded-modules ()
  12611. (interactive)
  12612. (mapc 'require
  12613. '(org-agenda org-archive org-clock org-colview
  12614. org-exp org-id org-export-latex org-publish
  12615. org-remember org-table)))
  12616. ;;;###autoload
  12617. (defun org-customize ()
  12618. "Call the customize function with org as argument."
  12619. (interactive)
  12620. (org-load-modules-maybe)
  12621. (org-require-autoloaded-modules)
  12622. (customize-browse 'org))
  12623. (defun org-create-customize-menu ()
  12624. "Create a full customization menu for Org-mode, insert it into the menu."
  12625. (interactive)
  12626. (org-load-modules-maybe)
  12627. (org-require-autoloaded-modules)
  12628. (if (fboundp 'customize-menu-create)
  12629. (progn
  12630. (easy-menu-change
  12631. '("Org") "Customize"
  12632. `(["Browse Org group" org-customize t]
  12633. "--"
  12634. ,(customize-menu-create 'org)
  12635. ["Set" Custom-set t]
  12636. ["Save" Custom-save t]
  12637. ["Reset to Current" Custom-reset-current t]
  12638. ["Reset to Saved" Custom-reset-saved t]
  12639. ["Reset to Standard Settings" Custom-reset-standard t]))
  12640. (message "\"Org\"-menu now contains full customization menu"))
  12641. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12642. ;;;; Miscellaneous stuff
  12643. ;;; Generally useful functions
  12644. (defun org-find-text-property-in-string (prop s)
  12645. "Return the first non-nil value of property PROP in string S."
  12646. (or (get-text-property 0 prop s)
  12647. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12648. prop s)))
  12649. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12650. "Display the given MESSAGE as a warning."
  12651. (if (fboundp 'display-warning)
  12652. (display-warning 'org message
  12653. (if (featurep 'xemacs)
  12654. 'warning
  12655. :warning))
  12656. (let ((buf (get-buffer-create "*Org warnings*")))
  12657. (with-current-buffer buf
  12658. (goto-char (point-max))
  12659. (insert "Warning (Org): " message)
  12660. (unless (bolp)
  12661. (newline)))
  12662. (display-buffer buf)
  12663. (sit-for 0))))
  12664. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12665. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12666. (if (and marker (marker-buffer marker)
  12667. (buffer-live-p (marker-buffer marker)))
  12668. (progn
  12669. (switch-to-buffer (marker-buffer marker))
  12670. (if (or (> marker (point-max)) (< marker (point-min)))
  12671. (widen))
  12672. (goto-char marker)
  12673. (org-show-context 'org-goto))
  12674. (if bookmark
  12675. (bookmark-jump bookmark)
  12676. (error "Cannot find location"))))
  12677. (defun org-quote-csv-field (s)
  12678. "Quote field for inclusion in CSV material."
  12679. (if (string-match "[\",]" s)
  12680. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12681. s))
  12682. (defun org-plist-delete (plist property)
  12683. "Delete PROPERTY from PLIST.
  12684. This is in contrast to merely setting it to 0."
  12685. (let (p)
  12686. (while plist
  12687. (if (not (eq property (car plist)))
  12688. (setq p (plist-put p (car plist) (nth 1 plist))))
  12689. (setq plist (cddr plist)))
  12690. p))
  12691. (defun org-force-self-insert (N)
  12692. "Needed to enforce self-insert under remapping."
  12693. (interactive "p")
  12694. (self-insert-command N))
  12695. (defun org-string-width (s)
  12696. "Compute width of string, ignoring invisible characters.
  12697. This ignores character with invisibility property `org-link', and also
  12698. characters with property `org-cwidth', because these will become invisible
  12699. upon the next fontification round."
  12700. (let (b l)
  12701. (when (or (eq t buffer-invisibility-spec)
  12702. (assq 'org-link buffer-invisibility-spec))
  12703. (while (setq b (text-property-any 0 (length s)
  12704. 'invisible 'org-link s))
  12705. (setq s (concat (substring s 0 b)
  12706. (substring s (or (next-single-property-change
  12707. b 'invisible s) (length s)))))))
  12708. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12709. (setq s (concat (substring s 0 b)
  12710. (substring s (or (next-single-property-change
  12711. b 'org-cwidth s) (length s))))))
  12712. (setq l (string-width s) b -1)
  12713. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12714. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12715. l))
  12716. (defun org-get-indentation (&optional line)
  12717. "Get the indentation of the current line, interpreting tabs.
  12718. When LINE is given, assume it represents a line and compute its indentation."
  12719. (if line
  12720. (if (string-match "^ *" (org-remove-tabs line))
  12721. (match-end 0))
  12722. (save-excursion
  12723. (beginning-of-line 1)
  12724. (skip-chars-forward " \t")
  12725. (current-column))))
  12726. (defun org-remove-tabs (s &optional width)
  12727. "Replace tabulators in S with spaces.
  12728. Assumes that s is a single line, starting in column 0."
  12729. (setq width (or width tab-width))
  12730. (while (string-match "\t" s)
  12731. (setq s (replace-match
  12732. (make-string
  12733. (- (* width (/ (+ (match-beginning 0) width) width))
  12734. (match-beginning 0)) ?\ )
  12735. t t s)))
  12736. s)
  12737. (defun org-fix-indentation (line ind)
  12738. "Fix indentation in LINE.
  12739. IND is a cons cell with target and minimum indentation.
  12740. If the current indentation in LINE is smaller than the minimum,
  12741. leave it alone. If it is larger than ind, set it to the target."
  12742. (let* ((l (org-remove-tabs line))
  12743. (i (org-get-indentation l))
  12744. (i1 (car ind)) (i2 (cdr ind)))
  12745. (if (>= i i2) (setq l (substring line i2)))
  12746. (if (> i1 0)
  12747. (concat (make-string i1 ?\ ) l)
  12748. l)))
  12749. (defun org-base-buffer (buffer)
  12750. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12751. (if (not buffer)
  12752. buffer
  12753. (or (buffer-base-buffer buffer)
  12754. buffer)))
  12755. (defun org-trim (s)
  12756. "Remove whitespace at beginning and end of string."
  12757. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12758. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12759. s)
  12760. (defun org-wrap (string &optional width lines)
  12761. "Wrap string to either a number of lines, or a width in characters.
  12762. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12763. that costs. If there is a word longer than WIDTH, the text is actually
  12764. wrapped to the length of that word.
  12765. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12766. many lines, whatever width that takes.
  12767. The return value is a list of lines, without newlines at the end."
  12768. (let* ((words (org-split-string string "[ \t\n]+"))
  12769. (maxword (apply 'max (mapcar 'org-string-width words)))
  12770. w ll)
  12771. (cond (width
  12772. (org-do-wrap words (max maxword width)))
  12773. (lines
  12774. (setq w maxword)
  12775. (setq ll (org-do-wrap words maxword))
  12776. (if (<= (length ll) lines)
  12777. ll
  12778. (setq ll words)
  12779. (while (> (length ll) lines)
  12780. (setq w (1+ w))
  12781. (setq ll (org-do-wrap words w)))
  12782. ll))
  12783. (t (error "Cannot wrap this")))))
  12784. (defun org-do-wrap (words width)
  12785. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12786. (let (lines line)
  12787. (while words
  12788. (setq line (pop words))
  12789. (while (and words (< (+ (length line) (length (car words))) width))
  12790. (setq line (concat line " " (pop words))))
  12791. (setq lines (push line lines)))
  12792. (nreverse lines)))
  12793. (defun org-split-string (string &optional separators)
  12794. "Splits STRING into substrings at SEPARATORS.
  12795. No empty strings are returned if there are matches at the beginning
  12796. and end of string."
  12797. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12798. (start 0)
  12799. notfirst
  12800. (list nil))
  12801. (while (and (string-match rexp string
  12802. (if (and notfirst
  12803. (= start (match-beginning 0))
  12804. (< start (length string)))
  12805. (1+ start) start))
  12806. (< (match-beginning 0) (length string)))
  12807. (setq notfirst t)
  12808. (or (eq (match-beginning 0) 0)
  12809. (and (eq (match-beginning 0) (match-end 0))
  12810. (eq (match-beginning 0) start))
  12811. (setq list
  12812. (cons (substring string start (match-beginning 0))
  12813. list)))
  12814. (setq start (match-end 0)))
  12815. (or (eq start (length string))
  12816. (setq list
  12817. (cons (substring string start)
  12818. list)))
  12819. (nreverse list)))
  12820. (defun org-context ()
  12821. "Return a list of contexts of the current cursor position.
  12822. If several contexts apply, all are returned.
  12823. Each context entry is a list with a symbol naming the context, and
  12824. two positions indicating start and end of the context. Possible
  12825. contexts are:
  12826. :headline anywhere in a headline
  12827. :headline-stars on the leading stars in a headline
  12828. :todo-keyword on a TODO keyword (including DONE) in a headline
  12829. :tags on the TAGS in a headline
  12830. :priority on the priority cookie in a headline
  12831. :item on the first line of a plain list item
  12832. :item-bullet on the bullet/number of a plain list item
  12833. :checkbox on the checkbox in a plain list item
  12834. :table in an org-mode table
  12835. :table-special on a special filed in a table
  12836. :table-table in a table.el table
  12837. :link on a hyperlink
  12838. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12839. :target on a <<target>>
  12840. :radio-target on a <<<radio-target>>>
  12841. :latex-fragment on a LaTeX fragment
  12842. :latex-preview on a LaTeX fragment with overlayed preview image
  12843. This function expects the position to be visible because it uses font-lock
  12844. faces as a help to recognize the following contexts: :table-special, :link,
  12845. and :keyword."
  12846. (let* ((f (get-text-property (point) 'face))
  12847. (faces (if (listp f) f (list f)))
  12848. (p (point)) clist o)
  12849. ;; First the large context
  12850. (cond
  12851. ((org-on-heading-p t)
  12852. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12853. (when (progn
  12854. (beginning-of-line 1)
  12855. (looking-at org-todo-line-tags-regexp))
  12856. (push (org-point-in-group p 1 :headline-stars) clist)
  12857. (push (org-point-in-group p 2 :todo-keyword) clist)
  12858. (push (org-point-in-group p 4 :tags) clist))
  12859. (goto-char p)
  12860. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12861. (if (looking-at "\\[#[A-Z0-9]\\]")
  12862. (push (org-point-in-group p 0 :priority) clist)))
  12863. ((org-at-item-p)
  12864. (push (org-point-in-group p 2 :item-bullet) clist)
  12865. (push (list :item (point-at-bol)
  12866. (save-excursion (org-end-of-item) (point)))
  12867. clist)
  12868. (and (org-at-item-checkbox-p)
  12869. (push (org-point-in-group p 0 :checkbox) clist)))
  12870. ((org-at-table-p)
  12871. (push (list :table (org-table-begin) (org-table-end)) clist)
  12872. (if (memq 'org-formula faces)
  12873. (push (list :table-special
  12874. (previous-single-property-change p 'face)
  12875. (next-single-property-change p 'face)) clist)))
  12876. ((org-at-table-p 'any)
  12877. (push (list :table-table) clist)))
  12878. (goto-char p)
  12879. ;; Now the small context
  12880. (cond
  12881. ((org-at-timestamp-p)
  12882. (push (org-point-in-group p 0 :timestamp) clist))
  12883. ((memq 'org-link faces)
  12884. (push (list :link
  12885. (previous-single-property-change p 'face)
  12886. (next-single-property-change p 'face)) clist))
  12887. ((memq 'org-special-keyword faces)
  12888. (push (list :keyword
  12889. (previous-single-property-change p 'face)
  12890. (next-single-property-change p 'face)) clist))
  12891. ((org-on-target-p)
  12892. (push (org-point-in-group p 0 :target) clist)
  12893. (goto-char (1- (match-beginning 0)))
  12894. (if (looking-at org-radio-target-regexp)
  12895. (push (org-point-in-group p 0 :radio-target) clist))
  12896. (goto-char p))
  12897. ((setq o (car (delq nil
  12898. (mapcar
  12899. (lambda (x)
  12900. (if (memq x org-latex-fragment-image-overlays) x))
  12901. (org-overlays-at (point))))))
  12902. (push (list :latex-fragment
  12903. (org-overlay-start o) (org-overlay-end o)) clist)
  12904. (push (list :latex-preview
  12905. (org-overlay-start o) (org-overlay-end o)) clist))
  12906. ((org-inside-LaTeX-fragment-p)
  12907. ;; FIXME: positions wrong.
  12908. (push (list :latex-fragment (point) (point)) clist)))
  12909. (setq clist (nreverse (delq nil clist)))
  12910. clist))
  12911. ;; FIXME: Compare with at-regexp-p Do we need both?
  12912. (defun org-in-regexp (re &optional nlines visually)
  12913. "Check if point is inside a match of regexp.
  12914. Normally only the current line is checked, but you can include NLINES extra
  12915. lines both before and after point into the search.
  12916. If VISUALLY is set, require that the cursor is not after the match but
  12917. really on, so that the block visually is on the match."
  12918. (catch 'exit
  12919. (let ((pos (point))
  12920. (eol (point-at-eol (+ 1 (or nlines 0))))
  12921. (inc (if visually 1 0)))
  12922. (save-excursion
  12923. (beginning-of-line (- 1 (or nlines 0)))
  12924. (while (re-search-forward re eol t)
  12925. (if (and (<= (match-beginning 0) pos)
  12926. (>= (+ inc (match-end 0)) pos))
  12927. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12928. (defun org-at-regexp-p (regexp)
  12929. "Is point inside a match of REGEXP in the current line?"
  12930. (catch 'exit
  12931. (save-excursion
  12932. (let ((pos (point)) (end (point-at-eol)))
  12933. (beginning-of-line 1)
  12934. (while (re-search-forward regexp end t)
  12935. (if (and (<= (match-beginning 0) pos)
  12936. (>= (match-end 0) pos))
  12937. (throw 'exit t)))
  12938. nil))))
  12939. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12940. "Call `multi-occur' with buffers for all agenda files."
  12941. (interactive "sOrg-files matching: \np")
  12942. (let* ((files (org-agenda-files))
  12943. (tnames (mapcar 'file-truename files))
  12944. (extra org-agenda-text-search-extra-files)
  12945. f)
  12946. (when (eq (car extra) 'agenda-archives)
  12947. (setq extra (cdr extra))
  12948. (setq files (org-add-archive-files files)))
  12949. (while (setq f (pop extra))
  12950. (unless (member (file-truename f) tnames)
  12951. (add-to-list 'files f 'append)
  12952. (add-to-list 'tnames (file-truename f) 'append)))
  12953. (multi-occur
  12954. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12955. regexp)))
  12956. (if (boundp 'occur-mode-find-occurrence-hook)
  12957. ;; Emacs 23
  12958. (add-hook 'occur-mode-find-occurrence-hook
  12959. (lambda ()
  12960. (when (org-mode-p)
  12961. (org-reveal))))
  12962. ;; Emacs 22
  12963. (defadvice occur-mode-goto-occurrence
  12964. (after org-occur-reveal activate)
  12965. (and (org-mode-p) (org-reveal)))
  12966. (defadvice occur-mode-goto-occurrence-other-window
  12967. (after org-occur-reveal activate)
  12968. (and (org-mode-p) (org-reveal)))
  12969. (defadvice occur-mode-display-occurrence
  12970. (after org-occur-reveal activate)
  12971. (when (org-mode-p)
  12972. (let ((pos (occur-mode-find-occurrence)))
  12973. (with-current-buffer (marker-buffer pos)
  12974. (save-excursion
  12975. (goto-char pos)
  12976. (org-reveal)))))))
  12977. (defun org-uniquify (list)
  12978. "Remove duplicate elements from LIST."
  12979. (let (res)
  12980. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12981. res))
  12982. (defun org-delete-all (elts list)
  12983. "Remove all elements in ELTS from LIST."
  12984. (while elts
  12985. (setq list (delete (pop elts) list)))
  12986. list)
  12987. (defun org-back-over-empty-lines ()
  12988. "Move backwards over whitespace, to the beginning of the first empty line.
  12989. Returns the number of empty lines passed."
  12990. (let ((pos (point)))
  12991. (skip-chars-backward " \t\n\r")
  12992. (beginning-of-line 2)
  12993. (goto-char (min (point) pos))
  12994. (count-lines (point) pos)))
  12995. (defun org-skip-whitespace ()
  12996. (skip-chars-forward " \t\n\r"))
  12997. (defun org-point-in-group (point group &optional context)
  12998. "Check if POINT is in match-group GROUP.
  12999. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13000. match. If the match group does ot exist or point is not inside it,
  13001. return nil."
  13002. (and (match-beginning group)
  13003. (>= point (match-beginning group))
  13004. (<= point (match-end group))
  13005. (if context
  13006. (list context (match-beginning group) (match-end group))
  13007. t)))
  13008. (defun org-switch-to-buffer-other-window (&rest args)
  13009. "Switch to buffer in a second window on the current frame.
  13010. In particular, do not allow pop-up frames."
  13011. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13012. special-display-function)
  13013. (apply 'switch-to-buffer-other-window args)))
  13014. (defun org-combine-plists (&rest plists)
  13015. "Create a single property list from all plists in PLISTS.
  13016. The process starts by copying the first list, and then setting properties
  13017. from the other lists. Settings in the last list are the most significant
  13018. ones and overrule settings in the other lists."
  13019. (let ((rtn (copy-sequence (pop plists)))
  13020. p v ls)
  13021. (while plists
  13022. (setq ls (pop plists))
  13023. (while ls
  13024. (setq p (pop ls) v (pop ls))
  13025. (setq rtn (plist-put rtn p v))))
  13026. rtn))
  13027. (defun org-move-line-down (arg)
  13028. "Move the current line down. With prefix argument, move it past ARG lines."
  13029. (interactive "p")
  13030. (let ((col (current-column))
  13031. beg end pos)
  13032. (beginning-of-line 1) (setq beg (point))
  13033. (beginning-of-line 2) (setq end (point))
  13034. (beginning-of-line (+ 1 arg))
  13035. (setq pos (move-marker (make-marker) (point)))
  13036. (insert (delete-and-extract-region beg end))
  13037. (goto-char pos)
  13038. (org-move-to-column col)))
  13039. (defun org-move-line-up (arg)
  13040. "Move the current line up. With prefix argument, move it past ARG lines."
  13041. (interactive "p")
  13042. (let ((col (current-column))
  13043. beg end pos)
  13044. (beginning-of-line 1) (setq beg (point))
  13045. (beginning-of-line 2) (setq end (point))
  13046. (beginning-of-line (- arg))
  13047. (setq pos (move-marker (make-marker) (point)))
  13048. (insert (delete-and-extract-region beg end))
  13049. (goto-char pos)
  13050. (org-move-to-column col)))
  13051. (defun org-replace-escapes (string table)
  13052. "Replace %-escapes in STRING with values in TABLE.
  13053. TABLE is an association list with keys like \"%a\" and string values.
  13054. The sequences in STRING may contain normal field width and padding information,
  13055. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13056. so values can contain further %-escapes if they are define later in TABLE."
  13057. (let ((case-fold-search nil)
  13058. e re rpl)
  13059. (while (setq e (pop table))
  13060. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13061. (while (string-match re string)
  13062. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13063. (cdr e)))
  13064. (setq string (replace-match rpl t t string))))
  13065. string))
  13066. (defun org-sublist (list start end)
  13067. "Return a section of LIST, from START to END.
  13068. Counting starts at 1."
  13069. (let (rtn (c start))
  13070. (setq list (nthcdr (1- start) list))
  13071. (while (and list (<= c end))
  13072. (push (pop list) rtn)
  13073. (setq c (1+ c)))
  13074. (nreverse rtn)))
  13075. (defun org-find-base-buffer-visiting (file)
  13076. "Like `find-buffer-visiting' but alway return the base buffer and
  13077. not an indirect buffer."
  13078. (let ((buf (find-buffer-visiting file)))
  13079. (if buf
  13080. (or (buffer-base-buffer buf) buf)
  13081. nil)))
  13082. (defun org-image-file-name-regexp (&optional extensions)
  13083. "Return regexp matching the file names of images.
  13084. If EXTENSIONS is given, only match these."
  13085. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13086. (image-file-name-regexp)
  13087. (let ((image-file-name-extensions
  13088. (or extensions
  13089. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13090. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13091. (concat "\\."
  13092. (regexp-opt (nconc (mapcar 'upcase
  13093. image-file-name-extensions)
  13094. image-file-name-extensions)
  13095. t)
  13096. "\\'"))))
  13097. (defun org-file-image-p (file &optional extensions)
  13098. "Return non-nil if FILE is an image."
  13099. (save-match-data
  13100. (string-match (org-image-file-name-regexp extensions) file)))
  13101. (defun org-get-cursor-date ()
  13102. "Return the date at cursor in as a time.
  13103. This works in the calendar and in the agenda, anywhere else it just
  13104. returns the current time."
  13105. (let (date day defd)
  13106. (cond
  13107. ((eq major-mode 'calendar-mode)
  13108. (setq date (calendar-cursor-to-date)
  13109. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13110. ((eq major-mode 'org-agenda-mode)
  13111. (setq day (get-text-property (point) 'day))
  13112. (if day
  13113. (setq date (calendar-gregorian-from-absolute day)
  13114. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13115. (nth 2 date))))))
  13116. (or defd (current-time))))
  13117. (defvar org-agenda-action-marker (make-marker)
  13118. "Marker pointing to the entry for the next agenda action.")
  13119. (defun org-mark-entry-for-agenda-action ()
  13120. "Mark the current entry as target of an agenda action.
  13121. Agenda actions are actions executed from the agenda with the key `k',
  13122. which make use of the date at the cursor."
  13123. (interactive)
  13124. (move-marker org-agenda-action-marker
  13125. (save-excursion (org-back-to-heading t) (point))
  13126. (current-buffer))
  13127. (message
  13128. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13129. ;;; Paragraph filling stuff.
  13130. ;; We want this to be just right, so use the full arsenal.
  13131. (defun org-indent-line-function ()
  13132. "Indent line like previous, but further if previous was headline or item."
  13133. (interactive)
  13134. (let* ((pos (point))
  13135. (itemp (org-at-item-p))
  13136. column bpos bcol tpos tcol bullet btype bullet-type)
  13137. ;; Find the previous relevant line
  13138. (beginning-of-line 1)
  13139. (cond
  13140. ((looking-at "#") (setq column 0))
  13141. ((looking-at "\\*+ ") (setq column 0))
  13142. (t
  13143. (beginning-of-line 0)
  13144. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13145. (beginning-of-line 0))
  13146. (cond
  13147. ((looking-at "\\*+[ \t]+")
  13148. (if (not org-adapt-indentation)
  13149. (setq column 0)
  13150. (goto-char (match-end 0))
  13151. (setq column (current-column))))
  13152. ((org-in-item-p)
  13153. (org-beginning-of-item)
  13154. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13155. (setq bpos (match-beginning 1) tpos (match-end 0)
  13156. bcol (progn (goto-char bpos) (current-column))
  13157. tcol (progn (goto-char tpos) (current-column))
  13158. bullet (match-string 1)
  13159. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13160. (if (> tcol (+ bcol org-description-max-indent))
  13161. (setq tcol (+ bcol 5)))
  13162. (if (not itemp)
  13163. (setq column tcol)
  13164. (goto-char pos)
  13165. (beginning-of-line 1)
  13166. (if (looking-at "\\S-")
  13167. (progn
  13168. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13169. (setq bullet (match-string 1)
  13170. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13171. (setq column (if (equal btype bullet-type) bcol tcol)))
  13172. (setq column (org-get-indentation)))))
  13173. (t (setq column (org-get-indentation))))))
  13174. (goto-char pos)
  13175. (if (<= (current-column) (current-indentation))
  13176. (org-indent-line-to column)
  13177. (save-excursion (org-indent-line-to column)))
  13178. (setq column (current-column))
  13179. (beginning-of-line 1)
  13180. (if (looking-at
  13181. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13182. (replace-match (concat "\\1" (format org-property-format
  13183. (match-string 2) (match-string 3)))
  13184. t nil))
  13185. (org-move-to-column column)))
  13186. (defun org-set-autofill-regexps ()
  13187. (interactive)
  13188. ;; In the paragraph separator we include headlines, because filling
  13189. ;; text in a line directly attached to a headline would otherwise
  13190. ;; fill the headline as well.
  13191. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13192. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13193. ;; The paragraph starter includes hand-formatted lists.
  13194. (org-set-local 'paragraph-start
  13195. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13196. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13197. ;; But only if the user has not turned off tables or fixed-width regions
  13198. (org-set-local
  13199. 'auto-fill-inhibit-regexp
  13200. (concat "\\*+ \\|#\\+"
  13201. "\\|[ \t]*" org-keyword-time-regexp
  13202. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13203. (concat
  13204. "\\|[ \t]*["
  13205. (if org-enable-table-editor "|" "")
  13206. (if org-enable-fixed-width-editor ":" "")
  13207. "]"))))
  13208. ;; We use our own fill-paragraph function, to make sure that tables
  13209. ;; and fixed-width regions are not wrapped. That function will pass
  13210. ;; through to `fill-paragraph' when appropriate.
  13211. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13212. ; Adaptive filling: To get full control, first make sure that
  13213. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13214. (org-set-local 'adaptive-fill-regexp "\000")
  13215. (org-set-local 'adaptive-fill-function
  13216. 'org-adaptive-fill-function)
  13217. (org-set-local
  13218. 'align-mode-rules-list
  13219. '((org-in-buffer-settings
  13220. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13221. (modes . '(org-mode))))))
  13222. (defun org-fill-paragraph (&optional justify)
  13223. "Re-align a table, pass through to fill-paragraph if no table."
  13224. (let ((table-p (org-at-table-p))
  13225. (table.el-p (org-at-table.el-p)))
  13226. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13227. (save-excursion (goto-char (point-at-bol))
  13228. (looking-at outline-regexp)))
  13229. t) ; skip headlines
  13230. (table.el-p t) ; skip table.el tables
  13231. (table-p (org-table-align) t) ; align org-mode tables
  13232. (t nil)))) ; call paragraph-fill
  13233. ;; For reference, this is the default value of adaptive-fill-regexp
  13234. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13235. (defun org-adaptive-fill-function ()
  13236. "Return a fill prefix for org-mode files.
  13237. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13238. work correctly."
  13239. (cond ((looking-at "#[ \t]+")
  13240. (match-string 0))
  13241. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13242. (save-excursion
  13243. (if (> (match-end 1) (+ (match-beginning 1)
  13244. org-description-max-indent))
  13245. (goto-char (+ (match-beginning 1) 5))
  13246. (goto-char (match-end 0)))
  13247. (make-string (current-column) ?\ )))
  13248. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13249. (save-excursion
  13250. (goto-char (match-end 0))
  13251. (make-string (current-column) ?\ )))
  13252. (t nil)))
  13253. ;;; Other stuff.
  13254. (defun org-toggle-fixed-width-section (arg)
  13255. "Toggle the fixed-width export.
  13256. If there is no active region, the QUOTE keyword at the current headline is
  13257. inserted or removed. When present, it causes the text between this headline
  13258. and the next to be exported as fixed-width text, and unmodified.
  13259. If there is an active region, this command adds or removes a colon as the
  13260. first character of this line. If the first character of a line is a colon,
  13261. this line is also exported in fixed-width font."
  13262. (interactive "P")
  13263. (let* ((cc 0)
  13264. (regionp (org-region-active-p))
  13265. (beg (if regionp (region-beginning) (point)))
  13266. (end (if regionp (region-end)))
  13267. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13268. (case-fold-search nil)
  13269. (re "[ \t]*\\(:\\)")
  13270. off)
  13271. (if regionp
  13272. (save-excursion
  13273. (goto-char beg)
  13274. (setq cc (current-column))
  13275. (beginning-of-line 1)
  13276. (setq off (looking-at re))
  13277. (while (> nlines 0)
  13278. (setq nlines (1- nlines))
  13279. (beginning-of-line 1)
  13280. (cond
  13281. (arg
  13282. (org-move-to-column cc t)
  13283. (insert ":\n")
  13284. (forward-line -1))
  13285. ((and off (looking-at re))
  13286. (replace-match "" t t nil 1))
  13287. ((not off) (org-move-to-column cc t) (insert ":")))
  13288. (forward-line 1)))
  13289. (save-excursion
  13290. (org-back-to-heading)
  13291. (if (looking-at (concat outline-regexp
  13292. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13293. (replace-match "" t t nil 1)
  13294. (if (looking-at outline-regexp)
  13295. (progn
  13296. (goto-char (match-end 0))
  13297. (insert org-quote-string " "))))))))
  13298. ;;;; Functions extending outline functionality
  13299. (defun org-beginning-of-line (&optional arg)
  13300. "Go to the beginning of the current line. If that is invisible, continue
  13301. to a visible line beginning. This makes the function of C-a more intuitive.
  13302. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13303. first attempt, and only move to after the tags when the cursor is already
  13304. beyond the end of the headline."
  13305. (interactive "P")
  13306. (let ((pos (point)) refpos)
  13307. (beginning-of-line 1)
  13308. (if (bobp)
  13309. nil
  13310. (backward-char 1)
  13311. (if (org-invisible-p)
  13312. (while (and (not (bobp)) (org-invisible-p))
  13313. (backward-char 1)
  13314. (beginning-of-line 1))
  13315. (forward-char 1)))
  13316. (when org-special-ctrl-a/e
  13317. (cond
  13318. ((and (looking-at org-complex-heading-regexp)
  13319. (= (char-after (match-end 1)) ?\ ))
  13320. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13321. (point-at-eol)))
  13322. (goto-char
  13323. (if (eq org-special-ctrl-a/e t)
  13324. (cond ((> pos refpos) refpos)
  13325. ((= pos (point)) refpos)
  13326. (t (point)))
  13327. (cond ((> pos (point)) (point))
  13328. ((not (eq last-command this-command)) (point))
  13329. (t refpos)))))
  13330. ((org-at-item-p)
  13331. (goto-char
  13332. (if (eq org-special-ctrl-a/e t)
  13333. (cond ((> pos (match-end 4)) (match-end 4))
  13334. ((= pos (point)) (match-end 4))
  13335. (t (point)))
  13336. (cond ((> pos (point)) (point))
  13337. ((not (eq last-command this-command)) (point))
  13338. (t (match-end 4))))))))
  13339. (org-no-warnings
  13340. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13341. (defun org-end-of-line (&optional arg)
  13342. "Go to the end of the line.
  13343. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13344. first attempt, and only move to after the tags when the cursor is already
  13345. beyond the end of the headline."
  13346. (interactive "P")
  13347. (if (or (not org-special-ctrl-a/e)
  13348. (not (org-on-heading-p)))
  13349. (end-of-line arg)
  13350. (let ((pos (point)))
  13351. (beginning-of-line 1)
  13352. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13353. (if (eq org-special-ctrl-a/e t)
  13354. (if (or (< pos (match-beginning 1))
  13355. (= pos (match-end 0)))
  13356. (goto-char (match-beginning 1))
  13357. (goto-char (match-end 0)))
  13358. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13359. (goto-char (match-end 0))
  13360. (goto-char (match-beginning 1))))
  13361. (end-of-line arg))))
  13362. (org-no-warnings
  13363. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13364. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13365. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13366. (defun org-kill-line (&optional arg)
  13367. "Kill line, to tags or end of line."
  13368. (interactive "P")
  13369. (cond
  13370. ((or (not org-special-ctrl-k)
  13371. (bolp)
  13372. (not (org-on-heading-p)))
  13373. (call-interactively 'kill-line))
  13374. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13375. (kill-region (point) (match-beginning 1))
  13376. (org-set-tags nil t))
  13377. (t (kill-region (point) (point-at-eol)))))
  13378. (define-key org-mode-map "\C-k" 'org-kill-line)
  13379. (defun org-yank (&optional arg)
  13380. "Yank. If the kill is a subtree, treat it specially.
  13381. This command will look at the current kill and check if is a single
  13382. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13383. cursor is at the beginning of a line or after the stars of a currently
  13384. empty headline, then the yank is handled specially. How exactly depends
  13385. on the value of the following variables, both set by default.
  13386. org-yank-folded-subtrees
  13387. When set, the subtree(s) will be folded after insertion, but only
  13388. if doing so would now swallow text after the yanked text.
  13389. org-yank-adjusted-subtrees
  13390. When set, the subtree will be promoted or demoted in order to
  13391. fit into the local outline tree structure, which means that the level
  13392. will be adjusted so that it becomes the smaller one of the two
  13393. *visible* surrounding headings.
  13394. Any prefix to this command will cause `yank' to be called directly with
  13395. no special treatment. In particular, a simple `C-u' prefix will just
  13396. plainly yank the text as it is.
  13397. \[1] Basically, the test checks if the first non-white line is a heading
  13398. and if there are no other headings with fewer stars."
  13399. (interactive "P")
  13400. (setq this-command 'yank)
  13401. (if arg
  13402. (call-interactively 'yank)
  13403. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13404. (and (org-kill-is-subtree-p)
  13405. (or (bolp)
  13406. (and (looking-at "[ \t]*$")
  13407. (string-match
  13408. "\\`\\*+\\'"
  13409. (buffer-substring (point-at-bol) (point)))))))
  13410. swallowp)
  13411. (cond
  13412. ((and subtreep org-yank-folded-subtrees)
  13413. (let ((beg (point))
  13414. end)
  13415. (if (and subtreep org-yank-adjusted-subtrees)
  13416. (org-paste-subtree nil nil 'for-yank)
  13417. (call-interactively 'yank))
  13418. (setq end (point))
  13419. (goto-char beg)
  13420. (when (and (bolp) subtreep
  13421. (not (setq swallowp
  13422. (org-yank-folding-would-swallow-text beg end))))
  13423. (or (looking-at outline-regexp)
  13424. (re-search-forward (concat "^" outline-regexp) end t))
  13425. (while (and (< (point) end) (looking-at outline-regexp))
  13426. (hide-subtree)
  13427. (org-cycle-show-empty-lines 'folded)
  13428. (condition-case nil
  13429. (outline-forward-same-level 1)
  13430. (error (goto-char end)))))
  13431. (when swallowp
  13432. (message
  13433. "Yanked text not folded because that would swallow text"))
  13434. (goto-char end)
  13435. (skip-chars-forward " \t\n\r")
  13436. (beginning-of-line 1)
  13437. (push-mark beg 'nomsg)))
  13438. ((and subtreep org-yank-adjusted-subtrees)
  13439. (let ((beg (point-at-bol)))
  13440. (org-paste-subtree nil nil 'for-yank)
  13441. (push-mark beg 'nomsg)))
  13442. (t
  13443. (call-interactively 'yank))))))
  13444. (defun org-yank-folding-would-swallow-text (beg end)
  13445. "Would hide-subtree at BEG swallow any text after END?"
  13446. (let (level)
  13447. (save-excursion
  13448. (goto-char beg)
  13449. (when (or (looking-at outline-regexp)
  13450. (re-search-forward (concat "^" outline-regexp) end t))
  13451. (setq level (org-outline-level)))
  13452. (goto-char end)
  13453. (skip-chars-forward " \t\r\n\v\f")
  13454. (if (or (eobp)
  13455. (and (bolp) (looking-at org-outline-regexp)
  13456. (<= (org-outline-level) level)))
  13457. nil ; Nothing would be swallowed
  13458. t)))) ; something would swallow
  13459. (define-key org-mode-map "\C-y" 'org-yank)
  13460. (defun org-invisible-p ()
  13461. "Check if point is at a character currently not visible."
  13462. ;; Early versions of noutline don't have `outline-invisible-p'.
  13463. (if (fboundp 'outline-invisible-p)
  13464. (outline-invisible-p)
  13465. (get-char-property (point) 'invisible)))
  13466. (defun org-invisible-p2 ()
  13467. "Check if point is at a character currently not visible."
  13468. (save-excursion
  13469. (if (and (eolp) (not (bobp))) (backward-char 1))
  13470. ;; Early versions of noutline don't have `outline-invisible-p'.
  13471. (if (fboundp 'outline-invisible-p)
  13472. (outline-invisible-p)
  13473. (get-char-property (point) 'invisible))))
  13474. (defun org-back-to-heading (&optional invisible-ok)
  13475. "Call `outline-back-to-heading', but provide a better error message."
  13476. (condition-case nil
  13477. (outline-back-to-heading invisible-ok)
  13478. (error (error "Before first headline at position %d in buffer %s"
  13479. (point) (current-buffer)))))
  13480. (defun org-before-first-heading-p ()
  13481. "Before first heading?"
  13482. (save-excursion
  13483. (null (re-search-backward "^\\*+ " nil t))))
  13484. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13485. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13486. (defun org-at-heading-or-item-p ()
  13487. (or (org-on-heading-p) (org-at-item-p)))
  13488. (defun org-on-target-p ()
  13489. (or (org-in-regexp org-radio-target-regexp)
  13490. (org-in-regexp org-target-regexp)))
  13491. (defun org-up-heading-all (arg)
  13492. "Move to the heading line of which the present line is a subheading.
  13493. This function considers both visible and invisible heading lines.
  13494. With argument, move up ARG levels."
  13495. (if (fboundp 'outline-up-heading-all)
  13496. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13497. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13498. (defun org-up-heading-safe ()
  13499. "Move to the heading line of which the present line is a subheading.
  13500. This version will not throw an error. It will return the level of the
  13501. headline found, or nil if no higher level is found."
  13502. (let (start-level re)
  13503. (org-back-to-heading t)
  13504. (setq start-level (funcall outline-level))
  13505. (if (equal start-level 1)
  13506. nil
  13507. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13508. (if (re-search-backward re nil t)
  13509. (funcall outline-level)))))
  13510. (defun org-first-sibling-p ()
  13511. "Is this heading the first child of its parents?"
  13512. (interactive)
  13513. (let ((re (concat "^" outline-regexp))
  13514. level l)
  13515. (unless (org-at-heading-p t)
  13516. (error "Not at a heading"))
  13517. (setq level (funcall outline-level))
  13518. (save-excursion
  13519. (if (not (re-search-backward re nil t))
  13520. t
  13521. (setq l (funcall outline-level))
  13522. (< l level)))))
  13523. (defun org-goto-sibling (&optional previous)
  13524. "Goto the next sibling, even if it is invisible.
  13525. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13526. when a sibling was found. When none is found, return nil and don't
  13527. move point."
  13528. (let ((fun (if previous 're-search-backward 're-search-forward))
  13529. (pos (point))
  13530. (re (concat "^" outline-regexp))
  13531. level l)
  13532. (when (condition-case nil (org-back-to-heading t) (error nil))
  13533. (setq level (funcall outline-level))
  13534. (catch 'exit
  13535. (or previous (forward-char 1))
  13536. (while (funcall fun re nil t)
  13537. (setq l (funcall outline-level))
  13538. (when (< l level) (goto-char pos) (throw 'exit nil))
  13539. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13540. (goto-char pos)
  13541. nil))))
  13542. (defun org-show-siblings ()
  13543. "Show all siblings of the current headline."
  13544. (save-excursion
  13545. (while (org-goto-sibling) (org-flag-heading nil)))
  13546. (save-excursion
  13547. (while (org-goto-sibling 'previous)
  13548. (org-flag-heading nil))))
  13549. (defun org-show-hidden-entry ()
  13550. "Show an entry where even the heading is hidden."
  13551. (save-excursion
  13552. (org-show-entry)))
  13553. (defun org-flag-heading (flag &optional entry)
  13554. "Flag the current heading. FLAG non-nil means make invisible.
  13555. When ENTRY is non-nil, show the entire entry."
  13556. (save-excursion
  13557. (org-back-to-heading t)
  13558. ;; Check if we should show the entire entry
  13559. (if entry
  13560. (progn
  13561. (org-show-entry)
  13562. (save-excursion
  13563. (and (outline-next-heading)
  13564. (org-flag-heading nil))))
  13565. (outline-flag-region (max (point-min) (1- (point)))
  13566. (save-excursion (outline-end-of-heading) (point))
  13567. flag))))
  13568. (defun org-forward-same-level (arg)
  13569. "Move forward to the ARG'th subheading at same level as this one.
  13570. Stop at the first and last subheadings of a superior heading.
  13571. This is like outline-forward-same-level, but invisible headings are ok."
  13572. (interactive "p")
  13573. (org-back-to-heading t)
  13574. (while (> arg 0)
  13575. (let ((point-to-move-to (save-excursion
  13576. (org-get-next-sibling))))
  13577. (if point-to-move-to
  13578. (progn
  13579. (goto-char point-to-move-to)
  13580. (setq arg (1- arg)))
  13581. (progn
  13582. (setq arg 0)
  13583. (error "No following same-level heading"))))))
  13584. (defun org-get-next-sibling ()
  13585. "Move to next heading of the same level, and return point.
  13586. If there is no such heading, return nil.
  13587. This is like outline-next-sibling, but invisible headings are ok."
  13588. (let ((level (funcall outline-level)))
  13589. (outline-next-heading)
  13590. (while (and (not (eobp)) (> (funcall outline-level) level))
  13591. (outline-next-heading))
  13592. (if (or (eobp) (< (funcall outline-level) level))
  13593. nil
  13594. (point))))
  13595. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13596. ;; This is an exact copy of the original function, but it uses
  13597. ;; `org-back-to-heading', to make it work also in invisible
  13598. ;; trees. And is uses an invisible-OK argument.
  13599. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13600. (org-back-to-heading invisible-OK)
  13601. (let ((first t)
  13602. (level (funcall outline-level)))
  13603. (while (and (not (eobp))
  13604. (or first (> (funcall outline-level) level)))
  13605. (setq first nil)
  13606. (outline-next-heading))
  13607. (unless to-heading
  13608. (if (memq (preceding-char) '(?\n ?\^M))
  13609. (progn
  13610. ;; Go to end of line before heading
  13611. (forward-char -1)
  13612. (if (memq (preceding-char) '(?\n ?\^M))
  13613. ;; leave blank line before heading
  13614. (forward-char -1))))))
  13615. (point))
  13616. (defun org-show-subtree ()
  13617. "Show everything after this heading at deeper levels."
  13618. (outline-flag-region
  13619. (point)
  13620. (save-excursion
  13621. (outline-end-of-subtree) (outline-next-heading) (point))
  13622. nil))
  13623. (defun org-show-entry ()
  13624. "Show the body directly following this heading.
  13625. Show the heading too, if it is currently invisible."
  13626. (interactive)
  13627. (save-excursion
  13628. (condition-case nil
  13629. (progn
  13630. (org-back-to-heading t)
  13631. (outline-flag-region
  13632. (max (point-min) (1- (point)))
  13633. (save-excursion
  13634. (re-search-forward
  13635. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13636. (or (match-beginning 1) (point-max)))
  13637. nil))
  13638. (error nil))))
  13639. (defun org-make-options-regexp (kwds)
  13640. "Make a regular expression for keyword lines."
  13641. (concat
  13642. "^"
  13643. "#?[ \t]*\\+\\("
  13644. (mapconcat 'regexp-quote kwds "\\|")
  13645. "\\):[ \t]*"
  13646. "\\(.+\\)"))
  13647. ;; Make isearch reveal the necessary context
  13648. (defun org-isearch-end ()
  13649. "Reveal context after isearch exits."
  13650. (when isearch-success ; only if search was successful
  13651. (if (featurep 'xemacs)
  13652. ;; Under XEmacs, the hook is run in the correct place,
  13653. ;; we directly show the context.
  13654. (org-show-context 'isearch)
  13655. ;; In Emacs the hook runs *before* restoring the overlays.
  13656. ;; So we have to use a one-time post-command-hook to do this.
  13657. ;; (Emacs 22 has a special variable, see function `org-mode')
  13658. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13659. isearch-mode-end-hook-quit)
  13660. ;; Only when the isearch was not quitted.
  13661. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13662. 'append 'local)))))
  13663. (defun org-isearch-post-command ()
  13664. "Remove self from hook, and show context."
  13665. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13666. (org-show-context 'isearch))
  13667. ;;;; Integration with and fixes for other packages
  13668. ;;; Imenu support
  13669. (defvar org-imenu-markers nil
  13670. "All markers currently used by Imenu.")
  13671. (make-variable-buffer-local 'org-imenu-markers)
  13672. (defun org-imenu-new-marker (&optional pos)
  13673. "Return a new marker for use by Imenu, and remember the marker."
  13674. (let ((m (make-marker)))
  13675. (move-marker m (or pos (point)))
  13676. (push m org-imenu-markers)
  13677. m))
  13678. (defun org-imenu-get-tree ()
  13679. "Produce the index for Imenu."
  13680. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13681. (setq org-imenu-markers nil)
  13682. (let* ((n org-imenu-depth)
  13683. (re (concat "^" outline-regexp))
  13684. (subs (make-vector (1+ n) nil))
  13685. (last-level 0)
  13686. m level head)
  13687. (save-excursion
  13688. (save-restriction
  13689. (widen)
  13690. (goto-char (point-max))
  13691. (while (re-search-backward re nil t)
  13692. (setq level (org-reduced-level (funcall outline-level)))
  13693. (when (<= level n)
  13694. (looking-at org-complex-heading-regexp)
  13695. (setq head (org-link-display-format
  13696. (org-match-string-no-properties 4))
  13697. m (org-imenu-new-marker))
  13698. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13699. (if (>= level last-level)
  13700. (push (cons head m) (aref subs level))
  13701. (push (cons head (aref subs (1+ level))) (aref subs level))
  13702. (loop for i from (1+ level) to n do (aset subs i nil)))
  13703. (setq last-level level)))))
  13704. (aref subs 1)))
  13705. (eval-after-load "imenu"
  13706. '(progn
  13707. (add-hook 'imenu-after-jump-hook
  13708. (lambda ()
  13709. (if (eq major-mode 'org-mode)
  13710. (org-show-context 'org-goto))))))
  13711. (defun org-link-display-format (link)
  13712. "Replace a link with either the description, or the link target
  13713. if no description is present"
  13714. (save-match-data
  13715. (if (string-match org-bracket-link-analytic-regexp link)
  13716. (replace-match (or (match-string 5 link)
  13717. (concat (match-string 1 link)
  13718. (match-string 3 link)))
  13719. nil nil link)
  13720. link)))
  13721. ;; Speedbar support
  13722. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13723. "Overlay marking the agenda restriction line in speedbar.")
  13724. (org-overlay-put org-speedbar-restriction-lock-overlay
  13725. 'face 'org-agenda-restriction-lock)
  13726. (org-overlay-put org-speedbar-restriction-lock-overlay
  13727. 'help-echo "Agendas are currently limited to this item.")
  13728. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13729. (defun org-speedbar-set-agenda-restriction ()
  13730. "Restrict future agenda commands to the location at point in speedbar.
  13731. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13732. (interactive)
  13733. (require 'org-agenda)
  13734. (let (p m tp np dir txt)
  13735. (cond
  13736. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13737. 'org-imenu t))
  13738. (setq m (get-text-property p 'org-imenu-marker))
  13739. (save-excursion
  13740. (save-restriction
  13741. (set-buffer (marker-buffer m))
  13742. (goto-char m)
  13743. (org-agenda-set-restriction-lock 'subtree))))
  13744. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13745. 'speedbar-function 'speedbar-find-file))
  13746. (setq tp (previous-single-property-change
  13747. (1+ p) 'speedbar-function)
  13748. np (next-single-property-change
  13749. tp 'speedbar-function)
  13750. dir (speedbar-line-directory)
  13751. txt (buffer-substring-no-properties (or tp (point-min))
  13752. (or np (point-max))))
  13753. (save-excursion
  13754. (save-restriction
  13755. (set-buffer (find-file-noselect
  13756. (let ((default-directory dir))
  13757. (expand-file-name txt))))
  13758. (unless (org-mode-p)
  13759. (error "Cannot restrict to non-Org-mode file"))
  13760. (org-agenda-set-restriction-lock 'file))))
  13761. (t (error "Don't know how to restrict Org-mode's agenda")))
  13762. (org-move-overlay org-speedbar-restriction-lock-overlay
  13763. (point-at-bol) (point-at-eol))
  13764. (setq current-prefix-arg nil)
  13765. (org-agenda-maybe-redo)))
  13766. (eval-after-load "speedbar"
  13767. '(progn
  13768. (speedbar-add-supported-extension ".org")
  13769. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13770. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13771. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13772. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13773. (add-hook 'speedbar-visiting-tag-hook
  13774. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13775. ;;; Fixes and Hacks for problems with other packages
  13776. ;; Make flyspell not check words in links, to not mess up our keymap
  13777. (defun org-mode-flyspell-verify ()
  13778. "Don't let flyspell put overlays at active buttons."
  13779. (not (get-text-property (point) 'keymap)))
  13780. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13781. (eval-after-load "bookmark"
  13782. '(if (boundp 'bookmark-after-jump-hook)
  13783. ;; We can use the hook
  13784. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13785. ;; Hook not available, use advice
  13786. (defadvice bookmark-jump (after org-make-visible activate)
  13787. "Make the position visible."
  13788. (org-bookmark-jump-unhide))))
  13789. ;; Make sure saveplace show the location if it was hidden
  13790. (eval-after-load "saveplace"
  13791. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13792. "Make the position visible."
  13793. (org-bookmark-jump-unhide)))
  13794. (defun org-bookmark-jump-unhide ()
  13795. "Unhide the current position, to show the bookmark location."
  13796. (and (org-mode-p)
  13797. (or (org-invisible-p)
  13798. (save-excursion (goto-char (max (point-min) (1- (point))))
  13799. (org-invisible-p)))
  13800. (org-show-context 'bookmark-jump)))
  13801. ;; Make session.el ignore our circular variable
  13802. (eval-after-load "session"
  13803. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13804. ;;;; Experimental code
  13805. (defun org-closed-in-range ()
  13806. "Sparse tree of items closed in a certain time range.
  13807. Still experimental, may disappear in the future."
  13808. (interactive)
  13809. ;; Get the time interval from the user.
  13810. (let* ((time1 (time-to-seconds
  13811. (org-read-date nil 'to-time nil "Starting date: ")))
  13812. (time2 (time-to-seconds
  13813. (org-read-date nil 'to-time nil "End date:")))
  13814. ;; callback function
  13815. (callback (lambda ()
  13816. (let ((time
  13817. (time-to-seconds
  13818. (apply 'encode-time
  13819. (org-parse-time-string
  13820. (match-string 1))))))
  13821. ;; check if time in interval
  13822. (and (>= time time1) (<= time time2))))))
  13823. ;; make tree, check each match with the callback
  13824. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13825. ;;;; Finish up
  13826. (provide 'org)
  13827. (run-hooks 'org-load-hook)
  13828. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13829. ;;; org.el ends here