org.el 565 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209
  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.20f
  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.20f"
  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 nil
  180. "Non-nil means, make shift-cursor commands select text when possible.
  181. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  182. selecting a region, or enlarge thusly regions started in this way.
  183. In Org-mode, in special contexts, these same keys are used for other
  184. purposes, important enough to compete with shift selection. Org tries
  185. to balance these needs by supporting `shift-select-mode' outside these
  186. special contexts, under control of this variable.
  187. The default of this variable is nil, to avoid confusing behavior. Shifted
  188. cursor keys will then execute Org commands in the following contexts:
  189. - on a headline, changing TODO state (left/right) and priority (up/down)
  190. - on a time stamp, changing the time
  191. - in a plain list item, changing the bullet type
  192. - in a property definition line, switching between allowed values
  193. - in the BEGIN line of a clock table (changing the time block).
  194. Outside these contexts, the commands will throw an error.
  195. When this variable is t and the cursor is not in a special context,
  196. Org-mode will support shift-selection for making and enlarging regions.
  197. To make this more effective, the bullet cycling will no longer happen
  198. anywhere in an item line, but only if the cursor is exactly on the bullet.
  199. If you set this variable to the symbol `always', then the keys
  200. will not be special in headlines, property lines, and item lines, to make
  201. shift selection work there as well. If this is what you want, you can
  202. use the following alternative commands: `C-c C-t' and `C-c ,' to
  203. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  204. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  205. edited by hand or in column view.
  206. However, when the cursor is on a timestamp, shift-cursor commands
  207. will still edit the time stamp - this is just too good to give up.
  208. XEmacs user should have this variable set to nil, because shift-select-mode
  209. is Emacs 23 only."
  210. :group 'org
  211. :type '(choice
  212. (const :tag "Never" nil)
  213. (const :tag "When outside special context" t)
  214. (const :tag "Everywhere except timestamps" always)))
  215. (defgroup org-startup nil
  216. "Options concerning startup of Org-mode."
  217. :tag "Org Startup"
  218. :group 'org)
  219. (defcustom org-startup-folded t
  220. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  221. This can also be configured on a per-file basis by adding one of
  222. the following lines anywhere in the buffer:
  223. #+STARTUP: fold
  224. #+STARTUP: nofold
  225. #+STARTUP: content"
  226. :group 'org-startup
  227. :type '(choice
  228. (const :tag "nofold: show all" nil)
  229. (const :tag "fold: overview" t)
  230. (const :tag "content: all headlines" content)))
  231. (defcustom org-startup-truncated t
  232. "Non-nil means, entering Org-mode will set `truncate-lines'.
  233. This is useful since some lines containing links can be very long and
  234. uninteresting. Also tables look terrible when wrapped."
  235. :group 'org-startup
  236. :type 'boolean)
  237. (defcustom org-startup-align-all-tables nil
  238. "Non-nil means, align all tables when visiting a file.
  239. This is useful when the column width in tables is forced with <N> cookies
  240. in table fields. Such tables will look correct only after the first re-align.
  241. This can also be configured on a per-file basis by adding one of
  242. the following lines anywhere in the buffer:
  243. #+STARTUP: align
  244. #+STARTUP: noalign"
  245. :group 'org-startup
  246. :type 'boolean)
  247. (defcustom org-insert-mode-line-in-empty-file nil
  248. "Non-nil means insert the first line setting Org-mode in empty files.
  249. When the function `org-mode' is called interactively in an empty file, this
  250. normally means that the file name does not automatically trigger Org-mode.
  251. To ensure that the file will always be in Org-mode in the future, a
  252. line enforcing Org-mode will be inserted into the buffer, if this option
  253. has been set."
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-replace-disputed-keys nil
  257. "Non-nil means use alternative key bindings for some keys.
  258. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  259. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  260. If you want to use Org-mode together with one of these other modes,
  261. or more generally if you would like to move some Org-mode commands to
  262. other keys, set this variable and configure the keys with the variable
  263. `org-disputed-keys'.
  264. This option is only relevant at load-time of Org-mode, and must be set
  265. *before* org.el is loaded. Changing it requires a restart of Emacs to
  266. become effective."
  267. :group 'org-startup
  268. :type 'boolean)
  269. (defcustom org-use-extra-keys nil
  270. "Non-nil means use extra key sequence definitions for certain
  271. commands. This happens automatically if you run XEmacs or if
  272. window-system is nil. This variable lets you do the same
  273. manually. You must set it before loading org.
  274. Example: on Carbon Emacs 22 running graphically, with an external
  275. keyboard on a Powerbook, the default way of setting M-left might
  276. not work for either Alt or ESC. Setting this variable will make
  277. it work for ESC."
  278. :group 'org-startup
  279. :type 'boolean)
  280. (if (fboundp 'defvaralias)
  281. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  282. (defcustom org-disputed-keys
  283. '(([(shift up)] . [(meta p)])
  284. ([(shift down)] . [(meta n)])
  285. ([(shift left)] . [(meta -)])
  286. ([(shift right)] . [(meta +)])
  287. ([(control shift right)] . [(meta shift +)])
  288. ([(control shift left)] . [(meta shift -)]))
  289. "Keys for which Org-mode and other modes compete.
  290. This is an alist, cars are the default keys, second element specifies
  291. the alternative to use when `org-replace-disputed-keys' is t.
  292. Keys can be specified in any syntax supported by `define-key'.
  293. The value of this option takes effect only at Org-mode's startup,
  294. therefore you'll have to restart Emacs to apply it after changing."
  295. :group 'org-startup
  296. :type 'alist)
  297. (defun org-key (key)
  298. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  299. Or return the original if not disputed."
  300. (if org-replace-disputed-keys
  301. (let* ((nkey (key-description key))
  302. (x (org-find-if (lambda (x)
  303. (equal (key-description (car x)) nkey))
  304. org-disputed-keys)))
  305. (if x (cdr x) key))
  306. key))
  307. (defun org-find-if (predicate seq)
  308. (catch 'exit
  309. (while seq
  310. (if (funcall predicate (car seq))
  311. (throw 'exit (car seq))
  312. (pop seq)))))
  313. (defun org-defkey (keymap key def)
  314. "Define a key, possibly translated, as returned by `org-key'."
  315. (define-key keymap (org-key key) def))
  316. (defcustom org-ellipsis nil
  317. "The ellipsis to use in the Org-mode outline.
  318. When nil, just use the standard three dots. When a string, use that instead,
  319. When a face, use the standard 3 dots, but with the specified face.
  320. The change affects only Org-mode (which will then use its own display table).
  321. Changing this requires executing `M-x org-mode' in a buffer to become
  322. effective."
  323. :group 'org-startup
  324. :type '(choice (const :tag "Default" nil)
  325. (face :tag "Face" :value org-warning)
  326. (string :tag "String" :value "...#")))
  327. (defvar org-display-table nil
  328. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  329. (defgroup org-keywords nil
  330. "Keywords in Org-mode."
  331. :tag "Org Keywords"
  332. :group 'org)
  333. (defcustom org-deadline-string "DEADLINE:"
  334. "String to mark deadline entries.
  335. A deadline is this string, followed by a time stamp. Should be a word,
  336. terminated by a colon. You can insert a schedule keyword and
  337. a timestamp with \\[org-deadline].
  338. Changes become only effective after restarting Emacs."
  339. :group 'org-keywords
  340. :type 'string)
  341. (defcustom org-scheduled-string "SCHEDULED:"
  342. "String to mark scheduled TODO entries.
  343. A schedule is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-schedule].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-closed-string "CLOSED:"
  350. "String used as the prefix for timestamps logging closing a TODO entry."
  351. :group 'org-keywords
  352. :type 'string)
  353. (defcustom org-clock-string "CLOCK:"
  354. "String used as prefix for timestamps clocking work hours on an item."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-comment-string "COMMENT"
  358. "Entries starting with this keyword will never be exported.
  359. An entry can be toggled between COMMENT and normal with
  360. \\[org-toggle-comment].
  361. Changes become only effective after restarting Emacs."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-quote-string "QUOTE"
  365. "Entries starting with this keyword will be exported in fixed-width font.
  366. Quoting applies only to the text in the entry following the headline, and does
  367. not extend beyond the next headline, even if that is lower level.
  368. An entry can be toggled between QUOTE and normal with
  369. \\[org-toggle-fixed-width-section]."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defconst org-repeat-re
  373. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  374. "Regular expression for specifying repeated events.
  375. After a match, group 1 contains the repeat expression.")
  376. (defgroup org-structure nil
  377. "Options concerning the general structure of Org-mode files."
  378. :tag "Org Structure"
  379. :group 'org)
  380. (defgroup org-reveal-location nil
  381. "Options about how to make context of a location visible."
  382. :tag "Org Reveal Location"
  383. :group 'org-structure)
  384. (defconst org-context-choice
  385. '(choice
  386. (const :tag "Always" t)
  387. (const :tag "Never" nil)
  388. (repeat :greedy t :tag "Individual contexts"
  389. (cons
  390. (choice :tag "Context"
  391. (const agenda)
  392. (const org-goto)
  393. (const occur-tree)
  394. (const tags-tree)
  395. (const link-search)
  396. (const mark-goto)
  397. (const bookmark-jump)
  398. (const isearch)
  399. (const default))
  400. (boolean))))
  401. "Contexts for the reveal options.")
  402. (defcustom org-show-hierarchy-above '((default . t))
  403. "Non-nil means, show full hierarchy when revealing a location.
  404. Org-mode often shows locations in an org-mode file which might have
  405. been invisible before. When this is set, the hierarchy of headings
  406. above the exposed location is shown.
  407. Turning this off for example for sparse trees makes them very compact.
  408. Instead of t, this can also be an alist specifying this option for different
  409. contexts. Valid contexts are
  410. agenda when exposing an entry from the agenda
  411. org-goto when using the command `org-goto' on key C-c C-j
  412. occur-tree when using the command `org-occur' on key C-c /
  413. tags-tree when constructing a sparse tree based on tags matches
  414. link-search when exposing search matches associated with a link
  415. mark-goto when exposing the jump goal of a mark
  416. bookmark-jump when exposing a bookmark location
  417. isearch when exiting from an incremental search
  418. default default for all contexts not set explicitly"
  419. :group 'org-reveal-location
  420. :type org-context-choice)
  421. (defcustom org-show-following-heading '((default . nil))
  422. "Non-nil means, show following heading when revealing a location.
  423. Org-mode often shows locations in an org-mode file which might have
  424. been invisible before. When this is set, the heading following the
  425. match is shown.
  426. Turning this off for example for sparse trees makes them very compact,
  427. but makes it harder to edit the location of the match. In such a case,
  428. use the command \\[org-reveal] to show more context.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. See `org-show-hierarchy-above' for valid contexts."
  431. :group 'org-reveal-location
  432. :type org-context-choice)
  433. (defcustom org-show-siblings '((default . nil) (isearch t))
  434. "Non-nil means, show all sibling heading when revealing a location.
  435. Org-mode often shows locations in an org-mode file which might have
  436. been invisible before. When this is set, the sibling of the current entry
  437. heading are all made visible. If `org-show-hierarchy-above' is t,
  438. the same happens on each level of the hierarchy above the current entry.
  439. By default this is on for the isearch context, off for all other contexts.
  440. Turning this off for example for sparse trees makes them very compact,
  441. but makes it harder to edit the location of the match. In such a case,
  442. use the command \\[org-reveal] to show more context.
  443. Instead of t, this can also be an alist specifying this option for different
  444. contexts. See `org-show-hierarchy-above' for valid contexts."
  445. :group 'org-reveal-location
  446. :type org-context-choice)
  447. (defcustom org-show-entry-below '((default . nil))
  448. "Non-nil means, show the entry below a headline when revealing a location.
  449. Org-mode often shows locations in an org-mode file which might have
  450. been invisible before. When this is set, the text below the headline that is
  451. exposed is also shown.
  452. By default this is off for all contexts.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-indirect-buffer-display 'other-window
  458. "How should indirect tree buffers be displayed?
  459. This applies to indirect buffers created with the commands
  460. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  461. Valid values are:
  462. current-window Display in the current window
  463. other-window Just display in another window.
  464. dedicated-frame Create one new frame, and re-use it each time.
  465. new-frame Make a new frame each time. Note that in this case
  466. previously-made indirect buffers are kept, and you need to
  467. kill these buffers yourself."
  468. :group 'org-structure
  469. :group 'org-agenda-windows
  470. :type '(choice
  471. (const :tag "In current window" current-window)
  472. (const :tag "In current frame, other window" other-window)
  473. (const :tag "Each time a new frame" new-frame)
  474. (const :tag "One dedicated frame" dedicated-frame)))
  475. (defgroup org-cycle nil
  476. "Options concerning visibility cycling in Org-mode."
  477. :tag "Org Cycle"
  478. :group 'org-structure)
  479. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  480. "Names of drawers. Drawers are not opened by cycling on the headline above.
  481. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  482. this:
  483. :DRAWERNAME:
  484. .....
  485. :END:
  486. The drawer \"PROPERTIES\" is special for capturing properties through
  487. the property API.
  488. Drawers can be defined on the per-file basis with a line like:
  489. #+DRAWERS: HIDDEN STATE PROPERTIES"
  490. :group 'org-structure
  491. :type '(repeat (string :tag "Drawer Name")))
  492. (defcustom org-cycle-global-at-bob nil
  493. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  494. This makes it possible to do global cycling without having to use S-TAB or
  495. C-u TAB. For this special case to work, the first line of the buffer
  496. must not be a headline - it may be empty ot some other text. When used in
  497. this way, `org-cycle-hook' is disables temporarily, to make sure the
  498. cursor stays at the beginning of the buffer.
  499. When this option is nil, don't do anything special at the beginning
  500. of the buffer."
  501. :group 'org-cycle
  502. :type 'boolean)
  503. (defcustom org-cycle-emulate-tab t
  504. "Where should `org-cycle' emulate TAB.
  505. nil Never
  506. white Only in completely white lines
  507. whitestart Only at the beginning of lines, before the first non-white char
  508. t Everywhere except in headlines
  509. exc-hl-bol Everywhere except at the start of a headline
  510. If TAB is used in a place where it does not emulate TAB, the current subtree
  511. visibility is cycled."
  512. :group 'org-cycle
  513. :type '(choice (const :tag "Never" nil)
  514. (const :tag "Only in completely white lines" white)
  515. (const :tag "Before first char in a line" whitestart)
  516. (const :tag "Everywhere except in headlines" t)
  517. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  518. ))
  519. (defcustom org-cycle-separator-lines 2
  520. "Number of empty lines needed to keep an empty line between collapsed trees.
  521. If you leave an empty line between the end of a subtree and the following
  522. headline, this empty line is hidden when the subtree is folded.
  523. Org-mode will leave (exactly) one empty line visible if the number of
  524. empty lines is equal or larger to the number given in this variable.
  525. So the default 2 means, at least 2 empty lines after the end of a subtree
  526. are needed to produce free space between a collapsed subtree and the
  527. following headline.
  528. Special case: when 0, never leave empty lines in collapsed view."
  529. :group 'org-cycle
  530. :type 'integer)
  531. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  532. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  533. org-cycle-hide-drawers
  534. org-cycle-show-empty-lines
  535. org-optimize-window-after-visibility-change)
  536. "Hook that is run after `org-cycle' has changed the buffer visibility.
  537. The function(s) in this hook must accept a single argument which indicates
  538. the new state that was set by the most recent `org-cycle' command. The
  539. argument is a symbol. After a global state change, it can have the values
  540. `overview', `content', or `all'. After a local state change, it can have
  541. the values `folded', `children', or `subtree'."
  542. :group 'org-cycle
  543. :type 'hook)
  544. (defgroup org-edit-structure nil
  545. "Options concerning structure editing in Org-mode."
  546. :tag "Org Edit Structure"
  547. :group 'org-structure)
  548. (defcustom org-odd-levels-only nil
  549. "Non-nil means, skip even levels and only use odd levels for the outline.
  550. This has the effect that two stars are being added/taken away in
  551. promotion/demotion commands. It also influences how levels are
  552. handled by the exporters.
  553. Changing it requires restart of `font-lock-mode' to become effective
  554. for fontification also in regions already fontified.
  555. You may also set this on a per-file basis by adding one of the following
  556. lines to the buffer:
  557. #+STARTUP: odd
  558. #+STARTUP: oddeven"
  559. :group 'org-edit-structure
  560. :group 'org-font-lock
  561. :type 'boolean)
  562. (defcustom org-adapt-indentation t
  563. "Non-nil means, adapt indentation when promoting and demoting.
  564. When this is set and the *entire* text in an entry is indented, the
  565. indentation is increased by one space in a demotion command, and
  566. decreased by one in a promotion command. If any line in the entry
  567. body starts at column 0, indentation is not changed at all."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-special-ctrl-a/e nil
  571. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  572. When t, `C-a' will bring back the cursor to the beginning of the
  573. headline text, i.e. after the stars and after a possible TODO keyword.
  574. In an item, this will be the position after the bullet.
  575. When the cursor is already at that position, another `C-a' will bring
  576. it to the beginning of the line.
  577. `C-e' will jump to the end of the headline, ignoring the presence of tags
  578. in the headline. A second `C-e' will then jump to the true end of the
  579. line, after any tags.
  580. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  581. and only a directly following, identical keypress will bring the cursor
  582. to the special positions."
  583. :group 'org-edit-structure
  584. :type '(choice
  585. (const :tag "off" nil)
  586. (const :tag "after bullet first" t)
  587. (const :tag "border first" reversed)))
  588. (if (fboundp 'defvaralias)
  589. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  590. (defcustom org-special-ctrl-k nil
  591. "Non-nil means `C-k' will behave specially in headlines.
  592. When nil, `C-k' will call the default `kill-line' command.
  593. When t, the following will happen while the cursor is in the headline:
  594. - When the cursor is at the beginning of a headline, kill the entire
  595. line and possible the folded subtree below the line.
  596. - When in the middle of the headline text, kill the headline up to the tags.
  597. - When after the headline text, kill the tags."
  598. :group 'org-edit-structure
  599. :type 'boolean)
  600. (defcustom org-yank-folded-subtrees t
  601. "Non-nil means, when yanking subtrees, fold them.
  602. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  603. it starts with a heading and all other headings in it are either children
  604. or siblings, then fold all the subtrees. However, do this only if no
  605. text after the yank would be swallowed into a folded tree by this action."
  606. :group 'org-edit-structure
  607. :type 'boolean)
  608. (defcustom org-yank-adjusted-subtrees t
  609. "Non-nil means, when yanking subtrees, adjust the level.
  610. With this setting, `org-paste-subtree' is used to insert the subtree, see
  611. this function for details."
  612. :group 'org-edit-structure
  613. :type 'boolean)
  614. (defcustom org-M-RET-may-split-line '((default . t))
  615. "Non-nil means, M-RET will split the line at the cursor position.
  616. When nil, it will go to the end of the line before making a
  617. new line.
  618. You may also set this option in a different way for different
  619. contexts. Valid contexts are:
  620. headline when creating a new headline
  621. item when creating a new item
  622. table in a table field
  623. default the value to be used for all contexts not explicitly
  624. customized"
  625. :group 'org-structure
  626. :group 'org-table
  627. :type '(choice
  628. (const :tag "Always" t)
  629. (const :tag "Never" nil)
  630. (repeat :greedy t :tag "Individual contexts"
  631. (cons
  632. (choice :tag "Context"
  633. (const headline)
  634. (const item)
  635. (const table)
  636. (const default))
  637. (boolean)))))
  638. (defcustom org-insert-heading-respect-content nil
  639. "Non-nil means, insert new headings after the current subtree.
  640. When nil, the new heading is created directly after the current line.
  641. The commands \\[org-insert-heading-respect-content] and
  642. \\[org-insert-todo-heading-respect-content] turn this variable on
  643. for the duration of the command."
  644. :group 'org-structure
  645. :type 'boolean)
  646. (defcustom org-blank-before-new-entry '((heading . auto)
  647. (plain-list-item . auto))
  648. "Should `org-insert-heading' leave a blank line before new heading/item?
  649. The value is an alist, with `heading' and `plain-list-item' as car,
  650. and a boolean flag as cdr."
  651. :group 'org-edit-structure
  652. :type '(list
  653. (cons (const heading)
  654. (choice (const :tag "Never" nil)
  655. (const :tag "Always" t)
  656. (const :tag "Auto" auto)))
  657. (cons (const plain-list-item)
  658. (choice (const :tag "Never" nil)
  659. (const :tag "Always" t)
  660. (const :tag "Auto" auto)))))
  661. (defcustom org-insert-heading-hook nil
  662. "Hook being run after inserting a new heading."
  663. :group 'org-edit-structure
  664. :type 'hook)
  665. (defcustom org-enable-fixed-width-editor t
  666. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  667. This currently only means, they are never auto-wrapped.
  668. When nil, such lines will be treated like ordinary lines.
  669. See also the QUOTE keyword."
  670. :group 'org-edit-structure
  671. :type 'boolean)
  672. (defcustom org-edit-src-region-extra nil
  673. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  674. For examples see the function `org-edit-src-find-region-and-lang'.
  675. The regular expression identifying the begin marker should end with a newline,
  676. and the regexp marking the end line should start with a newline, to make sure
  677. there are kept outside the narrowed region."
  678. :group 'org-edit-structure
  679. :type '(repeat
  680. (list
  681. (regexp :tag "begin regexp")
  682. (regexp :tag "end regexp")
  683. (choice :tag "language"
  684. (string :tag "specify")
  685. (integer :tag "from match group")
  686. (const :tag "from `lang' element")
  687. (const :tag "from `style' element")))))
  688. (defcustom org-coderef-label-format "(ref:%s)"
  689. "The default coderef format.
  690. This format string will be used to search for coderef labels in literal
  691. examples (EXAMPLE and SRC blocks). The format can be overwritten
  692. an individual literal example with the -f option, like
  693. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  694. ...
  695. #+END_SRC
  696. If you want to use this for HTML export, make sure that the format does
  697. not introduce special font-locking, and avoid the HTML special
  698. characters `<', `>', and `&'. The reason for this restriction is that
  699. the labels are searched for only after htmlize has done its job."
  700. :group 'org-edit-structure ; FIXME this is not in the right group
  701. :type 'string)
  702. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  703. "The mode that should be used to edit fixed-width regions.
  704. These are the regions where each line starts with a colon."
  705. :group 'org-edit-structure
  706. :type '(choice
  707. (const artist-mode)
  708. (const picture-mode)
  709. (const fundamental-mode)
  710. (function :tag "Other (specify)")))
  711. (defcustom org-goto-auto-isearch t
  712. "Non-nil means, typing characters in org-goto starts incremental search."
  713. :group 'org-edit-structure
  714. :type 'boolean)
  715. (defgroup org-sparse-trees nil
  716. "Options concerning sparse trees in Org-mode."
  717. :tag "Org Sparse Trees"
  718. :group 'org-structure)
  719. (defcustom org-highlight-sparse-tree-matches t
  720. "Non-nil means, highlight all matches that define a sparse tree.
  721. The highlights will automatically disappear the next time the buffer is
  722. changed by an edit command."
  723. :group 'org-sparse-trees
  724. :type 'boolean)
  725. (defcustom org-remove-highlights-with-change t
  726. "Non-nil means, any change to the buffer will remove temporary highlights.
  727. Such highlights are created by `org-occur' and `org-clock-display'.
  728. When nil, `C-c C-c needs to be used to get rid of the highlights.
  729. The highlights created by `org-preview-latex-fragment' always need
  730. `C-c C-c' to be removed."
  731. :group 'org-sparse-trees
  732. :group 'org-time
  733. :type 'boolean)
  734. (defcustom org-occur-hook '(org-first-headline-recenter)
  735. "Hook that is run after `org-occur' has constructed a sparse tree.
  736. This can be used to recenter the window to show as much of the structure
  737. as possible."
  738. :group 'org-sparse-trees
  739. :type 'hook)
  740. (defgroup org-imenu-and-speedbar nil
  741. "Options concerning imenu and speedbar in Org-mode."
  742. :tag "Org Imenu and Speedbar"
  743. :group 'org-structure)
  744. (defcustom org-imenu-depth 2
  745. "The maximum level for Imenu access to Org-mode headlines.
  746. This also applied for speedbar access."
  747. :group 'org-imenu-and-speedbar
  748. :type 'number)
  749. (defgroup org-table nil
  750. "Options concerning tables in Org-mode."
  751. :tag "Org Table"
  752. :group 'org)
  753. (defcustom org-enable-table-editor 'optimized
  754. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  755. When nil, such lines will be treated like ordinary lines.
  756. When equal to the symbol `optimized', the table editor will be optimized to
  757. do the following:
  758. - Automatic overwrite mode in front of whitespace in table fields.
  759. This makes the structure of the table stay in tact as long as the edited
  760. field does not exceed the column width.
  761. - Minimize the number of realigns. Normally, the table is aligned each time
  762. TAB or RET are pressed to move to another field. With optimization this
  763. happens only if changes to a field might have changed the column width.
  764. Optimization requires replacing the functions `self-insert-command',
  765. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  766. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  767. very good at guessing when a re-align will be necessary, but you can always
  768. force one with \\[org-ctrl-c-ctrl-c].
  769. If you would like to use the optimized version in Org-mode, but the
  770. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  771. This variable can be used to turn on and off the table editor during a session,
  772. but in order to toggle optimization, a restart is required.
  773. See also the variable `org-table-auto-blank-field'."
  774. :group 'org-table
  775. :type '(choice
  776. (const :tag "off" nil)
  777. (const :tag "on" t)
  778. (const :tag "on, optimized" optimized)))
  779. (defcustom org-table-tab-recognizes-table.el t
  780. "Non-nil means, TAB will automatically notice a table.el table.
  781. When it sees such a table, it moves point into it and - if necessary -
  782. calls `table-recognize-table'."
  783. :group 'org-table-editing
  784. :type 'boolean)
  785. (defgroup org-link nil
  786. "Options concerning links in Org-mode."
  787. :tag "Org Link"
  788. :group 'org)
  789. (defvar org-link-abbrev-alist-local nil
  790. "Buffer-local version of `org-link-abbrev-alist', which see.
  791. The value of this is taken from the #+LINK lines.")
  792. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  793. (defcustom org-link-abbrev-alist nil
  794. "Alist of link abbreviations.
  795. The car of each element is a string, to be replaced at the start of a link.
  796. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  797. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  798. [[linkkey:tag][description]]
  799. If REPLACE is a string, the tag will simply be appended to create the link.
  800. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  801. the placeholder \"%h\" will cause a url-encoded version of the tag to
  802. be inserted at that point (see the function `url-hexify-string').
  803. REPLACE may also be a function that will be called with the tag as the
  804. only argument to create the link, which should be returned as a string.
  805. See the manual for examples."
  806. :group 'org-link
  807. :type '(repeat
  808. (cons
  809. (string :tag "Protocol")
  810. (choice
  811. (string :tag "Format")
  812. (function)))))
  813. (defcustom org-descriptive-links t
  814. "Non-nil means, hide link part and only show description of bracket links.
  815. Bracket links are like [[link][description]]. This variable sets the initial
  816. state in new org-mode buffers. The setting can then be toggled on a
  817. per-buffer basis from the Org->Hyperlinks menu."
  818. :group 'org-link
  819. :type 'boolean)
  820. (defcustom org-link-file-path-type 'adaptive
  821. "How the path name in file links should be stored.
  822. Valid values are:
  823. relative Relative to the current directory, i.e. the directory of the file
  824. into which the link is being inserted.
  825. absolute Absolute path, if possible with ~ for home directory.
  826. noabbrev Absolute path, no abbreviation of home directory.
  827. adaptive Use relative path for files in the current directory and sub-
  828. directories of it. For other files, use an absolute path."
  829. :group 'org-link
  830. :type '(choice
  831. (const relative)
  832. (const absolute)
  833. (const noabbrev)
  834. (const adaptive)))
  835. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  836. "Types of links that should be activated in Org-mode files.
  837. This is a list of symbols, each leading to the activation of a certain link
  838. type. In principle, it does not hurt to turn on most link types - there may
  839. be a small gain when turning off unused link types. The types are:
  840. bracket The recommended [[link][description]] or [[link]] links with hiding.
  841. angular Links in angular brackets that may contain whitespace like
  842. <bbdb:Carsten Dominik>.
  843. plain Plain links in normal text, no whitespace, like http://google.com.
  844. radio Text that is matched by a radio target, see manual for details.
  845. tag Tag settings in a headline (link to tag search).
  846. date Time stamps (link to calendar).
  847. footnote Footnote labels.
  848. Changing this variable requires a restart of Emacs to become effective."
  849. :group 'org-link
  850. :type '(set :greedy t
  851. (const :tag "Double bracket links (new style)" bracket)
  852. (const :tag "Angular bracket links (old style)" angular)
  853. (const :tag "Plain text links" plain)
  854. (const :tag "Radio target matches" radio)
  855. (const :tag "Tags" tag)
  856. (const :tag "Timestamps" date)
  857. (const :tag "Footnotes" footnote)))
  858. (defcustom org-make-link-description-function nil
  859. "Function to use to generate link descriptions from links. If
  860. nil the link location will be used. This function must take two
  861. parameters; the first is the link and the second the description
  862. org-insert-link has generated, and should return the description
  863. to use."
  864. :group 'org-link
  865. :type 'function)
  866. (defgroup org-link-store nil
  867. "Options concerning storing links in Org-mode."
  868. :tag "Org Store Link"
  869. :group 'org-link)
  870. (defcustom org-email-link-description-format "Email %c: %.30s"
  871. "Format of the description part of a link to an email or usenet message.
  872. The following %-escapes will be replaced by corresponding information:
  873. %F full \"From\" field
  874. %f name, taken from \"From\" field, address if no name
  875. %T full \"To\" field
  876. %t first name in \"To\" field, address if no name
  877. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  878. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  879. %s subject
  880. %m message-id.
  881. You may use normal field width specification between the % and the letter.
  882. This is for example useful to limit the length of the subject.
  883. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  884. :group 'org-link-store
  885. :type 'string)
  886. (defcustom org-from-is-user-regexp
  887. (let (r1 r2)
  888. (when (and user-mail-address (not (string= user-mail-address "")))
  889. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  890. (when (and user-full-name (not (string= user-full-name "")))
  891. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  892. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  893. "Regexp matched against the \"From:\" header of an email or usenet message.
  894. It should match if the message is from the user him/herself."
  895. :group 'org-link-store
  896. :type 'regexp)
  897. (defcustom org-link-to-org-use-id 'create-if-interactive
  898. "Non-nil means, storing a link to an Org file will use entry IDs.
  899. Note that before this variable is even considered, org-id must be loaded,
  900. to please customize `org-modules' and turn it on.
  901. The variable can have the following values:
  902. t Create an ID if needed to make a link to the current entry.
  903. create-if-interactive
  904. If `org-store-link' is called directly (interactively, as a user
  905. command), do create an ID to support the link. But when doing the
  906. job for remember, only use the ID if it already exists. The
  907. purpose of this setting is to avoid proliferation of unwanted
  908. IDs, just because you happen to be in an Org file when you
  909. call `org-remember' that automatically and preemptively
  910. creates a link. If you do want to get an ID link in a remember
  911. template to an entry not having an ID, create it first by
  912. explicitly creating a link to it, using `C-c C-l' first.
  913. use-existing
  914. Use existing ID, do not create one.
  915. nil Never use an ID to make a link, instead link using a text search for
  916. the headline text."
  917. :group 'org-link-store
  918. :type '(choice
  919. (const :tag "Create ID to make link" t)
  920. (const :tag "Create if string link interactively"
  921. 'create-if-interactive)
  922. (const :tag "Only use existing" 'use-existing)
  923. (const :tag "Do not use ID to create link" nil)))
  924. (defcustom org-context-in-file-links t
  925. "Non-nil means, file links from `org-store-link' contain context.
  926. A search string will be added to the file name with :: as separator and
  927. used to find the context when the link is activated by the command
  928. `org-open-at-point'.
  929. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  930. negates this setting for the duration of the command."
  931. :group 'org-link-store
  932. :type 'boolean)
  933. (defcustom org-keep-stored-link-after-insertion nil
  934. "Non-nil means, keep link in list for entire session.
  935. The command `org-store-link' adds a link pointing to the current
  936. location to an internal list. These links accumulate during a session.
  937. The command `org-insert-link' can be used to insert links into any
  938. Org-mode file (offering completion for all stored links). When this
  939. option is nil, every link which has been inserted once using \\[org-insert-link]
  940. will be removed from the list, to make completing the unused links
  941. more efficient."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defgroup org-link-follow nil
  945. "Options concerning following links in Org-mode."
  946. :tag "Org Follow Link"
  947. :group 'org-link)
  948. (defcustom org-link-translation-function nil
  949. "Function to translate links with different syntax to Org syntax.
  950. This can be used to translate links created for example by the Planner
  951. or emacs-wiki packages to Org syntax.
  952. The function must accept two parameters, a TYPE containing the link
  953. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  954. which is everything after the link protocol. It should return a cons
  955. with possibly modified values of type and path.
  956. Org contains a function for this, so if you set this variable to
  957. `org-translate-link-from-planner', you should be able follow many
  958. links created by planner."
  959. :group 'org-link-follow
  960. :type 'function)
  961. (defcustom org-follow-link-hook nil
  962. "Hook that is run after a link has been followed."
  963. :group 'org-link-follow
  964. :type 'hook)
  965. (defcustom org-tab-follows-link nil
  966. "Non-nil means, on links TAB will follow the link.
  967. Needs to be set before org.el is loaded."
  968. :group 'org-link-follow
  969. :type 'boolean)
  970. (defcustom org-return-follows-link nil
  971. "Non-nil means, on links RET will follow the link.
  972. Needs to be set before org.el is loaded."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-mouse-1-follows-link
  976. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  977. "Non-nil means, mouse-1 on a link will follow the link.
  978. A longer mouse click will still set point. Does not work on XEmacs.
  979. Needs to be set before org.el is loaded."
  980. :group 'org-link-follow
  981. :type 'boolean)
  982. (defcustom org-mark-ring-length 4
  983. "Number of different positions to be recorded in the ring
  984. Changing this requires a restart of Emacs to work correctly."
  985. :group 'org-link-follow
  986. :type 'integer)
  987. (defcustom org-link-frame-setup
  988. '((vm . vm-visit-folder-other-frame)
  989. (gnus . gnus-other-frame)
  990. (file . find-file-other-window))
  991. "Setup the frame configuration for following links.
  992. When following a link with Emacs, it may often be useful to display
  993. this link in another window or frame. This variable can be used to
  994. set this up for the different types of links.
  995. For VM, use any of
  996. `vm-visit-folder'
  997. `vm-visit-folder-other-frame'
  998. For Gnus, use any of
  999. `gnus'
  1000. `gnus-other-frame'
  1001. `org-gnus-no-new-news'
  1002. For FILE, use any of
  1003. `find-file'
  1004. `find-file-other-window'
  1005. `find-file-other-frame'
  1006. For the calendar, use the variable `calendar-setup'.
  1007. For BBDB, it is currently only possible to display the matches in
  1008. another window."
  1009. :group 'org-link-follow
  1010. :type '(list
  1011. (cons (const vm)
  1012. (choice
  1013. (const vm-visit-folder)
  1014. (const vm-visit-folder-other-window)
  1015. (const vm-visit-folder-other-frame)))
  1016. (cons (const gnus)
  1017. (choice
  1018. (const gnus)
  1019. (const gnus-other-frame)
  1020. (const org-gnus-no-new-news)))
  1021. (cons (const file)
  1022. (choice
  1023. (const find-file)
  1024. (const find-file-other-window)
  1025. (const find-file-other-frame)))))
  1026. (defcustom org-display-internal-link-with-indirect-buffer nil
  1027. "Non-nil means, use indirect buffer to display infile links.
  1028. Activating internal links (from one location in a file to another location
  1029. in the same file) normally just jumps to the location. When the link is
  1030. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1031. another window. When this option is set, the other window actually displays
  1032. an indirect buffer clone of the current buffer, to avoid any visibility
  1033. changes to the current buffer."
  1034. :group 'org-link-follow
  1035. :type 'boolean)
  1036. (defcustom org-open-non-existing-files nil
  1037. "Non-nil means, `org-open-file' will open non-existing files.
  1038. When nil, an error will be generated."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-open-directory-means-index-dot-org nil
  1042. "Non-nil means, a link to a directory really means to index.org.
  1043. When nil, following a directory link will run dired or open a finder/explorer
  1044. window on that directory."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1048. "Function and arguments to call for following mailto links.
  1049. This is a list with the first element being a lisp function, and the
  1050. remaining elements being arguments to the function. In string arguments,
  1051. %a will be replaced by the address, and %s will be replaced by the subject
  1052. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1053. :group 'org-link-follow
  1054. :type '(choice
  1055. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1056. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1057. (const :tag "message-mail" (message-mail "%a" "%s"))
  1058. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1059. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1060. "Non-nil means, ask for confirmation before executing shell links.
  1061. Shell links can be dangerous: just think about a link
  1062. [[shell:rm -rf ~/*][Google Search]]
  1063. This link would show up in your Org-mode document as \"Google Search\",
  1064. but really it would remove your entire home directory.
  1065. Therefore we advise against setting this variable to nil.
  1066. Just change it to `y-or-n-p' of you want to confirm with a
  1067. single keystroke rather than having to type \"yes\"."
  1068. :group 'org-link-follow
  1069. :type '(choice
  1070. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1071. (const :tag "with y-or-n (faster)" y-or-n-p)
  1072. (const :tag "no confirmation (dangerous)" nil)))
  1073. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1074. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1075. Elisp links can be dangerous: just think about a link
  1076. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1077. This link would show up in your Org-mode document as \"Google Search\",
  1078. but really it would remove your entire home directory.
  1079. Therefore we advise against setting this variable to nil.
  1080. Just change it to `y-or-n-p' of you want to confirm with a
  1081. single keystroke rather than having to type \"yes\"."
  1082. :group 'org-link-follow
  1083. :type '(choice
  1084. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1085. (const :tag "with y-or-n (faster)" y-or-n-p)
  1086. (const :tag "no confirmation (dangerous)" nil)))
  1087. (defconst org-file-apps-defaults-gnu
  1088. '((remote . emacs)
  1089. (system . mailcap)
  1090. (t . mailcap))
  1091. "Default file applications on a UNIX or GNU/Linux system.
  1092. See `org-file-apps'.")
  1093. (defconst org-file-apps-defaults-macosx
  1094. '((remote . emacs)
  1095. (t . "open %s")
  1096. (system . "open %s")
  1097. ("ps.gz" . "gv %s")
  1098. ("eps.gz" . "gv %s")
  1099. ("dvi" . "xdvi %s")
  1100. ("fig" . "xfig %s"))
  1101. "Default file applications on a MacOS X system.
  1102. The system \"open\" is known as a default, but we use X11 applications
  1103. for some files for which the OS does not have a good default.
  1104. See `org-file-apps'.")
  1105. (defconst org-file-apps-defaults-windowsnt
  1106. (list
  1107. '(remote . emacs)
  1108. (cons t
  1109. (list (if (featurep 'xemacs)
  1110. 'mswindows-shell-execute
  1111. 'w32-shell-execute)
  1112. "open" 'file))
  1113. (cons 'system
  1114. (list (if (featurep 'xemacs)
  1115. 'mswindows-shell-execute
  1116. 'w32-shell-execute)
  1117. "open" 'file)))
  1118. "Default file applications on a Windows NT system.
  1119. The system \"open\" is used for most files.
  1120. See `org-file-apps'.")
  1121. (defcustom org-file-apps
  1122. '(
  1123. (auto-mode . emacs)
  1124. ("\\.x?html?\\'" . default)
  1125. ("\\.pdf\\'" . default)
  1126. )
  1127. "External applications for opening `file:path' items in a document.
  1128. Org-mode uses system defaults for different file types, but
  1129. you can use this variable to set the application for a given file
  1130. extension. The entries in this list are cons cells where the car identifies
  1131. files and the cdr the corresponding command. Possible values for the
  1132. file identifier are
  1133. \"regex\" Regular expression matched against the file name. For backward
  1134. compatibility, this can also be a string with only alphanumeric
  1135. characters, which is then interpreted as an extension.
  1136. `directory' Matches a directory
  1137. `remote' Matches a remote file, accessible through tramp or efs.
  1138. Remote files most likely should be visited through Emacs
  1139. because external applications cannot handle such paths.
  1140. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1141. so all files Emacs knows how to handle. Using this with
  1142. command `emacs' will open most files in Emacs. Beware that this
  1143. will also open html files inside Emacs, unless you add
  1144. (\"html\" . default) to the list as well.
  1145. t Default for files not matched by any of the other options.
  1146. `system' The system command to open files, like `open' on Windows
  1147. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1148. that will be selected if you call `C-c C-o' with a double
  1149. `C-u C-u' prefix.
  1150. Possible values for the command are:
  1151. `emacs' The file will be visited by the current Emacs process.
  1152. `default' Use the default application for this file type, which is the
  1153. association for t in the list, most likely in the system-specific
  1154. part.
  1155. This can be used to overrule an unwanted setting in the
  1156. system-specific variable.
  1157. `system' Use the system command for opening files, like \"open\".
  1158. This command is specified by the entry whose car is `system'.
  1159. Most likely, the system-specific version of this variable
  1160. does define this command, but you can overrule/replace it
  1161. here.
  1162. string A command to be executed by a shell; %s will be replaced
  1163. by the path to the file.
  1164. sexp A Lisp form which will be evaluated. The file path will
  1165. be available in the Lisp variable `file'.
  1166. For more examples, see the system specific constants
  1167. `org-file-apps-defaults-macosx'
  1168. `org-file-apps-defaults-windowsnt'
  1169. `org-file-apps-defaults-gnu'."
  1170. :group 'org-link-follow
  1171. :type '(repeat
  1172. (cons (choice :value ""
  1173. (string :tag "Extension")
  1174. (const :tag "System command to open files" system)
  1175. (const :tag "Default for unrecognized files" t)
  1176. (const :tag "Remote file" remote)
  1177. (const :tag "Links to a directory" directory)
  1178. (const :tag "Any files that have Emacs modes"
  1179. auto-mode))
  1180. (choice :value ""
  1181. (const :tag "Visit with Emacs" emacs)
  1182. (const :tag "Use default" default)
  1183. (const :tag "Use the system command" system)
  1184. (string :tag "Command")
  1185. (sexp :tag "Lisp form")))))
  1186. (defgroup org-refile nil
  1187. "Options concerning refiling entries in Org-mode."
  1188. :tag "Org Refile"
  1189. :group 'org)
  1190. (defcustom org-directory "~/org"
  1191. "Directory with org files.
  1192. This directory will be used as default to prompt for org files.
  1193. Used by the hooks for remember.el."
  1194. :group 'org-refile
  1195. :group 'org-remember
  1196. :type 'directory)
  1197. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1198. "Default target for storing notes.
  1199. Used by the hooks for remember.el. This can be a string, or nil to mean
  1200. the value of `remember-data-file'.
  1201. You can set this on a per-template basis with the variable
  1202. `org-remember-templates'."
  1203. :group 'org-refile
  1204. :group 'org-remember
  1205. :type '(choice
  1206. (const :tag "Default from remember-data-file" nil)
  1207. file))
  1208. (defcustom org-goto-interface 'outline
  1209. "The default interface to be used for `org-goto'.
  1210. Allowed values are:
  1211. outline The interface shows an outline of the relevant file
  1212. and the correct heading is found by moving through
  1213. the outline or by searching with incremental search.
  1214. outline-path-completion Headlines in the current buffer are offered via
  1215. completion. This is the interface also used by
  1216. the refile command."
  1217. :group 'org-refile
  1218. :type '(choice
  1219. (const :tag "Outline" outline)
  1220. (const :tag "Outline-path-completion" outline-path-completion)))
  1221. (defcustom org-goto-max-level 5
  1222. "Maximum level to be considered when running org-goto with refile interface."
  1223. :group 'org-refile
  1224. :type 'number)
  1225. (defcustom org-reverse-note-order nil
  1226. "Non-nil means, store new notes at the beginning of a file or entry.
  1227. When nil, new notes will be filed to the end of a file or entry.
  1228. This can also be a list with cons cells of regular expressions that
  1229. are matched against file names, and values."
  1230. :group 'org-remember
  1231. :group 'org-refile
  1232. :type '(choice
  1233. (const :tag "Reverse always" t)
  1234. (const :tag "Reverse never" nil)
  1235. (repeat :tag "By file name regexp"
  1236. (cons regexp boolean))))
  1237. (defcustom org-refile-targets nil
  1238. "Targets for refiling entries with \\[org-refile].
  1239. This is list of cons cells. Each cell contains:
  1240. - a specification of the files to be considered, either a list of files,
  1241. or a symbol whose function or variable value will be used to retrieve
  1242. a file name or a list of file names. If you use `org-agenda-files' for
  1243. that, all agenda files will be scanned for targets. Nil means, consider
  1244. headings in the current buffer.
  1245. - A specification of how to select find candidate refile targets. This
  1246. may be any of
  1247. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1248. This tag has to be present in all target headlines, inheritance will
  1249. not be considered.
  1250. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1251. todo keyword.
  1252. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1253. headlines that are refiling targets.
  1254. - a cons cell (:level . N). Any headline of level N is considered a target.
  1255. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1256. When this variable is nil, all top-level headlines in the current buffer
  1257. are used, equivalent to the value `((nil . (:level . 1))'."
  1258. :group 'org-refile
  1259. :type '(repeat
  1260. (cons
  1261. (choice :value org-agenda-files
  1262. (const :tag "All agenda files" org-agenda-files)
  1263. (const :tag "Current buffer" nil)
  1264. (function) (variable) (file))
  1265. (choice :tag "Identify target headline by"
  1266. (cons :tag "Specific tag" (const :value :tag) (string))
  1267. (cons :tag "TODO keyword" (const :value :todo) (string))
  1268. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1269. (cons :tag "Level number" (const :value :level) (integer))
  1270. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1271. (defcustom org-refile-use-outline-path nil
  1272. "Non-nil means, provide refile targets as paths.
  1273. So a level 3 headline will be available as level1/level2/level3.
  1274. When the value is `file', also include the file name (without directory)
  1275. into the path. When `full-file-path', include the full file path."
  1276. :group 'org-refile
  1277. :type '(choice
  1278. (const :tag "Not" nil)
  1279. (const :tag "Yes" t)
  1280. (const :tag "Start with file name" file)
  1281. (const :tag "Start with full file path" full-file-path)))
  1282. (defcustom org-outline-path-complete-in-steps t
  1283. "Non-nil means, complete the outline path in hierarchical steps.
  1284. When Org-mode uses the refile interface to select an outline path
  1285. \(see variable `org-refile-use-outline-path'), the completion of
  1286. the path can be done is a single go, or if can be done in steps down
  1287. the headline hierarchy. Going in steps is probably the best if you
  1288. do not use a special completion package like `ido' or `icicles'.
  1289. However, when using these packages, going in one step can be very
  1290. fast, while still showing the whole path to the entry."
  1291. :group 'org-refile
  1292. :type 'boolean)
  1293. (defgroup org-todo nil
  1294. "Options concerning TODO items in Org-mode."
  1295. :tag "Org TODO"
  1296. :group 'org)
  1297. (defgroup org-progress nil
  1298. "Options concerning Progress logging in Org-mode."
  1299. :tag "Org Progress"
  1300. :group 'org-time)
  1301. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1302. "List of TODO entry keyword sequences and their interpretation.
  1303. \\<org-mode-map>This is a list of sequences.
  1304. Each sequence starts with a symbol, either `sequence' or `type',
  1305. indicating if the keywords should be interpreted as a sequence of
  1306. action steps, or as different types of TODO items. The first
  1307. keywords are states requiring action - these states will select a headline
  1308. for inclusion into the global TODO list Org-mode produces. If one of
  1309. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1310. signify that no further action is necessary. If \"|\" is not found,
  1311. the last keyword is treated as the only DONE state of the sequence.
  1312. The command \\[org-todo] cycles an entry through these states, and one
  1313. additional state where no keyword is present. For details about this
  1314. cycling, see the manual.
  1315. TODO keywords and interpretation can also be set on a per-file basis with
  1316. the special #+SEQ_TODO and #+TYP_TODO lines.
  1317. Each keyword can optionally specify a character for fast state selection
  1318. \(in combination with the variable `org-use-fast-todo-selection')
  1319. and specifiers for state change logging, using the same syntax
  1320. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1321. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1322. indicates to record a time stamp each time this state is selected.
  1323. Each keyword may also specify if a timestamp or a note should be
  1324. recorded when entering or leaving the state, by adding additional
  1325. characters in the parenthesis after the keyword. This looks like this:
  1326. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1327. record only the time of the state change. With X and Y being either
  1328. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1329. Y when leaving the state if and only if the *target* state does not
  1330. define X. You may omit any of the fast-selection key or X or /Y,
  1331. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1332. For backward compatibility, this variable may also be just a list
  1333. of keywords - in this case the interpretation (sequence or type) will be
  1334. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1335. :group 'org-todo
  1336. :group 'org-keywords
  1337. :type '(choice
  1338. (repeat :tag "Old syntax, just keywords"
  1339. (string :tag "Keyword"))
  1340. (repeat :tag "New syntax"
  1341. (cons
  1342. (choice
  1343. :tag "Interpretation"
  1344. (const :tag "Sequence (cycling hits every state)" sequence)
  1345. (const :tag "Type (cycling directly to DONE)" type))
  1346. (repeat
  1347. (string :tag "Keyword"))))))
  1348. (defvar org-todo-keywords-1 nil
  1349. "All TODO and DONE keywords active in a buffer.")
  1350. (make-variable-buffer-local 'org-todo-keywords-1)
  1351. (defvar org-todo-keywords-for-agenda nil)
  1352. (defvar org-done-keywords-for-agenda nil)
  1353. (defvar org-todo-keyword-alist-for-agenda nil)
  1354. (defvar org-tag-alist-for-agenda nil)
  1355. (defvar org-agenda-contributing-files nil)
  1356. (defvar org-not-done-keywords nil)
  1357. (make-variable-buffer-local 'org-not-done-keywords)
  1358. (defvar org-done-keywords nil)
  1359. (make-variable-buffer-local 'org-done-keywords)
  1360. (defvar org-todo-heads nil)
  1361. (make-variable-buffer-local 'org-todo-heads)
  1362. (defvar org-todo-sets nil)
  1363. (make-variable-buffer-local 'org-todo-sets)
  1364. (defvar org-todo-log-states nil)
  1365. (make-variable-buffer-local 'org-todo-log-states)
  1366. (defvar org-todo-kwd-alist nil)
  1367. (make-variable-buffer-local 'org-todo-kwd-alist)
  1368. (defvar org-todo-key-alist nil)
  1369. (make-variable-buffer-local 'org-todo-key-alist)
  1370. (defvar org-todo-key-trigger nil)
  1371. (make-variable-buffer-local 'org-todo-key-trigger)
  1372. (defcustom org-todo-interpretation 'sequence
  1373. "Controls how TODO keywords are interpreted.
  1374. This variable is in principle obsolete and is only used for
  1375. backward compatibility, if the interpretation of todo keywords is
  1376. not given already in `org-todo-keywords'. See that variable for
  1377. more information."
  1378. :group 'org-todo
  1379. :group 'org-keywords
  1380. :type '(choice (const sequence)
  1381. (const type)))
  1382. (defcustom org-use-fast-todo-selection 'prefix
  1383. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1384. This variable describes if and under what circumstances the cycling
  1385. mechanism for TODO keywords will be replaced by a single-key, direct
  1386. selection scheme.
  1387. When nil, fast selection is never used.
  1388. When the symbol `prefix', it will be used when `org-todo' is called with
  1389. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1390. in an agenda buffer.
  1391. When t, fast selection is used by default. In this case, the prefix
  1392. argument forces cycling instead.
  1393. In all cases, the special interface is only used if access keys have actually
  1394. been assigned by the user, i.e. if keywords in the configuration are followed
  1395. by a letter in parenthesis, like TODO(t)."
  1396. :group 'org-todo
  1397. :type '(choice
  1398. (const :tag "Never" nil)
  1399. (const :tag "By default" t)
  1400. (const :tag "Only with C-u C-c C-t" prefix)))
  1401. (defcustom org-provide-todo-statistics t
  1402. "Non-nil means, update todo statistics after insert and toggle.
  1403. When this is set, todo statistics is updated in the parent of the current
  1404. entry each time a todo state is changed."
  1405. :group 'org-todo
  1406. :type 'boolean)
  1407. (defcustom org-after-todo-state-change-hook nil
  1408. "Hook which is run after the state of a TODO item was changed.
  1409. The new state (a string with a TODO keyword, or nil) is available in the
  1410. Lisp variable `state'."
  1411. :group 'org-todo
  1412. :type 'hook)
  1413. (defvar org-blocker-hook nil
  1414. "Hook for functions that are allowed to block a state change.
  1415. Each function gets as its single argument a property list, see
  1416. `org-trigger-hook' for more information about this list.
  1417. If any of the functions in this hook returns nil, the state change
  1418. is blocked.")
  1419. (defvar org-trigger-hook nil
  1420. "Hook for functions that are triggered by a state change.
  1421. Each function gets as its single argument a property list with at least
  1422. the following elements:
  1423. (:type type-of-change :position pos-at-entry-start
  1424. :from old-state :to new-state)
  1425. Depending on the type, more properties may be present.
  1426. This mechanism is currently implemented for:
  1427. TODO state changes
  1428. ------------------
  1429. :type todo-state-change
  1430. :from previous state (keyword as a string), or nil, or a symbol
  1431. 'todo' or 'done', to indicate the general type of state.
  1432. :to new state, like in :from")
  1433. (defcustom org-enforce-todo-dependencies nil
  1434. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1435. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1436. be blocked if any prior sibling is not yet done.
  1437. You need to set this variable through the customize interface, or to
  1438. restart emacs after changing the value."
  1439. :set (lambda (var val)
  1440. (set var val)
  1441. (if val
  1442. (add-hook 'org-blocker-hook 'org-block-todo-from-children-or-siblings)
  1443. (remove-hook 'org-blocker-hook 'org-block-todo-from-children-or-siblings)))
  1444. :group 'org-todo
  1445. :type 'boolean)
  1446. (defcustom org-todo-state-tags-triggers nil
  1447. "Tag changes that should be triggered by TODO state changes.
  1448. This is a list. Each entry is
  1449. (state-change (tag . flag) .......)
  1450. State-change can be a string with a state, and empty string to indicate the
  1451. state that has no TODO keyword, or it can be one of the symbols `todo'
  1452. or `done', meaning any not-done or done state, respectively."
  1453. :group 'org-todo
  1454. :group 'org-tags
  1455. :type '(repeat
  1456. (cons (choice :tag "When changing to"
  1457. (const :tag "Not-done state" todo)
  1458. (const :tag "Done state" done)
  1459. (string :tag "State"))
  1460. (repeat
  1461. (cons :tag "Tag action"
  1462. (string :tag "Tag")
  1463. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1464. (defcustom org-log-done nil
  1465. "Information to record when a task moves to the DONE state.
  1466. Possible values are:
  1467. nil Don't add anything, just change the keyword
  1468. time Add a time stamp to the task
  1469. note Prompt a closing note and add it with template `org-log-note-headings'
  1470. This option can also be set with on a per-file-basis with
  1471. #+STARTUP: nologdone
  1472. #+STARTUP: logdone
  1473. #+STARTUP: lognotedone
  1474. You can have local logging settings for a subtree by setting the LOGGING
  1475. property to one or more of these keywords."
  1476. :group 'org-todo
  1477. :group 'org-progress
  1478. :type '(choice
  1479. (const :tag "No logging" nil)
  1480. (const :tag "Record CLOSED timestamp" time)
  1481. (const :tag "Record CLOSED timestamp with closing note." note)))
  1482. ;; Normalize old uses of org-log-done.
  1483. (cond
  1484. ((eq org-log-done t) (setq org-log-done 'time))
  1485. ((and (listp org-log-done) (memq 'done org-log-done))
  1486. (setq org-log-done 'note)))
  1487. (defcustom org-log-note-clock-out nil
  1488. "Non-nil means, record a note when clocking out of an item.
  1489. This can also be configured on a per-file basis by adding one of
  1490. the following lines anywhere in the buffer:
  1491. #+STARTUP: lognoteclock-out
  1492. #+STARTUP: nolognoteclock-out"
  1493. :group 'org-todo
  1494. :group 'org-progress
  1495. :type 'boolean)
  1496. (defcustom org-log-done-with-time t
  1497. "Non-nil means, the CLOSED time stamp will contain date and time.
  1498. When nil, only the date will be recorded."
  1499. :group 'org-progress
  1500. :type 'boolean)
  1501. (defcustom org-log-note-headings
  1502. '((done . "CLOSING NOTE %t")
  1503. (state . "State %-12s %t")
  1504. (note . "Note taken on %t")
  1505. (clock-out . ""))
  1506. "Headings for notes added to entries.
  1507. The value is an alist, with the car being a symbol indicating the note
  1508. context, and the cdr is the heading to be used. The heading may also be the
  1509. empty string.
  1510. %t in the heading will be replaced by a time stamp.
  1511. %s will be replaced by the new TODO state, in double quotes.
  1512. %u will be replaced by the user name.
  1513. %U will be replaced by the full user name."
  1514. :group 'org-todo
  1515. :group 'org-progress
  1516. :type '(list :greedy t
  1517. (cons (const :tag "Heading when closing an item" done) string)
  1518. (cons (const :tag
  1519. "Heading when changing todo state (todo sequence only)"
  1520. state) string)
  1521. (cons (const :tag "Heading when just taking a note" note) string)
  1522. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1523. (unless (assq 'note org-log-note-headings)
  1524. (push '(note . "%t") org-log-note-headings))
  1525. (defcustom org-log-state-notes-insert-after-drawers nil
  1526. "Non-nil means, insert state change notes after any drawers in entry.
  1527. Only the drawers that *immediately* follow the headline and the
  1528. deadline/scheduled line are skipped.
  1529. When nil, insert notes right after the heading and perhaps the line
  1530. with deadline/scheduling if present."
  1531. :group 'org-todo
  1532. :group 'org-progress
  1533. :type 'boolean)
  1534. (defcustom org-log-states-order-reversed t
  1535. "Non-nil means, the latest state change note will be directly after heading.
  1536. When nil, the notes will be orderer according to time."
  1537. :group 'org-todo
  1538. :group 'org-progress
  1539. :type 'boolean)
  1540. (defcustom org-log-repeat 'time
  1541. "Non-nil means, record moving through the DONE state when triggering repeat.
  1542. An auto-repeating tasks is immediately switched back to TODO when marked
  1543. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1544. the TODO keyword definition, or recording a closing note by setting
  1545. `org-log-done', there will be no record of the task moving through DONE.
  1546. This variable forces taking a note anyway. Possible values are:
  1547. nil Don't force a record
  1548. time Record a time stamp
  1549. note Record a note
  1550. This option can also be set with on a per-file-basis with
  1551. #+STARTUP: logrepeat
  1552. #+STARTUP: lognoterepeat
  1553. #+STARTUP: nologrepeat
  1554. You can have local logging settings for a subtree by setting the LOGGING
  1555. property to one or more of these keywords."
  1556. :group 'org-todo
  1557. :group 'org-progress
  1558. :type '(choice
  1559. (const :tag "Don't force a record" nil)
  1560. (const :tag "Force recording the DONE state" time)
  1561. (const :tag "Force recording a note with the DONE state" note)))
  1562. (defgroup org-priorities nil
  1563. "Priorities in Org-mode."
  1564. :tag "Org Priorities"
  1565. :group 'org-todo)
  1566. (defcustom org-highest-priority ?A
  1567. "The highest priority of TODO items. A character like ?A, ?B etc.
  1568. Must have a smaller ASCII number than `org-lowest-priority'."
  1569. :group 'org-priorities
  1570. :type 'character)
  1571. (defcustom org-lowest-priority ?C
  1572. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1573. Must have a larger ASCII number than `org-highest-priority'."
  1574. :group 'org-priorities
  1575. :type 'character)
  1576. (defcustom org-default-priority ?B
  1577. "The default priority of TODO items.
  1578. This is the priority an item get if no explicit priority is given."
  1579. :group 'org-priorities
  1580. :type 'character)
  1581. (defcustom org-priority-start-cycle-with-default t
  1582. "Non-nil means, start with default priority when starting to cycle.
  1583. When this is nil, the first step in the cycle will be (depending on the
  1584. command used) one higher or lower that the default priority."
  1585. :group 'org-priorities
  1586. :type 'boolean)
  1587. (defgroup org-time nil
  1588. "Options concerning time stamps and deadlines in Org-mode."
  1589. :tag "Org Time"
  1590. :group 'org)
  1591. (defcustom org-insert-labeled-timestamps-at-point nil
  1592. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1593. When nil, these labeled time stamps are forces into the second line of an
  1594. entry, just after the headline. When scheduling from the global TODO list,
  1595. the time stamp will always be forced into the second line."
  1596. :group 'org-time
  1597. :type 'boolean)
  1598. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1599. "Formats for `format-time-string' which are used for time stamps.
  1600. It is not recommended to change this constant.")
  1601. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1602. "Number of minutes to round time stamps to.
  1603. These are two values, the first applies when first creating a time stamp.
  1604. The second applies when changing it with the commands `S-up' and `S-down'.
  1605. When changing the time stamp, this means that it will change in steps
  1606. of N minutes, as given by the second value.
  1607. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1608. numbers should be factors of 60, so for example 5, 10, 15.
  1609. When this is larger than 1, you can still force an exact time-stamp by using
  1610. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1611. and by using a prefix arg to `S-up/down' to specify the exact number
  1612. of minutes to shift."
  1613. :group 'org-time
  1614. :get '(lambda (var) ; Make sure all entries have 5 elements
  1615. (if (integerp (default-value var))
  1616. (list (default-value var) 5)
  1617. (default-value var)))
  1618. :type '(list
  1619. (integer :tag "when inserting times")
  1620. (integer :tag "when modifying times")))
  1621. ;; Normalize old customizations of this variable.
  1622. (when (integerp org-time-stamp-rounding-minutes)
  1623. (setq org-time-stamp-rounding-minutes
  1624. (list org-time-stamp-rounding-minutes
  1625. org-time-stamp-rounding-minutes)))
  1626. (defcustom org-display-custom-times nil
  1627. "Non-nil means, overlay custom formats over all time stamps.
  1628. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1629. To turn this on on a per-file basis, insert anywhere in the file:
  1630. #+STARTUP: customtime"
  1631. :group 'org-time
  1632. :set 'set-default
  1633. :type 'sexp)
  1634. (make-variable-buffer-local 'org-display-custom-times)
  1635. (defcustom org-time-stamp-custom-formats
  1636. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1637. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1638. These are overlayed over the default ISO format if the variable
  1639. `org-display-custom-times' is set. Time like %H:%M should be at the
  1640. end of the second format."
  1641. :group 'org-time
  1642. :type 'sexp)
  1643. (defun org-time-stamp-format (&optional long inactive)
  1644. "Get the right format for a time string."
  1645. (let ((f (if long (cdr org-time-stamp-formats)
  1646. (car org-time-stamp-formats))))
  1647. (if inactive
  1648. (concat "[" (substring f 1 -1) "]")
  1649. f)))
  1650. (defcustom org-time-clocksum-format "%d:%02d"
  1651. "The format string used when creating CLOCKSUM lines, or when
  1652. org-mode generates a time duration."
  1653. :group 'org-time
  1654. :type 'string)
  1655. (defcustom org-deadline-warning-days 14
  1656. "No. of days before expiration during which a deadline becomes active.
  1657. This variable governs the display in sparse trees and in the agenda.
  1658. When 0 or negative, it means use this number (the absolute value of it)
  1659. even if a deadline has a different individual lead time specified."
  1660. :group 'org-time
  1661. :group 'org-agenda-daily/weekly
  1662. :type 'number)
  1663. (defcustom org-read-date-prefer-future t
  1664. "Non-nil means, assume future for incomplete date input from user.
  1665. This affects the following situations:
  1666. 1. The user gives a day, but no month.
  1667. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1668. read this as the third of *next* month. However, if you enter \"17\",
  1669. it will be considered as *this* month.
  1670. 2. The user gives a month but not a year.
  1671. For example, if it is april and you enter \"feb 2\", this will be read
  1672. as feb 2, *next* year. \"May 5\", however, will be this year.
  1673. Currently this does not work for ISO week specifications.
  1674. When this option is nil, the current month and year will always be used
  1675. as defaults."
  1676. :group 'org-time
  1677. :type 'boolean)
  1678. (defcustom org-read-date-display-live t
  1679. "Non-nil means, display current interpretation of date prompt live.
  1680. This display will be in an overlay, in the minibuffer."
  1681. :group 'org-time
  1682. :type 'boolean)
  1683. (defcustom org-read-date-popup-calendar t
  1684. "Non-nil means, pop up a calendar when prompting for a date.
  1685. In the calendar, the date can be selected with mouse-1. However, the
  1686. minibuffer will also be active, and you can simply enter the date as well.
  1687. When nil, only the minibuffer will be available."
  1688. :group 'org-time
  1689. :type 'boolean)
  1690. (if (fboundp 'defvaralias)
  1691. (defvaralias 'org-popup-calendar-for-date-prompt
  1692. 'org-read-date-popup-calendar))
  1693. (defcustom org-extend-today-until 0
  1694. "The hour when your day really ends. Must be an integer.
  1695. This has influence for the following applications:
  1696. - When switching the agenda to \"today\". It it is still earlier than
  1697. the time given here, the day recognized as TODAY is actually yesterday.
  1698. - When a date is read from the user and it is still before the time given
  1699. here, the current date and time will be assumed to be yesterday, 23:59.
  1700. Also, timestamps inserted in remember templates follow this rule.
  1701. IMPORTANT: This is a feature whose implementation is and likely will
  1702. remain incomplete. Really, it is only here because past midnight seems to
  1703. be the favorite working time of John Wiegley :-)"
  1704. :group 'org-time
  1705. :type 'number)
  1706. (defcustom org-edit-timestamp-down-means-later nil
  1707. "Non-nil means, S-down will increase the time in a time stamp.
  1708. When nil, S-up will increase."
  1709. :group 'org-time
  1710. :type 'boolean)
  1711. (defcustom org-calendar-follow-timestamp-change t
  1712. "Non-nil means, make the calendar window follow timestamp changes.
  1713. When a timestamp is modified and the calendar window is visible, it will be
  1714. moved to the new date."
  1715. :group 'org-time
  1716. :type 'boolean)
  1717. (defgroup org-tags nil
  1718. "Options concerning tags in Org-mode."
  1719. :tag "Org Tags"
  1720. :group 'org)
  1721. (defcustom org-tag-alist nil
  1722. "List of tags allowed in Org-mode files.
  1723. When this list is nil, Org-mode will base TAG input on what is already in the
  1724. buffer.
  1725. The value of this variable is an alist, the car of each entry must be a
  1726. keyword as a string, the cdr may be a character that is used to select
  1727. that tag through the fast-tag-selection interface.
  1728. See the manual for details."
  1729. :group 'org-tags
  1730. :type '(repeat
  1731. (choice
  1732. (cons (string :tag "Tag name")
  1733. (character :tag "Access char"))
  1734. (const :tag "Start radio group" (:startgroup))
  1735. (const :tag "End radio group" (:endgroup)))))
  1736. (defvar org-file-tags nil
  1737. "List of tags that can be inherited by all entries in the file.
  1738. The tags will be inherited if the variable `org-use-tag-inheritance'
  1739. says they should be.
  1740. This variable is populated from #+TAG lines.")
  1741. (defcustom org-use-fast-tag-selection 'auto
  1742. "Non-nil means, use fast tag selection scheme.
  1743. This is a special interface to select and deselect tags with single keys.
  1744. When nil, fast selection is never used.
  1745. When the symbol `auto', fast selection is used if and only if selection
  1746. characters for tags have been configured, either through the variable
  1747. `org-tag-alist' or through a #+TAGS line in the buffer.
  1748. When t, fast selection is always used and selection keys are assigned
  1749. automatically if necessary."
  1750. :group 'org-tags
  1751. :type '(choice
  1752. (const :tag "Always" t)
  1753. (const :tag "Never" nil)
  1754. (const :tag "When selection characters are configured" 'auto)))
  1755. (defcustom org-fast-tag-selection-single-key nil
  1756. "Non-nil means, fast tag selection exits after first change.
  1757. When nil, you have to press RET to exit it.
  1758. During fast tag selection, you can toggle this flag with `C-c'.
  1759. This variable can also have the value `expert'. In this case, the window
  1760. displaying the tags menu is not even shown, until you press C-c again."
  1761. :group 'org-tags
  1762. :type '(choice
  1763. (const :tag "No" nil)
  1764. (const :tag "Yes" t)
  1765. (const :tag "Expert" expert)))
  1766. (defvar org-fast-tag-selection-include-todo nil
  1767. "Non-nil means, fast tags selection interface will also offer TODO states.
  1768. This is an undocumented feature, you should not rely on it.")
  1769. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1770. "The column to which tags should be indented in a headline.
  1771. If this number is positive, it specifies the column. If it is negative,
  1772. it means that the tags should be flushright to that column. For example,
  1773. -80 works well for a normal 80 character screen."
  1774. :group 'org-tags
  1775. :type 'integer)
  1776. (defcustom org-auto-align-tags t
  1777. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1778. These operations change the length of a headline and therefore shift
  1779. the tags around. With this options turned on, after each such operation
  1780. the tags are again aligned to `org-tags-column'."
  1781. :group 'org-tags
  1782. :type 'boolean)
  1783. (defcustom org-use-tag-inheritance t
  1784. "Non-nil means, tags in levels apply also for sublevels.
  1785. When nil, only the tags directly given in a specific line apply there.
  1786. This may also be a list of tags that should be inherited, or a regexp that
  1787. matches tags that should be inherited. Additional control is possible
  1788. with the variable `org-tags-exclude-from-inheritance' which gives an
  1789. explicit list of tags to be excluded from inheritance., even if the value of
  1790. `org-use-tag-inheritance' would select it for inheritance.
  1791. If this option is t, a match early-on in a tree can lead to a large
  1792. number of matches in the subtree when constructing the agenda or creating
  1793. a sparse tree. If you only want to see the first match in a tree during
  1794. a search, check out the variable `org-tags-match-list-sublevels'."
  1795. :group 'org-tags
  1796. :type '(choice
  1797. (const :tag "Not" nil)
  1798. (const :tag "Always" t)
  1799. (repeat :tag "Specific tags" (string :tag "Tag"))
  1800. (regexp :tag "Tags matched by regexp")))
  1801. (defcustom org-tags-exclude-from-inheritance nil
  1802. "List of tags that should never be inherited.
  1803. This is a way to exclude a few tags from inheritance. For way to do
  1804. the opposite, to actively allow inheritance for selected tags,
  1805. see the variable `org-use-tag-inheritance'."
  1806. :group 'org-tags
  1807. :type '(repeat (string :tag "Tag")))
  1808. (defun org-tag-inherit-p (tag)
  1809. "Check if TAG is one that should be inherited."
  1810. (cond
  1811. ((member tag org-tags-exclude-from-inheritance) nil)
  1812. ((eq org-use-tag-inheritance t) t)
  1813. ((not org-use-tag-inheritance) nil)
  1814. ((stringp org-use-tag-inheritance)
  1815. (string-match org-use-tag-inheritance tag))
  1816. ((listp org-use-tag-inheritance)
  1817. (member tag org-use-tag-inheritance))
  1818. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1819. (defcustom org-tags-match-list-sublevels t
  1820. "Non-nil means list also sublevels of headlines matching tag search.
  1821. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1822. the sublevels of a headline matching a tag search often also match
  1823. the same search. Listing all of them can create very long lists.
  1824. Setting this variable to nil causes subtrees of a match to be skipped.
  1825. This option is off by default, because inheritance in on. If you turn
  1826. inheritance off, you very likely want to turn this option on.
  1827. As a special case, if the tag search is restricted to TODO items, the
  1828. value of this variable is ignored and sublevels are always checked, to
  1829. make sure all corresponding TODO items find their way into the list.
  1830. This variable is semi-obsolete and probably should always be true. It
  1831. is better to limit inheritance to certain tags using the variables
  1832. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1833. :group 'org-tags
  1834. :type 'boolean)
  1835. (defvar org-tags-history nil
  1836. "History of minibuffer reads for tags.")
  1837. (defvar org-last-tags-completion-table nil
  1838. "The last used completion table for tags.")
  1839. (defvar org-after-tags-change-hook nil
  1840. "Hook that is run after the tags in a line have changed.")
  1841. (defgroup org-properties nil
  1842. "Options concerning properties in Org-mode."
  1843. :tag "Org Properties"
  1844. :group 'org)
  1845. (defcustom org-property-format "%-10s %s"
  1846. "How property key/value pairs should be formatted by `indent-line'.
  1847. When `indent-line' hits a property definition, it will format the line
  1848. according to this format, mainly to make sure that the values are
  1849. lined-up with respect to each other."
  1850. :group 'org-properties
  1851. :type 'string)
  1852. (defcustom org-use-property-inheritance nil
  1853. "Non-nil means, properties apply also for sublevels.
  1854. This setting is chiefly used during property searches. Turning it on can
  1855. cause significant overhead when doing a search, which is why it is not
  1856. on by default.
  1857. When nil, only the properties directly given in the current entry count.
  1858. When t, every property is inherited. The value may also be a list of
  1859. properties that should have inheritance, or a regular expression matching
  1860. properties that should be inherited.
  1861. However, note that some special properties use inheritance under special
  1862. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1863. and the properties ending in \"_ALL\" when they are used as descriptor
  1864. for valid values of a property.
  1865. Note for programmers:
  1866. When querying an entry with `org-entry-get', you can control if inheritance
  1867. should be used. By default, `org-entry-get' looks only at the local
  1868. properties. You can request inheritance by setting the inherit argument
  1869. to t (to force inheritance) or to `selective' (to respect the setting
  1870. in this variable)."
  1871. :group 'org-properties
  1872. :type '(choice
  1873. (const :tag "Not" nil)
  1874. (const :tag "Always" t)
  1875. (repeat :tag "Specific properties" (string :tag "Property"))
  1876. (regexp :tag "Properties matched by regexp")))
  1877. (defun org-property-inherit-p (property)
  1878. "Check if PROPERTY is one that should be inherited."
  1879. (cond
  1880. ((eq org-use-property-inheritance t) t)
  1881. ((not org-use-property-inheritance) nil)
  1882. ((stringp org-use-property-inheritance)
  1883. (string-match org-use-property-inheritance property))
  1884. ((listp org-use-property-inheritance)
  1885. (member property org-use-property-inheritance))
  1886. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1887. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1888. "The default column format, if no other format has been defined.
  1889. This variable can be set on the per-file basis by inserting a line
  1890. #+COLUMNS: %25ITEM ....."
  1891. :group 'org-properties
  1892. :type 'string)
  1893. (defcustom org-columns-ellipses ".."
  1894. "The ellipses to be used when a field in column view is truncated.
  1895. When this is the empty string, as many characters as possible are shown,
  1896. but then there will be no visual indication that the field has been truncated.
  1897. When this is a string of length N, the last N characters of a truncated
  1898. field are replaced by this string. If the column is narrower than the
  1899. ellipses string, only part of the ellipses string will be shown."
  1900. :group 'org-properties
  1901. :type 'string)
  1902. (defcustom org-columns-modify-value-for-display-function nil
  1903. "Function that modifies values for display in column view.
  1904. For example, it can be used to cut out a certain part from a time stamp.
  1905. The function must take 2 arguments:
  1906. column-title The title of the column (*not* the property name)
  1907. value The value that should be modified.
  1908. The function should return the value that should be displayed,
  1909. or nil if the normal value should be used."
  1910. :group 'org-properties
  1911. :type 'function)
  1912. (defcustom org-effort-property "Effort"
  1913. "The property that is being used to keep track of effort estimates.
  1914. Effort estimates given in this property need to have the format H:MM."
  1915. :group 'org-properties
  1916. :group 'org-progress
  1917. :type '(string :tag "Property"))
  1918. (defconst org-global-properties-fixed
  1919. '(("VISIBILITY_ALL" . "folded children content all"))
  1920. "List of property/value pairs that can be inherited by any entry.
  1921. These are fixed values, for the preset properties.")
  1922. (defcustom org-global-properties nil
  1923. "List of property/value pairs that can be inherited by any entry.
  1924. You can set buffer-local values for the same purpose in the variable
  1925. `org-file-properties' this by adding lines like
  1926. #+PROPERTY: NAME VALUE"
  1927. :group 'org-properties
  1928. :type '(repeat
  1929. (cons (string :tag "Property")
  1930. (string :tag "Value"))))
  1931. (defvar org-file-properties nil
  1932. "List of property/value pairs that can be inherited by any entry.
  1933. Valid for the current buffer.
  1934. This variable is populated from #+PROPERTY lines.")
  1935. (make-variable-buffer-local 'org-file-properties)
  1936. (defgroup org-agenda nil
  1937. "Options concerning agenda views in Org-mode."
  1938. :tag "Org Agenda"
  1939. :group 'org)
  1940. (defvar org-category nil
  1941. "Variable used by org files to set a category for agenda display.
  1942. Such files should use a file variable to set it, for example
  1943. # -*- mode: org; org-category: \"ELisp\"
  1944. or contain a special line
  1945. #+CATEGORY: ELisp
  1946. If the file does not specify a category, then file's base name
  1947. is used instead.")
  1948. (make-variable-buffer-local 'org-category)
  1949. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1950. (defcustom org-agenda-files nil
  1951. "The files to be used for agenda display.
  1952. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1953. \\[org-remove-file]. You can also use customize to edit the list.
  1954. If an entry is a directory, all files in that directory that are matched by
  1955. `org-agenda-file-regexp' will be part of the file list.
  1956. If the value of the variable is not a list but a single file name, then
  1957. the list of agenda files is actually stored and maintained in that file, one
  1958. agenda file per line."
  1959. :group 'org-agenda
  1960. :type '(choice
  1961. (repeat :tag "List of files and directories" file)
  1962. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1963. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1964. "Regular expression to match files for `org-agenda-files'.
  1965. If any element in the list in that variable contains a directory instead
  1966. of a normal file, all files in that directory that are matched by this
  1967. regular expression will be included."
  1968. :group 'org-agenda
  1969. :type 'regexp)
  1970. (defcustom org-agenda-text-search-extra-files nil
  1971. "List of extra files to be searched by text search commands.
  1972. These files will be search in addition to the agenda files by the
  1973. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1974. Note that these files will only be searched for text search commands,
  1975. not for the other agenda views like todo lists, tag searches or the weekly
  1976. agenda. This variable is intended to list notes and possibly archive files
  1977. that should also be searched by these two commands.
  1978. In fact, if the first element in the list is the symbol `agenda-archives',
  1979. than all archive files of all agenda files will be added to the search
  1980. scope."
  1981. :group 'org-agenda
  1982. :type '(set :greedy t
  1983. (const :tag "Agenda Archives" agenda-archives)
  1984. (repeat :inline t (file))))
  1985. (if (fboundp 'defvaralias)
  1986. (defvaralias 'org-agenda-multi-occur-extra-files
  1987. 'org-agenda-text-search-extra-files))
  1988. (defcustom org-agenda-skip-unavailable-files nil
  1989. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1990. A nil value means to remove them, after a query, from the list."
  1991. :group 'org-agenda
  1992. :type 'boolean)
  1993. (defcustom org-calendar-to-agenda-key [?c]
  1994. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1995. The command `org-calendar-goto-agenda' will be bound to this key. The
  1996. default is the character `c' because then `c' can be used to switch back and
  1997. forth between agenda and calendar."
  1998. :group 'org-agenda
  1999. :type 'sexp)
  2000. (defcustom org-calendar-agenda-action-key [?k]
  2001. "The key to be installed in `calendar-mode-map' for agenda-action.
  2002. The command `org-agenda-action' will be bound to this key. The
  2003. default is the character `k' because we use the same key in the agenda."
  2004. :group 'org-agenda
  2005. :type 'sexp)
  2006. (eval-after-load "calendar"
  2007. '(progn
  2008. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2009. 'org-calendar-goto-agenda)
  2010. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2011. 'org-agenda-action)))
  2012. (defgroup org-latex nil
  2013. "Options for embedding LaTeX code into Org-mode."
  2014. :tag "Org LaTeX"
  2015. :group 'org)
  2016. (defcustom org-format-latex-options
  2017. '(:foreground default :background default :scale 1.0
  2018. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2019. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2020. "Options for creating images from LaTeX fragments.
  2021. This is a property list with the following properties:
  2022. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2023. `default' means use the foreground of the default face.
  2024. :background the background color, or \"Transparent\".
  2025. `default' means use the background of the default face.
  2026. :scale a scaling factor for the size of the images.
  2027. :html-foreground, :html-background, :html-scale
  2028. the same numbers for HTML export.
  2029. :matchers a list indicating which matchers should be used to
  2030. find LaTeX fragments. Valid members of this list are:
  2031. \"begin\" find environments
  2032. \"$1\" find single characters surrounded by $.$
  2033. \"$\" find math expressions surrounded by $...$
  2034. \"$$\" find math expressions surrounded by $$....$$
  2035. \"\\(\" find math expressions surrounded by \\(...\\)
  2036. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2037. :group 'org-latex
  2038. :type 'plist)
  2039. (defcustom org-format-latex-header "\\documentclass{article}
  2040. \\usepackage{fullpage} % do not remove
  2041. \\usepackage{amssymb}
  2042. \\usepackage[usenames]{color}
  2043. \\usepackage{amsmath}
  2044. \\usepackage{latexsym}
  2045. \\usepackage[mathscr]{eucal}
  2046. \\pagestyle{empty} % do not remove"
  2047. "The document header used for processing LaTeX fragments."
  2048. :group 'org-latex
  2049. :type 'string)
  2050. (defgroup org-font-lock nil
  2051. "Font-lock settings for highlighting in Org-mode."
  2052. :tag "Org Font Lock"
  2053. :group 'org)
  2054. (defcustom org-level-color-stars-only nil
  2055. "Non-nil means fontify only the stars in each headline.
  2056. When nil, the entire headline is fontified.
  2057. Changing it requires restart of `font-lock-mode' to become effective
  2058. also in regions already fontified."
  2059. :group 'org-font-lock
  2060. :type 'boolean)
  2061. (defcustom org-hide-leading-stars nil
  2062. "Non-nil means, hide the first N-1 stars in a headline.
  2063. This works by using the face `org-hide' for these stars. This
  2064. face is white for a light background, and black for a dark
  2065. background. You may have to customize the face `org-hide' to
  2066. make this work.
  2067. Changing it requires restart of `font-lock-mode' to become effective
  2068. also in regions already fontified.
  2069. You may also set this on a per-file basis by adding one of the following
  2070. lines to the buffer:
  2071. #+STARTUP: hidestars
  2072. #+STARTUP: showstars"
  2073. :group 'org-font-lock
  2074. :type 'boolean)
  2075. (defcustom org-fontify-done-headline nil
  2076. "Non-nil means, change the face of a headline if it is marked DONE.
  2077. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2078. When this is non-nil, the headline after the keyword is set to the
  2079. `org-headline-done' as an additional indication."
  2080. :group 'org-font-lock
  2081. :type 'boolean)
  2082. (defcustom org-fontify-emphasized-text t
  2083. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2084. Changing this variable requires a restart of Emacs to take effect."
  2085. :group 'org-font-lock
  2086. :type 'boolean)
  2087. (defcustom org-highlight-latex-fragments-and-specials nil
  2088. "Non-nil means, fontify what is treated specially by the exporters."
  2089. :group 'org-font-lock
  2090. :type 'boolean)
  2091. (defcustom org-hide-emphasis-markers nil
  2092. "Non-nil mean font-lock should hide the emphasis marker characters."
  2093. :group 'org-font-lock
  2094. :type 'boolean)
  2095. (defvar org-emph-re nil
  2096. "Regular expression for matching emphasis.")
  2097. (defvar org-verbatim-re nil
  2098. "Regular expression for matching verbatim text.")
  2099. (defvar org-emphasis-regexp-components) ; defined just below
  2100. (defvar org-emphasis-alist) ; defined just below
  2101. (defun org-set-emph-re (var val)
  2102. "Set variable and compute the emphasis regular expression."
  2103. (set var val)
  2104. (when (and (boundp 'org-emphasis-alist)
  2105. (boundp 'org-emphasis-regexp-components)
  2106. org-emphasis-alist org-emphasis-regexp-components)
  2107. (let* ((e org-emphasis-regexp-components)
  2108. (pre (car e))
  2109. (post (nth 1 e))
  2110. (border (nth 2 e))
  2111. (body (nth 3 e))
  2112. (nl (nth 4 e))
  2113. (body1 (concat body "*?"))
  2114. (markers (mapconcat 'car org-emphasis-alist ""))
  2115. (vmarkers (mapconcat
  2116. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2117. org-emphasis-alist "")))
  2118. ;; make sure special characters appear at the right position in the class
  2119. (if (string-match "\\^" markers)
  2120. (setq markers (concat (replace-match "" t t markers) "^")))
  2121. (if (string-match "-" markers)
  2122. (setq markers (concat (replace-match "" t t markers) "-")))
  2123. (if (string-match "\\^" vmarkers)
  2124. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2125. (if (string-match "-" vmarkers)
  2126. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2127. (if (> nl 0)
  2128. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2129. (int-to-string nl) "\\}")))
  2130. ;; Make the regexp
  2131. (setq org-emph-re
  2132. (concat "\\([" pre "]\\|^\\)"
  2133. "\\("
  2134. "\\([" markers "]\\)"
  2135. "\\("
  2136. "[^" border "]\\|"
  2137. "[^" border "]"
  2138. body1
  2139. "[^" border "]"
  2140. "\\)"
  2141. "\\3\\)"
  2142. "\\([" post "]\\|$\\)"))
  2143. (setq org-verbatim-re
  2144. (concat "\\([" pre "]\\|^\\)"
  2145. "\\("
  2146. "\\([" vmarkers "]\\)"
  2147. "\\("
  2148. "[^" border "]\\|"
  2149. "[^" border "]"
  2150. body1
  2151. "[^" border "]"
  2152. "\\)"
  2153. "\\3\\)"
  2154. "\\([" post "]\\|$\\)")))))
  2155. (defcustom org-emphasis-regexp-components
  2156. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2157. "Components used to build the regular expression for emphasis.
  2158. This is a list with 6 entries. Terminology: In an emphasis string
  2159. like \" *strong word* \", we call the initial space PREMATCH, the final
  2160. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2161. and \"trong wor\" is the body. The different components in this variable
  2162. specify what is allowed/forbidden in each part:
  2163. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2164. post Chars allowed as postmatch. End of line will be allowed too.
  2165. border The chars *forbidden* as border characters.
  2166. body-regexp A regexp like \".\" to match a body character. Don't use
  2167. non-shy groups here, and don't allow newline here.
  2168. newline The maximum number of newlines allowed in an emphasis exp.
  2169. Use customize to modify this, or restart Emacs after changing it."
  2170. :group 'org-font-lock
  2171. :set 'org-set-emph-re
  2172. :type '(list
  2173. (sexp :tag "Allowed chars in pre ")
  2174. (sexp :tag "Allowed chars in post ")
  2175. (sexp :tag "Forbidden chars in border ")
  2176. (sexp :tag "Regexp for body ")
  2177. (integer :tag "number of newlines allowed")
  2178. (option (boolean :tag "Please ignore this button"))))
  2179. (defcustom org-emphasis-alist
  2180. `(("*" bold "<b>" "</b>")
  2181. ("/" italic "<i>" "</i>")
  2182. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2183. ("=" org-code "<code>" "</code>" verbatim)
  2184. ("~" org-verbatim "<code>" "</code>" verbatim)
  2185. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2186. "<del>" "</del>")
  2187. )
  2188. "Special syntax for emphasized text.
  2189. Text starting and ending with a special character will be emphasized, for
  2190. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2191. characters, the face to be used by font-lock for highlighting in Org-mode
  2192. Emacs buffers, and the HTML tags to be used for this.
  2193. Use customize to modify this, or restart Emacs after changing it."
  2194. :group 'org-font-lock
  2195. :set 'org-set-emph-re
  2196. :type '(repeat
  2197. (list
  2198. (string :tag "Marker character")
  2199. (choice
  2200. (face :tag "Font-lock-face")
  2201. (plist :tag "Face property list"))
  2202. (string :tag "HTML start tag")
  2203. (string :tag "HTML end tag")
  2204. (option (const verbatim)))))
  2205. ;;; Miscellaneous options
  2206. (defgroup org-completion nil
  2207. "Completion in Org-mode."
  2208. :tag "Org Completion"
  2209. :group 'org)
  2210. (defcustom org-completion-use-ido nil
  2211. "Non-nil means, use ido completion wherever possible.
  2212. Note that `ido-mode' must be active for this variable to be relevant.
  2213. If you decide to turn this variable on, you might well want to turn off
  2214. `org-outline-path-complete-in-steps'."
  2215. :group 'org-completion
  2216. :type 'boolean)
  2217. (defcustom org-completion-fallback-command 'hippie-expand
  2218. "The expansion command called by \\[org-complete] in normal context.
  2219. Normal means, no org-mode-specific context."
  2220. :group 'org-completion
  2221. :type 'function)
  2222. ;;; Functions and variables from ther packages
  2223. ;; Declared here to avoid compiler warnings
  2224. ;; XEmacs only
  2225. (defvar outline-mode-menu-heading)
  2226. (defvar outline-mode-menu-show)
  2227. (defvar outline-mode-menu-hide)
  2228. (defvar zmacs-regions) ; XEmacs regions
  2229. ;; Emacs only
  2230. (defvar mark-active)
  2231. ;; Various packages
  2232. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2233. (declare-function calendar-forward-day "cal-move" (arg))
  2234. (declare-function calendar-goto-date "cal-move" (date))
  2235. (declare-function calendar-goto-today "cal-move" ())
  2236. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2237. (defvar calc-embedded-close-formula)
  2238. (defvar calc-embedded-open-formula)
  2239. (declare-function cdlatex-tab "ext:cdlatex" ())
  2240. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2241. (defvar font-lock-unfontify-region-function)
  2242. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2243. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2244. (defvar iswitchb-temp-buflist)
  2245. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2246. (defvar org-agenda-tags-todo-honor-ignore-options)
  2247. (declare-function org-agenda-skip "org-agenda" ())
  2248. (declare-function org-format-agenda-item "org-agenda"
  2249. (extra txt &optional category tags dotime noprefix remove-re))
  2250. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2251. (declare-function org-agenda-change-all-lines "org-agenda"
  2252. (newhead hdmarker &optional fixface just-this))
  2253. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2254. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2255. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2256. (beg end))
  2257. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2258. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2259. "org-agenda" (&optional end))
  2260. (declare-function parse-time-string "parse-time" (string))
  2261. (declare-function remember "remember" (&optional initial))
  2262. (declare-function remember-buffer-desc "remember" ())
  2263. (declare-function remember-finalize "remember" ())
  2264. (defvar remember-save-after-remembering)
  2265. (defvar remember-data-file)
  2266. (defvar remember-register)
  2267. (defvar remember-buffer)
  2268. (defvar remember-handler-functions)
  2269. (defvar remember-annotation-functions)
  2270. (defvar texmathp-why)
  2271. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2272. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2273. (defvar w3m-current-url)
  2274. (defvar w3m-current-title)
  2275. (defvar org-latex-regexps)
  2276. ;;; Autoload and prepare some org modules
  2277. ;; Some table stuff that needs to be defined here, because it is used
  2278. ;; by the functions setting up org-mode or checking for table context.
  2279. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2280. "Detects an org-type or table-type table.")
  2281. (defconst org-table-line-regexp "^[ \t]*|"
  2282. "Detects an org-type table line.")
  2283. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2284. "Detects an org-type table line.")
  2285. (defconst org-table-hline-regexp "^[ \t]*|-"
  2286. "Detects an org-type table hline.")
  2287. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2288. "Detects a table-type table hline.")
  2289. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2290. "Searching from within a table (any type) this finds the first line
  2291. outside the table.")
  2292. ;; Autoload the functions in org-table.el that are needed by functions here.
  2293. (eval-and-compile
  2294. (org-autoload "org-table"
  2295. '(org-table-align org-table-begin org-table-blank-field
  2296. org-table-convert org-table-convert-region org-table-copy-down
  2297. org-table-copy-region org-table-create
  2298. org-table-create-or-convert-from-region
  2299. org-table-create-with-table.el org-table-current-dline
  2300. org-table-cut-region org-table-delete-column org-table-edit-field
  2301. org-table-edit-formulas org-table-end org-table-eval-formula
  2302. org-table-export org-table-field-info
  2303. org-table-get-stored-formulas org-table-goto-column
  2304. org-table-hline-and-move org-table-import org-table-insert-column
  2305. org-table-insert-hline org-table-insert-row org-table-iterate
  2306. org-table-justify-field-maybe org-table-kill-row
  2307. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2308. org-table-move-column org-table-move-column-left
  2309. org-table-move-column-right org-table-move-row
  2310. org-table-move-row-down org-table-move-row-up
  2311. org-table-next-field org-table-next-row org-table-paste-rectangle
  2312. org-table-previous-field org-table-recalculate
  2313. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2314. org-table-toggle-coordinate-overlays
  2315. org-table-toggle-formula-debugger org-table-wrap-region
  2316. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2317. (defun org-at-table-p (&optional table-type)
  2318. "Return t if the cursor is inside an org-type table.
  2319. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2320. (if org-enable-table-editor
  2321. (save-excursion
  2322. (beginning-of-line 1)
  2323. (looking-at (if table-type org-table-any-line-regexp
  2324. org-table-line-regexp)))
  2325. nil))
  2326. (defsubst org-table-p () (org-at-table-p))
  2327. (defun org-at-table.el-p ()
  2328. "Return t if and only if we are at a table.el table."
  2329. (and (org-at-table-p 'any)
  2330. (save-excursion
  2331. (goto-char (org-table-begin 'any))
  2332. (looking-at org-table1-hline-regexp))))
  2333. (defun org-table-recognize-table.el ()
  2334. "If there is a table.el table nearby, recognize it and move into it."
  2335. (if org-table-tab-recognizes-table.el
  2336. (if (org-at-table.el-p)
  2337. (progn
  2338. (beginning-of-line 1)
  2339. (if (looking-at org-table-dataline-regexp)
  2340. nil
  2341. (if (looking-at org-table1-hline-regexp)
  2342. (progn
  2343. (beginning-of-line 2)
  2344. (if (looking-at org-table-any-border-regexp)
  2345. (beginning-of-line -1)))))
  2346. (if (re-search-forward "|" (org-table-end t) t)
  2347. (progn
  2348. (require 'table)
  2349. (if (table--at-cell-p (point))
  2350. t
  2351. (message "recognizing table.el table...")
  2352. (table-recognize-table)
  2353. (message "recognizing table.el table...done")))
  2354. (error "This should not happen..."))
  2355. t)
  2356. nil)
  2357. nil))
  2358. (defun org-at-table-hline-p ()
  2359. "Return t if the cursor is inside a hline in a table."
  2360. (if org-enable-table-editor
  2361. (save-excursion
  2362. (beginning-of-line 1)
  2363. (looking-at org-table-hline-regexp))
  2364. nil))
  2365. (defvar org-table-clean-did-remove-column nil)
  2366. (defun org-table-map-tables (function)
  2367. "Apply FUNCTION to the start of all tables in the buffer."
  2368. (save-excursion
  2369. (save-restriction
  2370. (widen)
  2371. (goto-char (point-min))
  2372. (while (re-search-forward org-table-any-line-regexp nil t)
  2373. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2374. (beginning-of-line 1)
  2375. (if (looking-at org-table-line-regexp)
  2376. (save-excursion (funcall function)))
  2377. (re-search-forward org-table-any-border-regexp nil 1))))
  2378. (message "Mapping tables: done"))
  2379. ;; Declare and autoload functions from org-exp.el
  2380. (declare-function org-default-export-plist "org-exp")
  2381. (declare-function org-infile-export-plist "org-exp")
  2382. (declare-function org-get-current-options "org-exp")
  2383. (eval-and-compile
  2384. (org-autoload "org-exp"
  2385. '(org-export org-export-as-ascii org-export-visible
  2386. org-insert-export-options-template org-export-as-html-and-open
  2387. org-export-as-html-batch org-export-as-html-to-buffer
  2388. org-replace-region-by-html org-export-region-as-html
  2389. org-export-as-html org-export-icalendar-this-file
  2390. org-export-icalendar-all-agenda-files
  2391. org-table-clean-before-export
  2392. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2393. ;; Declare and autoload functions from org-agenda.el
  2394. (eval-and-compile
  2395. (org-autoload "org-agenda"
  2396. '(org-agenda org-agenda-list org-search-view
  2397. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2398. org-diary org-agenda-to-appt
  2399. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2400. ;; Autoload org-remember
  2401. (eval-and-compile
  2402. (org-autoload "org-remember"
  2403. '(org-remember-insinuate org-remember-annotation
  2404. org-remember-apply-template org-remember org-remember-handler)))
  2405. ;; Autoload org-clock.el
  2406. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2407. (beg end))
  2408. (declare-function org-clock-update-mode-line "org-clock" ())
  2409. (defvar org-clock-start-time)
  2410. (defvar org-clock-marker (make-marker)
  2411. "Marker recording the last clock-in.")
  2412. (eval-and-compile
  2413. (org-autoload
  2414. "org-clock"
  2415. '(org-clock-in org-clock-out org-clock-cancel
  2416. org-clock-goto org-clock-sum org-clock-display
  2417. org-clock-remove-overlays org-clock-report
  2418. org-clocktable-shift org-dblock-write:clocktable
  2419. org-get-clocktable)))
  2420. (defun org-clock-update-time-maybe ()
  2421. "If this is a CLOCK line, update it and return t.
  2422. Otherwise, return nil."
  2423. (interactive)
  2424. (save-excursion
  2425. (beginning-of-line 1)
  2426. (skip-chars-forward " \t")
  2427. (when (looking-at org-clock-string)
  2428. (let ((re (concat "[ \t]*" org-clock-string
  2429. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2430. "\\([ \t]*=>.*\\)?\\)?"))
  2431. ts te h m s neg)
  2432. (cond
  2433. ((not (looking-at re))
  2434. nil)
  2435. ((not (match-end 2))
  2436. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2437. (> org-clock-marker (point))
  2438. (<= org-clock-marker (point-at-eol)))
  2439. ;; The clock is running here
  2440. (setq org-clock-start-time
  2441. (apply 'encode-time
  2442. (org-parse-time-string (match-string 1))))
  2443. (org-clock-update-mode-line)))
  2444. (t
  2445. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2446. (end-of-line 1)
  2447. (setq ts (match-string 1)
  2448. te (match-string 3))
  2449. (setq s (- (time-to-seconds
  2450. (apply 'encode-time (org-parse-time-string te)))
  2451. (time-to-seconds
  2452. (apply 'encode-time (org-parse-time-string ts))))
  2453. neg (< s 0)
  2454. s (abs s)
  2455. h (floor (/ s 3600))
  2456. s (- s (* 3600 h))
  2457. m (floor (/ s 60))
  2458. s (- s (* 60 s)))
  2459. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2460. t))))))
  2461. (defun org-check-running-clock ()
  2462. "Check if the current buffer contains the running clock.
  2463. If yes, offer to stop it and to save the buffer with the changes."
  2464. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2465. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2466. (buffer-name))))
  2467. (org-clock-out)
  2468. (when (y-or-n-p "Save changed buffer?")
  2469. (save-buffer))))
  2470. (defun org-clocktable-try-shift (dir n)
  2471. "Check if this line starts a clock table, if yes, shift the time block."
  2472. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2473. (org-clocktable-shift dir n)))
  2474. ;; Autoload org-timer.el
  2475. ;(declare-function org-timer "org-timer")
  2476. (eval-and-compile
  2477. (org-autoload
  2478. "org-timer"
  2479. '(org-timer-start org-timer org-timer-item
  2480. org-timer-change-times-in-region)))
  2481. ;; Autoload archiving code
  2482. ;; The stuff that is needed for cycling and tags has to be defined here.
  2483. (defgroup org-archive nil
  2484. "Options concerning archiving in Org-mode."
  2485. :tag "Org Archive"
  2486. :group 'org-structure)
  2487. (defcustom org-archive-location "%s_archive::"
  2488. "The location where subtrees should be archived.
  2489. The value of this variable is a string, consisting of two parts,
  2490. separated by a double-colon. The first part is a filename and
  2491. the second part is a headline.
  2492. When the filename is omitted, archiving happens in the same file.
  2493. %s in the filename will be replaced by the current file
  2494. name (without the directory part). Archiving to a different file
  2495. is useful to keep archived entries from contributing to the
  2496. Org-mode Agenda.
  2497. The archived entries will be filed as subtrees of the specified
  2498. headline. When the headline is omitted, the subtrees are simply
  2499. filed away at the end of the file, as top-level entries. Also in
  2500. the heading you can use %s to represent the file name, this can be
  2501. useful when using the same archive for a number of different files.
  2502. Here are a few examples:
  2503. \"%s_archive::\"
  2504. If the current file is Projects.org, archive in file
  2505. Projects.org_archive, as top-level trees. This is the default.
  2506. \"::* Archived Tasks\"
  2507. Archive in the current file, under the top-level headline
  2508. \"* Archived Tasks\".
  2509. \"~/org/archive.org::\"
  2510. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2511. \"~/org/archive.org::From %s\"
  2512. Archive in file ~/org/archive.org (absolute path), und headlines
  2513. \"From FILENAME\" where file name is the current file name.
  2514. \"basement::** Finished Tasks\"
  2515. Archive in file ./basement (relative path), as level 3 trees
  2516. below the level 2 heading \"** Finished Tasks\".
  2517. You may set this option on a per-file basis by adding to the buffer a
  2518. line like
  2519. #+ARCHIVE: basement::** Finished Tasks
  2520. You may also define it locally for a subtree by setting an ARCHIVE property
  2521. in the entry. If such a property is found in an entry, or anywhere up
  2522. the hierarchy, it will be used."
  2523. :group 'org-archive
  2524. :type 'string)
  2525. (defcustom org-archive-tag "ARCHIVE"
  2526. "The tag that marks a subtree as archived.
  2527. An archived subtree does not open during visibility cycling, and does
  2528. not contribute to the agenda listings.
  2529. After changing this, font-lock must be restarted in the relevant buffers to
  2530. get the proper fontification."
  2531. :group 'org-archive
  2532. :group 'org-keywords
  2533. :type 'string)
  2534. (defcustom org-agenda-skip-archived-trees t
  2535. "Non-nil means, the agenda will skip any items located in archived trees.
  2536. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2537. variable is no longer recommended, you should leave it at the value t.
  2538. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2539. :group 'org-archive
  2540. :group 'org-agenda-skip
  2541. :type 'boolean)
  2542. (defcustom org-cycle-open-archived-trees nil
  2543. "Non-nil means, `org-cycle' will open archived trees.
  2544. An archived tree is a tree marked with the tag ARCHIVE.
  2545. When nil, archived trees will stay folded. You can still open them with
  2546. normal outline commands like `show-all', but not with the cycling commands."
  2547. :group 'org-archive
  2548. :group 'org-cycle
  2549. :type 'boolean)
  2550. (defcustom org-sparse-tree-open-archived-trees nil
  2551. "Non-nil means sparse tree construction shows matches in archived trees.
  2552. When nil, matches in these trees are highlighted, but the trees are kept in
  2553. collapsed state."
  2554. :group 'org-archive
  2555. :group 'org-sparse-trees
  2556. :type 'boolean)
  2557. (defun org-cycle-hide-archived-subtrees (state)
  2558. "Re-hide all archived subtrees after a visibility state change."
  2559. (when (and (not org-cycle-open-archived-trees)
  2560. (not (memq state '(overview folded))))
  2561. (save-excursion
  2562. (let* ((globalp (memq state '(contents all)))
  2563. (beg (if globalp (point-min) (point)))
  2564. (end (if globalp (point-max) (org-end-of-subtree t))))
  2565. (org-hide-archived-subtrees beg end)
  2566. (goto-char beg)
  2567. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2568. (message "%s" (substitute-command-keys
  2569. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2570. (defun org-force-cycle-archived ()
  2571. "Cycle subtree even if it is archived."
  2572. (interactive)
  2573. (setq this-command 'org-cycle)
  2574. (let ((org-cycle-open-archived-trees t))
  2575. (call-interactively 'org-cycle)))
  2576. (defun org-hide-archived-subtrees (beg end)
  2577. "Re-hide all archived subtrees after a visibility state change."
  2578. (save-excursion
  2579. (let* ((re (concat ":" org-archive-tag ":")))
  2580. (goto-char beg)
  2581. (while (re-search-forward re end t)
  2582. (and (org-on-heading-p) (hide-subtree))
  2583. (org-end-of-subtree t)))))
  2584. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2585. (eval-and-compile
  2586. (org-autoload "org-archive"
  2587. '(org-add-archive-files org-archive-subtree
  2588. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2589. ;; Autoload Column View Code
  2590. (declare-function org-columns-number-to-string "org-colview")
  2591. (declare-function org-columns-get-format-and-top-level "org-colview")
  2592. (declare-function org-columns-compute "org-colview")
  2593. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2594. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2595. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2596. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2597. ;; Autoload ID code
  2598. (declare-function org-id-store-link "org-id")
  2599. (org-autoload "org-id"
  2600. '(org-id-get-create org-id-new org-id-copy org-id-get
  2601. org-id-get-with-outline-path-completion
  2602. org-id-get-with-outline-drilling
  2603. org-id-goto org-id-find org-id-store-link))
  2604. ;;; Variables for pre-computed regular expressions, all buffer local
  2605. (defvar org-drawer-regexp nil
  2606. "Matches first line of a hidden block.")
  2607. (make-variable-buffer-local 'org-drawer-regexp)
  2608. (defvar org-todo-regexp nil
  2609. "Matches any of the TODO state keywords.")
  2610. (make-variable-buffer-local 'org-todo-regexp)
  2611. (defvar org-not-done-regexp nil
  2612. "Matches any of the TODO state keywords except the last one.")
  2613. (make-variable-buffer-local 'org-not-done-regexp)
  2614. (defvar org-todo-line-regexp nil
  2615. "Matches a headline and puts TODO state into group 2 if present.")
  2616. (make-variable-buffer-local 'org-todo-line-regexp)
  2617. (defvar org-complex-heading-regexp nil
  2618. "Matches a headline and puts everything into groups:
  2619. group 1: the stars
  2620. group 2: The todo keyword, maybe
  2621. group 3: Priority cookie
  2622. group 4: True headline
  2623. group 5: Tags")
  2624. (make-variable-buffer-local 'org-complex-heading-regexp)
  2625. (defvar org-todo-line-tags-regexp nil
  2626. "Matches a headline and puts TODO state into group 2 if present.
  2627. Also put tags into group 4 if tags are present.")
  2628. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2629. (defvar org-nl-done-regexp nil
  2630. "Matches newline followed by a headline with the DONE keyword.")
  2631. (make-variable-buffer-local 'org-nl-done-regexp)
  2632. (defvar org-looking-at-done-regexp nil
  2633. "Matches the DONE keyword a point.")
  2634. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2635. (defvar org-ds-keyword-length 12
  2636. "Maximum length of the Deadline and SCHEDULED keywords.")
  2637. (make-variable-buffer-local 'org-ds-keyword-length)
  2638. (defvar org-deadline-regexp nil
  2639. "Matches the DEADLINE keyword.")
  2640. (make-variable-buffer-local 'org-deadline-regexp)
  2641. (defvar org-deadline-time-regexp nil
  2642. "Matches the DEADLINE keyword together with a time stamp.")
  2643. (make-variable-buffer-local 'org-deadline-time-regexp)
  2644. (defvar org-deadline-line-regexp nil
  2645. "Matches the DEADLINE keyword and the rest of the line.")
  2646. (make-variable-buffer-local 'org-deadline-line-regexp)
  2647. (defvar org-scheduled-regexp nil
  2648. "Matches the SCHEDULED keyword.")
  2649. (make-variable-buffer-local 'org-scheduled-regexp)
  2650. (defvar org-scheduled-time-regexp nil
  2651. "Matches the SCHEDULED keyword together with a time stamp.")
  2652. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2653. (defvar org-closed-time-regexp nil
  2654. "Matches the CLOSED keyword together with a time stamp.")
  2655. (make-variable-buffer-local 'org-closed-time-regexp)
  2656. (defvar org-keyword-time-regexp nil
  2657. "Matches any of the 4 keywords, together with the time stamp.")
  2658. (make-variable-buffer-local 'org-keyword-time-regexp)
  2659. (defvar org-keyword-time-not-clock-regexp nil
  2660. "Matches any of the 3 keywords, together with the time stamp.")
  2661. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2662. (defvar org-maybe-keyword-time-regexp nil
  2663. "Matches a timestamp, possibly preceeded by a keyword.")
  2664. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2665. (defvar org-planning-or-clock-line-re nil
  2666. "Matches a line with planning or clock info.")
  2667. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2668. (defconst org-plain-time-of-day-regexp
  2669. (concat
  2670. "\\(\\<[012]?[0-9]"
  2671. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2672. "\\(--?"
  2673. "\\(\\<[012]?[0-9]"
  2674. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2675. "\\)?")
  2676. "Regular expression to match a plain time or time range.
  2677. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2678. groups carry important information:
  2679. 0 the full match
  2680. 1 the first time, range or not
  2681. 8 the second time, if it is a range.")
  2682. (defconst org-plain-time-extension-regexp
  2683. (concat
  2684. "\\(\\<[012]?[0-9]"
  2685. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2686. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2687. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2688. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2689. groups carry important information:
  2690. 0 the full match
  2691. 7 hours of duration
  2692. 9 minutes of duration")
  2693. (defconst org-stamp-time-of-day-regexp
  2694. (concat
  2695. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2696. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2697. "\\(--?"
  2698. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2699. "Regular expression to match a timestamp time or time range.
  2700. After a match, the following groups carry important information:
  2701. 0 the full match
  2702. 1 date plus weekday, for backreferencing to make sure both times on same day
  2703. 2 the first time, range or not
  2704. 4 the second time, if it is a range.")
  2705. (defconst org-startup-options
  2706. '(("fold" org-startup-folded t)
  2707. ("overview" org-startup-folded t)
  2708. ("nofold" org-startup-folded nil)
  2709. ("showall" org-startup-folded nil)
  2710. ("content" org-startup-folded content)
  2711. ("hidestars" org-hide-leading-stars t)
  2712. ("showstars" org-hide-leading-stars nil)
  2713. ("odd" org-odd-levels-only t)
  2714. ("oddeven" org-odd-levels-only nil)
  2715. ("align" org-startup-align-all-tables t)
  2716. ("noalign" org-startup-align-all-tables nil)
  2717. ("customtime" org-display-custom-times t)
  2718. ("logdone" org-log-done time)
  2719. ("lognotedone" org-log-done note)
  2720. ("nologdone" org-log-done nil)
  2721. ("lognoteclock-out" org-log-note-clock-out t)
  2722. ("nolognoteclock-out" org-log-note-clock-out nil)
  2723. ("logrepeat" org-log-repeat state)
  2724. ("lognoterepeat" org-log-repeat note)
  2725. ("nologrepeat" org-log-repeat nil)
  2726. ("fninline" org-footnote-define-inline t)
  2727. ("nofninline" org-footnote-define-inline nil)
  2728. ("fnlocal" org-footnote-section nil)
  2729. ("fnauto" org-footnote-auto-label t)
  2730. ("fnprompt" org-footnote-auto-label nil)
  2731. ("fnconfirm" org-footnote-auto-label confirm)
  2732. ("fnplain" org-footnote-auto-label plain)
  2733. ("constcgs" constants-unit-system cgs)
  2734. ("constSI" constants-unit-system SI))
  2735. "Variable associated with STARTUP options for org-mode.
  2736. Each element is a list of three items: The startup options as written
  2737. in the #+STARTUP line, the corresponding variable, and the value to
  2738. set this variable to if the option is found. An optional forth element PUSH
  2739. means to push this value onto the list in the variable.")
  2740. (defun org-set-regexps-and-options ()
  2741. "Precompute regular expressions for current buffer."
  2742. (when (org-mode-p)
  2743. (org-set-local 'org-todo-kwd-alist nil)
  2744. (org-set-local 'org-todo-key-alist nil)
  2745. (org-set-local 'org-todo-key-trigger nil)
  2746. (org-set-local 'org-todo-keywords-1 nil)
  2747. (org-set-local 'org-done-keywords nil)
  2748. (org-set-local 'org-todo-heads nil)
  2749. (org-set-local 'org-todo-sets nil)
  2750. (org-set-local 'org-todo-log-states nil)
  2751. (org-set-local 'org-file-properties nil)
  2752. (org-set-local 'org-file-tags nil)
  2753. (let ((re (org-make-options-regexp
  2754. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2755. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2756. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2757. (splitre "[ \t]+")
  2758. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2759. tail sep kws1 prio props ftags drawers
  2760. ext-setup-or-nil setup-contents (start 0))
  2761. (save-excursion
  2762. (save-restriction
  2763. (widen)
  2764. (goto-char (point-min))
  2765. (while (or (and ext-setup-or-nil
  2766. (string-match re ext-setup-or-nil start)
  2767. (setq start (match-end 0)))
  2768. (and (setq ext-setup-or-nil nil start 0)
  2769. (re-search-forward re nil t)))
  2770. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2771. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2772. (cond
  2773. ((equal key "CATEGORY")
  2774. (if (string-match "[ \t]+$" value)
  2775. (setq value (replace-match "" t t value)))
  2776. (setq cat value))
  2777. ((member key '("SEQ_TODO" "TODO"))
  2778. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2779. ((equal key "TYP_TODO")
  2780. (push (cons 'type (org-split-string value splitre)) kwds))
  2781. ((equal key "TAGS")
  2782. (setq tags (append tags (org-split-string value splitre))))
  2783. ((equal key "COLUMNS")
  2784. (org-set-local 'org-columns-default-format value))
  2785. ((equal key "LINK")
  2786. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2787. (push (cons (match-string 1 value)
  2788. (org-trim (match-string 2 value)))
  2789. links)))
  2790. ((equal key "PRIORITIES")
  2791. (setq prio (org-split-string value " +")))
  2792. ((equal key "PROPERTY")
  2793. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2794. (push (cons (match-string 1 value) (match-string 2 value))
  2795. props)))
  2796. ((equal key "FILETAGS")
  2797. (when (string-match "\\S-" value)
  2798. (setq ftags
  2799. (append
  2800. ftags
  2801. (apply 'append
  2802. (mapcar (lambda (x) (org-split-string x ":"))
  2803. (org-split-string value)))))))
  2804. ((equal key "DRAWERS")
  2805. (setq drawers (org-split-string value splitre)))
  2806. ((equal key "CONSTANTS")
  2807. (setq const (append const (org-split-string value splitre))))
  2808. ((equal key "STARTUP")
  2809. (let ((opts (org-split-string value splitre))
  2810. l var val)
  2811. (while (setq l (pop opts))
  2812. (when (setq l (assoc l org-startup-options))
  2813. (setq var (nth 1 l) val (nth 2 l))
  2814. (if (not (nth 3 l))
  2815. (set (make-local-variable var) val)
  2816. (if (not (listp (symbol-value var)))
  2817. (set (make-local-variable var) nil))
  2818. (set (make-local-variable var) (symbol-value var))
  2819. (add-to-list var val))))))
  2820. ((equal key "ARCHIVE")
  2821. (string-match " *$" value)
  2822. (setq arch (replace-match "" t t value))
  2823. (remove-text-properties 0 (length arch)
  2824. '(face t fontified t) arch))
  2825. ((equal key "SETUPFILE")
  2826. (setq setup-contents (org-file-contents
  2827. (expand-file-name
  2828. (org-remove-double-quotes value))
  2829. 'noerror))
  2830. (if (not ext-setup-or-nil)
  2831. (setq ext-setup-or-nil setup-contents start 0)
  2832. (setq ext-setup-or-nil
  2833. (concat (substring ext-setup-or-nil 0 start)
  2834. "\n" setup-contents "\n"
  2835. (substring ext-setup-or-nil start)))))
  2836. ))))
  2837. (when cat
  2838. (org-set-local 'org-category (intern cat))
  2839. (push (cons "CATEGORY" cat) props))
  2840. (when prio
  2841. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2842. (setq prio (mapcar 'string-to-char prio))
  2843. (org-set-local 'org-highest-priority (nth 0 prio))
  2844. (org-set-local 'org-lowest-priority (nth 1 prio))
  2845. (org-set-local 'org-default-priority (nth 2 prio)))
  2846. (and props (org-set-local 'org-file-properties (nreverse props)))
  2847. (and ftags (org-set-local 'org-file-tags ftags))
  2848. (and drawers (org-set-local 'org-drawers drawers))
  2849. (and arch (org-set-local 'org-archive-location arch))
  2850. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2851. ;; Process the TODO keywords
  2852. (unless kwds
  2853. ;; Use the global values as if they had been given locally.
  2854. (setq kwds (default-value 'org-todo-keywords))
  2855. (if (stringp (car kwds))
  2856. (setq kwds (list (cons org-todo-interpretation
  2857. (default-value 'org-todo-keywords)))))
  2858. (setq kwds (reverse kwds)))
  2859. (setq kwds (nreverse kwds))
  2860. (let (inter kws kw)
  2861. (while (setq kws (pop kwds))
  2862. (setq inter (pop kws) sep (member "|" kws)
  2863. kws0 (delete "|" (copy-sequence kws))
  2864. kwsa nil
  2865. kws1 (mapcar
  2866. (lambda (x)
  2867. ;; 1 2
  2868. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2869. (progn
  2870. (setq kw (match-string 1 x)
  2871. key (and (match-end 2) (match-string 2 x))
  2872. log (org-extract-log-state-settings x))
  2873. (push (cons kw (and key (string-to-char key))) kwsa)
  2874. (and log (push log org-todo-log-states))
  2875. kw)
  2876. (error "Invalid TODO keyword %s" x)))
  2877. kws0)
  2878. kwsa (if kwsa (append '((:startgroup))
  2879. (nreverse kwsa)
  2880. '((:endgroup))))
  2881. hw (car kws1)
  2882. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2883. tail (list inter hw (car dws) (org-last dws)))
  2884. (add-to-list 'org-todo-heads hw 'append)
  2885. (push kws1 org-todo-sets)
  2886. (setq org-done-keywords (append org-done-keywords dws nil))
  2887. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2888. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2889. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2890. (setq org-todo-sets (nreverse org-todo-sets)
  2891. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2892. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2893. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2894. ;; Process the constants
  2895. (when const
  2896. (let (e cst)
  2897. (while (setq e (pop const))
  2898. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2899. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2900. (setq org-table-formula-constants-local cst)))
  2901. ;; Process the tags.
  2902. (when tags
  2903. (let (e tgs)
  2904. (while (setq e (pop tags))
  2905. (cond
  2906. ((equal e "{") (push '(:startgroup) tgs))
  2907. ((equal e "}") (push '(:endgroup) tgs))
  2908. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2909. (push (cons (match-string 1 e)
  2910. (string-to-char (match-string 2 e)))
  2911. tgs))
  2912. (t (push (list e) tgs))))
  2913. (org-set-local 'org-tag-alist nil)
  2914. (while (setq e (pop tgs))
  2915. (or (and (stringp (car e))
  2916. (assoc (car e) org-tag-alist))
  2917. (push e org-tag-alist)))))
  2918. ;; Compute the regular expressions and other local variables
  2919. (if (not org-done-keywords)
  2920. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2921. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2922. (length org-scheduled-string)
  2923. (length org-clock-string)
  2924. (length org-closed-string)))
  2925. org-drawer-regexp
  2926. (concat "^[ \t]*:\\("
  2927. (mapconcat 'regexp-quote org-drawers "\\|")
  2928. "\\):[ \t]*$")
  2929. org-not-done-keywords
  2930. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2931. org-todo-regexp
  2932. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2933. "\\|") "\\)\\>")
  2934. org-not-done-regexp
  2935. (concat "\\<\\("
  2936. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2937. "\\)\\>")
  2938. org-todo-line-regexp
  2939. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2940. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2941. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2942. org-complex-heading-regexp
  2943. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2944. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2945. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2946. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2947. org-nl-done-regexp
  2948. (concat "\n\\*+[ \t]+"
  2949. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2950. "\\)" "\\>")
  2951. org-todo-line-tags-regexp
  2952. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2953. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2954. (org-re
  2955. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2956. org-looking-at-done-regexp
  2957. (concat "^" "\\(?:"
  2958. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2959. "\\>")
  2960. org-deadline-regexp (concat "\\<" org-deadline-string)
  2961. org-deadline-time-regexp
  2962. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2963. org-deadline-line-regexp
  2964. (concat "\\<\\(" org-deadline-string "\\).*")
  2965. org-scheduled-regexp
  2966. (concat "\\<" org-scheduled-string)
  2967. org-scheduled-time-regexp
  2968. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2969. org-closed-time-regexp
  2970. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2971. org-keyword-time-regexp
  2972. (concat "\\<\\(" org-scheduled-string
  2973. "\\|" org-deadline-string
  2974. "\\|" org-closed-string
  2975. "\\|" org-clock-string "\\)"
  2976. " *[[<]\\([^]>]+\\)[]>]")
  2977. org-keyword-time-not-clock-regexp
  2978. (concat "\\<\\(" org-scheduled-string
  2979. "\\|" org-deadline-string
  2980. "\\|" org-closed-string
  2981. "\\)"
  2982. " *[[<]\\([^]>]+\\)[]>]")
  2983. org-maybe-keyword-time-regexp
  2984. (concat "\\(\\<\\(" org-scheduled-string
  2985. "\\|" org-deadline-string
  2986. "\\|" org-closed-string
  2987. "\\|" org-clock-string "\\)\\)?"
  2988. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2989. org-planning-or-clock-line-re
  2990. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2991. "\\|" org-deadline-string
  2992. "\\|" org-closed-string "\\|" org-clock-string
  2993. "\\)\\>\\)")
  2994. )
  2995. (org-compute-latex-and-specials-regexp)
  2996. (org-set-font-lock-defaults))))
  2997. (defun org-file-contents (file &optional noerror)
  2998. "Return the contents of FILE, as a string."
  2999. (if (or (not file)
  3000. (not (file-readable-p file)))
  3001. (if noerror
  3002. (progn
  3003. (message "Cannot read file %s" file)
  3004. (ding) (sit-for 2)
  3005. "")
  3006. (error "Cannot read file %s" file))
  3007. (with-temp-buffer
  3008. (insert-file-contents file)
  3009. (buffer-string))))
  3010. (defun org-extract-log-state-settings (x)
  3011. "Extract the log state setting from a TODO keyword string.
  3012. This will extract info from a string like \"WAIT(w@/!)\"."
  3013. (let (kw key log1 log2)
  3014. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3015. (setq kw (match-string 1 x)
  3016. key (and (match-end 2) (match-string 2 x))
  3017. log1 (and (match-end 3) (match-string 3 x))
  3018. log2 (and (match-end 4) (match-string 4 x)))
  3019. (and (or log1 log2)
  3020. (list kw
  3021. (and log1 (if (equal log1 "!") 'time 'note))
  3022. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3023. (defun org-remove-keyword-keys (list)
  3024. "Remove a pair of parenthesis at the end of each string in LIST."
  3025. (mapcar (lambda (x)
  3026. (if (string-match "(.*)$" x)
  3027. (substring x 0 (match-beginning 0))
  3028. x))
  3029. list))
  3030. ;; FIXME: this could be done much better, using second characters etc.
  3031. (defun org-assign-fast-keys (alist)
  3032. "Assign fast keys to a keyword-key alist.
  3033. Respect keys that are already there."
  3034. (let (new e k c c1 c2 (char ?a))
  3035. (while (setq e (pop alist))
  3036. (cond
  3037. ((equal e '(:startgroup)) (push e new))
  3038. ((equal e '(:endgroup)) (push e new))
  3039. (t
  3040. (setq k (car e) c2 nil)
  3041. (if (cdr e)
  3042. (setq c (cdr e))
  3043. ;; automatically assign a character.
  3044. (setq c1 (string-to-char
  3045. (downcase (substring
  3046. k (if (= (string-to-char k) ?@) 1 0)))))
  3047. (if (or (rassoc c1 new) (rassoc c1 alist))
  3048. (while (or (rassoc char new) (rassoc char alist))
  3049. (setq char (1+ char)))
  3050. (setq c2 c1))
  3051. (setq c (or c2 char)))
  3052. (push (cons k c) new))))
  3053. (nreverse new)))
  3054. ;;; Some variables used in various places
  3055. (defvar org-window-configuration nil
  3056. "Used in various places to store a window configuration.")
  3057. (defvar org-finish-function nil
  3058. "Function to be called when `C-c C-c' is used.
  3059. This is for getting out of special buffers like remember.")
  3060. ;; FIXME: Occasionally check by commenting these, to make sure
  3061. ;; no other functions uses these, forgetting to let-bind them.
  3062. (defvar entry)
  3063. (defvar state)
  3064. (defvar last-state)
  3065. (defvar date)
  3066. (defvar description)
  3067. ;; Defined somewhere in this file, but used before definition.
  3068. (defvar org-html-entities)
  3069. (defvar org-struct-menu)
  3070. (defvar org-org-menu)
  3071. (defvar org-tbl-menu)
  3072. (defvar org-agenda-keymap)
  3073. ;;;; Define the Org-mode
  3074. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3075. (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."))
  3076. ;; We use a before-change function to check if a table might need
  3077. ;; an update.
  3078. (defvar org-table-may-need-update t
  3079. "Indicates that a table might need an update.
  3080. This variable is set by `org-before-change-function'.
  3081. `org-table-align' sets it back to nil.")
  3082. (defun org-before-change-function (beg end)
  3083. "Every change indicates that a table might need an update."
  3084. (setq org-table-may-need-update t))
  3085. (defvar org-mode-map)
  3086. (defvar org-mode-hook nil
  3087. "Mode hook for Org-mode, run after the mode was turned on.")
  3088. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3089. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3090. (defvar org-table-buffer-is-an nil)
  3091. (defconst org-outline-regexp "\\*+ ")
  3092. ;;;###autoload
  3093. (define-derived-mode org-mode outline-mode "Org"
  3094. "Outline-based notes management and organizer, alias
  3095. \"Carsten's outline-mode for keeping track of everything.\"
  3096. Org-mode develops organizational tasks around a NOTES file which
  3097. contains information about projects as plain text. Org-mode is
  3098. implemented on top of outline-mode, which is ideal to keep the content
  3099. of large files well structured. It supports ToDo items, deadlines and
  3100. time stamps, which magically appear in the diary listing of the Emacs
  3101. calendar. Tables are easily created with a built-in table editor.
  3102. Plain text URL-like links connect to websites, emails (VM), Usenet
  3103. messages (Gnus), BBDB entries, and any files related to the project.
  3104. For printing and sharing of notes, an Org-mode file (or a part of it)
  3105. can be exported as a structured ASCII or HTML file.
  3106. The following commands are available:
  3107. \\{org-mode-map}"
  3108. ;; Get rid of Outline menus, they are not needed
  3109. ;; Need to do this here because define-derived-mode sets up
  3110. ;; the keymap so late. Still, it is a waste to call this each time
  3111. ;; we switch another buffer into org-mode.
  3112. (if (featurep 'xemacs)
  3113. (when (boundp 'outline-mode-menu-heading)
  3114. ;; Assume this is Greg's port, it used easymenu
  3115. (easy-menu-remove outline-mode-menu-heading)
  3116. (easy-menu-remove outline-mode-menu-show)
  3117. (easy-menu-remove outline-mode-menu-hide))
  3118. (define-key org-mode-map [menu-bar headings] 'undefined)
  3119. (define-key org-mode-map [menu-bar hide] 'undefined)
  3120. (define-key org-mode-map [menu-bar show] 'undefined))
  3121. (org-load-modules-maybe)
  3122. (easy-menu-add org-org-menu)
  3123. (easy-menu-add org-tbl-menu)
  3124. (org-install-agenda-files-menu)
  3125. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3126. (org-add-to-invisibility-spec '(org-cwidth))
  3127. (when (featurep 'xemacs)
  3128. (org-set-local 'line-move-ignore-invisible t))
  3129. (org-set-local 'outline-regexp org-outline-regexp)
  3130. (org-set-local 'outline-level 'org-outline-level)
  3131. (when (and org-ellipsis
  3132. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3133. (fboundp 'make-glyph-code))
  3134. (unless org-display-table
  3135. (setq org-display-table (make-display-table)))
  3136. (set-display-table-slot
  3137. org-display-table 4
  3138. (vconcat (mapcar
  3139. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3140. org-ellipsis)))
  3141. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3142. (setq buffer-display-table org-display-table))
  3143. (org-set-regexps-and-options)
  3144. (when (and org-tag-faces (not org-tags-special-faces-re))
  3145. ;; tag faces set outside customize.... force initialization.
  3146. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3147. ;; Calc embedded
  3148. (org-set-local 'calc-embedded-open-mode "# ")
  3149. (modify-syntax-entry ?# "<")
  3150. (modify-syntax-entry ?@ "w")
  3151. (if org-startup-truncated (setq truncate-lines t))
  3152. (org-set-local 'font-lock-unfontify-region-function
  3153. 'org-unfontify-region)
  3154. ;; Activate before-change-function
  3155. (org-set-local 'org-table-may-need-update t)
  3156. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3157. 'local)
  3158. ;; Check for running clock before killing a buffer
  3159. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3160. ;; Paragraphs and auto-filling
  3161. (org-set-autofill-regexps)
  3162. (setq indent-line-function 'org-indent-line-function)
  3163. (org-update-radio-target-regexp)
  3164. ;; Comment characters
  3165. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3166. (org-set-local 'comment-padding " ")
  3167. ;; Align options lines
  3168. (org-set-local
  3169. 'align-mode-rules-list
  3170. '((org-in-buffer-settings
  3171. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3172. (modes . '(org-mode)))))
  3173. ;; Imenu
  3174. (org-set-local 'imenu-create-index-function
  3175. 'org-imenu-get-tree)
  3176. ;; Make isearch reveal context
  3177. (if (or (featurep 'xemacs)
  3178. (not (boundp 'outline-isearch-open-invisible-function)))
  3179. ;; Emacs 21 and XEmacs make use of the hook
  3180. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3181. ;; Emacs 22 deals with this through a special variable
  3182. (org-set-local 'outline-isearch-open-invisible-function
  3183. (lambda (&rest ignore) (org-show-context 'isearch))))
  3184. ;; If empty file that did not turn on org-mode automatically, make it to.
  3185. (if (and org-insert-mode-line-in-empty-file
  3186. (interactive-p)
  3187. (= (point-min) (point-max)))
  3188. (insert "# -*- mode: org -*-\n\n"))
  3189. (unless org-inhibit-startup
  3190. (when org-startup-align-all-tables
  3191. (let ((bmp (buffer-modified-p)))
  3192. (org-table-map-tables 'org-table-align)
  3193. (set-buffer-modified-p bmp)))
  3194. (org-set-startup-visibility)))
  3195. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3196. (defun org-current-time ()
  3197. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3198. (if (> (car org-time-stamp-rounding-minutes) 1)
  3199. (let ((r (car org-time-stamp-rounding-minutes))
  3200. (time (decode-time)))
  3201. (apply 'encode-time
  3202. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3203. (nthcdr 2 time))))
  3204. (current-time)))
  3205. ;;;; Font-Lock stuff, including the activators
  3206. (defvar org-mouse-map (make-sparse-keymap))
  3207. (org-defkey org-mouse-map
  3208. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3209. (org-defkey org-mouse-map
  3210. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3211. (when org-mouse-1-follows-link
  3212. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3213. (when org-tab-follows-link
  3214. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3215. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3216. (when org-return-follows-link
  3217. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3218. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3219. (require 'font-lock)
  3220. (defconst org-non-link-chars "]\t\n\r<>")
  3221. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3222. "shell" "elisp"))
  3223. (defvar org-link-types-re nil
  3224. "Matches a link that has a url-like prefix like \"http:\"")
  3225. (defvar org-link-re-with-space nil
  3226. "Matches a link with spaces, optional angular brackets around it.")
  3227. (defvar org-link-re-with-space2 nil
  3228. "Matches a link with spaces, optional angular brackets around it.")
  3229. (defvar org-link-re-with-space3 nil
  3230. "Matches a link with spaces, only for internal part in bracket links.")
  3231. (defvar org-angle-link-re nil
  3232. "Matches link with angular brackets, spaces are allowed.")
  3233. (defvar org-plain-link-re nil
  3234. "Matches plain link, without spaces.")
  3235. (defvar org-bracket-link-regexp nil
  3236. "Matches a link in double brackets.")
  3237. (defvar org-bracket-link-analytic-regexp nil
  3238. "Regular expression used to analyze links.
  3239. Here is what the match groups contain after a match:
  3240. 1: http:
  3241. 2: http
  3242. 3: path
  3243. 4: [desc]
  3244. 5: desc")
  3245. (defvar org-bracket-link-analytic-regexp++ nil
  3246. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3247. (defvar org-any-link-re nil
  3248. "Regular expression matching any link.")
  3249. (defun org-make-link-regexps ()
  3250. "Update the link regular expressions.
  3251. This should be called after the variable `org-link-types' has changed."
  3252. (setq org-link-types-re
  3253. (concat
  3254. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3255. org-link-re-with-space
  3256. (concat
  3257. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3258. "\\([^" org-non-link-chars " ]"
  3259. "[^" org-non-link-chars "]*"
  3260. "[^" org-non-link-chars " ]\\)>?")
  3261. org-link-re-with-space2
  3262. (concat
  3263. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3264. "\\([^" org-non-link-chars " ]"
  3265. "[^\t\n\r]*"
  3266. "[^" org-non-link-chars " ]\\)>?")
  3267. org-link-re-with-space3
  3268. (concat
  3269. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3270. "\\([^" org-non-link-chars " ]"
  3271. "[^\t\n\r]*\\)")
  3272. org-angle-link-re
  3273. (concat
  3274. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3275. "\\([^" org-non-link-chars " ]"
  3276. "[^" org-non-link-chars "]*"
  3277. "\\)>")
  3278. org-plain-link-re
  3279. (concat
  3280. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3281. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3282. org-bracket-link-regexp
  3283. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3284. org-bracket-link-analytic-regexp
  3285. (concat
  3286. "\\[\\["
  3287. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3288. "\\([^]]+\\)"
  3289. "\\]"
  3290. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3291. "\\]")
  3292. org-bracket-link-analytic-regexp++
  3293. (concat
  3294. "\\[\\["
  3295. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3296. "\\([^]]+\\)"
  3297. "\\]"
  3298. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3299. "\\]")
  3300. org-any-link-re
  3301. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3302. org-angle-link-re "\\)\\|\\("
  3303. org-plain-link-re "\\)")))
  3304. (org-make-link-regexps)
  3305. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3306. "Regular expression for fast time stamp matching.")
  3307. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3308. "Regular expression for fast time stamp matching.")
  3309. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3310. "Regular expression matching time strings for analysis.
  3311. This one does not require the space after the date, so it can be used
  3312. on a string that terminates immediately after the date.")
  3313. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3314. "Regular expression matching time strings for analysis.")
  3315. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3316. "Regular expression matching time stamps, with groups.")
  3317. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3318. "Regular expression matching time stamps (also [..]), with groups.")
  3319. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3320. "Regular expression matching a time stamp range.")
  3321. (defconst org-tr-regexp-both
  3322. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3323. "Regular expression matching a time stamp range.")
  3324. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3325. org-ts-regexp "\\)?")
  3326. "Regular expression matching a time stamp or time stamp range.")
  3327. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3328. org-ts-regexp-both "\\)?")
  3329. "Regular expression matching a time stamp or time stamp range.
  3330. The time stamps may be either active or inactive.")
  3331. (defvar org-emph-face nil)
  3332. (defun org-do-emphasis-faces (limit)
  3333. "Run through the buffer and add overlays to links."
  3334. (let (rtn)
  3335. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3336. (if (not (= (char-after (match-beginning 3))
  3337. (char-after (match-beginning 4))))
  3338. (progn
  3339. (setq rtn t)
  3340. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3341. 'face
  3342. (nth 1 (assoc (match-string 3)
  3343. org-emphasis-alist)))
  3344. (add-text-properties (match-beginning 2) (match-end 2)
  3345. '(font-lock-multiline t))
  3346. (when org-hide-emphasis-markers
  3347. (add-text-properties (match-end 4) (match-beginning 5)
  3348. '(invisible org-link))
  3349. (add-text-properties (match-beginning 3) (match-end 3)
  3350. '(invisible org-link)))))
  3351. (backward-char 1))
  3352. rtn))
  3353. (defun org-emphasize (&optional char)
  3354. "Insert or change an emphasis, i.e. a font like bold or italic.
  3355. If there is an active region, change that region to a new emphasis.
  3356. If there is no region, just insert the marker characters and position
  3357. the cursor between them.
  3358. CHAR should be either the marker character, or the first character of the
  3359. HTML tag associated with that emphasis. If CHAR is a space, the means
  3360. to remove the emphasis of the selected region.
  3361. If char is not given (for example in an interactive call) it
  3362. will be prompted for."
  3363. (interactive)
  3364. (let ((eal org-emphasis-alist) e det
  3365. (erc org-emphasis-regexp-components)
  3366. (prompt "")
  3367. (string "") beg end move tag c s)
  3368. (if (org-region-active-p)
  3369. (setq beg (region-beginning) end (region-end)
  3370. string (buffer-substring beg end))
  3371. (setq move t))
  3372. (while (setq e (pop eal))
  3373. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3374. c (aref tag 0))
  3375. (push (cons c (string-to-char (car e))) det)
  3376. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3377. (substring tag 1)))))
  3378. (setq det (nreverse det))
  3379. (unless char
  3380. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3381. (setq char (read-char-exclusive)))
  3382. (setq char (or (cdr (assoc char det)) char))
  3383. (if (equal char ?\ )
  3384. (setq s "" move nil)
  3385. (unless (assoc (char-to-string char) org-emphasis-alist)
  3386. (error "No such emphasis marker: \"%c\"" char))
  3387. (setq s (char-to-string char)))
  3388. (while (and (> (length string) 1)
  3389. (equal (substring string 0 1) (substring string -1))
  3390. (assoc (substring string 0 1) org-emphasis-alist))
  3391. (setq string (substring string 1 -1)))
  3392. (setq string (concat s string s))
  3393. (if beg (delete-region beg end))
  3394. (unless (or (bolp)
  3395. (string-match (concat "[" (nth 0 erc) "\n]")
  3396. (char-to-string (char-before (point)))))
  3397. (insert " "))
  3398. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3399. (char-to-string (char-after (point))))
  3400. (insert " ") (backward-char 1))
  3401. (insert string)
  3402. (and move (backward-char 1))))
  3403. (defconst org-nonsticky-props
  3404. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3405. (defun org-activate-plain-links (limit)
  3406. "Run through the buffer and add overlays to links."
  3407. (catch 'exit
  3408. (let (f)
  3409. (while (re-search-forward org-plain-link-re limit t)
  3410. (setq f (get-text-property (match-beginning 0) 'face))
  3411. (if (or (eq f 'org-tag)
  3412. (and (listp f) (memq 'org-tag f)))
  3413. nil
  3414. (add-text-properties (match-beginning 0) (match-end 0)
  3415. (list 'mouse-face 'highlight
  3416. 'rear-nonsticky org-nonsticky-props
  3417. 'keymap org-mouse-map
  3418. ))
  3419. (throw 'exit t))))))
  3420. (defun org-activate-code (limit)
  3421. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3422. (progn
  3423. (remove-text-properties (match-beginning 0) (match-end 0)
  3424. '(display t invisible t intangible t))
  3425. t)))
  3426. (defun org-activate-angle-links (limit)
  3427. "Run through the buffer and add overlays to links."
  3428. (if (re-search-forward org-angle-link-re limit t)
  3429. (progn
  3430. (add-text-properties (match-beginning 0) (match-end 0)
  3431. (list 'mouse-face 'highlight
  3432. 'rear-nonsticky org-nonsticky-props
  3433. 'keymap org-mouse-map
  3434. ))
  3435. t)))
  3436. (defun org-activate-footnote-links (limit)
  3437. "Run through the buffer and add overlays to links."
  3438. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3439. limit t)
  3440. (progn
  3441. (add-text-properties (match-beginning 2) (match-end 2)
  3442. (list 'mouse-face 'highlight
  3443. 'rear-nonsticky org-nonsticky-props
  3444. 'keymap org-mouse-map
  3445. 'help-echo
  3446. (if (= (point-at-bol) (match-beginning 2))
  3447. "Footnote definition"
  3448. "Footnote reference")
  3449. ))
  3450. t)))
  3451. (defun org-activate-bracket-links (limit)
  3452. "Run through the buffer and add overlays to bracketed links."
  3453. (if (re-search-forward org-bracket-link-regexp limit t)
  3454. (let* ((help (concat "LINK: "
  3455. (org-match-string-no-properties 1)))
  3456. ;; FIXME: above we should remove the escapes.
  3457. ;; but that requires another match, protecting match data,
  3458. ;; a lot of overhead for font-lock.
  3459. (ip (org-maybe-intangible
  3460. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3461. 'keymap org-mouse-map 'mouse-face 'highlight
  3462. 'font-lock-multiline t 'help-echo help)))
  3463. (vp (list 'rear-nonsticky org-nonsticky-props
  3464. 'keymap org-mouse-map 'mouse-face 'highlight
  3465. ' font-lock-multiline t 'help-echo help)))
  3466. ;; We need to remove the invisible property here. Table narrowing
  3467. ;; may have made some of this invisible.
  3468. (remove-text-properties (match-beginning 0) (match-end 0)
  3469. '(invisible nil))
  3470. (if (match-end 3)
  3471. (progn
  3472. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3473. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3474. (add-text-properties (match-end 3) (match-end 0) ip))
  3475. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3476. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3477. (add-text-properties (match-end 1) (match-end 0) ip))
  3478. t)))
  3479. (defun org-activate-dates (limit)
  3480. "Run through the buffer and add overlays to dates."
  3481. (if (re-search-forward org-tsr-regexp-both limit t)
  3482. (progn
  3483. (add-text-properties (match-beginning 0) (match-end 0)
  3484. (list 'mouse-face 'highlight
  3485. 'rear-nonsticky org-nonsticky-props
  3486. 'keymap org-mouse-map))
  3487. (when org-display-custom-times
  3488. (if (match-end 3)
  3489. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3490. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3491. t)))
  3492. (defvar org-target-link-regexp nil
  3493. "Regular expression matching radio targets in plain text.")
  3494. (make-variable-buffer-local 'org-target-link-regexp)
  3495. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3496. "Regular expression matching a link target.")
  3497. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3498. "Regular expression matching a radio target.")
  3499. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3500. "Regular expression matching any target.")
  3501. (defun org-activate-target-links (limit)
  3502. "Run through the buffer and add overlays to target matches."
  3503. (when org-target-link-regexp
  3504. (let ((case-fold-search t))
  3505. (if (re-search-forward org-target-link-regexp limit t)
  3506. (progn
  3507. (add-text-properties (match-beginning 0) (match-end 0)
  3508. (list 'mouse-face 'highlight
  3509. 'rear-nonsticky org-nonsticky-props
  3510. 'keymap org-mouse-map
  3511. 'help-echo "Radio target link"
  3512. 'org-linked-text t))
  3513. t)))))
  3514. (defun org-update-radio-target-regexp ()
  3515. "Find all radio targets in this file and update the regular expression."
  3516. (interactive)
  3517. (when (memq 'radio org-activate-links)
  3518. (setq org-target-link-regexp
  3519. (org-make-target-link-regexp (org-all-targets 'radio)))
  3520. (org-restart-font-lock)))
  3521. (defun org-hide-wide-columns (limit)
  3522. (let (s e)
  3523. (setq s (text-property-any (point) (or limit (point-max))
  3524. 'org-cwidth t))
  3525. (when s
  3526. (setq e (next-single-property-change s 'org-cwidth))
  3527. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3528. (goto-char e)
  3529. t)))
  3530. (defvar org-latex-and-specials-regexp nil
  3531. "Regular expression for highlighting export special stuff.")
  3532. (defvar org-match-substring-regexp)
  3533. (defvar org-match-substring-with-braces-regexp)
  3534. (defvar org-export-html-special-string-regexps)
  3535. (defun org-compute-latex-and-specials-regexp ()
  3536. "Compute regular expression for stuff treated specially by exporters."
  3537. (if (not org-highlight-latex-fragments-and-specials)
  3538. (org-set-local 'org-latex-and-specials-regexp nil)
  3539. (require 'org-exp)
  3540. (let*
  3541. ((matchers (plist-get org-format-latex-options :matchers))
  3542. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3543. org-latex-regexps)))
  3544. (options (org-combine-plists (org-default-export-plist)
  3545. (org-infile-export-plist)))
  3546. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3547. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3548. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3549. (org-export-html-expand (plist-get options :expand-quoted-html))
  3550. (org-export-with-special-strings (plist-get options :special-strings))
  3551. (re-sub
  3552. (cond
  3553. ((equal org-export-with-sub-superscripts '{})
  3554. (list org-match-substring-with-braces-regexp))
  3555. (org-export-with-sub-superscripts
  3556. (list org-match-substring-regexp))
  3557. (t nil)))
  3558. (re-latex
  3559. (if org-export-with-LaTeX-fragments
  3560. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3561. (re-macros
  3562. (if org-export-with-TeX-macros
  3563. (list (concat "\\\\"
  3564. (regexp-opt
  3565. (append (mapcar 'car org-html-entities)
  3566. (if (boundp 'org-latex-entities)
  3567. org-latex-entities nil))
  3568. 'words))) ; FIXME
  3569. ))
  3570. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3571. (re-special (if org-export-with-special-strings
  3572. (mapcar (lambda (x) (car x))
  3573. org-export-html-special-string-regexps)))
  3574. (re-rest
  3575. (delq nil
  3576. (list
  3577. (if org-export-html-expand "@<[^>\n]+>")
  3578. ))))
  3579. (org-set-local
  3580. 'org-latex-and-specials-regexp
  3581. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3582. re-rest) "\\|")))))
  3583. (defun org-do-latex-and-special-faces (limit)
  3584. "Run through the buffer and add overlays to links."
  3585. (when org-latex-and-specials-regexp
  3586. (let (rtn d)
  3587. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3588. limit t))
  3589. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3590. 'face))
  3591. '(org-code org-verbatim underline)))
  3592. (progn
  3593. (setq rtn t
  3594. d (cond ((member (char-after (1+ (match-beginning 0)))
  3595. '(?_ ?^)) 1)
  3596. (t 0)))
  3597. (font-lock-prepend-text-property
  3598. (+ d (match-beginning 0)) (match-end 0)
  3599. 'face 'org-latex-and-export-specials)
  3600. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3601. '(font-lock-multiline t)))))
  3602. rtn)))
  3603. (defun org-restart-font-lock ()
  3604. "Restart font-lock-mode, to force refontification."
  3605. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3606. (font-lock-mode -1)
  3607. (font-lock-mode 1)))
  3608. (defun org-all-targets (&optional radio)
  3609. "Return a list of all targets in this file.
  3610. With optional argument RADIO, only find radio targets."
  3611. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3612. rtn)
  3613. (save-excursion
  3614. (goto-char (point-min))
  3615. (while (re-search-forward re nil t)
  3616. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3617. rtn)))
  3618. (defun org-make-target-link-regexp (targets)
  3619. "Make regular expression matching all strings in TARGETS.
  3620. The regular expression finds the targets also if there is a line break
  3621. between words."
  3622. (and targets
  3623. (concat
  3624. "\\<\\("
  3625. (mapconcat
  3626. (lambda (x)
  3627. (while (string-match " +" x)
  3628. (setq x (replace-match "\\s-+" t t x)))
  3629. x)
  3630. targets
  3631. "\\|")
  3632. "\\)\\>")))
  3633. (defun org-activate-tags (limit)
  3634. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3635. (progn
  3636. (add-text-properties (match-beginning 1) (match-end 1)
  3637. (list 'mouse-face 'highlight
  3638. 'rear-nonsticky org-nonsticky-props
  3639. 'keymap org-mouse-map))
  3640. t)))
  3641. (defun org-outline-level ()
  3642. (save-excursion
  3643. (looking-at outline-regexp)
  3644. (if (match-beginning 1)
  3645. (+ (org-get-string-indentation (match-string 1)) 1000)
  3646. (1- (- (match-end 0) (match-beginning 0))))))
  3647. (defvar org-font-lock-keywords nil)
  3648. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3649. "Regular expression matching a property line.")
  3650. (defvar org-font-lock-hook nil
  3651. "Functions to be called for special font lock stuff.")
  3652. (defun org-font-lock-hook (limit)
  3653. (run-hook-with-args 'org-font-lock-hook limit))
  3654. (defun org-set-font-lock-defaults ()
  3655. (let* ((em org-fontify-emphasized-text)
  3656. (lk org-activate-links)
  3657. (org-font-lock-extra-keywords
  3658. (list
  3659. ;; Call the hook
  3660. '(org-font-lock-hook)
  3661. ;; Headlines
  3662. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3663. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3664. ;; Table lines
  3665. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3666. (1 'org-table t))
  3667. ;; Table internals
  3668. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3669. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3670. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3671. ;; Drawers
  3672. (list org-drawer-regexp '(0 'org-special-keyword t))
  3673. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3674. ;; Properties
  3675. (list org-property-re
  3676. '(1 'org-special-keyword t)
  3677. '(3 'org-property-value t))
  3678. (if org-format-transports-properties-p
  3679. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3680. ;; Links
  3681. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3682. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3683. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3684. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3685. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3686. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3687. (if (memq 'footnote lk) '(org-activate-footnote-links
  3688. (2 'org-footnote t)))
  3689. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3690. '(org-hide-wide-columns (0 nil append))
  3691. ;; TODO lines
  3692. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3693. '(1 (org-get-todo-face 1) t))
  3694. ;; DONE
  3695. (if org-fontify-done-headline
  3696. (list (concat "^[*]+ +\\<\\("
  3697. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3698. "\\)\\(.*\\)")
  3699. '(2 'org-headline-done t))
  3700. nil)
  3701. ;; Priorities
  3702. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3703. ;; Tags
  3704. '(org-font-lock-add-tag-faces)
  3705. ;; Special keywords
  3706. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3707. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3708. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3709. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3710. ;; Emphasis
  3711. (if em
  3712. (if (featurep 'xemacs)
  3713. '(org-do-emphasis-faces (0 nil append))
  3714. '(org-do-emphasis-faces)))
  3715. ;; Checkboxes
  3716. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3717. 2 'bold prepend)
  3718. (if org-provide-checkbox-statistics
  3719. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3720. (0 (org-get-checkbox-statistics-face) t)))
  3721. ;; Description list items
  3722. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3723. 2 'bold prepend)
  3724. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3725. '(1 'org-archived prepend))
  3726. ;; Specials
  3727. '(org-do-latex-and-special-faces)
  3728. ;; Code
  3729. '(org-activate-code (1 'org-code t))
  3730. ;; COMMENT
  3731. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3732. "\\|" org-quote-string "\\)\\>")
  3733. '(1 'org-special-keyword t))
  3734. '("^#.*" (0 'font-lock-comment-face t))
  3735. )))
  3736. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3737. ;; Now set the full font-lock-keywords
  3738. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3739. (org-set-local 'font-lock-defaults
  3740. '(org-font-lock-keywords t nil nil backward-paragraph))
  3741. (kill-local-variable 'font-lock-keywords) nil))
  3742. (defvar org-m nil)
  3743. (defvar org-l nil)
  3744. (defvar org-f nil)
  3745. (defun org-get-level-face (n)
  3746. "Get the right face for match N in font-lock matching of headlines."
  3747. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3748. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3749. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3750. (cond
  3751. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3752. ((eq n 2) org-f)
  3753. (t (if org-level-color-stars-only nil org-f))))
  3754. (defun org-get-todo-face (kwd)
  3755. "Get the right face for a TODO keyword KWD.
  3756. If KWD is a number, get the corresponding match group."
  3757. (if (numberp kwd) (setq kwd (match-string kwd)))
  3758. (or (cdr (assoc kwd org-todo-keyword-faces))
  3759. (and (member kwd org-done-keywords) 'org-done)
  3760. 'org-todo))
  3761. (defun org-font-lock-add-tag-faces (limit)
  3762. "Add the special tag faces."
  3763. (when (and org-tag-faces org-tags-special-faces-re)
  3764. (while (re-search-forward org-tags-special-faces-re limit t)
  3765. (add-text-properties (match-beginning 1) (match-end 1)
  3766. (list 'face (org-get-tag-face 1)
  3767. 'font-lock-fontified t))
  3768. (backward-char 1))))
  3769. (defun org-get-tag-face (kwd)
  3770. "Get the right face for a TODO keyword KWD.
  3771. If KWD is a number, get the corresponding match group."
  3772. (if (numberp kwd) (setq kwd (match-string kwd)))
  3773. (or (cdr (assoc kwd org-tag-faces))
  3774. 'org-tag))
  3775. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3776. "Remove fontification and activation overlays from links."
  3777. (font-lock-default-unfontify-region beg end)
  3778. (let* ((buffer-undo-list t)
  3779. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3780. (inhibit-modification-hooks t)
  3781. deactivate-mark buffer-file-name buffer-file-truename)
  3782. (remove-text-properties beg end
  3783. '(mouse-face t keymap t org-linked-text t
  3784. invisible t intangible t))))
  3785. ;;;; Visibility cycling, including org-goto and indirect buffer
  3786. ;;; Cycling
  3787. (defvar org-cycle-global-status nil)
  3788. (make-variable-buffer-local 'org-cycle-global-status)
  3789. (defvar org-cycle-subtree-status nil)
  3790. (make-variable-buffer-local 'org-cycle-subtree-status)
  3791. ;;;###autoload
  3792. (defun org-cycle (&optional arg)
  3793. "Visibility cycling for Org-mode.
  3794. - When this function is called with a prefix argument, rotate the entire
  3795. buffer through 3 states (global cycling)
  3796. 1. OVERVIEW: Show only top-level headlines.
  3797. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3798. 3. SHOW ALL: Show everything.
  3799. When called with two C-u C-u prefixes, switch to the startup visibility,
  3800. determined by the variable `org-startup-folded', and by any VISIBILITY
  3801. properties in the buffer.
  3802. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3803. including drawers.
  3804. - When point is at the beginning of a headline, rotate the subtree started
  3805. by this line through 3 different states (local cycling)
  3806. 1. FOLDED: Only the main headline is shown.
  3807. 2. CHILDREN: The main headline and the direct children are shown.
  3808. From this state, you can move to one of the children
  3809. and zoom in further.
  3810. 3. SUBTREE: Show the entire subtree, including body text.
  3811. - When there is a numeric prefix, go up to a heading with level ARG, do
  3812. a `show-subtree' and return to the previous cursor position. If ARG
  3813. is negative, go up that many levels.
  3814. - When point is not at the beginning of a headline, execute the global
  3815. binding for TAB, which is re-indenting the line. See the option
  3816. `org-cycle-emulate-tab' for details.
  3817. - Special case: if point is at the beginning of the buffer and there is
  3818. no headline in line 1, this function will act as if called with prefix arg.
  3819. But only if also the variable `org-cycle-global-at-bob' is t."
  3820. (interactive "P")
  3821. (org-load-modules-maybe)
  3822. (let* ((outline-regexp
  3823. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3824. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3825. outline-regexp))
  3826. (bob-special (and org-cycle-global-at-bob (bobp)
  3827. (not (looking-at outline-regexp))))
  3828. (org-cycle-hook
  3829. (if bob-special
  3830. (delq 'org-optimize-window-after-visibility-change
  3831. (copy-sequence org-cycle-hook))
  3832. org-cycle-hook))
  3833. (pos (point)))
  3834. (if (or bob-special (equal arg '(4)))
  3835. ;; special case: use global cycling
  3836. (setq arg t))
  3837. (cond
  3838. ((equal arg '(16))
  3839. (org-set-startup-visibility)
  3840. (message "Startup visibility, plus VISIBILITY properties"))
  3841. ((equal arg '(64))
  3842. (show-all)
  3843. (message "Entire buffer visible, including drawers"))
  3844. ((org-at-table-p 'any)
  3845. ;; Enter the table or move to the next field in the table
  3846. (or (org-table-recognize-table.el)
  3847. (progn
  3848. (if arg (org-table-edit-field t)
  3849. (org-table-justify-field-maybe)
  3850. (call-interactively 'org-table-next-field)))))
  3851. ((eq arg t) ;; Global cycling
  3852. (cond
  3853. ((and (eq last-command this-command)
  3854. (eq org-cycle-global-status 'overview))
  3855. ;; We just created the overview - now do table of contents
  3856. ;; This can be slow in very large buffers, so indicate action
  3857. (message "CONTENTS...")
  3858. (org-content)
  3859. (message "CONTENTS...done")
  3860. (setq org-cycle-global-status 'contents)
  3861. (run-hook-with-args 'org-cycle-hook 'contents))
  3862. ((and (eq last-command this-command)
  3863. (eq org-cycle-global-status 'contents))
  3864. ;; We just showed the table of contents - now show everything
  3865. (show-all)
  3866. (message "SHOW ALL")
  3867. (setq org-cycle-global-status 'all)
  3868. (run-hook-with-args 'org-cycle-hook 'all))
  3869. (t
  3870. ;; Default action: go to overview
  3871. (org-overview)
  3872. (message "OVERVIEW")
  3873. (setq org-cycle-global-status 'overview)
  3874. (run-hook-with-args 'org-cycle-hook 'overview))))
  3875. ((and org-drawers org-drawer-regexp
  3876. (save-excursion
  3877. (beginning-of-line 1)
  3878. (looking-at org-drawer-regexp)))
  3879. ;; Toggle block visibility
  3880. (org-flag-drawer
  3881. (not (get-char-property (match-end 0) 'invisible))))
  3882. ((integerp arg)
  3883. ;; Show-subtree, ARG levels up from here.
  3884. (save-excursion
  3885. (org-back-to-heading)
  3886. (outline-up-heading (if (< arg 0) (- arg)
  3887. (- (funcall outline-level) arg)))
  3888. (org-show-subtree)))
  3889. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3890. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3891. ;; At a heading: rotate between three different views
  3892. (org-back-to-heading)
  3893. (let ((goal-column 0) eoh eol eos)
  3894. ;; First, some boundaries
  3895. (save-excursion
  3896. (org-back-to-heading)
  3897. (save-excursion
  3898. (beginning-of-line 2)
  3899. (while (and (not (eobp)) ;; this is like `next-line'
  3900. (get-char-property (1- (point)) 'invisible))
  3901. (beginning-of-line 2)) (setq eol (point)))
  3902. (outline-end-of-heading) (setq eoh (point))
  3903. (org-end-of-subtree t)
  3904. (unless (eobp)
  3905. (skip-chars-forward " \t\n")
  3906. (beginning-of-line 1) ; in case this is an item
  3907. )
  3908. (setq eos (1- (point))))
  3909. ;; Find out what to do next and set `this-command'
  3910. (cond
  3911. ((= eos eoh)
  3912. ;; Nothing is hidden behind this heading
  3913. (message "EMPTY ENTRY")
  3914. (setq org-cycle-subtree-status nil)
  3915. (save-excursion
  3916. (goto-char eos)
  3917. (outline-next-heading)
  3918. (if (org-invisible-p) (org-flag-heading nil))))
  3919. ((or (>= eol eos)
  3920. (not (string-match "\\S-" (buffer-substring eol eos))))
  3921. ;; Entire subtree is hidden in one line: open it
  3922. (org-show-entry)
  3923. (show-children)
  3924. (message "CHILDREN")
  3925. (save-excursion
  3926. (goto-char eos)
  3927. (outline-next-heading)
  3928. (if (org-invisible-p) (org-flag-heading nil)))
  3929. (setq org-cycle-subtree-status 'children)
  3930. (run-hook-with-args 'org-cycle-hook 'children))
  3931. ((and (eq last-command this-command)
  3932. (eq org-cycle-subtree-status 'children))
  3933. ;; We just showed the children, now show everything.
  3934. (org-show-subtree)
  3935. (message "SUBTREE")
  3936. (setq org-cycle-subtree-status 'subtree)
  3937. (run-hook-with-args 'org-cycle-hook 'subtree))
  3938. (t
  3939. ;; Default action: hide the subtree.
  3940. (hide-subtree)
  3941. (message "FOLDED")
  3942. (setq org-cycle-subtree-status 'folded)
  3943. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3944. ;; TAB emulation and template completion
  3945. (buffer-read-only (org-back-to-heading))
  3946. ((org-try-structure-completion))
  3947. ((org-try-cdlatex-tab))
  3948. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3949. (or (not (bolp))
  3950. (not (looking-at outline-regexp))))
  3951. (call-interactively (global-key-binding "\t")))
  3952. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3953. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3954. (or (and (eq org-cycle-emulate-tab 'white)
  3955. (= (match-end 0) (point-at-eol)))
  3956. (and (eq org-cycle-emulate-tab 'whitestart)
  3957. (>= (match-end 0) pos))))
  3958. t
  3959. (eq org-cycle-emulate-tab t))
  3960. (call-interactively (global-key-binding "\t")))
  3961. (t (save-excursion
  3962. (org-back-to-heading)
  3963. (org-cycle))))))
  3964. ;;;###autoload
  3965. (defun org-global-cycle (&optional arg)
  3966. "Cycle the global visibility. For details see `org-cycle'.
  3967. With C-u prefix arg, switch to startup visibility.
  3968. With a numeric prefix, show all headlines up to that level."
  3969. (interactive "P")
  3970. (let ((org-cycle-include-plain-lists
  3971. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3972. (cond
  3973. ((integerp arg)
  3974. (show-all)
  3975. (hide-sublevels arg)
  3976. (setq org-cycle-global-status 'contents))
  3977. ((equal arg '(4))
  3978. (org-set-startup-visibility)
  3979. (message "Startup visibility, plus VISIBILITY properties."))
  3980. (t
  3981. (org-cycle '(4))))))
  3982. (defun org-set-startup-visibility ()
  3983. "Set the visibility required by startup options and properties."
  3984. (cond
  3985. ((eq org-startup-folded t)
  3986. (org-cycle '(4)))
  3987. ((eq org-startup-folded 'content)
  3988. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3989. (org-cycle '(4)) (org-cycle '(4)))))
  3990. (org-set-visibility-according-to-property 'no-cleanup)
  3991. (org-cycle-hide-archived-subtrees 'all)
  3992. (org-cycle-hide-drawers 'all)
  3993. (org-cycle-show-empty-lines 'all))
  3994. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3995. "Switch subtree visibilities according to :VISIBILITY: property."
  3996. (interactive)
  3997. (let (org-show-entry-below state)
  3998. (save-excursion
  3999. (goto-char (point-min))
  4000. (while (re-search-forward
  4001. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4002. nil t)
  4003. (setq state (match-string 1))
  4004. (save-excursion
  4005. (org-back-to-heading t)
  4006. (hide-subtree)
  4007. (org-reveal)
  4008. (cond
  4009. ((equal state '("fold" "folded"))
  4010. (hide-subtree))
  4011. ((equal state "children")
  4012. (org-show-hidden-entry)
  4013. (show-children))
  4014. ((equal state "content")
  4015. (save-excursion
  4016. (save-restriction
  4017. (org-narrow-to-subtree)
  4018. (org-content))))
  4019. ((member state '("all" "showall"))
  4020. (show-subtree)))))
  4021. (unless no-cleanup
  4022. (org-cycle-hide-archived-subtrees 'all)
  4023. (org-cycle-hide-drawers 'all)
  4024. (org-cycle-show-empty-lines 'all)))))
  4025. (defun org-overview ()
  4026. "Switch to overview mode, showing only top-level headlines.
  4027. Really, this shows all headlines with level equal or greater than the level
  4028. of the first headline in the buffer. This is important, because if the
  4029. first headline is not level one, then (hide-sublevels 1) gives confusing
  4030. results."
  4031. (interactive)
  4032. (let ((level (save-excursion
  4033. (goto-char (point-min))
  4034. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4035. (progn
  4036. (goto-char (match-beginning 0))
  4037. (funcall outline-level))))))
  4038. (and level (hide-sublevels level))))
  4039. (defun org-content (&optional arg)
  4040. "Show all headlines in the buffer, like a table of contents.
  4041. With numerical argument N, show content up to level N."
  4042. (interactive "P")
  4043. (save-excursion
  4044. ;; Visit all headings and show their offspring
  4045. (and (integerp arg) (org-overview))
  4046. (goto-char (point-max))
  4047. (catch 'exit
  4048. (while (and (progn (condition-case nil
  4049. (outline-previous-visible-heading 1)
  4050. (error (goto-char (point-min))))
  4051. t)
  4052. (looking-at outline-regexp))
  4053. (if (integerp arg)
  4054. (show-children (1- arg))
  4055. (show-branches))
  4056. (if (bobp) (throw 'exit nil))))))
  4057. (defun org-optimize-window-after-visibility-change (state)
  4058. "Adjust the window after a change in outline visibility.
  4059. This function is the default value of the hook `org-cycle-hook'."
  4060. (when (get-buffer-window (current-buffer))
  4061. (cond
  4062. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4063. ; ((eq state 'overview) (org-beginning-of-line))
  4064. ((eq state 'content) nil)
  4065. ((eq state 'all) nil)
  4066. ((eq state 'folded) nil)
  4067. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4068. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4069. (defun org-compact-display-after-subtree-move ()
  4070. "Show a compacter version of the tree of the entry's parent."
  4071. (save-excursion
  4072. (if (org-up-heading-safe)
  4073. (progn
  4074. (hide-subtree)
  4075. (show-entry)
  4076. (show-children)
  4077. (org-cycle-show-empty-lines 'children)
  4078. (org-cycle-hide-drawers 'children))
  4079. (org-overview))))
  4080. (defun org-cycle-show-empty-lines (state)
  4081. "Show empty lines above all visible headlines.
  4082. The region to be covered depends on STATE when called through
  4083. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4084. entire buffer covered. Note that an empty line is only shown if there
  4085. are at least `org-cycle-separator-lines' empty lines before the headline."
  4086. (when (> org-cycle-separator-lines 0)
  4087. (save-excursion
  4088. (let* ((n org-cycle-separator-lines)
  4089. (re (cond
  4090. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4091. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4092. (t (let ((ns (number-to-string (- n 2))))
  4093. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4094. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4095. beg end)
  4096. (cond
  4097. ((memq state '(overview contents t))
  4098. (setq beg (point-min) end (point-max)))
  4099. ((memq state '(children folded))
  4100. (setq beg (point) end (progn (org-end-of-subtree t t)
  4101. (beginning-of-line 2)
  4102. (point)))))
  4103. (when beg
  4104. (goto-char beg)
  4105. (while (re-search-forward re end t)
  4106. (if (not (get-char-property (match-end 1) 'invisible))
  4107. (outline-flag-region
  4108. (match-beginning 1) (match-end 1) nil)))))))
  4109. ;; Never hide empty lines at the end of the file.
  4110. (save-excursion
  4111. (goto-char (point-max))
  4112. (outline-previous-heading)
  4113. (outline-end-of-heading)
  4114. (if (and (looking-at "[ \t\n]+")
  4115. (= (match-end 0) (point-max)))
  4116. (outline-flag-region (point) (match-end 0) nil))))
  4117. (defun org-show-empty-lines-in-parent ()
  4118. "Move to the parent and re-show empty lines before visible headlines."
  4119. (save-excursion
  4120. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4121. (org-cycle-show-empty-lines context))))
  4122. (defun org-cycle-hide-drawers (state)
  4123. "Re-hide all drawers after a visibility state change."
  4124. (when (and (org-mode-p)
  4125. (not (memq state '(overview folded))))
  4126. (save-excursion
  4127. (let* ((globalp (memq state '(contents all)))
  4128. (beg (if globalp (point-min) (point)))
  4129. (end (if globalp (point-max) (org-end-of-subtree t))))
  4130. (goto-char beg)
  4131. (while (re-search-forward org-drawer-regexp end t)
  4132. (org-flag-drawer t))))))
  4133. (defun org-flag-drawer (flag)
  4134. (save-excursion
  4135. (beginning-of-line 1)
  4136. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4137. (let ((b (match-end 0))
  4138. (outline-regexp org-outline-regexp))
  4139. (if (re-search-forward
  4140. "^[ \t]*:END:"
  4141. (save-excursion (outline-next-heading) (point)) t)
  4142. (outline-flag-region b (point-at-eol) flag)
  4143. (error ":END: line missing"))))))
  4144. (defun org-subtree-end-visible-p ()
  4145. "Is the end of the current subtree visible?"
  4146. (pos-visible-in-window-p
  4147. (save-excursion (org-end-of-subtree t) (point))))
  4148. (defun org-first-headline-recenter (&optional N)
  4149. "Move cursor to the first headline and recenter the headline.
  4150. Optional argument N means, put the headline into the Nth line of the window."
  4151. (goto-char (point-min))
  4152. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4153. (beginning-of-line)
  4154. (recenter (prefix-numeric-value N))))
  4155. ;;; Org-goto
  4156. (defvar org-goto-window-configuration nil)
  4157. (defvar org-goto-marker nil)
  4158. (defvar org-goto-map
  4159. (let ((map (make-sparse-keymap)))
  4160. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4161. (while (setq cmd (pop cmds))
  4162. (substitute-key-definition cmd cmd map global-map)))
  4163. (suppress-keymap map)
  4164. (org-defkey map "\C-m" 'org-goto-ret)
  4165. (org-defkey map [(return)] 'org-goto-ret)
  4166. (org-defkey map [(left)] 'org-goto-left)
  4167. (org-defkey map [(right)] 'org-goto-right)
  4168. (org-defkey map [(control ?g)] 'org-goto-quit)
  4169. (org-defkey map "\C-i" 'org-cycle)
  4170. (org-defkey map [(tab)] 'org-cycle)
  4171. (org-defkey map [(down)] 'outline-next-visible-heading)
  4172. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4173. (if org-goto-auto-isearch
  4174. (if (fboundp 'define-key-after)
  4175. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4176. nil)
  4177. (org-defkey map "q" 'org-goto-quit)
  4178. (org-defkey map "n" 'outline-next-visible-heading)
  4179. (org-defkey map "p" 'outline-previous-visible-heading)
  4180. (org-defkey map "f" 'outline-forward-same-level)
  4181. (org-defkey map "b" 'outline-backward-same-level)
  4182. (org-defkey map "u" 'outline-up-heading))
  4183. (org-defkey map "/" 'org-occur)
  4184. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4185. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4186. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4187. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4188. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4189. map))
  4190. (defconst org-goto-help
  4191. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4192. RET=jump to location [Q]uit and return to previous location
  4193. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4194. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4195. ;; FIXME: Docstring doe not mention both interfaces
  4196. (defun org-goto (&optional alternative-interface)
  4197. "Look up a different location in the current file, keeping current visibility.
  4198. When you want look-up or go to a different location in a document, the
  4199. fastest way is often to fold the entire buffer and then dive into the tree.
  4200. This method has the disadvantage, that the previous location will be folded,
  4201. which may not be what you want.
  4202. This command works around this by showing a copy of the current buffer
  4203. in an indirect buffer, in overview mode. You can dive into the tree in
  4204. that copy, use org-occur and incremental search to find a location.
  4205. When pressing RET or `Q', the command returns to the original buffer in
  4206. which the visibility is still unchanged. After RET is will also jump to
  4207. the location selected in the indirect buffer and expose the
  4208. the headline hierarchy above."
  4209. (interactive "P")
  4210. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4211. (org-refile-use-outline-path t)
  4212. (interface
  4213. (if (not alternative-interface)
  4214. org-goto-interface
  4215. (if (eq org-goto-interface 'outline)
  4216. 'outline-path-completion
  4217. 'outline)))
  4218. (org-goto-start-pos (point))
  4219. (selected-point
  4220. (if (eq interface 'outline)
  4221. (car (org-get-location (current-buffer) org-goto-help))
  4222. (nth 3 (org-refile-get-location "Goto: ")))))
  4223. (if selected-point
  4224. (progn
  4225. (org-mark-ring-push org-goto-start-pos)
  4226. (goto-char selected-point)
  4227. (if (or (org-invisible-p) (org-invisible-p2))
  4228. (org-show-context 'org-goto)))
  4229. (message "Quit"))))
  4230. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4231. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4232. (defvar org-goto-local-auto-isearch-map) ; defined below
  4233. (defun org-get-location (buf help)
  4234. "Let the user select a location in the Org-mode buffer BUF.
  4235. This function uses a recursive edit. It returns the selected position
  4236. or nil."
  4237. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4238. (isearch-hide-immediately nil)
  4239. (isearch-search-fun-function
  4240. (lambda () 'org-goto-local-search-headings))
  4241. (org-goto-selected-point org-goto-exit-command))
  4242. (save-excursion
  4243. (save-window-excursion
  4244. (delete-other-windows)
  4245. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4246. (switch-to-buffer
  4247. (condition-case nil
  4248. (make-indirect-buffer (current-buffer) "*org-goto*")
  4249. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4250. (with-output-to-temp-buffer "*Help*"
  4251. (princ help))
  4252. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4253. (setq buffer-read-only nil)
  4254. (let ((org-startup-truncated t)
  4255. (org-startup-folded nil)
  4256. (org-startup-align-all-tables nil))
  4257. (org-mode)
  4258. (org-overview))
  4259. (setq buffer-read-only t)
  4260. (if (and (boundp 'org-goto-start-pos)
  4261. (integer-or-marker-p org-goto-start-pos))
  4262. (let ((org-show-hierarchy-above t)
  4263. (org-show-siblings t)
  4264. (org-show-following-heading t))
  4265. (goto-char org-goto-start-pos)
  4266. (and (org-invisible-p) (org-show-context)))
  4267. (goto-char (point-min)))
  4268. (org-beginning-of-line)
  4269. (message "Select location and press RET")
  4270. (use-local-map org-goto-map)
  4271. (recursive-edit)
  4272. ))
  4273. (kill-buffer "*org-goto*")
  4274. (cons org-goto-selected-point org-goto-exit-command)))
  4275. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4276. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4277. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4278. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4279. (defun org-goto-local-search-headings (string bound noerror)
  4280. "Search and make sure that any matches are in headlines."
  4281. (catch 'return
  4282. (while (if isearch-forward
  4283. (search-forward string bound noerror)
  4284. (search-backward string bound noerror))
  4285. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4286. (and (member :headline context)
  4287. (not (member :tags context))))
  4288. (throw 'return (point))))))
  4289. (defun org-goto-local-auto-isearch ()
  4290. "Start isearch."
  4291. (interactive)
  4292. (goto-char (point-min))
  4293. (let ((keys (this-command-keys)))
  4294. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4295. (isearch-mode t)
  4296. (isearch-process-search-char (string-to-char keys)))))
  4297. (defun org-goto-ret (&optional arg)
  4298. "Finish `org-goto' by going to the new location."
  4299. (interactive "P")
  4300. (setq org-goto-selected-point (point)
  4301. org-goto-exit-command 'return)
  4302. (throw 'exit nil))
  4303. (defun org-goto-left ()
  4304. "Finish `org-goto' by going to the new location."
  4305. (interactive)
  4306. (if (org-on-heading-p)
  4307. (progn
  4308. (beginning-of-line 1)
  4309. (setq org-goto-selected-point (point)
  4310. org-goto-exit-command 'left)
  4311. (throw 'exit nil))
  4312. (error "Not on a heading")))
  4313. (defun org-goto-right ()
  4314. "Finish `org-goto' by going to the new location."
  4315. (interactive)
  4316. (if (org-on-heading-p)
  4317. (progn
  4318. (setq org-goto-selected-point (point)
  4319. org-goto-exit-command 'right)
  4320. (throw 'exit nil))
  4321. (error "Not on a heading")))
  4322. (defun org-goto-quit ()
  4323. "Finish `org-goto' without cursor motion."
  4324. (interactive)
  4325. (setq org-goto-selected-point nil)
  4326. (setq org-goto-exit-command 'quit)
  4327. (throw 'exit nil))
  4328. ;;; Indirect buffer display of subtrees
  4329. (defvar org-indirect-dedicated-frame nil
  4330. "This is the frame being used for indirect tree display.")
  4331. (defvar org-last-indirect-buffer nil)
  4332. (defun org-tree-to-indirect-buffer (&optional arg)
  4333. "Create indirect buffer and narrow it to current subtree.
  4334. With numerical prefix ARG, go up to this level and then take that tree.
  4335. If ARG is negative, go up that many levels.
  4336. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4337. indirect buffer previously made with this command, to avoid proliferation of
  4338. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4339. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4340. is kept so that you can work with several indirect buffers at the same time.
  4341. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4342. requests that a new frame be made for the new buffer, so that the dedicated
  4343. frame is not changed."
  4344. (interactive "P")
  4345. (let ((cbuf (current-buffer))
  4346. (cwin (selected-window))
  4347. (pos (point))
  4348. beg end level heading ibuf)
  4349. (save-excursion
  4350. (org-back-to-heading t)
  4351. (when (numberp arg)
  4352. (setq level (org-outline-level))
  4353. (if (< arg 0) (setq arg (+ level arg)))
  4354. (while (> (setq level (org-outline-level)) arg)
  4355. (outline-up-heading 1 t)))
  4356. (setq beg (point)
  4357. heading (org-get-heading))
  4358. (org-end-of-subtree t) (setq end (point)))
  4359. (if (and (buffer-live-p org-last-indirect-buffer)
  4360. (not (eq org-indirect-buffer-display 'new-frame))
  4361. (not arg))
  4362. (kill-buffer org-last-indirect-buffer))
  4363. (setq ibuf (org-get-indirect-buffer cbuf)
  4364. org-last-indirect-buffer ibuf)
  4365. (cond
  4366. ((or (eq org-indirect-buffer-display 'new-frame)
  4367. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4368. (select-frame (make-frame))
  4369. (delete-other-windows)
  4370. (switch-to-buffer ibuf)
  4371. (org-set-frame-title heading))
  4372. ((eq org-indirect-buffer-display 'dedicated-frame)
  4373. (raise-frame
  4374. (select-frame (or (and org-indirect-dedicated-frame
  4375. (frame-live-p org-indirect-dedicated-frame)
  4376. org-indirect-dedicated-frame)
  4377. (setq org-indirect-dedicated-frame (make-frame)))))
  4378. (delete-other-windows)
  4379. (switch-to-buffer ibuf)
  4380. (org-set-frame-title (concat "Indirect: " heading)))
  4381. ((eq org-indirect-buffer-display 'current-window)
  4382. (switch-to-buffer ibuf))
  4383. ((eq org-indirect-buffer-display 'other-window)
  4384. (pop-to-buffer ibuf))
  4385. (t (error "Invalid value.")))
  4386. (if (featurep 'xemacs)
  4387. (save-excursion (org-mode) (turn-on-font-lock)))
  4388. (narrow-to-region beg end)
  4389. (show-all)
  4390. (goto-char pos)
  4391. (and (window-live-p cwin) (select-window cwin))))
  4392. (defun org-get-indirect-buffer (&optional buffer)
  4393. (setq buffer (or buffer (current-buffer)))
  4394. (let ((n 1) (base (buffer-name buffer)) bname)
  4395. (while (buffer-live-p
  4396. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4397. (setq n (1+ n)))
  4398. (condition-case nil
  4399. (make-indirect-buffer buffer bname 'clone)
  4400. (error (make-indirect-buffer buffer bname)))))
  4401. (defun org-set-frame-title (title)
  4402. "Set the title of the current frame to the string TITLE."
  4403. ;; FIXME: how to name a single frame in XEmacs???
  4404. (unless (featurep 'xemacs)
  4405. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4406. ;;;; Structure editing
  4407. ;;; Inserting headlines
  4408. (defun org-previous-line-empty-p ()
  4409. (save-excursion
  4410. (and (not (bobp))
  4411. (or (beginning-of-line 0) t)
  4412. (save-match-data
  4413. (looking-at "[ \t]*$")))))
  4414. (defun org-insert-heading (&optional force-heading)
  4415. "Insert a new heading or item with same depth at point.
  4416. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4417. If point is at the beginning of a headline, insert a sibling before the
  4418. current headline. If point is not at the beginning, do not split the line,
  4419. but create the new headline after the current line."
  4420. (interactive "P")
  4421. (if (= (buffer-size) 0)
  4422. (insert "\n* ")
  4423. (when (or force-heading (not (org-insert-item)))
  4424. (let* ((empty-line-p nil)
  4425. (head (save-excursion
  4426. (condition-case nil
  4427. (progn
  4428. (org-back-to-heading)
  4429. (setq empty-line-p (org-previous-line-empty-p))
  4430. (match-string 0))
  4431. (error "*"))))
  4432. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4433. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4434. pos hide-previous previous-pos)
  4435. (cond
  4436. ((and (org-on-heading-p) (bolp)
  4437. (or (bobp)
  4438. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4439. ;; insert before the current line
  4440. (open-line (if blank 2 1)))
  4441. ((and (bolp)
  4442. (or (bobp)
  4443. (save-excursion
  4444. (backward-char 1) (not (org-invisible-p)))))
  4445. ;; insert right here
  4446. nil)
  4447. (t
  4448. ;; somewhere in the line
  4449. (save-excursion
  4450. (setq previous-pos (point-at-bol))
  4451. (end-of-line)
  4452. (setq hide-previous (org-invisible-p)))
  4453. (and org-insert-heading-respect-content (org-show-subtree))
  4454. (let ((split
  4455. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4456. (save-excursion
  4457. (let ((p (point)))
  4458. (goto-char (point-at-bol))
  4459. (and (looking-at org-complex-heading-regexp)
  4460. (> p (match-beginning 4)))))))
  4461. tags pos)
  4462. (cond
  4463. (org-insert-heading-respect-content
  4464. (org-end-of-subtree nil t)
  4465. (or (bolp) (newline))
  4466. (or (org-previous-line-empty-p)
  4467. (and blank (newline)))
  4468. (open-line 1))
  4469. ((org-on-heading-p)
  4470. (when hide-previous
  4471. (show-children)
  4472. (org-show-entry))
  4473. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4474. (setq tags (and (match-end 2) (match-string 2)))
  4475. (and (match-end 1)
  4476. (delete-region (match-beginning 1) (match-end 1)))
  4477. (setq pos (point-at-bol))
  4478. (or split (end-of-line 1))
  4479. (delete-horizontal-space)
  4480. (newline (if blank 2 1))
  4481. (when tags
  4482. (save-excursion
  4483. (goto-char pos)
  4484. (end-of-line 1)
  4485. (insert " " tags)
  4486. (org-set-tags nil 'align))))
  4487. (t
  4488. (or split (end-of-line 1))
  4489. (newline (if blank 2 1)))))))
  4490. (insert head) (just-one-space)
  4491. (setq pos (point))
  4492. (end-of-line 1)
  4493. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4494. (when (and org-insert-heading-respect-content hide-previous)
  4495. (save-excursion
  4496. (goto-char previous-pos)
  4497. (hide-subtree)))
  4498. (run-hooks 'org-insert-heading-hook)))))
  4499. (defun org-get-heading (&optional no-tags)
  4500. "Return the heading of the current entry, without the stars."
  4501. (save-excursion
  4502. (org-back-to-heading t)
  4503. (if (looking-at
  4504. (if no-tags
  4505. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4506. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4507. (match-string 1) "")))
  4508. (defun org-heading-components ()
  4509. "Return the components of the current heading.
  4510. This is a list with the following elements:
  4511. - the level as an integer
  4512. - the reduced level, different if `org-odd-levels-only' is set.
  4513. - the TODO keyword, or nil
  4514. - the priority character, like ?A, or nil if no priority is given
  4515. - the headline text itself, or the tags string if no headline text
  4516. - the tags string, or nil."
  4517. (save-excursion
  4518. (org-back-to-heading t)
  4519. (if (looking-at org-complex-heading-regexp)
  4520. (list (length (match-string 1))
  4521. (org-reduced-level (length (match-string 1)))
  4522. (org-match-string-no-properties 2)
  4523. (and (match-end 3) (aref (match-string 3) 2))
  4524. (org-match-string-no-properties 4)
  4525. (org-match-string-no-properties 5)))))
  4526. (defun org-insert-heading-after-current ()
  4527. "Insert a new heading with same level as current, after current subtree."
  4528. (interactive)
  4529. (org-back-to-heading)
  4530. (org-insert-heading)
  4531. (org-move-subtree-down)
  4532. (end-of-line 1))
  4533. (defun org-insert-heading-respect-content ()
  4534. (interactive)
  4535. (let ((org-insert-heading-respect-content t))
  4536. (org-insert-heading t)))
  4537. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4538. (interactive "P")
  4539. (let ((org-insert-heading-respect-content t))
  4540. (org-insert-todo-heading force-state t)))
  4541. (defun org-insert-todo-heading (arg &optional force-heading)
  4542. "Insert a new heading with the same level and TODO state as current heading.
  4543. If the heading has no TODO state, or if the state is DONE, use the first
  4544. state (TODO by default). Also with prefix arg, force first state."
  4545. (interactive "P")
  4546. (when (or force-heading (not (org-insert-item 'checkbox)))
  4547. (org-insert-heading force-heading)
  4548. (save-excursion
  4549. (org-back-to-heading)
  4550. (outline-previous-heading)
  4551. (looking-at org-todo-line-regexp))
  4552. (if (or arg
  4553. (not (match-beginning 2))
  4554. (member (match-string 2) org-done-keywords))
  4555. (insert (car org-todo-keywords-1) " ")
  4556. (insert (match-string 2) " "))
  4557. (when org-provide-todo-statistics
  4558. (org-update-parent-todo-statistics))))
  4559. (defun org-insert-subheading (arg)
  4560. "Insert a new subheading and demote it.
  4561. Works for outline headings and for plain lists alike."
  4562. (interactive "P")
  4563. (org-insert-heading arg)
  4564. (cond
  4565. ((org-on-heading-p) (org-do-demote))
  4566. ((org-at-item-p) (org-indent-item 1))))
  4567. (defun org-insert-todo-subheading (arg)
  4568. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4569. Works for outline headings and for plain lists alike."
  4570. (interactive "P")
  4571. (org-insert-todo-heading arg)
  4572. (cond
  4573. ((org-on-heading-p) (org-do-demote))
  4574. ((org-at-item-p) (org-indent-item 1))))
  4575. ;;; Promotion and Demotion
  4576. (defun org-promote-subtree ()
  4577. "Promote the entire subtree.
  4578. See also `org-promote'."
  4579. (interactive)
  4580. (save-excursion
  4581. (org-map-tree 'org-promote))
  4582. (org-fix-position-after-promote))
  4583. (defun org-demote-subtree ()
  4584. "Demote the entire subtree. See `org-demote'.
  4585. See also `org-promote'."
  4586. (interactive)
  4587. (save-excursion
  4588. (org-map-tree 'org-demote))
  4589. (org-fix-position-after-promote))
  4590. (defun org-do-promote ()
  4591. "Promote the current heading higher up the tree.
  4592. If the region is active in `transient-mark-mode', promote all headings
  4593. in the region."
  4594. (interactive)
  4595. (save-excursion
  4596. (if (org-region-active-p)
  4597. (org-map-region 'org-promote (region-beginning) (region-end))
  4598. (org-promote)))
  4599. (org-fix-position-after-promote))
  4600. (defun org-do-demote ()
  4601. "Demote the current heading lower down the tree.
  4602. If the region is active in `transient-mark-mode', demote all headings
  4603. in the region."
  4604. (interactive)
  4605. (save-excursion
  4606. (if (org-region-active-p)
  4607. (org-map-region 'org-demote (region-beginning) (region-end))
  4608. (org-demote)))
  4609. (org-fix-position-after-promote))
  4610. (defun org-fix-position-after-promote ()
  4611. "Make sure that after pro/demotion cursor position is right."
  4612. (let ((pos (point)))
  4613. (when (save-excursion
  4614. (beginning-of-line 1)
  4615. (looking-at org-todo-line-regexp)
  4616. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4617. (cond ((eobp) (insert " "))
  4618. ((eolp) (insert " "))
  4619. ((equal (char-after) ?\ ) (forward-char 1))))))
  4620. (defun org-reduced-level (l)
  4621. "Compute the effective level of a heading.
  4622. This takes into account the setting of `org-odd-levels-only'."
  4623. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4624. (defun org-get-valid-level (level &optional change)
  4625. "Rectify a level change under the influence of `org-odd-levels-only'
  4626. LEVEL is a current level, CHANGE is by how much the level should be
  4627. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4628. even level numbers will become the next higher odd number."
  4629. (if org-odd-levels-only
  4630. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4631. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4632. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4633. (max 1 (+ level change))))
  4634. (if (boundp 'define-obsolete-function-alias)
  4635. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4636. (define-obsolete-function-alias 'org-get-legal-level
  4637. 'org-get-valid-level)
  4638. (define-obsolete-function-alias 'org-get-legal-level
  4639. 'org-get-valid-level "23.1")))
  4640. (defun org-promote ()
  4641. "Promote the current heading higher up the tree.
  4642. If the region is active in `transient-mark-mode', promote all headings
  4643. in the region."
  4644. (org-back-to-heading t)
  4645. (let* ((level (save-match-data (funcall outline-level)))
  4646. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4647. (diff (abs (- level (length up-head) -1))))
  4648. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4649. (replace-match up-head nil t)
  4650. ;; Fixup tag positioning
  4651. (and org-auto-align-tags (org-set-tags nil t))
  4652. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4653. (defun org-demote ()
  4654. "Demote the current heading lower down the tree.
  4655. If the region is active in `transient-mark-mode', demote all headings
  4656. in the region."
  4657. (org-back-to-heading t)
  4658. (let* ((level (save-match-data (funcall outline-level)))
  4659. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4660. (diff (abs (- level (length down-head) -1))))
  4661. (replace-match down-head nil t)
  4662. ;; Fixup tag positioning
  4663. (and org-auto-align-tags (org-set-tags nil t))
  4664. (if org-adapt-indentation (org-fixup-indentation diff))))
  4665. (defun org-map-tree (fun)
  4666. "Call FUN for every heading underneath the current one."
  4667. (org-back-to-heading)
  4668. (let ((level (funcall outline-level)))
  4669. (save-excursion
  4670. (funcall fun)
  4671. (while (and (progn
  4672. (outline-next-heading)
  4673. (> (funcall outline-level) level))
  4674. (not (eobp)))
  4675. (funcall fun)))))
  4676. (defun org-map-region (fun beg end)
  4677. "Call FUN for every heading between BEG and END."
  4678. (let ((org-ignore-region t))
  4679. (save-excursion
  4680. (setq end (copy-marker end))
  4681. (goto-char beg)
  4682. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4683. (< (point) end))
  4684. (funcall fun))
  4685. (while (and (progn
  4686. (outline-next-heading)
  4687. (< (point) end))
  4688. (not (eobp)))
  4689. (funcall fun)))))
  4690. (defun org-fixup-indentation (diff)
  4691. "Change the indentation in the current entry by DIFF
  4692. However, if any line in the current entry has no indentation, or if it
  4693. would end up with no indentation after the change, nothing at all is done."
  4694. (save-excursion
  4695. (let ((end (save-excursion (outline-next-heading)
  4696. (point-marker)))
  4697. (prohibit (if (> diff 0)
  4698. "^\\S-"
  4699. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4700. col)
  4701. (unless (save-excursion (end-of-line 1)
  4702. (re-search-forward prohibit end t))
  4703. (while (and (< (point) end)
  4704. (re-search-forward "^[ \t]+" end t))
  4705. (goto-char (match-end 0))
  4706. (setq col (current-column))
  4707. (if (< diff 0) (replace-match ""))
  4708. (org-indent-to-column (+ diff col))))
  4709. (move-marker end nil))))
  4710. (defun org-convert-to-odd-levels ()
  4711. "Convert an org-mode file with all levels allowed to one with odd levels.
  4712. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4713. level 5 etc."
  4714. (interactive)
  4715. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4716. (let ((org-odd-levels-only nil) n)
  4717. (save-excursion
  4718. (goto-char (point-min))
  4719. (while (re-search-forward "^\\*\\*+ " nil t)
  4720. (setq n (- (length (match-string 0)) 2))
  4721. (while (>= (setq n (1- n)) 0)
  4722. (org-demote))
  4723. (end-of-line 1))))))
  4724. (defun org-convert-to-oddeven-levels ()
  4725. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4726. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4727. section with an even level, conversion would destroy the structure of the file. An error
  4728. is signaled in this case."
  4729. (interactive)
  4730. (goto-char (point-min))
  4731. ;; First check if there are no even levels
  4732. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4733. (org-show-context t)
  4734. (error "Not all levels are odd in this file. Conversion not possible."))
  4735. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4736. (let ((org-odd-levels-only nil) n)
  4737. (save-excursion
  4738. (goto-char (point-min))
  4739. (while (re-search-forward "^\\*\\*+ " nil t)
  4740. (setq n (/ (1- (length (match-string 0))) 2))
  4741. (while (>= (setq n (1- n)) 0)
  4742. (org-promote))
  4743. (end-of-line 1))))))
  4744. (defun org-tr-level (n)
  4745. "Make N odd if required."
  4746. (if org-odd-levels-only (1+ (/ n 2)) n))
  4747. ;;; Vertical tree motion, cutting and pasting of subtrees
  4748. (defun org-move-subtree-up (&optional arg)
  4749. "Move the current subtree up past ARG headlines of the same level."
  4750. (interactive "p")
  4751. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4752. (defun org-move-subtree-down (&optional arg)
  4753. "Move the current subtree down past ARG headlines of the same level."
  4754. (interactive "p")
  4755. (setq arg (prefix-numeric-value arg))
  4756. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4757. 'outline-get-last-sibling))
  4758. (ins-point (make-marker))
  4759. (cnt (abs arg))
  4760. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4761. ;; Select the tree
  4762. (org-back-to-heading)
  4763. (setq beg0 (point))
  4764. (save-excursion
  4765. (setq ne-beg (org-back-over-empty-lines))
  4766. (setq beg (point)))
  4767. (save-match-data
  4768. (save-excursion (outline-end-of-heading)
  4769. (setq folded (org-invisible-p)))
  4770. (outline-end-of-subtree))
  4771. (outline-next-heading)
  4772. (setq ne-end (org-back-over-empty-lines))
  4773. (setq end (point))
  4774. (goto-char beg0)
  4775. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4776. ;; include less whitespace
  4777. (save-excursion
  4778. (goto-char beg)
  4779. (forward-line (- ne-beg ne-end))
  4780. (setq beg (point))))
  4781. ;; Find insertion point, with error handling
  4782. (while (> cnt 0)
  4783. (or (and (funcall movfunc) (looking-at outline-regexp))
  4784. (progn (goto-char beg0)
  4785. (error "Cannot move past superior level or buffer limit")))
  4786. (setq cnt (1- cnt)))
  4787. (if (> arg 0)
  4788. ;; Moving forward - still need to move over subtree
  4789. (progn (org-end-of-subtree t t)
  4790. (save-excursion
  4791. (org-back-over-empty-lines)
  4792. (or (bolp) (newline)))))
  4793. (setq ne-ins (org-back-over-empty-lines))
  4794. (move-marker ins-point (point))
  4795. (setq txt (buffer-substring beg end))
  4796. (org-save-markers-in-region beg end)
  4797. (delete-region beg end)
  4798. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4799. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4800. (let ((bbb (point)))
  4801. (insert-before-markers txt)
  4802. (org-reinstall-markers-in-region bbb)
  4803. (move-marker ins-point bbb))
  4804. (or (bolp) (insert "\n"))
  4805. (setq ins-end (point))
  4806. (goto-char ins-point)
  4807. (org-skip-whitespace)
  4808. (when (and (< arg 0)
  4809. (org-first-sibling-p)
  4810. (> ne-ins ne-beg))
  4811. ;; Move whitespace back to beginning
  4812. (save-excursion
  4813. (goto-char ins-end)
  4814. (let ((kill-whole-line t))
  4815. (kill-line (- ne-ins ne-beg)) (point)))
  4816. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4817. (move-marker ins-point nil)
  4818. (org-compact-display-after-subtree-move)
  4819. (org-show-empty-lines-in-parent)
  4820. (unless folded
  4821. (org-show-entry)
  4822. (show-children)
  4823. (org-cycle-hide-drawers 'children))))
  4824. (defvar org-subtree-clip ""
  4825. "Clipboard for cut and paste of subtrees.
  4826. This is actually only a copy of the kill, because we use the normal kill
  4827. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4828. (defvar org-subtree-clip-folded nil
  4829. "Was the last copied subtree folded?
  4830. This is used to fold the tree back after pasting.")
  4831. (defun org-cut-subtree (&optional n)
  4832. "Cut the current subtree into the clipboard.
  4833. With prefix arg N, cut this many sequential subtrees.
  4834. This is a short-hand for marking the subtree and then cutting it."
  4835. (interactive "p")
  4836. (org-copy-subtree n 'cut))
  4837. (defun org-copy-subtree (&optional n cut force-store-markers)
  4838. "Cut the current subtree into the clipboard.
  4839. With prefix arg N, cut this many sequential subtrees.
  4840. This is a short-hand for marking the subtree and then copying it.
  4841. If CUT is non-nil, actually cut the subtree.
  4842. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4843. of some markers in the region, even if CUT is non-nil. This is
  4844. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4845. (interactive "p")
  4846. (let (beg end folded (beg0 (point)))
  4847. (if (interactive-p)
  4848. (org-back-to-heading nil) ; take what looks like a subtree
  4849. (org-back-to-heading t)) ; take what is really there
  4850. (org-back-over-empty-lines)
  4851. (setq beg (point))
  4852. (skip-chars-forward " \t\r\n")
  4853. (save-match-data
  4854. (save-excursion (outline-end-of-heading)
  4855. (setq folded (org-invisible-p)))
  4856. (condition-case nil
  4857. (outline-forward-same-level (1- n))
  4858. (error nil))
  4859. (org-end-of-subtree t t))
  4860. (org-back-over-empty-lines)
  4861. (setq end (point))
  4862. (goto-char beg0)
  4863. (when (> end beg)
  4864. (setq org-subtree-clip-folded folded)
  4865. (when (or cut force-store-markers)
  4866. (org-save-markers-in-region beg end))
  4867. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4868. (setq org-subtree-clip (current-kill 0))
  4869. (message "%s: Subtree(s) with %d characters"
  4870. (if cut "Cut" "Copied")
  4871. (length org-subtree-clip)))))
  4872. (defun org-paste-subtree (&optional level tree for-yank)
  4873. "Paste the clipboard as a subtree, with modification of headline level.
  4874. The entire subtree is promoted or demoted in order to match a new headline
  4875. level.
  4876. If the cursor is at the beginning of a headline, the same level as
  4877. that headline is used to paste the tree
  4878. If not, the new level is derived from the *visible* headings
  4879. before and after the insertion point, and taken to be the inferior headline
  4880. level of the two. So if the previous visible heading is level 3 and the
  4881. next is level 4 (or vice versa), level 4 will be used for insertion.
  4882. This makes sure that the subtree remains an independent subtree and does
  4883. not swallow low level entries.
  4884. You can also force a different level, either by using a numeric prefix
  4885. argument, or by inserting the heading marker by hand. For example, if the
  4886. cursor is after \"*****\", then the tree will be shifted to level 5.
  4887. If optional TREE is given, use this text instead of the kill ring.
  4888. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4889. move back over whitespace before inserting, and move point to the end of
  4890. the inserted text when done."
  4891. (interactive "P")
  4892. (unless (org-kill-is-subtree-p tree)
  4893. (error "%s"
  4894. (substitute-command-keys
  4895. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4896. (let* ((visp (not (org-invisible-p)))
  4897. (txt (or tree (and kill-ring (current-kill 0))))
  4898. (^re (concat "^\\(" outline-regexp "\\)"))
  4899. (re (concat "\\(" outline-regexp "\\)"))
  4900. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4901. (old-level (if (string-match ^re txt)
  4902. (- (match-end 0) (match-beginning 0) 1)
  4903. -1))
  4904. (force-level (cond (level (prefix-numeric-value level))
  4905. ((and (looking-at "[ \t]*$")
  4906. (string-match
  4907. ^re_ (buffer-substring
  4908. (point-at-bol) (point))))
  4909. (- (match-end 1) (match-beginning 1)))
  4910. ((and (bolp)
  4911. (looking-at org-outline-regexp))
  4912. (- (match-end 0) (point) 1))
  4913. (t nil)))
  4914. (previous-level (save-excursion
  4915. (condition-case nil
  4916. (progn
  4917. (outline-previous-visible-heading 1)
  4918. (if (looking-at re)
  4919. (- (match-end 0) (match-beginning 0) 1)
  4920. 1))
  4921. (error 1))))
  4922. (next-level (save-excursion
  4923. (condition-case nil
  4924. (progn
  4925. (or (looking-at outline-regexp)
  4926. (outline-next-visible-heading 1))
  4927. (if (looking-at re)
  4928. (- (match-end 0) (match-beginning 0) 1)
  4929. 1))
  4930. (error 1))))
  4931. (new-level (or force-level (max previous-level next-level)))
  4932. (shift (if (or (= old-level -1)
  4933. (= new-level -1)
  4934. (= old-level new-level))
  4935. 0
  4936. (- new-level old-level)))
  4937. (delta (if (> shift 0) -1 1))
  4938. (func (if (> shift 0) 'org-demote 'org-promote))
  4939. (org-odd-levels-only nil)
  4940. beg end newend)
  4941. ;; Remove the forced level indicator
  4942. (if force-level
  4943. (delete-region (point-at-bol) (point)))
  4944. ;; Paste
  4945. (beginning-of-line 1)
  4946. (unless for-yank (org-back-over-empty-lines))
  4947. (setq beg (point))
  4948. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4949. (insert-before-markers txt)
  4950. (unless (string-match "\n\\'" txt) (insert "\n"))
  4951. (setq newend (point))
  4952. (org-reinstall-markers-in-region beg)
  4953. (setq end (point))
  4954. (goto-char beg)
  4955. (skip-chars-forward " \t\n\r")
  4956. (setq beg (point))
  4957. (if (and (org-invisible-p) visp)
  4958. (save-excursion (outline-show-heading)))
  4959. ;; Shift if necessary
  4960. (unless (= shift 0)
  4961. (save-restriction
  4962. (narrow-to-region beg end)
  4963. (while (not (= shift 0))
  4964. (org-map-region func (point-min) (point-max))
  4965. (setq shift (+ delta shift)))
  4966. (goto-char (point-min))
  4967. (setq newend (point-max))))
  4968. (when (or (interactive-p) for-yank)
  4969. (message "Clipboard pasted as level %d subtree" new-level))
  4970. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4971. kill-ring
  4972. (eq org-subtree-clip (current-kill 0))
  4973. org-subtree-clip-folded)
  4974. ;; The tree was folded before it was killed/copied
  4975. (hide-subtree))
  4976. (and for-yank (goto-char newend))))
  4977. (defun org-kill-is-subtree-p (&optional txt)
  4978. "Check if the current kill is an outline subtree, or a set of trees.
  4979. Returns nil if kill does not start with a headline, or if the first
  4980. headline level is not the largest headline level in the tree.
  4981. So this will actually accept several entries of equal levels as well,
  4982. which is OK for `org-paste-subtree'.
  4983. If optional TXT is given, check this string instead of the current kill."
  4984. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4985. (start-level (and kill
  4986. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4987. org-outline-regexp "\\)")
  4988. kill)
  4989. (- (match-end 2) (match-beginning 2) 1)))
  4990. (re (concat "^" org-outline-regexp))
  4991. (start (1+ (or (match-beginning 2) -1))))
  4992. (if (not start-level)
  4993. (progn
  4994. nil) ;; does not even start with a heading
  4995. (catch 'exit
  4996. (while (setq start (string-match re kill (1+ start)))
  4997. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4998. (throw 'exit nil)))
  4999. t))))
  5000. (defvar org-markers-to-move nil
  5001. "Markers that should be moved with a cut-and-paste operation.
  5002. Those markers are stored together with their positions relative to
  5003. the start of the region.")
  5004. (defun org-save-markers-in-region (beg end)
  5005. "Check markers in region.
  5006. If these markers are between BEG and END, record their position relative
  5007. to BEG, so that after moving the block of text, we can put the markers back
  5008. into place.
  5009. This function gets called just before an entry or tree gets cut from the
  5010. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5011. called immediately, to move the markers with the entries."
  5012. (setq org-markers-to-move nil)
  5013. (when (featurep 'org-clock)
  5014. (org-clock-save-markers-for-cut-and-paste beg end))
  5015. (when (featurep 'org-agenda)
  5016. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5017. (defun org-check-and-save-marker (marker beg end)
  5018. "Check if MARKER is between BEG and END.
  5019. If yes, remember the marker and the distance to BEG."
  5020. (when (and (marker-buffer marker)
  5021. (equal (marker-buffer marker) (current-buffer)))
  5022. (if (and (>= marker beg) (< marker end))
  5023. (push (cons marker (- marker beg)) org-markers-to-move))))
  5024. (defun org-reinstall-markers-in-region (beg)
  5025. "Move all remembered markers to their position relative to BEG."
  5026. (mapc (lambda (x)
  5027. (move-marker (car x) (+ beg (cdr x))))
  5028. org-markers-to-move)
  5029. (setq org-markers-to-move nil))
  5030. (defun org-narrow-to-subtree ()
  5031. "Narrow buffer to the current subtree."
  5032. (interactive)
  5033. (save-excursion
  5034. (save-match-data
  5035. (narrow-to-region
  5036. (progn (org-back-to-heading) (point))
  5037. (progn (org-end-of-subtree t) (point))))))
  5038. ;;; Outline Sorting
  5039. (defun org-sort (with-case)
  5040. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5041. Optional argument WITH-CASE means sort case-sensitively."
  5042. (interactive "P")
  5043. (if (org-at-table-p)
  5044. (org-call-with-arg 'org-table-sort-lines with-case)
  5045. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5046. (defun org-sort-remove-invisible (s)
  5047. (remove-text-properties 0 (length s) org-rm-props s)
  5048. (while (string-match org-bracket-link-regexp s)
  5049. (setq s (replace-match (if (match-end 2)
  5050. (match-string 3 s)
  5051. (match-string 1 s)) t t s)))
  5052. s)
  5053. (defvar org-priority-regexp) ; defined later in the file
  5054. (defun org-sort-entries-or-items
  5055. (&optional with-case sorting-type getkey-func compare-func property)
  5056. "Sort entries on a certain level of an outline tree.
  5057. If there is an active region, the entries in the region are sorted.
  5058. Else, if the cursor is before the first entry, sort the top-level items.
  5059. Else, the children of the entry at point are sorted.
  5060. Sorting can be alphabetically, numerically, and by date/time as given by
  5061. the first time stamp in the entry. The command prompts for the sorting
  5062. type unless it has been given to the function through the SORTING-TYPE
  5063. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5064. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5065. called with point at the beginning of the record. It must return either
  5066. a string or a number that should serve as the sorting key for that record.
  5067. Comparing entries ignores case by default. However, with an optional argument
  5068. WITH-CASE, the sorting considers case as well."
  5069. (interactive "P")
  5070. (let ((case-func (if with-case 'identity 'downcase))
  5071. start beg end stars re re2
  5072. txt what tmp plain-list-p)
  5073. ;; Find beginning and end of region to sort
  5074. (cond
  5075. ((org-region-active-p)
  5076. ;; we will sort the region
  5077. (setq end (region-end)
  5078. what "region")
  5079. (goto-char (region-beginning))
  5080. (if (not (org-on-heading-p)) (outline-next-heading))
  5081. (setq start (point)))
  5082. ((org-at-item-p)
  5083. ;; we will sort this plain list
  5084. (org-beginning-of-item-list) (setq start (point))
  5085. (org-end-of-item-list) (setq end (point))
  5086. (goto-char start)
  5087. (setq plain-list-p t
  5088. what "plain list"))
  5089. ((or (org-on-heading-p)
  5090. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5091. ;; we will sort the children of the current headline
  5092. (org-back-to-heading)
  5093. (setq start (point)
  5094. end (progn (org-end-of-subtree t t)
  5095. (org-back-over-empty-lines)
  5096. (point))
  5097. what "children")
  5098. (goto-char start)
  5099. (show-subtree)
  5100. (outline-next-heading))
  5101. (t
  5102. ;; we will sort the top-level entries in this file
  5103. (goto-char (point-min))
  5104. (or (org-on-heading-p) (outline-next-heading))
  5105. (setq start (point) end (point-max) what "top-level")
  5106. (goto-char start)
  5107. (show-all)))
  5108. (setq beg (point))
  5109. (if (>= beg end) (error "Nothing to sort"))
  5110. (unless plain-list-p
  5111. (looking-at "\\(\\*+\\)")
  5112. (setq stars (match-string 1)
  5113. re (concat "^" (regexp-quote stars) " +")
  5114. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5115. txt (buffer-substring beg end))
  5116. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5117. (if (and (not (equal stars "*")) (string-match re2 txt))
  5118. (error "Region to sort contains a level above the first entry")))
  5119. (unless sorting-type
  5120. (message
  5121. (if plain-list-p
  5122. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5123. "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:")
  5124. what)
  5125. (setq sorting-type (read-char-exclusive))
  5126. (and (= (downcase sorting-type) ?f)
  5127. (setq getkey-func
  5128. (org-ido-completing-read "Sort using function: "
  5129. obarray 'fboundp t nil nil))
  5130. (setq getkey-func (intern getkey-func)))
  5131. (and (= (downcase sorting-type) ?r)
  5132. (setq property
  5133. (org-ido-completing-read "Property: "
  5134. (mapcar 'list (org-buffer-property-keys t))
  5135. nil t))))
  5136. (message "Sorting entries...")
  5137. (save-restriction
  5138. (narrow-to-region start end)
  5139. (let ((dcst (downcase sorting-type))
  5140. (now (current-time)))
  5141. (sort-subr
  5142. (/= dcst sorting-type)
  5143. ;; This function moves to the beginning character of the "record" to
  5144. ;; be sorted.
  5145. (if plain-list-p
  5146. (lambda nil
  5147. (if (org-at-item-p) t (goto-char (point-max))))
  5148. (lambda nil
  5149. (if (re-search-forward re nil t)
  5150. (goto-char (match-beginning 0))
  5151. (goto-char (point-max)))))
  5152. ;; This function moves to the last character of the "record" being
  5153. ;; sorted.
  5154. (if plain-list-p
  5155. 'org-end-of-item
  5156. (lambda nil
  5157. (save-match-data
  5158. (condition-case nil
  5159. (outline-forward-same-level 1)
  5160. (error
  5161. (goto-char (point-max)))))))
  5162. ;; This function returns the value that gets sorted against.
  5163. (if plain-list-p
  5164. (lambda nil
  5165. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5166. (cond
  5167. ((= dcst ?n)
  5168. (string-to-number (buffer-substring (match-end 0)
  5169. (point-at-eol))))
  5170. ((= dcst ?a)
  5171. (buffer-substring (match-end 0) (point-at-eol)))
  5172. ((= dcst ?t)
  5173. (if (re-search-forward org-ts-regexp
  5174. (point-at-eol) t)
  5175. (org-time-string-to-time (match-string 0))
  5176. now))
  5177. ((= dcst ?f)
  5178. (if getkey-func
  5179. (progn
  5180. (setq tmp (funcall getkey-func))
  5181. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5182. tmp)
  5183. (error "Invalid key function `%s'" getkey-func)))
  5184. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5185. (lambda nil
  5186. (cond
  5187. ((= dcst ?n)
  5188. (if (looking-at org-complex-heading-regexp)
  5189. (string-to-number (match-string 4))
  5190. nil))
  5191. ((= dcst ?a)
  5192. (if (looking-at org-complex-heading-regexp)
  5193. (funcall case-func (match-string 4))
  5194. nil))
  5195. ((= dcst ?t)
  5196. (if (re-search-forward org-ts-regexp
  5197. (save-excursion
  5198. (forward-line 2)
  5199. (point)) t)
  5200. (org-time-string-to-time (match-string 0))
  5201. now))
  5202. ((= dcst ?p)
  5203. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5204. (string-to-char (match-string 2))
  5205. org-default-priority))
  5206. ((= dcst ?r)
  5207. (or (org-entry-get nil property) ""))
  5208. ((= dcst ?o)
  5209. (if (looking-at org-complex-heading-regexp)
  5210. (- 9999 (length (member (match-string 2)
  5211. org-todo-keywords-1)))))
  5212. ((= dcst ?f)
  5213. (if getkey-func
  5214. (progn
  5215. (setq tmp (funcall getkey-func))
  5216. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5217. tmp)
  5218. (error "Invalid key function `%s'" getkey-func)))
  5219. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5220. nil
  5221. (cond
  5222. ((= dcst ?a) 'string<)
  5223. ((= dcst ?t) 'time-less-p)
  5224. ((= dcst ?f) compare-func)
  5225. (t nil)))))
  5226. (message "Sorting entries...done")))
  5227. (defun org-do-sort (table what &optional with-case sorting-type)
  5228. "Sort TABLE of WHAT according to SORTING-TYPE.
  5229. The user will be prompted for the SORTING-TYPE if the call to this
  5230. function does not specify it. WHAT is only for the prompt, to indicate
  5231. what is being sorted. The sorting key will be extracted from
  5232. the car of the elements of the table.
  5233. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5234. (unless sorting-type
  5235. (message
  5236. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5237. what)
  5238. (setq sorting-type (read-char-exclusive)))
  5239. (let ((dcst (downcase sorting-type))
  5240. extractfun comparefun)
  5241. ;; Define the appropriate functions
  5242. (cond
  5243. ((= dcst ?n)
  5244. (setq extractfun 'string-to-number
  5245. comparefun (if (= dcst sorting-type) '< '>)))
  5246. ((= dcst ?a)
  5247. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5248. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5249. comparefun (if (= dcst sorting-type)
  5250. 'string<
  5251. (lambda (a b) (and (not (string< a b))
  5252. (not (string= a b)))))))
  5253. ((= dcst ?t)
  5254. (setq extractfun
  5255. (lambda (x)
  5256. (if (string-match org-ts-regexp x)
  5257. (time-to-seconds
  5258. (org-time-string-to-time (match-string 0 x)))
  5259. 0))
  5260. comparefun (if (= dcst sorting-type) '< '>)))
  5261. (t (error "Invalid sorting type `%c'" sorting-type)))
  5262. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5263. table)
  5264. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5265. ;;; Editing source examples
  5266. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5267. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5268. (defvar org-edit-src-force-single-line nil)
  5269. (defvar org-edit-src-from-org-mode nil)
  5270. (defvar org-edit-src-picture nil)
  5271. (define-minor-mode org-exit-edit-mode
  5272. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5273. (defun org-edit-src-code ()
  5274. "Edit the source code example at point.
  5275. An indirect buffer is created, and that buffer is then narrowed to the
  5276. example at point and switched to the correct language mode. When done,
  5277. exit by killing the buffer with \\[org-edit-src-exit]."
  5278. (interactive)
  5279. (let ((line (org-current-line))
  5280. (case-fold-search t)
  5281. (msg (substitute-command-keys
  5282. "Edit, then exit with C-c ' (C-c and single quote)"))
  5283. (info (org-edit-src-find-region-and-lang))
  5284. (org-mode-p (eq major-mode 'org-mode))
  5285. beg end lang lang-f single lfmt)
  5286. (if (not info)
  5287. nil
  5288. (setq beg (nth 0 info)
  5289. end (nth 1 info)
  5290. lang (nth 2 info)
  5291. single (nth 3 info)
  5292. lfmt (nth 4 info)
  5293. lang-f (intern (concat lang "-mode")))
  5294. (unless (functionp lang-f)
  5295. (error "No such language mode: %s" lang-f))
  5296. (goto-line line)
  5297. (if (get-buffer "*Org Edit Src Example*")
  5298. (kill-buffer "*Org Edit Src Example*"))
  5299. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5300. "*Org Edit Src Example*"))
  5301. (narrow-to-region beg end)
  5302. (remove-text-properties beg end '(display nil invisible nil
  5303. intangible nil))
  5304. (let ((org-inhibit-startup t))
  5305. (funcall lang-f))
  5306. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5307. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5308. (when lfmt
  5309. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5310. (when org-mode-p
  5311. (goto-char (point-min))
  5312. (while (re-search-forward "^," nil t)
  5313. (replace-match "")))
  5314. (goto-line line)
  5315. (org-exit-edit-mode)
  5316. (org-set-local 'header-line-format msg)
  5317. (message "%s" msg)
  5318. t)))
  5319. (defun org-edit-fixed-width-region ()
  5320. "Edit the fixed-width ascii drawing at point.
  5321. This must be a region where each line starts with a colon followed by
  5322. a space character.
  5323. An indirect buffer is created, and that buffer is then narrowed to the
  5324. example at point and switched to artist-mode. When done,
  5325. exit by killing the buffer with \\[org-edit-src-exit]."
  5326. (interactive)
  5327. (let ((line (org-current-line))
  5328. (case-fold-search t)
  5329. (msg (substitute-command-keys
  5330. "Edit, then exit with C-c ' (C-c and single quote)"))
  5331. (org-mode-p (eq major-mode 'org-mode))
  5332. beg end)
  5333. (beginning-of-line 1)
  5334. (if (looking-at "[ \t]*[^:\n \t]")
  5335. nil
  5336. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5337. (setq beg (point) end beg)
  5338. (save-excursion
  5339. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5340. (setq beg (point-at-bol 2))
  5341. (setq beg (point))))
  5342. (save-excursion
  5343. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5344. (setq end (1- (match-beginning 0)))
  5345. (setq end (point))))
  5346. (goto-line line))
  5347. (if (get-buffer "*Org Edit Picture*")
  5348. (kill-buffer "*Org Edit Picture*"))
  5349. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5350. "*Org Edit Picture*"))
  5351. (narrow-to-region beg end)
  5352. (remove-text-properties beg end '(display nil invisible nil
  5353. intangible nil))
  5354. (when (fboundp 'font-lock-unfontify-region)
  5355. (font-lock-unfontify-region (point-min) (point-max)))
  5356. (cond
  5357. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5358. (fundamental-mode)
  5359. (artist-mode 1))
  5360. (t (funcall org-edit-fixed-width-region-mode)))
  5361. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5362. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5363. (set (make-local-variable 'org-edit-src-picture) t)
  5364. (goto-char (point-min))
  5365. (while (re-search-forward "^[ \t]*: ?" nil t)
  5366. (replace-match ""))
  5367. (goto-line line)
  5368. (org-exit-edit-mode)
  5369. (org-set-local 'header-line-format msg)
  5370. (message "%s" msg)
  5371. t)))
  5372. (defun org-edit-src-find-region-and-lang ()
  5373. "Find the region and language for a local edit.
  5374. Return a list with beginning and end of the region, a string representing
  5375. the language, a switch telling of the content should be in a single line."
  5376. (let ((re-list
  5377. (append
  5378. org-edit-src-region-extra
  5379. '(
  5380. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5381. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5382. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5383. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5384. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5385. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5386. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5387. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5388. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5389. ("^#\\+html:" "\n" "html" single-line)
  5390. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5391. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5392. ("^#\\+latex:" "\n" "latex" single-line)
  5393. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5394. ("^#\\+ascii:" "\n" "ascii" single-line)
  5395. )))
  5396. (pos (point))
  5397. re1 re2 single beg end lang lfmt match-re1)
  5398. (catch 'exit
  5399. (while (setq entry (pop re-list))
  5400. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5401. single (nth 3 entry))
  5402. (save-excursion
  5403. (if (or (looking-at re1)
  5404. (re-search-backward re1 nil t))
  5405. (progn
  5406. (setq match-re1 (match-string 0))
  5407. (setq beg (match-end 0)
  5408. lang (org-edit-src-get-lang lang)
  5409. lfmt (org-edit-src-get-label-format match-re1))
  5410. (if (and (re-search-forward re2 nil t)
  5411. (>= (match-end 0) pos))
  5412. (throw 'exit (list beg (match-beginning 0)
  5413. lang single lfmt))))
  5414. (if (or (looking-at re2)
  5415. (re-search-forward re2 nil t))
  5416. (progn
  5417. (setq end (match-beginning 0))
  5418. (if (and (re-search-backward re1 nil t)
  5419. (<= (match-beginning 0) pos))
  5420. (progn
  5421. (setq lfmt (org-edit-src-get-label-format
  5422. (match-string 0)))
  5423. (throw 'exit
  5424. (list (match-end 0) end
  5425. (org-edit-src-get-lang lang)
  5426. single lfmt))))))))))))
  5427. (defun org-edit-src-get-lang (lang)
  5428. "Extract the src language."
  5429. (let ((m (match-string 0)))
  5430. (cond
  5431. ((stringp lang) lang)
  5432. ((integerp lang) (match-string lang))
  5433. ((and (eq lang 'lang)
  5434. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5435. (match-string 1 m))
  5436. ((and (eq lang 'style)
  5437. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5438. (match-string 1 m))
  5439. (t "fundamental"))))
  5440. (defun org-edit-src-get-label-format (s)
  5441. "Extract the label format."
  5442. (save-match-data
  5443. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5444. (match-string 1 s))))
  5445. (defun org-edit-src-exit ()
  5446. "Exit special edit and protect problematic lines."
  5447. (interactive)
  5448. (unless (buffer-base-buffer (current-buffer))
  5449. (error "This is not an indirect buffer, something is wrong..."))
  5450. (unless (> (point-min) 1)
  5451. (error "This buffer is not narrowed, something is wrong..."))
  5452. (goto-char (point-min))
  5453. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5454. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5455. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5456. (goto-char (point-min))
  5457. (while (re-search-forward "\n" nil t)
  5458. (replace-match " "))
  5459. (goto-char (point-min))
  5460. (if (looking-at "\\s-*") (replace-match " "))
  5461. (if (re-search-forward "\\s-+\\'" nil t)
  5462. (replace-match "")))
  5463. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5464. (goto-char (point-min))
  5465. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5466. (replace-match ",\\1"))
  5467. (when font-lock-mode
  5468. (font-lock-unfontify-region (point-min) (point-max)))
  5469. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5470. (when (org-bound-and-true-p org-edit-src-picture)
  5471. (untabify (point-min) (point-max))
  5472. (goto-char (point-min))
  5473. (while (re-search-forward "^" nil t)
  5474. (replace-match ": "))
  5475. (when font-lock-mode
  5476. (font-lock-unfontify-region (point-min) (point-max)))
  5477. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5478. (kill-buffer (current-buffer))
  5479. (and (org-mode-p) (org-restart-font-lock)))
  5480. ;;; The orgstruct minor mode
  5481. ;; Define a minor mode which can be used in other modes in order to
  5482. ;; integrate the org-mode structure editing commands.
  5483. ;; This is really a hack, because the org-mode structure commands use
  5484. ;; keys which normally belong to the major mode. Here is how it
  5485. ;; works: The minor mode defines all the keys necessary to operate the
  5486. ;; structure commands, but wraps the commands into a function which
  5487. ;; tests if the cursor is currently at a headline or a plain list
  5488. ;; item. If that is the case, the structure command is used,
  5489. ;; temporarily setting many Org-mode variables like regular
  5490. ;; expressions for filling etc. However, when any of those keys is
  5491. ;; used at a different location, function uses `key-binding' to look
  5492. ;; up if the key has an associated command in another currently active
  5493. ;; keymap (minor modes, major mode, global), and executes that
  5494. ;; command. There might be problems if any of the keys is otherwise
  5495. ;; used as a prefix key.
  5496. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5497. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5498. ;; addresses this by checking explicitly for both bindings.
  5499. (defvar orgstruct-mode-map (make-sparse-keymap)
  5500. "Keymap for the minor `orgstruct-mode'.")
  5501. (defvar org-local-vars nil
  5502. "List of local variables, for use by `orgstruct-mode'")
  5503. ;;;###autoload
  5504. (define-minor-mode orgstruct-mode
  5505. "Toggle the minor more `orgstruct-mode'.
  5506. This mode is for using Org-mode structure commands in other modes.
  5507. The following key behave as if Org-mode was active, if the cursor
  5508. is on a headline, or on a plain list item (both in the definition
  5509. of Org-mode).
  5510. M-up Move entry/item up
  5511. M-down Move entry/item down
  5512. M-left Promote
  5513. M-right Demote
  5514. M-S-up Move entry/item up
  5515. M-S-down Move entry/item down
  5516. M-S-left Promote subtree
  5517. M-S-right Demote subtree
  5518. M-q Fill paragraph and items like in Org-mode
  5519. C-c ^ Sort entries
  5520. C-c - Cycle list bullet
  5521. TAB Cycle item visibility
  5522. M-RET Insert new heading/item
  5523. S-M-RET Insert new TODO heading / Checkbox item
  5524. C-c C-c Set tags / toggle checkbox"
  5525. nil " OrgStruct" nil
  5526. (org-load-modules-maybe)
  5527. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5528. ;;;###autoload
  5529. (defun turn-on-orgstruct ()
  5530. "Unconditionally turn on `orgstruct-mode'."
  5531. (orgstruct-mode 1))
  5532. ;;;###autoload
  5533. (defun turn-on-orgstruct++ ()
  5534. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5535. In addition to setting orgstruct-mode, this also exports all indentation and
  5536. autofilling variables from org-mode into the buffer. Note that turning
  5537. off orgstruct-mode will *not* remove these additional settings."
  5538. (orgstruct-mode 1)
  5539. (let (var val)
  5540. (mapc
  5541. (lambda (x)
  5542. (when (string-match
  5543. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5544. (symbol-name (car x)))
  5545. (setq var (car x) val (nth 1 x))
  5546. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5547. org-local-vars)))
  5548. (defun orgstruct-error ()
  5549. "Error when there is no default binding for a structure key."
  5550. (interactive)
  5551. (error "This key has no function outside structure elements"))
  5552. (defun orgstruct-setup ()
  5553. "Setup orgstruct keymaps."
  5554. (let ((nfunc 0)
  5555. (bindings
  5556. (list
  5557. '([(meta up)] org-metaup)
  5558. '([(meta down)] org-metadown)
  5559. '([(meta left)] org-metaleft)
  5560. '([(meta right)] org-metaright)
  5561. '([(meta shift up)] org-shiftmetaup)
  5562. '([(meta shift down)] org-shiftmetadown)
  5563. '([(meta shift left)] org-shiftmetaleft)
  5564. '([(meta shift right)] org-shiftmetaright)
  5565. '([(shift up)] org-shiftup)
  5566. '([(shift down)] org-shiftdown)
  5567. '([(shift left)] org-shiftleft)
  5568. '([(shift right)] org-shiftright)
  5569. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5570. '("\M-q" fill-paragraph)
  5571. '("\C-c^" org-sort)
  5572. '("\C-c-" org-cycle-list-bullet)))
  5573. elt key fun cmd)
  5574. (while (setq elt (pop bindings))
  5575. (setq nfunc (1+ nfunc))
  5576. (setq key (org-key (car elt))
  5577. fun (nth 1 elt)
  5578. cmd (orgstruct-make-binding fun nfunc key))
  5579. (org-defkey orgstruct-mode-map key cmd))
  5580. ;; Special treatment needed for TAB and RET
  5581. (org-defkey orgstruct-mode-map [(tab)]
  5582. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5583. (org-defkey orgstruct-mode-map "\C-i"
  5584. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5585. (org-defkey orgstruct-mode-map "\M-\C-m"
  5586. (orgstruct-make-binding 'org-insert-heading 105
  5587. "\M-\C-m" [(meta return)]))
  5588. (org-defkey orgstruct-mode-map [(meta return)]
  5589. (orgstruct-make-binding 'org-insert-heading 106
  5590. [(meta return)] "\M-\C-m"))
  5591. (org-defkey orgstruct-mode-map [(shift meta return)]
  5592. (orgstruct-make-binding 'org-insert-todo-heading 107
  5593. [(meta return)] "\M-\C-m"))
  5594. (unless org-local-vars
  5595. (setq org-local-vars (org-get-local-variables)))
  5596. t))
  5597. (defun orgstruct-make-binding (fun n &rest keys)
  5598. "Create a function for binding in the structure minor mode.
  5599. FUN is the command to call inside a table. N is used to create a unique
  5600. command name. KEYS are keys that should be checked in for a command
  5601. to execute outside of tables."
  5602. (eval
  5603. (list 'defun
  5604. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5605. '(arg)
  5606. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5607. "Outside of structure, run the binding of `"
  5608. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5609. "'.")
  5610. '(interactive "p")
  5611. (list 'if
  5612. '(org-context-p 'headline 'item)
  5613. (list 'org-run-like-in-org-mode (list 'quote fun))
  5614. (list 'let '(orgstruct-mode)
  5615. (list 'call-interactively
  5616. (append '(or)
  5617. (mapcar (lambda (k)
  5618. (list 'key-binding k))
  5619. keys)
  5620. '('orgstruct-error))))))))
  5621. (defun org-context-p (&rest contexts)
  5622. "Check if local context is any of CONTEXTS.
  5623. Possible values in the list of contexts are `table', `headline', and `item'."
  5624. (let ((pos (point)))
  5625. (goto-char (point-at-bol))
  5626. (prog1 (or (and (memq 'table contexts)
  5627. (looking-at "[ \t]*|"))
  5628. (and (memq 'headline contexts)
  5629. ;;????????? (looking-at "\\*+"))
  5630. (looking-at outline-regexp))
  5631. (and (memq 'item contexts)
  5632. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5633. (goto-char pos))))
  5634. (defun org-get-local-variables ()
  5635. "Return a list of all local variables in an org-mode buffer."
  5636. (let (varlist)
  5637. (with-current-buffer (get-buffer-create "*Org tmp*")
  5638. (erase-buffer)
  5639. (org-mode)
  5640. (setq varlist (buffer-local-variables)))
  5641. (kill-buffer "*Org tmp*")
  5642. (delq nil
  5643. (mapcar
  5644. (lambda (x)
  5645. (setq x
  5646. (if (symbolp x)
  5647. (list x)
  5648. (list (car x) (list 'quote (cdr x)))))
  5649. (if (string-match
  5650. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5651. (symbol-name (car x)))
  5652. x nil))
  5653. varlist))))
  5654. ;;;###autoload
  5655. (defun org-run-like-in-org-mode (cmd)
  5656. (org-load-modules-maybe)
  5657. (unless org-local-vars
  5658. (setq org-local-vars (org-get-local-variables)))
  5659. (eval (list 'let org-local-vars
  5660. (list 'call-interactively (list 'quote cmd)))))
  5661. ;;;; Archiving
  5662. (defun org-get-category (&optional pos)
  5663. "Get the category applying to position POS."
  5664. (get-text-property (or pos (point)) 'org-category))
  5665. (defun org-refresh-category-properties ()
  5666. "Refresh category text properties in the buffer."
  5667. (let ((def-cat (cond
  5668. ((null org-category)
  5669. (if buffer-file-name
  5670. (file-name-sans-extension
  5671. (file-name-nondirectory buffer-file-name))
  5672. "???"))
  5673. ((symbolp org-category) (symbol-name org-category))
  5674. (t org-category)))
  5675. beg end cat pos optionp)
  5676. (org-unmodified
  5677. (save-excursion
  5678. (save-restriction
  5679. (widen)
  5680. (goto-char (point-min))
  5681. (put-text-property (point) (point-max) 'org-category def-cat)
  5682. (while (re-search-forward
  5683. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5684. (setq pos (match-end 0)
  5685. optionp (equal (char-after (match-beginning 0)) ?#)
  5686. cat (org-trim (match-string 2)))
  5687. (if optionp
  5688. (setq beg (point-at-bol) end (point-max))
  5689. (org-back-to-heading t)
  5690. (setq beg (point) end (org-end-of-subtree t t)))
  5691. (put-text-property beg end 'org-category cat)
  5692. (goto-char pos)))))))
  5693. ;;;; Link Stuff
  5694. ;;; Link abbreviations
  5695. (defun org-link-expand-abbrev (link)
  5696. "Apply replacements as defined in `org-link-abbrev-alist."
  5697. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5698. (let* ((key (match-string 1 link))
  5699. (as (or (assoc key org-link-abbrev-alist-local)
  5700. (assoc key org-link-abbrev-alist)))
  5701. (tag (and (match-end 2) (match-string 3 link)))
  5702. rpl)
  5703. (if (not as)
  5704. link
  5705. (setq rpl (cdr as))
  5706. (cond
  5707. ((symbolp rpl) (funcall rpl tag))
  5708. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5709. ((string-match "%h" rpl)
  5710. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5711. (t (concat rpl tag)))))
  5712. link))
  5713. ;;; Storing and inserting links
  5714. (defvar org-insert-link-history nil
  5715. "Minibuffer history for links inserted with `org-insert-link'.")
  5716. (defvar org-stored-links nil
  5717. "Contains the links stored with `org-store-link'.")
  5718. (defvar org-store-link-plist nil
  5719. "Plist with info about the most recently link created with `org-store-link'.")
  5720. (defvar org-link-protocols nil
  5721. "Link protocols added to Org-mode using `org-add-link-type'.")
  5722. (defvar org-store-link-functions nil
  5723. "List of functions that are called to create and store a link.
  5724. Each function will be called in turn until one returns a non-nil
  5725. value. Each function should check if it is responsible for creating
  5726. this link (for example by looking at the major mode).
  5727. If not, it must exit and return nil.
  5728. If yes, it should return a non-nil value after a calling
  5729. `org-store-link-props' with a list of properties and values.
  5730. Special properties are:
  5731. :type The link prefix. like \"http\". This must be given.
  5732. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5733. This is obligatory as well.
  5734. :description Optional default description for the second pair
  5735. of brackets in an Org-mode link. The user can still change
  5736. this when inserting this link into an Org-mode buffer.
  5737. In addition to these, any additional properties can be specified
  5738. and then used in remember templates.")
  5739. (defun org-add-link-type (type &optional follow export)
  5740. "Add TYPE to the list of `org-link-types'.
  5741. Re-compute all regular expressions depending on `org-link-types'
  5742. FOLLOW and EXPORT are two functions.
  5743. FOLLOW should take the link path as the single argument and do whatever
  5744. is necessary to follow the link, for example find a file or display
  5745. a mail message.
  5746. EXPORT should format the link path for export to one of the export formats.
  5747. It should be a function accepting three arguments:
  5748. path the path of the link, the text after the prefix (like \"http:\")
  5749. desc the description of the link, if any, nil if there was no description
  5750. format the export format, a symbol like `html' or `latex'.
  5751. The function may use the FORMAT information to return different values
  5752. depending on the format. The return value will be put literally into
  5753. the exported file.
  5754. Org-mode has a built-in default for exporting links. If you are happy with
  5755. this default, there is no need to define an export function for the link
  5756. type. For a simple example of an export function, see `org-bbdb.el'."
  5757. (add-to-list 'org-link-types type t)
  5758. (org-make-link-regexps)
  5759. (if (assoc type org-link-protocols)
  5760. (setcdr (assoc type org-link-protocols) (list follow export))
  5761. (push (list type follow export) org-link-protocols)))
  5762. ;;;###autoload
  5763. (defun org-store-link (arg)
  5764. "\\<org-mode-map>Store an org-link to the current location.
  5765. This link is added to `org-stored-links' and can later be inserted
  5766. into an org-buffer with \\[org-insert-link].
  5767. For some link types, a prefix arg is interpreted:
  5768. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5769. For file links, arg negates `org-context-in-file-links'."
  5770. (interactive "P")
  5771. (org-load-modules-maybe)
  5772. (setq org-store-link-plist nil) ; reset
  5773. (let (link cpltxt desc description search txt)
  5774. (cond
  5775. ((run-hook-with-args-until-success 'org-store-link-functions)
  5776. (setq link (plist-get org-store-link-plist :link)
  5777. desc (or (plist-get org-store-link-plist :description) link)))
  5778. ((equal (buffer-name) "*Org Edit Src Example*")
  5779. (let (label gc)
  5780. (while (or (not label)
  5781. (save-excursion
  5782. (save-restriction
  5783. (widen)
  5784. (goto-char (point-min))
  5785. (re-search-forward
  5786. (regexp-quote (format org-coderef-label-format label))
  5787. nil t))))
  5788. (when label (message "Label exists already") (sit-for 2))
  5789. (setq label (read-string "Code line label: " label)))
  5790. (end-of-line 1)
  5791. (setq link (format org-coderef-label-format label))
  5792. (setq gc (- 79 (length link)))
  5793. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5794. (insert link)
  5795. (setq link (concat "(" label ")") desc nil)))
  5796. ((eq major-mode 'calendar-mode)
  5797. (let ((cd (calendar-cursor-to-date)))
  5798. (setq link
  5799. (format-time-string
  5800. (car org-time-stamp-formats)
  5801. (apply 'encode-time
  5802. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5803. nil nil nil))))
  5804. (org-store-link-props :type "calendar" :date cd)))
  5805. ((eq major-mode 'w3-mode)
  5806. (setq cpltxt (url-view-url t)
  5807. link (org-make-link cpltxt))
  5808. (org-store-link-props :type "w3" :url (url-view-url t)))
  5809. ((eq major-mode 'w3m-mode)
  5810. (setq cpltxt (or w3m-current-title w3m-current-url)
  5811. link (org-make-link w3m-current-url))
  5812. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5813. ((setq search (run-hook-with-args-until-success
  5814. 'org-create-file-search-functions))
  5815. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5816. "::" search))
  5817. (setq cpltxt (or description link)))
  5818. ((eq major-mode 'image-mode)
  5819. (setq cpltxt (concat "file:"
  5820. (abbreviate-file-name buffer-file-name))
  5821. link (org-make-link cpltxt))
  5822. (org-store-link-props :type "image" :file buffer-file-name))
  5823. ((eq major-mode 'dired-mode)
  5824. ;; link to the file in the current line
  5825. (setq cpltxt (concat "file:"
  5826. (abbreviate-file-name
  5827. (expand-file-name
  5828. (dired-get-filename nil t))))
  5829. link (org-make-link cpltxt)))
  5830. ((and buffer-file-name (org-mode-p))
  5831. (cond
  5832. ((org-in-regexp "<<\\(.*?\\)>>")
  5833. (setq cpltxt
  5834. (concat "file:"
  5835. (abbreviate-file-name buffer-file-name)
  5836. "::" (match-string 1))
  5837. link (org-make-link cpltxt)))
  5838. ((and (featurep 'org-id)
  5839. (or (eq org-link-to-org-use-id t)
  5840. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5841. (interactive-p))
  5842. (and org-link-to-org-use-id
  5843. (condition-case nil
  5844. (org-entry-get nil "ID")
  5845. (error nil)))))
  5846. ;; We can make a link using the ID.
  5847. (setq link (condition-case nil
  5848. (prog1 (org-id-store-link)
  5849. (setq desc (plist-get org-store-link-plist
  5850. :description)))
  5851. (error
  5852. ;; probably before first headline, link to file only
  5853. (concat "file:"
  5854. (abbreviate-file-name buffer-file-name))))))
  5855. (t
  5856. ;; Just link to current headline
  5857. (setq cpltxt (concat "file:"
  5858. (abbreviate-file-name buffer-file-name)))
  5859. ;; Add a context search string
  5860. (when (org-xor org-context-in-file-links arg)
  5861. (setq txt (cond
  5862. ((org-on-heading-p) nil)
  5863. ((org-region-active-p)
  5864. (buffer-substring (region-beginning) (region-end)))
  5865. (t nil)))
  5866. (when (or (null txt) (string-match "\\S-" txt))
  5867. (setq cpltxt
  5868. (concat cpltxt "::"
  5869. (condition-case nil
  5870. (org-make-org-heading-search-string txt)
  5871. (error "")))
  5872. desc "NONE")))
  5873. (if (string-match "::\\'" cpltxt)
  5874. (setq cpltxt (substring cpltxt 0 -2)))
  5875. (setq link (org-make-link cpltxt)))))
  5876. ((buffer-file-name (buffer-base-buffer))
  5877. ;; Just link to this file here.
  5878. (setq cpltxt (concat "file:"
  5879. (abbreviate-file-name
  5880. (buffer-file-name (buffer-base-buffer)))))
  5881. ;; Add a context string
  5882. (when (org-xor org-context-in-file-links arg)
  5883. (setq txt (if (org-region-active-p)
  5884. (buffer-substring (region-beginning) (region-end))
  5885. (buffer-substring (point-at-bol) (point-at-eol))))
  5886. ;; Only use search option if there is some text.
  5887. (when (string-match "\\S-" txt)
  5888. (setq cpltxt
  5889. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5890. desc "NONE")))
  5891. (setq link (org-make-link cpltxt)))
  5892. ((interactive-p)
  5893. (error "Cannot link to a buffer which is not visiting a file"))
  5894. (t (setq link nil)))
  5895. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5896. (setq link (or link cpltxt)
  5897. desc (or desc cpltxt))
  5898. (if (equal desc "NONE") (setq desc nil))
  5899. (if (and (interactive-p) link)
  5900. (progn
  5901. (setq org-stored-links
  5902. (cons (list link desc) org-stored-links))
  5903. (message "Stored: %s" (or desc link)))
  5904. (and link (org-make-link-string link desc)))))
  5905. (defun org-store-link-props (&rest plist)
  5906. "Store link properties, extract names and addresses."
  5907. (let (x adr)
  5908. (when (setq x (plist-get plist :from))
  5909. (setq adr (mail-extract-address-components x))
  5910. (setq plist (plist-put plist :fromname (car adr)))
  5911. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5912. (when (setq x (plist-get plist :to))
  5913. (setq adr (mail-extract-address-components x))
  5914. (setq plist (plist-put plist :toname (car adr)))
  5915. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5916. (let ((from (plist-get plist :from))
  5917. (to (plist-get plist :to)))
  5918. (when (and from to org-from-is-user-regexp)
  5919. (setq plist
  5920. (plist-put plist :fromto
  5921. (if (string-match org-from-is-user-regexp from)
  5922. (concat "to %t")
  5923. (concat "from %f"))))))
  5924. (setq org-store-link-plist plist))
  5925. (defun org-add-link-props (&rest plist)
  5926. "Add these properties to the link property list."
  5927. (let (key value)
  5928. (while plist
  5929. (setq key (pop plist) value (pop plist))
  5930. (setq org-store-link-plist
  5931. (plist-put org-store-link-plist key value)))))
  5932. (defun org-email-link-description (&optional fmt)
  5933. "Return the description part of an email link.
  5934. This takes information from `org-store-link-plist' and formats it
  5935. according to FMT (default from `org-email-link-description-format')."
  5936. (setq fmt (or fmt org-email-link-description-format))
  5937. (let* ((p org-store-link-plist)
  5938. (to (plist-get p :toaddress))
  5939. (from (plist-get p :fromaddress))
  5940. (table
  5941. (list
  5942. (cons "%c" (plist-get p :fromto))
  5943. (cons "%F" (plist-get p :from))
  5944. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5945. (cons "%T" (plist-get p :to))
  5946. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5947. (cons "%s" (plist-get p :subject))
  5948. (cons "%m" (plist-get p :message-id)))))
  5949. (when (string-match "%c" fmt)
  5950. ;; Check if the user wrote this message
  5951. (if (and org-from-is-user-regexp from to
  5952. (save-match-data (string-match org-from-is-user-regexp from)))
  5953. (setq fmt (replace-match "to %t" t t fmt))
  5954. (setq fmt (replace-match "from %f" t t fmt))))
  5955. (org-replace-escapes fmt table)))
  5956. (defun org-make-org-heading-search-string (&optional string heading)
  5957. "Make search string for STRING or current headline."
  5958. (interactive)
  5959. (let ((s (or string (org-get-heading))))
  5960. (unless (and string (not heading))
  5961. ;; We are using a headline, clean up garbage in there.
  5962. (if (string-match org-todo-regexp s)
  5963. (setq s (replace-match "" t t s)))
  5964. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5965. (setq s (replace-match "" t t s)))
  5966. (setq s (org-trim s))
  5967. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5968. org-comment-string "\\)") s)
  5969. (setq s (replace-match "" t t s)))
  5970. (while (string-match org-ts-regexp s)
  5971. (setq s (replace-match "" t t s))))
  5972. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5973. (setq s (replace-match " " t t s)))
  5974. (or string (setq s (concat "*" s))) ; Add * for headlines
  5975. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5976. (defun org-make-link (&rest strings)
  5977. "Concatenate STRINGS."
  5978. (apply 'concat strings))
  5979. (defun org-make-link-string (link &optional description)
  5980. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5981. (unless (string-match "\\S-" link)
  5982. (error "Empty link"))
  5983. (when (stringp description)
  5984. ;; Remove brackets from the description, they are fatal.
  5985. (while (string-match "\\[" description)
  5986. (setq description (replace-match "{" t t description)))
  5987. (while (string-match "\\]" description)
  5988. (setq description (replace-match "}" t t description))))
  5989. (when (equal (org-link-escape link) description)
  5990. ;; No description needed, it is identical
  5991. (setq description nil))
  5992. (when (and (not description)
  5993. (not (equal link (org-link-escape link))))
  5994. (setq description (org-extract-attributes link)))
  5995. (concat "[[" (org-link-escape link) "]"
  5996. (if description (concat "[" description "]") "")
  5997. "]"))
  5998. (defconst org-link-escape-chars
  5999. '((?\ . "%20")
  6000. (?\[ . "%5B")
  6001. (?\] . "%5D")
  6002. (?\340 . "%E0") ; `a
  6003. (?\342 . "%E2") ; ^a
  6004. (?\347 . "%E7") ; ,c
  6005. (?\350 . "%E8") ; `e
  6006. (?\351 . "%E9") ; 'e
  6007. (?\352 . "%EA") ; ^e
  6008. (?\356 . "%EE") ; ^i
  6009. (?\364 . "%F4") ; ^o
  6010. (?\371 . "%F9") ; `u
  6011. (?\373 . "%FB") ; ^u
  6012. (?\; . "%3B")
  6013. (?? . "%3F")
  6014. (?= . "%3D")
  6015. (?+ . "%2B")
  6016. )
  6017. "Association list of escapes for some characters problematic in links.
  6018. This is the list that is used for internal purposes.")
  6019. (defconst org-link-escape-chars-browser
  6020. '((?\ . "%20")) ; 32 for the SPC char
  6021. "Association list of escapes for some characters problematic in links.
  6022. This is the list that is used before handing over to the browser.")
  6023. (defun org-link-escape (text &optional table)
  6024. "Escape characters in TEXT that are problematic for links."
  6025. (setq table (or table org-link-escape-chars))
  6026. (when text
  6027. (let ((re (mapconcat (lambda (x) (regexp-quote
  6028. (char-to-string (car x))))
  6029. table "\\|")))
  6030. (while (string-match re text)
  6031. (setq text
  6032. (replace-match
  6033. (cdr (assoc (string-to-char (match-string 0 text))
  6034. table))
  6035. t t text)))
  6036. text)))
  6037. (defun org-link-unescape (text &optional table)
  6038. "Reverse the action of `org-link-escape'."
  6039. (setq table (or table org-link-escape-chars))
  6040. (when text
  6041. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6042. table "\\|")))
  6043. (while (string-match re text)
  6044. (setq text
  6045. (replace-match
  6046. (char-to-string (car (rassoc (match-string 0 text) table)))
  6047. t t text)))
  6048. text)))
  6049. (defun org-xor (a b)
  6050. "Exclusive or."
  6051. (if a (not b) b))
  6052. (defun org-fixup-message-id-for-http (s)
  6053. "Replace special characters in a message id, so it can be used in an http query."
  6054. (while (string-match "<" s)
  6055. (setq s (replace-match "%3C" t t s)))
  6056. (while (string-match ">" s)
  6057. (setq s (replace-match "%3E" t t s)))
  6058. (while (string-match "@" s)
  6059. (setq s (replace-match "%40" t t s)))
  6060. s)
  6061. ;;;###autoload
  6062. (defun org-insert-link-global ()
  6063. "Insert a link like Org-mode does.
  6064. This command can be called in any mode to insert a link in Org-mode syntax."
  6065. (interactive)
  6066. (org-load-modules-maybe)
  6067. (org-run-like-in-org-mode 'org-insert-link))
  6068. (defun org-insert-link (&optional complete-file link-location)
  6069. "Insert a link. At the prompt, enter the link.
  6070. Completion can be used to insert any of the link protocol prefixes like
  6071. http or ftp in use.
  6072. The history can be used to select a link previously stored with
  6073. `org-store-link'. When the empty string is entered (i.e. if you just
  6074. press RET at the prompt), the link defaults to the most recently
  6075. stored link. As SPC triggers completion in the minibuffer, you need to
  6076. use M-SPC or C-q SPC to force the insertion of a space character.
  6077. You will also be prompted for a description, and if one is given, it will
  6078. be displayed in the buffer instead of the link.
  6079. If there is already a link at point, this command will allow you to edit link
  6080. and description parts.
  6081. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6082. be selected using completion. The path to the file will be relative to the
  6083. current directory if the file is in the current directory or a subdirectory.
  6084. Otherwise, the link will be the absolute path as completed in the minibuffer
  6085. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6086. option `org-link-file-path-type'.
  6087. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6088. the current directory or below.
  6089. With three \\[universal-argument] prefixes, negate the meaning of
  6090. `org-keep-stored-link-after-insertion'.
  6091. If `org-make-link-description-function' is non-nil, this function will be
  6092. called with the link target, and the result will be the default
  6093. link description.
  6094. If the LINK-LOCATION parameter is non-nil, this value will be
  6095. used as the link location instead of reading one interactively."
  6096. (interactive "P")
  6097. (let* ((wcf (current-window-configuration))
  6098. (region (if (org-region-active-p)
  6099. (buffer-substring (region-beginning) (region-end))))
  6100. (remove (and region (list (region-beginning) (region-end))))
  6101. (desc region)
  6102. tmphist ; byte-compile incorrectly complains about this
  6103. (link link-location)
  6104. entry file)
  6105. (cond
  6106. (link-location) ; specified by arg, just use it.
  6107. ((org-in-regexp org-bracket-link-regexp 1)
  6108. ;; We do have a link at point, and we are going to edit it.
  6109. (setq remove (list (match-beginning 0) (match-end 0)))
  6110. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6111. (setq link (read-string "Link: "
  6112. (org-link-unescape
  6113. (org-match-string-no-properties 1)))))
  6114. ((or (org-in-regexp org-angle-link-re)
  6115. (org-in-regexp org-plain-link-re))
  6116. ;; Convert to bracket link
  6117. (setq remove (list (match-beginning 0) (match-end 0))
  6118. link (read-string "Link: "
  6119. (org-remove-angle-brackets (match-string 0)))))
  6120. ((member complete-file '((4) (16)))
  6121. ;; Completing read for file names.
  6122. (setq file (read-file-name "File: "))
  6123. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6124. (pwd1 (file-name-as-directory (abbreviate-file-name
  6125. (expand-file-name ".")))))
  6126. (cond
  6127. ((equal complete-file '(16))
  6128. (setq link (org-make-link
  6129. "file:"
  6130. (abbreviate-file-name (expand-file-name file)))))
  6131. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6132. (setq link (org-make-link "file:" (match-string 1 file))))
  6133. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6134. (expand-file-name file))
  6135. (setq link (org-make-link
  6136. "file:" (match-string 1 (expand-file-name file)))))
  6137. (t (setq link (org-make-link "file:" file))))))
  6138. (t
  6139. ;; Read link, with completion for stored links.
  6140. (with-output-to-temp-buffer "*Org Links*"
  6141. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6142. (when org-stored-links
  6143. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6144. (princ (mapconcat
  6145. (lambda (x)
  6146. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6147. (reverse org-stored-links) "\n"))))
  6148. (let ((cw (selected-window)))
  6149. (select-window (get-buffer-window "*Org Links*"))
  6150. (setq truncate-lines t)
  6151. (org-fit-window-to-buffer)
  6152. (select-window cw))
  6153. ;; Fake a link history, containing the stored links.
  6154. (setq tmphist (append (mapcar 'car org-stored-links)
  6155. org-insert-link-history))
  6156. (unwind-protect
  6157. (setq link
  6158. (let ((org-completion-use-ido nil))
  6159. (org-completing-read
  6160. "Link: "
  6161. (append
  6162. (mapcar (lambda (x) (list (concat (car x) ":")))
  6163. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6164. (mapcar (lambda (x) (list (concat x ":")))
  6165. org-link-types))
  6166. nil nil nil
  6167. 'tmphist
  6168. (or (car (car org-stored-links))))))
  6169. (set-window-configuration wcf)
  6170. (kill-buffer "*Org Links*"))
  6171. (setq entry (assoc link org-stored-links))
  6172. (or entry (push link org-insert-link-history))
  6173. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6174. (not org-keep-stored-link-after-insertion))
  6175. (setq org-stored-links (delq (assoc link org-stored-links)
  6176. org-stored-links)))
  6177. (setq desc (or desc (nth 1 entry)))))
  6178. (if (string-match org-plain-link-re link)
  6179. ;; URL-like link, normalize the use of angular brackets.
  6180. (setq link (org-make-link (org-remove-angle-brackets link))))
  6181. ;; Check if we are linking to the current file with a search option
  6182. ;; If yes, simplify the link by using only the search option.
  6183. (when (and buffer-file-name
  6184. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6185. (let* ((path (match-string 1 link))
  6186. (case-fold-search nil)
  6187. (search (match-string 2 link)))
  6188. (save-match-data
  6189. (if (equal (file-truename buffer-file-name) (file-truename path))
  6190. ;; We are linking to this same file, with a search option
  6191. (setq link search)))))
  6192. ;; Check if we can/should use a relative path. If yes, simplify the link
  6193. (when (string-match "^file:\\(.*\\)" link)
  6194. (let* ((path (match-string 1 link))
  6195. (origpath path)
  6196. (case-fold-search nil))
  6197. (cond
  6198. ((or (eq org-link-file-path-type 'absolute)
  6199. (equal complete-file '(16)))
  6200. (setq path (abbreviate-file-name (expand-file-name path))))
  6201. ((eq org-link-file-path-type 'noabbrev)
  6202. (setq path (expand-file-name path)))
  6203. ((eq org-link-file-path-type 'relative)
  6204. (setq path (file-relative-name path)))
  6205. (t
  6206. (save-match-data
  6207. (if (string-match (concat "^" (regexp-quote
  6208. (file-name-as-directory
  6209. (expand-file-name "."))))
  6210. (expand-file-name path))
  6211. ;; We are linking a file with relative path name.
  6212. (setq path (substring (expand-file-name path)
  6213. (match-end 0)))
  6214. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6215. (setq link (concat "file:" path))
  6216. (if (equal desc origpath)
  6217. (setq desc path))))
  6218. (if org-make-link-description-function
  6219. (setq desc (funcall org-make-link-description-function link desc)))
  6220. (setq desc (read-string "Description: " desc))
  6221. (unless (string-match "\\S-" desc) (setq desc nil))
  6222. (if remove (apply 'delete-region remove))
  6223. (insert (org-make-link-string link desc))))
  6224. (defun org-completing-read (&rest args)
  6225. "Completing-read with SPACE being a normal character."
  6226. (let ((minibuffer-local-completion-map
  6227. (copy-keymap minibuffer-local-completion-map)))
  6228. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6229. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6230. (apply 'org-ido-completing-read args)))
  6231. (defun org-ido-completing-read (&rest args)
  6232. "Completing-read using `ido-mode' speedups if available"
  6233. (if (and org-completion-use-ido
  6234. (fboundp 'ido-completing-read)
  6235. (boundp 'ido-mode) ido-mode
  6236. (listp (second args)))
  6237. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6238. (apply 'completing-read args)))
  6239. (defun org-extract-attributes (s)
  6240. "Extract the attributes cookie from a string and set as text property."
  6241. (let (a attr (start 0) key value)
  6242. (save-match-data
  6243. (when (string-match "{{\\([^}]+\\)}}$" s)
  6244. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6245. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6246. (setq key (match-string 1 a) value (match-string 2 a)
  6247. start (match-end 0)
  6248. attr (plist-put attr (intern key) value))))
  6249. (org-add-props s nil 'org-attr attr))
  6250. s))
  6251. (defun org-attributes-to-string (plist)
  6252. "Format a property list into an HTML attribute list."
  6253. (let ((s "") key value)
  6254. (while plist
  6255. (setq key (pop plist) value (pop plist))
  6256. (and value
  6257. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6258. s))
  6259. ;;; Opening/following a link
  6260. (defvar org-link-search-failed nil)
  6261. (defun org-next-link ()
  6262. "Move forward to the next link.
  6263. If the link is in hidden text, expose it."
  6264. (interactive)
  6265. (when (and org-link-search-failed (eq this-command last-command))
  6266. (goto-char (point-min))
  6267. (message "Link search wrapped back to beginning of buffer"))
  6268. (setq org-link-search-failed nil)
  6269. (let* ((pos (point))
  6270. (ct (org-context))
  6271. (a (assoc :link ct)))
  6272. (if a (goto-char (nth 2 a)))
  6273. (if (re-search-forward org-any-link-re nil t)
  6274. (progn
  6275. (goto-char (match-beginning 0))
  6276. (if (org-invisible-p) (org-show-context)))
  6277. (goto-char pos)
  6278. (setq org-link-search-failed t)
  6279. (error "No further link found"))))
  6280. (defun org-previous-link ()
  6281. "Move backward to the previous link.
  6282. If the link is in hidden text, expose it."
  6283. (interactive)
  6284. (when (and org-link-search-failed (eq this-command last-command))
  6285. (goto-char (point-max))
  6286. (message "Link search wrapped back to end of buffer"))
  6287. (setq org-link-search-failed nil)
  6288. (let* ((pos (point))
  6289. (ct (org-context))
  6290. (a (assoc :link ct)))
  6291. (if a (goto-char (nth 1 a)))
  6292. (if (re-search-backward org-any-link-re nil t)
  6293. (progn
  6294. (goto-char (match-beginning 0))
  6295. (if (org-invisible-p) (org-show-context)))
  6296. (goto-char pos)
  6297. (setq org-link-search-failed t)
  6298. (error "No further link found"))))
  6299. (defun org-translate-link (s)
  6300. "Translate a link string if a translation function has been defined."
  6301. (if (and org-link-translation-function
  6302. (fboundp org-link-translation-function)
  6303. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6304. (progn
  6305. (setq s (funcall org-link-translation-function
  6306. (match-string 1) (match-string 2)))
  6307. (concat (car s) ":" (cdr s)))
  6308. s))
  6309. (defun org-translate-link-from-planner (type path)
  6310. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6311. This is still an experimental function, your mileage may vary."
  6312. (cond
  6313. ((member type '("http" "https" "news" "ftp"))
  6314. ;; standard Internet links are the same.
  6315. nil)
  6316. ((and (equal type "irc") (string-match "^//" path))
  6317. ;; Planner has two / at the beginning of an irc link, we have 1.
  6318. ;; We should have zero, actually....
  6319. (setq path (substring path 1)))
  6320. ((and (equal type "lisp") (string-match "^/" path))
  6321. ;; Planner has a slash, we do not.
  6322. (setq type "elisp" path (substring path 1)))
  6323. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6324. ;; A typical message link. Planner has the id after the fina slash,
  6325. ;; we separate it with a hash mark
  6326. (setq path (concat (match-string 1 path) "#"
  6327. (org-remove-angle-brackets (match-string 2 path)))))
  6328. )
  6329. (cons type path))
  6330. (defun org-find-file-at-mouse (ev)
  6331. "Open file link or URL at mouse."
  6332. (interactive "e")
  6333. (mouse-set-point ev)
  6334. (org-open-at-point 'in-emacs))
  6335. (defun org-open-at-mouse (ev)
  6336. "Open file link or URL at mouse."
  6337. (interactive "e")
  6338. (mouse-set-point ev)
  6339. (if (eq major-mode 'org-agenda-mode)
  6340. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6341. (org-open-at-point))
  6342. (defvar org-window-config-before-follow-link nil
  6343. "The window configuration before following a link.
  6344. This is saved in case the need arises to restore it.")
  6345. (defvar org-open-link-marker (make-marker)
  6346. "Marker pointing to the location where `org-open-at-point; was called.")
  6347. ;;;###autoload
  6348. (defun org-open-at-point-global ()
  6349. "Follow a link like Org-mode does.
  6350. This command can be called in any mode to follow a link that has
  6351. Org-mode syntax."
  6352. (interactive)
  6353. (org-run-like-in-org-mode 'org-open-at-point))
  6354. ;;;###autoload
  6355. (defun org-open-link-from-string (s &optional arg)
  6356. "Open a link in the string S, as if it was in Org-mode."
  6357. (interactive "sLink: \nP")
  6358. (with-temp-buffer
  6359. (let ((org-inhibit-startup t))
  6360. (org-mode)
  6361. (insert s)
  6362. (goto-char (point-min))
  6363. (org-open-at-point arg))))
  6364. (defun org-open-at-point (&optional in-emacs)
  6365. "Open link at or after point.
  6366. If there is no link at point, this function will search forward up to
  6367. the end of the current subtree.
  6368. Normally, files will be opened by an appropriate application. If the
  6369. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6370. With a double prefix argument, try to open outside of Emacs, in the
  6371. application the system uses for this file type."
  6372. (interactive "P")
  6373. (org-load-modules-maybe)
  6374. (move-marker org-open-link-marker (point))
  6375. (setq org-window-config-before-follow-link (current-window-configuration))
  6376. (org-remove-occur-highlights nil nil t)
  6377. (cond
  6378. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6379. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6380. (org-footnote-action))
  6381. (t
  6382. (let (type path link line search (pos (point)))
  6383. (catch 'match
  6384. (save-excursion
  6385. (skip-chars-forward "^]\n\r")
  6386. (when (org-in-regexp org-bracket-link-regexp)
  6387. (setq link (org-extract-attributes
  6388. (org-link-unescape (org-match-string-no-properties 1))))
  6389. (while (string-match " *\n *" link)
  6390. (setq link (replace-match " " t t link)))
  6391. (setq link (org-link-expand-abbrev link))
  6392. (cond
  6393. ((or (file-name-absolute-p link)
  6394. (string-match "^\\.\\.?/" link))
  6395. (setq type "file" path link))
  6396. ((string-match org-link-re-with-space3 link)
  6397. (setq type (match-string 1 link) path (match-string 2 link)))
  6398. (t (setq type "thisfile" path link)))
  6399. (throw 'match t)))
  6400. (when (get-text-property (point) 'org-linked-text)
  6401. (setq type "thisfile"
  6402. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6403. (1+ (point)) (point))
  6404. path (buffer-substring
  6405. (previous-single-property-change pos 'org-linked-text)
  6406. (next-single-property-change pos 'org-linked-text)))
  6407. (throw 'match t))
  6408. (save-excursion
  6409. (when (or (org-in-regexp org-angle-link-re)
  6410. (org-in-regexp org-plain-link-re))
  6411. (setq type (match-string 1) path (match-string 2))
  6412. (throw 'match t)))
  6413. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6414. (setq type "tree-match"
  6415. path (match-string 1))
  6416. (throw 'match t))
  6417. (save-excursion
  6418. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6419. (setq type "tags"
  6420. path (match-string 1))
  6421. (while (string-match ":" path)
  6422. (setq path (replace-match "+" t t path)))
  6423. (throw 'match t))))
  6424. (unless path
  6425. (error "No link found"))
  6426. ;; Remove any trailing spaces in path
  6427. (if (string-match " +\\'" path)
  6428. (setq path (replace-match "" t t path)))
  6429. (if (and org-link-translation-function
  6430. (fboundp org-link-translation-function))
  6431. ;; Check if we need to translate the link
  6432. (let ((tmp (funcall org-link-translation-function type path)))
  6433. (setq type (car tmp) path (cdr tmp))))
  6434. (cond
  6435. ((assoc type org-link-protocols)
  6436. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6437. ((equal type "mailto")
  6438. (let ((cmd (car org-link-mailto-program))
  6439. (args (cdr org-link-mailto-program)) args1
  6440. (address path) (subject "") a)
  6441. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6442. (setq address (match-string 1 path)
  6443. subject (org-link-escape (match-string 2 path))))
  6444. (while args
  6445. (cond
  6446. ((not (stringp (car args))) (push (pop args) args1))
  6447. (t (setq a (pop args))
  6448. (if (string-match "%a" a)
  6449. (setq a (replace-match address t t a)))
  6450. (if (string-match "%s" a)
  6451. (setq a (replace-match subject t t a)))
  6452. (push a args1))))
  6453. (apply cmd (nreverse args1))))
  6454. ((member type '("http" "https" "ftp" "news"))
  6455. (browse-url (concat type ":" (org-link-escape
  6456. path org-link-escape-chars-browser))))
  6457. ((member type '("message"))
  6458. (browse-url (concat type ":" path)))
  6459. ((string= type "tags")
  6460. (org-tags-view in-emacs path))
  6461. ((string= type "thisfile")
  6462. (if in-emacs
  6463. (switch-to-buffer-other-window
  6464. (org-get-buffer-for-internal-link (current-buffer)))
  6465. (org-mark-ring-push))
  6466. (let ((cmd `(org-link-search
  6467. ,path
  6468. ,(cond ((equal in-emacs '(4)) 'occur)
  6469. ((equal in-emacs '(16)) 'org-occur)
  6470. (t nil))
  6471. ,pos)))
  6472. (condition-case nil (eval cmd)
  6473. (error (progn (widen) (eval cmd))))))
  6474. ((string= type "tree-match")
  6475. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6476. ((string= type "file")
  6477. (if (string-match "::\\([0-9]+\\)\\'" path)
  6478. (setq line (string-to-number (match-string 1 path))
  6479. path (substring path 0 (match-beginning 0)))
  6480. (if (string-match "::\\(.+\\)\\'" path)
  6481. (setq search (match-string 1 path)
  6482. path (substring path 0 (match-beginning 0)))))
  6483. (if (string-match "[*?{]" (file-name-nondirectory path))
  6484. (dired path)
  6485. (org-open-file path in-emacs line search)))
  6486. ((string= type "news")
  6487. (require 'org-gnus)
  6488. (org-gnus-follow-link path))
  6489. ((string= type "shell")
  6490. (let ((cmd path))
  6491. (if (or (not org-confirm-shell-link-function)
  6492. (funcall org-confirm-shell-link-function
  6493. (format "Execute \"%s\" in shell? "
  6494. (org-add-props cmd nil
  6495. 'face 'org-warning))))
  6496. (progn
  6497. (message "Executing %s" cmd)
  6498. (shell-command cmd))
  6499. (error "Abort"))))
  6500. ((string= type "elisp")
  6501. (let ((cmd path))
  6502. (if (or (not org-confirm-elisp-link-function)
  6503. (funcall org-confirm-elisp-link-function
  6504. (format "Execute \"%s\" as elisp? "
  6505. (org-add-props cmd nil
  6506. 'face 'org-warning))))
  6507. (message "%s => %s" cmd
  6508. (if (equal (string-to-char cmd) ?\()
  6509. (eval (read cmd))
  6510. (call-interactively (read cmd))))
  6511. (error "Abort"))))
  6512. (t
  6513. (browse-url-at-point))))))
  6514. (move-marker org-open-link-marker nil)
  6515. (run-hook-with-args 'org-follow-link-hook))
  6516. ;;;; Time estimates
  6517. (defun org-get-effort (&optional pom)
  6518. "Get the effort estimate for the current entry."
  6519. (org-entry-get pom org-effort-property))
  6520. ;;; File search
  6521. (defvar org-create-file-search-functions nil
  6522. "List of functions to construct the right search string for a file link.
  6523. These functions are called in turn with point at the location to
  6524. which the link should point.
  6525. A function in the hook should first test if it would like to
  6526. handle this file type, for example by checking the major-mode or
  6527. the file extension. If it decides not to handle this file, it
  6528. should just return nil to give other functions a chance. If it
  6529. does handle the file, it must return the search string to be used
  6530. when following the link. The search string will be part of the
  6531. file link, given after a double colon, and `org-open-at-point'
  6532. will automatically search for it. If special measures must be
  6533. taken to make the search successful, another function should be
  6534. added to the companion hook `org-execute-file-search-functions',
  6535. which see.
  6536. A function in this hook may also use `setq' to set the variable
  6537. `description' to provide a suggestion for the descriptive text to
  6538. be used for this link when it gets inserted into an Org-mode
  6539. buffer with \\[org-insert-link].")
  6540. (defvar org-execute-file-search-functions nil
  6541. "List of functions to execute a file search triggered by a link.
  6542. Functions added to this hook must accept a single argument, the
  6543. search string that was part of the file link, the part after the
  6544. double colon. The function must first check if it would like to
  6545. handle this search, for example by checking the major-mode or the
  6546. file extension. If it decides not to handle this search, it
  6547. should just return nil to give other functions a chance. If it
  6548. does handle the search, it must return a non-nil value to keep
  6549. other functions from trying.
  6550. Each function can access the current prefix argument through the
  6551. variable `current-prefix-argument'. Note that a single prefix is
  6552. used to force opening a link in Emacs, so it may be good to only
  6553. use a numeric or double prefix to guide the search function.
  6554. In case this is needed, a function in this hook can also restore
  6555. the window configuration before `org-open-at-point' was called using:
  6556. (set-window-configuration org-window-config-before-follow-link)")
  6557. (defun org-link-search (s &optional type avoid-pos)
  6558. "Search for a link search option.
  6559. If S is surrounded by forward slashes, it is interpreted as a
  6560. regular expression. In org-mode files, this will create an `org-occur'
  6561. sparse tree. In ordinary files, `occur' will be used to list matches.
  6562. If the current buffer is in `dired-mode', grep will be used to search
  6563. in all files. If AVOID-POS is given, ignore matches near that position."
  6564. (let ((case-fold-search t)
  6565. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6566. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6567. (append '(("") (" ") ("\t") ("\n"))
  6568. org-emphasis-alist)
  6569. "\\|") "\\)"))
  6570. (pos (point))
  6571. (pre nil) (post nil)
  6572. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6573. (cond
  6574. ;; First check if there are any special
  6575. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6576. ;; Now try the builtin stuff
  6577. ((save-excursion
  6578. (goto-char (point-min))
  6579. (and
  6580. (re-search-forward
  6581. (concat "<<" (regexp-quote s0) ">>") nil t)
  6582. (setq type 'dedicated
  6583. pos (match-beginning 0))))
  6584. ;; There is an exact target for this
  6585. (goto-char pos))
  6586. ((and (string-match "^(\\(.*\\))$" s0)
  6587. (save-excursion
  6588. (goto-char (point-min))
  6589. (and
  6590. (re-search-forward
  6591. (concat "[^[]" (regexp-quote
  6592. (format org-coderef-label-format
  6593. (match-string 1 s0))))
  6594. nil t)
  6595. (setq type 'dedicated
  6596. pos (1+ (match-beginning 0))))))
  6597. ;; There is a coderef target for this
  6598. (goto-char pos))
  6599. ((string-match "^/\\(.*\\)/$" s)
  6600. ;; A regular expression
  6601. (cond
  6602. ((org-mode-p)
  6603. (org-occur (match-string 1 s)))
  6604. ;;((eq major-mode 'dired-mode)
  6605. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6606. (t (org-do-occur (match-string 1 s)))))
  6607. (t
  6608. ;; A normal search strings
  6609. (when (equal (string-to-char s) ?*)
  6610. ;; Anchor on headlines, post may include tags.
  6611. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6612. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6613. s (substring s 1)))
  6614. (remove-text-properties
  6615. 0 (length s)
  6616. '(face nil mouse-face nil keymap nil fontified nil) s)
  6617. ;; Make a series of regular expressions to find a match
  6618. (setq words (org-split-string s "[ \n\r\t]+")
  6619. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6620. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6621. "\\)" markers)
  6622. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6623. re2a (concat "[ \t\r\n]" re2a_)
  6624. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6625. re4 (concat "[^a-zA-Z_]" re4_)
  6626. re1 (concat pre re2 post)
  6627. re3 (concat pre (if pre re4_ re4) post)
  6628. re5 (concat pre ".*" re4)
  6629. re2 (concat pre re2)
  6630. re2a (concat pre (if pre re2a_ re2a))
  6631. re4 (concat pre (if pre re4_ re4))
  6632. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6633. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6634. re5 "\\)"
  6635. ))
  6636. (cond
  6637. ((eq type 'org-occur) (org-occur reall))
  6638. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6639. (t (goto-char (point-min))
  6640. (setq type 'fuzzy)
  6641. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6642. (org-search-not-self 1 re1 nil t)
  6643. (org-search-not-self 1 re2 nil t)
  6644. (org-search-not-self 1 re2a nil t)
  6645. (org-search-not-self 1 re3 nil t)
  6646. (org-search-not-self 1 re4 nil t)
  6647. (org-search-not-self 1 re5 nil t)
  6648. )
  6649. (goto-char (match-beginning 1))
  6650. (goto-char pos)
  6651. (error "No match")))))
  6652. (t
  6653. ;; Normal string-search
  6654. (goto-char (point-min))
  6655. (if (search-forward s nil t)
  6656. (goto-char (match-beginning 0))
  6657. (error "No match"))))
  6658. (and (org-mode-p) (org-show-context 'link-search))
  6659. type))
  6660. (defun org-search-not-self (group &rest args)
  6661. "Execute `re-search-forward', but only accept matches that do not
  6662. enclose the position of `org-open-link-marker'."
  6663. (let ((m org-open-link-marker))
  6664. (catch 'exit
  6665. (while (apply 're-search-forward args)
  6666. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6667. (goto-char (match-end group))
  6668. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6669. (> (match-beginning 0) (marker-position m))
  6670. (< (match-end 0) (marker-position m)))
  6671. (save-match-data
  6672. (or (not (org-in-regexp
  6673. org-bracket-link-analytic-regexp 1))
  6674. (not (match-end 4)) ; no description
  6675. (and (<= (match-beginning 4) (point))
  6676. (>= (match-end 4) (point))))))
  6677. (throw 'exit (point))))))))
  6678. (defun org-get-buffer-for-internal-link (buffer)
  6679. "Return a buffer to be used for displaying the link target of internal links."
  6680. (cond
  6681. ((not org-display-internal-link-with-indirect-buffer)
  6682. buffer)
  6683. ((string-match "(Clone)$" (buffer-name buffer))
  6684. (message "Buffer is already a clone, not making another one")
  6685. ;; we also do not modify visibility in this case
  6686. buffer)
  6687. (t ; make a new indirect buffer for displaying the link
  6688. (let* ((bn (buffer-name buffer))
  6689. (ibn (concat bn "(Clone)"))
  6690. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6691. (with-current-buffer ib (org-overview))
  6692. ib))))
  6693. (defun org-do-occur (regexp &optional cleanup)
  6694. "Call the Emacs command `occur'.
  6695. If CLEANUP is non-nil, remove the printout of the regular expression
  6696. in the *Occur* buffer. This is useful if the regex is long and not useful
  6697. to read."
  6698. (occur regexp)
  6699. (when cleanup
  6700. (let ((cwin (selected-window)) win beg end)
  6701. (when (setq win (get-buffer-window "*Occur*"))
  6702. (select-window win))
  6703. (goto-char (point-min))
  6704. (when (re-search-forward "match[a-z]+" nil t)
  6705. (setq beg (match-end 0))
  6706. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6707. (setq end (1- (match-beginning 0)))))
  6708. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6709. (goto-char (point-min))
  6710. (select-window cwin))))
  6711. ;;; The mark ring for links jumps
  6712. (defvar org-mark-ring nil
  6713. "Mark ring for positions before jumps in Org-mode.")
  6714. (defvar org-mark-ring-last-goto nil
  6715. "Last position in the mark ring used to go back.")
  6716. ;; Fill and close the ring
  6717. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6718. (loop for i from 1 to org-mark-ring-length do
  6719. (push (make-marker) org-mark-ring))
  6720. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6721. org-mark-ring)
  6722. (defun org-mark-ring-push (&optional pos buffer)
  6723. "Put the current position or POS into the mark ring and rotate it."
  6724. (interactive)
  6725. (setq pos (or pos (point)))
  6726. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6727. (move-marker (car org-mark-ring)
  6728. (or pos (point))
  6729. (or buffer (current-buffer)))
  6730. (message "%s"
  6731. (substitute-command-keys
  6732. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6733. (defun org-mark-ring-goto (&optional n)
  6734. "Jump to the previous position in the mark ring.
  6735. With prefix arg N, jump back that many stored positions. When
  6736. called several times in succession, walk through the entire ring.
  6737. Org-mode commands jumping to a different position in the current file,
  6738. or to another Org-mode file, automatically push the old position
  6739. onto the ring."
  6740. (interactive "p")
  6741. (let (p m)
  6742. (if (eq last-command this-command)
  6743. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6744. (setq p org-mark-ring))
  6745. (setq org-mark-ring-last-goto p)
  6746. (setq m (car p))
  6747. (switch-to-buffer (marker-buffer m))
  6748. (goto-char m)
  6749. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6750. (defun org-remove-angle-brackets (s)
  6751. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6752. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6753. s)
  6754. (defun org-add-angle-brackets (s)
  6755. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6756. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6757. s)
  6758. (defun org-remove-double-quotes (s)
  6759. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6760. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6761. s)
  6762. ;;; Following specific links
  6763. (defun org-follow-timestamp-link ()
  6764. (cond
  6765. ((org-at-date-range-p t)
  6766. (let ((org-agenda-start-on-weekday)
  6767. (t1 (match-string 1))
  6768. (t2 (match-string 2)))
  6769. (setq t1 (time-to-days (org-time-string-to-time t1))
  6770. t2 (time-to-days (org-time-string-to-time t2)))
  6771. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6772. ((org-at-timestamp-p t)
  6773. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6774. (substring (match-string 1) 0 10)))
  6775. 1))
  6776. (t (error "This should not happen"))))
  6777. ;;; Following file links
  6778. (defvar org-wait nil)
  6779. (defun org-open-file (path &optional in-emacs line search)
  6780. "Open the file at PATH.
  6781. First, this expands any special file name abbreviations. Then the
  6782. configuration variable `org-file-apps' is checked if it contains an
  6783. entry for this file type, and if yes, the corresponding command is launched.
  6784. If no application is found, Emacs simply visits the file.
  6785. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6786. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6787. and o use an external application to visit the file.
  6788. Optional LINE specifies a line to go to, optional SEARCH a string to
  6789. search for. If LINE or SEARCH is given, the file will always be
  6790. opened in Emacs.
  6791. If the file does not exist, an error is thrown."
  6792. (setq in-emacs (or in-emacs line search))
  6793. (let* ((file (if (equal path "")
  6794. buffer-file-name
  6795. (substitute-in-file-name (expand-file-name path))))
  6796. (apps (append org-file-apps (org-default-apps)))
  6797. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6798. (dirp (if remp nil (file-directory-p file)))
  6799. (file (if (and dirp org-open-directory-means-index-dot-org)
  6800. (concat (file-name-as-directory file) "index.org")
  6801. file))
  6802. (a-m-a-p (assq 'auto-mode apps))
  6803. (dfile (downcase file))
  6804. (old-buffer (current-buffer))
  6805. (old-pos (point))
  6806. (old-mode major-mode)
  6807. ext cmd)
  6808. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6809. (setq ext (match-string 1 dfile))
  6810. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6811. (setq ext (match-string 1 dfile))))
  6812. (cond
  6813. ((equal in-emacs '(16))
  6814. (setq cmd (cdr (assoc 'system apps))))
  6815. (in-emacs (setq cmd 'emacs))
  6816. (t
  6817. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6818. (and dirp (cdr (assoc 'directory apps)))
  6819. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6820. 'string-match)
  6821. (cdr (assoc ext apps))
  6822. (cdr (assoc t apps))))))
  6823. (when (eq cmd 'system)
  6824. (setq cmd (cdr (assoc 'system apps))))
  6825. (when (eq cmd 'default)
  6826. (setq cmd (cdr (assoc t apps))))
  6827. (when (eq cmd 'mailcap)
  6828. (require 'mailcap)
  6829. (mailcap-parse-mailcaps)
  6830. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6831. (command (mailcap-mime-info mime-type)))
  6832. (if (stringp command)
  6833. (setq cmd command)
  6834. (setq cmd 'emacs))))
  6835. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6836. (not (file-exists-p file))
  6837. (not org-open-non-existing-files))
  6838. (error "No such file: %s" file))
  6839. (cond
  6840. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6841. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6842. (while (string-match "['\"]%s['\"]" cmd)
  6843. (setq cmd (replace-match "%s" t t cmd)))
  6844. (while (string-match "%s" cmd)
  6845. (setq cmd (replace-match
  6846. (save-match-data
  6847. (shell-quote-argument
  6848. (convert-standard-filename file)))
  6849. t t cmd)))
  6850. (save-window-excursion
  6851. (start-process-shell-command cmd nil cmd)
  6852. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6853. ))
  6854. ((or (stringp cmd)
  6855. (eq cmd 'emacs))
  6856. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6857. (widen)
  6858. (if line (goto-line line)
  6859. (if search (org-link-search search))))
  6860. ((consp cmd)
  6861. (let ((file (convert-standard-filename file)))
  6862. (eval cmd)))
  6863. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6864. (and (org-mode-p) (eq old-mode 'org-mode)
  6865. (or (not (equal old-buffer (current-buffer)))
  6866. (not (equal old-pos (point))))
  6867. (org-mark-ring-push old-pos old-buffer))))
  6868. (defun org-default-apps ()
  6869. "Return the default applications for this operating system."
  6870. (cond
  6871. ((eq system-type 'darwin)
  6872. org-file-apps-defaults-macosx)
  6873. ((eq system-type 'windows-nt)
  6874. org-file-apps-defaults-windowsnt)
  6875. (t org-file-apps-defaults-gnu)))
  6876. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6877. "Convert extensions to regular expressions in the cars of LIST.
  6878. Also, weed out any non-string entries, because the return value is used
  6879. only for regexp matching.
  6880. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6881. point to the symbol `emacs', indicating that the file should
  6882. be opened in Emacs."
  6883. (append
  6884. (delq nil
  6885. (mapcar (lambda (x)
  6886. (if (not (stringp (car x)))
  6887. nil
  6888. (if (string-match "\\W" (car x))
  6889. x
  6890. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6891. list))
  6892. (if add-auto-mode
  6893. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6894. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6895. (defun org-file-remote-p (file)
  6896. "Test whether FILE specifies a location on a remote system.
  6897. Return non-nil if the location is indeed remote.
  6898. For example, the filename \"/user@host:/foo\" specifies a location
  6899. on the system \"/user@host:\"."
  6900. (cond ((fboundp 'file-remote-p)
  6901. (file-remote-p file))
  6902. ((fboundp 'tramp-handle-file-remote-p)
  6903. (tramp-handle-file-remote-p file))
  6904. ((and (boundp 'ange-ftp-name-format)
  6905. (string-match (car ange-ftp-name-format) file))
  6906. t)
  6907. (t nil)))
  6908. ;;;; Refiling
  6909. (defun org-get-org-file ()
  6910. "Read a filename, with default directory `org-directory'."
  6911. (let ((default (or org-default-notes-file remember-data-file)))
  6912. (read-file-name (format "File name [%s]: " default)
  6913. (file-name-as-directory org-directory)
  6914. default)))
  6915. (defun org-notes-order-reversed-p ()
  6916. "Check if the current file should receive notes in reversed order."
  6917. (cond
  6918. ((not org-reverse-note-order) nil)
  6919. ((eq t org-reverse-note-order) t)
  6920. ((not (listp org-reverse-note-order)) nil)
  6921. (t (catch 'exit
  6922. (let ((all org-reverse-note-order)
  6923. entry)
  6924. (while (setq entry (pop all))
  6925. (if (string-match (car entry) buffer-file-name)
  6926. (throw 'exit (cdr entry))))
  6927. nil)))))
  6928. (defvar org-refile-target-table nil
  6929. "The list of refile targets, created by `org-refile'.")
  6930. (defvar org-agenda-new-buffers nil
  6931. "Buffers created to visit agenda files.")
  6932. (defun org-get-refile-targets (&optional default-buffer)
  6933. "Produce a table with refile targets."
  6934. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6935. targets txt re files f desc descre fast-path-p level)
  6936. (message "Getting targets...")
  6937. (with-current-buffer (or default-buffer (current-buffer))
  6938. (while (setq entry (pop entries))
  6939. (setq files (car entry) desc (cdr entry))
  6940. (setq fast-path-p nil)
  6941. (cond
  6942. ((null files) (setq files (list (current-buffer))))
  6943. ((eq files 'org-agenda-files)
  6944. (setq files (org-agenda-files 'unrestricted)))
  6945. ((and (symbolp files) (fboundp files))
  6946. (setq files (funcall files)))
  6947. ((and (symbolp files) (boundp files))
  6948. (setq files (symbol-value files))))
  6949. (if (stringp files) (setq files (list files)))
  6950. (cond
  6951. ((eq (car desc) :tag)
  6952. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6953. ((eq (car desc) :todo)
  6954. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6955. ((eq (car desc) :regexp)
  6956. (setq descre (cdr desc)))
  6957. ((eq (car desc) :level)
  6958. (setq descre (concat "^\\*\\{" (number-to-string
  6959. (if org-odd-levels-only
  6960. (1- (* 2 (cdr desc)))
  6961. (cdr desc)))
  6962. "\\}[ \t]")))
  6963. ((eq (car desc) :maxlevel)
  6964. (setq fast-path-p t)
  6965. (setq descre (concat "^\\*\\{1," (number-to-string
  6966. (if org-odd-levels-only
  6967. (1- (* 2 (cdr desc)))
  6968. (cdr desc)))
  6969. "\\}[ \t]")))
  6970. (t (error "Bad refiling target description %s" desc)))
  6971. (while (setq f (pop files))
  6972. (save-excursion
  6973. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6974. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6975. (setq f (expand-file-name f))
  6976. (save-excursion
  6977. (save-restriction
  6978. (widen)
  6979. (goto-char (point-min))
  6980. (while (re-search-forward descre nil t)
  6981. (goto-char (point-at-bol))
  6982. (when (looking-at org-complex-heading-regexp)
  6983. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6984. txt (org-link-display-format (match-string 4))
  6985. re (concat "^" (regexp-quote
  6986. (buffer-substring (match-beginning 1)
  6987. (match-end 4)))))
  6988. (if (match-end 5) (setq re (concat re "[ \t]+"
  6989. (regexp-quote
  6990. (match-string 5)))))
  6991. (setq re (concat re "[ \t]*$"))
  6992. (when org-refile-use-outline-path
  6993. (setq txt (mapconcat 'org-protect-slash
  6994. (append
  6995. (if (eq org-refile-use-outline-path 'file)
  6996. (list (file-name-nondirectory
  6997. (buffer-file-name (buffer-base-buffer))))
  6998. (if (eq org-refile-use-outline-path 'full-file-path)
  6999. (list (buffer-file-name (buffer-base-buffer)))))
  7000. (org-get-outline-path fast-path-p level txt)
  7001. (list txt))
  7002. "/")))
  7003. (push (list txt f re (point)) targets))
  7004. (goto-char (point-at-eol))))))))
  7005. (message "Getting targets...done")
  7006. (nreverse targets))))
  7007. (defun org-protect-slash (s)
  7008. (while (string-match "/" s)
  7009. (setq s (replace-match "\\" t t s)))
  7010. s)
  7011. (defvar org-olpa (make-vector 20 nil))
  7012. (defun org-get-outline-path (&optional fastp level heading)
  7013. "Return the outline path to the current entry, as a list."
  7014. (if fastp
  7015. (progn
  7016. (if (> level 19)
  7017. (error "Outline path failure, more than 19 levels."))
  7018. (loop for i from level upto 19 do
  7019. (aset org-olpa i nil))
  7020. (prog1
  7021. (delq nil (append org-olpa nil))
  7022. (aset org-olpa level heading)))
  7023. (let (rtn)
  7024. (save-excursion
  7025. (while (org-up-heading-safe)
  7026. (when (looking-at org-complex-heading-regexp)
  7027. (push (org-match-string-no-properties 4) rtn)))
  7028. rtn))))
  7029. (defvar org-refile-history nil
  7030. "History for refiling operations.")
  7031. (defun org-refile (&optional goto default-buffer)
  7032. "Move the entry at point to another heading.
  7033. The list of target headings is compiled using the information in
  7034. `org-refile-targets', which see. This list is created before each use
  7035. and will therefore always be up-to-date.
  7036. At the target location, the entry is filed as a subitem of the target heading.
  7037. Depending on `org-reverse-note-order', the new subitem will either be the
  7038. first or the last subitem.
  7039. If there is an active region, all entries in that region will be moved.
  7040. However, the region must fulfil the requirement that the first heading
  7041. is the first one sets the top-level of the moved text - at most siblings
  7042. below it are allowed.
  7043. With prefix arg GOTO, the command will only visit the target location,
  7044. not actually move anything.
  7045. With a double prefix `C-u C-u', go to the location where the last refiling
  7046. operation has put the subtree."
  7047. (interactive "P")
  7048. (let* ((cbuf (current-buffer))
  7049. (regionp (org-region-active-p))
  7050. (region-start (and regionp (region-beginning)))
  7051. (region-end (and regionp (region-end)))
  7052. (region-length (and regionp (- region-end region-start)))
  7053. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7054. pos it nbuf file re level reversed)
  7055. (when regionp (goto-char region-start)
  7056. (unless (org-kill-is-subtree-p
  7057. (buffer-substring region-start region-end))
  7058. (error "The region is not a (sequence of) subtree(s)")))
  7059. (if (equal goto '(16))
  7060. (org-refile-goto-last-stored)
  7061. (when (setq it (org-refile-get-location
  7062. (if goto "Goto: " "Refile to: ") default-buffer))
  7063. (setq file (nth 1 it)
  7064. re (nth 2 it)
  7065. pos (nth 3 it))
  7066. (if (and (equal (buffer-file-name) file)
  7067. (if regionp
  7068. (and (>= pos region-start)
  7069. (<= pos region-end))
  7070. (and (>= pos (point))
  7071. (< pos (save-excursion
  7072. (org-end-of-subtree t t))))))
  7073. (error "Cannot refile to position inside the tree or region"))
  7074. (setq nbuf (or (find-buffer-visiting file)
  7075. (find-file-noselect file)))
  7076. (if goto
  7077. (progn
  7078. (switch-to-buffer nbuf)
  7079. (goto-char pos)
  7080. (org-show-context 'org-goto))
  7081. (if regionp
  7082. (progn
  7083. (kill-new (buffer-substring region-start region-end))
  7084. (org-save-markers-in-region region-start region-end))
  7085. (org-copy-subtree 1 nil t))
  7086. (save-excursion
  7087. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7088. (find-file-noselect file))))
  7089. (setq reversed (org-notes-order-reversed-p))
  7090. (save-excursion
  7091. (save-restriction
  7092. (widen)
  7093. (goto-char pos)
  7094. (looking-at outline-regexp)
  7095. (setq level (org-get-valid-level (funcall outline-level) 1))
  7096. (goto-char
  7097. (if reversed
  7098. (or (outline-next-heading) (point-max))
  7099. (or (save-excursion (outline-get-next-sibling))
  7100. (org-end-of-subtree t t)
  7101. (point-max))))
  7102. (if (not (bolp)) (newline))
  7103. (bookmark-set "org-refile-last-stored")
  7104. (org-paste-subtree level))))
  7105. (if regionp
  7106. (delete-region (point) (+ (point) region-length))
  7107. (org-cut-subtree))
  7108. (setq org-markers-to-move nil)
  7109. (message "Refiled to \"%s\"" (car it)))))))
  7110. (defun org-refile-goto-last-stored ()
  7111. "Go to the location where the last refile was stored."
  7112. (interactive)
  7113. (bookmark-jump "org-refile-last-stored")
  7114. (message "This is the location of the last refile"))
  7115. (defun org-refile-get-location (&optional prompt default-buffer)
  7116. "Prompt the user for a refile location, using PROMPT."
  7117. (let ((org-refile-targets org-refile-targets)
  7118. (org-refile-use-outline-path org-refile-use-outline-path))
  7119. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7120. (unless org-refile-target-table
  7121. (error "No refile targets"))
  7122. (let* ((cbuf (current-buffer))
  7123. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7124. (cfunc (if (and org-refile-use-outline-path
  7125. org-outline-path-complete-in-steps)
  7126. 'org-olpath-completing-read
  7127. 'org-ido-completing-read))
  7128. (extra (if org-refile-use-outline-path "/" ""))
  7129. (filename (and cfn (expand-file-name cfn)))
  7130. (tbl (mapcar
  7131. (lambda (x)
  7132. (if (not (equal filename (nth 1 x)))
  7133. (cons (concat (car x) extra " ("
  7134. (file-name-nondirectory (nth 1 x)) ")")
  7135. (cdr x))
  7136. (cons (concat (car x) extra) (cdr x))))
  7137. org-refile-target-table))
  7138. (completion-ignore-case t))
  7139. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7140. tbl)))
  7141. (defun org-olpath-completing-read (prompt collection &rest args)
  7142. "Read an outline path like a file name."
  7143. (let ((thetable collection))
  7144. (apply
  7145. 'org-ido-completing-read prompt
  7146. (lambda (string predicate &optional flag)
  7147. (let (rtn r f (l (length string)))
  7148. (cond
  7149. ((eq flag nil)
  7150. ;; try completion
  7151. (try-completion string thetable))
  7152. ((eq flag t)
  7153. ;; all-completions
  7154. (setq rtn (all-completions string thetable predicate))
  7155. (mapcar
  7156. (lambda (x)
  7157. (setq r (substring x l))
  7158. (if (string-match " ([^)]*)$" x)
  7159. (setq f (match-string 0 x))
  7160. (setq f ""))
  7161. (if (string-match "/" r)
  7162. (concat string (substring r 0 (match-end 0)) f)
  7163. x))
  7164. rtn))
  7165. ((eq flag 'lambda)
  7166. ;; exact match?
  7167. (assoc string thetable)))
  7168. ))
  7169. args)))
  7170. ;;;; Dynamic blocks
  7171. (defun org-find-dblock (name)
  7172. "Find the first dynamic block with name NAME in the buffer.
  7173. If not found, stay at current position and return nil."
  7174. (let (pos)
  7175. (save-excursion
  7176. (goto-char (point-min))
  7177. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7178. nil t)
  7179. (match-beginning 0))))
  7180. (if pos (goto-char pos))
  7181. pos))
  7182. (defconst org-dblock-start-re
  7183. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7184. "Matches the startline of a dynamic block, with parameters.")
  7185. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7186. "Matches the end of a dynamic block.")
  7187. (defun org-create-dblock (plist)
  7188. "Create a dynamic block section, with parameters taken from PLIST.
  7189. PLIST must contain a :name entry which is used as name of the block."
  7190. (unless (bolp) (newline))
  7191. (let ((name (plist-get plist :name)))
  7192. (insert "#+BEGIN: " name)
  7193. (while plist
  7194. (if (eq (car plist) :name)
  7195. (setq plist (cddr plist))
  7196. (insert " " (prin1-to-string (pop plist)))))
  7197. (insert "\n\n#+END:\n")
  7198. (beginning-of-line -2)))
  7199. (defun org-prepare-dblock ()
  7200. "Prepare dynamic block for refresh.
  7201. This empties the block, puts the cursor at the insert position and returns
  7202. the property list including an extra property :name with the block name."
  7203. (unless (looking-at org-dblock-start-re)
  7204. (error "Not at a dynamic block"))
  7205. (let* ((begdel (1+ (match-end 0)))
  7206. (name (org-no-properties (match-string 1)))
  7207. (params (append (list :name name)
  7208. (read (concat "(" (match-string 3) ")")))))
  7209. (unless (re-search-forward org-dblock-end-re nil t)
  7210. (error "Dynamic block not terminated"))
  7211. (setq params
  7212. (append params
  7213. (list :content (buffer-substring
  7214. begdel (match-beginning 0)))))
  7215. (delete-region begdel (match-beginning 0))
  7216. (goto-char begdel)
  7217. (open-line 1)
  7218. params))
  7219. (defun org-map-dblocks (&optional command)
  7220. "Apply COMMAND to all dynamic blocks in the current buffer.
  7221. If COMMAND is not given, use `org-update-dblock'."
  7222. (let ((cmd (or command 'org-update-dblock))
  7223. pos)
  7224. (save-excursion
  7225. (goto-char (point-min))
  7226. (while (re-search-forward org-dblock-start-re nil t)
  7227. (goto-char (setq pos (match-beginning 0)))
  7228. (condition-case nil
  7229. (funcall cmd)
  7230. (error (message "Error during update of dynamic block")))
  7231. (goto-char pos)
  7232. (unless (re-search-forward org-dblock-end-re nil t)
  7233. (error "Dynamic block not terminated"))))))
  7234. (defun org-dblock-update (&optional arg)
  7235. "User command for updating dynamic blocks.
  7236. Update the dynamic block at point. With prefix ARG, update all dynamic
  7237. blocks in the buffer."
  7238. (interactive "P")
  7239. (if arg
  7240. (org-update-all-dblocks)
  7241. (or (looking-at org-dblock-start-re)
  7242. (org-beginning-of-dblock))
  7243. (org-update-dblock)))
  7244. (defun org-update-dblock ()
  7245. "Update the dynamic block at point
  7246. This means to empty the block, parse for parameters and then call
  7247. the correct writing function."
  7248. (save-window-excursion
  7249. (let* ((pos (point))
  7250. (line (org-current-line))
  7251. (params (org-prepare-dblock))
  7252. (name (plist-get params :name))
  7253. (cmd (intern (concat "org-dblock-write:" name))))
  7254. (message "Updating dynamic block `%s' at line %d..." name line)
  7255. (funcall cmd params)
  7256. (message "Updating dynamic block `%s' at line %d...done" name line)
  7257. (goto-char pos))))
  7258. (defun org-beginning-of-dblock ()
  7259. "Find the beginning of the dynamic block at point.
  7260. Error if there is no such block at point."
  7261. (let ((pos (point))
  7262. beg)
  7263. (end-of-line 1)
  7264. (if (and (re-search-backward org-dblock-start-re nil t)
  7265. (setq beg (match-beginning 0))
  7266. (re-search-forward org-dblock-end-re nil t)
  7267. (> (match-end 0) pos))
  7268. (goto-char beg)
  7269. (goto-char pos)
  7270. (error "Not in a dynamic block"))))
  7271. (defun org-update-all-dblocks ()
  7272. "Update all dynamic blocks in the buffer.
  7273. This function can be used in a hook."
  7274. (when (org-mode-p)
  7275. (org-map-dblocks 'org-update-dblock)))
  7276. ;;;; Completion
  7277. (defconst org-additional-option-like-keywords
  7278. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7279. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7280. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7281. "BEGIN_QUOTE" "END_QUOTE"
  7282. "BEGIN_VERSE" "END_VERSE"
  7283. "BEGIN_SRC" "END_SRC"
  7284. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7285. (defcustom org-structure-template-alist
  7286. '(
  7287. ("s" "#+begin_src ?\n\n#+end_src"
  7288. "<src lang=\"?\">\n\n</src>")
  7289. ("e" "#+begin_example\n?\n#+end_example"
  7290. "<example>\n?\n</example>")
  7291. ("q" "#+begin_quote\n?\n#+end_quote"
  7292. "<quote>\n?\n</quote>")
  7293. ("v" "#+begin_verse\n?\n#+end_verse"
  7294. "<verse>\n?\n/verse>")
  7295. ("l" "#+begin_latex\n?\n#+end_latex"
  7296. "<literal style=\"latex\">\n?\n</literal>")
  7297. ("L" "#+latex: "
  7298. "<literal style=\"latex\">?</literal>")
  7299. ("h" "#+begin_html\n?\n#+end_html"
  7300. "<literal style=\"html\">\n?\n</literal>")
  7301. ("H" "#+html: "
  7302. "<literal style=\"html\">?</literal>")
  7303. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7304. ("A" "#+ascii: ")
  7305. ("i" "#+include %file ?"
  7306. "<include file=%file markup=\"?\">")
  7307. )
  7308. "Structure completion elements.
  7309. This is a list of abbreviation keys and values. The value gets inserted
  7310. it you type @samp{.} followed by the key and then the completion key,
  7311. usually `M-TAB'. %file will be replaced by a file name after prompting
  7312. for the file using completion.
  7313. There are two templates for each key, the first uses the original Org syntax,
  7314. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7315. the default when the /org-mtags.el/ module has been loaded. See also the
  7316. variable `org-mtags-prefer-muse-templates'.
  7317. This is an experimental feature, it is undecided if it is going to stay in."
  7318. :group 'org-completion
  7319. :type '(repeat
  7320. (string :tag "Key")
  7321. (string :tag "Template")
  7322. (string :tag "Muse Template")))
  7323. (defun org-try-structure-completion ()
  7324. "Try to complete a structure template before point.
  7325. This looks for strings like \"<e\" on an otherwise empty line and
  7326. expands them."
  7327. (let ((l (buffer-substring (point-at-bol) (point)))
  7328. a)
  7329. (when (and (looking-at "[ \t]*$")
  7330. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7331. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7332. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7333. (match-beginning 1)) a)
  7334. t)))
  7335. (defun org-complete-expand-structure-template (start cell)
  7336. "Expand a structure template."
  7337. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7338. (rpl (nth (if musep 2 1) cell)))
  7339. (delete-region start (point))
  7340. (when (string-match "\\`#\\+" rpl)
  7341. (cond
  7342. ((bolp))
  7343. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7344. (delete-region (point-at-bol) (point)))
  7345. (t (newline))))
  7346. (setq start (point))
  7347. (if (string-match "%file" rpl)
  7348. (setq rpl (replace-match
  7349. (concat
  7350. "\""
  7351. (save-match-data
  7352. (abbreviate-file-name (read-file-name "Include file: ")))
  7353. "\"")
  7354. t t rpl)))
  7355. (insert rpl)
  7356. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7357. (defun org-complete (&optional arg)
  7358. "Perform completion on word at point.
  7359. At the beginning of a headline, this completes TODO keywords as given in
  7360. `org-todo-keywords'.
  7361. If the current word is preceded by a backslash, completes the TeX symbols
  7362. that are supported for HTML support.
  7363. If the current word is preceded by \"#+\", completes special words for
  7364. setting file options.
  7365. In the line after \"#+STARTUP:, complete valid keywords.\"
  7366. At all other locations, this simply calls the value of
  7367. `org-completion-fallback-command'."
  7368. (interactive "P")
  7369. (org-without-partial-completion
  7370. (catch 'exit
  7371. (let* ((a nil)
  7372. (end (point))
  7373. (beg1 (save-excursion
  7374. (skip-chars-backward (org-re "[:alnum:]_@"))
  7375. (point)))
  7376. (beg (save-excursion
  7377. (skip-chars-backward "a-zA-Z0-9_:$")
  7378. (point)))
  7379. (confirm (lambda (x) (stringp (car x))))
  7380. (searchhead (equal (char-before beg) ?*))
  7381. (struct
  7382. (when (and (member (char-before beg1) '(?. ?<))
  7383. (setq a (assoc (buffer-substring beg1 (point))
  7384. org-structure-template-alist)))
  7385. (org-complete-expand-structure-template (1- beg1) a)
  7386. (throw 'exit t)))
  7387. (tag (and (equal (char-before beg1) ?:)
  7388. (equal (char-after (point-at-bol)) ?*)))
  7389. (prop (and (equal (char-before beg1) ?:)
  7390. (not (equal (char-after (point-at-bol)) ?*))))
  7391. (texp (equal (char-before beg) ?\\))
  7392. (link (equal (char-before beg) ?\[))
  7393. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7394. beg)
  7395. "#+"))
  7396. (startup (string-match "^#\\+STARTUP:.*"
  7397. (buffer-substring (point-at-bol) (point))))
  7398. (completion-ignore-case opt)
  7399. (type nil)
  7400. (tbl nil)
  7401. (table (cond
  7402. (opt
  7403. (setq type :opt)
  7404. (require 'org-exp)
  7405. (append
  7406. (mapcar
  7407. (lambda (x)
  7408. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7409. (cons (match-string 2 x) (match-string 1 x)))
  7410. (org-split-string (org-get-current-options) "\n"))
  7411. (mapcar 'list org-additional-option-like-keywords)))
  7412. (startup
  7413. (setq type :startup)
  7414. org-startup-options)
  7415. (link (append org-link-abbrev-alist-local
  7416. org-link-abbrev-alist))
  7417. (texp
  7418. (setq type :tex)
  7419. org-html-entities)
  7420. ((string-match "\\`\\*+[ \t]+\\'"
  7421. (buffer-substring (point-at-bol) beg))
  7422. (setq type :todo)
  7423. (mapcar 'list org-todo-keywords-1))
  7424. (searchhead
  7425. (setq type :searchhead)
  7426. (save-excursion
  7427. (goto-char (point-min))
  7428. (while (re-search-forward org-todo-line-regexp nil t)
  7429. (push (list
  7430. (org-make-org-heading-search-string
  7431. (match-string 3) t))
  7432. tbl)))
  7433. tbl)
  7434. (tag (setq type :tag beg beg1)
  7435. (or org-tag-alist (org-get-buffer-tags)))
  7436. (prop (setq type :prop beg beg1)
  7437. (mapcar 'list (org-buffer-property-keys nil t t)))
  7438. (t (progn
  7439. (call-interactively org-completion-fallback-command)
  7440. (throw 'exit nil)))))
  7441. (pattern (buffer-substring-no-properties beg end))
  7442. (completion (try-completion pattern table confirm)))
  7443. (cond ((eq completion t)
  7444. (if (not (assoc (upcase pattern) table))
  7445. (message "Already complete")
  7446. (if (and (equal type :opt)
  7447. (not (member (car (assoc (upcase pattern) table))
  7448. org-additional-option-like-keywords)))
  7449. (insert (substring (cdr (assoc (upcase pattern) table))
  7450. (length pattern)))
  7451. (if (memq type '(:tag :prop)) (insert ":")))))
  7452. ((null completion)
  7453. (message "Can't find completion for \"%s\"" pattern)
  7454. (ding))
  7455. ((not (string= pattern completion))
  7456. (delete-region beg end)
  7457. (if (string-match " +$" completion)
  7458. (setq completion (replace-match "" t t completion)))
  7459. (insert completion)
  7460. (if (get-buffer-window "*Completions*")
  7461. (delete-window (get-buffer-window "*Completions*")))
  7462. (if (assoc completion table)
  7463. (if (eq type :todo) (insert " ")
  7464. (if (memq type '(:tag :prop)) (insert ":"))))
  7465. (if (and (equal type :opt) (assoc completion table))
  7466. (message "%s" (substitute-command-keys
  7467. "Press \\[org-complete] again to insert example settings"))))
  7468. (t
  7469. (message "Making completion list...")
  7470. (let ((list (sort (all-completions pattern table confirm)
  7471. 'string<)))
  7472. (with-output-to-temp-buffer "*Completions*"
  7473. (condition-case nil
  7474. ;; Protection needed for XEmacs and emacs 21
  7475. (display-completion-list list pattern)
  7476. (error (display-completion-list list)))))
  7477. (message "Making completion list...%s" "done")))))))
  7478. ;;;; TODO, DEADLINE, Comments
  7479. (defun org-toggle-comment ()
  7480. "Change the COMMENT state of an entry."
  7481. (interactive)
  7482. (save-excursion
  7483. (org-back-to-heading)
  7484. (let (case-fold-search)
  7485. (if (looking-at (concat outline-regexp
  7486. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7487. (replace-match "" t t nil 1)
  7488. (if (looking-at outline-regexp)
  7489. (progn
  7490. (goto-char (match-end 0))
  7491. (insert org-comment-string " ")))))))
  7492. (defvar org-last-todo-state-is-todo nil
  7493. "This is non-nil when the last TODO state change led to a TODO state.
  7494. If the last change removed the TODO tag or switched to DONE, then
  7495. this is nil.")
  7496. (defvar org-setting-tags nil) ; dynamically skipped
  7497. (defun org-parse-local-options (string var)
  7498. "Parse STRING for startup setting relevant for variable VAR."
  7499. (let ((rtn (symbol-value var))
  7500. e opts)
  7501. (save-match-data
  7502. (if (or (not string) (not (string-match "\\S-" string)))
  7503. rtn
  7504. (setq opts (delq nil (mapcar (lambda (x)
  7505. (setq e (assoc x org-startup-options))
  7506. (if (eq (nth 1 e) var) e nil))
  7507. (org-split-string string "[ \t]+"))))
  7508. (if (not opts)
  7509. rtn
  7510. (setq rtn nil)
  7511. (while (setq e (pop opts))
  7512. (if (not (nth 3 e))
  7513. (setq rtn (nth 2 e))
  7514. (if (not (listp rtn)) (setq rtn nil))
  7515. (push (nth 2 e) rtn)))
  7516. rtn)))))
  7517. (defvar org-agenda-headline-snapshot-before-repeat)
  7518. (defun org-todo (&optional arg)
  7519. "Change the TODO state of an item.
  7520. The state of an item is given by a keyword at the start of the heading,
  7521. like
  7522. *** TODO Write paper
  7523. *** DONE Call mom
  7524. The different keywords are specified in the variable `org-todo-keywords'.
  7525. By default the available states are \"TODO\" and \"DONE\".
  7526. So for this example: when the item starts with TODO, it is changed to DONE.
  7527. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7528. DONE are present, add TODO at the beginning of the heading.
  7529. With C-u prefix arg, use completion to determine the new state.
  7530. With numeric prefix arg, switch to that state.
  7531. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7532. For calling through lisp, arg is also interpreted in the following way:
  7533. 'none -> empty state
  7534. \"\"(empty string) -> switch to empty state
  7535. 'done -> switch to DONE
  7536. 'nextset -> switch to the next set of keywords
  7537. 'previousset -> switch to the previous set of keywords
  7538. \"WAITING\" -> switch to the specified keyword, but only if it
  7539. really is a member of `org-todo-keywords'."
  7540. (interactive "P")
  7541. (if (equal arg '(16)) (setq arg 'nextset))
  7542. (save-excursion
  7543. (catch 'exit
  7544. (org-back-to-heading)
  7545. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7546. (or (looking-at (concat " +" org-todo-regexp " *"))
  7547. (looking-at " *"))
  7548. (let* ((match-data (match-data))
  7549. (startpos (point-at-bol))
  7550. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7551. (org-log-done org-log-done)
  7552. (org-log-repeat org-log-repeat)
  7553. (org-todo-log-states org-todo-log-states)
  7554. (this (match-string 1))
  7555. (hl-pos (match-beginning 0))
  7556. (head (org-get-todo-sequence-head this))
  7557. (ass (assoc head org-todo-kwd-alist))
  7558. (interpret (nth 1 ass))
  7559. (done-word (nth 3 ass))
  7560. (final-done-word (nth 4 ass))
  7561. (last-state (or this ""))
  7562. (completion-ignore-case t)
  7563. (member (member this org-todo-keywords-1))
  7564. (tail (cdr member))
  7565. (state (cond
  7566. ((and org-todo-key-trigger
  7567. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7568. (and (not arg) org-use-fast-todo-selection
  7569. (not (eq org-use-fast-todo-selection 'prefix)))))
  7570. ;; Use fast selection
  7571. (org-fast-todo-selection))
  7572. ((and (equal arg '(4))
  7573. (or (not org-use-fast-todo-selection)
  7574. (not org-todo-key-trigger)))
  7575. ;; Read a state with completion
  7576. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7577. org-todo-keywords-1)
  7578. nil t))
  7579. ((eq arg 'right)
  7580. (if this
  7581. (if tail (car tail) nil)
  7582. (car org-todo-keywords-1)))
  7583. ((eq arg 'left)
  7584. (if (equal member org-todo-keywords-1)
  7585. nil
  7586. (if this
  7587. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7588. org-todo-keywords-1)
  7589. (org-last org-todo-keywords-1))))
  7590. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7591. (setq arg nil))) ; hack to fall back to cycling
  7592. (arg
  7593. ;; user or caller requests a specific state
  7594. (cond
  7595. ((equal arg "") nil)
  7596. ((eq arg 'none) nil)
  7597. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7598. ((eq arg 'nextset)
  7599. (or (car (cdr (member head org-todo-heads)))
  7600. (car org-todo-heads)))
  7601. ((eq arg 'previousset)
  7602. (let ((org-todo-heads (reverse org-todo-heads)))
  7603. (or (car (cdr (member head org-todo-heads)))
  7604. (car org-todo-heads))))
  7605. ((car (member arg org-todo-keywords-1)))
  7606. ((nth (1- (prefix-numeric-value arg))
  7607. org-todo-keywords-1))))
  7608. ((null member) (or head (car org-todo-keywords-1)))
  7609. ((equal this final-done-word) nil) ;; -> make empty
  7610. ((null tail) nil) ;; -> first entry
  7611. ((eq interpret 'sequence)
  7612. (car tail))
  7613. ((memq interpret '(type priority))
  7614. (if (eq this-command last-command)
  7615. (car tail)
  7616. (if (> (length tail) 0)
  7617. (or done-word (car org-done-keywords))
  7618. nil)))
  7619. (t nil)))
  7620. (next (if state (concat " " state " ") " "))
  7621. (change-plist (list :type 'todo-state-change :from this :to state
  7622. :position startpos))
  7623. dolog now-done-p)
  7624. (when org-blocker-hook
  7625. (setq org-last-todo-state-is-todo
  7626. (not (member this org-done-keywords)))
  7627. (unless (save-excursion
  7628. (save-match-data
  7629. (run-hook-with-args-until-failure
  7630. 'org-blocker-hook change-plist)))
  7631. (if (interactive-p)
  7632. (error "TODO state change from %s to %s blocked" this state)
  7633. ;; fail silently
  7634. (message "TODO state change from %s to %s blocked" this state)
  7635. (throw 'exit nil))))
  7636. (store-match-data match-data)
  7637. (replace-match next t t)
  7638. (unless (pos-visible-in-window-p hl-pos)
  7639. (message "TODO state changed to %s" (org-trim next)))
  7640. (unless head
  7641. (setq head (org-get-todo-sequence-head state)
  7642. ass (assoc head org-todo-kwd-alist)
  7643. interpret (nth 1 ass)
  7644. done-word (nth 3 ass)
  7645. final-done-word (nth 4 ass)))
  7646. (when (memq arg '(nextset previousset))
  7647. (message "Keyword-Set %d/%d: %s"
  7648. (- (length org-todo-sets) -1
  7649. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7650. (length org-todo-sets)
  7651. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7652. (setq org-last-todo-state-is-todo
  7653. (not (member state org-done-keywords)))
  7654. (setq now-done-p (and (member state org-done-keywords)
  7655. (not (member this org-done-keywords))))
  7656. (and logging (org-local-logging logging))
  7657. (when (and (or org-todo-log-states org-log-done)
  7658. (not (memq arg '(nextset previousset))))
  7659. ;; we need to look at recording a time and note
  7660. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7661. (nth 2 (assoc this org-todo-log-states))))
  7662. (when (and state
  7663. (member state org-not-done-keywords)
  7664. (not (member this org-not-done-keywords)))
  7665. ;; This is now a todo state and was not one before
  7666. ;; If there was a CLOSED time stamp, get rid of it.
  7667. (org-add-planning-info nil nil 'closed))
  7668. (when (and now-done-p org-log-done)
  7669. ;; It is now done, and it was not done before
  7670. (org-add-planning-info 'closed (org-current-time))
  7671. (if (and (not dolog) (eq 'note org-log-done))
  7672. (org-add-log-setup 'done state 'findpos 'note)))
  7673. (when (and state dolog)
  7674. ;; This is a non-nil state, and we need to log it
  7675. (org-add-log-setup 'state state 'findpos dolog)))
  7676. ;; Fixup tag positioning
  7677. (org-todo-trigger-tag-changes state)
  7678. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7679. (when org-provide-todo-statistics
  7680. (org-update-parent-todo-statistics))
  7681. (run-hooks 'org-after-todo-state-change-hook)
  7682. (if (and arg (not (member state org-done-keywords)))
  7683. (setq head (org-get-todo-sequence-head state)))
  7684. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7685. ;; Do we need to trigger a repeat?
  7686. (when now-done-p
  7687. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7688. ;; This is for the agenda, take a snapshot of the headline.
  7689. (save-match-data
  7690. (setq org-agenda-headline-snapshot-before-repeat
  7691. (org-get-heading))))
  7692. (org-auto-repeat-maybe state))
  7693. ;; Fixup cursor location if close to the keyword
  7694. (if (and (outline-on-heading-p)
  7695. (not (bolp))
  7696. (save-excursion (beginning-of-line 1)
  7697. (looking-at org-todo-line-regexp))
  7698. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7699. (progn
  7700. (goto-char (or (match-end 2) (match-end 1)))
  7701. (just-one-space)))
  7702. (when org-trigger-hook
  7703. (save-excursion
  7704. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7705. (defun org-block-todo-from-children-or-siblings (change-plist)
  7706. "Block turning an entry into a TODO, using the hierarchy.
  7707. This checks whether the current task should be blocked from state
  7708. changes. Such blocking occurs when:
  7709. 1. The task has children which are not all in a completed state.
  7710. 2. A task has a parent with the property :ORDERED:, and there
  7711. are siblings prior to the current task with incomplete
  7712. status."
  7713. (catch 'dont-block
  7714. ;; If this is not a todo state change, or if this entry is already DONE,
  7715. ;; do not block
  7716. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7717. (member (plist-get change-plist :from)
  7718. (cons 'done org-done-keywords)))
  7719. (throw 'dont-block t))
  7720. ;; If this task has children, and any are undone, it's blocked
  7721. (save-excursion
  7722. (org-back-to-heading t)
  7723. (let ((this-level (funcall outline-level)))
  7724. (outline-next-heading)
  7725. (let ((child-level (funcall outline-level)))
  7726. (while (and (not (eobp))
  7727. (> child-level this-level))
  7728. ;; this todo has children, check whether they are all
  7729. ;; completed
  7730. (if (and (not (org-entry-is-done-p))
  7731. (org-entry-is-todo-p))
  7732. (throw 'dont-block nil))
  7733. (outline-next-heading)
  7734. (setq child-level (funcall outline-level))))))
  7735. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7736. ;; any previous siblings are undone, it's blocked
  7737. (save-excursion
  7738. (org-back-to-heading t)
  7739. (when (save-excursion
  7740. (ignore-errors
  7741. (outline-up-heading 1)
  7742. (org-entry-get (point) "ORDERED")))
  7743. (let* ((this-level (funcall outline-level))
  7744. (current-level this-level))
  7745. (while (and (not (bobp))
  7746. (= current-level this-level))
  7747. (outline-previous-heading)
  7748. (setq current-level (funcall outline-level))
  7749. (if (= current-level this-level)
  7750. ;; this todo has children, check whether they are all
  7751. ;; completed
  7752. (if (and (not (org-entry-is-done-p))
  7753. (org-entry-is-todo-p))
  7754. (throw 'dont-block nil)))))))
  7755. t)) ; don't block
  7756. (defun org-toggle-ordered-property ()
  7757. "Toggle the ORDERED property of the current entry."
  7758. (interactive)
  7759. (save-excursion
  7760. (org-back-to-heading)
  7761. (if (org-entry-get nil "ORDERED")
  7762. (progn
  7763. (org-delete-property "ORDERED")
  7764. (message "Subtasks can be completed in arbitrary order or parallel"))
  7765. (org-entry-put nil "ORDERED" "t")
  7766. (message "Subtasks must be completed in sequence"))))
  7767. (defun org-update-parent-todo-statistics ()
  7768. "Update any statistics cookie in the parent of the current headline."
  7769. (interactive)
  7770. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7771. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7772. (catch 'exit
  7773. (save-excursion
  7774. (setq level (org-up-heading-safe))
  7775. (unless level
  7776. (throw 'exit nil))
  7777. (while (re-search-forward box-re (point-at-eol) t)
  7778. (setq is-percent (match-end 2))
  7779. (save-match-data
  7780. (unless (outline-next-heading) (throw 'exit nil))
  7781. (while (looking-at org-todo-line-regexp)
  7782. (setq kwd (match-string 2))
  7783. (and kwd (setq cnt-all (1+ cnt-all)))
  7784. (and (member kwd org-done-keywords)
  7785. (setq cnt-done (1+ cnt-done)))
  7786. (condition-case nil
  7787. (org-forward-same-level 1)
  7788. (error (end-of-line 1)))))
  7789. (replace-match
  7790. (if is-percent
  7791. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7792. (format "[%d/%d]" cnt-done cnt-all))))
  7793. (run-hook-with-args 'org-after-todo-statistics-hook
  7794. cnt-done (- cnt-all cnt-done))))))
  7795. (defvar org-after-todo-statistics-hook nil
  7796. "Hook that is called after a TODO statistics cookie has been updated.
  7797. Each function is called with two arguments: the number of not-done entries
  7798. and the number of done entries.
  7799. For example, the following function, when added to this hook, will switch
  7800. an entry to DONE when all children are done, and back to TODO when new
  7801. entries are set to a TODO status. Note that this hook is only called
  7802. when there is a statistics cookie in the headline!
  7803. (defun org-summary-todo (n-done n-not-done)
  7804. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7805. (let (org-log-done org-log-states) ; turn off logging
  7806. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7807. ")
  7808. (defun org-todo-trigger-tag-changes (state)
  7809. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7810. (let ((l org-todo-state-tags-triggers)
  7811. changes)
  7812. (when (or (not state) (equal state ""))
  7813. (setq changes (append changes (cdr (assoc "" l)))))
  7814. (when (and (stringp state) (> (length state) 0))
  7815. (setq changes (append changes (cdr (assoc state l)))))
  7816. (when (member state org-not-done-keywords)
  7817. (setq changes (append changes (cdr (assoc 'todo l)))))
  7818. (when (member state org-done-keywords)
  7819. (setq changes (append changes (cdr (assoc 'done l)))))
  7820. (dolist (c changes)
  7821. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7822. (defun org-local-logging (value)
  7823. "Get logging settings from a property VALUE."
  7824. (let* (words w a)
  7825. ;; directly set the variables, they are already local.
  7826. (setq org-log-done nil
  7827. org-log-repeat nil
  7828. org-todo-log-states nil)
  7829. (setq words (org-split-string value))
  7830. (while (setq w (pop words))
  7831. (cond
  7832. ((setq a (assoc w org-startup-options))
  7833. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7834. (set (nth 1 a) (nth 2 a))))
  7835. ((setq a (org-extract-log-state-settings w))
  7836. (and (member (car a) org-todo-keywords-1)
  7837. (push a org-todo-log-states)))))))
  7838. (defun org-get-todo-sequence-head (kwd)
  7839. "Return the head of the TODO sequence to which KWD belongs.
  7840. If KWD is not set, check if there is a text property remembering the
  7841. right sequence."
  7842. (let (p)
  7843. (cond
  7844. ((not kwd)
  7845. (or (get-text-property (point-at-bol) 'org-todo-head)
  7846. (progn
  7847. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7848. nil (point-at-eol)))
  7849. (get-text-property p 'org-todo-head))))
  7850. ((not (member kwd org-todo-keywords-1))
  7851. (car org-todo-keywords-1))
  7852. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7853. (defun org-fast-todo-selection ()
  7854. "Fast TODO keyword selection with single keys.
  7855. Returns the new TODO keyword, or nil if no state change should occur."
  7856. (let* ((fulltable org-todo-key-alist)
  7857. (done-keywords org-done-keywords) ;; needed for the faces.
  7858. (maxlen (apply 'max (mapcar
  7859. (lambda (x)
  7860. (if (stringp (car x)) (string-width (car x)) 0))
  7861. fulltable)))
  7862. (expert nil)
  7863. (fwidth (+ maxlen 3 1 3))
  7864. (ncol (/ (- (window-width) 4) fwidth))
  7865. tg cnt e c tbl
  7866. groups ingroup)
  7867. (save-excursion
  7868. (save-window-excursion
  7869. (if expert
  7870. (set-buffer (get-buffer-create " *Org todo*"))
  7871. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7872. (erase-buffer)
  7873. (org-set-local 'org-done-keywords done-keywords)
  7874. (setq tbl fulltable cnt 0)
  7875. (while (setq e (pop tbl))
  7876. (cond
  7877. ((equal e '(:startgroup))
  7878. (push '() groups) (setq ingroup t)
  7879. (when (not (= cnt 0))
  7880. (setq cnt 0)
  7881. (insert "\n"))
  7882. (insert "{ "))
  7883. ((equal e '(:endgroup))
  7884. (setq ingroup nil cnt 0)
  7885. (insert "}\n"))
  7886. (t
  7887. (setq tg (car e) c (cdr e))
  7888. (if ingroup (push tg (car groups)))
  7889. (setq tg (org-add-props tg nil 'face
  7890. (org-get-todo-face tg)))
  7891. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7892. (insert "[" c "] " tg (make-string
  7893. (- fwidth 4 (length tg)) ?\ ))
  7894. (when (= (setq cnt (1+ cnt)) ncol)
  7895. (insert "\n")
  7896. (if ingroup (insert " "))
  7897. (setq cnt 0)))))
  7898. (insert "\n")
  7899. (goto-char (point-min))
  7900. (if (not expert) (org-fit-window-to-buffer))
  7901. (message "[a-z..]:Set [SPC]:clear")
  7902. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7903. (cond
  7904. ((or (= c ?\C-g)
  7905. (and (= c ?q) (not (rassoc c fulltable))))
  7906. (setq quit-flag t))
  7907. ((= c ?\ ) nil)
  7908. ((setq e (rassoc c fulltable) tg (car e))
  7909. tg)
  7910. (t (setq quit-flag t)))))))
  7911. (defun org-entry-is-todo-p ()
  7912. (member (org-get-todo-state) org-not-done-keywords))
  7913. (defun org-entry-is-done-p ()
  7914. (member (org-get-todo-state) org-done-keywords))
  7915. (defun org-get-todo-state ()
  7916. (save-excursion
  7917. (org-back-to-heading t)
  7918. (and (looking-at org-todo-line-regexp)
  7919. (match-end 2)
  7920. (match-string 2))))
  7921. (defun org-at-date-range-p (&optional inactive-ok)
  7922. "Is the cursor inside a date range?"
  7923. (interactive)
  7924. (save-excursion
  7925. (catch 'exit
  7926. (let ((pos (point)))
  7927. (skip-chars-backward "^[<\r\n")
  7928. (skip-chars-backward "<[")
  7929. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7930. (>= (match-end 0) pos)
  7931. (throw 'exit t))
  7932. (skip-chars-backward "^<[\r\n")
  7933. (skip-chars-backward "<[")
  7934. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7935. (>= (match-end 0) pos)
  7936. (throw 'exit t)))
  7937. nil)))
  7938. (defun org-get-repeat ()
  7939. "Check if there is a deadline/schedule with repeater in this entry."
  7940. (save-match-data
  7941. (save-excursion
  7942. (org-back-to-heading t)
  7943. (if (re-search-forward
  7944. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7945. (match-string 1)))))
  7946. (defvar org-last-changed-timestamp)
  7947. (defvar org-last-inserted-timestamp)
  7948. (defvar org-log-post-message)
  7949. (defvar org-log-note-purpose)
  7950. (defvar org-log-note-how)
  7951. (defvar org-log-note-extra)
  7952. (defun org-auto-repeat-maybe (done-word)
  7953. "Check if the current headline contains a repeated deadline/schedule.
  7954. If yes, set TODO state back to what it was and change the base date
  7955. of repeating deadline/scheduled time stamps to new date.
  7956. This function is run automatically after each state change to a DONE state."
  7957. ;; last-state is dynamically scoped into this function
  7958. (let* ((repeat (org-get-repeat))
  7959. (aa (assoc last-state org-todo-kwd-alist))
  7960. (interpret (nth 1 aa))
  7961. (head (nth 2 aa))
  7962. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7963. (msg "Entry repeats: ")
  7964. (org-log-done nil)
  7965. (org-todo-log-states nil)
  7966. (nshiftmax 10) (nshift 0)
  7967. re type n what ts time)
  7968. (when repeat
  7969. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7970. (org-todo (if (eq interpret 'type) last-state head))
  7971. (when org-log-repeat
  7972. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7973. (memq 'org-add-log-note post-command-hook))
  7974. ;; OK, we are already setup for some record
  7975. (if (eq org-log-repeat 'note)
  7976. ;; make sure we take a note, not only a time stamp
  7977. (setq org-log-note-how 'note))
  7978. ;; Set up for taking a record
  7979. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7980. 'findpos org-log-repeat)))
  7981. (org-back-to-heading t)
  7982. (org-add-planning-info nil nil 'closed)
  7983. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7984. org-deadline-time-regexp "\\)\\|\\("
  7985. org-ts-regexp "\\)"))
  7986. (while (re-search-forward
  7987. re (save-excursion (outline-next-heading) (point)) t)
  7988. (setq type (if (match-end 1) org-scheduled-string
  7989. (if (match-end 3) org-deadline-string "Plain:"))
  7990. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  7991. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7992. (setq n (string-to-number (match-string 2 ts))
  7993. what (match-string 3 ts))
  7994. (if (equal what "w") (setq n (* n 7) what "d"))
  7995. ;; Preparation, see if we need to modify the start date for the change
  7996. (when (match-end 1)
  7997. (setq time (save-match-data (org-time-string-to-time ts)))
  7998. (cond
  7999. ((equal (match-string 1 ts) ".")
  8000. ;; Shift starting date to today
  8001. (org-timestamp-change
  8002. (- (time-to-days (current-time)) (time-to-days time))
  8003. 'day))
  8004. ((equal (match-string 1 ts) "+")
  8005. (while (or (= nshift 0)
  8006. (<= (time-to-days time) (time-to-days (current-time))))
  8007. (when (= (incf nshift) nshiftmax)
  8008. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8009. (error "Abort")))
  8010. (org-timestamp-change n (cdr (assoc what whata)))
  8011. (org-at-timestamp-p t)
  8012. (setq ts (match-string 1))
  8013. (setq time (save-match-data (org-time-string-to-time ts))))
  8014. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8015. ;; rematch, so that we have everything in place for the real shift
  8016. (org-at-timestamp-p t)
  8017. (setq ts (match-string 1))
  8018. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8019. (org-timestamp-change n (cdr (assoc what whata)))
  8020. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8021. (setq org-log-post-message msg)
  8022. (message "%s" msg))))
  8023. (defun org-show-todo-tree (arg)
  8024. "Make a compact tree which shows all headlines marked with TODO.
  8025. The tree will show the lines where the regexp matches, and all higher
  8026. headlines above the match.
  8027. With a \\[universal-argument] prefix, also show the DONE entries.
  8028. With a numeric prefix N, construct a sparse tree for the Nth element
  8029. of `org-todo-keywords-1'."
  8030. (interactive "P")
  8031. (let ((case-fold-search nil)
  8032. (kwd-re
  8033. (cond ((null arg) org-not-done-regexp)
  8034. ((equal arg '(4))
  8035. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8036. (mapcar 'list org-todo-keywords-1))))
  8037. (concat "\\("
  8038. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8039. "\\)\\>")))
  8040. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8041. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8042. org-todo-keywords-1)))
  8043. (t (error "Invalid prefix argument: %s" arg)))))
  8044. (message "%d TODO entries found"
  8045. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8046. (defun org-deadline (&optional remove time)
  8047. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8048. With argument REMOVE, remove any deadline from the item.
  8049. When TIME is set, it should be an internal time specification, and the
  8050. scheduling will use the corresponding date."
  8051. (interactive "P")
  8052. (if remove
  8053. (progn
  8054. (org-remove-timestamp-with-keyword org-deadline-string)
  8055. (message "Item no longer has a deadline."))
  8056. (if (org-get-repeat)
  8057. (error "Cannot change deadline on task with repeater, please do that by hand")
  8058. (org-add-planning-info 'deadline time 'closed)
  8059. (message "Deadline on %s" org-last-inserted-timestamp))))
  8060. (defun org-schedule (&optional remove time)
  8061. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8062. With argument REMOVE, remove any scheduling date from the item.
  8063. When TIME is set, it should be an internal time specification, and the
  8064. scheduling will use the corresponding date."
  8065. (interactive "P")
  8066. (if remove
  8067. (progn
  8068. (org-remove-timestamp-with-keyword org-scheduled-string)
  8069. (message "Item is no longer scheduled."))
  8070. (if (org-get-repeat)
  8071. (error "Cannot reschedule task with repeater, please do that by hand")
  8072. (org-add-planning-info 'scheduled time 'closed)
  8073. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8074. (defun org-remove-timestamp-with-keyword (keyword)
  8075. "Remove all time stamps with KEYWORD in the current entry."
  8076. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8077. beg)
  8078. (save-excursion
  8079. (org-back-to-heading t)
  8080. (setq beg (point))
  8081. (org-end-of-subtree t t)
  8082. (while (re-search-backward re beg t)
  8083. (replace-match "")
  8084. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8085. (equal (char-before) ?\ ))
  8086. (backward-delete-char 1)
  8087. (if (string-match "^[ \t]*$" (buffer-substring
  8088. (point-at-bol) (point-at-eol)))
  8089. (delete-region (point-at-bol)
  8090. (min (point-max) (1+ (point-at-eol))))))))))
  8091. (defun org-add-planning-info (what &optional time &rest remove)
  8092. "Insert new timestamp with keyword in the line directly after the headline.
  8093. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8094. If non is given, the user is prompted for a date.
  8095. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8096. be removed."
  8097. (interactive)
  8098. (let (org-time-was-given org-end-time-was-given ts
  8099. end default-time default-input)
  8100. (when (and (not time) (memq what '(scheduled deadline)))
  8101. ;; Try to get a default date/time from existing timestamp
  8102. (save-excursion
  8103. (org-back-to-heading t)
  8104. (setq end (save-excursion (outline-next-heading) (point)))
  8105. (when (re-search-forward (if (eq what 'scheduled)
  8106. org-scheduled-time-regexp
  8107. org-deadline-time-regexp)
  8108. end t)
  8109. (setq ts (match-string 1)
  8110. default-time
  8111. (apply 'encode-time (org-parse-time-string ts))
  8112. default-input (and ts (org-get-compact-tod ts))))))
  8113. (when what
  8114. ;; If necessary, get the time from the user
  8115. (setq time (or time (org-read-date nil 'to-time nil nil
  8116. default-time default-input))))
  8117. (when (and org-insert-labeled-timestamps-at-point
  8118. (member what '(scheduled deadline)))
  8119. (insert
  8120. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8121. (org-insert-time-stamp time org-time-was-given
  8122. nil nil nil (list org-end-time-was-given))
  8123. (setq what nil))
  8124. (save-excursion
  8125. (save-restriction
  8126. (let (col list elt ts buffer-invisibility-spec)
  8127. (org-back-to-heading t)
  8128. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8129. (goto-char (match-end 1))
  8130. (setq col (current-column))
  8131. (goto-char (match-end 0))
  8132. (if (eobp) (insert "\n") (forward-char 1))
  8133. (if (and (not (looking-at outline-regexp))
  8134. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8135. "[^\r\n]*"))
  8136. (not (equal (match-string 1) org-clock-string)))
  8137. (narrow-to-region (match-beginning 0) (match-end 0))
  8138. (insert-before-markers "\n")
  8139. (backward-char 1)
  8140. (narrow-to-region (point) (point))
  8141. (and org-adapt-indentation (org-indent-to-column col)))
  8142. ;; Check if we have to remove something.
  8143. (setq list (cons what remove))
  8144. (while list
  8145. (setq elt (pop list))
  8146. (goto-char (point-min))
  8147. (when (or (and (eq elt 'scheduled)
  8148. (re-search-forward org-scheduled-time-regexp nil t))
  8149. (and (eq elt 'deadline)
  8150. (re-search-forward org-deadline-time-regexp nil t))
  8151. (and (eq elt 'closed)
  8152. (re-search-forward org-closed-time-regexp nil t)))
  8153. (replace-match "")
  8154. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8155. (if (looking-at " +") (replace-match ""))))
  8156. (goto-char (point-max))
  8157. (when what
  8158. (insert
  8159. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8160. (cond ((eq what 'scheduled) org-scheduled-string)
  8161. ((eq what 'deadline) org-deadline-string)
  8162. ((eq what 'closed) org-closed-string))
  8163. " ")
  8164. (setq ts (org-insert-time-stamp
  8165. time
  8166. (or org-time-was-given
  8167. (and (eq what 'closed) org-log-done-with-time))
  8168. (eq what 'closed)
  8169. nil nil (list org-end-time-was-given)))
  8170. (end-of-line 1))
  8171. (goto-char (point-min))
  8172. (widen)
  8173. (if (and (looking-at "[ \t]+\n")
  8174. (equal (char-before) ?\n))
  8175. (delete-region (1- (point)) (point-at-eol)))
  8176. ts)))))
  8177. (defvar org-log-note-marker (make-marker))
  8178. (defvar org-log-note-purpose nil)
  8179. (defvar org-log-note-state nil)
  8180. (defvar org-log-note-how nil)
  8181. (defvar org-log-note-extra nil)
  8182. (defvar org-log-note-window-configuration nil)
  8183. (defvar org-log-note-return-to (make-marker))
  8184. (defvar org-log-post-message nil
  8185. "Message to be displayed after a log note has been stored.
  8186. The auto-repeater uses this.")
  8187. (defun org-add-note ()
  8188. "Add a note to the current entry.
  8189. This is done in the same way as adding a state change note."
  8190. (interactive)
  8191. (org-add-log-setup 'note nil 'findpos nil))
  8192. (defvar org-property-end-re)
  8193. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8194. "Set up the post command hook to take a note.
  8195. If this is about to TODO state change, the new state is expected in STATE.
  8196. When FINDPOS is non-nil, find the correct position for the note in
  8197. the current entry. If not, assume that it can be inserted at point.
  8198. HOW is an indicator what kind of note should be created.
  8199. EXTRA is additional text that will be inserted into the notes buffer."
  8200. (save-restriction
  8201. (save-excursion
  8202. (when findpos
  8203. (org-back-to-heading t)
  8204. (narrow-to-region (point) (save-excursion
  8205. (outline-next-heading) (point)))
  8206. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8207. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8208. "[^\r\n]*\\)?"))
  8209. (goto-char (match-end 0))
  8210. (when (and org-log-state-notes-insert-after-drawers
  8211. (save-excursion
  8212. (forward-line) (looking-at org-drawer-regexp)))
  8213. (progn (forward-line)
  8214. (while (looking-at org-drawer-regexp)
  8215. (goto-char (match-end 0))
  8216. (re-search-forward org-property-end-re (point-max) t)
  8217. (forward-line))
  8218. (forward-line -1)))
  8219. (unless org-log-states-order-reversed
  8220. (and (= (char-after) ?\n) (forward-char 1))
  8221. (org-skip-over-state-notes)
  8222. (skip-chars-backward " \t\n\r")))
  8223. (move-marker org-log-note-marker (point))
  8224. (setq org-log-note-purpose purpose
  8225. org-log-note-state state
  8226. org-log-note-how how
  8227. org-log-note-extra extra)
  8228. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8229. (defun org-skip-over-state-notes ()
  8230. "Skip past the list of State notes in an entry."
  8231. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8232. (while (looking-at "[ \t]*- State")
  8233. (condition-case nil
  8234. (org-next-item)
  8235. (error (org-end-of-item)))))
  8236. (defun org-add-log-note (&optional purpose)
  8237. "Pop up a window for taking a note, and add this note later at point."
  8238. (remove-hook 'post-command-hook 'org-add-log-note)
  8239. (setq org-log-note-window-configuration (current-window-configuration))
  8240. (delete-other-windows)
  8241. (move-marker org-log-note-return-to (point))
  8242. (switch-to-buffer (marker-buffer org-log-note-marker))
  8243. (goto-char org-log-note-marker)
  8244. (org-switch-to-buffer-other-window "*Org Note*")
  8245. (erase-buffer)
  8246. (if (memq org-log-note-how '(time state))
  8247. (let (current-prefix-arg) (org-store-log-note))
  8248. (let ((org-inhibit-startup t)) (org-mode))
  8249. (insert (format "# Insert note for %s.
  8250. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8251. (cond
  8252. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8253. ((eq org-log-note-purpose 'done) "closed todo item")
  8254. ((eq org-log-note-purpose 'state)
  8255. (format "state change to \"%s\"" org-log-note-state))
  8256. ((eq org-log-note-purpose 'note)
  8257. "this entry")
  8258. (t (error "This should not happen")))))
  8259. (if org-log-note-extra (insert org-log-note-extra))
  8260. (org-set-local 'org-finish-function 'org-store-log-note)))
  8261. (defvar org-note-abort nil) ; dynamically scoped
  8262. (defun org-store-log-note ()
  8263. "Finish taking a log note, and insert it to where it belongs."
  8264. (let ((txt (buffer-string))
  8265. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8266. lines ind)
  8267. (kill-buffer (current-buffer))
  8268. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8269. (setq txt (replace-match "" t t txt)))
  8270. (if (string-match "\\s-+\\'" txt)
  8271. (setq txt (replace-match "" t t txt)))
  8272. (setq lines (org-split-string txt "\n"))
  8273. (when (and note (string-match "\\S-" note))
  8274. (setq note
  8275. (org-replace-escapes
  8276. note
  8277. (list (cons "%u" (user-login-name))
  8278. (cons "%U" user-full-name)
  8279. (cons "%t" (format-time-string
  8280. (org-time-stamp-format 'long 'inactive)
  8281. (current-time)))
  8282. (cons "%s" (if org-log-note-state
  8283. (concat "\"" org-log-note-state "\"")
  8284. "")))))
  8285. (if lines (setq note (concat note " \\\\")))
  8286. (push note lines))
  8287. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8288. (when lines
  8289. (save-excursion
  8290. (set-buffer (marker-buffer org-log-note-marker))
  8291. (save-excursion
  8292. (goto-char org-log-note-marker)
  8293. (move-marker org-log-note-marker nil)
  8294. (end-of-line 1)
  8295. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8296. (indent-relative nil)
  8297. (insert "- " (pop lines))
  8298. (org-indent-line-function)
  8299. (beginning-of-line 1)
  8300. (looking-at "[ \t]*")
  8301. (setq ind (concat (match-string 0) " "))
  8302. (end-of-line 1)
  8303. (while lines (insert "\n" ind (pop lines)))))))
  8304. (set-window-configuration org-log-note-window-configuration)
  8305. (with-current-buffer (marker-buffer org-log-note-return-to)
  8306. (goto-char org-log-note-return-to))
  8307. (move-marker org-log-note-return-to nil)
  8308. (and org-log-post-message (message "%s" org-log-post-message)))
  8309. (defun org-sparse-tree (&optional arg)
  8310. "Create a sparse tree, prompt for the details.
  8311. This command can create sparse trees. You first need to select the type
  8312. of match used to create the tree:
  8313. t Show entries with a specific TODO keyword.
  8314. T Show entries selected by a tags match.
  8315. p Enter a property name and its value (both with completion on existing
  8316. names/values) and show entries with that property.
  8317. r Show entries matching a regular expression
  8318. d Show deadlines due within `org-deadline-warning-days'."
  8319. (interactive "P")
  8320. (let (ans kwd value)
  8321. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8322. (setq ans (read-char-exclusive))
  8323. (cond
  8324. ((equal ans ?d)
  8325. (call-interactively 'org-check-deadlines))
  8326. ((equal ans ?b)
  8327. (call-interactively 'org-check-before-date))
  8328. ((equal ans ?t)
  8329. (org-show-todo-tree '(4)))
  8330. ((equal ans ?T)
  8331. (call-interactively 'org-tags-sparse-tree))
  8332. ((member ans '(?p ?P))
  8333. (setq kwd (org-ido-completing-read "Property: "
  8334. (mapcar 'list (org-buffer-property-keys))))
  8335. (setq value (org-ido-completing-read "Value: "
  8336. (mapcar 'list (org-property-values kwd))))
  8337. (unless (string-match "\\`{.*}\\'" value)
  8338. (setq value (concat "\"" value "\"")))
  8339. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8340. ((member ans '(?r ?R ?/))
  8341. (call-interactively 'org-occur))
  8342. (t (error "No such sparse tree command \"%c\"" ans)))))
  8343. (defvar org-occur-highlights nil
  8344. "List of overlays used for occur matches.")
  8345. (make-variable-buffer-local 'org-occur-highlights)
  8346. (defvar org-occur-parameters nil
  8347. "Parameters of the active org-occur calls.
  8348. This is a list, each call to org-occur pushes as cons cell,
  8349. containing the regular expression and the callback, onto the list.
  8350. The list can contain several entries if `org-occur' has been called
  8351. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8352. will only contain one set of parameters. When the highlights are
  8353. removed (for example with `C-c C-c', or with the next edit (depending
  8354. on `org-remove-highlights-with-change'), this variable is emptied
  8355. as well.")
  8356. (make-variable-buffer-local 'org-occur-parameters)
  8357. (defun org-occur (regexp &optional keep-previous callback)
  8358. "Make a compact tree which shows all matches of REGEXP.
  8359. The tree will show the lines where the regexp matches, and all higher
  8360. headlines above the match. It will also show the heading after the match,
  8361. to make sure editing the matching entry is easy.
  8362. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8363. call to `org-occur' will be kept, to allow stacking of calls to this
  8364. command.
  8365. If CALLBACK is non-nil, it is a function which is called to confirm
  8366. that the match should indeed be shown."
  8367. (interactive "sRegexp: \nP")
  8368. (unless keep-previous
  8369. (org-remove-occur-highlights nil nil t))
  8370. (push (cons regexp callback) org-occur-parameters)
  8371. (let ((cnt 0))
  8372. (save-excursion
  8373. (goto-char (point-min))
  8374. (if (or (not keep-previous) ; do not want to keep
  8375. (not org-occur-highlights)) ; no previous matches
  8376. ;; hide everything
  8377. (org-overview))
  8378. (while (re-search-forward regexp nil t)
  8379. (when (or (not callback)
  8380. (save-match-data (funcall callback)))
  8381. (setq cnt (1+ cnt))
  8382. (when org-highlight-sparse-tree-matches
  8383. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8384. (org-show-context 'occur-tree))))
  8385. (when org-remove-highlights-with-change
  8386. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8387. nil 'local))
  8388. (unless org-sparse-tree-open-archived-trees
  8389. (org-hide-archived-subtrees (point-min) (point-max)))
  8390. (run-hooks 'org-occur-hook)
  8391. (if (interactive-p)
  8392. (message "%d match(es) for regexp %s" cnt regexp))
  8393. cnt))
  8394. (defun org-show-context (&optional key)
  8395. "Make sure point and context and visible.
  8396. How much context is shown depends upon the variables
  8397. `org-show-hierarchy-above', `org-show-following-heading'. and
  8398. `org-show-siblings'."
  8399. (let ((heading-p (org-on-heading-p t))
  8400. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8401. (following-p (org-get-alist-option org-show-following-heading key))
  8402. (entry-p (org-get-alist-option org-show-entry-below key))
  8403. (siblings-p (org-get-alist-option org-show-siblings key)))
  8404. (catch 'exit
  8405. ;; Show heading or entry text
  8406. (if (and heading-p (not entry-p))
  8407. (org-flag-heading nil) ; only show the heading
  8408. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8409. (org-show-hidden-entry))) ; show entire entry
  8410. (when following-p
  8411. ;; Show next sibling, or heading below text
  8412. (save-excursion
  8413. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8414. (org-flag-heading nil))))
  8415. (when siblings-p (org-show-siblings))
  8416. (when hierarchy-p
  8417. ;; show all higher headings, possibly with siblings
  8418. (save-excursion
  8419. (while (and (condition-case nil
  8420. (progn (org-up-heading-all 1) t)
  8421. (error nil))
  8422. (not (bobp)))
  8423. (org-flag-heading nil)
  8424. (when siblings-p (org-show-siblings))))))))
  8425. (defun org-reveal (&optional siblings)
  8426. "Show current entry, hierarchy above it, and the following headline.
  8427. This can be used to show a consistent set of context around locations
  8428. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8429. not t for the search context.
  8430. With optional argument SIBLINGS, on each level of the hierarchy all
  8431. siblings are shown. This repairs the tree structure to what it would
  8432. look like when opened with hierarchical calls to `org-cycle'."
  8433. (interactive "P")
  8434. (let ((org-show-hierarchy-above t)
  8435. (org-show-following-heading t)
  8436. (org-show-siblings (if siblings t org-show-siblings)))
  8437. (org-show-context nil)))
  8438. (defun org-highlight-new-match (beg end)
  8439. "Highlight from BEG to END and mark the highlight is an occur headline."
  8440. (let ((ov (org-make-overlay beg end)))
  8441. (org-overlay-put ov 'face 'secondary-selection)
  8442. (push ov org-occur-highlights)))
  8443. (defun org-remove-occur-highlights (&optional beg end noremove)
  8444. "Remove the occur highlights from the buffer.
  8445. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8446. from the `before-change-functions' in the current buffer."
  8447. (interactive)
  8448. (unless org-inhibit-highlight-removal
  8449. (mapc 'org-delete-overlay org-occur-highlights)
  8450. (setq org-occur-highlights nil)
  8451. (setq org-occur-parameters nil)
  8452. (unless noremove
  8453. (remove-hook 'before-change-functions
  8454. 'org-remove-occur-highlights 'local))))
  8455. ;;;; Priorities
  8456. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8457. "Regular expression matching the priority indicator.")
  8458. (defvar org-remove-priority-next-time nil)
  8459. (defun org-priority-up ()
  8460. "Increase the priority of the current item."
  8461. (interactive)
  8462. (org-priority 'up))
  8463. (defun org-priority-down ()
  8464. "Decrease the priority of the current item."
  8465. (interactive)
  8466. (org-priority 'down))
  8467. (defun org-priority (&optional action)
  8468. "Change the priority of an item by ARG.
  8469. ACTION can be `set', `up', `down', or a character."
  8470. (interactive)
  8471. (setq action (or action 'set))
  8472. (let (current new news have remove)
  8473. (save-excursion
  8474. (org-back-to-heading)
  8475. (if (looking-at org-priority-regexp)
  8476. (setq current (string-to-char (match-string 2))
  8477. have t)
  8478. (setq current org-default-priority))
  8479. (cond
  8480. ((or (eq action 'set)
  8481. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8482. (if (not (eq action 'set))
  8483. (setq new action)
  8484. (message "Priority %c-%c, SPC to remove: "
  8485. org-highest-priority org-lowest-priority)
  8486. (setq new (read-char-exclusive)))
  8487. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8488. (= (upcase org-lowest-priority) org-lowest-priority))
  8489. (setq new (upcase new)))
  8490. (cond ((equal new ?\ ) (setq remove t))
  8491. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8492. (error "Priority must be between `%c' and `%c'"
  8493. org-highest-priority org-lowest-priority))))
  8494. ((eq action 'up)
  8495. (if (and (not have) (eq last-command this-command))
  8496. (setq new org-lowest-priority)
  8497. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8498. org-default-priority (1- current)))))
  8499. ((eq action 'down)
  8500. (if (and (not have) (eq last-command this-command))
  8501. (setq new org-highest-priority)
  8502. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8503. org-default-priority (1+ current)))))
  8504. (t (error "Invalid action")))
  8505. (if (or (< (upcase new) org-highest-priority)
  8506. (> (upcase new) org-lowest-priority))
  8507. (setq remove t))
  8508. (setq news (format "%c" new))
  8509. (if have
  8510. (if remove
  8511. (replace-match "" t t nil 1)
  8512. (replace-match news t t nil 2))
  8513. (if remove
  8514. (error "No priority cookie found in line")
  8515. (looking-at org-todo-line-regexp)
  8516. (if (match-end 2)
  8517. (progn
  8518. (goto-char (match-end 2))
  8519. (insert " [#" news "]"))
  8520. (goto-char (match-beginning 3))
  8521. (insert "[#" news "] ")))))
  8522. (org-preserve-lc (org-set-tags nil 'align))
  8523. (if remove
  8524. (message "Priority removed")
  8525. (message "Priority of current item set to %s" news))))
  8526. (defun org-get-priority (s)
  8527. "Find priority cookie and return priority."
  8528. (save-match-data
  8529. (if (not (string-match org-priority-regexp s))
  8530. (* 1000 (- org-lowest-priority org-default-priority))
  8531. (* 1000 (- org-lowest-priority
  8532. (string-to-char (match-string 2 s)))))))
  8533. ;;;; Tags
  8534. (defvar org-agenda-archives-mode)
  8535. (defun org-scan-tags (action matcher &optional todo-only)
  8536. "Scan headline tags with inheritance and produce output ACTION.
  8537. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8538. or `agenda' to produce an entry list for an agenda view. It can also be
  8539. a Lisp form or a function that should be called at each matched headline, in
  8540. this case the return value is a list of all return values from these calls.
  8541. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8542. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8543. only lines with a TODO keyword are included in the output."
  8544. (require 'org-agenda)
  8545. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8546. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8547. (org-re
  8548. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8549. (props (list 'face 'default
  8550. 'done-face 'org-done
  8551. 'undone-face 'default
  8552. 'mouse-face 'highlight
  8553. 'org-not-done-regexp org-not-done-regexp
  8554. 'org-todo-regexp org-todo-regexp
  8555. 'keymap org-agenda-keymap
  8556. 'help-echo
  8557. (format "mouse-2 or RET jump to org file %s"
  8558. (abbreviate-file-name
  8559. (or (buffer-file-name (buffer-base-buffer))
  8560. (buffer-name (buffer-base-buffer)))))))
  8561. (case-fold-search nil)
  8562. lspos tags tags-list
  8563. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8564. (llast 0) rtn rtn1 level category i txt
  8565. todo marker entry priority)
  8566. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8567. (setq action (list 'lambda nil action)))
  8568. (save-excursion
  8569. (goto-char (point-min))
  8570. (when (eq action 'sparse-tree)
  8571. (org-overview)
  8572. (org-remove-occur-highlights))
  8573. (while (re-search-forward re nil t)
  8574. (catch :skip
  8575. (setq todo (if (match-end 1) (match-string 2))
  8576. tags (if (match-end 4) (match-string 4)))
  8577. (goto-char (setq lspos (1+ (match-beginning 0))))
  8578. (setq level (org-reduced-level (funcall outline-level))
  8579. category (org-get-category))
  8580. (setq i llast llast level)
  8581. ;; remove tag lists from same and sublevels
  8582. (while (>= i level)
  8583. (when (setq entry (assoc i tags-alist))
  8584. (setq tags-alist (delete entry tags-alist)))
  8585. (setq i (1- i)))
  8586. ;; add the next tags
  8587. (when tags
  8588. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8589. tags-alist
  8590. (cons (cons level tags) tags-alist)))
  8591. ;; compile tags for current headline
  8592. (setq tags-list
  8593. (if org-use-tag-inheritance
  8594. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8595. tags))
  8596. (when org-use-tag-inheritance
  8597. (setcdr (car tags-alist)
  8598. (mapcar (lambda (x)
  8599. (setq x (copy-sequence x))
  8600. (org-add-prop-inherited x))
  8601. (cdar tags-alist))))
  8602. (when (and tags org-use-tag-inheritance
  8603. (not (eq t org-use-tag-inheritance)))
  8604. ;; selective inheritance, remove uninherited ones
  8605. (setcdr (car tags-alist)
  8606. (org-remove-uniherited-tags (cdar tags-alist))))
  8607. (when (and (or (not todo-only)
  8608. (and (member todo org-not-done-keywords)
  8609. (or (not org-agenda-tags-todo-honor-ignore-options)
  8610. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8611. (let ((case-fold-search t)) (eval matcher))
  8612. (or
  8613. (not (member org-archive-tag tags-list))
  8614. ;; we have an archive tag, should we use this anyway?
  8615. (or (not org-agenda-skip-archived-trees)
  8616. (and (eq action 'agenda) org-agenda-archives-mode))))
  8617. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8618. ;; select this headline
  8619. (cond
  8620. ((eq action 'sparse-tree)
  8621. (and org-highlight-sparse-tree-matches
  8622. (org-get-heading) (match-end 0)
  8623. (org-highlight-new-match
  8624. (match-beginning 0) (match-beginning 1)))
  8625. (org-show-context 'tags-tree))
  8626. ((eq action 'agenda)
  8627. (setq txt (org-format-agenda-item
  8628. ""
  8629. (concat
  8630. (if org-tags-match-list-sublevels
  8631. (make-string (1- level) ?.) "")
  8632. (org-get-heading))
  8633. category (org-get-tags-at))
  8634. priority (org-get-priority txt))
  8635. (goto-char lspos)
  8636. (setq marker (org-agenda-new-marker))
  8637. (org-add-props txt props
  8638. 'org-marker marker 'org-hd-marker marker 'org-category category
  8639. 'priority priority 'type "tagsmatch")
  8640. (push txt rtn))
  8641. ((functionp action)
  8642. (save-excursion
  8643. (setq rtn1 (funcall action))
  8644. (push rtn1 rtn))
  8645. (goto-char (point-at-eol)))
  8646. (t (error "Invalid action")))
  8647. ;; if we are to skip sublevels, jump to end of subtree
  8648. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8649. (when (and (eq action 'sparse-tree)
  8650. (not org-sparse-tree-open-archived-trees))
  8651. (org-hide-archived-subtrees (point-min) (point-max)))
  8652. (nreverse rtn)))
  8653. (defun org-remove-uniherited-tags (tags)
  8654. "Remove all tags that are not inherited from the list TAGS."
  8655. (cond
  8656. ((eq org-use-tag-inheritance t)
  8657. (if org-tags-exclude-from-inheritance
  8658. (org-delete-all org-tags-exclude-from-inheritance tags)
  8659. tags))
  8660. ((not org-use-tag-inheritance) nil)
  8661. ((stringp org-use-tag-inheritance)
  8662. (delq nil (mapcar
  8663. (lambda (x)
  8664. (if (and (string-match org-use-tag-inheritance x)
  8665. (not (member x org-tags-exclude-from-inheritance)))
  8666. x nil))
  8667. tags)))
  8668. ((listp org-use-tag-inheritance)
  8669. (delq nil (mapcar
  8670. (lambda (x)
  8671. (if (member x org-use-tag-inheritance) x nil))
  8672. tags)))))
  8673. (defvar todo-only) ;; dynamically scoped
  8674. (defun org-tags-sparse-tree (&optional todo-only match)
  8675. "Create a sparse tree according to tags string MATCH.
  8676. MATCH can contain positive and negative selection of tags, like
  8677. \"+WORK+URGENT-WITHBOSS\".
  8678. If optional argument TODO-ONLY is non-nil, only select lines that are
  8679. also TODO lines."
  8680. (interactive "P")
  8681. (org-prepare-agenda-buffers (list (current-buffer)))
  8682. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8683. (defvar org-cached-props nil)
  8684. (defun org-cached-entry-get (pom property)
  8685. (if (or (eq t org-use-property-inheritance)
  8686. (and (stringp org-use-property-inheritance)
  8687. (string-match org-use-property-inheritance property))
  8688. (and (listp org-use-property-inheritance)
  8689. (member property org-use-property-inheritance)))
  8690. ;; Caching is not possible, check it directly
  8691. (org-entry-get pom property 'inherit)
  8692. ;; Get all properties, so that we can do complicated checks easily
  8693. (cdr (assoc property (or org-cached-props
  8694. (setq org-cached-props
  8695. (org-entry-properties pom)))))))
  8696. (defun org-global-tags-completion-table (&optional files)
  8697. "Return the list of all tags in all agenda buffer/files."
  8698. (save-excursion
  8699. (org-uniquify
  8700. (delq nil
  8701. (apply 'append
  8702. (mapcar
  8703. (lambda (file)
  8704. (set-buffer (find-file-noselect file))
  8705. (append (org-get-buffer-tags)
  8706. (mapcar (lambda (x) (if (stringp (car-safe x))
  8707. (list (car-safe x)) nil))
  8708. org-tag-alist)))
  8709. (if (and files (car files))
  8710. files
  8711. (org-agenda-files))))))))
  8712. (defun org-make-tags-matcher (match)
  8713. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8714. ;; todo-only is scoped dynamically into this function, and the function
  8715. ;; may change it if the matcher asks for it.
  8716. (unless match
  8717. ;; Get a new match request, with completion
  8718. (let ((org-last-tags-completion-table
  8719. (org-global-tags-completion-table)))
  8720. (setq match (org-completing-read
  8721. "Match: " 'org-tags-completion-function nil nil nil
  8722. 'org-tags-history))))
  8723. ;; Parse the string and create a lisp form
  8724. (let ((match0 match)
  8725. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8726. minus tag mm
  8727. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8728. orterms term orlist re-p str-p level-p level-op time-p
  8729. prop-p pn pv po cat-p gv rest)
  8730. (if (string-match "/+" match)
  8731. ;; match contains also a todo-matching request
  8732. (progn
  8733. (setq tagsmatch (substring match 0 (match-beginning 0))
  8734. todomatch (substring match (match-end 0)))
  8735. (if (string-match "^!" todomatch)
  8736. (setq todo-only t todomatch (substring todomatch 1)))
  8737. (if (string-match "^\\s-*$" todomatch)
  8738. (setq todomatch nil)))
  8739. ;; only matching tags
  8740. (setq tagsmatch match todomatch nil))
  8741. ;; Make the tags matcher
  8742. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8743. (setq tagsmatcher t)
  8744. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8745. (while (setq term (pop orterms))
  8746. (while (and (equal (substring term -1) "\\") orterms)
  8747. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8748. (while (string-match re term)
  8749. (setq rest (substring term (match-end 0))
  8750. minus (and (match-end 1)
  8751. (equal (match-string 1 term) "-"))
  8752. tag (match-string 2 term)
  8753. re-p (equal (string-to-char tag) ?{)
  8754. level-p (match-end 4)
  8755. prop-p (match-end 5)
  8756. mm (cond
  8757. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8758. (level-p
  8759. (setq level-op (org-op-to-function (match-string 3 term)))
  8760. `(,level-op level ,(string-to-number
  8761. (match-string 4 term))))
  8762. (prop-p
  8763. (setq pn (match-string 5 term)
  8764. po (match-string 6 term)
  8765. pv (match-string 7 term)
  8766. cat-p (equal pn "CATEGORY")
  8767. re-p (equal (string-to-char pv) ?{)
  8768. str-p (equal (string-to-char pv) ?\")
  8769. time-p (save-match-data
  8770. (string-match "^\"[[<].*[]>]\"$" pv))
  8771. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8772. (if time-p (setq pv (org-matcher-time pv)))
  8773. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8774. (cond
  8775. ((equal pn "CATEGORY")
  8776. (setq gv '(get-text-property (point) 'org-category)))
  8777. ((equal pn "TODO")
  8778. (setq gv 'todo))
  8779. (t
  8780. (setq gv `(org-cached-entry-get nil ,pn))))
  8781. (if re-p
  8782. (if (eq po 'org<>)
  8783. `(not (string-match ,pv (or ,gv "")))
  8784. `(string-match ,pv (or ,gv "")))
  8785. (if str-p
  8786. `(,po (or ,gv "") ,pv)
  8787. `(,po (string-to-number (or ,gv ""))
  8788. ,(string-to-number pv) ))))
  8789. (t `(member ,(downcase tag) tags-list)))
  8790. mm (if minus (list 'not mm) mm)
  8791. term rest)
  8792. (push mm tagsmatcher))
  8793. (push (if (> (length tagsmatcher) 1)
  8794. (cons 'and tagsmatcher)
  8795. (car tagsmatcher))
  8796. orlist)
  8797. (setq tagsmatcher nil))
  8798. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8799. (setq tagsmatcher
  8800. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8801. ;; Make the todo matcher
  8802. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8803. (setq todomatcher t)
  8804. (setq orterms (org-split-string todomatch "|") orlist nil)
  8805. (while (setq term (pop orterms))
  8806. (while (string-match re term)
  8807. (setq minus (and (match-end 1)
  8808. (equal (match-string 1 term) "-"))
  8809. kwd (match-string 2 term)
  8810. re-p (equal (string-to-char kwd) ?{)
  8811. term (substring term (match-end 0))
  8812. mm (if re-p
  8813. `(string-match ,(substring kwd 1 -1) todo)
  8814. (list 'equal 'todo kwd))
  8815. mm (if minus (list 'not mm) mm))
  8816. (push mm todomatcher))
  8817. (push (if (> (length todomatcher) 1)
  8818. (cons 'and todomatcher)
  8819. (car todomatcher))
  8820. orlist)
  8821. (setq todomatcher nil))
  8822. (setq todomatcher (if (> (length orlist) 1)
  8823. (cons 'or orlist) (car orlist))))
  8824. ;; Return the string and lisp forms of the matcher
  8825. (setq matcher (if todomatcher
  8826. (list 'and tagsmatcher todomatcher)
  8827. tagsmatcher))
  8828. (cons match0 matcher)))
  8829. (defun org-op-to-function (op &optional stringp)
  8830. "Turn an operator into the appropriate function."
  8831. (setq op
  8832. (cond
  8833. ((equal op "<" ) '(< string< org-time<))
  8834. ((equal op ">" ) '(> org-string> org-time>))
  8835. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8836. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8837. ((member op '("=" "==")) '(= string= org-time=))
  8838. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8839. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8840. (defun org<> (a b) (not (= a b)))
  8841. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8842. (defun org-string>= (a b) (not (string< a b)))
  8843. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8844. (defun org-string<> (a b) (not (string= a b)))
  8845. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8846. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8847. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8848. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8849. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8850. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8851. (defun org-2ft (s)
  8852. "Convert S to a floating point time.
  8853. If S is already a number, just return it. If it is a string, parse
  8854. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8855. (cond
  8856. ((numberp s) s)
  8857. ((stringp s)
  8858. (condition-case nil
  8859. (float-time (apply 'encode-time (org-parse-time-string s)))
  8860. (error 0.)))
  8861. (t 0.)))
  8862. (defun org-time-today ()
  8863. "Time in seconds today at 0:00.
  8864. Returns the float number of seconds since the beginning of the
  8865. epoch to the beginning of today (00:00)."
  8866. (float-time (apply 'encode-time
  8867. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8868. (defun org-matcher-time (s)
  8869. "Interpret a time comparison value."
  8870. (save-match-data
  8871. (cond
  8872. ((string= s "<now>") (float-time))
  8873. ((string= s "<today>") (org-time-today))
  8874. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8875. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8876. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8877. (+ (org-time-today)
  8878. (* (string-to-number (match-string 1 s))
  8879. (cdr (assoc (match-string 2 s)
  8880. '(("d" . 86400.0) ("w" . 604800.0)
  8881. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8882. (t (org-2ft s)))))
  8883. (defun org-match-any-p (re list)
  8884. "Does re match any element of list?"
  8885. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8886. (delq nil list))
  8887. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8888. (defvar org-tags-overlay (org-make-overlay 1 1))
  8889. (org-detach-overlay org-tags-overlay)
  8890. (defun org-get-local-tags-at (&optional pos)
  8891. "Get a list of tags defined in the current headline."
  8892. (org-get-tags-at pos 'local))
  8893. (defun org-get-local-tags ()
  8894. "Get a list of tags defined in the current headline."
  8895. (org-get-tags-at nil 'local))
  8896. (defun org-get-tags-at (&optional pos local)
  8897. "Get a list of all headline tags applicable at POS.
  8898. POS defaults to point. If tags are inherited, the list contains
  8899. the targets in the same sequence as the headlines appear, i.e.
  8900. the tags of the current headline come last.
  8901. When LOCAL is non-nil, only return tags from the current headline,
  8902. ignore inherited ones."
  8903. (interactive)
  8904. (let (tags ltags lastpos parent)
  8905. (save-excursion
  8906. (save-restriction
  8907. (widen)
  8908. (goto-char (or pos (point)))
  8909. (save-match-data
  8910. (catch 'done
  8911. (condition-case nil
  8912. (progn
  8913. (org-back-to-heading t)
  8914. (while (not (equal lastpos (point)))
  8915. (setq lastpos (point))
  8916. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8917. (setq ltags (org-split-string
  8918. (org-match-string-no-properties 1) ":"))
  8919. (when parent
  8920. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8921. (setq tags (append
  8922. (if parent
  8923. (org-remove-uniherited-tags ltags)
  8924. ltags)
  8925. tags)))
  8926. (or org-use-tag-inheritance (throw 'done t))
  8927. (if local (throw 'done t))
  8928. (org-up-heading-all 1)
  8929. (setq parent t)))
  8930. (error nil)))))
  8931. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8932. (defun org-add-prop-inherited (s)
  8933. (add-text-properties 0 (length s) '(inherited t) s)
  8934. s)
  8935. (defun org-toggle-tag (tag &optional onoff)
  8936. "Toggle the tag TAG for the current line.
  8937. If ONOFF is `on' or `off', don't toggle but set to this state."
  8938. (let (res current)
  8939. (save-excursion
  8940. (org-back-to-heading t)
  8941. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8942. (point-at-eol) t)
  8943. (progn
  8944. (setq current (match-string 1))
  8945. (replace-match ""))
  8946. (setq current ""))
  8947. (setq current (nreverse (org-split-string current ":")))
  8948. (cond
  8949. ((eq onoff 'on)
  8950. (setq res t)
  8951. (or (member tag current) (push tag current)))
  8952. ((eq onoff 'off)
  8953. (or (not (member tag current)) (setq current (delete tag current))))
  8954. (t (if (member tag current)
  8955. (setq current (delete tag current))
  8956. (setq res t)
  8957. (push tag current))))
  8958. (end-of-line 1)
  8959. (if current
  8960. (progn
  8961. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8962. (org-set-tags nil t))
  8963. (delete-horizontal-space))
  8964. (run-hooks 'org-after-tags-change-hook))
  8965. res))
  8966. (defun org-align-tags-here (to-col)
  8967. ;; Assumes that this is a headline
  8968. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8969. (beginning-of-line 1)
  8970. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8971. (< pos (match-beginning 2)))
  8972. (progn
  8973. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8974. (goto-char (match-beginning 1))
  8975. (insert " ")
  8976. (delete-region (point) (1+ (match-beginning 2)))
  8977. (setq ncol (max (1+ (current-column))
  8978. (1+ col)
  8979. (if (> to-col 0)
  8980. to-col
  8981. (- (abs to-col) tags-l))))
  8982. (setq p (point))
  8983. (insert (make-string (- ncol (current-column)) ?\ ))
  8984. (setq ncol (current-column))
  8985. (when indent-tabs-mode (tabify p (point-at-eol)))
  8986. (org-move-to-column (min ncol col) t))
  8987. (goto-char pos))))
  8988. (defun org-set-tags-command (&optional arg just-align)
  8989. "Call the set-tags command for the current entry."
  8990. (interactive "P")
  8991. (if (org-on-heading-p)
  8992. (org-set-tags arg just-align)
  8993. (save-excursion
  8994. (org-back-to-heading t)
  8995. (org-set-tags arg just-align))))
  8996. (defun org-set-tags (&optional arg just-align)
  8997. "Set the tags for the current headline.
  8998. With prefix ARG, realign all tags in headings in the current buffer."
  8999. (interactive "P")
  9000. (let* ((re (concat "^" outline-regexp))
  9001. (current (org-get-tags-string))
  9002. (col (current-column))
  9003. (org-setting-tags t)
  9004. table current-tags inherited-tags ; computed below when needed
  9005. tags p0 c0 c1 rpl)
  9006. (if arg
  9007. (save-excursion
  9008. (goto-char (point-min))
  9009. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9010. (while (re-search-forward re nil t)
  9011. (org-set-tags nil t)
  9012. (end-of-line 1)))
  9013. (message "All tags realigned to column %d" org-tags-column))
  9014. (if just-align
  9015. (setq tags current)
  9016. ;; Get a new set of tags from the user
  9017. (save-excursion
  9018. (setq table (or org-tag-alist (org-get-buffer-tags))
  9019. org-last-tags-completion-table table
  9020. current-tags (org-split-string current ":")
  9021. inherited-tags (nreverse
  9022. (nthcdr (length current-tags)
  9023. (nreverse (org-get-tags-at))))
  9024. tags
  9025. (if (or (eq t org-use-fast-tag-selection)
  9026. (and org-use-fast-tag-selection
  9027. (delq nil (mapcar 'cdr table))))
  9028. (org-fast-tag-selection
  9029. current-tags inherited-tags table
  9030. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9031. (let ((org-add-colon-after-tag-completion t))
  9032. (org-trim
  9033. (org-without-partial-completion
  9034. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9035. nil nil current 'org-tags-history)))))))
  9036. (while (string-match "[-+&]+" tags)
  9037. ;; No boolean logic, just a list
  9038. (setq tags (replace-match ":" t t tags))))
  9039. (if (string-match "\\`[\t ]*\\'" tags)
  9040. (setq tags "")
  9041. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9042. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9043. ;; Insert new tags at the correct column
  9044. (beginning-of-line 1)
  9045. (cond
  9046. ((and (equal current "") (equal tags "")))
  9047. ((re-search-forward
  9048. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9049. (point-at-eol) t)
  9050. (if (equal tags "")
  9051. (setq rpl "")
  9052. (goto-char (match-beginning 0))
  9053. (setq c0 (current-column) p0 (point)
  9054. c1 (max (1+ c0) (if (> org-tags-column 0)
  9055. org-tags-column
  9056. (- (- org-tags-column) (length tags))))
  9057. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9058. (replace-match rpl t t)
  9059. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9060. tags)
  9061. (t (error "Tags alignment failed")))
  9062. (org-move-to-column col)
  9063. (unless just-align
  9064. (run-hooks 'org-after-tags-change-hook)))))
  9065. (defun org-change-tag-in-region (beg end tag off)
  9066. "Add or remove TAG for each entry in the region.
  9067. This works in the agenda, and also in an org-mode buffer."
  9068. (interactive
  9069. (list (region-beginning) (region-end)
  9070. (let ((org-last-tags-completion-table
  9071. (if (org-mode-p)
  9072. (org-get-buffer-tags)
  9073. (org-global-tags-completion-table))))
  9074. (org-ido-completing-read
  9075. "Tag: " 'org-tags-completion-function nil nil nil
  9076. 'org-tags-history))
  9077. (progn
  9078. (message "[s]et or [r]emove? ")
  9079. (equal (read-char-exclusive) ?r))))
  9080. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9081. (let ((agendap (equal major-mode 'org-agenda-mode))
  9082. l1 l2 m buf pos newhead (cnt 0))
  9083. (goto-char end)
  9084. (setq l2 (1- (org-current-line)))
  9085. (goto-char beg)
  9086. (setq l1 (org-current-line))
  9087. (loop for l from l1 to l2 do
  9088. (goto-line l)
  9089. (setq m (get-text-property (point) 'org-hd-marker))
  9090. (when (or (and (org-mode-p) (org-on-heading-p))
  9091. (and agendap m))
  9092. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9093. pos (if agendap m (point)))
  9094. (with-current-buffer buf
  9095. (save-excursion
  9096. (save-restriction
  9097. (goto-char pos)
  9098. (setq cnt (1+ cnt))
  9099. (org-toggle-tag tag (if off 'off 'on))
  9100. (setq newhead (org-get-heading)))))
  9101. (and agendap (org-agenda-change-all-lines newhead m))))
  9102. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9103. (defun org-tags-completion-function (string predicate &optional flag)
  9104. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9105. (confirm (lambda (x) (stringp (car x)))))
  9106. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9107. (setq s1 (match-string 1 string)
  9108. s2 (match-string 2 string))
  9109. (setq s1 "" s2 string))
  9110. (cond
  9111. ((eq flag nil)
  9112. ;; try completion
  9113. (setq rtn (try-completion s2 ctable confirm))
  9114. (if (stringp rtn)
  9115. (setq rtn
  9116. (concat s1 s2 (substring rtn (length s2))
  9117. (if (and org-add-colon-after-tag-completion
  9118. (assoc rtn ctable))
  9119. ":" ""))))
  9120. rtn)
  9121. ((eq flag t)
  9122. ;; all-completions
  9123. (all-completions s2 ctable confirm)
  9124. )
  9125. ((eq flag 'lambda)
  9126. ;; exact match?
  9127. (assoc s2 ctable)))
  9128. ))
  9129. (defun org-fast-tag-insert (kwd tags face &optional end)
  9130. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9131. (insert (format "%-12s" (concat kwd ":"))
  9132. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9133. (or end "")))
  9134. (defun org-fast-tag-show-exit (flag)
  9135. (save-excursion
  9136. (goto-line 3)
  9137. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9138. (replace-match ""))
  9139. (when flag
  9140. (end-of-line 1)
  9141. (org-move-to-column (- (window-width) 19) t)
  9142. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9143. (defun org-set-current-tags-overlay (current prefix)
  9144. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9145. (if (featurep 'xemacs)
  9146. (org-overlay-display org-tags-overlay (concat prefix s)
  9147. 'secondary-selection)
  9148. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9149. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9150. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9151. "Fast tag selection with single keys.
  9152. CURRENT is the current list of tags in the headline, INHERITED is the
  9153. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9154. possibly with grouping information. TODO-TABLE is a similar table with
  9155. TODO keywords, should these have keys assigned to them.
  9156. If the keys are nil, a-z are automatically assigned.
  9157. Returns the new tags string, or nil to not change the current settings."
  9158. (let* ((fulltable (append table todo-table))
  9159. (maxlen (apply 'max (mapcar
  9160. (lambda (x)
  9161. (if (stringp (car x)) (string-width (car x)) 0))
  9162. fulltable)))
  9163. (buf (current-buffer))
  9164. (expert (eq org-fast-tag-selection-single-key 'expert))
  9165. (buffer-tags nil)
  9166. (fwidth (+ maxlen 3 1 3))
  9167. (ncol (/ (- (window-width) 4) fwidth))
  9168. (i-face 'org-done)
  9169. (c-face 'org-todo)
  9170. tg cnt e c char c1 c2 ntable tbl rtn
  9171. ov-start ov-end ov-prefix
  9172. (exit-after-next org-fast-tag-selection-single-key)
  9173. (done-keywords org-done-keywords)
  9174. groups ingroup)
  9175. (save-excursion
  9176. (beginning-of-line 1)
  9177. (if (looking-at
  9178. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9179. (setq ov-start (match-beginning 1)
  9180. ov-end (match-end 1)
  9181. ov-prefix "")
  9182. (setq ov-start (1- (point-at-eol))
  9183. ov-end (1+ ov-start))
  9184. (skip-chars-forward "^\n\r")
  9185. (setq ov-prefix
  9186. (concat
  9187. (buffer-substring (1- (point)) (point))
  9188. (if (> (current-column) org-tags-column)
  9189. " "
  9190. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9191. (org-move-overlay org-tags-overlay ov-start ov-end)
  9192. (save-window-excursion
  9193. (if expert
  9194. (set-buffer (get-buffer-create " *Org tags*"))
  9195. (delete-other-windows)
  9196. (split-window-vertically)
  9197. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9198. (erase-buffer)
  9199. (org-set-local 'org-done-keywords done-keywords)
  9200. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9201. (org-fast-tag-insert "Current" current c-face "\n\n")
  9202. (org-fast-tag-show-exit exit-after-next)
  9203. (org-set-current-tags-overlay current ov-prefix)
  9204. (setq tbl fulltable char ?a cnt 0)
  9205. (while (setq e (pop tbl))
  9206. (cond
  9207. ((equal e '(:startgroup))
  9208. (push '() groups) (setq ingroup t)
  9209. (when (not (= cnt 0))
  9210. (setq cnt 0)
  9211. (insert "\n"))
  9212. (insert "{ "))
  9213. ((equal e '(:endgroup))
  9214. (setq ingroup nil cnt 0)
  9215. (insert "}\n"))
  9216. (t
  9217. (setq tg (car e) c2 nil)
  9218. (if (cdr e)
  9219. (setq c (cdr e))
  9220. ;; automatically assign a character.
  9221. (setq c1 (string-to-char
  9222. (downcase (substring
  9223. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9224. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9225. (while (or (rassoc char ntable) (rassoc char table))
  9226. (setq char (1+ char)))
  9227. (setq c2 c1))
  9228. (setq c (or c2 char)))
  9229. (if ingroup (push tg (car groups)))
  9230. (setq tg (org-add-props tg nil 'face
  9231. (cond
  9232. ((not (assoc tg table))
  9233. (org-get-todo-face tg))
  9234. ((member tg current) c-face)
  9235. ((member tg inherited) i-face)
  9236. (t nil))))
  9237. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9238. (insert "[" c "] " tg (make-string
  9239. (- fwidth 4 (length tg)) ?\ ))
  9240. (push (cons tg c) ntable)
  9241. (when (= (setq cnt (1+ cnt)) ncol)
  9242. (insert "\n")
  9243. (if ingroup (insert " "))
  9244. (setq cnt 0)))))
  9245. (setq ntable (nreverse ntable))
  9246. (insert "\n")
  9247. (goto-char (point-min))
  9248. (if (not expert) (org-fit-window-to-buffer))
  9249. (setq rtn
  9250. (catch 'exit
  9251. (while t
  9252. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9253. (if groups " [!] no groups" " [!]groups")
  9254. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9255. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9256. (cond
  9257. ((= c ?\r) (throw 'exit t))
  9258. ((= c ?!)
  9259. (setq groups (not groups))
  9260. (goto-char (point-min))
  9261. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9262. ((= c ?\C-c)
  9263. (if (not expert)
  9264. (org-fast-tag-show-exit
  9265. (setq exit-after-next (not exit-after-next)))
  9266. (setq expert nil)
  9267. (delete-other-windows)
  9268. (split-window-vertically)
  9269. (org-switch-to-buffer-other-window " *Org tags*")
  9270. (org-fit-window-to-buffer)))
  9271. ((or (= c ?\C-g)
  9272. (and (= c ?q) (not (rassoc c ntable))))
  9273. (org-detach-overlay org-tags-overlay)
  9274. (setq quit-flag t))
  9275. ((= c ?\ )
  9276. (setq current nil)
  9277. (if exit-after-next (setq exit-after-next 'now)))
  9278. ((= c ?\t)
  9279. (condition-case nil
  9280. (setq tg (org-ido-completing-read
  9281. "Tag: "
  9282. (or buffer-tags
  9283. (with-current-buffer buf
  9284. (org-get-buffer-tags)))))
  9285. (quit (setq tg "")))
  9286. (when (string-match "\\S-" tg)
  9287. (add-to-list 'buffer-tags (list tg))
  9288. (if (member tg current)
  9289. (setq current (delete tg current))
  9290. (push tg current)))
  9291. (if exit-after-next (setq exit-after-next 'now)))
  9292. ((setq e (rassoc c todo-table) tg (car e))
  9293. (with-current-buffer buf
  9294. (save-excursion (org-todo tg)))
  9295. (if exit-after-next (setq exit-after-next 'now)))
  9296. ((setq e (rassoc c ntable) tg (car e))
  9297. (if (member tg current)
  9298. (setq current (delete tg current))
  9299. (loop for g in groups do
  9300. (if (member tg g)
  9301. (mapc (lambda (x)
  9302. (setq current (delete x current)))
  9303. g)))
  9304. (push tg current))
  9305. (if exit-after-next (setq exit-after-next 'now))))
  9306. ;; Create a sorted list
  9307. (setq current
  9308. (sort current
  9309. (lambda (a b)
  9310. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9311. (if (eq exit-after-next 'now) (throw 'exit t))
  9312. (goto-char (point-min))
  9313. (beginning-of-line 2)
  9314. (delete-region (point) (point-at-eol))
  9315. (org-fast-tag-insert "Current" current c-face)
  9316. (org-set-current-tags-overlay current ov-prefix)
  9317. (while (re-search-forward
  9318. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9319. (setq tg (match-string 1))
  9320. (add-text-properties
  9321. (match-beginning 1) (match-end 1)
  9322. (list 'face
  9323. (cond
  9324. ((member tg current) c-face)
  9325. ((member tg inherited) i-face)
  9326. (t (get-text-property (match-beginning 1) 'face))))))
  9327. (goto-char (point-min)))))
  9328. (org-detach-overlay org-tags-overlay)
  9329. (if rtn
  9330. (mapconcat 'identity current ":")
  9331. nil))))
  9332. (defun org-get-tags-string ()
  9333. "Get the TAGS string in the current headline."
  9334. (unless (org-on-heading-p t)
  9335. (error "Not on a heading"))
  9336. (save-excursion
  9337. (beginning-of-line 1)
  9338. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9339. (org-match-string-no-properties 1)
  9340. "")))
  9341. (defun org-get-tags ()
  9342. "Get the list of tags specified in the current headline."
  9343. (org-split-string (org-get-tags-string) ":"))
  9344. (defun org-get-buffer-tags ()
  9345. "Get a table of all tags used in the buffer, for completion."
  9346. (let (tags)
  9347. (save-excursion
  9348. (goto-char (point-min))
  9349. (while (re-search-forward
  9350. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9351. (when (equal (char-after (point-at-bol 0)) ?*)
  9352. (mapc (lambda (x) (add-to-list 'tags x))
  9353. (org-split-string (org-match-string-no-properties 1) ":")))))
  9354. (mapcar 'list tags)))
  9355. ;;;; The mapping API
  9356. ;;;###autoload
  9357. (defun org-map-entries (func &optional match scope &rest skip)
  9358. "Call FUNC at each headline selected by MATCH in SCOPE.
  9359. FUNC is a function or a lisp form. The function will be called without
  9360. arguments, with the cursor positioned at the beginning of the headline.
  9361. The return values of all calls to the function will be collected and
  9362. returned as a list.
  9363. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9364. Only headlines that are matched by this query will be considered during
  9365. the iteration. When MATCH is nil or t, all headlines will be
  9366. visited by the iteration.
  9367. SCOPE determines the scope of this command. It can be any of:
  9368. nil The current buffer, respecting the restriction if any
  9369. tree The subtree started with the entry at point
  9370. file The current buffer, without restriction
  9371. file-with-archives
  9372. The current buffer, and any archives associated with it
  9373. agenda All agenda files
  9374. agenda-with-archives
  9375. All agenda files with any archive files associated with them
  9376. \(file1 file2 ...)
  9377. If this is a list, all files in the list will be scanned
  9378. The remaining args are treated as settings for the skipping facilities of
  9379. the scanner. The following items can be given here:
  9380. archive skip trees with the archive tag.
  9381. comment skip trees with the COMMENT keyword
  9382. function or Emacs Lisp form:
  9383. will be used as value for `org-agenda-skip-function', so whenever
  9384. the the function returns t, FUNC will not be called for that
  9385. entry and search will continue from the point where the
  9386. function leaves it."
  9387. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9388. (org-agenda-skip-archived-trees (memq 'archive skip))
  9389. (org-agenda-skip-comment-trees (memq 'comment skip))
  9390. (org-agenda-skip-function
  9391. (car (org-delete-all '(comment archive) skip)))
  9392. (org-tags-match-list-sublevels t)
  9393. matcher file res
  9394. org-todo-keywords-for-agenda
  9395. org-done-keywords-for-agenda
  9396. org-todo-keyword-alist-for-agenda
  9397. org-tag-alist-for-agenda)
  9398. (cond
  9399. ((eq match t) (setq matcher t))
  9400. ((eq match nil) (setq matcher t))
  9401. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9402. (save-excursion
  9403. (save-restriction
  9404. (when (eq scope 'tree)
  9405. (org-back-to-heading t)
  9406. (org-narrow-to-subtree)
  9407. (setq scope nil))
  9408. (if (not scope)
  9409. (progn
  9410. (org-prepare-agenda-buffers
  9411. (list (buffer-file-name (current-buffer))))
  9412. (setq res (org-scan-tags func matcher)))
  9413. ;; Get the right scope
  9414. (cond
  9415. ((and scope (listp scope) (symbolp (car scope)))
  9416. (setq scope (eval scope)))
  9417. ((eq scope 'agenda)
  9418. (setq scope (org-agenda-files t)))
  9419. ((eq scope 'agenda-with-archives)
  9420. (setq scope (org-agenda-files t))
  9421. (setq scope (org-add-archive-files scope)))
  9422. ((eq scope 'file)
  9423. (setq scope (list (buffer-file-name))))
  9424. ((eq scope 'file-with-archives)
  9425. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9426. (org-prepare-agenda-buffers scope)
  9427. (while (setq file (pop scope))
  9428. (with-current-buffer (org-find-base-buffer-visiting file)
  9429. (save-excursion
  9430. (save-restriction
  9431. (widen)
  9432. (goto-char (point-min))
  9433. (setq res (append res (org-scan-tags func matcher))))))))))
  9434. res))
  9435. ;;;; Properties
  9436. ;;; Setting and retrieving properties
  9437. (defconst org-special-properties
  9438. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9439. "TIMESTAMP" "TIMESTAMP_IA")
  9440. "The special properties valid in Org-mode.
  9441. These are properties that are not defined in the property drawer,
  9442. but in some other way.")
  9443. (defconst org-default-properties
  9444. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9445. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9446. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9447. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9448. "Some properties that are used by Org-mode for various purposes.
  9449. Being in this list makes sure that they are offered for completion.")
  9450. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9451. "Regular expression matching the first line of a property drawer.")
  9452. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9453. "Regular expression matching the first line of a property drawer.")
  9454. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9455. "Regular expression matching the first line of a property drawer.")
  9456. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9457. "Regular expression matching the first line of a property drawer.")
  9458. (defconst org-property-drawer-re
  9459. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9460. org-property-end-re "\\)\n?")
  9461. "Matches an entire property drawer.")
  9462. (defconst org-clock-drawer-re
  9463. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9464. org-property-end-re "\\)\n?")
  9465. "Matches an entire clock drawer.")
  9466. (defun org-property-action ()
  9467. "Do an action on properties."
  9468. (interactive)
  9469. (let (c)
  9470. (org-at-property-p)
  9471. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9472. (setq c (read-char-exclusive))
  9473. (cond
  9474. ((equal c ?s)
  9475. (call-interactively 'org-set-property))
  9476. ((equal c ?d)
  9477. (call-interactively 'org-delete-property))
  9478. ((equal c ?D)
  9479. (call-interactively 'org-delete-property-globally))
  9480. ((equal c ?c)
  9481. (call-interactively 'org-compute-property-at-point))
  9482. (t (error "No such property action %c" c)))))
  9483. (defun org-at-property-p ()
  9484. "Is the cursor in a property line?"
  9485. ;; FIXME: Does not check if we are actually in the drawer.
  9486. ;; FIXME: also returns true on any drawers.....
  9487. ;; This is used by C-c C-c for property action.
  9488. (save-excursion
  9489. (beginning-of-line 1)
  9490. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9491. (defun org-get-property-block (&optional beg end force)
  9492. "Return the (beg . end) range of the body of the property drawer.
  9493. BEG and END can be beginning and end of subtree, if not given
  9494. they will be found.
  9495. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9496. (catch 'exit
  9497. (save-excursion
  9498. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9499. (end (or end (progn (outline-next-heading) (point)))))
  9500. (goto-char beg)
  9501. (if (re-search-forward org-property-start-re end t)
  9502. (setq beg (1+ (match-end 0)))
  9503. (if force
  9504. (save-excursion
  9505. (org-insert-property-drawer)
  9506. (setq end (progn (outline-next-heading) (point))))
  9507. (throw 'exit nil))
  9508. (goto-char beg)
  9509. (if (re-search-forward org-property-start-re end t)
  9510. (setq beg (1+ (match-end 0)))))
  9511. (if (re-search-forward org-property-end-re end t)
  9512. (setq end (match-beginning 0))
  9513. (or force (throw 'exit nil))
  9514. (goto-char beg)
  9515. (setq end beg)
  9516. (org-indent-line-function)
  9517. (insert ":END:\n"))
  9518. (cons beg end)))))
  9519. (defun org-entry-properties (&optional pom which)
  9520. "Get all properties of the entry at point-or-marker POM.
  9521. This includes the TODO keyword, the tags, time strings for deadline,
  9522. scheduled, and clocking, and any additional properties defined in the
  9523. entry. The return value is an alist, keys may occur multiple times
  9524. if the property key was used several times.
  9525. POM may also be nil, in which case the current entry is used.
  9526. If WHICH is nil or `all', get all properties. If WHICH is
  9527. `special' or `standard', only get that subclass."
  9528. (setq which (or which 'all))
  9529. (org-with-point-at pom
  9530. (let ((clockstr (substring org-clock-string 0 -1))
  9531. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9532. beg end range props sum-props key value string clocksum)
  9533. (save-excursion
  9534. (when (condition-case nil
  9535. (and (org-mode-p) (org-back-to-heading t))
  9536. (error nil))
  9537. (setq beg (point))
  9538. (setq sum-props (get-text-property (point) 'org-summaries))
  9539. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9540. (outline-next-heading)
  9541. (setq end (point))
  9542. (when (memq which '(all special))
  9543. ;; Get the special properties, like TODO and tags
  9544. (goto-char beg)
  9545. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9546. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9547. (when (looking-at org-priority-regexp)
  9548. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9549. (when (and (setq value (org-get-tags-string))
  9550. (string-match "\\S-" value))
  9551. (push (cons "TAGS" value) props))
  9552. (when (setq value (org-get-tags-at))
  9553. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9554. props))
  9555. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9556. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9557. string (if (equal key clockstr)
  9558. (org-no-properties
  9559. (org-trim
  9560. (buffer-substring
  9561. (match-beginning 3) (goto-char (point-at-eol)))))
  9562. (substring (org-match-string-no-properties 3) 1 -1)))
  9563. (unless key
  9564. (if (= (char-after (match-beginning 3)) ?\[)
  9565. (setq key "TIMESTAMP_IA")
  9566. (setq key "TIMESTAMP")))
  9567. (when (or (equal key clockstr) (not (assoc key props)))
  9568. (push (cons key string) props)))
  9569. )
  9570. (when (memq which '(all standard))
  9571. ;; Get the standard properties, like :PORP: ...
  9572. (setq range (org-get-property-block beg end))
  9573. (when range
  9574. (goto-char (car range))
  9575. (while (re-search-forward
  9576. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9577. (cdr range) t)
  9578. (setq key (org-match-string-no-properties 1)
  9579. value (org-trim (or (org-match-string-no-properties 2) "")))
  9580. (unless (member key excluded)
  9581. (push (cons key (or value "")) props)))))
  9582. (if clocksum
  9583. (push (cons "CLOCKSUM"
  9584. (org-columns-number-to-string (/ (float clocksum) 60.)
  9585. 'add_times))
  9586. props))
  9587. (unless (assoc "CATEGORY" props)
  9588. (setq value (or (org-get-category)
  9589. (progn (org-refresh-category-properties)
  9590. (org-get-category))))
  9591. (push (cons "CATEGORY" value) props))
  9592. (append sum-props (nreverse props)))))))
  9593. (defun org-entry-get (pom property &optional inherit)
  9594. "Get value of PROPERTY for entry at point-or-marker POM.
  9595. If INHERIT is non-nil and the entry does not have the property,
  9596. then also check higher levels of the hierarchy.
  9597. If INHERIT is the symbol `selective', use inheritance only if the setting
  9598. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9599. If the property is present but empty, the return value is the empty string.
  9600. If the property is not present at all, nil is returned."
  9601. (org-with-point-at pom
  9602. (if (and inherit (if (eq inherit 'selective)
  9603. (org-property-inherit-p property)
  9604. t))
  9605. (org-entry-get-with-inheritance property)
  9606. (if (member property org-special-properties)
  9607. ;; We need a special property. Use brute force, get all properties.
  9608. (cdr (assoc property (org-entry-properties nil 'special)))
  9609. (let ((range (org-get-property-block)))
  9610. (if (and range
  9611. (goto-char (car range))
  9612. (re-search-forward
  9613. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9614. (cdr range) t))
  9615. ;; Found the property, return it.
  9616. (if (match-end 1)
  9617. (org-match-string-no-properties 1)
  9618. "")))))))
  9619. (defun org-property-or-variable-value (var &optional inherit)
  9620. "Check if there is a property fixing the value of VAR.
  9621. If yes, return this value. If not, return the current value of the variable."
  9622. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9623. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9624. (read prop)
  9625. (symbol-value var))))
  9626. (defun org-entry-delete (pom property)
  9627. "Delete the property PROPERTY from entry at point-or-marker POM."
  9628. (org-with-point-at pom
  9629. (if (member property org-special-properties)
  9630. nil ; cannot delete these properties.
  9631. (let ((range (org-get-property-block)))
  9632. (if (and range
  9633. (goto-char (car range))
  9634. (re-search-forward
  9635. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9636. (cdr range) t))
  9637. (progn
  9638. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9639. t)
  9640. nil)))))
  9641. ;; Multi-values properties are properties that contain multiple values
  9642. ;; These values are assumed to be single words, separated by whitespace.
  9643. (defun org-entry-add-to-multivalued-property (pom property value)
  9644. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9645. (let* ((old (org-entry-get pom property))
  9646. (values (and old (org-split-string old "[ \t]"))))
  9647. (setq value (org-entry-protect-space value))
  9648. (unless (member value values)
  9649. (setq values (cons value values))
  9650. (org-entry-put pom property
  9651. (mapconcat 'identity values " ")))))
  9652. (defun org-entry-remove-from-multivalued-property (pom property value)
  9653. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9654. (let* ((old (org-entry-get pom property))
  9655. (values (and old (org-split-string old "[ \t]"))))
  9656. (setq value (org-entry-protect-space value))
  9657. (when (member value values)
  9658. (setq values (delete value values))
  9659. (org-entry-put pom property
  9660. (mapconcat 'identity values " ")))))
  9661. (defun org-entry-member-in-multivalued-property (pom property value)
  9662. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9663. (let* ((old (org-entry-get pom property))
  9664. (values (and old (org-split-string old "[ \t]"))))
  9665. (setq value (org-entry-protect-space value))
  9666. (member value values)))
  9667. (defun org-entry-get-multivalued-property (pom property)
  9668. "Return a list of values in a multivalued property."
  9669. (let* ((value (org-entry-get pom property))
  9670. (values (and value (org-split-string value "[ \t]"))))
  9671. (mapcar 'org-entry-restore-space values)))
  9672. (defun org-entry-put-multivalued-property (pom property &rest values)
  9673. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9674. VALUES should be a list of strings. Spaces will be protected."
  9675. (org-entry-put pom property
  9676. (mapconcat 'org-entry-protect-space values " "))
  9677. (let* ((value (org-entry-get pom property))
  9678. (values (and value (org-split-string value "[ \t]"))))
  9679. (mapcar 'org-entry-restore-space values)))
  9680. (defun org-entry-protect-space (s)
  9681. "Protect spaces and newline in string S."
  9682. (while (string-match " " s)
  9683. (setq s (replace-match "%20" t t s)))
  9684. (while (string-match "\n" s)
  9685. (setq s (replace-match "%0A" t t s)))
  9686. s)
  9687. (defun org-entry-restore-space (s)
  9688. "Restore spaces and newline in string S."
  9689. (while (string-match "%20" s)
  9690. (setq s (replace-match " " t t s)))
  9691. (while (string-match "%0A" s)
  9692. (setq s (replace-match "\n" t t s)))
  9693. s)
  9694. (defvar org-entry-property-inherited-from (make-marker)
  9695. "Marker pointing to the entry from where a property was inherited.
  9696. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9697. location of the entry where the inheritance search matched. If there was
  9698. no match, the marker will point nowhere.
  9699. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9700. is set.")
  9701. (defun org-entry-get-with-inheritance (property)
  9702. "Get entry property, and search higher levels if not present."
  9703. (move-marker org-entry-property-inherited-from nil)
  9704. (let (tmp)
  9705. (save-excursion
  9706. (save-restriction
  9707. (widen)
  9708. (catch 'ex
  9709. (while t
  9710. (when (setq tmp (org-entry-get nil property))
  9711. (org-back-to-heading t)
  9712. (move-marker org-entry-property-inherited-from (point))
  9713. (throw 'ex tmp))
  9714. (or (org-up-heading-safe) (throw 'ex nil)))))
  9715. (or tmp
  9716. (cdr (assoc property org-file-properties))
  9717. (cdr (assoc property org-global-properties))
  9718. (cdr (assoc property org-global-properties-fixed))))))
  9719. (defun org-entry-put (pom property value)
  9720. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9721. (org-with-point-at pom
  9722. (org-back-to-heading t)
  9723. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9724. range)
  9725. (cond
  9726. ((equal property "TODO")
  9727. (when (and (stringp value) (string-match "\\S-" value)
  9728. (not (member value org-todo-keywords-1)))
  9729. (error "\"%s\" is not a valid TODO state" value))
  9730. (if (or (not value)
  9731. (not (string-match "\\S-" value)))
  9732. (setq value 'none))
  9733. (org-todo value)
  9734. (org-set-tags nil 'align))
  9735. ((equal property "PRIORITY")
  9736. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9737. (string-to-char value) ?\ ))
  9738. (org-set-tags nil 'align))
  9739. ((equal property "SCHEDULED")
  9740. (if (re-search-forward org-scheduled-time-regexp end t)
  9741. (cond
  9742. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9743. ((eq value 'later) (org-timestamp-change 1 'day))
  9744. (t (call-interactively 'org-schedule)))
  9745. (call-interactively 'org-schedule)))
  9746. ((equal property "DEADLINE")
  9747. (if (re-search-forward org-deadline-time-regexp end t)
  9748. (cond
  9749. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9750. ((eq value 'later) (org-timestamp-change 1 'day))
  9751. (t (call-interactively 'org-deadline)))
  9752. (call-interactively 'org-deadline)))
  9753. ((member property org-special-properties)
  9754. (error "The %s property can not yet be set with `org-entry-put'"
  9755. property))
  9756. (t ; a non-special property
  9757. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9758. (setq range (org-get-property-block beg end 'force))
  9759. (goto-char (car range))
  9760. (if (re-search-forward
  9761. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9762. (progn
  9763. (delete-region (match-beginning 1) (match-end 1))
  9764. (goto-char (match-beginning 1)))
  9765. (goto-char (cdr range))
  9766. (insert "\n")
  9767. (backward-char 1)
  9768. (org-indent-line-function)
  9769. (insert ":" property ":"))
  9770. (and value (insert " " value))
  9771. (org-indent-line-function)))))))
  9772. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9773. "Get all property keys in the current buffer.
  9774. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9775. like tags and TODO state.
  9776. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9777. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9778. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9779. formats in the current buffer."
  9780. (let (rtn range cfmt s p)
  9781. (save-excursion
  9782. (save-restriction
  9783. (widen)
  9784. (goto-char (point-min))
  9785. (while (re-search-forward org-property-start-re nil t)
  9786. (setq range (org-get-property-block))
  9787. (goto-char (car range))
  9788. (while (re-search-forward
  9789. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9790. (cdr range) t)
  9791. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9792. (outline-next-heading))))
  9793. (when include-specials
  9794. (setq rtn (append org-special-properties rtn)))
  9795. (when include-defaults
  9796. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9797. (when include-columns
  9798. (save-excursion
  9799. (save-restriction
  9800. (widen)
  9801. (goto-char (point-min))
  9802. (while (re-search-forward
  9803. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9804. nil t)
  9805. (setq cfmt (match-string 2) s 0)
  9806. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9807. cfmt s)
  9808. (setq s (match-end 0)
  9809. p (match-string 1 cfmt))
  9810. (unless (or (equal p "ITEM")
  9811. (member p org-special-properties))
  9812. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9813. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9814. (defun org-property-values (key)
  9815. "Return a list of all values of property KEY."
  9816. (save-excursion
  9817. (save-restriction
  9818. (widen)
  9819. (goto-char (point-min))
  9820. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9821. values)
  9822. (while (re-search-forward re nil t)
  9823. (add-to-list 'values (org-trim (match-string 1))))
  9824. (delete "" values)))))
  9825. (defun org-insert-property-drawer ()
  9826. "Insert a property drawer into the current entry."
  9827. (interactive)
  9828. (org-back-to-heading t)
  9829. (looking-at outline-regexp)
  9830. (let ((indent (- (match-end 0)(match-beginning 0)))
  9831. (beg (point))
  9832. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9833. end hiddenp)
  9834. (outline-next-heading)
  9835. (setq end (point))
  9836. (goto-char beg)
  9837. (while (re-search-forward re end t))
  9838. (setq hiddenp (org-invisible-p))
  9839. (end-of-line 1)
  9840. (and (equal (char-after) ?\n) (forward-char 1))
  9841. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9842. (beginning-of-line 2))
  9843. (org-skip-over-state-notes)
  9844. (skip-chars-backward " \t\n\r")
  9845. (if (eq (char-before) ?*) (forward-char 1))
  9846. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9847. (beginning-of-line 0)
  9848. (org-indent-to-column indent)
  9849. (beginning-of-line 2)
  9850. (org-indent-to-column indent)
  9851. (beginning-of-line 0)
  9852. (if hiddenp
  9853. (save-excursion
  9854. (org-back-to-heading t)
  9855. (hide-entry))
  9856. (org-flag-drawer t))))
  9857. (defun org-set-property (property value)
  9858. "In the current entry, set PROPERTY to VALUE.
  9859. When called interactively, this will prompt for a property name, offering
  9860. completion on existing and default properties. And then it will prompt
  9861. for a value, offering completion either on allowed values (via an inherited
  9862. xxx_ALL property) or on existing values in other instances of this property
  9863. in the current file."
  9864. (interactive
  9865. (let* ((completion-ignore-case t)
  9866. (keys (org-buffer-property-keys nil t t))
  9867. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9868. (prop (if (member prop0 keys)
  9869. prop0
  9870. (or (cdr (assoc (downcase prop0)
  9871. (mapcar (lambda (x) (cons (downcase x) x))
  9872. keys)))
  9873. prop0)))
  9874. (cur (org-entry-get nil prop))
  9875. (allowed (org-property-get-allowed-values nil prop 'table))
  9876. (existing (mapcar 'list (org-property-values prop)))
  9877. (val (if allowed
  9878. (org-completing-read "Value: " allowed nil 'req-match)
  9879. (org-completing-read
  9880. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9881. (concat "[" cur "]") "")
  9882. ": ")
  9883. existing nil nil "" nil cur))))
  9884. (list prop (if (equal val "") cur val))))
  9885. (unless (equal (org-entry-get nil property) value)
  9886. (org-entry-put nil property value)))
  9887. (defun org-delete-property (property)
  9888. "In the current entry, delete PROPERTY."
  9889. (interactive
  9890. (let* ((completion-ignore-case t)
  9891. (prop (org-ido-completing-read
  9892. "Property: " (org-entry-properties nil 'standard))))
  9893. (list prop)))
  9894. (message "Property %s %s" property
  9895. (if (org-entry-delete nil property)
  9896. "deleted"
  9897. "was not present in the entry")))
  9898. (defun org-delete-property-globally (property)
  9899. "Remove PROPERTY globally, from all entries."
  9900. (interactive
  9901. (let* ((completion-ignore-case t)
  9902. (prop (org-ido-completing-read
  9903. "Globally remove property: "
  9904. (mapcar 'list (org-buffer-property-keys)))))
  9905. (list prop)))
  9906. (save-excursion
  9907. (save-restriction
  9908. (widen)
  9909. (goto-char (point-min))
  9910. (let ((cnt 0))
  9911. (while (re-search-forward
  9912. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9913. nil t)
  9914. (setq cnt (1+ cnt))
  9915. (replace-match ""))
  9916. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9917. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9918. (defun org-compute-property-at-point ()
  9919. "Compute the property at point.
  9920. This looks for an enclosing column format, extracts the operator and
  9921. then applies it to the property in the column format's scope."
  9922. (interactive)
  9923. (unless (org-at-property-p)
  9924. (error "Not at a property"))
  9925. (let ((prop (org-match-string-no-properties 2)))
  9926. (org-columns-get-format-and-top-level)
  9927. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9928. (error "No operator defined for property %s" prop))
  9929. (org-columns-compute prop)))
  9930. (defun org-property-get-allowed-values (pom property &optional table)
  9931. "Get allowed values for the property PROPERTY.
  9932. When TABLE is non-nil, return an alist that can directly be used for
  9933. completion."
  9934. (let (vals)
  9935. (cond
  9936. ((equal property "TODO")
  9937. (setq vals (org-with-point-at pom
  9938. (append org-todo-keywords-1 '("")))))
  9939. ((equal property "PRIORITY")
  9940. (let ((n org-lowest-priority))
  9941. (while (>= n org-highest-priority)
  9942. (push (char-to-string n) vals)
  9943. (setq n (1- n)))))
  9944. ((member property org-special-properties))
  9945. (t
  9946. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9947. (when (and vals (string-match "\\S-" vals))
  9948. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9949. (setq vals (mapcar (lambda (x)
  9950. (cond ((stringp x) x)
  9951. ((numberp x) (number-to-string x))
  9952. ((symbolp x) (symbol-name x))
  9953. (t "???")))
  9954. vals)))))
  9955. (if table (mapcar 'list vals) vals)))
  9956. (defun org-property-previous-allowed-value (&optional previous)
  9957. "Switch to the next allowed value for this property."
  9958. (interactive)
  9959. (org-property-next-allowed-value t))
  9960. (defun org-property-next-allowed-value (&optional previous)
  9961. "Switch to the next allowed value for this property."
  9962. (interactive)
  9963. (unless (org-at-property-p)
  9964. (error "Not at a property"))
  9965. (let* ((key (match-string 2))
  9966. (value (match-string 3))
  9967. (allowed (or (org-property-get-allowed-values (point) key)
  9968. (and (member value '("[ ]" "[-]" "[X]"))
  9969. '("[ ]" "[X]"))))
  9970. nval)
  9971. (unless allowed
  9972. (error "Allowed values for this property have not been defined"))
  9973. (if previous (setq allowed (reverse allowed)))
  9974. (if (member value allowed)
  9975. (setq nval (car (cdr (member value allowed)))))
  9976. (setq nval (or nval (car allowed)))
  9977. (if (equal nval value)
  9978. (error "Only one allowed value for this property"))
  9979. (org-at-property-p)
  9980. (replace-match (concat " :" key ": " nval) t t)
  9981. (org-indent-line-function)
  9982. (beginning-of-line 1)
  9983. (skip-chars-forward " \t")))
  9984. (defun org-find-entry-with-id (ident)
  9985. "Locate the entry that contains the ID property with exact value IDENT.
  9986. IDENT can be a string, a symbol or a number, this function will search for
  9987. the string representation of it.
  9988. Return the position where this entry starts, or nil if there is no such entry."
  9989. (interactive "sID: ")
  9990. (let ((id (cond
  9991. ((stringp ident) ident)
  9992. ((symbol-name ident) (symbol-name ident))
  9993. ((numberp ident) (number-to-string ident))
  9994. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9995. (case-fold-search nil))
  9996. (save-excursion
  9997. (save-restriction
  9998. (widen)
  9999. (goto-char (point-min))
  10000. (when (re-search-forward
  10001. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10002. nil t)
  10003. (org-back-to-heading)
  10004. (point))))))
  10005. ;;;; Timestamps
  10006. (defvar org-last-changed-timestamp nil)
  10007. (defvar org-last-inserted-timestamp nil
  10008. "The last time stamp inserted with `org-insert-time-stamp'.")
  10009. (defvar org-time-was-given) ; dynamically scoped parameter
  10010. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10011. (defvar org-ts-what) ; dynamically scoped parameter
  10012. (defun org-time-stamp (arg &optional inactive)
  10013. "Prompt for a date/time and insert a time stamp.
  10014. If the user specifies a time like HH:MM, or if this command is called
  10015. with a prefix argument, the time stamp will contain date and time.
  10016. Otherwise, only the date will be included. All parts of a date not
  10017. specified by the user will be filled in from the current date/time.
  10018. So if you press just return without typing anything, the time stamp
  10019. will represent the current date/time. If there is already a timestamp
  10020. at the cursor, it will be modified."
  10021. (interactive "P")
  10022. (let* ((ts nil)
  10023. (default-time
  10024. ;; Default time is either today, or, when entering a range,
  10025. ;; the range start.
  10026. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10027. (save-excursion
  10028. (re-search-backward
  10029. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10030. (- (point) 20) t)))
  10031. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10032. (current-time)))
  10033. (default-input (and ts (org-get-compact-tod ts)))
  10034. org-time-was-given org-end-time-was-given time)
  10035. (cond
  10036. ((and (org-at-timestamp-p t)
  10037. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10038. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10039. (insert "--")
  10040. (setq time (let ((this-command this-command))
  10041. (org-read-date arg 'totime nil nil
  10042. default-time default-input)))
  10043. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10044. ((org-at-timestamp-p t)
  10045. (setq time (let ((this-command this-command))
  10046. (org-read-date arg 'totime nil nil default-time default-input)))
  10047. (when (org-at-timestamp-p t) ; just to get the match data
  10048. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10049. (replace-match "")
  10050. (setq org-last-changed-timestamp
  10051. (org-insert-time-stamp
  10052. time (or org-time-was-given arg)
  10053. inactive nil nil (list org-end-time-was-given))))
  10054. (message "Timestamp updated"))
  10055. (t
  10056. (setq time (let ((this-command this-command))
  10057. (org-read-date arg 'totime nil nil default-time default-input)))
  10058. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10059. nil nil (list org-end-time-was-given))))))
  10060. ;; FIXME: can we use this for something else, like computing time differences?
  10061. (defun org-get-compact-tod (s)
  10062. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10063. (let* ((t1 (match-string 1 s))
  10064. (h1 (string-to-number (match-string 2 s)))
  10065. (m1 (string-to-number (match-string 3 s)))
  10066. (t2 (and (match-end 4) (match-string 5 s)))
  10067. (h2 (and t2 (string-to-number (match-string 6 s))))
  10068. (m2 (and t2 (string-to-number (match-string 7 s))))
  10069. dh dm)
  10070. (if (not t2)
  10071. t1
  10072. (setq dh (- h2 h1) dm (- m2 m1))
  10073. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10074. (concat t1 "+" (number-to-string dh)
  10075. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10076. (defun org-time-stamp-inactive (&optional arg)
  10077. "Insert an inactive time stamp.
  10078. An inactive time stamp is enclosed in square brackets instead of angle
  10079. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10080. does not link to the calendar and cannot be changed with the S-cursor keys.
  10081. So these are more for recording a certain time/date."
  10082. (interactive "P")
  10083. (org-time-stamp arg 'inactive))
  10084. (defvar org-date-ovl (org-make-overlay 1 1))
  10085. (org-overlay-put org-date-ovl 'face 'org-warning)
  10086. (org-detach-overlay org-date-ovl)
  10087. (defvar org-ans1) ; dynamically scoped parameter
  10088. (defvar org-ans2) ; dynamically scoped parameter
  10089. (defvar org-plain-time-of-day-regexp) ; defined below
  10090. (defvar org-overriding-default-time nil) ; dynamically scoped
  10091. (defvar org-read-date-overlay nil)
  10092. (defvar org-dcst nil) ; dynamically scoped
  10093. (defun org-read-date (&optional with-time to-time from-string prompt
  10094. default-time default-input)
  10095. "Read a date, possibly a time, and make things smooth for the user.
  10096. The prompt will suggest to enter an ISO date, but you can also enter anything
  10097. which will at least partially be understood by `parse-time-string'.
  10098. Unrecognized parts of the date will default to the current day, month, year,
  10099. hour and minute. If this command is called to replace a timestamp at point,
  10100. of to enter the second timestamp of a range, the default time is taken from the
  10101. existing stamp. For example,
  10102. 3-2-5 --> 2003-02-05
  10103. feb 15 --> currentyear-02-15
  10104. sep 12 9 --> 2009-09-12
  10105. 12:45 --> today 12:45
  10106. 22 sept 0:34 --> currentyear-09-22 0:34
  10107. 12 --> currentyear-currentmonth-12
  10108. Fri --> nearest Friday (today or later)
  10109. etc.
  10110. Furthermore you can specify a relative date by giving, as the *first* thing
  10111. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10112. change in days weeks, months, years.
  10113. With a single plus or minus, the date is relative to today. With a double
  10114. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10115. +4d --> four days from today
  10116. +4 --> same as above
  10117. +2w --> two weeks from today
  10118. ++5 --> five days from default date
  10119. The function understands only English month and weekday abbreviations,
  10120. but this can be configured with the variables `parse-time-months' and
  10121. `parse-time-weekdays'.
  10122. While prompting, a calendar is popped up - you can also select the
  10123. date with the mouse (button 1). The calendar shows a period of three
  10124. months. To scroll it to other months, use the keys `>' and `<'.
  10125. If you don't like the calendar, turn it off with
  10126. \(setq org-read-date-popup-calendar nil)
  10127. With optional argument TO-TIME, the date will immediately be converted
  10128. to an internal time.
  10129. With an optional argument WITH-TIME, the prompt will suggest to also
  10130. insert a time. Note that when WITH-TIME is not set, you can still
  10131. enter a time, and this function will inform the calling routine about
  10132. this change. The calling routine may then choose to change the format
  10133. used to insert the time stamp into the buffer to include the time.
  10134. With optional argument FROM-STRING, read from this string instead from
  10135. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10136. the time/date that is used for everything that is not specified by the
  10137. user."
  10138. (require 'parse-time)
  10139. (let* ((org-time-stamp-rounding-minutes
  10140. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10141. (org-dcst org-display-custom-times)
  10142. (ct (org-current-time))
  10143. (def (or org-overriding-default-time default-time ct))
  10144. (defdecode (decode-time def))
  10145. (dummy (progn
  10146. (when (< (nth 2 defdecode) org-extend-today-until)
  10147. (setcar (nthcdr 2 defdecode) -1)
  10148. (setcar (nthcdr 1 defdecode) 59)
  10149. (setq def (apply 'encode-time defdecode)
  10150. defdecode (decode-time def)))))
  10151. (calendar-move-hook nil)
  10152. (calendar-view-diary-initially-flag nil)
  10153. (view-diary-entries-initially nil)
  10154. (calendar-view-holidays-initially-flag nil)
  10155. (view-calendar-holidays-initially nil)
  10156. (timestr (format-time-string
  10157. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10158. (prompt (concat (if prompt (concat prompt " ") "")
  10159. (format "Date+time [%s]: " timestr)))
  10160. ans (org-ans0 "") org-ans1 org-ans2 final)
  10161. (cond
  10162. (from-string (setq ans from-string))
  10163. (org-read-date-popup-calendar
  10164. (save-excursion
  10165. (save-window-excursion
  10166. (calendar)
  10167. (calendar-forward-day (- (time-to-days def)
  10168. (calendar-absolute-from-gregorian
  10169. (calendar-current-date))))
  10170. (org-eval-in-calendar nil t)
  10171. (let* ((old-map (current-local-map))
  10172. (map (copy-keymap calendar-mode-map))
  10173. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10174. (org-defkey map (kbd "RET") 'org-calendar-select)
  10175. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10176. 'org-calendar-select-mouse)
  10177. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10178. 'org-calendar-select-mouse)
  10179. (org-defkey minibuffer-local-map [(meta shift left)]
  10180. (lambda () (interactive)
  10181. (org-eval-in-calendar '(calendar-backward-month 1))))
  10182. (org-defkey minibuffer-local-map [(meta shift right)]
  10183. (lambda () (interactive)
  10184. (org-eval-in-calendar '(calendar-forward-month 1))))
  10185. (org-defkey minibuffer-local-map [(meta shift up)]
  10186. (lambda () (interactive)
  10187. (org-eval-in-calendar '(calendar-backward-year 1))))
  10188. (org-defkey minibuffer-local-map [(meta shift down)]
  10189. (lambda () (interactive)
  10190. (org-eval-in-calendar '(calendar-forward-year 1))))
  10191. (org-defkey minibuffer-local-map [(shift up)]
  10192. (lambda () (interactive)
  10193. (org-eval-in-calendar '(calendar-backward-week 1))))
  10194. (org-defkey minibuffer-local-map [(shift down)]
  10195. (lambda () (interactive)
  10196. (org-eval-in-calendar '(calendar-forward-week 1))))
  10197. (org-defkey minibuffer-local-map [(shift left)]
  10198. (lambda () (interactive)
  10199. (org-eval-in-calendar '(calendar-backward-day 1))))
  10200. (org-defkey minibuffer-local-map [(shift right)]
  10201. (lambda () (interactive)
  10202. (org-eval-in-calendar '(calendar-forward-day 1))))
  10203. (org-defkey minibuffer-local-map ">"
  10204. (lambda () (interactive)
  10205. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10206. (org-defkey minibuffer-local-map "<"
  10207. (lambda () (interactive)
  10208. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10209. (unwind-protect
  10210. (progn
  10211. (use-local-map map)
  10212. (add-hook 'post-command-hook 'org-read-date-display)
  10213. (setq org-ans0 (read-string prompt default-input nil nil))
  10214. ;; org-ans0: from prompt
  10215. ;; org-ans1: from mouse click
  10216. ;; org-ans2: from calendar motion
  10217. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10218. (remove-hook 'post-command-hook 'org-read-date-display)
  10219. (use-local-map old-map)
  10220. (when org-read-date-overlay
  10221. (org-delete-overlay org-read-date-overlay)
  10222. (setq org-read-date-overlay nil)))))))
  10223. (t ; Naked prompt only
  10224. (unwind-protect
  10225. (setq ans (read-string prompt default-input nil timestr))
  10226. (when org-read-date-overlay
  10227. (org-delete-overlay org-read-date-overlay)
  10228. (setq org-read-date-overlay nil)))))
  10229. (setq final (org-read-date-analyze ans def defdecode))
  10230. (if to-time
  10231. (apply 'encode-time final)
  10232. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10233. (format "%04d-%02d-%02d %02d:%02d"
  10234. (nth 5 final) (nth 4 final) (nth 3 final)
  10235. (nth 2 final) (nth 1 final))
  10236. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10237. (defvar def)
  10238. (defvar defdecode)
  10239. (defvar with-time)
  10240. (defun org-read-date-display ()
  10241. "Display the current date prompt interpretation in the minibuffer."
  10242. (when org-read-date-display-live
  10243. (when org-read-date-overlay
  10244. (org-delete-overlay org-read-date-overlay))
  10245. (let ((p (point)))
  10246. (end-of-line 1)
  10247. (while (not (equal (buffer-substring
  10248. (max (point-min) (- (point) 4)) (point))
  10249. " "))
  10250. (insert " "))
  10251. (goto-char p))
  10252. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10253. " " (or org-ans1 org-ans2)))
  10254. (org-end-time-was-given nil)
  10255. (f (org-read-date-analyze ans def defdecode))
  10256. (fmts (if org-dcst
  10257. org-time-stamp-custom-formats
  10258. org-time-stamp-formats))
  10259. (fmt (if (or with-time
  10260. (and (boundp 'org-time-was-given) org-time-was-given))
  10261. (cdr fmts)
  10262. (car fmts)))
  10263. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10264. (when (and org-end-time-was-given
  10265. (string-match org-plain-time-of-day-regexp txt))
  10266. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10267. org-end-time-was-given
  10268. (substring txt (match-end 0)))))
  10269. (setq org-read-date-overlay
  10270. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10271. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10272. (defun org-read-date-analyze (ans def defdecode)
  10273. "Analyse the combined answer of the date prompt."
  10274. ;; FIXME: cleanup and comment
  10275. (let (delta deltan deltaw deltadef year month day
  10276. hour minute second wday pm h2 m2 tl wday1
  10277. iso-year iso-weekday iso-week iso-year iso-date)
  10278. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10279. (setq ans "+0"))
  10280. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10281. (setq ans (replace-match "" t t ans)
  10282. deltan (car delta)
  10283. deltaw (nth 1 delta)
  10284. deltadef (nth 2 delta)))
  10285. ;; Check if there is an iso week date in there
  10286. ;; If yes, sore the info and postpone interpreting it until the rest
  10287. ;; of the parsing is done
  10288. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10289. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10290. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10291. iso-week (string-to-number (match-string 2 ans)))
  10292. (setq ans (replace-match "" t t ans)))
  10293. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10294. (when (string-match
  10295. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10296. (setq year (if (match-end 2)
  10297. (string-to-number (match-string 2 ans))
  10298. (string-to-number (format-time-string "%Y")))
  10299. month (string-to-number (match-string 3 ans))
  10300. day (string-to-number (match-string 4 ans)))
  10301. (if (< year 100) (setq year (+ 2000 year)))
  10302. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10303. t nil ans)))
  10304. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10305. ;; If there is a time with am/pm, and *no* time without it, we convert
  10306. ;; so that matching will be successful.
  10307. (loop for i from 1 to 2 do ; twice, for end time as well
  10308. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10309. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10310. (setq hour (string-to-number (match-string 1 ans))
  10311. minute (if (match-end 3)
  10312. (string-to-number (match-string 3 ans))
  10313. 0)
  10314. pm (equal ?p
  10315. (string-to-char (downcase (match-string 4 ans)))))
  10316. (if (and (= hour 12) (not pm))
  10317. (setq hour 0)
  10318. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10319. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10320. t t ans))))
  10321. ;; Check if a time range is given as a duration
  10322. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10323. (setq hour (string-to-number (match-string 1 ans))
  10324. h2 (+ hour (string-to-number (match-string 3 ans)))
  10325. minute (string-to-number (match-string 2 ans))
  10326. m2 (+ minute (if (match-end 5) (string-to-number
  10327. (match-string 5 ans))0)))
  10328. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10329. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10330. t t ans)))
  10331. ;; Check if there is a time range
  10332. (when (boundp 'org-end-time-was-given)
  10333. (setq org-time-was-given nil)
  10334. (when (and (string-match org-plain-time-of-day-regexp ans)
  10335. (match-end 8))
  10336. (setq org-end-time-was-given (match-string 8 ans))
  10337. (setq ans (concat (substring ans 0 (match-beginning 7))
  10338. (substring ans (match-end 7))))))
  10339. (setq tl (parse-time-string ans)
  10340. day (or (nth 3 tl) (nth 3 defdecode))
  10341. month (or (nth 4 tl)
  10342. (if (and org-read-date-prefer-future
  10343. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10344. (1+ (nth 4 defdecode))
  10345. (nth 4 defdecode)))
  10346. year (or (nth 5 tl)
  10347. (if (and org-read-date-prefer-future
  10348. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10349. (1+ (nth 5 defdecode))
  10350. (nth 5 defdecode)))
  10351. hour (or (nth 2 tl) (nth 2 defdecode))
  10352. minute (or (nth 1 tl) (nth 1 defdecode))
  10353. second (or (nth 0 tl) 0)
  10354. wday (nth 6 tl))
  10355. ;; Special date definitions below
  10356. (cond
  10357. (iso-week
  10358. ;; There was an iso week
  10359. (setq year (or iso-year year)
  10360. day (or iso-weekday wday 1)
  10361. wday nil ; to make sure that the trigger below does not match
  10362. iso-date (calendar-gregorian-from-absolute
  10363. (calendar-absolute-from-iso
  10364. (list iso-week day year))))
  10365. ; FIXME: Should we also push ISO weeks into the future?
  10366. ; (when (and org-read-date-prefer-future
  10367. ; (not iso-year)
  10368. ; (< (calendar-absolute-from-gregorian iso-date)
  10369. ; (time-to-days (current-time))))
  10370. ; (setq year (1+ year)
  10371. ; iso-date (calendar-gregorian-from-absolute
  10372. ; (calendar-absolute-from-iso
  10373. ; (list iso-week day year)))))
  10374. (setq month (car iso-date)
  10375. year (nth 2 iso-date)
  10376. day (nth 1 iso-date)))
  10377. (deltan
  10378. (unless deltadef
  10379. (let ((now (decode-time (current-time))))
  10380. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10381. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10382. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10383. ((equal deltaw "m") (setq month (+ month deltan)))
  10384. ((equal deltaw "y") (setq year (+ year deltan)))))
  10385. ((and wday (not (nth 3 tl)))
  10386. ;; Weekday was given, but no day, so pick that day in the week
  10387. ;; on or after the derived date.
  10388. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10389. (unless (equal wday wday1)
  10390. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10391. (if (and (boundp 'org-time-was-given)
  10392. (nth 2 tl))
  10393. (setq org-time-was-given t))
  10394. (if (< year 100) (setq year (+ 2000 year)))
  10395. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10396. (list second minute hour day month year)))
  10397. (defvar parse-time-weekdays)
  10398. (defun org-read-date-get-relative (s today default)
  10399. "Check string S for special relative date string.
  10400. TODAY and DEFAULT are internal times, for today and for a default.
  10401. Return shift list (N what def-flag)
  10402. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10403. N is the number of WHATs to shift.
  10404. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10405. the DEFAULT date rather than TODAY."
  10406. (when (and
  10407. (string-match
  10408. (concat
  10409. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10410. "\\([0-9]+\\)?"
  10411. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10412. "\\([ \t]\\|$\\)") s)
  10413. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10414. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10415. (string-to-char (substring (match-string 1 s) -1))
  10416. ?+))
  10417. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10418. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10419. (what (if (match-end 3) (match-string 3 s) "d"))
  10420. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10421. (date (if rel default today))
  10422. (wday (nth 6 (decode-time date)))
  10423. delta)
  10424. (if wday1
  10425. (progn
  10426. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10427. (if (= dir ?-) (setq delta (- delta 7)))
  10428. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10429. (list delta "d" rel))
  10430. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10431. (defun org-eval-in-calendar (form &optional keepdate)
  10432. "Eval FORM in the calendar window and return to current window.
  10433. Also, store the cursor date in variable org-ans2."
  10434. (let ((sw (selected-window)))
  10435. (select-window (get-buffer-window "*Calendar*"))
  10436. (eval form)
  10437. (when (and (not keepdate) (calendar-cursor-to-date))
  10438. (let* ((date (calendar-cursor-to-date))
  10439. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10440. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10441. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10442. (select-window sw)))
  10443. (defun org-calendar-select ()
  10444. "Return to `org-read-date' with the date currently selected.
  10445. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10446. (interactive)
  10447. (when (calendar-cursor-to-date)
  10448. (let* ((date (calendar-cursor-to-date))
  10449. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10450. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10451. (if (active-minibuffer-window) (exit-minibuffer))))
  10452. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10453. "Insert a date stamp for the date given by the internal TIME.
  10454. WITH-HM means, use the stamp format that includes the time of the day.
  10455. INACTIVE means use square brackets instead of angular ones, so that the
  10456. stamp will not contribute to the agenda.
  10457. PRE and POST are optional strings to be inserted before and after the
  10458. stamp.
  10459. The command returns the inserted time stamp."
  10460. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10461. stamp)
  10462. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10463. (insert-before-markers (or pre ""))
  10464. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10465. (when (listp extra)
  10466. (setq extra (car extra))
  10467. (if (and (stringp extra)
  10468. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10469. (setq extra (format "-%02d:%02d"
  10470. (string-to-number (match-string 1 extra))
  10471. (string-to-number (match-string 2 extra))))
  10472. (setq extra nil)))
  10473. (when extra
  10474. (backward-char 1)
  10475. (insert-before-markers extra)
  10476. (forward-char 1))
  10477. (insert-before-markers (or post ""))
  10478. (setq org-last-inserted-timestamp stamp)))
  10479. (defun org-toggle-time-stamp-overlays ()
  10480. "Toggle the use of custom time stamp formats."
  10481. (interactive)
  10482. (setq org-display-custom-times (not org-display-custom-times))
  10483. (unless org-display-custom-times
  10484. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10485. (while (setq p (next-single-property-change p 'display))
  10486. (if (and (get-text-property p 'display)
  10487. (eq (get-text-property p 'face) 'org-date))
  10488. (remove-text-properties
  10489. p (setq p (next-single-property-change p 'display))
  10490. '(display t))))
  10491. (set-buffer-modified-p bmp)))
  10492. (if (featurep 'xemacs)
  10493. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10494. (org-restart-font-lock)
  10495. (setq org-table-may-need-update t)
  10496. (if org-display-custom-times
  10497. (message "Time stamps are overlayed with custom format")
  10498. (message "Time stamp overlays removed")))
  10499. (defun org-display-custom-time (beg end)
  10500. "Overlay modified time stamp format over timestamp between BEG and END."
  10501. (let* ((ts (buffer-substring beg end))
  10502. t1 w1 with-hm tf time str w2 (off 0))
  10503. (save-match-data
  10504. (setq t1 (org-parse-time-string ts t))
  10505. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10506. (setq off (- (match-end 0) (match-beginning 0)))))
  10507. (setq end (- end off))
  10508. (setq w1 (- end beg)
  10509. with-hm (and (nth 1 t1) (nth 2 t1))
  10510. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10511. time (org-fix-decoded-time t1)
  10512. str (org-add-props
  10513. (format-time-string
  10514. (substring tf 1 -1) (apply 'encode-time time))
  10515. nil 'mouse-face 'highlight)
  10516. w2 (length str))
  10517. (if (not (= w2 w1))
  10518. (add-text-properties (1+ beg) (+ 2 beg)
  10519. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10520. (if (featurep 'xemacs)
  10521. (progn
  10522. (put-text-property beg end 'invisible t)
  10523. (put-text-property beg end 'end-glyph (make-glyph str)))
  10524. (put-text-property beg end 'display str))))
  10525. (defun org-translate-time (string)
  10526. "Translate all timestamps in STRING to custom format.
  10527. But do this only if the variable `org-display-custom-times' is set."
  10528. (when org-display-custom-times
  10529. (save-match-data
  10530. (let* ((start 0)
  10531. (re org-ts-regexp-both)
  10532. t1 with-hm inactive tf time str beg end)
  10533. (while (setq start (string-match re string start))
  10534. (setq beg (match-beginning 0)
  10535. end (match-end 0)
  10536. t1 (save-match-data
  10537. (org-parse-time-string (substring string beg end) t))
  10538. with-hm (and (nth 1 t1) (nth 2 t1))
  10539. inactive (equal (substring string beg (1+ beg)) "[")
  10540. tf (funcall (if with-hm 'cdr 'car)
  10541. org-time-stamp-custom-formats)
  10542. time (org-fix-decoded-time t1)
  10543. str (format-time-string
  10544. (concat
  10545. (if inactive "[" "<") (substring tf 1 -1)
  10546. (if inactive "]" ">"))
  10547. (apply 'encode-time time))
  10548. string (replace-match str t t string)
  10549. start (+ start (length str)))))))
  10550. string)
  10551. (defun org-fix-decoded-time (time)
  10552. "Set 0 instead of nil for the first 6 elements of time.
  10553. Don't touch the rest."
  10554. (let ((n 0))
  10555. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10556. (defun org-days-to-time (timestamp-string)
  10557. "Difference between TIMESTAMP-STRING and now in days."
  10558. (- (time-to-days (org-time-string-to-time timestamp-string))
  10559. (time-to-days (current-time))))
  10560. (defun org-deadline-close (timestamp-string &optional ndays)
  10561. "Is the time in TIMESTAMP-STRING close to the current date?"
  10562. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10563. (and (< (org-days-to-time timestamp-string) ndays)
  10564. (not (org-entry-is-done-p))))
  10565. (defun org-get-wdays (ts)
  10566. "Get the deadline lead time appropriate for timestring TS."
  10567. (cond
  10568. ((<= org-deadline-warning-days 0)
  10569. ;; 0 or negative, enforce this value no matter what
  10570. (- org-deadline-warning-days))
  10571. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10572. ;; lead time is specified.
  10573. (floor (* (string-to-number (match-string 1 ts))
  10574. (cdr (assoc (match-string 2 ts)
  10575. '(("d" . 1) ("w" . 7)
  10576. ("m" . 30.4) ("y" . 365.25)))))))
  10577. ;; go for the default.
  10578. (t org-deadline-warning-days)))
  10579. (defun org-calendar-select-mouse (ev)
  10580. "Return to `org-read-date' with the date currently selected.
  10581. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10582. (interactive "e")
  10583. (mouse-set-point ev)
  10584. (when (calendar-cursor-to-date)
  10585. (let* ((date (calendar-cursor-to-date))
  10586. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10587. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10588. (if (active-minibuffer-window) (exit-minibuffer))))
  10589. (defun org-check-deadlines (ndays)
  10590. "Check if there are any deadlines due or past due.
  10591. A deadline is considered due if it happens within `org-deadline-warning-days'
  10592. days from today's date. If the deadline appears in an entry marked DONE,
  10593. it is not shown. The prefix arg NDAYS can be used to test that many
  10594. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10595. (interactive "P")
  10596. (let* ((org-warn-days
  10597. (cond
  10598. ((equal ndays '(4)) 100000)
  10599. (ndays (prefix-numeric-value ndays))
  10600. (t (abs org-deadline-warning-days))))
  10601. (case-fold-search nil)
  10602. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10603. (callback
  10604. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10605. (message "%d deadlines past-due or due within %d days"
  10606. (org-occur regexp nil callback)
  10607. org-warn-days)))
  10608. (defun org-check-before-date (date)
  10609. "Check if there are deadlines or scheduled entries before DATE."
  10610. (interactive (list (org-read-date)))
  10611. (let ((case-fold-search nil)
  10612. (regexp (concat "\\<\\(" org-deadline-string
  10613. "\\|" org-scheduled-string
  10614. "\\) *<\\([^>]+\\)>"))
  10615. (callback
  10616. (lambda () (time-less-p
  10617. (org-time-string-to-time (match-string 2))
  10618. (org-time-string-to-time date)))))
  10619. (message "%d entries before %s"
  10620. (org-occur regexp nil callback) date)))
  10621. (defun org-evaluate-time-range (&optional to-buffer)
  10622. "Evaluate a time range by computing the difference between start and end.
  10623. Normally the result is just printed in the echo area, but with prefix arg
  10624. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10625. If the time range is actually in a table, the result is inserted into the
  10626. next column.
  10627. For time difference computation, a year is assumed to be exactly 365
  10628. days in order to avoid rounding problems."
  10629. (interactive "P")
  10630. (or
  10631. (org-clock-update-time-maybe)
  10632. (save-excursion
  10633. (unless (org-at-date-range-p t)
  10634. (goto-char (point-at-bol))
  10635. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10636. (if (not (org-at-date-range-p t))
  10637. (error "Not at a time-stamp range, and none found in current line")))
  10638. (let* ((ts1 (match-string 1))
  10639. (ts2 (match-string 2))
  10640. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10641. (match-end (match-end 0))
  10642. (time1 (org-time-string-to-time ts1))
  10643. (time2 (org-time-string-to-time ts2))
  10644. (t1 (time-to-seconds time1))
  10645. (t2 (time-to-seconds time2))
  10646. (diff (abs (- t2 t1)))
  10647. (negative (< (- t2 t1) 0))
  10648. ;; (ys (floor (* 365 24 60 60)))
  10649. (ds (* 24 60 60))
  10650. (hs (* 60 60))
  10651. (fy "%dy %dd %02d:%02d")
  10652. (fy1 "%dy %dd")
  10653. (fd "%dd %02d:%02d")
  10654. (fd1 "%dd")
  10655. (fh "%02d:%02d")
  10656. y d h m align)
  10657. (if havetime
  10658. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10659. y 0
  10660. d (floor (/ diff ds)) diff (mod diff ds)
  10661. h (floor (/ diff hs)) diff (mod diff hs)
  10662. m (floor (/ diff 60)))
  10663. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10664. y 0
  10665. d (floor (+ (/ diff ds) 0.5))
  10666. h 0 m 0))
  10667. (if (not to-buffer)
  10668. (message "%s" (org-make-tdiff-string y d h m))
  10669. (if (org-at-table-p)
  10670. (progn
  10671. (goto-char match-end)
  10672. (setq align t)
  10673. (and (looking-at " *|") (goto-char (match-end 0))))
  10674. (goto-char match-end))
  10675. (if (looking-at
  10676. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10677. (replace-match ""))
  10678. (if negative (insert " -"))
  10679. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10680. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10681. (insert " " (format fh h m))))
  10682. (if align (org-table-align))
  10683. (message "Time difference inserted")))))
  10684. (defun org-make-tdiff-string (y d h m)
  10685. (let ((fmt "")
  10686. (l nil))
  10687. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10688. l (push y l)))
  10689. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10690. l (push d l)))
  10691. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10692. l (push h l)))
  10693. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10694. l (push m l)))
  10695. (apply 'format fmt (nreverse l))))
  10696. (defun org-time-string-to-time (s)
  10697. (apply 'encode-time (org-parse-time-string s)))
  10698. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10699. "Convert a time stamp to an absolute day number.
  10700. If there is a specifyer for a cyclic time stamp, get the closest date to
  10701. DAYNR.
  10702. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10703. (cond
  10704. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10705. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10706. daynr
  10707. (+ daynr 1000)))
  10708. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10709. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10710. (time-to-days (current-time))) (match-string 0 s)
  10711. prefer show-all))
  10712. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10713. (defun org-days-to-iso-week (days)
  10714. "Return the iso week number."
  10715. (require 'cal-iso)
  10716. (car (calendar-iso-from-absolute days)))
  10717. (defun org-small-year-to-year (year)
  10718. "Convert 2-digit years into 4-digit years.
  10719. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10720. The year 2000 cannot be abbreviated. Any year larger than 99
  10721. is returned unchanged."
  10722. (if (< year 38)
  10723. (setq year (+ 2000 year))
  10724. (if (< year 100)
  10725. (setq year (+ 1900 year))))
  10726. year)
  10727. (defun org-time-from-absolute (d)
  10728. "Return the time corresponding to date D.
  10729. D may be an absolute day number, or a calendar-type list (month day year)."
  10730. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10731. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10732. (defun org-calendar-holiday ()
  10733. "List of holidays, for Diary display in Org-mode."
  10734. (require 'holidays)
  10735. (let ((hl (funcall
  10736. (if (fboundp 'calendar-check-holidays)
  10737. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10738. (if hl (mapconcat 'identity hl "; "))))
  10739. (defun org-diary-sexp-entry (sexp entry date)
  10740. "Process a SEXP diary ENTRY for DATE."
  10741. (require 'diary-lib)
  10742. (let ((result (if calendar-debug-sexp
  10743. (let ((stack-trace-on-error t))
  10744. (eval (car (read-from-string sexp))))
  10745. (condition-case nil
  10746. (eval (car (read-from-string sexp)))
  10747. (error
  10748. (beep)
  10749. (message "Bad sexp at line %d in %s: %s"
  10750. (org-current-line)
  10751. (buffer-file-name) sexp)
  10752. (sleep-for 2))))))
  10753. (cond ((stringp result) result)
  10754. ((and (consp result)
  10755. (stringp (cdr result))) (cdr result))
  10756. (result entry)
  10757. (t nil))))
  10758. (defun org-diary-to-ical-string (frombuf)
  10759. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10760. This uses the icalendar.el library."
  10761. (let* ((tmpdir (if (featurep 'xemacs)
  10762. (temp-directory)
  10763. temporary-file-directory))
  10764. (tmpfile (make-temp-name
  10765. (expand-file-name "orgics" tmpdir)))
  10766. buf rtn b e)
  10767. (save-excursion
  10768. (set-buffer frombuf)
  10769. (icalendar-export-region (point-min) (point-max) tmpfile)
  10770. (setq buf (find-buffer-visiting tmpfile))
  10771. (set-buffer buf)
  10772. (goto-char (point-min))
  10773. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10774. (setq b (match-beginning 0)))
  10775. (goto-char (point-max))
  10776. (if (re-search-backward "^END:VEVENT" nil t)
  10777. (setq e (match-end 0)))
  10778. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10779. (kill-buffer buf)
  10780. (delete-file tmpfile)
  10781. rtn))
  10782. (defun org-closest-date (start current change prefer show-all)
  10783. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10784. When PREFER is `past' return a date that is either CURRENT or past.
  10785. When PREFER is `future', return a date that is either CURRENT or future.
  10786. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10787. ;; Make the proper lists from the dates
  10788. (catch 'exit
  10789. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10790. dn dw sday cday n1 n2 n0
  10791. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10792. (setq start (org-date-to-gregorian start)
  10793. current (org-date-to-gregorian
  10794. (if show-all
  10795. current
  10796. (time-to-days (current-time))))
  10797. sday (calendar-absolute-from-gregorian start)
  10798. cday (calendar-absolute-from-gregorian current))
  10799. (if (<= cday sday) (throw 'exit sday))
  10800. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10801. (setq dn (string-to-number (match-string 1 change))
  10802. dw (cdr (assoc (match-string 2 change) a1)))
  10803. (error "Invalid change specifyer: %s" change))
  10804. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10805. (cond
  10806. ((eq dw 'day)
  10807. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10808. n2 (+ n1 dn)))
  10809. ((eq dw 'year)
  10810. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10811. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10812. (setq date1 (list m d y1)
  10813. n1 (calendar-absolute-from-gregorian date1)
  10814. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10815. n2 (calendar-absolute-from-gregorian date2)))
  10816. ((eq dw 'month)
  10817. ;; approx number of month between the two dates
  10818. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10819. ;; How often does dn fit in there?
  10820. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10821. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10822. m (+ m nm)
  10823. ny (floor (/ m 12))
  10824. y (+ y ny)
  10825. m (- m (* ny 12)))
  10826. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10827. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10828. (setq m2 (+ m dn) y2 y)
  10829. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10830. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10831. (while (<= n2 cday)
  10832. (setq n1 n2 m m2 y y2)
  10833. (setq m2 (+ m dn) y2 y)
  10834. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10835. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10836. ;; Make sure n1 is the earlier date
  10837. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10838. (if show-all
  10839. (cond
  10840. ((eq prefer 'past) n1)
  10841. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10842. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10843. (cond
  10844. ((eq prefer 'past) n1)
  10845. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10846. (t (if (= cday n1) n1 n2)))))))
  10847. (defun org-date-to-gregorian (date)
  10848. "Turn any specification of DATE into a gregorian date for the calendar."
  10849. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10850. ((and (listp date) (= (length date) 3)) date)
  10851. ((stringp date)
  10852. (setq date (org-parse-time-string date))
  10853. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10854. ((listp date)
  10855. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10856. (defun org-parse-time-string (s &optional nodefault)
  10857. "Parse the standard Org-mode time string.
  10858. This should be a lot faster than the normal `parse-time-string'.
  10859. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10860. hour and minute fields will be nil if not given."
  10861. (if (string-match org-ts-regexp0 s)
  10862. (list 0
  10863. (if (or (match-beginning 8) (not nodefault))
  10864. (string-to-number (or (match-string 8 s) "0")))
  10865. (if (or (match-beginning 7) (not nodefault))
  10866. (string-to-number (or (match-string 7 s) "0")))
  10867. (string-to-number (match-string 4 s))
  10868. (string-to-number (match-string 3 s))
  10869. (string-to-number (match-string 2 s))
  10870. nil nil nil)
  10871. (make-list 9 0)))
  10872. (defun org-timestamp-up (&optional arg)
  10873. "Increase the date item at the cursor by one.
  10874. If the cursor is on the year, change the year. If it is on the month or
  10875. the day, change that.
  10876. With prefix ARG, change by that many units."
  10877. (interactive "p")
  10878. (org-timestamp-change (prefix-numeric-value arg)))
  10879. (defun org-timestamp-down (&optional arg)
  10880. "Decrease the date item at the cursor by one.
  10881. If the cursor is on the year, change the year. If it is on the month or
  10882. the day, change that.
  10883. With prefix ARG, change by that many units."
  10884. (interactive "p")
  10885. (org-timestamp-change (- (prefix-numeric-value arg))))
  10886. (defun org-timestamp-up-day (&optional arg)
  10887. "Increase the date in the time stamp by one day.
  10888. With prefix ARG, change that many days."
  10889. (interactive "p")
  10890. (if (and (not (org-at-timestamp-p t))
  10891. (org-on-heading-p))
  10892. (org-todo 'up)
  10893. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10894. (defun org-timestamp-down-day (&optional arg)
  10895. "Decrease the date in the time stamp by one day.
  10896. With prefix ARG, change that many days."
  10897. (interactive "p")
  10898. (if (and (not (org-at-timestamp-p t))
  10899. (org-on-heading-p))
  10900. (org-todo 'down)
  10901. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10902. (defun org-at-timestamp-p (&optional inactive-ok)
  10903. "Determine if the cursor is in or at a timestamp."
  10904. (interactive)
  10905. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10906. (pos (point))
  10907. (ans (or (looking-at tsr)
  10908. (save-excursion
  10909. (skip-chars-backward "^[<\n\r\t")
  10910. (if (> (point) (point-min)) (backward-char 1))
  10911. (and (looking-at tsr)
  10912. (> (- (match-end 0) pos) -1))))))
  10913. (and ans
  10914. (boundp 'org-ts-what)
  10915. (setq org-ts-what
  10916. (cond
  10917. ((= pos (match-beginning 0)) 'bracket)
  10918. ((= pos (1- (match-end 0))) 'bracket)
  10919. ((org-pos-in-match-range pos 2) 'year)
  10920. ((org-pos-in-match-range pos 3) 'month)
  10921. ((org-pos-in-match-range pos 7) 'hour)
  10922. ((org-pos-in-match-range pos 8) 'minute)
  10923. ((or (org-pos-in-match-range pos 4)
  10924. (org-pos-in-match-range pos 5)) 'day)
  10925. ((and (> pos (or (match-end 8) (match-end 5)))
  10926. (< pos (match-end 0)))
  10927. (- pos (or (match-end 8) (match-end 5))))
  10928. (t 'day))))
  10929. ans))
  10930. (defun org-toggle-timestamp-type ()
  10931. "Toggle the type (<active> or [inactive]) of a time stamp."
  10932. (interactive)
  10933. (when (org-at-timestamp-p t)
  10934. (let ((beg (match-beginning 0)) (end (match-end 0))
  10935. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10936. (save-excursion
  10937. (goto-char beg)
  10938. (while (re-search-forward "[][<>]" end t)
  10939. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10940. t t)))
  10941. (message "Timestamp is now %sactive"
  10942. (if (equal (char-after beg) ?<) "" "in")))))
  10943. (defun org-timestamp-change (n &optional what)
  10944. "Change the date in the time stamp at point.
  10945. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10946. `year', `minute', `second'. If WHAT is not given, the cursor position
  10947. in the timestamp determines what will be changed."
  10948. (let ((pos (point))
  10949. with-hm inactive
  10950. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10951. org-ts-what
  10952. extra rem
  10953. ts time time0)
  10954. (if (not (org-at-timestamp-p t))
  10955. (error "Not at a timestamp"))
  10956. (if (and (not what) (eq org-ts-what 'bracket))
  10957. (org-toggle-timestamp-type)
  10958. (if (and (not what) (not (eq org-ts-what 'day))
  10959. org-display-custom-times
  10960. (get-text-property (point) 'display)
  10961. (not (get-text-property (1- (point)) 'display)))
  10962. (setq org-ts-what 'day))
  10963. (setq org-ts-what (or what org-ts-what)
  10964. inactive (= (char-after (match-beginning 0)) ?\[)
  10965. ts (match-string 0))
  10966. (replace-match "")
  10967. (if (string-match
  10968. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10969. ts)
  10970. (setq extra (match-string 1 ts)))
  10971. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10972. (setq with-hm t))
  10973. (setq time0 (org-parse-time-string ts))
  10974. (when (and (eq org-ts-what 'minute)
  10975. (eq current-prefix-arg nil))
  10976. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10977. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10978. (setcar (cdr time0) (+ (nth 1 time0)
  10979. (if (> n 0) (- rem) (- dm rem))))))
  10980. (setq time
  10981. (encode-time (or (car time0) 0)
  10982. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10983. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10984. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10985. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10986. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10987. (nthcdr 6 time0)))
  10988. (when (integerp org-ts-what)
  10989. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10990. (if (eq what 'calendar)
  10991. (let ((cal-date (org-get-date-from-calendar)))
  10992. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10993. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10994. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10995. (setcar time0 (or (car time0) 0))
  10996. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10997. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10998. (setq time (apply 'encode-time time0))))
  10999. (setq org-last-changed-timestamp
  11000. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11001. (org-clock-update-time-maybe)
  11002. (goto-char pos)
  11003. ;; Try to recenter the calendar window, if any
  11004. (if (and org-calendar-follow-timestamp-change
  11005. (get-buffer-window "*Calendar*" t)
  11006. (memq org-ts-what '(day month year)))
  11007. (org-recenter-calendar (time-to-days time))))))
  11008. (defun org-modify-ts-extra (s pos n dm)
  11009. "Change the different parts of the lead-time and repeat fields in timestamp."
  11010. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11011. ng h m new rem)
  11012. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11013. (cond
  11014. ((or (org-pos-in-match-range pos 2)
  11015. (org-pos-in-match-range pos 3))
  11016. (setq m (string-to-number (match-string 3 s))
  11017. h (string-to-number (match-string 2 s)))
  11018. (if (org-pos-in-match-range pos 2)
  11019. (setq h (+ h n))
  11020. (setq n (* dm (org-no-warnings (signum n))))
  11021. (when (not (= 0 (setq rem (% m dm))))
  11022. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11023. (setq m (+ m n)))
  11024. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11025. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11026. (setq h (min 24 (max 0 h)))
  11027. (setq ng 1 new (format "-%02d:%02d" h m)))
  11028. ((org-pos-in-match-range pos 6)
  11029. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11030. ((org-pos-in-match-range pos 5)
  11031. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11032. ((org-pos-in-match-range pos 9)
  11033. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11034. ((org-pos-in-match-range pos 8)
  11035. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11036. (when ng
  11037. (setq s (concat
  11038. (substring s 0 (match-beginning ng))
  11039. new
  11040. (substring s (match-end ng))))))
  11041. s))
  11042. (defun org-recenter-calendar (date)
  11043. "If the calendar is visible, recenter it to DATE."
  11044. (let* ((win (selected-window))
  11045. (cwin (get-buffer-window "*Calendar*" t))
  11046. (calendar-move-hook nil))
  11047. (when cwin
  11048. (select-window cwin)
  11049. (calendar-goto-date (if (listp date) date
  11050. (calendar-gregorian-from-absolute date)))
  11051. (select-window win))))
  11052. (defun org-goto-calendar (&optional arg)
  11053. "Go to the Emacs calendar at the current date.
  11054. If there is a time stamp in the current line, go to that date.
  11055. A prefix ARG can be used to force the current date."
  11056. (interactive "P")
  11057. (let ((tsr org-ts-regexp) diff
  11058. (calendar-move-hook nil)
  11059. (calendar-view-holidays-initially-flag nil)
  11060. (view-calendar-holidays-initially nil)
  11061. (calendar-view-diary-initially-flag nil)
  11062. (view-diary-entries-initially nil))
  11063. (if (or (org-at-timestamp-p)
  11064. (save-excursion
  11065. (beginning-of-line 1)
  11066. (looking-at (concat ".*" tsr))))
  11067. (let ((d1 (time-to-days (current-time)))
  11068. (d2 (time-to-days
  11069. (org-time-string-to-time (match-string 1)))))
  11070. (setq diff (- d2 d1))))
  11071. (calendar)
  11072. (calendar-goto-today)
  11073. (if (and diff (not arg)) (calendar-forward-day diff))))
  11074. (defun org-get-date-from-calendar ()
  11075. "Return a list (month day year) of date at point in calendar."
  11076. (with-current-buffer "*Calendar*"
  11077. (save-match-data
  11078. (calendar-cursor-to-date))))
  11079. (defun org-date-from-calendar ()
  11080. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11081. If there is already a time stamp at the cursor position, update it."
  11082. (interactive)
  11083. (if (org-at-timestamp-p t)
  11084. (org-timestamp-change 0 'calendar)
  11085. (let ((cal-date (org-get-date-from-calendar)))
  11086. (org-insert-time-stamp
  11087. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11088. (defun org-minutes-to-hh:mm-string (m)
  11089. "Compute H:MM from a number of minutes."
  11090. (let ((h (/ m 60)))
  11091. (setq m (- m (* 60 h)))
  11092. (format org-time-clocksum-format h m)))
  11093. (defun org-hh:mm-string-to-minutes (s)
  11094. "Convert a string H:MM to a number of minutes."
  11095. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11096. (+ (* (string-to-number (match-string 1 s)) 60)
  11097. (string-to-number (match-string 2 s)))
  11098. 0))
  11099. ;;;; Agenda files
  11100. ;;;###autoload
  11101. (defun org-iswitchb (&optional arg)
  11102. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11103. With a prefix argument, restrict available to files.
  11104. With two prefix arguments, restrict available buffers to agenda files.
  11105. Due to some yet unresolved reason, the global function
  11106. `iswitchb-mode' needs to be active for this function to work."
  11107. (interactive "P")
  11108. (require 'iswitchb)
  11109. (let ((enabled iswitchb-mode) blist)
  11110. (or enabled (iswitchb-mode 1))
  11111. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11112. ((equal arg '(16)) (org-buffer-list 'agenda))
  11113. (t (org-buffer-list))))
  11114. (unwind-protect
  11115. (let ((iswitchb-make-buflist-hook
  11116. (lambda ()
  11117. (setq iswitchb-temp-buflist
  11118. (mapcar 'buffer-name blist)))))
  11119. (switch-to-buffer
  11120. (iswitchb-read-buffer
  11121. "Switch-to: " nil t))
  11122. (or enabled (iswitchb-mode -1))))))
  11123. ;;;###autoload
  11124. (defun org-ido-switchb (&optional arg)
  11125. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11126. With a prefix argument, restrict available to files.
  11127. With two prefix arguments, restrict available buffers to agenda files."
  11128. (interactive "P")
  11129. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11130. ((equal arg '(16)) (org-buffer-list 'agenda))
  11131. (t (org-buffer-list)))))
  11132. (switch-to-buffer
  11133. (org-ido-completing-read "Org buffer: "
  11134. (mapcar 'buffer-name blist)
  11135. nil t))))
  11136. (defun org-buffer-list (&optional predicate exclude-tmp)
  11137. "Return a list of Org buffers.
  11138. PREDICATE can be `export', `files' or `agenda'.
  11139. export restrict the list to Export buffers.
  11140. files restrict the list to buffers visiting Org files.
  11141. agenda restrict the list to buffers visiting agenda files.
  11142. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11143. (let* ((bfn nil)
  11144. (agenda-files (and (eq predicate 'agenda)
  11145. (mapcar 'file-truename (org-agenda-files t))))
  11146. (filter
  11147. (cond
  11148. ((eq predicate 'files)
  11149. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11150. ((eq predicate 'export)
  11151. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11152. ((eq predicate 'agenda)
  11153. (lambda (b)
  11154. (with-current-buffer b
  11155. (and (eq major-mode 'org-mode)
  11156. (setq bfn (buffer-file-name b))
  11157. (member (file-truename bfn) agenda-files)))))
  11158. (t (lambda (b) (with-current-buffer b
  11159. (or (eq major-mode 'org-mode)
  11160. (string-match "\*Org .*Export"
  11161. (buffer-name b)))))))))
  11162. (delq nil
  11163. (mapcar
  11164. (lambda(b)
  11165. (if (and (funcall filter b)
  11166. (or (not exclude-tmp)
  11167. (not (string-match "tmp" (buffer-name b)))))
  11168. b
  11169. nil))
  11170. (buffer-list)))))
  11171. (defun org-agenda-files (&optional unrestricted archives)
  11172. "Get the list of agenda files.
  11173. Optional UNRESTRICTED means return the full list even if a restriction
  11174. is currently in place.
  11175. When ARCHIVES is t, include all archive files hat are really being
  11176. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11177. `org-agenda-archives-mode' is t."
  11178. (let ((files
  11179. (cond
  11180. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11181. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11182. ((listp org-agenda-files) org-agenda-files)
  11183. (t (error "Invalid value of `org-agenda-files'")))))
  11184. (setq files (apply 'append
  11185. (mapcar (lambda (f)
  11186. (if (file-directory-p f)
  11187. (directory-files
  11188. f t org-agenda-file-regexp)
  11189. (list f)))
  11190. files)))
  11191. (when org-agenda-skip-unavailable-files
  11192. (setq files (delq nil
  11193. (mapcar (function
  11194. (lambda (file)
  11195. (and (file-readable-p file) file)))
  11196. files))))
  11197. (when (or (eq archives t)
  11198. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11199. (setq files (org-add-archive-files files)))
  11200. files))
  11201. (defun org-edit-agenda-file-list ()
  11202. "Edit the list of agenda files.
  11203. Depending on setup, this either uses customize to edit the variable
  11204. `org-agenda-files', or it visits the file that is holding the list. In the
  11205. latter case, the buffer is set up in a way that saving it automatically kills
  11206. the buffer and restores the previous window configuration."
  11207. (interactive)
  11208. (if (stringp org-agenda-files)
  11209. (let ((cw (current-window-configuration)))
  11210. (find-file org-agenda-files)
  11211. (org-set-local 'org-window-configuration cw)
  11212. (org-add-hook 'after-save-hook
  11213. (lambda ()
  11214. (set-window-configuration
  11215. (prog1 org-window-configuration
  11216. (kill-buffer (current-buffer))))
  11217. (org-install-agenda-files-menu)
  11218. (message "New agenda file list installed"))
  11219. nil 'local)
  11220. (message "%s" (substitute-command-keys
  11221. "Edit list and finish with \\[save-buffer]")))
  11222. (customize-variable 'org-agenda-files)))
  11223. (defun org-store-new-agenda-file-list (list)
  11224. "Set new value for the agenda file list and save it correctly."
  11225. (if (stringp org-agenda-files)
  11226. (let ((f org-agenda-files) b)
  11227. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11228. (with-temp-file f
  11229. (insert (mapconcat 'identity list "\n") "\n")))
  11230. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11231. (setq org-agenda-files list)
  11232. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11233. (defun org-read-agenda-file-list ()
  11234. "Read the list of agenda files from a file."
  11235. (when (file-directory-p org-agenda-files)
  11236. (error "`org-agenda-files' cannot be a single directory"))
  11237. (when (stringp org-agenda-files)
  11238. (with-temp-buffer
  11239. (insert-file-contents org-agenda-files)
  11240. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11241. ;;;###autoload
  11242. (defun org-cycle-agenda-files ()
  11243. "Cycle through the files in `org-agenda-files'.
  11244. If the current buffer visits an agenda file, find the next one in the list.
  11245. If the current buffer does not, find the first agenda file."
  11246. (interactive)
  11247. (let* ((fs (org-agenda-files t))
  11248. (files (append fs (list (car fs))))
  11249. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11250. file)
  11251. (unless files (error "No agenda files"))
  11252. (catch 'exit
  11253. (while (setq file (pop files))
  11254. (if (equal (file-truename file) tcf)
  11255. (when (car files)
  11256. (find-file (car files))
  11257. (throw 'exit t))))
  11258. (find-file (car fs)))
  11259. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11260. (defun org-agenda-file-to-front (&optional to-end)
  11261. "Move/add the current file to the top of the agenda file list.
  11262. If the file is not present in the list, it is added to the front. If it is
  11263. present, it is moved there. With optional argument TO-END, add/move to the
  11264. end of the list."
  11265. (interactive "P")
  11266. (let ((org-agenda-skip-unavailable-files nil)
  11267. (file-alist (mapcar (lambda (x)
  11268. (cons (file-truename x) x))
  11269. (org-agenda-files t)))
  11270. (ctf (file-truename buffer-file-name))
  11271. x had)
  11272. (setq x (assoc ctf file-alist) had x)
  11273. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11274. (if to-end
  11275. (setq file-alist (append (delq x file-alist) (list x)))
  11276. (setq file-alist (cons x (delq x file-alist))))
  11277. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11278. (org-install-agenda-files-menu)
  11279. (message "File %s to %s of agenda file list"
  11280. (if had "moved" "added") (if to-end "end" "front"))))
  11281. (defun org-remove-file (&optional file)
  11282. "Remove current file from the list of files in variable `org-agenda-files'.
  11283. These are the files which are being checked for agenda entries.
  11284. Optional argument FILE means, use this file instead of the current."
  11285. (interactive)
  11286. (let* ((org-agenda-skip-unavailable-files nil)
  11287. (file (or file buffer-file-name))
  11288. (true-file (file-truename file))
  11289. (afile (abbreviate-file-name file))
  11290. (files (delq nil (mapcar
  11291. (lambda (x)
  11292. (if (equal true-file
  11293. (file-truename x))
  11294. nil x))
  11295. (org-agenda-files t)))))
  11296. (if (not (= (length files) (length (org-agenda-files t))))
  11297. (progn
  11298. (org-store-new-agenda-file-list files)
  11299. (org-install-agenda-files-menu)
  11300. (message "Removed file: %s" afile))
  11301. (message "File was not in list: %s (not removed)" afile))))
  11302. (defun org-file-menu-entry (file)
  11303. (vector file (list 'find-file file) t))
  11304. (defun org-check-agenda-file (file)
  11305. "Make sure FILE exists. If not, ask user what to do."
  11306. (when (not (file-exists-p file))
  11307. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11308. (abbreviate-file-name file))
  11309. (let ((r (downcase (read-char-exclusive))))
  11310. (cond
  11311. ((equal r ?r)
  11312. (org-remove-file file)
  11313. (throw 'nextfile t))
  11314. (t (error "Abort"))))))
  11315. (defun org-get-agenda-file-buffer (file)
  11316. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11317. it to the list of buffers which might be released later."
  11318. (let ((buf (org-find-base-buffer-visiting file)))
  11319. (if buf
  11320. buf ; just return it
  11321. ;; Make a new buffer and remember it
  11322. (setq buf (find-file-noselect file))
  11323. (if buf (push buf org-agenda-new-buffers))
  11324. buf)))
  11325. (defun org-release-buffers (blist)
  11326. "Release all buffers in list, asking the user for confirmation when needed.
  11327. When a buffer is unmodified, it is just killed. When modified, it is saved
  11328. \(if the user agrees) and then killed."
  11329. (let (buf file)
  11330. (while (setq buf (pop blist))
  11331. (setq file (buffer-file-name buf))
  11332. (when (and (buffer-modified-p buf)
  11333. file
  11334. (y-or-n-p (format "Save file %s? " file)))
  11335. (with-current-buffer buf (save-buffer)))
  11336. (kill-buffer buf))))
  11337. (defun org-prepare-agenda-buffers (files)
  11338. "Create buffers for all agenda files, protect archived trees and comments."
  11339. (interactive)
  11340. (let ((pa '(:org-archived t))
  11341. (pc '(:org-comment t))
  11342. (pall '(:org-archived t :org-comment t))
  11343. (inhibit-read-only t)
  11344. (rea (concat ":" org-archive-tag ":"))
  11345. bmp file re)
  11346. (save-excursion
  11347. (save-restriction
  11348. (while (setq file (pop files))
  11349. (if (bufferp file)
  11350. (set-buffer file)
  11351. (org-check-agenda-file file)
  11352. (set-buffer (org-get-agenda-file-buffer file)))
  11353. (widen)
  11354. (setq bmp (buffer-modified-p))
  11355. (org-refresh-category-properties)
  11356. (setq org-todo-keywords-for-agenda
  11357. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11358. (setq org-done-keywords-for-agenda
  11359. (append org-done-keywords-for-agenda org-done-keywords))
  11360. (setq org-todo-keyword-alist-for-agenda
  11361. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11362. (setq org-tag-alist-for-agenda
  11363. (append org-tag-alist-for-agenda org-tag-alist))
  11364. (save-excursion
  11365. (remove-text-properties (point-min) (point-max) pall)
  11366. (when org-agenda-skip-archived-trees
  11367. (goto-char (point-min))
  11368. (while (re-search-forward rea nil t)
  11369. (if (org-on-heading-p t)
  11370. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11371. (goto-char (point-min))
  11372. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11373. (while (re-search-forward re nil t)
  11374. (add-text-properties
  11375. (match-beginning 0) (org-end-of-subtree t) pc)))
  11376. (set-buffer-modified-p bmp))))
  11377. (setq org-todo-keyword-alist-for-agenda
  11378. (org-uniquify org-todo-keyword-alist-for-agenda)
  11379. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11380. ;;;; Embedded LaTeX
  11381. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11382. "Keymap for the minor `org-cdlatex-mode'.")
  11383. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11384. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11385. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11386. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11387. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11388. (defvar org-cdlatex-texmathp-advice-is-done nil
  11389. "Flag remembering if we have applied the advice to texmathp already.")
  11390. (define-minor-mode org-cdlatex-mode
  11391. "Toggle the minor `org-cdlatex-mode'.
  11392. This mode supports entering LaTeX environment and math in LaTeX fragments
  11393. in Org-mode.
  11394. \\{org-cdlatex-mode-map}"
  11395. nil " OCDL" nil
  11396. (when org-cdlatex-mode (require 'cdlatex))
  11397. (unless org-cdlatex-texmathp-advice-is-done
  11398. (setq org-cdlatex-texmathp-advice-is-done t)
  11399. (defadvice texmathp (around org-math-always-on activate)
  11400. "Always return t in org-mode buffers.
  11401. This is because we want to insert math symbols without dollars even outside
  11402. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11403. an embedded LaTeX fragment, let texmathp do its job.
  11404. \\[org-cdlatex-mode-map]"
  11405. (interactive)
  11406. (let (p)
  11407. (cond
  11408. ((not (org-mode-p)) ad-do-it)
  11409. ((eq this-command 'cdlatex-math-symbol)
  11410. (setq ad-return-value t
  11411. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11412. (t
  11413. (let ((p (org-inside-LaTeX-fragment-p)))
  11414. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11415. (setq ad-return-value t
  11416. texmathp-why '("Org-mode embedded math" . 0))
  11417. (if p ad-do-it)))))))))
  11418. (defun turn-on-org-cdlatex ()
  11419. "Unconditionally turn on `org-cdlatex-mode'."
  11420. (org-cdlatex-mode 1))
  11421. (defun org-inside-LaTeX-fragment-p ()
  11422. "Test if point is inside a LaTeX fragment.
  11423. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11424. sequence appearing also before point.
  11425. Even though the matchers for math are configurable, this function assumes
  11426. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11427. delimiters are skipped when they have been removed by customization.
  11428. The return value is nil, or a cons cell with the delimiter and
  11429. and the position of this delimiter.
  11430. This function does a reasonably good job, but can locally be fooled by
  11431. for example currency specifications. For example it will assume being in
  11432. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11433. fragments that are properly closed, but during editing, we have to live
  11434. with the uncertainty caused by missing closing delimiters. This function
  11435. looks only before point, not after."
  11436. (catch 'exit
  11437. (let ((pos (point))
  11438. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11439. (lim (progn
  11440. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11441. (point)))
  11442. dd-on str (start 0) m re)
  11443. (goto-char pos)
  11444. (when dodollar
  11445. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11446. re (nth 1 (assoc "$" org-latex-regexps)))
  11447. (while (string-match re str start)
  11448. (cond
  11449. ((= (match-end 0) (length str))
  11450. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11451. ((= (match-end 0) (- (length str) 5))
  11452. (throw 'exit nil))
  11453. (t (setq start (match-end 0))))))
  11454. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11455. (goto-char pos)
  11456. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11457. (and (match-beginning 2) (throw 'exit nil))
  11458. ;; count $$
  11459. (while (re-search-backward "\\$\\$" lim t)
  11460. (setq dd-on (not dd-on)))
  11461. (goto-char pos)
  11462. (if dd-on (cons "$$" m))))))
  11463. (defun org-try-cdlatex-tab ()
  11464. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11465. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11466. - inside a LaTeX fragment, or
  11467. - after the first word in a line, where an abbreviation expansion could
  11468. insert a LaTeX environment."
  11469. (when org-cdlatex-mode
  11470. (cond
  11471. ((save-excursion
  11472. (skip-chars-backward "a-zA-Z0-9*")
  11473. (skip-chars-backward " \t")
  11474. (bolp))
  11475. (cdlatex-tab) t)
  11476. ((org-inside-LaTeX-fragment-p)
  11477. (cdlatex-tab) t)
  11478. (t nil))))
  11479. (defun org-cdlatex-underscore-caret (&optional arg)
  11480. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11481. Revert to the normal definition outside of these fragments."
  11482. (interactive "P")
  11483. (if (org-inside-LaTeX-fragment-p)
  11484. (call-interactively 'cdlatex-sub-superscript)
  11485. (let (org-cdlatex-mode)
  11486. (call-interactively (key-binding (vector last-input-event))))))
  11487. (defun org-cdlatex-math-modify (&optional arg)
  11488. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11489. Revert to the normal definition outside of these fragments."
  11490. (interactive "P")
  11491. (if (org-inside-LaTeX-fragment-p)
  11492. (call-interactively 'cdlatex-math-modify)
  11493. (let (org-cdlatex-mode)
  11494. (call-interactively (key-binding (vector last-input-event))))))
  11495. (defvar org-latex-fragment-image-overlays nil
  11496. "List of overlays carrying the images of latex fragments.")
  11497. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11498. (defun org-remove-latex-fragment-image-overlays ()
  11499. "Remove all overlays with LaTeX fragment images in current buffer."
  11500. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11501. (setq org-latex-fragment-image-overlays nil))
  11502. (defun org-preview-latex-fragment (&optional subtree)
  11503. "Preview the LaTeX fragment at point, or all locally or globally.
  11504. If the cursor is in a LaTeX fragment, create the image and overlay
  11505. it over the source code. If there is no fragment at point, display
  11506. all fragments in the current text, from one headline to the next. With
  11507. prefix SUBTREE, display all fragments in the current subtree. With a
  11508. double prefix `C-u C-u', or when the cursor is before the first headline,
  11509. display all fragments in the buffer.
  11510. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11511. (interactive "P")
  11512. (org-remove-latex-fragment-image-overlays)
  11513. (save-excursion
  11514. (save-restriction
  11515. (let (beg end at msg)
  11516. (cond
  11517. ((or (equal subtree '(16))
  11518. (not (save-excursion
  11519. (re-search-backward (concat "^" outline-regexp) nil t))))
  11520. (setq beg (point-min) end (point-max)
  11521. msg "Creating images for buffer...%s"))
  11522. ((equal subtree '(4))
  11523. (org-back-to-heading)
  11524. (setq beg (point) end (org-end-of-subtree t)
  11525. msg "Creating images for subtree...%s"))
  11526. (t
  11527. (if (setq at (org-inside-LaTeX-fragment-p))
  11528. (goto-char (max (point-min) (- (cdr at) 2)))
  11529. (org-back-to-heading))
  11530. (setq beg (point) end (progn (outline-next-heading) (point))
  11531. msg (if at "Creating image...%s"
  11532. "Creating images for entry...%s"))))
  11533. (message msg "")
  11534. (narrow-to-region beg end)
  11535. (goto-char beg)
  11536. (org-format-latex
  11537. (concat "ltxpng/" (file-name-sans-extension
  11538. (file-name-nondirectory
  11539. buffer-file-name)))
  11540. default-directory 'overlays msg at 'forbuffer)
  11541. (message msg "done. Use `C-c C-c' to remove images.")))))
  11542. (defvar org-latex-regexps
  11543. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11544. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11545. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11546. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11547. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11548. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11549. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11550. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11551. "Regular expressions for matching embedded LaTeX.")
  11552. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11553. "Replace LaTeX fragments with links to an image, and produce images."
  11554. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11555. (let* ((prefixnodir (file-name-nondirectory prefix))
  11556. (absprefix (expand-file-name prefix dir))
  11557. (todir (file-name-directory absprefix))
  11558. (opt org-format-latex-options)
  11559. (matchers (plist-get opt :matchers))
  11560. (re-list org-latex-regexps)
  11561. (cnt 0) txt link beg end re e checkdir
  11562. m n block linkfile movefile ov)
  11563. ;; Check if there are old images files with this prefix, and remove them
  11564. (when (file-directory-p todir)
  11565. (mapc 'delete-file
  11566. (directory-files
  11567. todir 'full
  11568. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11569. ;; Check the different regular expressions
  11570. (while (setq e (pop re-list))
  11571. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11572. block (if (nth 3 e) "\n\n" ""))
  11573. (when (member m matchers)
  11574. (goto-char (point-min))
  11575. (while (re-search-forward re nil t)
  11576. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11577. (setq txt (match-string n)
  11578. beg (match-beginning n) end (match-end n)
  11579. cnt (1+ cnt)
  11580. linkfile (format "%s_%04d.png" prefix cnt)
  11581. movefile (format "%s_%04d.png" absprefix cnt)
  11582. link (concat block "[[file:" linkfile "]]" block))
  11583. (if msg (message msg cnt))
  11584. (goto-char beg)
  11585. (unless checkdir ; make sure the directory exists
  11586. (setq checkdir t)
  11587. (or (file-directory-p todir) (make-directory todir)))
  11588. (org-create-formula-image
  11589. txt movefile opt forbuffer)
  11590. (if overlays
  11591. (progn
  11592. (setq ov (org-make-overlay beg end))
  11593. (if (featurep 'xemacs)
  11594. (progn
  11595. (org-overlay-put ov 'invisible t)
  11596. (org-overlay-put
  11597. ov 'end-glyph
  11598. (make-glyph (vector 'png :file movefile))))
  11599. (org-overlay-put
  11600. ov 'display
  11601. (list 'image :type 'png :file movefile :ascent 'center)))
  11602. (push ov org-latex-fragment-image-overlays)
  11603. (goto-char end))
  11604. (delete-region beg end)
  11605. (insert link))))))))
  11606. ;; This function borrows from Ganesh Swami's latex2png.el
  11607. (defun org-create-formula-image (string tofile options buffer)
  11608. (let* ((tmpdir (if (featurep 'xemacs)
  11609. (temp-directory)
  11610. temporary-file-directory))
  11611. (texfilebase (make-temp-name
  11612. (expand-file-name "orgtex" tmpdir)))
  11613. (texfile (concat texfilebase ".tex"))
  11614. (dvifile (concat texfilebase ".dvi"))
  11615. (pngfile (concat texfilebase ".png"))
  11616. (fnh (if (featurep 'xemacs)
  11617. (font-height (get-face-font 'default))
  11618. (face-attribute 'default :height nil)))
  11619. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11620. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11621. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11622. "Black"))
  11623. (bg (or (plist-get options (if buffer :background :html-background))
  11624. "Transparent")))
  11625. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11626. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11627. (with-temp-file texfile
  11628. (insert org-format-latex-header
  11629. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11630. (let ((dir default-directory))
  11631. (condition-case nil
  11632. (progn
  11633. (cd tmpdir)
  11634. (call-process "latex" nil nil nil texfile))
  11635. (error nil))
  11636. (cd dir))
  11637. (if (not (file-exists-p dvifile))
  11638. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11639. (condition-case nil
  11640. (call-process "dvipng" nil nil nil
  11641. "-E" "-fg" fg "-bg" bg
  11642. "-D" dpi
  11643. ;;"-x" scale "-y" scale
  11644. "-T" "tight"
  11645. "-o" pngfile
  11646. dvifile)
  11647. (error nil))
  11648. (if (not (file-exists-p pngfile))
  11649. (progn (message "Failed to create png file from %s" texfile) nil)
  11650. ;; Use the requested file name and clean up
  11651. (copy-file pngfile tofile 'replace)
  11652. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11653. (delete-file (concat texfilebase e)))
  11654. pngfile))))
  11655. (defun org-dvipng-color (attr)
  11656. "Return an rgb color specification for dvipng."
  11657. (apply 'format "rgb %s %s %s"
  11658. (mapcar 'org-normalize-color
  11659. (color-values (face-attribute 'default attr nil)))))
  11660. (defun org-normalize-color (value)
  11661. "Return string to be used as color value for an RGB component."
  11662. (format "%g" (/ value 65535.0)))
  11663. ;;;; Key bindings
  11664. ;; Make `C-c C-x' a prefix key
  11665. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11666. ;; TAB key with modifiers
  11667. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11668. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11669. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11670. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11671. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11672. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11673. ;; The following line is necessary under Suse GNU/Linux
  11674. (unless (featurep 'xemacs)
  11675. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11676. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11677. (define-key org-mode-map [backtab] 'org-shifttab)
  11678. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11679. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11680. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11681. ;; Cursor keys with modifiers
  11682. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11683. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11684. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11685. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11686. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11687. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11688. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11689. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11690. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11691. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11692. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11693. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11694. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11695. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11696. ;;; Extra keys for tty access.
  11697. ;; We only set them when really needed because otherwise the
  11698. ;; menus don't show the simple keys
  11699. (when (or org-use-extra-keys
  11700. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11701. (not window-system))
  11702. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11703. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11704. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11705. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11706. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11707. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11708. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11709. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11710. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11711. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11712. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11713. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11714. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11715. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11716. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11717. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11718. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11719. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11720. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11721. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11722. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11723. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11724. ;; All the other keys
  11725. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11726. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11727. (if (boundp 'narrow-map)
  11728. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11729. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11730. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11731. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11732. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11733. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11734. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11735. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11736. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11737. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11738. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11739. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11740. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11741. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11742. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11743. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11744. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11745. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11746. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11747. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11748. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11749. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11750. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11751. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11752. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11753. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11754. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11755. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11756. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11757. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11758. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11759. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11760. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11761. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11762. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11763. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11764. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11765. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11766. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11767. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11768. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11769. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11770. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11771. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11772. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11773. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11774. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11775. (org-defkey org-mode-map "\C-m" 'org-return)
  11776. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11777. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11778. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11779. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11780. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11781. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11782. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11783. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11784. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11785. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11786. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11787. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11788. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11789. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11790. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11791. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11792. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11793. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11794. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11795. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11796. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11797. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11798. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11799. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11800. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11801. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11802. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11803. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11804. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11805. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11806. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11807. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11808. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  11809. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11810. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11811. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11812. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11813. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11814. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11815. (when (featurep 'xemacs)
  11816. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11817. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11818. (defun org-self-insert-command (N)
  11819. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11820. If the cursor is in a table looking at whitespace, the whitespace is
  11821. overwritten, and the table is not marked as requiring realignment."
  11822. (interactive "p")
  11823. (if (and (org-table-p)
  11824. (progn
  11825. ;; check if we blank the field, and if that triggers align
  11826. (and (featurep 'org-table) org-table-auto-blank-field
  11827. (member last-command
  11828. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11829. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11830. ;; got extra space, this field does not determine column width
  11831. (let (org-table-may-need-update) (org-table-blank-field))
  11832. ;; no extra space, this field may determine column width
  11833. (org-table-blank-field)))
  11834. t)
  11835. (eq N 1)
  11836. (looking-at "[^|\n]* |"))
  11837. (let (org-table-may-need-update)
  11838. (goto-char (1- (match-end 0)))
  11839. (delete-backward-char 1)
  11840. (goto-char (match-beginning 0))
  11841. (self-insert-command N))
  11842. (setq org-table-may-need-update t)
  11843. (self-insert-command N)
  11844. (org-fix-tags-on-the-fly)))
  11845. (defun org-fix-tags-on-the-fly ()
  11846. (when (and (equal (char-after (point-at-bol)) ?*)
  11847. (org-on-heading-p))
  11848. (org-align-tags-here org-tags-column)))
  11849. (defun org-delete-backward-char (N)
  11850. "Like `delete-backward-char', insert whitespace at field end in tables.
  11851. When deleting backwards, in tables this function will insert whitespace in
  11852. front of the next \"|\" separator, to keep the table aligned. The table will
  11853. still be marked for re-alignment if the field did fill the entire column,
  11854. because, in this case the deletion might narrow the column."
  11855. (interactive "p")
  11856. (if (and (org-table-p)
  11857. (eq N 1)
  11858. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11859. (looking-at ".*?|"))
  11860. (let ((pos (point))
  11861. (noalign (looking-at "[^|\n\r]* |"))
  11862. (c org-table-may-need-update))
  11863. (backward-delete-char N)
  11864. (skip-chars-forward "^|")
  11865. (insert " ")
  11866. (goto-char (1- pos))
  11867. ;; noalign: if there were two spaces at the end, this field
  11868. ;; does not determine the width of the column.
  11869. (if noalign (setq org-table-may-need-update c)))
  11870. (backward-delete-char N)
  11871. (org-fix-tags-on-the-fly)))
  11872. (defun org-delete-char (N)
  11873. "Like `delete-char', but insert whitespace at field end in tables.
  11874. When deleting characters, in tables this function will insert whitespace in
  11875. front of the next \"|\" separator, to keep the table aligned. The table will
  11876. still be marked for re-alignment if the field did fill the entire column,
  11877. because, in this case the deletion might narrow the column."
  11878. (interactive "p")
  11879. (if (and (org-table-p)
  11880. (not (bolp))
  11881. (not (= (char-after) ?|))
  11882. (eq N 1))
  11883. (if (looking-at ".*?|")
  11884. (let ((pos (point))
  11885. (noalign (looking-at "[^|\n\r]* |"))
  11886. (c org-table-may-need-update))
  11887. (replace-match (concat
  11888. (substring (match-string 0) 1 -1)
  11889. " |"))
  11890. (goto-char pos)
  11891. ;; noalign: if there were two spaces at the end, this field
  11892. ;; does not determine the width of the column.
  11893. (if noalign (setq org-table-may-need-update c)))
  11894. (delete-char N))
  11895. (delete-char N)
  11896. (org-fix-tags-on-the-fly)))
  11897. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11898. (put 'org-self-insert-command 'delete-selection t)
  11899. (put 'orgtbl-self-insert-command 'delete-selection t)
  11900. (put 'org-delete-char 'delete-selection 'supersede)
  11901. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11902. (put 'org-yank 'delete-selection 'yank)
  11903. ;; Make `flyspell-mode' delay after some commands
  11904. (put 'org-self-insert-command 'flyspell-delayed t)
  11905. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11906. (put 'org-delete-char 'flyspell-delayed t)
  11907. (put 'org-delete-backward-char 'flyspell-delayed t)
  11908. ;; Make pabbrev-mode expand after org-mode commands
  11909. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11910. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11911. ;; How to do this: Measure non-white length of current string
  11912. ;; If equal to column width, we should realign.
  11913. (defun org-remap (map &rest commands)
  11914. "In MAP, remap the functions given in COMMANDS.
  11915. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11916. (let (new old)
  11917. (while commands
  11918. (setq old (pop commands) new (pop commands))
  11919. (if (fboundp 'command-remapping)
  11920. (org-defkey map (vector 'remap old) new)
  11921. (substitute-key-definition old new map global-map)))))
  11922. (when (eq org-enable-table-editor 'optimized)
  11923. ;; If the user wants maximum table support, we need to hijack
  11924. ;; some standard editing functions
  11925. (org-remap org-mode-map
  11926. 'self-insert-command 'org-self-insert-command
  11927. 'delete-char 'org-delete-char
  11928. 'delete-backward-char 'org-delete-backward-char)
  11929. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11930. (defun org-modifier-cursor-error ()
  11931. "Throw an error, a modified cursor command was applied in wrong context."
  11932. (error "This command is active in special context like tables, headlines or items"))
  11933. (defun org-shiftselect-error ()
  11934. "Throw an error because Shift-Cursor command was applied in wrong context."
  11935. (if (and (boundp 'shift-select-mode) shift-select-mode)
  11936. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  11937. (error "This command works only in special context like headlines or timestamps.")))
  11938. (defun org-call-for-shift-select (cmd)
  11939. (let ((this-command-keys-shift-translated t))
  11940. (call-interactively cmd)))
  11941. (defun org-shifttab (&optional arg)
  11942. "Global visibility cycling or move to previous table field.
  11943. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11944. on context.
  11945. See the individual commands for more information."
  11946. (interactive "P")
  11947. (cond
  11948. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11949. ((integerp arg)
  11950. (message "Content view to level: %d" arg)
  11951. (org-content (prefix-numeric-value arg))
  11952. (setq org-cycle-global-status 'overview))
  11953. (t (call-interactively 'org-global-cycle))))
  11954. (defun org-shiftmetaleft ()
  11955. "Promote subtree or delete table column.
  11956. Calls `org-promote-subtree', `org-outdent-item',
  11957. or `org-table-delete-column', depending on context.
  11958. See the individual commands for more information."
  11959. (interactive)
  11960. (cond
  11961. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11962. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11963. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11964. (t (org-modifier-cursor-error))))
  11965. (defun org-shiftmetaright ()
  11966. "Demote subtree or insert table column.
  11967. Calls `org-demote-subtree', `org-indent-item',
  11968. or `org-table-insert-column', depending on context.
  11969. See the individual commands for more information."
  11970. (interactive)
  11971. (cond
  11972. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11973. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11974. ((org-at-item-p) (call-interactively 'org-indent-item))
  11975. (t (org-modifier-cursor-error))))
  11976. (defun org-shiftmetaup (&optional arg)
  11977. "Move subtree up or kill table row.
  11978. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11979. `org-move-item-up' depending on context. See the individual commands
  11980. for more information."
  11981. (interactive "P")
  11982. (cond
  11983. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11984. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11985. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11986. (t (org-modifier-cursor-error))))
  11987. (defun org-shiftmetadown (&optional arg)
  11988. "Move subtree down or insert table row.
  11989. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11990. `org-move-item-down', depending on context. See the individual
  11991. commands for more information."
  11992. (interactive "P")
  11993. (cond
  11994. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11995. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11996. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11997. (t (org-modifier-cursor-error))))
  11998. (defun org-metaleft (&optional arg)
  11999. "Promote heading or move table column to left.
  12000. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12001. With no specific context, calls the Emacs default `backward-word'.
  12002. See the individual commands for more information."
  12003. (interactive "P")
  12004. (cond
  12005. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12006. ((or (org-on-heading-p) (org-region-active-p))
  12007. (call-interactively 'org-do-promote))
  12008. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12009. (t (call-interactively 'backward-word))))
  12010. (defun org-metaright (&optional arg)
  12011. "Demote subtree or move table column to right.
  12012. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12013. With no specific context, calls the Emacs default `forward-word'.
  12014. See the individual commands for more information."
  12015. (interactive "P")
  12016. (cond
  12017. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12018. ((or (org-on-heading-p) (org-region-active-p))
  12019. (call-interactively 'org-do-demote))
  12020. ((org-at-item-p) (call-interactively 'org-indent-item))
  12021. (t (call-interactively 'forward-word))))
  12022. (defun org-metaup (&optional arg)
  12023. "Move subtree up or move table row up.
  12024. Calls `org-move-subtree-up' or `org-table-move-row' or
  12025. `org-move-item-up', depending on context. See the individual commands
  12026. for more information."
  12027. (interactive "P")
  12028. (cond
  12029. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12030. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12031. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12032. (t (transpose-lines 1) (beginning-of-line -1))))
  12033. (defun org-metadown (&optional arg)
  12034. "Move subtree down or move table row down.
  12035. Calls `org-move-subtree-down' or `org-table-move-row' or
  12036. `org-move-item-down', depending on context. See the individual
  12037. commands for more information."
  12038. (interactive "P")
  12039. (cond
  12040. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12041. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12042. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12043. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12044. (defun org-shiftup (&optional arg)
  12045. "Increase item in timestamp or increase priority of current headline.
  12046. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12047. depending on context. See the individual commands for more information."
  12048. (interactive "P")
  12049. (cond
  12050. ((and org-support-shift-select (org-region-active-p))
  12051. (org-call-for-shift-select 'previous-line))
  12052. ((org-at-timestamp-p t)
  12053. (call-interactively (if org-edit-timestamp-down-means-later
  12054. 'org-timestamp-down 'org-timestamp-up)))
  12055. ((and (not (eq org-support-shift-select 'always))
  12056. (org-on-heading-p))
  12057. (call-interactively 'org-priority-up))
  12058. ((and (not org-support-shift-select) (org-at-item-p))
  12059. (call-interactively 'org-previous-item))
  12060. ((org-clocktable-try-shift 'up arg))
  12061. (org-support-shift-select
  12062. (org-call-for-shift-select 'previous-line))
  12063. (t (org-shiftselect-error))))
  12064. (defun org-shiftdown (&optional arg)
  12065. "Decrease item in timestamp or decrease priority of current headline.
  12066. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12067. depending on context. See the individual commands for more information."
  12068. (interactive "P")
  12069. (cond
  12070. ((and org-support-shift-select (org-region-active-p))
  12071. (org-call-for-shift-select 'next-line))
  12072. ((org-at-timestamp-p t)
  12073. (call-interactively (if org-edit-timestamp-down-means-later
  12074. 'org-timestamp-up 'org-timestamp-down)))
  12075. ((and (not (eq org-support-shift-select 'always))
  12076. (org-on-heading-p))
  12077. (call-interactively 'org-priority-down))
  12078. ((and (not org-support-shift-select) (org-at-item-p))
  12079. (call-interactively 'org-next-item))
  12080. ((org-clocktable-try-shift 'down arg))
  12081. (org-support-shift-select
  12082. (org-call-for-shift-select 'next-line))
  12083. (t (org-shiftselect-error))))
  12084. (defun org-shiftright (&optional arg)
  12085. "Cycle the thing at point or in the current line, depending on context.
  12086. Depending on context, this does one of the following:
  12087. - switch a timestamp at point one day into the future
  12088. - on a headline, switch to the next TODO keyword.
  12089. - on an item, switch entire list to the next bullet type
  12090. - on a property line, switch to the next allowed value
  12091. - on a clocktable definition line, move time block into the future"
  12092. (interactive "P")
  12093. (cond
  12094. ((and org-support-shift-select (org-region-active-p))
  12095. (org-call-for-shift-select 'forward-char))
  12096. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12097. ((and (not (eq org-support-shift-select 'always))
  12098. (org-on-heading-p))
  12099. (org-call-with-arg 'org-todo 'right))
  12100. ((or (and org-support-shift-select
  12101. (not (eq org-support-shift-select 'always))
  12102. (org-at-item-bullet-p))
  12103. (and (not org-support-shift-select) (org-at-item-p)))
  12104. (org-call-with-arg 'org-cycle-list-bullet nil))
  12105. ((and (not (eq org-support-shift-select 'always))
  12106. (org-at-property-p))
  12107. (call-interactively 'org-property-next-allowed-value))
  12108. ((org-clocktable-try-shift 'right arg))
  12109. (org-support-shift-select
  12110. (org-call-for-shift-select 'forward-char))
  12111. (t (org-shiftselect-error))))
  12112. (defun org-shiftleft (&optional arg)
  12113. "Cycle the thing at point or in the current line, depending on context.
  12114. Depending on context, this does one of the following:
  12115. - switch a timestamp at point one day into the past
  12116. - on a headline, switch to the previous TODO keyword.
  12117. - on an item, switch entire list to the previous bullet type
  12118. - on a property line, switch to the previous allowed value
  12119. - on a clocktable definition line, move time block into the past"
  12120. (interactive "P")
  12121. (cond
  12122. ((and org-support-shift-select (org-region-active-p))
  12123. (org-call-for-shift-select 'backward-char))
  12124. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12125. ((and (not (eq org-support-shift-select 'always))
  12126. (org-on-heading-p))
  12127. (org-call-with-arg 'org-todo 'left))
  12128. ((or (and org-support-shift-select
  12129. (not (eq org-support-shift-select 'always))
  12130. (org-at-item-bullet-p))
  12131. (and (not org-support-shift-select) (org-at-item-p)))
  12132. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12133. ((and (not (eq org-support-shift-select 'always))
  12134. (org-at-property-p))
  12135. (call-interactively 'org-property-previous-allowed-value))
  12136. ((org-clocktable-try-shift 'left arg))
  12137. (org-support-shift-select
  12138. (org-call-for-shift-select 'backward-char))
  12139. (t (org-shiftselect-error))))
  12140. (defun org-shiftcontrolright ()
  12141. "Switch to next TODO set."
  12142. (interactive)
  12143. (cond
  12144. ((and org-support-shift-select (org-region-active-p))
  12145. (org-call-for-shift-select 'forward-word))
  12146. ((and (not (eq org-support-shift-select 'always))
  12147. (org-on-heading-p))
  12148. (org-call-with-arg 'org-todo 'nextset))
  12149. (org-support-shift-select
  12150. (org-call-for-shift-select 'forward-word))
  12151. (t (org-shiftselect-error))))
  12152. (defun org-shiftcontrolleft ()
  12153. "Switch to previous TODO set."
  12154. (interactive)
  12155. (cond
  12156. ((and org-support-shift-select (org-region-active-p))
  12157. (org-call-for-shift-select 'backward-word))
  12158. ((and (not (eq org-support-shift-select 'always))
  12159. (org-on-heading-p))
  12160. (org-call-with-arg 'org-todo 'previousset))
  12161. (org-support-shift-select
  12162. (org-call-for-shift-select 'backward-word))
  12163. (t (org-shiftselect-error))))
  12164. (defun org-ctrl-c-ret ()
  12165. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12166. (interactive)
  12167. (cond
  12168. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12169. (t (call-interactively 'org-insert-heading))))
  12170. (defun org-copy-special ()
  12171. "Copy region in table or copy current subtree.
  12172. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12173. See the individual commands for more information."
  12174. (interactive)
  12175. (call-interactively
  12176. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12177. (defun org-cut-special ()
  12178. "Cut region in table or cut current subtree.
  12179. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12180. See the individual commands for more information."
  12181. (interactive)
  12182. (call-interactively
  12183. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12184. (defun org-paste-special (arg)
  12185. "Paste rectangular region into table, or past subtree relative to level.
  12186. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12187. See the individual commands for more information."
  12188. (interactive "P")
  12189. (if (org-at-table-p)
  12190. (org-table-paste-rectangle)
  12191. (org-paste-subtree arg)))
  12192. (defun org-edit-special ()
  12193. "Call a special editor for the stuff at point.
  12194. When at a table, call the formula editor with `org-table-edit-formulas'.
  12195. When at the first line of an src example, call `org-edit-src-code'.
  12196. When in an #+include line, visit the include file. Otherwise call
  12197. `ffap' to visit the file at point."
  12198. (interactive)
  12199. (cond
  12200. ((org-at-table-p)
  12201. (call-interactively 'org-table-edit-formulas))
  12202. ((save-excursion
  12203. (beginning-of-line 1)
  12204. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12205. (find-file (org-trim (match-string 1))))
  12206. ((org-edit-src-code))
  12207. ((org-edit-fixed-width-region))
  12208. (t (call-interactively 'ffap))))
  12209. (defun org-ctrl-c-ctrl-c (&optional arg)
  12210. "Set tags in headline, or update according to changed information at point.
  12211. This command does many different things, depending on context:
  12212. - If the cursor is in a headline, prompt for tags and insert them
  12213. into the current line, aligned to `org-tags-column'. When called
  12214. with prefix arg, realign all tags in the current buffer.
  12215. - If the cursor is in one of the special #+KEYWORD lines, this
  12216. triggers scanning the buffer for these lines and updating the
  12217. information.
  12218. - If the cursor is inside a table, realign the table. This command
  12219. works even if the automatic table editor has been turned off.
  12220. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12221. the entire table.
  12222. - If the cursor is at a footnote reference or definition, jump to
  12223. the corresponding definition or references, respectively.
  12224. - If the cursor is a the beginning of a dynamic block, update it.
  12225. - If the cursor is inside a table created by the table.el package,
  12226. activate that table.
  12227. - If the current buffer is a remember buffer, close note and file
  12228. it. A prefix argument of 1 files to the default location
  12229. without further interaction. A prefix argument of 2 files to
  12230. the currently clocking task.
  12231. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12232. links in this buffer.
  12233. - If the cursor is on a numbered item in a plain list, renumber the
  12234. ordered list.
  12235. - If the cursor is on a checkbox, toggle it."
  12236. (interactive "P")
  12237. (let ((org-enable-table-editor t))
  12238. (cond
  12239. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12240. org-occur-highlights
  12241. org-latex-fragment-image-overlays)
  12242. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12243. (org-remove-occur-highlights)
  12244. (org-remove-latex-fragment-image-overlays)
  12245. (message "Temporary highlights/overlays removed from current buffer"))
  12246. ((and (local-variable-p 'org-finish-function (current-buffer))
  12247. (fboundp org-finish-function))
  12248. (funcall org-finish-function))
  12249. ((org-at-property-p)
  12250. (call-interactively 'org-property-action))
  12251. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12252. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12253. ((org-at-table.el-p)
  12254. (require 'table)
  12255. (beginning-of-line 1)
  12256. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12257. (call-interactively 'table-recognize-table))
  12258. ((org-at-table-p)
  12259. (org-table-maybe-eval-formula)
  12260. (if arg
  12261. (call-interactively 'org-table-recalculate)
  12262. (org-table-maybe-recalculate-line))
  12263. (call-interactively 'org-table-align))
  12264. ((or (org-footnote-at-reference-p)
  12265. (org-footnote-at-definition-p))
  12266. (call-interactively 'org-footnote-action))
  12267. ((org-at-item-checkbox-p)
  12268. (call-interactively 'org-toggle-checkbox))
  12269. ((org-at-item-p)
  12270. (call-interactively 'org-maybe-renumber-ordered-list))
  12271. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12272. ;; Dynamic block
  12273. (beginning-of-line 1)
  12274. (save-excursion (org-update-dblock)))
  12275. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12276. (cond
  12277. ((equal (match-string 1) "TBLFM")
  12278. ;; Recalculate the table before this line
  12279. (save-excursion
  12280. (beginning-of-line 1)
  12281. (skip-chars-backward " \r\n\t")
  12282. (if (org-at-table-p)
  12283. (org-call-with-arg 'org-table-recalculate t))))
  12284. (t
  12285. ; (org-set-regexps-and-options)
  12286. ; (org-restart-font-lock)
  12287. (let ((org-inhibit-startup t)) (org-mode-restart))
  12288. (message "Local setup has been refreshed"))))
  12289. (t (error "C-c C-c can do nothing useful at this location.")))))
  12290. (defun org-mode-restart ()
  12291. "Restart Org-mode, to scan again for special lines.
  12292. Also updates the keyword regular expressions."
  12293. (interactive)
  12294. (org-mode)
  12295. (message "Org-mode restarted"))
  12296. (defun org-kill-note-or-show-branches ()
  12297. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12298. (interactive)
  12299. (if (not org-finish-function)
  12300. (call-interactively 'show-branches)
  12301. (let ((org-note-abort t))
  12302. (funcall org-finish-function))))
  12303. (defun org-return (&optional indent)
  12304. "Goto next table row or insert a newline.
  12305. Calls `org-table-next-row' or `newline', depending on context.
  12306. See the individual commands for more information."
  12307. (interactive)
  12308. (cond
  12309. ((bobp) (if indent (newline-and-indent) (newline)))
  12310. ((and (org-at-heading-p)
  12311. (looking-at
  12312. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12313. (org-show-entry)
  12314. (end-of-line 1)
  12315. (newline))
  12316. ((org-at-table-p)
  12317. (org-table-justify-field-maybe)
  12318. (call-interactively 'org-table-next-row))
  12319. (t (if indent (newline-and-indent) (newline)))))
  12320. (defun org-return-indent ()
  12321. "Goto next table row or insert a newline and indent.
  12322. Calls `org-table-next-row' or `newline-and-indent', depending on
  12323. context. See the individual commands for more information."
  12324. (interactive)
  12325. (org-return t))
  12326. (defun org-ctrl-c-star ()
  12327. "Compute table, or change heading status of lines.
  12328. Calls `org-table-recalculate' or `org-toggle-heading',
  12329. depending on context."
  12330. (interactive)
  12331. (cond
  12332. ((org-at-table-p)
  12333. (call-interactively 'org-table-recalculate))
  12334. (t
  12335. ;; Convert all lines in region to list items
  12336. (call-interactively 'org-toggle-heading))))
  12337. (defun org-ctrl-c-minus ()
  12338. "Insert separator line in table or modify bullet status of line.
  12339. Also turns a plain line or a region of lines into list items.
  12340. Calls `org-table-insert-hline', `org-toggle-item', or
  12341. `org-cycle-list-bullet', depending on context."
  12342. (interactive)
  12343. (cond
  12344. ((org-at-table-p)
  12345. (call-interactively 'org-table-insert-hline))
  12346. ((org-region-active-p)
  12347. (call-interactively 'org-toggle-item))
  12348. ((org-in-item-p)
  12349. (call-interactively 'org-cycle-list-bullet))
  12350. (t
  12351. (call-interactively 'org-toggle-item))))
  12352. (defun org-toggle-item ()
  12353. "Convert headings or normal lines to items, items to normal lines.
  12354. If there is no active region, only the current line is considered.
  12355. If the first line in the region is a headline, convert all headlines to items.
  12356. If the first line in the region is an item, convert all items to normal lines.
  12357. If the first line is normal text, add an item bullet to each line."
  12358. (interactive)
  12359. (let (l2 l beg end)
  12360. (if (org-region-active-p)
  12361. (setq beg (region-beginning) end (region-end))
  12362. (setq beg (point-at-bol)
  12363. end (min (1+ (point-at-eol)) (point-max))))
  12364. (save-excursion
  12365. (goto-char end)
  12366. (setq l2 (org-current-line))
  12367. (goto-char beg)
  12368. (beginning-of-line 1)
  12369. (setq l (1- (org-current-line)))
  12370. (if (org-at-item-p)
  12371. ;; We already have items, de-itemize
  12372. (while (< (setq l (1+ l)) l2)
  12373. (when (org-at-item-p)
  12374. (goto-char (match-beginning 2))
  12375. (delete-region (match-beginning 2) (match-end 2))
  12376. (and (looking-at "[ \t]+") (replace-match "")))
  12377. (beginning-of-line 2))
  12378. (if (org-on-heading-p)
  12379. ;; Headings, convert to items
  12380. (while (< (setq l (1+ l)) l2)
  12381. (if (looking-at org-outline-regexp)
  12382. (replace-match "- " t t))
  12383. (beginning-of-line 2))
  12384. ;; normal lines, turn them into items
  12385. (while (< (setq l (1+ l)) l2)
  12386. (unless (org-at-item-p)
  12387. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12388. (replace-match "\\1- \\2")))
  12389. (beginning-of-line 2)))))))
  12390. (defun org-toggle-heading (&optional nstars)
  12391. "Convert headings to normal text, or items or text to headings.
  12392. If there is no active region, only the current line is considered.
  12393. If the first line is a heading, remove the stars from all headlines
  12394. in the region.
  12395. If the first line is a plain list item, turn all plain list items into
  12396. headings.
  12397. If the first line is a normal line, turn each and every line in the region
  12398. into a heading.
  12399. When converting a line into a heading, the number of stars is chosen
  12400. such that the lines become children of the current entry. However, when
  12401. a prefix argument is given, its value determines the number of stars to add."
  12402. (interactive "P")
  12403. (let (l2 l itemp beg end)
  12404. (if (org-region-active-p)
  12405. (setq beg (region-beginning) end (region-end))
  12406. (setq beg (point-at-bol)
  12407. end (min (1+ (point-at-eol)) (point-max))))
  12408. (save-excursion
  12409. (goto-char end)
  12410. (setq l2 (org-current-line))
  12411. (goto-char beg)
  12412. (beginning-of-line 1)
  12413. (setq l (1- (org-current-line)))
  12414. (if (org-on-heading-p)
  12415. ;; We already have headlines, de-star them
  12416. (while (< (setq l (1+ l)) l2)
  12417. (when (org-on-heading-p t)
  12418. (and (looking-at outline-regexp) (replace-match "")))
  12419. (beginning-of-line 2))
  12420. (setq itemp (org-at-item-p))
  12421. (let* ((stars
  12422. (if nstars
  12423. (make-string (prefix-numeric-value current-prefix-arg)
  12424. ?*)
  12425. (save-excursion
  12426. (re-search-backward org-complex-heading-regexp nil t)
  12427. (or (match-string 1) "*"))))
  12428. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12429. (rpl (concat stars add-stars " ")))
  12430. (while (< (setq l (1+ l)) l2)
  12431. (if itemp
  12432. (and (org-at-item-p) (replace-match rpl t t))
  12433. (unless (org-on-heading-p)
  12434. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12435. (replace-match (concat rpl (match-string 2))))))
  12436. (beginning-of-line 2)))))))
  12437. (defun org-meta-return (&optional arg)
  12438. "Insert a new heading or wrap a region in a table.
  12439. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12440. See the individual commands for more information."
  12441. (interactive "P")
  12442. (cond
  12443. ((org-at-table-p)
  12444. (call-interactively 'org-table-wrap-region))
  12445. (t (call-interactively 'org-insert-heading))))
  12446. ;;; Menu entries
  12447. ;; Define the Org-mode menus
  12448. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12449. '("Tbl"
  12450. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12451. ["Next Field" org-cycle (org-at-table-p)]
  12452. ["Previous Field" org-shifttab (org-at-table-p)]
  12453. ["Next Row" org-return (org-at-table-p)]
  12454. "--"
  12455. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12456. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12457. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12458. "--"
  12459. ("Column"
  12460. ["Move Column Left" org-metaleft (org-at-table-p)]
  12461. ["Move Column Right" org-metaright (org-at-table-p)]
  12462. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12463. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12464. ("Row"
  12465. ["Move Row Up" org-metaup (org-at-table-p)]
  12466. ["Move Row Down" org-metadown (org-at-table-p)]
  12467. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12468. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12469. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12470. "--"
  12471. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12472. ("Rectangle"
  12473. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12474. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12475. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12476. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12477. "--"
  12478. ("Calculate"
  12479. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12480. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12481. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12482. "--"
  12483. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12484. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12485. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12486. "--"
  12487. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12488. "--"
  12489. ["Sum Column/Rectangle" org-table-sum
  12490. (or (org-at-table-p) (org-region-active-p))]
  12491. ["Which Column?" org-table-current-column (org-at-table-p)])
  12492. ["Debug Formulas"
  12493. org-table-toggle-formula-debugger
  12494. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12495. ["Show Col/Row Numbers"
  12496. org-table-toggle-coordinate-overlays
  12497. :style toggle
  12498. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12499. "--"
  12500. ["Create" org-table-create (and (not (org-at-table-p))
  12501. org-enable-table-editor)]
  12502. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12503. ["Import from File" org-table-import (not (org-at-table-p))]
  12504. ["Export to File" org-table-export (org-at-table-p)]
  12505. "--"
  12506. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12507. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12508. '("Org"
  12509. ("Show/Hide"
  12510. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12511. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12512. ["Sparse Tree..." org-sparse-tree t]
  12513. ["Reveal Context" org-reveal t]
  12514. ["Show All" show-all t]
  12515. "--"
  12516. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12517. "--"
  12518. ["New Heading" org-insert-heading t]
  12519. ("Navigate Headings"
  12520. ["Up" outline-up-heading t]
  12521. ["Next" outline-next-visible-heading t]
  12522. ["Previous" outline-previous-visible-heading t]
  12523. ["Next Same Level" outline-forward-same-level t]
  12524. ["Previous Same Level" outline-backward-same-level t]
  12525. "--"
  12526. ["Jump" org-goto t])
  12527. ("Edit Structure"
  12528. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12529. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12530. "--"
  12531. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12532. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12533. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12534. "--"
  12535. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12536. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12537. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12538. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12539. "--"
  12540. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12541. "--"
  12542. ["Convert to odd levels" org-convert-to-odd-levels t]
  12543. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12544. ("Editing"
  12545. ["Emphasis..." org-emphasize t]
  12546. ["Edit Source Example" org-edit-special t]
  12547. "--"
  12548. ["Footnote new/jump" org-footnote-action t]
  12549. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12550. ("Archive"
  12551. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12552. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12553. ; :active t :keys "C-u C-c C-x C-a"]
  12554. ["Sparse trees open ARCHIVE trees"
  12555. (setq org-sparse-tree-open-archived-trees
  12556. (not org-sparse-tree-open-archived-trees))
  12557. :style toggle :selected org-sparse-tree-open-archived-trees]
  12558. ["Cycling opens ARCHIVE trees"
  12559. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12560. :style toggle :selected org-cycle-open-archived-trees]
  12561. "--"
  12562. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12563. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12564. ; ["Check and Move Children" (org-archive-subtree '(4))
  12565. ; :active t :keys "C-u C-c C-x C-s"]
  12566. )
  12567. "--"
  12568. ("TODO Lists"
  12569. ["TODO/DONE/-" org-todo t]
  12570. ("Select keyword"
  12571. ["Next keyword" org-shiftright (org-on-heading-p)]
  12572. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12573. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12574. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12575. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12576. ["Show TODO Tree" org-show-todo-tree t]
  12577. ["Global TODO list" org-todo-list t]
  12578. "--"
  12579. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12580. :selected org-enforce-todo-dependencies :style toggle :active t]
  12581. "Settings for tree at point"
  12582. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12583. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12584. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12585. ["Do Children parallel" org-toggle-ordered-property :style radio
  12586. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12587. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12588. "--"
  12589. ["Set Priority" org-priority t]
  12590. ["Priority Up" org-shiftup t]
  12591. ["Priority Down" org-shiftdown t])
  12592. ("TAGS and Properties"
  12593. ["Set Tags" org-set-tags-command t]
  12594. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12595. "--"
  12596. ["Set property" org-set-property t]
  12597. ["Column view of properties" org-columns t]
  12598. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12599. ("Dates and Scheduling"
  12600. ["Timestamp" org-time-stamp t]
  12601. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12602. ("Change Date"
  12603. ["1 Day Later" org-shiftright t]
  12604. ["1 Day Earlier" org-shiftleft t]
  12605. ["1 ... Later" org-shiftup t]
  12606. ["1 ... Earlier" org-shiftdown t])
  12607. ["Compute Time Range" org-evaluate-time-range t]
  12608. ["Schedule Item" org-schedule t]
  12609. ["Deadline" org-deadline t]
  12610. "--"
  12611. ["Custom time format" org-toggle-time-stamp-overlays
  12612. :style radio :selected org-display-custom-times]
  12613. "--"
  12614. ["Goto Calendar" org-goto-calendar t]
  12615. ["Date from Calendar" org-date-from-calendar t]
  12616. "--"
  12617. ["Start/Restart Timer" org-timer-start t]
  12618. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12619. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12620. ["Insert Timer String" org-timer t]
  12621. ["Insert Timer Item" org-timer-item t])
  12622. ("Logging work"
  12623. ["Clock in" org-clock-in t]
  12624. ["Clock out" org-clock-out t]
  12625. ["Clock cancel" org-clock-cancel t]
  12626. ["Goto running clock" org-clock-goto t]
  12627. ["Display times" org-clock-display t]
  12628. ["Create clock table" org-clock-report t]
  12629. "--"
  12630. ["Record DONE time"
  12631. (progn (setq org-log-done (not org-log-done))
  12632. (message "Switching to %s will %s record a timestamp"
  12633. (car org-done-keywords)
  12634. (if org-log-done "automatically" "not")))
  12635. :style toggle :selected org-log-done])
  12636. "--"
  12637. ["Agenda Command..." org-agenda t]
  12638. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12639. ("File List for Agenda")
  12640. ("Special views current file"
  12641. ["TODO Tree" org-show-todo-tree t]
  12642. ["Check Deadlines" org-check-deadlines t]
  12643. ["Timeline" org-timeline t]
  12644. ["Tags Tree" org-tags-sparse-tree t])
  12645. "--"
  12646. ("Hyperlinks"
  12647. ["Store Link (Global)" org-store-link t]
  12648. ["Insert Link" org-insert-link t]
  12649. ["Follow Link" org-open-at-point t]
  12650. "--"
  12651. ["Next link" org-next-link t]
  12652. ["Previous link" org-previous-link t]
  12653. "--"
  12654. ["Descriptive Links"
  12655. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12656. :style radio
  12657. :selected (member '(org-link) buffer-invisibility-spec)]
  12658. ["Literal Links"
  12659. (progn
  12660. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12661. :style radio
  12662. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12663. "--"
  12664. ["Export/Publish..." org-export t]
  12665. ("LaTeX"
  12666. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12667. :selected org-cdlatex-mode]
  12668. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12669. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12670. ["Modify math symbol" org-cdlatex-math-modify
  12671. (org-inside-LaTeX-fragment-p)]
  12672. ["Export LaTeX fragments as images"
  12673. (if (featurep 'org-exp)
  12674. (setq org-export-with-LaTeX-fragments
  12675. (not org-export-with-LaTeX-fragments))
  12676. (require 'org-exp))
  12677. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12678. org-export-with-LaTeX-fragments)])
  12679. "--"
  12680. ("Documentation"
  12681. ["Show Version" org-version t]
  12682. ["Info Documentation" org-info t])
  12683. ("Customize"
  12684. ["Browse Org Group" org-customize t]
  12685. "--"
  12686. ["Expand This Menu" org-create-customize-menu
  12687. (fboundp 'customize-menu-create)])
  12688. "--"
  12689. ["Refresh setup" org-mode-restart t]
  12690. ))
  12691. (defun org-info (&optional node)
  12692. "Read documentation for Org-mode in the info system.
  12693. With optional NODE, go directly to that node."
  12694. (interactive)
  12695. (info (format "(org)%s" (or node ""))))
  12696. (defun org-install-agenda-files-menu ()
  12697. (let ((bl (buffer-list)))
  12698. (save-excursion
  12699. (while bl
  12700. (set-buffer (pop bl))
  12701. (if (org-mode-p) (setq bl nil)))
  12702. (when (org-mode-p)
  12703. (easy-menu-change
  12704. '("Org") "File List for Agenda"
  12705. (append
  12706. (list
  12707. ["Edit File List" (org-edit-agenda-file-list) t]
  12708. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12709. ["Remove Current File from List" org-remove-file t]
  12710. ["Cycle through agenda files" org-cycle-agenda-files t]
  12711. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12712. "--")
  12713. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12714. ;;;; Documentation
  12715. ;;;###autoload
  12716. (defun org-require-autoloaded-modules ()
  12717. (interactive)
  12718. (mapc 'require
  12719. '(org-agenda org-archive org-clock org-colview
  12720. org-exp org-id org-export-latex org-publish
  12721. org-remember org-table)))
  12722. ;;;###autoload
  12723. (defun org-customize ()
  12724. "Call the customize function with org as argument."
  12725. (interactive)
  12726. (org-load-modules-maybe)
  12727. (org-require-autoloaded-modules)
  12728. (customize-browse 'org))
  12729. (defun org-create-customize-menu ()
  12730. "Create a full customization menu for Org-mode, insert it into the menu."
  12731. (interactive)
  12732. (org-load-modules-maybe)
  12733. (org-require-autoloaded-modules)
  12734. (if (fboundp 'customize-menu-create)
  12735. (progn
  12736. (easy-menu-change
  12737. '("Org") "Customize"
  12738. `(["Browse Org group" org-customize t]
  12739. "--"
  12740. ,(customize-menu-create 'org)
  12741. ["Set" Custom-set t]
  12742. ["Save" Custom-save t]
  12743. ["Reset to Current" Custom-reset-current t]
  12744. ["Reset to Saved" Custom-reset-saved t]
  12745. ["Reset to Standard Settings" Custom-reset-standard t]))
  12746. (message "\"Org\"-menu now contains full customization menu"))
  12747. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12748. ;;;; Miscellaneous stuff
  12749. ;;; Generally useful functions
  12750. (defun org-find-text-property-in-string (prop s)
  12751. "Return the first non-nil value of property PROP in string S."
  12752. (or (get-text-property 0 prop s)
  12753. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12754. prop s)))
  12755. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12756. "Display the given MESSAGE as a warning."
  12757. (if (fboundp 'display-warning)
  12758. (display-warning 'org message
  12759. (if (featurep 'xemacs)
  12760. 'warning
  12761. :warning))
  12762. (let ((buf (get-buffer-create "*Org warnings*")))
  12763. (with-current-buffer buf
  12764. (goto-char (point-max))
  12765. (insert "Warning (Org): " message)
  12766. (unless (bolp)
  12767. (newline)))
  12768. (display-buffer buf)
  12769. (sit-for 0))))
  12770. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12771. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12772. (if (and marker (marker-buffer marker)
  12773. (buffer-live-p (marker-buffer marker)))
  12774. (progn
  12775. (switch-to-buffer (marker-buffer marker))
  12776. (if (or (> marker (point-max)) (< marker (point-min)))
  12777. (widen))
  12778. (goto-char marker)
  12779. (org-show-context 'org-goto))
  12780. (if bookmark
  12781. (bookmark-jump bookmark)
  12782. (error "Cannot find location"))))
  12783. (defun org-quote-csv-field (s)
  12784. "Quote field for inclusion in CSV material."
  12785. (if (string-match "[\",]" s)
  12786. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12787. s))
  12788. (defun org-plist-delete (plist property)
  12789. "Delete PROPERTY from PLIST.
  12790. This is in contrast to merely setting it to 0."
  12791. (let (p)
  12792. (while plist
  12793. (if (not (eq property (car plist)))
  12794. (setq p (plist-put p (car plist) (nth 1 plist))))
  12795. (setq plist (cddr plist)))
  12796. p))
  12797. (defun org-force-self-insert (N)
  12798. "Needed to enforce self-insert under remapping."
  12799. (interactive "p")
  12800. (self-insert-command N))
  12801. (defun org-string-width (s)
  12802. "Compute width of string, ignoring invisible characters.
  12803. This ignores character with invisibility property `org-link', and also
  12804. characters with property `org-cwidth', because these will become invisible
  12805. upon the next fontification round."
  12806. (let (b l)
  12807. (when (or (eq t buffer-invisibility-spec)
  12808. (assq 'org-link buffer-invisibility-spec))
  12809. (while (setq b (text-property-any 0 (length s)
  12810. 'invisible 'org-link s))
  12811. (setq s (concat (substring s 0 b)
  12812. (substring s (or (next-single-property-change
  12813. b 'invisible s) (length s)))))))
  12814. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12815. (setq s (concat (substring s 0 b)
  12816. (substring s (or (next-single-property-change
  12817. b 'org-cwidth s) (length s))))))
  12818. (setq l (string-width s) b -1)
  12819. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12820. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12821. l))
  12822. (defun org-get-indentation (&optional line)
  12823. "Get the indentation of the current line, interpreting tabs.
  12824. When LINE is given, assume it represents a line and compute its indentation."
  12825. (if line
  12826. (if (string-match "^ *" (org-remove-tabs line))
  12827. (match-end 0))
  12828. (save-excursion
  12829. (beginning-of-line 1)
  12830. (skip-chars-forward " \t")
  12831. (current-column))))
  12832. (defun org-remove-tabs (s &optional width)
  12833. "Replace tabulators in S with spaces.
  12834. Assumes that s is a single line, starting in column 0."
  12835. (setq width (or width tab-width))
  12836. (while (string-match "\t" s)
  12837. (setq s (replace-match
  12838. (make-string
  12839. (- (* width (/ (+ (match-beginning 0) width) width))
  12840. (match-beginning 0)) ?\ )
  12841. t t s)))
  12842. s)
  12843. (defun org-fix-indentation (line ind)
  12844. "Fix indentation in LINE.
  12845. IND is a cons cell with target and minimum indentation.
  12846. If the current indentation in LINE is smaller than the minimum,
  12847. leave it alone. If it is larger than ind, set it to the target."
  12848. (let* ((l (org-remove-tabs line))
  12849. (i (org-get-indentation l))
  12850. (i1 (car ind)) (i2 (cdr ind)))
  12851. (if (>= i i2) (setq l (substring line i2)))
  12852. (if (> i1 0)
  12853. (concat (make-string i1 ?\ ) l)
  12854. l)))
  12855. (defun org-base-buffer (buffer)
  12856. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12857. (if (not buffer)
  12858. buffer
  12859. (or (buffer-base-buffer buffer)
  12860. buffer)))
  12861. (defun org-trim (s)
  12862. "Remove whitespace at beginning and end of string."
  12863. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12864. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12865. s)
  12866. (defun org-wrap (string &optional width lines)
  12867. "Wrap string to either a number of lines, or a width in characters.
  12868. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12869. that costs. If there is a word longer than WIDTH, the text is actually
  12870. wrapped to the length of that word.
  12871. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12872. many lines, whatever width that takes.
  12873. The return value is a list of lines, without newlines at the end."
  12874. (let* ((words (org-split-string string "[ \t\n]+"))
  12875. (maxword (apply 'max (mapcar 'org-string-width words)))
  12876. w ll)
  12877. (cond (width
  12878. (org-do-wrap words (max maxword width)))
  12879. (lines
  12880. (setq w maxword)
  12881. (setq ll (org-do-wrap words maxword))
  12882. (if (<= (length ll) lines)
  12883. ll
  12884. (setq ll words)
  12885. (while (> (length ll) lines)
  12886. (setq w (1+ w))
  12887. (setq ll (org-do-wrap words w)))
  12888. ll))
  12889. (t (error "Cannot wrap this")))))
  12890. (defun org-do-wrap (words width)
  12891. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12892. (let (lines line)
  12893. (while words
  12894. (setq line (pop words))
  12895. (while (and words (< (+ (length line) (length (car words))) width))
  12896. (setq line (concat line " " (pop words))))
  12897. (setq lines (push line lines)))
  12898. (nreverse lines)))
  12899. (defun org-split-string (string &optional separators)
  12900. "Splits STRING into substrings at SEPARATORS.
  12901. No empty strings are returned if there are matches at the beginning
  12902. and end of string."
  12903. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12904. (start 0)
  12905. notfirst
  12906. (list nil))
  12907. (while (and (string-match rexp string
  12908. (if (and notfirst
  12909. (= start (match-beginning 0))
  12910. (< start (length string)))
  12911. (1+ start) start))
  12912. (< (match-beginning 0) (length string)))
  12913. (setq notfirst t)
  12914. (or (eq (match-beginning 0) 0)
  12915. (and (eq (match-beginning 0) (match-end 0))
  12916. (eq (match-beginning 0) start))
  12917. (setq list
  12918. (cons (substring string start (match-beginning 0))
  12919. list)))
  12920. (setq start (match-end 0)))
  12921. (or (eq start (length string))
  12922. (setq list
  12923. (cons (substring string start)
  12924. list)))
  12925. (nreverse list)))
  12926. (defun org-context ()
  12927. "Return a list of contexts of the current cursor position.
  12928. If several contexts apply, all are returned.
  12929. Each context entry is a list with a symbol naming the context, and
  12930. two positions indicating start and end of the context. Possible
  12931. contexts are:
  12932. :headline anywhere in a headline
  12933. :headline-stars on the leading stars in a headline
  12934. :todo-keyword on a TODO keyword (including DONE) in a headline
  12935. :tags on the TAGS in a headline
  12936. :priority on the priority cookie in a headline
  12937. :item on the first line of a plain list item
  12938. :item-bullet on the bullet/number of a plain list item
  12939. :checkbox on the checkbox in a plain list item
  12940. :table in an org-mode table
  12941. :table-special on a special filed in a table
  12942. :table-table in a table.el table
  12943. :link on a hyperlink
  12944. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12945. :target on a <<target>>
  12946. :radio-target on a <<<radio-target>>>
  12947. :latex-fragment on a LaTeX fragment
  12948. :latex-preview on a LaTeX fragment with overlayed preview image
  12949. This function expects the position to be visible because it uses font-lock
  12950. faces as a help to recognize the following contexts: :table-special, :link,
  12951. and :keyword."
  12952. (let* ((f (get-text-property (point) 'face))
  12953. (faces (if (listp f) f (list f)))
  12954. (p (point)) clist o)
  12955. ;; First the large context
  12956. (cond
  12957. ((org-on-heading-p t)
  12958. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12959. (when (progn
  12960. (beginning-of-line 1)
  12961. (looking-at org-todo-line-tags-regexp))
  12962. (push (org-point-in-group p 1 :headline-stars) clist)
  12963. (push (org-point-in-group p 2 :todo-keyword) clist)
  12964. (push (org-point-in-group p 4 :tags) clist))
  12965. (goto-char p)
  12966. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12967. (if (looking-at "\\[#[A-Z0-9]\\]")
  12968. (push (org-point-in-group p 0 :priority) clist)))
  12969. ((org-at-item-p)
  12970. (push (org-point-in-group p 2 :item-bullet) clist)
  12971. (push (list :item (point-at-bol)
  12972. (save-excursion (org-end-of-item) (point)))
  12973. clist)
  12974. (and (org-at-item-checkbox-p)
  12975. (push (org-point-in-group p 0 :checkbox) clist)))
  12976. ((org-at-table-p)
  12977. (push (list :table (org-table-begin) (org-table-end)) clist)
  12978. (if (memq 'org-formula faces)
  12979. (push (list :table-special
  12980. (previous-single-property-change p 'face)
  12981. (next-single-property-change p 'face)) clist)))
  12982. ((org-at-table-p 'any)
  12983. (push (list :table-table) clist)))
  12984. (goto-char p)
  12985. ;; Now the small context
  12986. (cond
  12987. ((org-at-timestamp-p)
  12988. (push (org-point-in-group p 0 :timestamp) clist))
  12989. ((memq 'org-link faces)
  12990. (push (list :link
  12991. (previous-single-property-change p 'face)
  12992. (next-single-property-change p 'face)) clist))
  12993. ((memq 'org-special-keyword faces)
  12994. (push (list :keyword
  12995. (previous-single-property-change p 'face)
  12996. (next-single-property-change p 'face)) clist))
  12997. ((org-on-target-p)
  12998. (push (org-point-in-group p 0 :target) clist)
  12999. (goto-char (1- (match-beginning 0)))
  13000. (if (looking-at org-radio-target-regexp)
  13001. (push (org-point-in-group p 0 :radio-target) clist))
  13002. (goto-char p))
  13003. ((setq o (car (delq nil
  13004. (mapcar
  13005. (lambda (x)
  13006. (if (memq x org-latex-fragment-image-overlays) x))
  13007. (org-overlays-at (point))))))
  13008. (push (list :latex-fragment
  13009. (org-overlay-start o) (org-overlay-end o)) clist)
  13010. (push (list :latex-preview
  13011. (org-overlay-start o) (org-overlay-end o)) clist))
  13012. ((org-inside-LaTeX-fragment-p)
  13013. ;; FIXME: positions wrong.
  13014. (push (list :latex-fragment (point) (point)) clist)))
  13015. (setq clist (nreverse (delq nil clist)))
  13016. clist))
  13017. ;; FIXME: Compare with at-regexp-p Do we need both?
  13018. (defun org-in-regexp (re &optional nlines visually)
  13019. "Check if point is inside a match of regexp.
  13020. Normally only the current line is checked, but you can include NLINES extra
  13021. lines both before and after point into the search.
  13022. If VISUALLY is set, require that the cursor is not after the match but
  13023. really on, so that the block visually is on the match."
  13024. (catch 'exit
  13025. (let ((pos (point))
  13026. (eol (point-at-eol (+ 1 (or nlines 0))))
  13027. (inc (if visually 1 0)))
  13028. (save-excursion
  13029. (beginning-of-line (- 1 (or nlines 0)))
  13030. (while (re-search-forward re eol t)
  13031. (if (and (<= (match-beginning 0) pos)
  13032. (>= (+ inc (match-end 0)) pos))
  13033. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13034. (defun org-at-regexp-p (regexp)
  13035. "Is point inside a match of REGEXP in the current line?"
  13036. (catch 'exit
  13037. (save-excursion
  13038. (let ((pos (point)) (end (point-at-eol)))
  13039. (beginning-of-line 1)
  13040. (while (re-search-forward regexp end t)
  13041. (if (and (<= (match-beginning 0) pos)
  13042. (>= (match-end 0) pos))
  13043. (throw 'exit t)))
  13044. nil))))
  13045. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13046. "Call `multi-occur' with buffers for all agenda files."
  13047. (interactive "sOrg-files matching: \np")
  13048. (let* ((files (org-agenda-files))
  13049. (tnames (mapcar 'file-truename files))
  13050. (extra org-agenda-text-search-extra-files)
  13051. f)
  13052. (when (eq (car extra) 'agenda-archives)
  13053. (setq extra (cdr extra))
  13054. (setq files (org-add-archive-files files)))
  13055. (while (setq f (pop extra))
  13056. (unless (member (file-truename f) tnames)
  13057. (add-to-list 'files f 'append)
  13058. (add-to-list 'tnames (file-truename f) 'append)))
  13059. (multi-occur
  13060. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13061. regexp)))
  13062. (if (boundp 'occur-mode-find-occurrence-hook)
  13063. ;; Emacs 23
  13064. (add-hook 'occur-mode-find-occurrence-hook
  13065. (lambda ()
  13066. (when (org-mode-p)
  13067. (org-reveal))))
  13068. ;; Emacs 22
  13069. (defadvice occur-mode-goto-occurrence
  13070. (after org-occur-reveal activate)
  13071. (and (org-mode-p) (org-reveal)))
  13072. (defadvice occur-mode-goto-occurrence-other-window
  13073. (after org-occur-reveal activate)
  13074. (and (org-mode-p) (org-reveal)))
  13075. (defadvice occur-mode-display-occurrence
  13076. (after org-occur-reveal activate)
  13077. (when (org-mode-p)
  13078. (let ((pos (occur-mode-find-occurrence)))
  13079. (with-current-buffer (marker-buffer pos)
  13080. (save-excursion
  13081. (goto-char pos)
  13082. (org-reveal)))))))
  13083. (defun org-uniquify (list)
  13084. "Remove duplicate elements from LIST."
  13085. (let (res)
  13086. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13087. res))
  13088. (defun org-delete-all (elts list)
  13089. "Remove all elements in ELTS from LIST."
  13090. (while elts
  13091. (setq list (delete (pop elts) list)))
  13092. list)
  13093. (defun org-back-over-empty-lines ()
  13094. "Move backwards over whitespace, to the beginning of the first empty line.
  13095. Returns the number of empty lines passed."
  13096. (let ((pos (point)))
  13097. (skip-chars-backward " \t\n\r")
  13098. (beginning-of-line 2)
  13099. (goto-char (min (point) pos))
  13100. (count-lines (point) pos)))
  13101. (defun org-skip-whitespace ()
  13102. (skip-chars-forward " \t\n\r"))
  13103. (defun org-point-in-group (point group &optional context)
  13104. "Check if POINT is in match-group GROUP.
  13105. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13106. match. If the match group does ot exist or point is not inside it,
  13107. return nil."
  13108. (and (match-beginning group)
  13109. (>= point (match-beginning group))
  13110. (<= point (match-end group))
  13111. (if context
  13112. (list context (match-beginning group) (match-end group))
  13113. t)))
  13114. (defun org-switch-to-buffer-other-window (&rest args)
  13115. "Switch to buffer in a second window on the current frame.
  13116. In particular, do not allow pop-up frames."
  13117. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13118. special-display-function)
  13119. (apply 'switch-to-buffer-other-window args)))
  13120. (defun org-combine-plists (&rest plists)
  13121. "Create a single property list from all plists in PLISTS.
  13122. The process starts by copying the first list, and then setting properties
  13123. from the other lists. Settings in the last list are the most significant
  13124. ones and overrule settings in the other lists."
  13125. (let ((rtn (copy-sequence (pop plists)))
  13126. p v ls)
  13127. (while plists
  13128. (setq ls (pop plists))
  13129. (while ls
  13130. (setq p (pop ls) v (pop ls))
  13131. (setq rtn (plist-put rtn p v))))
  13132. rtn))
  13133. (defun org-move-line-down (arg)
  13134. "Move the current line down. With prefix argument, move it past ARG lines."
  13135. (interactive "p")
  13136. (let ((col (current-column))
  13137. beg end pos)
  13138. (beginning-of-line 1) (setq beg (point))
  13139. (beginning-of-line 2) (setq end (point))
  13140. (beginning-of-line (+ 1 arg))
  13141. (setq pos (move-marker (make-marker) (point)))
  13142. (insert (delete-and-extract-region beg end))
  13143. (goto-char pos)
  13144. (org-move-to-column col)))
  13145. (defun org-move-line-up (arg)
  13146. "Move the current line up. With prefix argument, move it past ARG lines."
  13147. (interactive "p")
  13148. (let ((col (current-column))
  13149. beg end pos)
  13150. (beginning-of-line 1) (setq beg (point))
  13151. (beginning-of-line 2) (setq end (point))
  13152. (beginning-of-line (- arg))
  13153. (setq pos (move-marker (make-marker) (point)))
  13154. (insert (delete-and-extract-region beg end))
  13155. (goto-char pos)
  13156. (org-move-to-column col)))
  13157. (defun org-replace-escapes (string table)
  13158. "Replace %-escapes in STRING with values in TABLE.
  13159. TABLE is an association list with keys like \"%a\" and string values.
  13160. The sequences in STRING may contain normal field width and padding information,
  13161. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13162. so values can contain further %-escapes if they are define later in TABLE."
  13163. (let ((case-fold-search nil)
  13164. e re rpl)
  13165. (while (setq e (pop table))
  13166. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13167. (while (string-match re string)
  13168. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13169. (cdr e)))
  13170. (setq string (replace-match rpl t t string))))
  13171. string))
  13172. (defun org-sublist (list start end)
  13173. "Return a section of LIST, from START to END.
  13174. Counting starts at 1."
  13175. (let (rtn (c start))
  13176. (setq list (nthcdr (1- start) list))
  13177. (while (and list (<= c end))
  13178. (push (pop list) rtn)
  13179. (setq c (1+ c)))
  13180. (nreverse rtn)))
  13181. (defun org-find-base-buffer-visiting (file)
  13182. "Like `find-buffer-visiting' but alway return the base buffer and
  13183. not an indirect buffer."
  13184. (let ((buf (find-buffer-visiting file)))
  13185. (if buf
  13186. (or (buffer-base-buffer buf) buf)
  13187. nil)))
  13188. (defun org-image-file-name-regexp (&optional extensions)
  13189. "Return regexp matching the file names of images.
  13190. If EXTENSIONS is given, only match these."
  13191. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13192. (image-file-name-regexp)
  13193. (let ((image-file-name-extensions
  13194. (or extensions
  13195. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13196. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13197. (concat "\\."
  13198. (regexp-opt (nconc (mapcar 'upcase
  13199. image-file-name-extensions)
  13200. image-file-name-extensions)
  13201. t)
  13202. "\\'"))))
  13203. (defun org-file-image-p (file &optional extensions)
  13204. "Return non-nil if FILE is an image."
  13205. (save-match-data
  13206. (string-match (org-image-file-name-regexp extensions) file)))
  13207. (defun org-get-cursor-date ()
  13208. "Return the date at cursor in as a time.
  13209. This works in the calendar and in the agenda, anywhere else it just
  13210. returns the current time."
  13211. (let (date day defd)
  13212. (cond
  13213. ((eq major-mode 'calendar-mode)
  13214. (setq date (calendar-cursor-to-date)
  13215. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13216. ((eq major-mode 'org-agenda-mode)
  13217. (setq day (get-text-property (point) 'day))
  13218. (if day
  13219. (setq date (calendar-gregorian-from-absolute day)
  13220. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13221. (nth 2 date))))))
  13222. (or defd (current-time))))
  13223. (defvar org-agenda-action-marker (make-marker)
  13224. "Marker pointing to the entry for the next agenda action.")
  13225. (defun org-mark-entry-for-agenda-action ()
  13226. "Mark the current entry as target of an agenda action.
  13227. Agenda actions are actions executed from the agenda with the key `k',
  13228. which make use of the date at the cursor."
  13229. (interactive)
  13230. (move-marker org-agenda-action-marker
  13231. (save-excursion (org-back-to-heading t) (point))
  13232. (current-buffer))
  13233. (message
  13234. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13235. ;;; Paragraph filling stuff.
  13236. ;; We want this to be just right, so use the full arsenal.
  13237. (defun org-indent-line-function ()
  13238. "Indent line like previous, but further if previous was headline or item."
  13239. (interactive)
  13240. (let* ((pos (point))
  13241. (itemp (org-at-item-p))
  13242. column bpos bcol tpos tcol bullet btype bullet-type)
  13243. ;; Find the previous relevant line
  13244. (beginning-of-line 1)
  13245. (cond
  13246. ((looking-at "#") (setq column 0))
  13247. ((looking-at "\\*+ ") (setq column 0))
  13248. (t
  13249. (beginning-of-line 0)
  13250. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13251. (beginning-of-line 0))
  13252. (cond
  13253. ((looking-at "\\*+[ \t]+")
  13254. (if (not org-adapt-indentation)
  13255. (setq column 0)
  13256. (goto-char (match-end 0))
  13257. (setq column (current-column))))
  13258. ((org-in-item-p)
  13259. (org-beginning-of-item)
  13260. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13261. (setq bpos (match-beginning 1) tpos (match-end 0)
  13262. bcol (progn (goto-char bpos) (current-column))
  13263. tcol (progn (goto-char tpos) (current-column))
  13264. bullet (match-string 1)
  13265. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13266. (if (> tcol (+ bcol org-description-max-indent))
  13267. (setq tcol (+ bcol 5)))
  13268. (if (not itemp)
  13269. (setq column tcol)
  13270. (goto-char pos)
  13271. (beginning-of-line 1)
  13272. (if (looking-at "\\S-")
  13273. (progn
  13274. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13275. (setq bullet (match-string 1)
  13276. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13277. (setq column (if (equal btype bullet-type) bcol tcol)))
  13278. (setq column (org-get-indentation)))))
  13279. (t (setq column (org-get-indentation))))))
  13280. (goto-char pos)
  13281. (if (<= (current-column) (current-indentation))
  13282. (org-indent-line-to column)
  13283. (save-excursion (org-indent-line-to column)))
  13284. (setq column (current-column))
  13285. (beginning-of-line 1)
  13286. (if (looking-at
  13287. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13288. (replace-match (concat "\\1" (format org-property-format
  13289. (match-string 2) (match-string 3)))
  13290. t nil))
  13291. (org-move-to-column column)))
  13292. (defun org-set-autofill-regexps ()
  13293. (interactive)
  13294. ;; In the paragraph separator we include headlines, because filling
  13295. ;; text in a line directly attached to a headline would otherwise
  13296. ;; fill the headline as well.
  13297. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13298. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13299. ;; The paragraph starter includes hand-formatted lists.
  13300. (org-set-local 'paragraph-start
  13301. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13302. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13303. ;; But only if the user has not turned off tables or fixed-width regions
  13304. (org-set-local
  13305. 'auto-fill-inhibit-regexp
  13306. (concat "\\*+ \\|#\\+"
  13307. "\\|[ \t]*" org-keyword-time-regexp
  13308. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13309. (concat
  13310. "\\|[ \t]*["
  13311. (if org-enable-table-editor "|" "")
  13312. (if org-enable-fixed-width-editor ":" "")
  13313. "]"))))
  13314. ;; We use our own fill-paragraph function, to make sure that tables
  13315. ;; and fixed-width regions are not wrapped. That function will pass
  13316. ;; through to `fill-paragraph' when appropriate.
  13317. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13318. ; Adaptive filling: To get full control, first make sure that
  13319. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13320. (org-set-local 'adaptive-fill-regexp "\000")
  13321. (org-set-local 'adaptive-fill-function
  13322. 'org-adaptive-fill-function)
  13323. (org-set-local
  13324. 'align-mode-rules-list
  13325. '((org-in-buffer-settings
  13326. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13327. (modes . '(org-mode))))))
  13328. (defun org-fill-paragraph (&optional justify)
  13329. "Re-align a table, pass through to fill-paragraph if no table."
  13330. (let ((table-p (org-at-table-p))
  13331. (table.el-p (org-at-table.el-p)))
  13332. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13333. (save-excursion (goto-char (point-at-bol))
  13334. (looking-at outline-regexp)))
  13335. t) ; skip headlines
  13336. (table.el-p t) ; skip table.el tables
  13337. (table-p (org-table-align) t) ; align org-mode tables
  13338. (t nil)))) ; call paragraph-fill
  13339. ;; For reference, this is the default value of adaptive-fill-regexp
  13340. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13341. (defun org-adaptive-fill-function ()
  13342. "Return a fill prefix for org-mode files.
  13343. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13344. work correctly."
  13345. (cond ((looking-at "#[ \t]+")
  13346. (match-string 0))
  13347. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13348. (save-excursion
  13349. (if (> (match-end 1) (+ (match-beginning 1)
  13350. org-description-max-indent))
  13351. (goto-char (+ (match-beginning 1) 5))
  13352. (goto-char (match-end 0)))
  13353. (make-string (current-column) ?\ )))
  13354. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13355. (save-excursion
  13356. (goto-char (match-end 0))
  13357. (make-string (current-column) ?\ )))
  13358. (t nil)))
  13359. ;;; Other stuff.
  13360. (defun org-toggle-fixed-width-section (arg)
  13361. "Toggle the fixed-width export.
  13362. If there is no active region, the QUOTE keyword at the current headline is
  13363. inserted or removed. When present, it causes the text between this headline
  13364. and the next to be exported as fixed-width text, and unmodified.
  13365. If there is an active region, this command adds or removes a colon as the
  13366. first character of this line. If the first character of a line is a colon,
  13367. this line is also exported in fixed-width font."
  13368. (interactive "P")
  13369. (let* ((cc 0)
  13370. (regionp (org-region-active-p))
  13371. (beg (if regionp (region-beginning) (point)))
  13372. (end (if regionp (region-end)))
  13373. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13374. (case-fold-search nil)
  13375. (re "[ \t]*\\(:\\)")
  13376. off)
  13377. (if regionp
  13378. (save-excursion
  13379. (goto-char beg)
  13380. (setq cc (current-column))
  13381. (beginning-of-line 1)
  13382. (setq off (looking-at re))
  13383. (while (> nlines 0)
  13384. (setq nlines (1- nlines))
  13385. (beginning-of-line 1)
  13386. (cond
  13387. (arg
  13388. (org-move-to-column cc t)
  13389. (insert ":\n")
  13390. (forward-line -1))
  13391. ((and off (looking-at re))
  13392. (replace-match "" t t nil 1))
  13393. ((not off) (org-move-to-column cc t) (insert ":")))
  13394. (forward-line 1)))
  13395. (save-excursion
  13396. (org-back-to-heading)
  13397. (if (looking-at (concat outline-regexp
  13398. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13399. (replace-match "" t t nil 1)
  13400. (if (looking-at outline-regexp)
  13401. (progn
  13402. (goto-char (match-end 0))
  13403. (insert org-quote-string " "))))))))
  13404. ;;;; Functions extending outline functionality
  13405. (defun org-beginning-of-line (&optional arg)
  13406. "Go to the beginning of the current line. If that is invisible, continue
  13407. to a visible line beginning. This makes the function of C-a more intuitive.
  13408. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13409. first attempt, and only move to after the tags when the cursor is already
  13410. beyond the end of the headline."
  13411. (interactive "P")
  13412. (let ((pos (point)) refpos)
  13413. (beginning-of-line 1)
  13414. (if (bobp)
  13415. nil
  13416. (backward-char 1)
  13417. (if (org-invisible-p)
  13418. (while (and (not (bobp)) (org-invisible-p))
  13419. (backward-char 1)
  13420. (beginning-of-line 1))
  13421. (forward-char 1)))
  13422. (when org-special-ctrl-a/e
  13423. (cond
  13424. ((and (looking-at org-complex-heading-regexp)
  13425. (= (char-after (match-end 1)) ?\ ))
  13426. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13427. (point-at-eol)))
  13428. (goto-char
  13429. (if (eq org-special-ctrl-a/e t)
  13430. (cond ((> pos refpos) refpos)
  13431. ((= pos (point)) refpos)
  13432. (t (point)))
  13433. (cond ((> pos (point)) (point))
  13434. ((not (eq last-command this-command)) (point))
  13435. (t refpos)))))
  13436. ((org-at-item-p)
  13437. (goto-char
  13438. (if (eq org-special-ctrl-a/e t)
  13439. (cond ((> pos (match-end 4)) (match-end 4))
  13440. ((= pos (point)) (match-end 4))
  13441. (t (point)))
  13442. (cond ((> pos (point)) (point))
  13443. ((not (eq last-command this-command)) (point))
  13444. (t (match-end 4))))))))
  13445. (org-no-warnings
  13446. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13447. (defun org-end-of-line (&optional arg)
  13448. "Go to the end of the line.
  13449. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13450. first attempt, and only move to after the tags when the cursor is already
  13451. beyond the end of the headline."
  13452. (interactive "P")
  13453. (if (or (not org-special-ctrl-a/e)
  13454. (not (org-on-heading-p)))
  13455. (end-of-line arg)
  13456. (let ((pos (point)))
  13457. (beginning-of-line 1)
  13458. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13459. (if (eq org-special-ctrl-a/e t)
  13460. (if (or (< pos (match-beginning 1))
  13461. (= pos (match-end 0)))
  13462. (goto-char (match-beginning 1))
  13463. (goto-char (match-end 0)))
  13464. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13465. (goto-char (match-end 0))
  13466. (goto-char (match-beginning 1))))
  13467. (end-of-line arg))))
  13468. (org-no-warnings
  13469. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13470. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13471. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13472. (defun org-kill-line (&optional arg)
  13473. "Kill line, to tags or end of line."
  13474. (interactive "P")
  13475. (cond
  13476. ((or (not org-special-ctrl-k)
  13477. (bolp)
  13478. (not (org-on-heading-p)))
  13479. (call-interactively 'kill-line))
  13480. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13481. (kill-region (point) (match-beginning 1))
  13482. (org-set-tags nil t))
  13483. (t (kill-region (point) (point-at-eol)))))
  13484. (define-key org-mode-map "\C-k" 'org-kill-line)
  13485. (defun org-yank (&optional arg)
  13486. "Yank. If the kill is a subtree, treat it specially.
  13487. This command will look at the current kill and check if is a single
  13488. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13489. cursor is at the beginning of a line or after the stars of a currently
  13490. empty headline, then the yank is handled specially. How exactly depends
  13491. on the value of the following variables, both set by default.
  13492. org-yank-folded-subtrees
  13493. When set, the subtree(s) will be folded after insertion, but only
  13494. if doing so would now swallow text after the yanked text.
  13495. org-yank-adjusted-subtrees
  13496. When set, the subtree will be promoted or demoted in order to
  13497. fit into the local outline tree structure, which means that the level
  13498. will be adjusted so that it becomes the smaller one of the two
  13499. *visible* surrounding headings.
  13500. Any prefix to this command will cause `yank' to be called directly with
  13501. no special treatment. In particular, a simple `C-u' prefix will just
  13502. plainly yank the text as it is.
  13503. \[1] Basically, the test checks if the first non-white line is a heading
  13504. and if there are no other headings with fewer stars."
  13505. (interactive "P")
  13506. (setq this-command 'yank)
  13507. (if arg
  13508. (call-interactively 'yank)
  13509. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13510. (and (org-kill-is-subtree-p)
  13511. (or (bolp)
  13512. (and (looking-at "[ \t]*$")
  13513. (string-match
  13514. "\\`\\*+\\'"
  13515. (buffer-substring (point-at-bol) (point)))))))
  13516. swallowp)
  13517. (cond
  13518. ((and subtreep org-yank-folded-subtrees)
  13519. (let ((beg (point))
  13520. end)
  13521. (if (and subtreep org-yank-adjusted-subtrees)
  13522. (org-paste-subtree nil nil 'for-yank)
  13523. (call-interactively 'yank))
  13524. (setq end (point))
  13525. (goto-char beg)
  13526. (when (and (bolp) subtreep
  13527. (not (setq swallowp
  13528. (org-yank-folding-would-swallow-text beg end))))
  13529. (or (looking-at outline-regexp)
  13530. (re-search-forward (concat "^" outline-regexp) end t))
  13531. (while (and (< (point) end) (looking-at outline-regexp))
  13532. (hide-subtree)
  13533. (org-cycle-show-empty-lines 'folded)
  13534. (condition-case nil
  13535. (outline-forward-same-level 1)
  13536. (error (goto-char end)))))
  13537. (when swallowp
  13538. (message
  13539. "Yanked text not folded because that would swallow text"))
  13540. (goto-char end)
  13541. (skip-chars-forward " \t\n\r")
  13542. (beginning-of-line 1)
  13543. (push-mark beg 'nomsg)))
  13544. ((and subtreep org-yank-adjusted-subtrees)
  13545. (let ((beg (point-at-bol)))
  13546. (org-paste-subtree nil nil 'for-yank)
  13547. (push-mark beg 'nomsg)))
  13548. (t
  13549. (call-interactively 'yank))))))
  13550. (defun org-yank-folding-would-swallow-text (beg end)
  13551. "Would hide-subtree at BEG swallow any text after END?"
  13552. (let (level)
  13553. (save-excursion
  13554. (goto-char beg)
  13555. (when (or (looking-at outline-regexp)
  13556. (re-search-forward (concat "^" outline-regexp) end t))
  13557. (setq level (org-outline-level)))
  13558. (goto-char end)
  13559. (skip-chars-forward " \t\r\n\v\f")
  13560. (if (or (eobp)
  13561. (and (bolp) (looking-at org-outline-regexp)
  13562. (<= (org-outline-level) level)))
  13563. nil ; Nothing would be swallowed
  13564. t)))) ; something would swallow
  13565. (define-key org-mode-map "\C-y" 'org-yank)
  13566. (defun org-invisible-p ()
  13567. "Check if point is at a character currently not visible."
  13568. ;; Early versions of noutline don't have `outline-invisible-p'.
  13569. (if (fboundp 'outline-invisible-p)
  13570. (outline-invisible-p)
  13571. (get-char-property (point) 'invisible)))
  13572. (defun org-invisible-p2 ()
  13573. "Check if point is at a character currently not visible."
  13574. (save-excursion
  13575. (if (and (eolp) (not (bobp))) (backward-char 1))
  13576. ;; Early versions of noutline don't have `outline-invisible-p'.
  13577. (if (fboundp 'outline-invisible-p)
  13578. (outline-invisible-p)
  13579. (get-char-property (point) 'invisible))))
  13580. (defun org-back-to-heading (&optional invisible-ok)
  13581. "Call `outline-back-to-heading', but provide a better error message."
  13582. (condition-case nil
  13583. (outline-back-to-heading invisible-ok)
  13584. (error (error "Before first headline at position %d in buffer %s"
  13585. (point) (current-buffer)))))
  13586. (defun org-before-first-heading-p ()
  13587. "Before first heading?"
  13588. (save-excursion
  13589. (null (re-search-backward "^\\*+ " nil t))))
  13590. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13591. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13592. (defun org-at-heading-or-item-p ()
  13593. (or (org-on-heading-p) (org-at-item-p)))
  13594. (defun org-on-target-p ()
  13595. (or (org-in-regexp org-radio-target-regexp)
  13596. (org-in-regexp org-target-regexp)))
  13597. (defun org-up-heading-all (arg)
  13598. "Move to the heading line of which the present line is a subheading.
  13599. This function considers both visible and invisible heading lines.
  13600. With argument, move up ARG levels."
  13601. (if (fboundp 'outline-up-heading-all)
  13602. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13603. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13604. (defun org-up-heading-safe ()
  13605. "Move to the heading line of which the present line is a subheading.
  13606. This version will not throw an error. It will return the level of the
  13607. headline found, or nil if no higher level is found."
  13608. (let (start-level re)
  13609. (org-back-to-heading t)
  13610. (setq start-level (funcall outline-level))
  13611. (if (equal start-level 1)
  13612. nil
  13613. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13614. (if (re-search-backward re nil t)
  13615. (funcall outline-level)))))
  13616. (defun org-first-sibling-p ()
  13617. "Is this heading the first child of its parents?"
  13618. (interactive)
  13619. (let ((re (concat "^" outline-regexp))
  13620. level l)
  13621. (unless (org-at-heading-p t)
  13622. (error "Not at a heading"))
  13623. (setq level (funcall outline-level))
  13624. (save-excursion
  13625. (if (not (re-search-backward re nil t))
  13626. t
  13627. (setq l (funcall outline-level))
  13628. (< l level)))))
  13629. (defun org-goto-sibling (&optional previous)
  13630. "Goto the next sibling, even if it is invisible.
  13631. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13632. when a sibling was found. When none is found, return nil and don't
  13633. move point."
  13634. (let ((fun (if previous 're-search-backward 're-search-forward))
  13635. (pos (point))
  13636. (re (concat "^" outline-regexp))
  13637. level l)
  13638. (when (condition-case nil (org-back-to-heading t) (error nil))
  13639. (setq level (funcall outline-level))
  13640. (catch 'exit
  13641. (or previous (forward-char 1))
  13642. (while (funcall fun re nil t)
  13643. (setq l (funcall outline-level))
  13644. (when (< l level) (goto-char pos) (throw 'exit nil))
  13645. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13646. (goto-char pos)
  13647. nil))))
  13648. (defun org-show-siblings ()
  13649. "Show all siblings of the current headline."
  13650. (save-excursion
  13651. (while (org-goto-sibling) (org-flag-heading nil)))
  13652. (save-excursion
  13653. (while (org-goto-sibling 'previous)
  13654. (org-flag-heading nil))))
  13655. (defun org-show-hidden-entry ()
  13656. "Show an entry where even the heading is hidden."
  13657. (save-excursion
  13658. (org-show-entry)))
  13659. (defun org-flag-heading (flag &optional entry)
  13660. "Flag the current heading. FLAG non-nil means make invisible.
  13661. When ENTRY is non-nil, show the entire entry."
  13662. (save-excursion
  13663. (org-back-to-heading t)
  13664. ;; Check if we should show the entire entry
  13665. (if entry
  13666. (progn
  13667. (org-show-entry)
  13668. (save-excursion
  13669. (and (outline-next-heading)
  13670. (org-flag-heading nil))))
  13671. (outline-flag-region (max (point-min) (1- (point)))
  13672. (save-excursion (outline-end-of-heading) (point))
  13673. flag))))
  13674. (defun org-forward-same-level (arg)
  13675. "Move forward to the ARG'th subheading at same level as this one.
  13676. Stop at the first and last subheadings of a superior heading.
  13677. This is like outline-forward-same-level, but invisible headings are ok."
  13678. (interactive "p")
  13679. (org-back-to-heading t)
  13680. (while (> arg 0)
  13681. (let ((point-to-move-to (save-excursion
  13682. (org-get-next-sibling))))
  13683. (if point-to-move-to
  13684. (progn
  13685. (goto-char point-to-move-to)
  13686. (setq arg (1- arg)))
  13687. (progn
  13688. (setq arg 0)
  13689. (error "No following same-level heading"))))))
  13690. (defun org-get-next-sibling ()
  13691. "Move to next heading of the same level, and return point.
  13692. If there is no such heading, return nil.
  13693. This is like outline-next-sibling, but invisible headings are ok."
  13694. (let ((level (funcall outline-level)))
  13695. (outline-next-heading)
  13696. (while (and (not (eobp)) (> (funcall outline-level) level))
  13697. (outline-next-heading))
  13698. (if (or (eobp) (< (funcall outline-level) level))
  13699. nil
  13700. (point))))
  13701. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13702. ;; This is an exact copy of the original function, but it uses
  13703. ;; `org-back-to-heading', to make it work also in invisible
  13704. ;; trees. And is uses an invisible-OK argument.
  13705. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13706. (org-back-to-heading invisible-OK)
  13707. (let ((first t)
  13708. (level (funcall outline-level)))
  13709. (while (and (not (eobp))
  13710. (or first (> (funcall outline-level) level)))
  13711. (setq first nil)
  13712. (outline-next-heading))
  13713. (unless to-heading
  13714. (if (memq (preceding-char) '(?\n ?\^M))
  13715. (progn
  13716. ;; Go to end of line before heading
  13717. (forward-char -1)
  13718. (if (memq (preceding-char) '(?\n ?\^M))
  13719. ;; leave blank line before heading
  13720. (forward-char -1))))))
  13721. (point))
  13722. (defun org-show-subtree ()
  13723. "Show everything after this heading at deeper levels."
  13724. (outline-flag-region
  13725. (point)
  13726. (save-excursion
  13727. (outline-end-of-subtree) (outline-next-heading) (point))
  13728. nil))
  13729. (defun org-show-entry ()
  13730. "Show the body directly following this heading.
  13731. Show the heading too, if it is currently invisible."
  13732. (interactive)
  13733. (save-excursion
  13734. (condition-case nil
  13735. (progn
  13736. (org-back-to-heading t)
  13737. (outline-flag-region
  13738. (max (point-min) (1- (point)))
  13739. (save-excursion
  13740. (re-search-forward
  13741. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13742. (or (match-beginning 1) (point-max)))
  13743. nil))
  13744. (error nil))))
  13745. (defun org-make-options-regexp (kwds)
  13746. "Make a regular expression for keyword lines."
  13747. (concat
  13748. "^"
  13749. "#?[ \t]*\\+\\("
  13750. (mapconcat 'regexp-quote kwds "\\|")
  13751. "\\):[ \t]*"
  13752. "\\(.+\\)"))
  13753. ;; Make isearch reveal the necessary context
  13754. (defun org-isearch-end ()
  13755. "Reveal context after isearch exits."
  13756. (when isearch-success ; only if search was successful
  13757. (if (featurep 'xemacs)
  13758. ;; Under XEmacs, the hook is run in the correct place,
  13759. ;; we directly show the context.
  13760. (org-show-context 'isearch)
  13761. ;; In Emacs the hook runs *before* restoring the overlays.
  13762. ;; So we have to use a one-time post-command-hook to do this.
  13763. ;; (Emacs 22 has a special variable, see function `org-mode')
  13764. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13765. isearch-mode-end-hook-quit)
  13766. ;; Only when the isearch was not quitted.
  13767. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13768. 'append 'local)))))
  13769. (defun org-isearch-post-command ()
  13770. "Remove self from hook, and show context."
  13771. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13772. (org-show-context 'isearch))
  13773. ;;;; Integration with and fixes for other packages
  13774. ;;; Imenu support
  13775. (defvar org-imenu-markers nil
  13776. "All markers currently used by Imenu.")
  13777. (make-variable-buffer-local 'org-imenu-markers)
  13778. (defun org-imenu-new-marker (&optional pos)
  13779. "Return a new marker for use by Imenu, and remember the marker."
  13780. (let ((m (make-marker)))
  13781. (move-marker m (or pos (point)))
  13782. (push m org-imenu-markers)
  13783. m))
  13784. (defun org-imenu-get-tree ()
  13785. "Produce the index for Imenu."
  13786. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13787. (setq org-imenu-markers nil)
  13788. (let* ((n org-imenu-depth)
  13789. (re (concat "^" outline-regexp))
  13790. (subs (make-vector (1+ n) nil))
  13791. (last-level 0)
  13792. m level head)
  13793. (save-excursion
  13794. (save-restriction
  13795. (widen)
  13796. (goto-char (point-max))
  13797. (while (re-search-backward re nil t)
  13798. (setq level (org-reduced-level (funcall outline-level)))
  13799. (when (<= level n)
  13800. (looking-at org-complex-heading-regexp)
  13801. (setq head (org-link-display-format
  13802. (org-match-string-no-properties 4))
  13803. m (org-imenu-new-marker))
  13804. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13805. (if (>= level last-level)
  13806. (push (cons head m) (aref subs level))
  13807. (push (cons head (aref subs (1+ level))) (aref subs level))
  13808. (loop for i from (1+ level) to n do (aset subs i nil)))
  13809. (setq last-level level)))))
  13810. (aref subs 1)))
  13811. (eval-after-load "imenu"
  13812. '(progn
  13813. (add-hook 'imenu-after-jump-hook
  13814. (lambda ()
  13815. (if (eq major-mode 'org-mode)
  13816. (org-show-context 'org-goto))))))
  13817. (defun org-link-display-format (link)
  13818. "Replace a link with either the description, or the link target
  13819. if no description is present"
  13820. (save-match-data
  13821. (if (string-match org-bracket-link-analytic-regexp link)
  13822. (replace-match (or (match-string 5 link)
  13823. (concat (match-string 1 link)
  13824. (match-string 3 link)))
  13825. nil nil link)
  13826. link)))
  13827. ;; Speedbar support
  13828. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13829. "Overlay marking the agenda restriction line in speedbar.")
  13830. (org-overlay-put org-speedbar-restriction-lock-overlay
  13831. 'face 'org-agenda-restriction-lock)
  13832. (org-overlay-put org-speedbar-restriction-lock-overlay
  13833. 'help-echo "Agendas are currently limited to this item.")
  13834. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13835. (defun org-speedbar-set-agenda-restriction ()
  13836. "Restrict future agenda commands to the location at point in speedbar.
  13837. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13838. (interactive)
  13839. (require 'org-agenda)
  13840. (let (p m tp np dir txt)
  13841. (cond
  13842. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13843. 'org-imenu t))
  13844. (setq m (get-text-property p 'org-imenu-marker))
  13845. (save-excursion
  13846. (save-restriction
  13847. (set-buffer (marker-buffer m))
  13848. (goto-char m)
  13849. (org-agenda-set-restriction-lock 'subtree))))
  13850. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13851. 'speedbar-function 'speedbar-find-file))
  13852. (setq tp (previous-single-property-change
  13853. (1+ p) 'speedbar-function)
  13854. np (next-single-property-change
  13855. tp 'speedbar-function)
  13856. dir (speedbar-line-directory)
  13857. txt (buffer-substring-no-properties (or tp (point-min))
  13858. (or np (point-max))))
  13859. (save-excursion
  13860. (save-restriction
  13861. (set-buffer (find-file-noselect
  13862. (let ((default-directory dir))
  13863. (expand-file-name txt))))
  13864. (unless (org-mode-p)
  13865. (error "Cannot restrict to non-Org-mode file"))
  13866. (org-agenda-set-restriction-lock 'file))))
  13867. (t (error "Don't know how to restrict Org-mode's agenda")))
  13868. (org-move-overlay org-speedbar-restriction-lock-overlay
  13869. (point-at-bol) (point-at-eol))
  13870. (setq current-prefix-arg nil)
  13871. (org-agenda-maybe-redo)))
  13872. (eval-after-load "speedbar"
  13873. '(progn
  13874. (speedbar-add-supported-extension ".org")
  13875. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13876. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13877. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13878. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13879. (add-hook 'speedbar-visiting-tag-hook
  13880. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13881. ;;; Fixes and Hacks for problems with other packages
  13882. ;; Make flyspell not check words in links, to not mess up our keymap
  13883. (defun org-mode-flyspell-verify ()
  13884. "Don't let flyspell put overlays at active buttons."
  13885. (not (get-text-property (point) 'keymap)))
  13886. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13887. (eval-after-load "bookmark"
  13888. '(if (boundp 'bookmark-after-jump-hook)
  13889. ;; We can use the hook
  13890. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13891. ;; Hook not available, use advice
  13892. (defadvice bookmark-jump (after org-make-visible activate)
  13893. "Make the position visible."
  13894. (org-bookmark-jump-unhide))))
  13895. ;; Make sure saveplace show the location if it was hidden
  13896. (eval-after-load "saveplace"
  13897. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13898. "Make the position visible."
  13899. (org-bookmark-jump-unhide)))
  13900. (defun org-bookmark-jump-unhide ()
  13901. "Unhide the current position, to show the bookmark location."
  13902. (and (org-mode-p)
  13903. (or (org-invisible-p)
  13904. (save-excursion (goto-char (max (point-min) (1- (point))))
  13905. (org-invisible-p)))
  13906. (org-show-context 'bookmark-jump)))
  13907. ;; Make session.el ignore our circular variable
  13908. (eval-after-load "session"
  13909. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13910. ;;;; Experimental code
  13911. (defun org-closed-in-range ()
  13912. "Sparse tree of items closed in a certain time range.
  13913. Still experimental, may disappear in the future."
  13914. (interactive)
  13915. ;; Get the time interval from the user.
  13916. (let* ((time1 (time-to-seconds
  13917. (org-read-date nil 'to-time nil "Starting date: ")))
  13918. (time2 (time-to-seconds
  13919. (org-read-date nil 'to-time nil "End date:")))
  13920. ;; callback function
  13921. (callback (lambda ()
  13922. (let ((time
  13923. (time-to-seconds
  13924. (apply 'encode-time
  13925. (org-parse-time-string
  13926. (match-string 1))))))
  13927. ;; check if time in interval
  13928. (and (>= time time1) (<= time time2))))))
  13929. ;; make tree, check each match with the callback
  13930. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13931. ;;;; Finish up
  13932. (provide 'org)
  13933. (run-hooks 'org-load-hook)
  13934. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13935. ;;; org.el ends here